blob: cdba4f03d465396c20693c2e954a5d8869dbe3b0 [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,
215 * seeding, structure cloning, openning/closing devices at mount/umount time
216 *
217 * global::fs_devs - add, remove, updates to the global list
218 *
219 * does not protect: manipulation of the fs_devices::devices list!
220 *
221 * btrfs_device::name - renames (write side), read is RCU
222 *
223 * fs_devices::device_list_mutex (per-fs, with RCU)
224 * ------------------------------------------------
225 * protects updates to fs_devices::devices, ie. adding and deleting
226 *
227 * simple list traversal with read-only actions can be done with RCU protection
228 *
229 * may be used to exclude some operations from running concurrently without any
230 * modifications to the list (see write_all_supers)
231 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200232 * balance_mutex
233 * -------------
234 * protects balance structures (status, state) and context accessed from
235 * several places (internally, ioctl)
236 *
237 * chunk_mutex
238 * -----------
239 * protects chunks, adding or removing during allocation, trim or when a new
240 * device is added/removed
241 *
242 * cleaner_mutex
243 * -------------
244 * a big lock that is held by the cleaner thread and prevents running subvolume
245 * cleaning together with relocation or delayed iputs
246 *
247 *
248 * Lock nesting
249 * ============
250 *
251 * uuid_mutex
252 * volume_mutex
253 * device_list_mutex
254 * chunk_mutex
255 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800256 *
257 *
258 * Exclusive operations, BTRFS_FS_EXCL_OP
259 * ======================================
260 *
261 * Maintains the exclusivity of the following operations that apply to the
262 * whole filesystem and cannot run in parallel.
263 *
264 * - Balance (*)
265 * - Device add
266 * - Device remove
267 * - Device replace (*)
268 * - Resize
269 *
270 * The device operations (as above) can be in one of the following states:
271 *
272 * - Running state
273 * - Paused state
274 * - Completed state
275 *
276 * Only device operations marked with (*) can go into the Paused state for the
277 * following reasons:
278 *
279 * - ioctl (only Balance can be Paused through ioctl)
280 * - filesystem remounted as read-only
281 * - filesystem unmounted and mounted as read-only
282 * - system power-cycle and filesystem mounted as read-only
283 * - filesystem or device errors leading to forced read-only
284 *
285 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
286 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
287 * A device operation in Paused or Running state can be canceled or resumed
288 * either by ioctl (Balance only) or when remounted as read-write.
289 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
290 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200291 */
292
Miao Xie67a2c452014-09-03 21:35:43 +0800293DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400294static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800295struct list_head *btrfs_get_fs_uuids(void)
296{
297 return &fs_uuids;
298}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400299
David Sterba2dfeca92017-06-14 02:48:07 +0200300/*
301 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200302 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
303 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200304 *
305 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
306 * The returned struct is not linked onto any lists and can be destroyed with
307 * kfree() right away.
308 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200309static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
310 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300311{
312 struct btrfs_fs_devices *fs_devs;
313
David Sterba78f2c9e2016-02-11 14:25:38 +0100314 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300315 if (!fs_devs)
316 return ERR_PTR(-ENOMEM);
317
318 mutex_init(&fs_devs->device_list_mutex);
319
320 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800321 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300322 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800323 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300324 if (fsid)
325 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300326
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200327 if (metadata_fsid)
328 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
329 else if (fsid)
330 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
331
Ilya Dryomov2208a372013-08-12 14:33:03 +0300332 return fs_devs;
333}
334
David Sterbaa425f9d2018-03-20 15:47:33 +0100335void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100336{
337 rcu_string_free(device->name);
338 bio_put(device->flush_bio);
339 kfree(device);
340}
341
Yan Zhenge4404d62008-12-12 10:03:26 -0500342static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
343{
344 struct btrfs_device *device;
345 WARN_ON(fs_devices->opened);
346 while (!list_empty(&fs_devices->devices)) {
347 device = list_entry(fs_devices->devices.next,
348 struct btrfs_device, dev_list);
349 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100350 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500351 }
352 kfree(fs_devices);
353}
354
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000355static void btrfs_kobject_uevent(struct block_device *bdev,
356 enum kobject_action action)
357{
358 int ret;
359
360 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
361 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500362 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000363 action,
364 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
365 &disk_to_dev(bdev->bd_disk)->kobj);
366}
367
David Sterbaffc5a372018-02-19 17:24:15 +0100368void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400369{
370 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400371
Yan Zheng2b820322008-11-17 21:11:30 -0500372 while (!list_empty(&fs_uuids)) {
373 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800374 struct btrfs_fs_devices, fs_list);
375 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500376 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400377 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400378}
379
David Sterba48dae9c2017-10-30 18:10:25 +0100380/*
381 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
382 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100383 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100384 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300385static struct btrfs_device *__alloc_device(void)
386{
387 struct btrfs_device *dev;
388
David Sterba78f2c9e2016-02-11 14:25:38 +0100389 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300390 if (!dev)
391 return ERR_PTR(-ENOMEM);
392
David Sterbae0ae9992017-06-06 17:06:06 +0200393 /*
394 * Preallocate a bio that's always going to be used for flushing device
395 * barriers and matches the device lifespan
396 */
397 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
398 if (!dev->flush_bio) {
399 kfree(dev);
400 return ERR_PTR(-ENOMEM);
401 }
David Sterbae0ae9992017-06-06 17:06:06 +0200402
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300403 INIT_LIST_HEAD(&dev->dev_list);
404 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800405 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300406
407 spin_lock_init(&dev->io_lock);
408
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300409 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800410 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100411 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700412 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800413 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300414
415 return dev;
416}
417
David Sterba35c70102017-06-15 19:51:51 +0200418/*
419 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
420 * return NULL.
421 *
422 * If devid and uuid are both specified, the match must be exact, otherwise
423 * only devid is used.
424 */
425static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
426 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427{
428 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400429
Anand Jain636d2c92018-04-12 10:29:29 +0800430 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400431 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400432 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400433 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400434 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400435 }
436 return NULL;
437}
438
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200439static noinline struct btrfs_fs_devices *find_fsid(
440 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400441{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400442 struct btrfs_fs_devices *fs_devices;
443
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200444 ASSERT(fsid);
445
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200446 if (metadata_fsid) {
447 /*
448 * Handle scanned device having completed its fsid change but
449 * belonging to a fs_devices that was created by first scanning
450 * a device which didn't have its fsid/metadata_uuid changed
451 * at all and the CHANGING_FSID_V2 flag set.
452 */
453 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
454 if (fs_devices->fsid_change &&
455 memcmp(metadata_fsid, fs_devices->fsid,
456 BTRFS_FSID_SIZE) == 0 &&
457 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
458 BTRFS_FSID_SIZE) == 0) {
459 return fs_devices;
460 }
461 }
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200462 /*
463 * Handle scanned device having completed its fsid change but
464 * belonging to a fs_devices that was created by a device that
465 * has an outdated pair of fsid/metadata_uuid and
466 * CHANGING_FSID_V2 flag set.
467 */
468 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
469 if (fs_devices->fsid_change &&
470 memcmp(fs_devices->metadata_uuid,
471 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
472 memcmp(metadata_fsid, fs_devices->metadata_uuid,
473 BTRFS_FSID_SIZE) == 0) {
474 return fs_devices;
475 }
476 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200477 }
478
479 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800480 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200481 if (metadata_fsid) {
482 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
483 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
484 BTRFS_FSID_SIZE) == 0)
485 return fs_devices;
486 } else {
487 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
488 return fs_devices;
489 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400490 }
491 return NULL;
492}
493
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100494static int
495btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
496 int flush, struct block_device **bdev,
497 struct buffer_head **bh)
498{
499 int ret;
500
501 *bdev = blkdev_get_by_path(device_path, flags, holder);
502
503 if (IS_ERR(*bdev)) {
504 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100505 goto error;
506 }
507
508 if (flush)
509 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200510 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100511 if (ret) {
512 blkdev_put(*bdev, flags);
513 goto error;
514 }
515 invalidate_bdev(*bdev);
516 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800517 if (IS_ERR(*bh)) {
518 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100519 blkdev_put(*bdev, flags);
520 goto error;
521 }
522
523 return 0;
524
525error:
526 *bdev = NULL;
527 *bh = NULL;
528 return ret;
529}
530
Chris Masonffbd5172009-04-20 15:50:09 -0400531static void requeue_list(struct btrfs_pending_bios *pending_bios,
532 struct bio *head, struct bio *tail)
533{
534
535 struct bio *old_head;
536
537 old_head = pending_bios->head;
538 pending_bios->head = head;
539 if (pending_bios->tail)
540 tail->bi_next = old_head;
541 else
542 pending_bios->tail = tail;
543}
544
Chris Mason8b712842008-06-11 16:50:36 -0400545/*
546 * we try to collect pending bios for a device so we don't get a large
547 * number of procs sending bios down to the same device. This greatly
548 * improves the schedulers ability to collect and merge the bios.
549 *
550 * But, it also turns into a long list of bios to process and that is sure
551 * to eventually make the worker thread block. The solution here is to
552 * make some progress and then put this work struct back at the end of
553 * the list if the block device is congested. This way, multiple devices
554 * can make progress from a single worker thread.
555 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100556static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400558 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400559 struct bio *pending;
560 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400561 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400562 struct bio *tail;
563 struct bio *cur;
564 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400565 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400566 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400567 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400568 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000569 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400570 struct blk_plug plug;
571
572 /*
573 * this function runs all the bios we've collected for
574 * a particular device. We don't want to wander off to
575 * another device without first sending all of these down.
576 * So, setup a plug here and finish it off before we return
577 */
578 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400579
Jan Karaefa7c9f2017-02-02 15:56:53 +0100580 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400581
Chris Mason8b712842008-06-11 16:50:36 -0400582loop:
583 spin_lock(&device->io_lock);
584
Chris Masona6837052009-02-04 09:19:41 -0500585loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400586 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400587
Chris Mason8b712842008-06-11 16:50:36 -0400588 /* take all the bios off the list at once and process them
589 * later on (without the lock held). But, remember the
590 * tail and other pointers so the bios can be properly reinserted
591 * into the list if we hit congestion
592 */
Chris Masond84275c2009-06-09 15:39:08 -0400593 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400594 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400595 force_reg = 1;
596 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400597 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400598 force_reg = 0;
599 }
Chris Masonffbd5172009-04-20 15:50:09 -0400600
601 pending = pending_bios->head;
602 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400603 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400604
605 /*
606 * if pending was null this time around, no bios need processing
607 * at all and we can stop. Otherwise it'll loop back up again
608 * and do an additional check so no bios are missed.
609 *
610 * device->running_pending is used to synchronize with the
611 * schedule_bio code.
612 */
Chris Masonffbd5172009-04-20 15:50:09 -0400613 if (device->pending_sync_bios.head == NULL &&
614 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400615 again = 0;
616 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400617 } else {
618 again = 1;
619 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400620 }
Chris Masonffbd5172009-04-20 15:50:09 -0400621
622 pending_bios->head = NULL;
623 pending_bios->tail = NULL;
624
Chris Mason8b712842008-06-11 16:50:36 -0400625 spin_unlock(&device->io_lock);
626
Chris Masond3977122009-01-05 21:25:51 -0500627 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400628
629 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400630 /* we want to work on both lists, but do more bios on the
631 * sync list than the regular list
632 */
633 if ((num_run > 32 &&
634 pending_bios != &device->pending_sync_bios &&
635 device->pending_sync_bios.head) ||
636 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
637 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400638 spin_lock(&device->io_lock);
639 requeue_list(pending_bios, pending, tail);
640 goto loop_lock;
641 }
642
Chris Mason8b712842008-06-11 16:50:36 -0400643 cur = pending;
644 pending = pending->bi_next;
645 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400646
Jens Axboedac56212015-04-17 16:23:59 -0600647 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400648
Chris Mason2ab1ba62011-08-04 14:28:36 -0400649 /*
650 * if we're doing the sync list, record that our
651 * plug has some sync requests on it
652 *
653 * If we're doing the regular list and there are
654 * sync requests sitting around, unplug before
655 * we add more
656 */
657 if (pending_bios == &device->pending_sync_bios) {
658 sync_pending = 1;
659 } else if (sync_pending) {
660 blk_finish_plug(&plug);
661 blk_start_plug(&plug);
662 sync_pending = 0;
663 }
664
Mike Christie4e49ea42016-06-05 14:31:41 -0500665 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400666 num_run++;
667 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100668
669 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400670
671 /*
672 * we made progress, there is more work to do and the bdi
673 * is now congested. Back off and let other work structs
674 * run instead
675 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400676 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500677 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400678 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400679
Chris Masonb765ead2009-04-03 10:27:10 -0400680 ioc = current->io_context;
681
682 /*
683 * the main goal here is that we don't want to
684 * block if we're going to be able to submit
685 * more requests without blocking.
686 *
687 * This code does two great things, it pokes into
688 * the elevator code from a filesystem _and_
689 * it makes assumptions about how batching works.
690 */
691 if (ioc && ioc->nr_batch_requests > 0 &&
692 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
693 (last_waited == 0 ||
694 ioc->last_waited == last_waited)) {
695 /*
696 * we want to go through our batch of
697 * requests and stop. So, we copy out
698 * the ioc->last_waited time and test
699 * against it before looping
700 */
701 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100702 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400703 continue;
704 }
Chris Mason8b712842008-06-11 16:50:36 -0400705 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400706 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500707 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400708
709 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800710 btrfs_queue_work(fs_info->submit_workers,
711 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400712 goto done;
713 }
714 }
Chris Masonffbd5172009-04-20 15:50:09 -0400715
Chris Mason51684082010-03-10 15:33:32 -0500716 cond_resched();
717 if (again)
718 goto loop;
719
720 spin_lock(&device->io_lock);
721 if (device->pending_bios.head || device->pending_sync_bios.head)
722 goto loop_lock;
723 spin_unlock(&device->io_lock);
724
Chris Mason8b712842008-06-11 16:50:36 -0400725done:
Chris Mason211588a2011-04-19 20:12:40 -0400726 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400727}
728
Christoph Hellwigb2950862008-12-02 09:54:17 -0500729static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400730{
731 struct btrfs_device *device;
732
733 device = container_of(work, struct btrfs_device, work);
734 run_scheduled_bios(device);
735}
736
Anand Jaind8367db2018-01-18 22:00:37 +0800737/*
738 * Search and remove all stale (devices which are not mounted) devices.
739 * When both inputs are NULL, it will search and release all stale devices.
740 * path: Optional. When provided will it release all unmounted devices
741 * matching this path only.
742 * skip_dev: Optional. Will skip this device when searching for the stale
743 * devices.
744 */
745static void btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800746 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800747{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800748 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
749 struct btrfs_device *device, *tmp_device;
Anand Jain4fde46f2015-06-17 21:10:48 +0800750
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800751 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain7bcb8162018-05-29 17:23:20 +0800752 mutex_lock(&fs_devices->device_list_mutex);
753 if (fs_devices->opened) {
754 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain4fde46f2015-06-17 21:10:48 +0800755 continue;
Anand Jain7bcb8162018-05-29 17:23:20 +0800756 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800757
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800758 list_for_each_entry_safe(device, tmp_device,
759 &fs_devices->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800760 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800761
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800762 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800763 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800764 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800765 continue;
766
Anand Jain4fde46f2015-06-17 21:10:48 +0800767 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800768 if (path)
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800769 not_found = strcmp(rcu_str_deref(device->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800770 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800771 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800772 if (not_found)
773 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800774
Anand Jain4fde46f2015-06-17 21:10:48 +0800775 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800776 fs_devices->num_devices--;
777 list_del(&device->dev_list);
778 btrfs_free_device(device);
779
780 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200781 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800782 }
783 mutex_unlock(&fs_devices->device_list_mutex);
784 if (fs_devices->num_devices == 0) {
785 btrfs_sysfs_remove_fsid(fs_devices);
786 list_del(&fs_devices->fs_list);
787 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800788 }
789 }
790}
791
Anand Jain0fb08bc2017-11-09 23:45:24 +0800792static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
793 struct btrfs_device *device, fmode_t flags,
794 void *holder)
795{
796 struct request_queue *q;
797 struct block_device *bdev;
798 struct buffer_head *bh;
799 struct btrfs_super_block *disk_super;
800 u64 devid;
801 int ret;
802
803 if (device->bdev)
804 return -EINVAL;
805 if (!device->name)
806 return -EINVAL;
807
808 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
809 &bdev, &bh);
810 if (ret)
811 return ret;
812
813 disk_super = (struct btrfs_super_block *)bh->b_data;
814 devid = btrfs_stack_device_id(&disk_super->dev_item);
815 if (devid != device->devid)
816 goto error_brelse;
817
818 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
819 goto error_brelse;
820
821 device->generation = btrfs_super_generation(disk_super);
822
823 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200824 if (btrfs_super_incompat_flags(disk_super) &
825 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
826 pr_err(
827 "BTRFS: Invalid seeding and uuid-changed device detected\n");
828 goto error_brelse;
829 }
830
Anand Jainebbede42017-12-04 12:54:52 +0800831 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800832 fs_devices->seeding = 1;
833 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800834 if (bdev_read_only(bdev))
835 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
836 else
837 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800838 }
839
840 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800841 if (!blk_queue_nonrot(q))
842 fs_devices->rotating = 1;
843
844 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800845 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800846 device->mode = flags;
847
848 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800849 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
850 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800851 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800852 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800853 }
854 brelse(bh);
855
856 return 0;
857
858error_brelse:
859 brelse(bh);
860 blkdev_put(bdev, flags);
861
862 return -EINVAL;
863}
864
David Sterba60999ca2014-03-26 18:26:36 +0100865/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200866 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
867 * being created with a disk that has already completed its fsid change.
868 */
869static struct btrfs_fs_devices *find_fsid_inprogress(
870 struct btrfs_super_block *disk_super)
871{
872 struct btrfs_fs_devices *fs_devices;
873
874 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
875 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
876 BTRFS_FSID_SIZE) != 0 &&
877 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
878 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
879 return fs_devices;
880 }
881 }
882
883 return NULL;
884}
885
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200886
887static struct btrfs_fs_devices *find_fsid_changed(
888 struct btrfs_super_block *disk_super)
889{
890 struct btrfs_fs_devices *fs_devices;
891
892 /*
893 * Handles the case where scanned device is part of an fs that had
894 * multiple successful changes of FSID but curently device didn't
895 * observe it. Meaning our fsid will be different than theirs.
896 */
897 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
898 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
899 BTRFS_FSID_SIZE) != 0 &&
900 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
901 BTRFS_FSID_SIZE) == 0 &&
902 memcmp(fs_devices->fsid, disk_super->fsid,
903 BTRFS_FSID_SIZE) != 0) {
904 return fs_devices;
905 }
906 }
907
908 return NULL;
909}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200910/*
David Sterba60999ca2014-03-26 18:26:36 +0100911 * Add new device to list of registered devices
912 *
913 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800914 * device pointer which was just added or updated when successful
915 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100916 */
Anand Jaine124ece2018-01-18 22:02:35 +0800917static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800918 struct btrfs_super_block *disk_super,
919 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400920{
921 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200922 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400923 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400924 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800925 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200926 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
927 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200928 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
929 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400930
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200931 if (fsid_change_in_progress) {
932 if (!has_metadata_uuid) {
933 /*
934 * When we have an image which has CHANGING_FSID_V2 set
935 * it might belong to either a filesystem which has
936 * disks with completed fsid change or it might belong
937 * to fs with no UUID changes in effect, handle both.
938 */
939 fs_devices = find_fsid_inprogress(disk_super);
940 if (!fs_devices)
941 fs_devices = find_fsid(disk_super->fsid, NULL);
942 } else {
943 fs_devices = find_fsid_changed(disk_super);
944 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200945 } else if (has_metadata_uuid) {
946 fs_devices = find_fsid(disk_super->fsid,
947 disk_super->metadata_uuid);
948 } else {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200949 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200950 }
951
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200952
Chris Mason8a4b83c2008-03-24 15:02:07 -0400953 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200954 if (has_metadata_uuid)
955 fs_devices = alloc_fs_devices(disk_super->fsid,
956 disk_super->metadata_uuid);
957 else
958 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
959
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200960 fs_devices->fsid_change = fsid_change_in_progress;
961
Ilya Dryomov2208a372013-08-12 14:33:03 +0300962 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800963 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300964
Anand Jain9c6d1732018-05-29 14:10:20 +0800965 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800966 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300967
Chris Mason8a4b83c2008-03-24 15:02:07 -0400968 device = NULL;
969 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800970 mutex_lock(&fs_devices->device_list_mutex);
David Sterba35c70102017-06-15 19:51:51 +0200971 device = find_device(fs_devices, devid,
972 disk_super->dev_item.uuid);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200973
974 /*
975 * If this disk has been pulled into an fs devices created by
976 * a device which had the CHANGING_FSID_V2 flag then replace the
977 * metadata_uuid/fsid values of the fs_devices.
978 */
979 if (has_metadata_uuid && fs_devices->fsid_change &&
980 found_transid > fs_devices->latest_generation) {
981 memcpy(fs_devices->fsid, disk_super->fsid,
982 BTRFS_FSID_SIZE);
983 memcpy(fs_devices->metadata_uuid,
984 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
985
986 fs_devices->fsid_change = false;
987 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400988 }
Miao Xie443f24f2014-07-24 11:37:15 +0800989
Chris Mason8a4b83c2008-03-24 15:02:07 -0400990 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800991 if (fs_devices->opened) {
992 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800993 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800994 }
Yan Zheng2b820322008-11-17 21:11:30 -0500995
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300996 device = btrfs_alloc_device(NULL, &devid,
997 disk_super->dev_item.uuid);
998 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800999 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001000 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +08001001 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001002 }
Josef Bacik606686e2012-06-04 14:03:51 -04001003
1004 name = rcu_string_strdup(path, GFP_NOFS);
1005 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001006 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +08001007 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001008 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001009 }
Josef Bacik606686e2012-06-04 14:03:51 -04001010 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +02001011
Xiao Guangrong1f781602011-04-20 10:09:16 +00001012 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001013 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -04001014
Yan Zheng2b820322008-11-17 21:11:30 -05001015 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001016 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +08001017
1018 if (disk_super->label[0])
1019 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1020 disk_super->label, devid, found_transid, path);
1021 else
1022 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1023 disk_super->fsid, devid, found_transid, path);
1024
Josef Bacik606686e2012-06-04 14:03:51 -04001025 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +08001026 /*
1027 * When FS is already mounted.
1028 * 1. If you are here and if the device->name is NULL that
1029 * means this device was missing at time of FS mount.
1030 * 2. If you are here and if the device->name is different
1031 * from 'path' that means either
1032 * a. The same device disappeared and reappeared with
1033 * different name. or
1034 * b. The missing-disk-which-was-replaced, has
1035 * reappeared now.
1036 *
1037 * We must allow 1 and 2a above. But 2b would be a spurious
1038 * and unintentional.
1039 *
1040 * Further in case of 1 and 2a above, the disk at 'path'
1041 * would have missed some transaction when it was away and
1042 * in case of 2a the stale bdev has to be updated as well.
1043 * 2b must not be allowed at all time.
1044 */
1045
1046 /*
Chris Mason0f23ae72014-09-18 07:49:05 -07001047 * For now, we do allow update to btrfs_fs_device through the
1048 * btrfs dev scan cli after FS has been mounted. We're still
1049 * tracking a problem where systems fail mount by subvolume id
1050 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +08001051 */
Chris Mason0f23ae72014-09-18 07:49:05 -07001052 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +08001053 /*
1054 * That is if the FS is _not_ mounted and if you
1055 * are here, that means there is more than one
1056 * disk with same uuid and devid.We keep the one
1057 * with larger generation number or the last-in if
1058 * generation are equal.
1059 */
Anand Jain9c6d1732018-05-29 14:10:20 +08001060 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001061 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +08001062 }
Anand Jainb96de002014-07-03 18:22:05 +08001063
Anand Jaina9261d42018-10-15 10:45:17 +08001064 /*
1065 * We are going to replace the device path for a given devid,
1066 * make sure it's the same device if the device is mounted
1067 */
1068 if (device->bdev) {
1069 struct block_device *path_bdev;
1070
1071 path_bdev = lookup_bdev(path);
1072 if (IS_ERR(path_bdev)) {
1073 mutex_unlock(&fs_devices->device_list_mutex);
1074 return ERR_CAST(path_bdev);
1075 }
1076
1077 if (device->bdev != path_bdev) {
1078 bdput(path_bdev);
1079 mutex_unlock(&fs_devices->device_list_mutex);
1080 btrfs_warn_in_rcu(device->fs_info,
1081 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1082 disk_super->fsid, devid,
1083 rcu_str_deref(device->name), path);
1084 return ERR_PTR(-EEXIST);
1085 }
1086 bdput(path_bdev);
1087 btrfs_info_in_rcu(device->fs_info,
1088 "device fsid %pU devid %llu moved old:%s new:%s",
1089 disk_super->fsid, devid,
1090 rcu_str_deref(device->name), path);
1091 }
1092
Josef Bacik606686e2012-06-04 14:03:51 -04001093 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +08001094 if (!name) {
1095 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001096 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +08001097 }
Josef Bacik606686e2012-06-04 14:03:51 -04001098 rcu_string_free(device->name);
1099 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +08001100 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05001101 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08001102 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05001103 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001104 }
1105
Anand Jain77bdae42014-07-03 18:22:06 +08001106 /*
1107 * Unmount does not free the btrfs_device struct but would zero
1108 * generation along with most of the other members. So just update
1109 * it back. We need it to pick the disk with largest generation
1110 * (as above).
1111 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001112 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +08001113 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001114 fs_devices->latest_generation = max_t(u64, found_transid,
1115 fs_devices->latest_generation);
1116 }
Anand Jain77bdae42014-07-03 18:22:06 +08001117
Anand Jainf2788d22018-01-18 22:02:34 +08001118 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1119
Anand Jain9c6d1732018-05-29 14:10:20 +08001120 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001121 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001122}
1123
Yan Zhenge4404d62008-12-12 10:03:26 -05001124static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1125{
1126 struct btrfs_fs_devices *fs_devices;
1127 struct btrfs_device *device;
1128 struct btrfs_device *orig_dev;
1129
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001130 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001131 if (IS_ERR(fs_devices))
1132 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001133
Miao Xieadbbb862014-09-03 21:35:42 +08001134 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001135 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001136
Xiao Guangrong46224702011-04-20 10:08:47 +00001137 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -05001138 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001139 struct rcu_string *name;
1140
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001141 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1142 orig_dev->uuid);
1143 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -05001144 goto error;
1145
Josef Bacik606686e2012-06-04 14:03:51 -04001146 /*
1147 * This is ok to do without rcu read locked because we hold the
1148 * uuid mutex so nothing we touch in here is going to disappear.
1149 */
Anand Jaine755f782014-06-30 17:12:47 +08001150 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001151 name = rcu_string_strdup(orig_dev->name->str,
1152 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001153 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001154 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +08001155 goto error;
1156 }
1157 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001158 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001159
Yan Zhenge4404d62008-12-12 10:03:26 -05001160 list_add(&device->dev_list, &fs_devices->devices);
1161 device->fs_devices = fs_devices;
1162 fs_devices->num_devices++;
1163 }
Miao Xieadbbb862014-09-03 21:35:42 +08001164 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001165 return fs_devices;
1166error:
Miao Xieadbbb862014-09-03 21:35:42 +08001167 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001168 free_fs_devices(fs_devices);
1169 return ERR_PTR(-ENOMEM);
1170}
1171
Anand Jain9b99b112018-02-27 12:41:59 +08001172/*
1173 * After we have read the system tree and know devids belonging to
1174 * this filesystem, remove the device which does not belong there.
1175 */
1176void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001177{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001178 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001179 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001180
Chris Masondfe25022008-05-13 13:46:40 -04001181 mutex_lock(&uuid_mutex);
1182again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001183 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001184 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001185 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1186 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001187 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1188 &device->dev_state) &&
1189 (!latest_dev ||
1190 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001191 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001192 }
Yan Zheng2b820322008-11-17 21:11:30 -05001193 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001194 }
Yan Zheng2b820322008-11-17 21:11:30 -05001195
Stefan Behrens8dabb742012-11-06 13:15:27 +01001196 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1197 /*
1198 * In the first step, keep the device which has
1199 * the correct fsid and the devid that is used
1200 * for the dev_replace procedure.
1201 * In the second step, the dev_replace state is
1202 * read from the device tree and it is known
1203 * whether the procedure is really active or
1204 * not, which means whether this device is
1205 * used or whether it should be removed.
1206 */
Anand Jain401e29c2017-12-04 12:54:55 +08001207 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1208 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001209 continue;
1210 }
1211 }
Yan Zheng2b820322008-11-17 21:11:30 -05001212 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001213 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001214 device->bdev = NULL;
1215 fs_devices->open_devices--;
1216 }
Anand Jainebbede42017-12-04 12:54:52 +08001217 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001218 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001219 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001220 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1221 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001222 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001223 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001224 list_del_init(&device->dev_list);
1225 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001226 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001227 }
Yan Zheng2b820322008-11-17 21:11:30 -05001228
1229 if (fs_devices->seed) {
1230 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001231 goto again;
1232 }
1233
Miao Xie443f24f2014-07-24 11:37:15 +08001234 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001235
Chris Masondfe25022008-05-13 13:46:40 -04001236 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001237}
Chris Masona0af4692008-05-13 16:03:06 -04001238
David Sterbaf06c5962017-06-06 17:08:23 +02001239static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +00001240{
1241 struct btrfs_device *device;
1242
1243 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +01001244 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001245}
1246
Anand Jain14238812016-07-22 06:04:53 +08001247static void btrfs_close_bdev(struct btrfs_device *device)
1248{
David Sterba08ffcae2017-06-19 16:55:35 +02001249 if (!device->bdev)
1250 return;
1251
Anand Jainebbede42017-12-04 12:54:52 +08001252 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001253 sync_blockdev(device->bdev);
1254 invalidate_bdev(device->bdev);
1255 }
1256
David Sterba08ffcae2017-06-19 16:55:35 +02001257 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001258}
1259
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001260static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001261{
1262 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1263 struct btrfs_device *new_device;
1264 struct rcu_string *name;
1265
1266 if (device->bdev)
1267 fs_devices->open_devices--;
1268
Anand Jainebbede42017-12-04 12:54:52 +08001269 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001270 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1271 list_del_init(&device->dev_alloc_list);
1272 fs_devices->rw_devices--;
1273 }
1274
Anand Jaine6e674b2017-12-04 12:54:54 +08001275 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001276 fs_devices->missing_devices--;
1277
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001278 btrfs_close_bdev(device);
1279
Anand Jainf448341a2016-06-14 18:55:25 +08001280 new_device = btrfs_alloc_device(NULL, &device->devid,
1281 device->uuid);
1282 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1283
1284 /* Safe because we are under uuid_mutex */
1285 if (device->name) {
1286 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1287 BUG_ON(!name); /* -ENOMEM */
1288 rcu_assign_pointer(new_device->name, name);
1289 }
1290
1291 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1292 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001293
1294 call_rcu(&device->rcu, free_device_rcu);
Anand Jainf448341a2016-06-14 18:55:25 +08001295}
1296
Anand Jain0226e0e2018-04-12 10:29:27 +08001297static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001298{
Sasha Levin2037a092015-05-12 19:31:37 -04001299 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001300
Yan Zheng2b820322008-11-17 21:11:30 -05001301 if (--fs_devices->opened > 0)
1302 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001303
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001304 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001305 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001306 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001307 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001308 mutex_unlock(&fs_devices->device_list_mutex);
1309
Yan Zhenge4404d62008-12-12 10:03:26 -05001310 WARN_ON(fs_devices->open_devices);
1311 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001312 fs_devices->opened = 0;
1313 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001314
Chris Mason8a4b83c2008-03-24 15:02:07 -04001315 return 0;
1316}
1317
Yan Zheng2b820322008-11-17 21:11:30 -05001318int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1319{
Yan Zhenge4404d62008-12-12 10:03:26 -05001320 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001321 int ret;
1322
1323 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001324 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001325 if (!fs_devices->opened) {
1326 seed_devices = fs_devices->seed;
1327 fs_devices->seed = NULL;
1328 }
Yan Zheng2b820322008-11-17 21:11:30 -05001329 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001330
1331 while (seed_devices) {
1332 fs_devices = seed_devices;
1333 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001334 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 free_fs_devices(fs_devices);
1336 }
Yan Zheng2b820322008-11-17 21:11:30 -05001337 return ret;
1338}
1339
Anand Jain897fb572018-04-12 10:29:28 +08001340static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001341 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001342{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001343 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001344 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001345 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001346
Tejun Heod4d77622010-11-13 11:55:18 +01001347 flags |= FMODE_EXCL;
1348
Anand Jainf117e292018-04-12 10:29:26 +08001349 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001350 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001351 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001352 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001353
Anand Jain9f050db2017-11-09 23:45:25 +08001354 if (!latest_dev ||
1355 device->generation > latest_dev->generation)
1356 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001357 }
Chris Masona0af4692008-05-13 16:03:06 -04001358 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001359 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001360 goto out;
1361 }
Yan Zheng2b820322008-11-17 21:11:30 -05001362 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001363 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001364 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001365out:
Yan Zheng2b820322008-11-17 21:11:30 -05001366 return ret;
1367}
1368
Anand Jainf8e10cd2018-01-22 14:49:36 -08001369static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1370{
1371 struct btrfs_device *dev1, *dev2;
1372
1373 dev1 = list_entry(a, struct btrfs_device, dev_list);
1374 dev2 = list_entry(b, struct btrfs_device, dev_list);
1375
1376 if (dev1->devid < dev2->devid)
1377 return -1;
1378 else if (dev1->devid > dev2->devid)
1379 return 1;
1380 return 0;
1381}
1382
Yan Zheng2b820322008-11-17 21:11:30 -05001383int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001384 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001385{
1386 int ret;
1387
David Sterbaf5194e32018-06-19 17:09:47 +02001388 lockdep_assert_held(&uuid_mutex);
1389
Anand Jain542c5902018-04-12 10:29:34 +08001390 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001391 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001392 fs_devices->opened++;
1393 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001394 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001395 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001396 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001397 }
Anand Jain542c5902018-04-12 10:29:34 +08001398 mutex_unlock(&fs_devices->device_list_mutex);
1399
Chris Mason8a4b83c2008-03-24 15:02:07 -04001400 return ret;
1401}
1402
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001403static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001404{
1405 kunmap(page);
1406 put_page(page);
1407}
1408
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001409static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1410 struct page **page,
1411 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001412{
1413 void *p;
1414 pgoff_t index;
1415
1416 /* make sure our super fits in the device */
1417 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1418 return 1;
1419
1420 /* make sure our super fits in the page */
1421 if (sizeof(**disk_super) > PAGE_SIZE)
1422 return 1;
1423
1424 /* make sure our super doesn't straddle pages on disk */
1425 index = bytenr >> PAGE_SHIFT;
1426 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1427 return 1;
1428
1429 /* pull in the page with our super */
1430 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1431 index, GFP_KERNEL);
1432
1433 if (IS_ERR_OR_NULL(*page))
1434 return 1;
1435
1436 p = kmap(*page);
1437
1438 /* align our pointer to the offset of the super block */
1439 *disk_super = p + (bytenr & ~PAGE_MASK);
1440
1441 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1442 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1443 btrfs_release_disk_super(*page);
1444 return 1;
1445 }
1446
1447 if ((*disk_super)->label[0] &&
1448 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1449 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1450
1451 return 0;
1452}
1453
David Sterba6f60cbd2013-02-15 11:31:02 -07001454/*
1455 * Look for a btrfs signature on a device. This may be called out of the mount path
1456 * and we are not allowed to call set_blocksize during the scan. The superblock
1457 * is read via pagecache
1458 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001459struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1460 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001461{
1462 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001463 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001464 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001465 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001466 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001467 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001468
David Sterba899f9302018-06-19 16:37:36 +02001469 lockdep_assert_held(&uuid_mutex);
1470
David Sterba6f60cbd2013-02-15 11:31:02 -07001471 /*
1472 * we would like to check all the supers, but that would make
1473 * a btrfs mount succeed after a mkfs from a different FS.
1474 * So, we need to add a special mount option to scan for
1475 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1476 */
1477 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001478 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001479
1480 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001481 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001482 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001483
Anand Jain05a5c552017-12-15 15:40:16 +08001484 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001485 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001486 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001487 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001488
Anand Jain4306a972018-05-29 12:28:37 +08001489 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001490 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001491 if (new_device_added)
1492 btrfs_free_stale_devices(path, device);
1493 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001494
Anand Jain6cf86a002016-02-13 10:01:29 +08001495 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001496
1497error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001498 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001499
Gu Jinxiang36350e92018-07-12 14:23:16 +08001500 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001501}
Chris Mason0b86a832008-03-24 15:01:56 -04001502
Jeff Mahoney499f3772015-06-15 09:41:17 -04001503static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001504 struct btrfs_device *device,
1505 u64 *start, u64 len)
1506{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001507 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001508 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001509 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001510 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001511 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001512
Jeff Mahoney499f3772015-06-15 09:41:17 -04001513 if (transaction)
1514 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001515again:
1516 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001517 struct map_lookup *map;
1518 int i;
1519
Jeff Mahoney95617d62015-06-03 10:55:48 -04001520 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001521 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001522 u64 end;
1523
Josef Bacik6df9a952013-06-27 13:22:46 -04001524 if (map->stripes[i].dev != device)
1525 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001526 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001527 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001528 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001529 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001530 /*
1531 * Make sure that while processing the pinned list we do
1532 * not override our *start with a lower value, because
1533 * we can have pinned chunks that fall within this
1534 * device hole and that have lower physical addresses
1535 * than the pending chunks we processed before. If we
1536 * do not take this special care we can end up getting
1537 * 2 pending chunks that start at the same physical
1538 * device offsets because the end offset of a pinned
1539 * chunk can be equal to the start offset of some
1540 * pending chunk.
1541 */
1542 end = map->stripes[i].physical + em->orig_block_len;
1543 if (end > *start) {
1544 *start = end;
1545 ret = 1;
1546 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001547 }
1548 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001549 if (search_list != &fs_info->pinned_chunks) {
1550 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001551 goto again;
1552 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001553
1554 return ret;
1555}
1556
1557
Chris Mason0b86a832008-03-24 15:01:56 -04001558/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001559 * find_free_dev_extent_start - find free space in the specified device
1560 * @device: the device which we search the free space in
1561 * @num_bytes: the size of the free space that we need
1562 * @search_start: the position from which to begin the search
1563 * @start: store the start of the free space.
1564 * @len: the size of the free space. that we find, or the size
1565 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001566 *
Chris Mason0b86a832008-03-24 15:01:56 -04001567 * this uses a pretty simple search, the expectation is that it is
1568 * called very infrequently and that a given device has a small number
1569 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001570 *
1571 * @start is used to store the start of the free space if we find. But if we
1572 * don't find suitable free space, it will be used to store the start position
1573 * of the max free space.
1574 *
1575 * @len is used to store the size of the free space that we find.
1576 * But if we don't find suitable free space, it is used to store the size of
1577 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001578 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001579int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1580 struct btrfs_device *device, u64 num_bytes,
1581 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001582{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001583 struct btrfs_fs_info *fs_info = device->fs_info;
1584 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001585 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001586 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001587 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001588 u64 hole_size;
1589 u64 max_hole_start;
1590 u64 max_hole_size;
1591 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001592 u64 search_end = device->total_bytes;
1593 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001594 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001595 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001596
1597 /*
1598 * We don't want to overwrite the superblock on the drive nor any area
1599 * used by the boot loader (grub for example), so we make sure to start
1600 * at an offset of at least 1MB.
1601 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001602 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001603
Josef Bacik6df9a952013-06-27 13:22:46 -04001604 path = btrfs_alloc_path();
1605 if (!path)
1606 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001607
Miao Xie7bfc8372011-01-05 10:07:26 +00001608 max_hole_start = search_start;
1609 max_hole_size = 0;
1610
Zhao Leif2ab7612015-02-16 18:52:17 +08001611again:
Anand Jain401e29c2017-12-04 12:54:55 +08001612 if (search_start >= search_end ||
1613 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001614 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001615 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001616 }
1617
David Sterbae4058b52015-11-27 16:31:35 +01001618 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001619 path->search_commit_root = 1;
1620 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001621
Chris Mason0b86a832008-03-24 15:01:56 -04001622 key.objectid = device->devid;
1623 key.offset = search_start;
1624 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001625
Li Zefan125ccb02011-12-08 15:07:24 +08001626 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001627 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001628 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001629 if (ret > 0) {
1630 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1631 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001632 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001633 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001634
Chris Mason0b86a832008-03-24 15:01:56 -04001635 while (1) {
1636 l = path->nodes[0];
1637 slot = path->slots[0];
1638 if (slot >= btrfs_header_nritems(l)) {
1639 ret = btrfs_next_leaf(root, path);
1640 if (ret == 0)
1641 continue;
1642 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001643 goto out;
1644
1645 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001646 }
1647 btrfs_item_key_to_cpu(l, &key, slot);
1648
1649 if (key.objectid < device->devid)
1650 goto next;
1651
1652 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001653 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001654
David Sterba962a2982014-06-04 18:41:45 +02001655 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001656 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001657
Miao Xie7bfc8372011-01-05 10:07:26 +00001658 if (key.offset > search_start) {
1659 hole_size = key.offset - search_start;
1660
Josef Bacik6df9a952013-06-27 13:22:46 -04001661 /*
1662 * Have to check before we set max_hole_start, otherwise
1663 * we could end up sending back this offset anyway.
1664 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001665 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001666 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001667 hole_size)) {
1668 if (key.offset >= search_start) {
1669 hole_size = key.offset - search_start;
1670 } else {
1671 WARN_ON_ONCE(1);
1672 hole_size = 0;
1673 }
1674 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001675
Miao Xie7bfc8372011-01-05 10:07:26 +00001676 if (hole_size > max_hole_size) {
1677 max_hole_start = search_start;
1678 max_hole_size = hole_size;
1679 }
1680
1681 /*
1682 * If this free space is greater than which we need,
1683 * it must be the max free space that we have found
1684 * until now, so max_hole_start must point to the start
1685 * of this free space and the length of this free space
1686 * is stored in max_hole_size. Thus, we return
1687 * max_hole_start and max_hole_size and go back to the
1688 * caller.
1689 */
1690 if (hole_size >= num_bytes) {
1691 ret = 0;
1692 goto out;
1693 }
1694 }
1695
Chris Mason0b86a832008-03-24 15:01:56 -04001696 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001697 extent_end = key.offset + btrfs_dev_extent_length(l,
1698 dev_extent);
1699 if (extent_end > search_start)
1700 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001701next:
1702 path->slots[0]++;
1703 cond_resched();
1704 }
Chris Mason0b86a832008-03-24 15:01:56 -04001705
liubo38c01b92011-08-02 02:39:03 +00001706 /*
1707 * At this point, search_start should be the end of
1708 * allocated dev extents, and when shrinking the device,
1709 * search_end may be smaller than search_start.
1710 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001711 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001712 hole_size = search_end - search_start;
1713
Jeff Mahoney499f3772015-06-15 09:41:17 -04001714 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001715 hole_size)) {
1716 btrfs_release_path(path);
1717 goto again;
1718 }
Chris Mason0b86a832008-03-24 15:01:56 -04001719
Zhao Leif2ab7612015-02-16 18:52:17 +08001720 if (hole_size > max_hole_size) {
1721 max_hole_start = search_start;
1722 max_hole_size = hole_size;
1723 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001724 }
1725
Miao Xie7bfc8372011-01-05 10:07:26 +00001726 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001727 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001728 ret = -ENOSPC;
1729 else
1730 ret = 0;
1731
1732out:
Yan Zheng2b820322008-11-17 21:11:30 -05001733 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001734 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001735 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001736 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001737 return ret;
1738}
1739
Jeff Mahoney499f3772015-06-15 09:41:17 -04001740int find_free_dev_extent(struct btrfs_trans_handle *trans,
1741 struct btrfs_device *device, u64 num_bytes,
1742 u64 *start, u64 *len)
1743{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001744 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001745 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001746 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001747}
1748
Christoph Hellwigb2950862008-12-02 09:54:17 -05001749static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001750 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001751 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001752{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001753 struct btrfs_fs_info *fs_info = device->fs_info;
1754 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001755 int ret;
1756 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001757 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001758 struct btrfs_key found_key;
1759 struct extent_buffer *leaf = NULL;
1760 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001761
1762 path = btrfs_alloc_path();
1763 if (!path)
1764 return -ENOMEM;
1765
1766 key.objectid = device->devid;
1767 key.offset = start;
1768 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001769again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001770 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001771 if (ret > 0) {
1772 ret = btrfs_previous_item(root, path, key.objectid,
1773 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001774 if (ret)
1775 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001776 leaf = path->nodes[0];
1777 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1778 extent = btrfs_item_ptr(leaf, path->slots[0],
1779 struct btrfs_dev_extent);
1780 BUG_ON(found_key.offset > start || found_key.offset +
1781 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001782 key = found_key;
1783 btrfs_release_path(path);
1784 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001785 } else if (ret == 0) {
1786 leaf = path->nodes[0];
1787 extent = btrfs_item_ptr(leaf, path->slots[0],
1788 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001789 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001790 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001791 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001792 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001793
Miao Xie2196d6e2014-09-03 21:35:41 +08001794 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1795
Chris Mason8f18cf12008-04-25 16:53:30 -04001796 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001797 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001798 btrfs_handle_fs_error(fs_info, ret,
1799 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001800 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001801 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001802 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001803out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001804 btrfs_free_path(path);
1805 return ret;
1806}
1807
Eric Sandeen48a3b632013-04-25 20:41:01 +00001808static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1809 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001810 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001811{
1812 int ret;
1813 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001814 struct btrfs_fs_info *fs_info = device->fs_info;
1815 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001816 struct btrfs_dev_extent *extent;
1817 struct extent_buffer *leaf;
1818 struct btrfs_key key;
1819
Anand Jaine12c9622017-12-04 12:54:53 +08001820 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001821 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001822 path = btrfs_alloc_path();
1823 if (!path)
1824 return -ENOMEM;
1825
Chris Mason0b86a832008-03-24 15:01:56 -04001826 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001827 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001828 key.type = BTRFS_DEV_EXTENT_KEY;
1829 ret = btrfs_insert_empty_item(trans, root, path, &key,
1830 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001831 if (ret)
1832 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001833
1834 leaf = path->nodes[0];
1835 extent = btrfs_item_ptr(leaf, path->slots[0],
1836 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001837 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1838 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001839 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1840 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001841 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1842
Chris Mason0b86a832008-03-24 15:01:56 -04001843 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1844 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001845out:
Chris Mason0b86a832008-03-24 15:01:56 -04001846 btrfs_free_path(path);
1847 return ret;
1848}
1849
Josef Bacik6df9a952013-06-27 13:22:46 -04001850static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001851{
Josef Bacik6df9a952013-06-27 13:22:46 -04001852 struct extent_map_tree *em_tree;
1853 struct extent_map *em;
1854 struct rb_node *n;
1855 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001856
Josef Bacik6df9a952013-06-27 13:22:46 -04001857 em_tree = &fs_info->mapping_tree.map_tree;
1858 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001859 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001860 if (n) {
1861 em = rb_entry(n, struct extent_map, rb_node);
1862 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001863 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001864 read_unlock(&em_tree->lock);
1865
Chris Mason0b86a832008-03-24 15:01:56 -04001866 return ret;
1867}
1868
Ilya Dryomov53f10652013-08-12 14:33:01 +03001869static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1870 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001871{
1872 int ret;
1873 struct btrfs_key key;
1874 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001875 struct btrfs_path *path;
1876
Yan Zheng2b820322008-11-17 21:11:30 -05001877 path = btrfs_alloc_path();
1878 if (!path)
1879 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001880
1881 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1882 key.type = BTRFS_DEV_ITEM_KEY;
1883 key.offset = (u64)-1;
1884
Ilya Dryomov53f10652013-08-12 14:33:01 +03001885 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001886 if (ret < 0)
1887 goto error;
1888
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001889 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001890
Ilya Dryomov53f10652013-08-12 14:33:01 +03001891 ret = btrfs_previous_item(fs_info->chunk_root, path,
1892 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001893 BTRFS_DEV_ITEM_KEY);
1894 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001895 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001896 } else {
1897 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1898 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001899 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001900 }
1901 ret = 0;
1902error:
Yan Zheng2b820322008-11-17 21:11:30 -05001903 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001904 return ret;
1905}
1906
1907/*
1908 * the device information is stored in the chunk root
1909 * the btrfs_device struct should be fully filled in
1910 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001911static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001912 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001913{
1914 int ret;
1915 struct btrfs_path *path;
1916 struct btrfs_dev_item *dev_item;
1917 struct extent_buffer *leaf;
1918 struct btrfs_key key;
1919 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001920
Chris Mason0b86a832008-03-24 15:01:56 -04001921 path = btrfs_alloc_path();
1922 if (!path)
1923 return -ENOMEM;
1924
Chris Mason0b86a832008-03-24 15:01:56 -04001925 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1926 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001927 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001928
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001929 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1930 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001931 if (ret)
1932 goto out;
1933
1934 leaf = path->nodes[0];
1935 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1936
1937 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001938 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001939 btrfs_set_device_type(leaf, dev_item, device->type);
1940 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1941 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1942 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001943 btrfs_set_device_total_bytes(leaf, dev_item,
1944 btrfs_device_get_disk_total_bytes(device));
1945 btrfs_set_device_bytes_used(leaf, dev_item,
1946 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001947 btrfs_set_device_group(leaf, dev_item, 0);
1948 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1949 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001950 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001951
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001952 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001953 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001954 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001955 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1956 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001957 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001958
Yan Zheng2b820322008-11-17 21:11:30 -05001959 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001960out:
1961 btrfs_free_path(path);
1962 return ret;
1963}
Chris Mason8f18cf12008-04-25 16:53:30 -04001964
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001965/*
1966 * Function to update ctime/mtime for a given device path.
1967 * Mainly used for ctime/mtime based probe like libblkid.
1968 */
David Sterbada353f62017-02-14 17:55:53 +01001969static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001970{
1971 struct file *filp;
1972
1973 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001974 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001975 return;
1976 file_update_time(filp);
1977 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001978}
1979
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001980static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001981 struct btrfs_device *device)
1982{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001983 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001984 int ret;
1985 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001986 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001987 struct btrfs_trans_handle *trans;
1988
Chris Masona061fc82008-05-07 11:43:44 -04001989 path = btrfs_alloc_path();
1990 if (!path)
1991 return -ENOMEM;
1992
Yan, Zhenga22285a2010-05-16 10:48:46 -04001993 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001994 if (IS_ERR(trans)) {
1995 btrfs_free_path(path);
1996 return PTR_ERR(trans);
1997 }
Chris Masona061fc82008-05-07 11:43:44 -04001998 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1999 key.type = BTRFS_DEV_ITEM_KEY;
2000 key.offset = device->devid;
2001
2002 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002003 if (ret) {
2004 if (ret > 0)
2005 ret = -ENOENT;
2006 btrfs_abort_transaction(trans, ret);
2007 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002008 goto out;
2009 }
2010
2011 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002012 if (ret) {
2013 btrfs_abort_transaction(trans, ret);
2014 btrfs_end_transaction(trans);
2015 }
2016
Chris Masona061fc82008-05-07 11:43:44 -04002017out:
2018 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002019 if (!ret)
2020 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002021 return ret;
2022}
2023
David Sterba3cc31a02016-02-15 16:00:26 +01002024/*
2025 * Verify that @num_devices satisfies the RAID profile constraints in the whole
2026 * filesystem. It's up to the caller to adjust that number regarding eg. device
2027 * replace.
2028 */
2029static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
2030 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04002031{
Chris Masona061fc82008-05-07 11:43:44 -04002032 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00002033 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01002034 int i;
Chris Masona061fc82008-05-07 11:43:44 -04002035
Miao Xiede98ced2013-01-29 10:13:12 +00002036 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08002037 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002038
Anand Jainbd45ffb2016-02-13 10:01:34 +08002039 all_avail = fs_info->avail_data_alloc_bits |
2040 fs_info->avail_system_alloc_bits |
2041 fs_info->avail_metadata_alloc_bits;
2042 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002043
David Sterba418775a2016-02-15 16:28:14 +01002044 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002045 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002046 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002047
David Sterba418775a2016-02-15 16:28:14 +01002048 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002049 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002050
David Sterba418775a2016-02-15 16:28:14 +01002051 if (ret)
2052 return ret;
2053 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002054 }
2055
2056 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002057}
2058
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002059static struct btrfs_device * btrfs_find_next_active_device(
2060 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002061{
2062 struct btrfs_device *next_device;
2063
2064 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2065 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002066 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2067 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002068 return next_device;
2069 }
2070
2071 return NULL;
2072}
2073
2074/*
2075 * Helper function to check if the given device is part of s_bdev / latest_bdev
2076 * and replace it with the provided or the next active device, in the context
2077 * where this function called, there should be always be another device (or
2078 * this_dev) which is active.
2079 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002080void btrfs_assign_next_active_device(struct btrfs_device *device,
2081 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08002082{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002083 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002084 struct btrfs_device *next_device;
2085
2086 if (this_dev)
2087 next_device = this_dev;
2088 else
2089 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2090 device);
2091 ASSERT(next_device);
2092
2093 if (fs_info->sb->s_bdev &&
2094 (fs_info->sb->s_bdev == device->bdev))
2095 fs_info->sb->s_bdev = next_device->bdev;
2096
2097 if (fs_info->fs_devices->latest_bdev == device->bdev)
2098 fs_info->fs_devices->latest_bdev = next_device->bdev;
2099}
2100
Anand Jain1da73962018-08-10 13:53:21 +08002101/*
2102 * Return btrfs_fs_devices::num_devices excluding the device that's being
2103 * currently replaced.
2104 */
2105static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2106{
2107 u64 num_devices = fs_info->fs_devices->num_devices;
2108
2109 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
2110 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2111 ASSERT(num_devices > 1);
2112 num_devices--;
2113 }
2114 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
2115
2116 return num_devices;
2117}
2118
David Sterbada353f62017-02-14 17:55:53 +01002119int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2120 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002121{
2122 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002123 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002124 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002125 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002126 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002127
2128 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002129
Anand Jain1da73962018-08-10 13:53:21 +08002130 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002131
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002132 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002133 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002134 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002135
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002136 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2137
2138 if (IS_ERR(device)) {
2139 if (PTR_ERR(device) == -ENOENT &&
2140 strcmp(device_path, "missing") == 0)
2141 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2142 else
2143 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002144 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002145 }
Yan Zheng2b820322008-11-17 21:11:30 -05002146
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002147 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2148 btrfs_warn_in_rcu(fs_info,
2149 "cannot remove device %s (devid %llu) due to active swapfile",
2150 rcu_str_deref(device->name), device->devid);
2151 ret = -ETXTBSY;
2152 goto out;
2153 }
2154
Anand Jain401e29c2017-12-04 12:54:55 +08002155 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002156 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002157 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002158 }
2159
Anand Jainebbede42017-12-04 12:54:52 +08002160 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2161 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002162 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002163 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002164 }
2165
Anand Jainebbede42017-12-04 12:54:52 +08002166 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002167 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002168 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002169 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002170 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002171 }
Chris Masona061fc82008-05-07 11:43:44 -04002172
Carey Underwoodd7901552013-03-04 16:37:06 -06002173 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002174 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002175 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002176 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002177 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002178
Stefan Behrens63a212a2012-11-05 18:29:28 +01002179 /*
2180 * TODO: the superblock still includes this device in its num_devices
2181 * counter although write_all_supers() is not locked out. This
2182 * could give a filesystem state which requires a degraded mount.
2183 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002184 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002185 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002186 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002187
Anand Jaine12c9622017-12-04 12:54:53 +08002188 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002189 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002190
2191 /*
2192 * the device list mutex makes sure that we don't change
2193 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002194 * the device supers. Whoever is writing all supers, should
2195 * lock the device list mutex before getting the number of
2196 * devices in the super block (super_copy). Conversely,
2197 * whoever updates the number of devices in the super block
2198 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002199 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002200
Anand Jain41a52a02018-04-12 10:29:31 +08002201 /*
2202 * In normal cases the cur_devices == fs_devices. But in case
2203 * of deleting a seed device, the cur_devices should point to
2204 * its own fs_devices listed under the fs_devices->seed.
2205 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002206 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002207 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002208 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002209
Anand Jain41a52a02018-04-12 10:29:31 +08002210 cur_devices->num_devices--;
2211 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002212 /* Update total_devices of the parent fs_devices if it's seed */
2213 if (cur_devices != fs_devices)
2214 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002215
Anand Jaine6e674b2017-12-04 12:54:54 +08002216 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002217 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002218
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002219 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002220
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002221 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002222 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002223 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002224 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002225 }
Anand Jain99994cd2014-06-03 11:36:00 +08002226
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002227 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2228 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002229 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002230
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002231 /*
2232 * at this point, the device is zero sized and detached from
2233 * the devices list. All that's left is to zero out the old
2234 * supers and free the device.
2235 */
Anand Jainebbede42017-12-04 12:54:52 +08002236 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002237 btrfs_scratch_superblocks(device->bdev, device->name->str);
2238
2239 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002240 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002241
Xiao Guangrong1f781602011-04-20 10:09:16 +00002242 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002243 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002244 if (fs_devices->seed == cur_devices) {
2245 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002246 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002247 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002248 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002249 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002250 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002251 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002252 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002253 }
2254
Chris Masona061fc82008-05-07 11:43:44 -04002255out:
2256 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002257 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002258
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002259error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002260 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002261 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002262 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002263 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002264 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002265 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002266 }
Anand Jain24fc5722016-02-13 10:01:36 +08002267 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002268}
2269
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002270void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002271{
Anand Jaind51908c2014-08-13 14:24:19 +08002272 struct btrfs_fs_devices *fs_devices;
2273
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002274 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002275
Anand Jain25e8e912014-08-20 10:56:56 +08002276 /*
2277 * in case of fs with no seed, srcdev->fs_devices will point
2278 * to fs_devices of fs_info. However when the dev being replaced is
2279 * a seed dev it will point to the seed's local fs_devices. In short
2280 * srcdev will have its correct fs_devices in both the cases.
2281 */
2282 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002283
Stefan Behrense93c89c2012-11-05 17:33:06 +01002284 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002285 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002286 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002287 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002288 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002289
Anand Jainebbede42017-12-04 12:54:52 +08002290 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002291 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002292
Miao Xie82372bc2014-09-03 21:35:44 +08002293 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002294 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002295}
2296
2297void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2298 struct btrfs_device *srcdev)
2299{
2300 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002301
Anand Jainebbede42017-12-04 12:54:52 +08002302 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002303 /* zero out the old super if it is writable */
2304 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2305 }
Anand Jain14238812016-07-22 06:04:53 +08002306
2307 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002308 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002309
Anand Jain94d5f0c2014-08-13 14:24:22 +08002310 /* if this is no devs we rather delete the fs_devices */
2311 if (!fs_devices->num_devices) {
2312 struct btrfs_fs_devices *tmp_fs_devices;
2313
Anand Jain6dd38f82017-10-17 06:53:50 +08002314 /*
2315 * On a mounted FS, num_devices can't be zero unless it's a
2316 * seed. In case of a seed device being replaced, the replace
2317 * target added to the sprout FS, so there will be no more
2318 * device left under the seed FS.
2319 */
2320 ASSERT(fs_devices->seeding);
2321
Anand Jain94d5f0c2014-08-13 14:24:22 +08002322 tmp_fs_devices = fs_info->fs_devices;
2323 while (tmp_fs_devices) {
2324 if (tmp_fs_devices->seed == fs_devices) {
2325 tmp_fs_devices->seed = fs_devices->seed;
2326 break;
2327 }
2328 tmp_fs_devices = tmp_fs_devices->seed;
2329 }
2330 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002331 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002332 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002333 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002334}
2335
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002336void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002337{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002338 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002339
Anand Jaind9a071f2018-04-12 10:29:38 +08002340 WARN_ON(!tgtdev);
2341 mutex_lock(&fs_devices->device_list_mutex);
2342
2343 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002344
Anand Jain779bf3fe2016-04-18 16:51:23 +08002345 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002346 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002347
Anand Jaind9a071f2018-04-12 10:29:38 +08002348 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002349
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002350 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002351
Stefan Behrense93c89c2012-11-05 17:33:06 +01002352 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002353
Anand Jaind9a071f2018-04-12 10:29:38 +08002354 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002355
2356 /*
2357 * The update_dev_time() with in btrfs_scratch_superblocks()
2358 * may lead to a call to btrfs_show_devname() which will try
2359 * to hold device_list_mutex. And here this device
2360 * is already out of device list, so we don't have to hold
2361 * the device_list_mutex lock.
2362 */
2363 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002364
2365 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002366 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002367}
2368
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002369static struct btrfs_device *btrfs_find_device_by_path(
2370 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002371{
2372 int ret = 0;
2373 struct btrfs_super_block *disk_super;
2374 u64 devid;
2375 u8 *dev_uuid;
2376 struct block_device *bdev;
2377 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002378 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002379
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002380 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002381 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002382 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002383 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002384 disk_super = (struct btrfs_super_block *)bh->b_data;
2385 devid = btrfs_stack_device_id(&disk_super->dev_item);
2386 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002387 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2388 device = btrfs_find_device(fs_info, devid, dev_uuid,
2389 disk_super->metadata_uuid);
2390 else
2391 device = btrfs_find_device(fs_info, devid,
2392 dev_uuid, disk_super->fsid);
2393
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002394 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002395 if (!device)
2396 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002397 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002398 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002399}
2400
Nikolay Borisov6c050402018-09-03 12:46:13 +03002401static struct btrfs_device *btrfs_find_device_missing_or_by_path(
2402 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002403{
Nikolay Borisov6c050402018-09-03 12:46:13 +03002404 struct btrfs_device *device = NULL;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002405 if (strcmp(device_path, "missing") == 0) {
2406 struct list_head *devices;
2407 struct btrfs_device *tmp;
2408
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002410 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002411 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2412 &tmp->dev_state) && !tmp->bdev) {
Nikolay Borisov6c050402018-09-03 12:46:13 +03002413 device = tmp;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002414 break;
2415 }
2416 }
2417
Nikolay Borisov6c050402018-09-03 12:46:13 +03002418 if (!device)
2419 return ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002420 } else {
Nikolay Borisov6c050402018-09-03 12:46:13 +03002421 device = btrfs_find_device_by_path(fs_info, device_path);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002422 }
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002423
Nikolay Borisov6c050402018-09-03 12:46:13 +03002424 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002425}
2426
Yan Zheng2b820322008-11-17 21:11:30 -05002427/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002428 * Lookup a device given by device id, or the path if the id is 0.
2429 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002430struct btrfs_device *btrfs_find_device_by_devspec(
2431 struct btrfs_fs_info *fs_info, u64 devid, const char *devpath)
Anand Jain24e04742016-02-13 10:01:35 +08002432{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002433 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002434
David Sterba5c5c0df2016-02-15 16:39:55 +01002435 if (devid) {
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002436 device = btrfs_find_device(fs_info, devid, NULL, NULL);
2437 if (!device)
2438 return ERR_PTR(-ENOENT);
Anand Jain24e04742016-02-13 10:01:35 +08002439 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002440 if (!devpath || !devpath[0])
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002441 return ERR_PTR(-EINVAL);
2442 device = btrfs_find_device_missing_or_by_path(fs_info, devpath);
Anand Jain24e04742016-02-13 10:01:35 +08002443 }
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002444 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002445}
2446
Yan Zheng2b820322008-11-17 21:11:30 -05002447/*
2448 * does all the dirty work required for changing file system's UUID.
2449 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002450static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002451{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002452 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002453 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002454 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002456 struct btrfs_device *device;
2457 u64 super_flags;
2458
David Sterbaa32bf9a2018-03-16 02:21:22 +01002459 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002460 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002461 return -EINVAL;
2462
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002463 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002464 if (IS_ERR(seed_devices))
2465 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002466
Yan Zhenge4404d62008-12-12 10:03:26 -05002467 old_devices = clone_fs_devices(fs_devices);
2468 if (IS_ERR(old_devices)) {
2469 kfree(seed_devices);
2470 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002471 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002472
Anand Jainc4babc52018-04-12 10:29:25 +08002473 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002474
Yan Zhenge4404d62008-12-12 10:03:26 -05002475 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2476 seed_devices->opened = 1;
2477 INIT_LIST_HEAD(&seed_devices->devices);
2478 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002479 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002480
Anand Jain321a4bf2018-07-16 22:58:09 +08002481 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002482 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2483 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002484 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002485 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002486
David Sterba34441362016-10-04 19:34:27 +02002487 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002488 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002489 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002490
Yan Zheng2b820322008-11-17 21:11:30 -05002491 fs_devices->seeding = 0;
2492 fs_devices->num_devices = 0;
2493 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002494 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002495 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002496 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002497
2498 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002499 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002500 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002501 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002502
Yan Zheng2b820322008-11-17 21:11:30 -05002503 super_flags = btrfs_super_flags(disk_super) &
2504 ~BTRFS_SUPER_FLAG_SEEDING;
2505 btrfs_set_super_flags(disk_super, super_flags);
2506
2507 return 0;
2508}
2509
2510/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002511 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002512 */
2513static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002514 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002515{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002516 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002517 struct btrfs_path *path;
2518 struct extent_buffer *leaf;
2519 struct btrfs_dev_item *dev_item;
2520 struct btrfs_device *device;
2521 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002522 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002523 u8 dev_uuid[BTRFS_UUID_SIZE];
2524 u64 devid;
2525 int ret;
2526
2527 path = btrfs_alloc_path();
2528 if (!path)
2529 return -ENOMEM;
2530
Yan Zheng2b820322008-11-17 21:11:30 -05002531 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2532 key.offset = 0;
2533 key.type = BTRFS_DEV_ITEM_KEY;
2534
2535 while (1) {
2536 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2537 if (ret < 0)
2538 goto error;
2539
2540 leaf = path->nodes[0];
2541next_slot:
2542 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2543 ret = btrfs_next_leaf(root, path);
2544 if (ret > 0)
2545 break;
2546 if (ret < 0)
2547 goto error;
2548 leaf = path->nodes[0];
2549 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002550 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002551 continue;
2552 }
2553
2554 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2555 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2556 key.type != BTRFS_DEV_ITEM_KEY)
2557 break;
2558
2559 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2560 struct btrfs_dev_item);
2561 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002562 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002563 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002564 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002565 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002566 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002567 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002568
2569 if (device->fs_devices->seeding) {
2570 btrfs_set_device_generation(leaf, dev_item,
2571 device->generation);
2572 btrfs_mark_buffer_dirty(leaf);
2573 }
2574
2575 path->slots[0]++;
2576 goto next_slot;
2577 }
2578 ret = 0;
2579error:
2580 btrfs_free_path(path);
2581 return ret;
2582}
2583
David Sterbada353f62017-02-14 17:55:53 +01002584int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002585{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002586 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002587 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002588 struct btrfs_trans_handle *trans;
2589 struct btrfs_device *device;
2590 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002591 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002592 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002593 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002594 u64 orig_super_total_bytes;
2595 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002596 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002597 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002598 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002599
Anand Jain5da54bc2018-07-03 13:14:50 +08002600 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002601 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002602
Li Zefana5d16332011-12-07 20:08:40 -05002603 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002604 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002605 if (IS_ERR(bdev))
2606 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002607
Anand Jain5da54bc2018-07-03 13:14:50 +08002608 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002609 seeding_dev = 1;
2610 down_write(&sb->s_umount);
2611 mutex_lock(&uuid_mutex);
2612 }
2613
Chris Mason8c8bee12008-09-29 11:19:10 -04002614 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002615
Anand Jain5da54bc2018-07-03 13:14:50 +08002616 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002617 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002618 if (device->bdev == bdev) {
2619 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002620 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002621 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002622 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002623 }
2624 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002625 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002626
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002627 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002628 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002629 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002630 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002631 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002632 }
2633
David Sterba78f2c9e2016-02-11 14:25:38 +01002634 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002635 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002636 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002637 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002638 }
Josef Bacik606686e2012-06-04 14:03:51 -04002639 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002640
Yan, Zhenga22285a2010-05-16 10:48:46 -04002641 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002642 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002643 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002644 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002645 }
2646
Josef Bacikd5e20032011-08-04 14:52:27 +00002647 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002648 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002649 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 device->io_width = fs_info->sectorsize;
2651 device->io_align = fs_info->sectorsize;
2652 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002653 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2654 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002655 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002656 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002657 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002658 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002659 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002660 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002661 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002662 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002663 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002664
Yan Zheng2b820322008-11-17 21:11:30 -05002665 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002666 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002667 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002668 if (ret) {
2669 btrfs_abort_transaction(trans, ret);
2670 goto error_trans;
2671 }
Yan Zheng2b820322008-11-17 21:11:30 -05002672 }
2673
Anand Jain5da54bc2018-07-03 13:14:50 +08002674 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002675
Anand Jain5da54bc2018-07-03 13:14:50 +08002676 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002677 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002678 list_add_rcu(&device->dev_list, &fs_devices->devices);
2679 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2680 fs_devices->num_devices++;
2681 fs_devices->open_devices++;
2682 fs_devices->rw_devices++;
2683 fs_devices->total_devices++;
2684 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002685
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002686 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002687
Anand Jaine884f4f2017-04-04 18:40:19 +08002688 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002689 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002690
Naohiro Aota39379fa2018-07-27 09:04:55 +09002691 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002692 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002693 round_down(orig_super_total_bytes + device->total_bytes,
2694 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002695
Naohiro Aota39379fa2018-07-27 09:04:55 +09002696 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2697 btrfs_set_super_num_devices(fs_info->super_copy,
2698 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002699
2700 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002701 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002702
Miao Xie2196d6e2014-09-03 21:35:41 +08002703 /*
2704 * we've got more storage, clear any full flags on the space
2705 * infos
2706 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002707 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002708
David Sterba34441362016-10-04 19:34:27 +02002709 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002710 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002711
Yan Zheng2b820322008-11-17 21:11:30 -05002712 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002713 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002714 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002715 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002716 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002717 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002718 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002719 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002720 }
2721
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002722 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002723 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002724 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002725 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002726 }
2727
2728 if (seeding_dev) {
2729 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2730
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002731 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002732 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002733 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002734 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002735 }
Anand Jainb2373f22014-06-03 11:36:03 +08002736
2737 /* Sprouting would change fsid of the mounted root,
2738 * so rename the fsid on the sysfs
2739 */
2740 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002741 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002742 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002743 btrfs_warn(fs_info,
2744 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002745 }
2746
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002747 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002748
2749 if (seeding_dev) {
2750 mutex_unlock(&uuid_mutex);
2751 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002752 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002753
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002754 if (ret) /* transaction commit */
2755 return ret;
2756
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002757 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002758 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002759 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002760 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002761 trans = btrfs_attach_transaction(root);
2762 if (IS_ERR(trans)) {
2763 if (PTR_ERR(trans) == -ENOENT)
2764 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002765 ret = PTR_ERR(trans);
2766 trans = NULL;
2767 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002768 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002769 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002770 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002771
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002772 /* Update ctime/mtime for libblkid */
2773 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002774 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002775
Anand Jaind31c32f2017-09-28 14:51:10 +08002776error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002777 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002778 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2779 mutex_lock(&fs_info->chunk_mutex);
2780 list_del_rcu(&device->dev_list);
2781 list_del(&device->dev_alloc_list);
2782 fs_info->fs_devices->num_devices--;
2783 fs_info->fs_devices->open_devices--;
2784 fs_info->fs_devices->rw_devices--;
2785 fs_info->fs_devices->total_devices--;
2786 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2787 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2788 btrfs_set_super_total_bytes(fs_info->super_copy,
2789 orig_super_total_bytes);
2790 btrfs_set_super_num_devices(fs_info->super_copy,
2791 orig_super_num_devices);
2792 mutex_unlock(&fs_info->chunk_mutex);
2793 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002794error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002795 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002796 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002797 if (trans)
2798 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002799error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002800 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002801error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002802 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002803 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002804 mutex_unlock(&uuid_mutex);
2805 up_write(&sb->s_umount);
2806 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002807 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002808}
2809
Chris Masond3977122009-01-05 21:25:51 -05002810static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2811 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002812{
2813 int ret;
2814 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002815 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002816 struct btrfs_dev_item *dev_item;
2817 struct extent_buffer *leaf;
2818 struct btrfs_key key;
2819
Chris Mason0b86a832008-03-24 15:01:56 -04002820 path = btrfs_alloc_path();
2821 if (!path)
2822 return -ENOMEM;
2823
2824 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2825 key.type = BTRFS_DEV_ITEM_KEY;
2826 key.offset = device->devid;
2827
2828 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2829 if (ret < 0)
2830 goto out;
2831
2832 if (ret > 0) {
2833 ret = -ENOENT;
2834 goto out;
2835 }
2836
2837 leaf = path->nodes[0];
2838 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2839
2840 btrfs_set_device_id(leaf, dev_item, device->devid);
2841 btrfs_set_device_type(leaf, dev_item, device->type);
2842 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2843 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2844 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002845 btrfs_set_device_total_bytes(leaf, dev_item,
2846 btrfs_device_get_disk_total_bytes(device));
2847 btrfs_set_device_bytes_used(leaf, dev_item,
2848 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002849 btrfs_mark_buffer_dirty(leaf);
2850
2851out:
2852 btrfs_free_path(path);
2853 return ret;
2854}
2855
Miao Xie2196d6e2014-09-03 21:35:41 +08002856int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002857 struct btrfs_device *device, u64 new_size)
2858{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002859 struct btrfs_fs_info *fs_info = device->fs_info;
2860 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002861 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002862 u64 old_total;
2863 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002864
Anand Jainebbede42017-12-04 12:54:52 +08002865 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002866 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002867
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002868 new_size = round_down(new_size, fs_info->sectorsize);
2869
David Sterba34441362016-10-04 19:34:27 +02002870 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002871 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002872 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002873
Stefan Behrens63a212a2012-11-05 18:29:28 +01002874 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002875 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002876 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002877 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002878 }
Yan Zheng2b820322008-11-17 21:11:30 -05002879
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002880 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002881
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002882 btrfs_set_super_total_bytes(super_copy,
2883 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002884 device->fs_devices->total_rw_bytes += diff;
2885
Miao Xie7cc8e582014-09-03 21:35:38 +08002886 btrfs_device_set_total_bytes(device, new_size);
2887 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002888 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002889 if (list_empty(&device->resized_list))
2890 list_add_tail(&device->resized_list,
2891 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002892 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002893
Chris Mason8f18cf12008-04-25 16:53:30 -04002894 return btrfs_update_device(trans, device);
2895}
2896
Nikolay Borisovf4208792018-07-20 19:37:52 +03002897static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002898{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002899 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002900 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002901 int ret;
2902 struct btrfs_path *path;
2903 struct btrfs_key key;
2904
Chris Mason8f18cf12008-04-25 16:53:30 -04002905 path = btrfs_alloc_path();
2906 if (!path)
2907 return -ENOMEM;
2908
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002909 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002910 key.offset = chunk_offset;
2911 key.type = BTRFS_CHUNK_ITEM_KEY;
2912
2913 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002914 if (ret < 0)
2915 goto out;
2916 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 btrfs_handle_fs_error(fs_info, -ENOENT,
2918 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002919 ret = -ENOENT;
2920 goto out;
2921 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002922
2923 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 btrfs_handle_fs_error(fs_info, ret,
2926 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002927out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002928 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002929 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002930}
2931
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002932static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002935 struct btrfs_disk_key *disk_key;
2936 struct btrfs_chunk *chunk;
2937 u8 *ptr;
2938 int ret = 0;
2939 u32 num_stripes;
2940 u32 array_size;
2941 u32 len = 0;
2942 u32 cur;
2943 struct btrfs_key key;
2944
David Sterba34441362016-10-04 19:34:27 +02002945 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002946 array_size = btrfs_super_sys_array_size(super_copy);
2947
2948 ptr = super_copy->sys_chunk_array;
2949 cur = 0;
2950
2951 while (cur < array_size) {
2952 disk_key = (struct btrfs_disk_key *)ptr;
2953 btrfs_disk_key_to_cpu(&key, disk_key);
2954
2955 len = sizeof(*disk_key);
2956
2957 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2958 chunk = (struct btrfs_chunk *)(ptr + len);
2959 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2960 len += btrfs_chunk_item_size(num_stripes);
2961 } else {
2962 ret = -EIO;
2963 break;
2964 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002965 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002966 key.offset == chunk_offset) {
2967 memmove(ptr, ptr + len, array_size - (cur + len));
2968 array_size -= len;
2969 btrfs_set_super_sys_array_size(super_copy, array_size);
2970 } else {
2971 ptr += len;
2972 cur += len;
2973 }
2974 }
David Sterba34441362016-10-04 19:34:27 +02002975 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002976 return ret;
2977}
2978
Omar Sandoval60ca8422018-05-16 16:34:31 -07002979/*
2980 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2981 * @logical: Logical block offset in bytes.
2982 * @length: Length of extent in bytes.
2983 *
2984 * Return: Chunk mapping or ERR_PTR.
2985 */
2986struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2987 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002988{
2989 struct extent_map_tree *em_tree;
2990 struct extent_map *em;
2991
2992 em_tree = &fs_info->mapping_tree.map_tree;
2993 read_lock(&em_tree->lock);
2994 em = lookup_extent_mapping(em_tree, logical, length);
2995 read_unlock(&em_tree->lock);
2996
2997 if (!em) {
2998 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2999 logical, length);
3000 return ERR_PTR(-EINVAL);
3001 }
3002
3003 if (em->start > logical || em->start + em->len < logical) {
3004 btrfs_crit(fs_info,
3005 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
3006 logical, length, em->start, em->start + em->len);
3007 free_extent_map(em);
3008 return ERR_PTR(-EINVAL);
3009 }
3010
3011 /* callers are responsible for dropping em's ref. */
3012 return em;
3013}
3014
Nikolay Borisov97aff912018-07-20 19:37:53 +03003015int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04003016{
Nikolay Borisov97aff912018-07-20 19:37:53 +03003017 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003018 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003019 struct map_lookup *map;
3020 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003021 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003022 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003023
Omar Sandoval60ca8422018-05-16 16:34:31 -07003024 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07003025 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04003026 /*
3027 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08003028 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04003029 * do anything we still error out.
3030 */
3031 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07003032 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003033 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04003034 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02003035 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003036 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02003037 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003038
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003039 /*
3040 * Take the device list mutex to prevent races with the final phase of
3041 * a device replace operation that replaces the device object associated
3042 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3043 */
3044 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003045 for (i = 0; i < map->num_stripes; i++) {
3046 struct btrfs_device *device = map->stripes[i].dev;
3047 ret = btrfs_free_dev_extent(trans, device,
3048 map->stripes[i].physical,
3049 &dev_extent_len);
3050 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003051 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04003052 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003053 goto out;
3054 }
3055
3056 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003057 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003058 btrfs_device_set_bytes_used(device,
3059 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003060 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003061 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003062 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003063 }
3064
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003065 ret = btrfs_update_device(trans, device);
3066 if (ret) {
3067 mutex_unlock(&fs_devices->device_list_mutex);
3068 btrfs_abort_transaction(trans, ret);
3069 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003070 }
3071 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003072 mutex_unlock(&fs_devices->device_list_mutex);
3073
Nikolay Borisovf4208792018-07-20 19:37:52 +03003074 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003075 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003076 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003077 goto out;
3078 }
3079
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003080 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003081
3082 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003083 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003085 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003086 goto out;
3087 }
3088 }
3089
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003090 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003091 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003092 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003093 goto out;
3094 }
3095
Josef Bacik47ab2a62014-09-18 11:20:02 -04003096out:
3097 /* once for us */
3098 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003099 return ret;
3100}
3101
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003102static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003103{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003104 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003105 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04003106 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003107
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003108 /*
3109 * Prevent races with automatic removal of unused block groups.
3110 * After we relocate and before we remove the chunk with offset
3111 * chunk_offset, automatic removal of the block group can kick in,
3112 * resulting in a failure when calling btrfs_remove_chunk() below.
3113 *
3114 * Make sure to acquire this mutex before doing a tree search (dev
3115 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3116 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3117 * we release the path used to search the chunk/dev tree and before
3118 * the current task acquires this mutex and calls us.
3119 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003120 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003121
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003122 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003123 if (ret)
3124 return -ENOSPC;
3125
Chris Mason8f18cf12008-04-25 16:53:30 -04003126 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003127 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003128 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003129 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003130 if (ret)
3131 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003132
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003133 /*
3134 * We add the kobjects here (and after forcing data chunk creation)
3135 * since relocation is the only place we'll create chunks of a new
3136 * type at runtime. The only place where we'll remove the last
3137 * chunk of a type is the call immediately below this one. Even
3138 * so, we're protected against races with the cleaner thread since
3139 * we're covered by the delete_unused_bgs_mutex.
3140 */
3141 btrfs_add_raid_kobjects(fs_info);
3142
Chris Mason19c4d2f2016-10-10 13:43:31 -07003143 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3144 chunk_offset);
3145 if (IS_ERR(trans)) {
3146 ret = PTR_ERR(trans);
3147 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3148 return ret;
3149 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003150
Chris Mason19c4d2f2016-10-10 13:43:31 -07003151 /*
3152 * step two, delete the device extents and the
3153 * chunk tree entries
3154 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003155 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003156 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003157 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003158}
3159
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003160static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003161{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003162 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003163 struct btrfs_path *path;
3164 struct extent_buffer *leaf;
3165 struct btrfs_chunk *chunk;
3166 struct btrfs_key key;
3167 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003168 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003169 bool retried = false;
3170 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003171 int ret;
3172
3173 path = btrfs_alloc_path();
3174 if (!path)
3175 return -ENOMEM;
3176
Josef Bacikba1bf482009-09-11 16:11:19 -04003177again:
Yan Zheng2b820322008-11-17 21:11:30 -05003178 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3179 key.offset = (u64)-1;
3180 key.type = BTRFS_CHUNK_ITEM_KEY;
3181
3182 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003183 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003184 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003185 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003186 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003187 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003188 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003189 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003190
3191 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3192 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003193 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003194 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003195 if (ret < 0)
3196 goto error;
3197 if (ret > 0)
3198 break;
3199
3200 leaf = path->nodes[0];
3201 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3202
3203 chunk = btrfs_item_ptr(leaf, path->slots[0],
3204 struct btrfs_chunk);
3205 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003206 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003207
3208 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003209 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003210 if (ret == -ENOSPC)
3211 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303212 else
3213 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003214 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003215 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003216
3217 if (found_key.offset == 0)
3218 break;
3219 key.offset = found_key.offset - 1;
3220 }
3221 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003222 if (failed && !retried) {
3223 failed = 0;
3224 retried = true;
3225 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303226 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003227 ret = -ENOSPC;
3228 }
Yan Zheng2b820322008-11-17 21:11:30 -05003229error:
3230 btrfs_free_path(path);
3231 return ret;
3232}
3233
Liu Boa6f93c72017-11-15 16:28:11 -07003234/*
3235 * return 1 : allocate a data chunk successfully,
3236 * return <0: errors during allocating a data chunk,
3237 * return 0 : no need to allocate a data chunk.
3238 */
3239static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3240 u64 chunk_offset)
3241{
3242 struct btrfs_block_group_cache *cache;
3243 u64 bytes_used;
3244 u64 chunk_type;
3245
3246 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3247 ASSERT(cache);
3248 chunk_type = cache->flags;
3249 btrfs_put_block_group(cache);
3250
3251 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3252 spin_lock(&fs_info->data_sinfo->lock);
3253 bytes_used = fs_info->data_sinfo->bytes_used;
3254 spin_unlock(&fs_info->data_sinfo->lock);
3255
3256 if (!bytes_used) {
3257 struct btrfs_trans_handle *trans;
3258 int ret;
3259
3260 trans = btrfs_join_transaction(fs_info->tree_root);
3261 if (IS_ERR(trans))
3262 return PTR_ERR(trans);
3263
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003264 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003265 BTRFS_BLOCK_GROUP_DATA);
3266 btrfs_end_transaction(trans);
3267 if (ret < 0)
3268 return ret;
3269
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003270 btrfs_add_raid_kobjects(fs_info);
3271
Liu Boa6f93c72017-11-15 16:28:11 -07003272 return 1;
3273 }
3274 }
3275 return 0;
3276}
3277
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003278static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003279 struct btrfs_balance_control *bctl)
3280{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003281 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003282 struct btrfs_trans_handle *trans;
3283 struct btrfs_balance_item *item;
3284 struct btrfs_disk_balance_args disk_bargs;
3285 struct btrfs_path *path;
3286 struct extent_buffer *leaf;
3287 struct btrfs_key key;
3288 int ret, err;
3289
3290 path = btrfs_alloc_path();
3291 if (!path)
3292 return -ENOMEM;
3293
3294 trans = btrfs_start_transaction(root, 0);
3295 if (IS_ERR(trans)) {
3296 btrfs_free_path(path);
3297 return PTR_ERR(trans);
3298 }
3299
3300 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003301 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003302 key.offset = 0;
3303
3304 ret = btrfs_insert_empty_item(trans, root, path, &key,
3305 sizeof(*item));
3306 if (ret)
3307 goto out;
3308
3309 leaf = path->nodes[0];
3310 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3311
David Sterbab159fa22016-11-08 18:09:03 +01003312 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003313
3314 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3315 btrfs_set_balance_data(leaf, item, &disk_bargs);
3316 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3317 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3318 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3319 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3320
3321 btrfs_set_balance_flags(leaf, item, bctl->flags);
3322
3323 btrfs_mark_buffer_dirty(leaf);
3324out:
3325 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003326 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003327 if (err && !ret)
3328 ret = err;
3329 return ret;
3330}
3331
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003332static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003333{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003334 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003335 struct btrfs_trans_handle *trans;
3336 struct btrfs_path *path;
3337 struct btrfs_key key;
3338 int ret, err;
3339
3340 path = btrfs_alloc_path();
3341 if (!path)
3342 return -ENOMEM;
3343
3344 trans = btrfs_start_transaction(root, 0);
3345 if (IS_ERR(trans)) {
3346 btrfs_free_path(path);
3347 return PTR_ERR(trans);
3348 }
3349
3350 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003351 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003352 key.offset = 0;
3353
3354 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3355 if (ret < 0)
3356 goto out;
3357 if (ret > 0) {
3358 ret = -ENOENT;
3359 goto out;
3360 }
3361
3362 ret = btrfs_del_item(trans, root, path);
3363out:
3364 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003365 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003366 if (err && !ret)
3367 ret = err;
3368 return ret;
3369}
3370
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003371/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003372 * This is a heuristic used to reduce the number of chunks balanced on
3373 * resume after balance was interrupted.
3374 */
3375static void update_balance_args(struct btrfs_balance_control *bctl)
3376{
3377 /*
3378 * Turn on soft mode for chunk types that were being converted.
3379 */
3380 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3381 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3382 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3383 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3384 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3385 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3386
3387 /*
3388 * Turn on usage filter if is not already used. The idea is
3389 * that chunks that we have already balanced should be
3390 * reasonably full. Don't do it for chunks that are being
3391 * converted - that will keep us from relocating unconverted
3392 * (albeit full) chunks.
3393 */
3394 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003395 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003396 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3397 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3398 bctl->data.usage = 90;
3399 }
3400 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003401 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003402 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3403 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3404 bctl->sys.usage = 90;
3405 }
3406 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003407 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003408 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3409 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3410 bctl->meta.usage = 90;
3411 }
3412}
3413
3414/*
David Sterba149196a2018-03-20 20:23:09 +01003415 * Clear the balance status in fs_info and delete the balance item from disk.
3416 */
3417static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003418{
3419 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003420 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003421
3422 BUG_ON(!fs_info->balance_ctl);
3423
3424 spin_lock(&fs_info->balance_lock);
3425 fs_info->balance_ctl = NULL;
3426 spin_unlock(&fs_info->balance_lock);
3427
3428 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003429 ret = del_balance_item(fs_info);
3430 if (ret)
3431 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003432}
3433
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003434/*
3435 * Balance filters. Return 1 if chunk should be filtered out
3436 * (should not be balanced).
3437 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003438static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003439 struct btrfs_balance_args *bargs)
3440{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003441 chunk_type = chunk_to_extended(chunk_type) &
3442 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003443
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003444 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003445 return 0;
3446
3447 return 1;
3448}
3449
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003450static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003451 struct btrfs_balance_args *bargs)
3452{
3453 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003454 u64 chunk_used;
3455 u64 user_thresh_min;
3456 u64 user_thresh_max;
3457 int ret = 1;
3458
3459 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3460 chunk_used = btrfs_block_group_used(&cache->item);
3461
3462 if (bargs->usage_min == 0)
3463 user_thresh_min = 0;
3464 else
3465 user_thresh_min = div_factor_fine(cache->key.offset,
3466 bargs->usage_min);
3467
3468 if (bargs->usage_max == 0)
3469 user_thresh_max = 1;
3470 else if (bargs->usage_max > 100)
3471 user_thresh_max = cache->key.offset;
3472 else
3473 user_thresh_max = div_factor_fine(cache->key.offset,
3474 bargs->usage_max);
3475
3476 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3477 ret = 0;
3478
3479 btrfs_put_block_group(cache);
3480 return ret;
3481}
3482
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003483static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003484 u64 chunk_offset, struct btrfs_balance_args *bargs)
3485{
3486 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003487 u64 chunk_used, user_thresh;
3488 int ret = 1;
3489
3490 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3491 chunk_used = btrfs_block_group_used(&cache->item);
3492
David Sterbabc309462015-10-20 18:22:13 +02003493 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003494 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003495 else if (bargs->usage > 100)
3496 user_thresh = cache->key.offset;
3497 else
3498 user_thresh = div_factor_fine(cache->key.offset,
3499 bargs->usage);
3500
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003501 if (chunk_used < user_thresh)
3502 ret = 0;
3503
3504 btrfs_put_block_group(cache);
3505 return ret;
3506}
3507
Ilya Dryomov409d4042012-01-16 22:04:47 +02003508static int chunk_devid_filter(struct extent_buffer *leaf,
3509 struct btrfs_chunk *chunk,
3510 struct btrfs_balance_args *bargs)
3511{
3512 struct btrfs_stripe *stripe;
3513 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3514 int i;
3515
3516 for (i = 0; i < num_stripes; i++) {
3517 stripe = btrfs_stripe_nr(chunk, i);
3518 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3519 return 0;
3520 }
3521
3522 return 1;
3523}
3524
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003525/* [pstart, pend) */
3526static int chunk_drange_filter(struct extent_buffer *leaf,
3527 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003528 struct btrfs_balance_args *bargs)
3529{
3530 struct btrfs_stripe *stripe;
3531 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3532 u64 stripe_offset;
3533 u64 stripe_length;
3534 int factor;
3535 int i;
3536
3537 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3538 return 0;
3539
3540 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003541 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3542 factor = num_stripes / 2;
3543 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3544 factor = num_stripes - 1;
3545 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3546 factor = num_stripes - 2;
3547 } else {
3548 factor = num_stripes;
3549 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003550
3551 for (i = 0; i < num_stripes; i++) {
3552 stripe = btrfs_stripe_nr(chunk, i);
3553 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3554 continue;
3555
3556 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3557 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003558 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003559
3560 if (stripe_offset < bargs->pend &&
3561 stripe_offset + stripe_length > bargs->pstart)
3562 return 0;
3563 }
3564
3565 return 1;
3566}
3567
Ilya Dryomovea671762012-01-16 22:04:48 +02003568/* [vstart, vend) */
3569static int chunk_vrange_filter(struct extent_buffer *leaf,
3570 struct btrfs_chunk *chunk,
3571 u64 chunk_offset,
3572 struct btrfs_balance_args *bargs)
3573{
3574 if (chunk_offset < bargs->vend &&
3575 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3576 /* at least part of the chunk is inside this vrange */
3577 return 0;
3578
3579 return 1;
3580}
3581
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003582static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3583 struct btrfs_chunk *chunk,
3584 struct btrfs_balance_args *bargs)
3585{
3586 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3587
3588 if (bargs->stripes_min <= num_stripes
3589 && num_stripes <= bargs->stripes_max)
3590 return 0;
3591
3592 return 1;
3593}
3594
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003595static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003596 struct btrfs_balance_args *bargs)
3597{
3598 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3599 return 0;
3600
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003601 chunk_type = chunk_to_extended(chunk_type) &
3602 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003603
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003604 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003605 return 1;
3606
3607 return 0;
3608}
3609
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003610static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003611 struct extent_buffer *leaf,
3612 struct btrfs_chunk *chunk, u64 chunk_offset)
3613{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003614 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003615 struct btrfs_balance_args *bargs = NULL;
3616 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3617
3618 /* type filter */
3619 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3620 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3621 return 0;
3622 }
3623
3624 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3625 bargs = &bctl->data;
3626 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3627 bargs = &bctl->sys;
3628 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3629 bargs = &bctl->meta;
3630
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003631 /* profiles filter */
3632 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3633 chunk_profiles_filter(chunk_type, bargs)) {
3634 return 0;
3635 }
3636
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003637 /* usage filter */
3638 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003639 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003640 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003641 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003642 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003643 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003644 }
3645
Ilya Dryomov409d4042012-01-16 22:04:47 +02003646 /* devid filter */
3647 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3648 chunk_devid_filter(leaf, chunk, bargs)) {
3649 return 0;
3650 }
3651
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003652 /* drange filter, makes sense only with devid filter */
3653 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003654 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003655 return 0;
3656 }
3657
Ilya Dryomovea671762012-01-16 22:04:48 +02003658 /* vrange filter */
3659 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3660 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3661 return 0;
3662 }
3663
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003664 /* stripes filter */
3665 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3666 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3667 return 0;
3668 }
3669
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003670 /* soft profile changing mode */
3671 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3672 chunk_soft_convert_filter(chunk_type, bargs)) {
3673 return 0;
3674 }
3675
David Sterba7d824b62014-05-07 17:37:51 +02003676 /*
3677 * limited by count, must be the last filter
3678 */
3679 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3680 if (bargs->limit == 0)
3681 return 0;
3682 else
3683 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003684 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3685 /*
3686 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003687 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003688 * about the count of all chunks that satisfy the filters.
3689 */
3690 if (bargs->limit_max == 0)
3691 return 0;
3692 else
3693 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003694 }
3695
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003696 return 1;
3697}
3698
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003699static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003700{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003701 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003702 struct btrfs_root *chunk_root = fs_info->chunk_root;
3703 struct btrfs_root *dev_root = fs_info->dev_root;
3704 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003705 struct btrfs_device *device;
3706 u64 old_size;
3707 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003708 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003709 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003710 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003711 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003712 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003713 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003714 struct extent_buffer *leaf;
3715 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003716 int ret;
3717 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003718 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003719 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003720 u64 limit_data = bctl->data.limit;
3721 u64 limit_meta = bctl->meta.limit;
3722 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003723 u32 count_data = 0;
3724 u32 count_meta = 0;
3725 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003726 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003727
Chris Masonec44a352008-04-28 15:29:52 -04003728 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003729 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003730 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003731 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003732 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003733 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003734 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003735 btrfs_device_get_total_bytes(device) -
3736 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003737 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003738 continue;
3739
3740 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003741 if (ret == -ENOSPC)
3742 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003743 if (ret) {
3744 /* btrfs_shrink_device never returns ret > 0 */
3745 WARN_ON(ret > 0);
3746 goto error;
3747 }
Chris Masonec44a352008-04-28 15:29:52 -04003748
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003750 if (IS_ERR(trans)) {
3751 ret = PTR_ERR(trans);
3752 btrfs_info_in_rcu(fs_info,
3753 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3754 rcu_str_deref(device->name), ret,
3755 old_size, old_size - size_to_free);
3756 goto error;
3757 }
Chris Masonec44a352008-04-28 15:29:52 -04003758
3759 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003760 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003761 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003762 /* btrfs_grow_device never returns ret > 0 */
3763 WARN_ON(ret > 0);
3764 btrfs_info_in_rcu(fs_info,
3765 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3766 rcu_str_deref(device->name), ret,
3767 old_size, old_size - size_to_free);
3768 goto error;
3769 }
Chris Masonec44a352008-04-28 15:29:52 -04003770
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003771 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003772 }
3773
3774 /* step two, relocate all the chunks */
3775 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003776 if (!path) {
3777 ret = -ENOMEM;
3778 goto error;
3779 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003780
3781 /* zero out stat counters */
3782 spin_lock(&fs_info->balance_lock);
3783 memset(&bctl->stat, 0, sizeof(bctl->stat));
3784 spin_unlock(&fs_info->balance_lock);
3785again:
David Sterba7d824b62014-05-07 17:37:51 +02003786 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003787 /*
3788 * The single value limit and min/max limits use the same bytes
3789 * in the
3790 */
David Sterba7d824b62014-05-07 17:37:51 +02003791 bctl->data.limit = limit_data;
3792 bctl->meta.limit = limit_meta;
3793 bctl->sys.limit = limit_sys;
3794 }
Chris Masonec44a352008-04-28 15:29:52 -04003795 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3796 key.offset = (u64)-1;
3797 key.type = BTRFS_CHUNK_ITEM_KEY;
3798
Chris Masond3977122009-01-05 21:25:51 -05003799 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003800 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003801 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003802 ret = -ECANCELED;
3803 goto error;
3804 }
3805
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003806 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003807 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003808 if (ret < 0) {
3809 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003810 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003811 }
Chris Masonec44a352008-04-28 15:29:52 -04003812
3813 /*
3814 * this shouldn't happen, it means the last relocate
3815 * failed
3816 */
3817 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003818 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003819
3820 ret = btrfs_previous_item(chunk_root, path, 0,
3821 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003822 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003823 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003824 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003825 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003826 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003827
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003828 leaf = path->nodes[0];
3829 slot = path->slots[0];
3830 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3831
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003832 if (found_key.objectid != key.objectid) {
3833 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003834 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003835 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003836
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003837 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003838 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003839
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003840 if (!counting) {
3841 spin_lock(&fs_info->balance_lock);
3842 bctl->stat.considered++;
3843 spin_unlock(&fs_info->balance_lock);
3844 }
3845
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003846 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003847 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003848
David Sterbab3b4aa72011-04-21 01:20:15 +02003849 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003850 if (!ret) {
3851 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003852 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003853 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003854
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003855 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003856 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003857 spin_lock(&fs_info->balance_lock);
3858 bctl->stat.expected++;
3859 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003860
3861 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3862 count_data++;
3863 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3864 count_sys++;
3865 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3866 count_meta++;
3867
3868 goto loop;
3869 }
3870
3871 /*
3872 * Apply limit_min filter, no need to check if the LIMITS
3873 * filter is used, limit_min is 0 by default
3874 */
3875 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3876 count_data < bctl->data.limit_min)
3877 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3878 count_meta < bctl->meta.limit_min)
3879 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3880 count_sys < bctl->sys.limit_min)) {
3881 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003882 goto loop;
3883 }
3884
Liu Boa6f93c72017-11-15 16:28:11 -07003885 if (!chunk_reserved) {
3886 /*
3887 * We may be relocating the only data chunk we have,
3888 * which could potentially end up with losing data's
3889 * raid profile, so lets allocate an empty one in
3890 * advance.
3891 */
3892 ret = btrfs_may_alloc_data_chunk(fs_info,
3893 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003894 if (ret < 0) {
3895 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3896 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003897 } else if (ret == 1) {
3898 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003899 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003900 }
3901
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003902 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003903 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003904 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003905 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003906 } else if (ret == -ETXTBSY) {
3907 btrfs_info(fs_info,
3908 "skipping relocation of block group %llu due to active swapfile",
3909 found_key.offset);
3910 ret = 0;
3911 } else if (ret) {
3912 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003913 } else {
3914 spin_lock(&fs_info->balance_lock);
3915 bctl->stat.completed++;
3916 spin_unlock(&fs_info->balance_lock);
3917 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003918loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003919 if (found_key.offset == 0)
3920 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003921 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003922 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003923
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003924 if (counting) {
3925 btrfs_release_path(path);
3926 counting = false;
3927 goto again;
3928 }
Chris Masonec44a352008-04-28 15:29:52 -04003929error:
3930 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003931 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003932 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003933 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003934 if (!ret)
3935 ret = -ENOSPC;
3936 }
3937
Chris Masonec44a352008-04-28 15:29:52 -04003938 return ret;
3939}
3940
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003941/**
3942 * alloc_profile_is_valid - see if a given profile is valid and reduced
3943 * @flags: profile to validate
3944 * @extended: if true @flags is treated as an extended profile
3945 */
3946static int alloc_profile_is_valid(u64 flags, int extended)
3947{
3948 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3949 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3950
3951 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3952
3953 /* 1) check that all other bits are zeroed */
3954 if (flags & ~mask)
3955 return 0;
3956
3957 /* 2) see if profile is reduced */
3958 if (flags == 0)
3959 return !extended; /* "0" is valid for usual profiles */
3960
3961 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003962 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003963}
3964
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003965static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3966{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003967 /* cancel requested || normal exit path */
3968 return atomic_read(&fs_info->balance_cancel_req) ||
3969 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3970 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003971}
3972
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003973/* Non-zero return value signifies invalidity */
3974static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3975 u64 allowed)
3976{
3977 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3978 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3979 (bctl_arg->target & ~allowed)));
3980}
3981
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003982/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003983 * Fill @buf with textual description of balance filter flags @bargs, up to
3984 * @size_buf including the terminating null. The output may be trimmed if it
3985 * does not fit into the provided buffer.
3986 */
3987static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3988 u32 size_buf)
3989{
3990 int ret;
3991 u32 size_bp = size_buf;
3992 char *bp = buf;
3993 u64 flags = bargs->flags;
3994 char tmp_buf[128] = {'\0'};
3995
3996 if (!flags)
3997 return;
3998
3999#define CHECK_APPEND_NOARG(a) \
4000 do { \
4001 ret = snprintf(bp, size_bp, (a)); \
4002 if (ret < 0 || ret >= size_bp) \
4003 goto out_overflow; \
4004 size_bp -= ret; \
4005 bp += ret; \
4006 } while (0)
4007
4008#define CHECK_APPEND_1ARG(a, v1) \
4009 do { \
4010 ret = snprintf(bp, size_bp, (a), (v1)); \
4011 if (ret < 0 || ret >= size_bp) \
4012 goto out_overflow; \
4013 size_bp -= ret; \
4014 bp += ret; \
4015 } while (0)
4016
4017#define CHECK_APPEND_2ARG(a, v1, v2) \
4018 do { \
4019 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
4020 if (ret < 0 || ret >= size_bp) \
4021 goto out_overflow; \
4022 size_bp -= ret; \
4023 bp += ret; \
4024 } while (0)
4025
4026 if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
4027 int index = btrfs_bg_flags_to_raid_index(bargs->target);
4028
4029 CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
4030 }
4031
4032 if (flags & BTRFS_BALANCE_ARGS_SOFT)
4033 CHECK_APPEND_NOARG("soft,");
4034
4035 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
4036 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
4037 sizeof(tmp_buf));
4038 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
4039 }
4040
4041 if (flags & BTRFS_BALANCE_ARGS_USAGE)
4042 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
4043
4044 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
4045 CHECK_APPEND_2ARG("usage=%u..%u,",
4046 bargs->usage_min, bargs->usage_max);
4047
4048 if (flags & BTRFS_BALANCE_ARGS_DEVID)
4049 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
4050
4051 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
4052 CHECK_APPEND_2ARG("drange=%llu..%llu,",
4053 bargs->pstart, bargs->pend);
4054
4055 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
4056 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
4057 bargs->vstart, bargs->vend);
4058
4059 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
4060 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
4061
4062 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
4063 CHECK_APPEND_2ARG("limit=%u..%u,",
4064 bargs->limit_min, bargs->limit_max);
4065
4066 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
4067 CHECK_APPEND_2ARG("stripes=%u..%u,",
4068 bargs->stripes_min, bargs->stripes_max);
4069
4070#undef CHECK_APPEND_2ARG
4071#undef CHECK_APPEND_1ARG
4072#undef CHECK_APPEND_NOARG
4073
4074out_overflow:
4075
4076 if (size_bp < size_buf)
4077 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
4078 else
4079 buf[0] = '\0';
4080}
4081
4082static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
4083{
4084 u32 size_buf = 1024;
4085 char tmp_buf[192] = {'\0'};
4086 char *buf;
4087 char *bp;
4088 u32 size_bp = size_buf;
4089 int ret;
4090 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4091
4092 buf = kzalloc(size_buf, GFP_KERNEL);
4093 if (!buf)
4094 return;
4095
4096 bp = buf;
4097
4098#define CHECK_APPEND_1ARG(a, v1) \
4099 do { \
4100 ret = snprintf(bp, size_bp, (a), (v1)); \
4101 if (ret < 0 || ret >= size_bp) \
4102 goto out_overflow; \
4103 size_bp -= ret; \
4104 bp += ret; \
4105 } while (0)
4106
4107 if (bctl->flags & BTRFS_BALANCE_FORCE)
4108 CHECK_APPEND_1ARG("%s", "-f ");
4109
4110 if (bctl->flags & BTRFS_BALANCE_DATA) {
4111 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4112 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4113 }
4114
4115 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4116 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4117 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4118 }
4119
4120 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4121 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4122 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4123 }
4124
4125#undef CHECK_APPEND_1ARG
4126
4127out_overflow:
4128
4129 if (size_bp < size_buf)
4130 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4131 btrfs_info(fs_info, "balance: %s %s",
4132 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4133 "resume" : "start", buf);
4134
4135 kfree(buf);
4136}
4137
4138/*
David Sterbadccdb072018-03-21 00:20:05 +01004139 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004140 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004141int btrfs_balance(struct btrfs_fs_info *fs_info,
4142 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004143 struct btrfs_ioctl_balance_args *bargs)
4144{
Adam Borowski14506122017-03-07 23:34:44 +01004145 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004146 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004147 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004148 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004149 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004150 unsigned seq;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004151 bool reducing_integrity;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004152
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004153 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004154 atomic_read(&fs_info->balance_pause_req) ||
4155 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004156 ret = -EINVAL;
4157 goto out;
4158 }
4159
Ilya Dryomove4837f82012-03-27 17:09:17 +03004160 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4161 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4162 mixed = 1;
4163
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004164 /*
4165 * In case of mixed groups both data and meta should be picked,
4166 * and identical options should be given for both of them.
4167 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004168 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4169 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004170 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4171 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4172 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004173 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004174 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004175 ret = -EINVAL;
4176 goto out;
4177 }
4178 }
4179
Anand Jain1da73962018-08-10 13:53:21 +08004180 num_devices = btrfs_num_devices(fs_info);
4181
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04004182 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
4183 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004184 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00004185 if (num_devices > 2)
4186 allowed |= BTRFS_BLOCK_GROUP_RAID5;
4187 if (num_devices > 3)
4188 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
4189 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004190 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004191 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
4192
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004193 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004194 "balance: invalid convert data profile %s",
4195 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004196 ret = -EINVAL;
4197 goto out;
4198 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004199 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004200 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
4201
Frank Holtonefe120a2013-12-20 11:37:06 -05004202 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004203 "balance: invalid convert metadata profile %s",
4204 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004205 ret = -EINVAL;
4206 goto out;
4207 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004208 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004209 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
4210
Frank Holtonefe120a2013-12-20 11:37:06 -05004211 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004212 "balance: invalid convert system profile %s",
4213 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004214 ret = -EINVAL;
4215 goto out;
4216 }
4217
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004218 /* allow to reduce meta or sys integrity only if force set */
4219 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004220 BTRFS_BLOCK_GROUP_RAID10 |
4221 BTRFS_BLOCK_GROUP_RAID5 |
4222 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00004223 do {
4224 seq = read_seqbegin(&fs_info->profiles_lock);
4225
4226 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4227 (fs_info->avail_system_alloc_bits & allowed) &&
4228 !(bctl->sys.target & allowed)) ||
4229 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4230 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004231 !(bctl->meta.target & allowed)))
4232 reducing_integrity = true;
4233 else
4234 reducing_integrity = false;
4235
4236 /* if we're not converting, the target field is uninitialized */
4237 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4238 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4239 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4240 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004241 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004242
Filipe Manana5a8067c2018-11-19 09:48:12 +00004243 if (reducing_integrity) {
4244 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4245 btrfs_info(fs_info,
4246 "balance: force reducing metadata integrity");
4247 } else {
4248 btrfs_err(fs_info,
4249 "balance: reduces metadata integrity, use --force if you want this");
4250 ret = -EINVAL;
4251 goto out;
4252 }
4253 }
4254
Adam Borowski14506122017-03-07 23:34:44 +01004255 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4256 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004257 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
4258 int data_index = btrfs_bg_flags_to_raid_index(data_target);
4259
Sam Tygieree592d02016-01-06 08:46:12 +00004260 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004261 "balance: metadata profile %s has lower redundancy than data profile %s",
4262 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00004263 }
4264
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004265 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004266 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004267 goto out;
4268
Ilya Dryomov59641012012-01-16 22:04:48 +02004269 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4270 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004271 BUG_ON(fs_info->balance_ctl);
4272 spin_lock(&fs_info->balance_lock);
4273 fs_info->balance_ctl = bctl;
4274 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004275 } else {
4276 BUG_ON(ret != -EEXIST);
4277 spin_lock(&fs_info->balance_lock);
4278 update_balance_args(bctl);
4279 spin_unlock(&fs_info->balance_lock);
4280 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004281
David Sterba3009a622018-03-21 01:31:04 +01004282 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4283 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004284 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004285 mutex_unlock(&fs_info->balance_mutex);
4286
4287 ret = __btrfs_balance(fs_info);
4288
4289 mutex_lock(&fs_info->balance_mutex);
David Sterba3009a622018-03-21 01:31:04 +01004290 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004291
4292 if (bargs) {
4293 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004294 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004295 }
4296
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004297 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4298 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004299 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004300 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004301 }
4302
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004303 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004304
4305 return ret;
4306out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004307 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004308 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004309 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004310 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004311 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4312
Ilya Dryomov59641012012-01-16 22:04:48 +02004313 return ret;
4314}
4315
4316static int balance_kthread(void *data)
4317{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004318 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004319 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004320
Ilya Dryomov59641012012-01-16 22:04:48 +02004321 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004322 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004323 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004324 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004325
Ilya Dryomov59641012012-01-16 22:04:48 +02004326 return ret;
4327}
4328
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004329int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4330{
4331 struct task_struct *tsk;
4332
David Sterba1354e1a2018-03-21 02:29:13 +01004333 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004334 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004335 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004336 return 0;
4337 }
David Sterba1354e1a2018-03-21 02:29:13 +01004338 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004339
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004340 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004341 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004342 return 0;
4343 }
4344
Anand Jain02ee6542018-05-17 15:16:51 +08004345 /*
4346 * A ro->rw remount sequence should continue with the paused balance
4347 * regardless of who pauses it, system or the user as of now, so set
4348 * the resume flag.
4349 */
4350 spin_lock(&fs_info->balance_lock);
4351 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4352 spin_unlock(&fs_info->balance_lock);
4353
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004354 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304355 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004356}
4357
Ilya Dryomov68310a52012-06-22 12:24:12 -06004358int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004359{
Ilya Dryomov59641012012-01-16 22:04:48 +02004360 struct btrfs_balance_control *bctl;
4361 struct btrfs_balance_item *item;
4362 struct btrfs_disk_balance_args disk_bargs;
4363 struct btrfs_path *path;
4364 struct extent_buffer *leaf;
4365 struct btrfs_key key;
4366 int ret;
4367
4368 path = btrfs_alloc_path();
4369 if (!path)
4370 return -ENOMEM;
4371
Ilya Dryomov68310a52012-06-22 12:24:12 -06004372 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004373 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004374 key.offset = 0;
4375
4376 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4377 if (ret < 0)
4378 goto out;
4379 if (ret > 0) { /* ret = -ENOENT; */
4380 ret = 0;
4381 goto out;
4382 }
4383
Ilya Dryomov59641012012-01-16 22:04:48 +02004384 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4385 if (!bctl) {
4386 ret = -ENOMEM;
4387 goto out;
4388 }
4389
Ilya Dryomov59641012012-01-16 22:04:48 +02004390 leaf = path->nodes[0];
4391 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4392
Ilya Dryomov68310a52012-06-22 12:24:12 -06004393 bctl->flags = btrfs_balance_flags(leaf, item);
4394 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004395
4396 btrfs_balance_data(leaf, item, &disk_bargs);
4397 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4398 btrfs_balance_meta(leaf, item, &disk_bargs);
4399 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4400 btrfs_balance_sys(leaf, item, &disk_bargs);
4401 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4402
David Sterbaeee95e32018-03-20 20:07:58 +01004403 /*
4404 * This should never happen, as the paused balance state is recovered
4405 * during mount without any chance of other exclusive ops to collide.
4406 *
4407 * This gives the exclusive op status to balance and keeps in paused
4408 * state until user intervention (cancel or umount). If the ownership
4409 * cannot be assigned, show a message but do not fail. The balance
4410 * is in a paused state and must have fs_info::balance_ctl properly
4411 * set up.
4412 */
4413 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4414 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004415 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004416
Ilya Dryomov68310a52012-06-22 12:24:12 -06004417 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004418 BUG_ON(fs_info->balance_ctl);
4419 spin_lock(&fs_info->balance_lock);
4420 fs_info->balance_ctl = bctl;
4421 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004422 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004423out:
4424 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004425 return ret;
4426}
4427
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004428int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4429{
4430 int ret = 0;
4431
4432 mutex_lock(&fs_info->balance_mutex);
4433 if (!fs_info->balance_ctl) {
4434 mutex_unlock(&fs_info->balance_mutex);
4435 return -ENOTCONN;
4436 }
4437
David Sterba3009a622018-03-21 01:31:04 +01004438 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004439 atomic_inc(&fs_info->balance_pause_req);
4440 mutex_unlock(&fs_info->balance_mutex);
4441
4442 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004443 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004444
4445 mutex_lock(&fs_info->balance_mutex);
4446 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004447 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004448 atomic_dec(&fs_info->balance_pause_req);
4449 } else {
4450 ret = -ENOTCONN;
4451 }
4452
4453 mutex_unlock(&fs_info->balance_mutex);
4454 return ret;
4455}
4456
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004457int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4458{
4459 mutex_lock(&fs_info->balance_mutex);
4460 if (!fs_info->balance_ctl) {
4461 mutex_unlock(&fs_info->balance_mutex);
4462 return -ENOTCONN;
4463 }
4464
David Sterbacf7d20f2018-03-21 01:45:32 +01004465 /*
4466 * A paused balance with the item stored on disk can be resumed at
4467 * mount time if the mount is read-write. Otherwise it's still paused
4468 * and we must not allow cancelling as it deletes the item.
4469 */
4470 if (sb_rdonly(fs_info->sb)) {
4471 mutex_unlock(&fs_info->balance_mutex);
4472 return -EROFS;
4473 }
4474
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004475 atomic_inc(&fs_info->balance_cancel_req);
4476 /*
4477 * if we are running just wait and return, balance item is
4478 * deleted in btrfs_balance in this case
4479 */
David Sterba3009a622018-03-21 01:31:04 +01004480 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004481 mutex_unlock(&fs_info->balance_mutex);
4482 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004483 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004484 mutex_lock(&fs_info->balance_mutex);
4485 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004486 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004487 /*
4488 * Lock released to allow other waiters to continue, we'll
4489 * reexamine the status again.
4490 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004491 mutex_lock(&fs_info->balance_mutex);
4492
David Sterbaa17c95d2018-03-20 17:28:05 +01004493 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004494 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004495 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004496 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004497 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004498 }
4499
David Sterba3009a622018-03-21 01:31:04 +01004500 BUG_ON(fs_info->balance_ctl ||
4501 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004502 atomic_dec(&fs_info->balance_cancel_req);
4503 mutex_unlock(&fs_info->balance_mutex);
4504 return 0;
4505}
4506
Stefan Behrens803b2f52013-08-15 17:11:21 +02004507static int btrfs_uuid_scan_kthread(void *data)
4508{
4509 struct btrfs_fs_info *fs_info = data;
4510 struct btrfs_root *root = fs_info->tree_root;
4511 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004512 struct btrfs_path *path = NULL;
4513 int ret = 0;
4514 struct extent_buffer *eb;
4515 int slot;
4516 struct btrfs_root_item root_item;
4517 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004518 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004519
4520 path = btrfs_alloc_path();
4521 if (!path) {
4522 ret = -ENOMEM;
4523 goto out;
4524 }
4525
4526 key.objectid = 0;
4527 key.type = BTRFS_ROOT_ITEM_KEY;
4528 key.offset = 0;
4529
Stefan Behrens803b2f52013-08-15 17:11:21 +02004530 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004531 ret = btrfs_search_forward(root, &key, path,
4532 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004533 if (ret) {
4534 if (ret > 0)
4535 ret = 0;
4536 break;
4537 }
4538
4539 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4540 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4541 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4542 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4543 goto skip;
4544
4545 eb = path->nodes[0];
4546 slot = path->slots[0];
4547 item_size = btrfs_item_size_nr(eb, slot);
4548 if (item_size < sizeof(root_item))
4549 goto skip;
4550
Stefan Behrens803b2f52013-08-15 17:11:21 +02004551 read_extent_buffer(eb, &root_item,
4552 btrfs_item_ptr_offset(eb, slot),
4553 (int)sizeof(root_item));
4554 if (btrfs_root_refs(&root_item) == 0)
4555 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004556
4557 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4558 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4559 if (trans)
4560 goto update_tree;
4561
4562 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004563 /*
4564 * 1 - subvol uuid item
4565 * 1 - received_subvol uuid item
4566 */
4567 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4568 if (IS_ERR(trans)) {
4569 ret = PTR_ERR(trans);
4570 break;
4571 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004572 continue;
4573 } else {
4574 goto skip;
4575 }
4576update_tree:
4577 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004578 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004579 BTRFS_UUID_KEY_SUBVOL,
4580 key.objectid);
4581 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004582 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004583 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004584 break;
4585 }
4586 }
4587
4588 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004589 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004590 root_item.received_uuid,
4591 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4592 key.objectid);
4593 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004594 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004595 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004596 break;
4597 }
4598 }
4599
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004600skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004601 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004602 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004603 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004604 if (ret)
4605 break;
4606 }
4607
Stefan Behrens803b2f52013-08-15 17:11:21 +02004608 btrfs_release_path(path);
4609 if (key.offset < (u64)-1) {
4610 key.offset++;
4611 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4612 key.offset = 0;
4613 key.type = BTRFS_ROOT_ITEM_KEY;
4614 } else if (key.objectid < (u64)-1) {
4615 key.offset = 0;
4616 key.type = BTRFS_ROOT_ITEM_KEY;
4617 key.objectid++;
4618 } else {
4619 break;
4620 }
4621 cond_resched();
4622 }
4623
4624out:
4625 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004626 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004627 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004628 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004629 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004630 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004631 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004632 up(&fs_info->uuid_tree_rescan_sem);
4633 return 0;
4634}
4635
Stefan Behrens70f80172013-08-15 17:11:23 +02004636/*
4637 * Callback for btrfs_uuid_tree_iterate().
4638 * returns:
4639 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004640 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004641 * > 0 if the check failed, which means the caller shall remove the entry.
4642 */
4643static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4644 u8 *uuid, u8 type, u64 subid)
4645{
4646 struct btrfs_key key;
4647 int ret = 0;
4648 struct btrfs_root *subvol_root;
4649
4650 if (type != BTRFS_UUID_KEY_SUBVOL &&
4651 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4652 goto out;
4653
4654 key.objectid = subid;
4655 key.type = BTRFS_ROOT_ITEM_KEY;
4656 key.offset = (u64)-1;
4657 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4658 if (IS_ERR(subvol_root)) {
4659 ret = PTR_ERR(subvol_root);
4660 if (ret == -ENOENT)
4661 ret = 1;
4662 goto out;
4663 }
4664
4665 switch (type) {
4666 case BTRFS_UUID_KEY_SUBVOL:
4667 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4668 ret = 1;
4669 break;
4670 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4671 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4672 BTRFS_UUID_SIZE))
4673 ret = 1;
4674 break;
4675 }
4676
4677out:
4678 return ret;
4679}
4680
4681static int btrfs_uuid_rescan_kthread(void *data)
4682{
4683 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4684 int ret;
4685
4686 /*
4687 * 1st step is to iterate through the existing UUID tree and
4688 * to delete all entries that contain outdated data.
4689 * 2nd step is to add all missing entries to the UUID tree.
4690 */
4691 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4692 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004693 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004694 up(&fs_info->uuid_tree_rescan_sem);
4695 return ret;
4696 }
4697 return btrfs_uuid_scan_kthread(data);
4698}
4699
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004700int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4701{
4702 struct btrfs_trans_handle *trans;
4703 struct btrfs_root *tree_root = fs_info->tree_root;
4704 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004705 struct task_struct *task;
4706 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004707
4708 /*
4709 * 1 - root node
4710 * 1 - root item
4711 */
4712 trans = btrfs_start_transaction(tree_root, 2);
4713 if (IS_ERR(trans))
4714 return PTR_ERR(trans);
4715
4716 uuid_root = btrfs_create_tree(trans, fs_info,
4717 BTRFS_UUID_TREE_OBJECTID);
4718 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004719 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004720 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004721 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004722 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004723 }
4724
4725 fs_info->uuid_root = uuid_root;
4726
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004727 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004728 if (ret)
4729 return ret;
4730
4731 down(&fs_info->uuid_tree_rescan_sem);
4732 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4733 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004734 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004735 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004736 up(&fs_info->uuid_tree_rescan_sem);
4737 return PTR_ERR(task);
4738 }
4739
4740 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004741}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004742
Stefan Behrens70f80172013-08-15 17:11:23 +02004743int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4744{
4745 struct task_struct *task;
4746
4747 down(&fs_info->uuid_tree_rescan_sem);
4748 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4749 if (IS_ERR(task)) {
4750 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004751 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004752 up(&fs_info->uuid_tree_rescan_sem);
4753 return PTR_ERR(task);
4754 }
4755
4756 return 0;
4757}
4758
Chris Mason8f18cf12008-04-25 16:53:30 -04004759/*
4760 * shrinking a device means finding all of the device extents past
4761 * the new size, and then following the back refs to the chunks.
4762 * The chunk relocation code actually frees the device extent
4763 */
4764int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4765{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 struct btrfs_fs_info *fs_info = device->fs_info;
4767 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004768 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004769 struct btrfs_dev_extent *dev_extent = NULL;
4770 struct btrfs_path *path;
4771 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004772 u64 chunk_offset;
4773 int ret;
4774 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004775 int failed = 0;
4776 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004777 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004778 struct extent_buffer *l;
4779 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004781 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004782 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004783 u64 diff;
4784
4785 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004786 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004787
Anand Jain401e29c2017-12-04 12:54:55 +08004788 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004789 return -EINVAL;
4790
Chris Mason8f18cf12008-04-25 16:53:30 -04004791 path = btrfs_alloc_path();
4792 if (!path)
4793 return -ENOMEM;
4794
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004795 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004796
David Sterba34441362016-10-04 19:34:27 +02004797 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004798
Miao Xie7cc8e582014-09-03 21:35:38 +08004799 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004800 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004801 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004802 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004803 }
David Sterba34441362016-10-04 19:34:27 +02004804 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004805
Josef Bacikba1bf482009-09-11 16:11:19 -04004806again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004807 key.objectid = device->devid;
4808 key.offset = (u64)-1;
4809 key.type = BTRFS_DEV_EXTENT_KEY;
4810
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004811 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004812 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004813 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004814 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004815 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004816 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004817 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004818
4819 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004820 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004821 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004822 if (ret < 0)
4823 goto done;
4824 if (ret) {
4825 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004826 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004827 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004828 }
4829
4830 l = path->nodes[0];
4831 slot = path->slots[0];
4832 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4833
Josef Bacikba1bf482009-09-11 16:11:19 -04004834 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004835 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004836 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004837 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004838 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004839
4840 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4841 length = btrfs_dev_extent_length(l, dev_extent);
4842
Josef Bacikba1bf482009-09-11 16:11:19 -04004843 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004844 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004845 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004846 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004847 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004848
Chris Mason8f18cf12008-04-25 16:53:30 -04004849 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004850 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004851
Liu Boa6f93c72017-11-15 16:28:11 -07004852 /*
4853 * We may be relocating the only data chunk we have,
4854 * which could potentially end up with losing data's
4855 * raid profile, so lets allocate an empty one in
4856 * advance.
4857 */
4858 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4859 if (ret < 0) {
4860 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4861 goto done;
4862 }
4863
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004864 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4865 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004866 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004867 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004868 } else if (ret) {
4869 if (ret == -ETXTBSY) {
4870 btrfs_warn(fs_info,
4871 "could not shrink block group %llu due to active swapfile",
4872 chunk_offset);
4873 }
4874 goto done;
4875 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004876 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004877
4878 if (failed && !retried) {
4879 failed = 0;
4880 retried = true;
4881 goto again;
4882 } else if (failed && retried) {
4883 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004884 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004885 }
4886
Chris Balld6397ba2009-04-27 07:29:03 -04004887 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004888 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004889 if (IS_ERR(trans)) {
4890 ret = PTR_ERR(trans);
4891 goto done;
4892 }
4893
David Sterba34441362016-10-04 19:34:27 +02004894 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004895
4896 /*
4897 * We checked in the above loop all device extents that were already in
4898 * the device tree. However before we have updated the device's
4899 * total_bytes to the new size, we might have had chunk allocations that
4900 * have not complete yet (new block groups attached to transaction
4901 * handles), and therefore their device extents were not yet in the
4902 * device tree and we missed them in the loop above. So if we have any
4903 * pending chunk using a device extent that overlaps the device range
4904 * that we can not use anymore, commit the current transaction and
4905 * repeat the search on the device tree - this way we guarantee we will
4906 * not have chunks using device extents that end beyond 'new_size'.
4907 */
4908 if (!checked_pending_chunks) {
4909 u64 start = new_size;
4910 u64 len = old_size - new_size;
4911
Jeff Mahoney499f3772015-06-15 09:41:17 -04004912 if (contains_pending_extent(trans->transaction, device,
4913 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004914 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004915 checked_pending_chunks = true;
4916 failed = 0;
4917 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004918 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004919 if (ret)
4920 goto done;
4921 goto again;
4922 }
4923 }
4924
Miao Xie7cc8e582014-09-03 21:35:38 +08004925 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004926 if (list_empty(&device->resized_list))
4927 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004928 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004929
Chris Balld6397ba2009-04-27 07:29:03 -04004930 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004931 btrfs_set_super_total_bytes(super_copy,
4932 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004933 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004934
4935 /* Now btrfs_update_device() will change the on-disk size. */
4936 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004937 if (ret < 0) {
4938 btrfs_abort_transaction(trans, ret);
4939 btrfs_end_transaction(trans);
4940 } else {
4941 ret = btrfs_commit_transaction(trans);
4942 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004943done:
4944 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004945 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004946 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004947 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004948 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004949 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004950 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004951 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004952 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004953 return ret;
4954}
4955
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004956static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004957 struct btrfs_key *key,
4958 struct btrfs_chunk *chunk, int item_size)
4959{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004960 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004961 struct btrfs_disk_key disk_key;
4962 u32 array_size;
4963 u8 *ptr;
4964
David Sterba34441362016-10-04 19:34:27 +02004965 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004966 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004967 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004968 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004969 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004970 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004971 }
Chris Mason0b86a832008-03-24 15:01:56 -04004972
4973 ptr = super_copy->sys_chunk_array + array_size;
4974 btrfs_cpu_key_to_disk(&disk_key, key);
4975 memcpy(ptr, &disk_key, sizeof(disk_key));
4976 ptr += sizeof(disk_key);
4977 memcpy(ptr, chunk, item_size);
4978 item_size += sizeof(disk_key);
4979 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004980 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004981
Chris Mason0b86a832008-03-24 15:01:56 -04004982 return 0;
4983}
4984
Miao Xieb2117a32011-01-05 10:07:28 +00004985/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004986 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004987 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004988static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004989{
Arne Jansen73c5de02011-04-12 12:07:57 +02004990 const struct btrfs_device_info *di_a = a;
4991 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004992
Arne Jansen73c5de02011-04-12 12:07:57 +02004993 if (di_a->max_avail > di_b->max_avail)
4994 return -1;
4995 if (di_a->max_avail < di_b->max_avail)
4996 return 1;
4997 if (di_a->total_avail > di_b->total_avail)
4998 return -1;
4999 if (di_a->total_avail < di_b->total_avail)
5000 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00005001 return 0;
5002}
5003
David Woodhouse53b381b2013-01-29 18:40:14 -05005004static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
5005{
Zhao Leiffe2d202015-01-20 15:11:44 +08005006 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05005007 return;
5008
Miao Xieceda0862013-04-11 10:30:16 +00005009 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05005010}
5011
Qu Wenruo062d4d12018-01-30 18:20:46 +08005012#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005013 - sizeof(struct btrfs_chunk)) \
5014 / sizeof(struct btrfs_stripe) + 1)
5015
5016#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
5017 - 2 * sizeof(struct btrfs_disk_key) \
5018 - 2 * sizeof(struct btrfs_chunk)) \
5019 / sizeof(struct btrfs_stripe) + 1)
5020
Miao Xieb2117a32011-01-05 10:07:28 +00005021static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01005022 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00005023{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005024 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00005025 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03005026 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02005027 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00005028 struct extent_map_tree *em_tree;
5029 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02005030 struct btrfs_device_info *devices_info = NULL;
5031 u64 total_avail;
5032 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05005033 int data_stripes; /* number of stripes that count for
5034 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02005035 int sub_stripes; /* sub_stripes info for map */
5036 int dev_stripes; /* stripes per dev */
5037 int devs_max; /* max devs to use */
5038 int devs_min; /* min devs needed */
5039 int devs_increment; /* ndevs has to be a multiple of this */
5040 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005041 int nparity; /* number of stripes worth of bytes to
5042 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00005043 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02005044 u64 max_stripe_size;
5045 u64 max_chunk_size;
5046 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005047 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02005048 int ndevs;
5049 int i;
5050 int j;
Liu Bo31e50222012-11-21 14:18:10 +00005051 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00005052
Ilya Dryomov0c460c02012-03-27 17:09:17 +03005053 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02005054
Qu Wenruo4117f202018-01-22 13:50:54 +08005055 if (list_empty(&fs_devices->alloc_list)) {
5056 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5057 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005058 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005059 }
Miao Xieb2117a32011-01-05 10:07:28 +00005060
Qu Wenruo3e72ee82018-01-30 18:20:45 +08005061 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02005062
Liu Bo31e50222012-11-21 14:18:10 +00005063 sub_stripes = btrfs_raid_array[index].sub_stripes;
5064 dev_stripes = btrfs_raid_array[index].dev_stripes;
5065 devs_max = btrfs_raid_array[index].devs_max;
5066 devs_min = btrfs_raid_array[index].devs_min;
5067 devs_increment = btrfs_raid_array[index].devs_increment;
5068 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005069 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02005070
5071 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09005072 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08005073 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005074 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005075 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005076 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05005077 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09005078 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
5079 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05005080 else
Byongho Leeee221842015-12-15 01:42:10 +09005081 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005082 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005083 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005084 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005085 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09005086 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005087 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005088 if (!devs_max)
5089 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02005090 } else {
David Sterba351fd352014-05-15 16:48:20 +02005091 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02005092 type);
5093 BUG_ON(1);
5094 }
5095
5096 /* we don't want a chunk larger than 10% of writeable space */
5097 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
5098 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00005099
David Sterba31e818f2015-02-20 18:00:26 +01005100 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005101 GFP_NOFS);
5102 if (!devices_info)
5103 return -ENOMEM;
5104
Arne Jansen73c5de02011-04-12 12:07:57 +02005105 /*
5106 * in the first pass through the devices list, we gather information
5107 * about the available holes on each device.
5108 */
5109 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03005110 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005111 u64 max_avail;
5112 u64 dev_offset;
5113
Anand Jainebbede42017-12-04 12:54:52 +08005114 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005115 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05005116 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02005117 continue;
5118 }
5119
Anand Jaine12c9622017-12-04 12:54:53 +08005120 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5121 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08005122 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02005123 continue;
5124
5125 if (device->total_bytes > device->bytes_used)
5126 total_avail = device->total_bytes - device->bytes_used;
5127 else
5128 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00005129
5130 /* If there is no space on this device, skip it. */
5131 if (total_avail == 0)
5132 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02005133
Josef Bacik6df9a952013-06-27 13:22:46 -04005134 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02005135 max_stripe_size * dev_stripes,
5136 &dev_offset, &max_avail);
5137 if (ret && ret != -ENOSPC)
5138 goto error;
5139
5140 if (ret == 0)
5141 max_avail = max_stripe_size * dev_stripes;
5142
Qu Wenruo4117f202018-01-22 13:50:54 +08005143 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5144 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5145 btrfs_debug(info,
5146 "%s: devid %llu has no free space, have=%llu want=%u",
5147 __func__, device->devid, max_avail,
5148 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005149 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08005150 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005151
Eric Sandeen063d0062013-01-31 00:55:01 +00005152 if (ndevs == fs_devices->rw_devices) {
5153 WARN(1, "%s: found more than %llu devices\n",
5154 __func__, fs_devices->rw_devices);
5155 break;
5156 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005157 devices_info[ndevs].dev_offset = dev_offset;
5158 devices_info[ndevs].max_avail = max_avail;
5159 devices_info[ndevs].total_avail = total_avail;
5160 devices_info[ndevs].dev = device;
5161 ++ndevs;
5162 }
5163
5164 /*
5165 * now sort the devices by hole size / available space
5166 */
5167 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5168 btrfs_cmp_device_info, NULL);
5169
5170 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03005171 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02005172
Qu Wenruoba89b802018-01-31 13:56:15 +08005173 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005174 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005175 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5176 btrfs_debug(info,
5177 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08005178 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08005179 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005180 goto error;
5181 }
5182
Nikolay Borisovf148ef42017-06-27 10:02:26 +03005183 ndevs = min(ndevs, devs_max);
5184
Arne Jansen73c5de02011-04-12 12:07:57 +02005185 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005186 * The primary goal is to maximize the number of stripes, so use as
5187 * many devices as possible, even if the stripes are not maximum sized.
5188 *
5189 * The DUP profile stores more than one stripe per device, the
5190 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02005191 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005192 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005193 num_stripes = ndevs * dev_stripes;
5194
David Woodhouse53b381b2013-01-29 18:40:14 -05005195 /*
5196 * this will have to be fixed for RAID1 and RAID10 over
5197 * more drives
5198 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005199 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05005200
5201 /*
5202 * Use the number of data stripes to figure out how big this chunk
5203 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005204 * and compare that answer with the max chunk size. If it's higher,
5205 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05005206 */
5207 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005208 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005209 * Reduce stripe_size, round it up to a 16MB boundary again and
5210 * then use it, unless it ends up being even bigger than the
5211 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05005212 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005213 stripe_size = min(round_up(div_u64(max_chunk_size,
5214 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005215 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05005216 }
5217
Ilya Dryomov37db63a2012-04-13 17:05:08 +03005218 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005219 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02005220
Miao Xieb2117a32011-01-05 10:07:28 +00005221 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5222 if (!map) {
5223 ret = -ENOMEM;
5224 goto error;
5225 }
5226 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04005227
Arne Jansen73c5de02011-04-12 12:07:57 +02005228 for (i = 0; i < ndevs; ++i) {
5229 for (j = 0; j < dev_stripes; ++j) {
5230 int s = i * dev_stripes + j;
5231 map->stripes[s].dev = devices_info[i].dev;
5232 map->stripes[s].physical = devices_info[i].dev_offset +
5233 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04005234 }
Chris Mason6324fbf2008-03-24 15:01:59 -04005235 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005236 map->stripe_len = BTRFS_STRIPE_LEN;
5237 map->io_align = BTRFS_STRIPE_LEN;
5238 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04005239 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04005240 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005241
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005242 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005243
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005244 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005245
David Sterba172ddd62011-04-21 00:48:27 +02005246 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005247 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005248 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005249 ret = -ENOMEM;
5250 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005251 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005252 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005253 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005254 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005255 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005256 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005257 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005258 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005261 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005262 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005263 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005264 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005265 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005266 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005267 }
Yan Zheng2b820322008-11-17 21:11:30 -05005268
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005269 list_add_tail(&em->list, &trans->transaction->pending_chunks);
5270 refcount_inc(&em->refs);
5271 write_unlock(&em_tree->lock);
5272
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005273 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005274 if (ret)
5275 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005276
Hans van Kranenburg2f29df42018-10-04 23:24:38 +02005277 for (i = 0; i < map->num_stripes; i++)
5278 btrfs_device_set_bytes_used(map->stripes[i].dev,
5279 map->stripes[i].dev->bytes_used + stripe_size);
Miao Xie43530c42014-09-03 21:35:36 +08005280
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005281 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005282
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005283 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005284 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005285
Miao Xieb2117a32011-01-05 10:07:28 +00005286 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005287 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005288
Josef Bacik6df9a952013-06-27 13:22:46 -04005289error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005290 write_lock(&em_tree->lock);
5291 remove_extent_mapping(em_tree, em);
5292 write_unlock(&em_tree->lock);
5293
5294 /* One for our allocation */
5295 free_extent_map(em);
5296 /* One for the tree reference */
5297 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00005298 /* One for the pending_chunks list reference */
5299 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005300error:
Miao Xieb2117a32011-01-05 10:07:28 +00005301 kfree(devices_info);
5302 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005303}
5304
Josef Bacik6df9a952013-06-27 13:22:46 -04005305int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005306 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005307{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005308 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005309 struct btrfs_root *extent_root = fs_info->extent_root;
5310 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005311 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005312 struct btrfs_device *device;
5313 struct btrfs_chunk *chunk;
5314 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005315 struct extent_map *em;
5316 struct map_lookup *map;
5317 size_t item_size;
5318 u64 dev_offset;
5319 u64 stripe_size;
5320 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005321 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005322
Omar Sandoval60ca8422018-05-16 16:34:31 -07005323 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005324 if (IS_ERR(em))
5325 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005326
Jeff Mahoney95617d62015-06-03 10:55:48 -04005327 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005328 item_size = btrfs_chunk_item_size(map->num_stripes);
5329 stripe_size = em->orig_block_len;
5330
5331 chunk = kzalloc(item_size, GFP_NOFS);
5332 if (!chunk) {
5333 ret = -ENOMEM;
5334 goto out;
5335 }
5336
Filipe Manana50460e32015-11-20 10:42:47 +00005337 /*
5338 * Take the device list mutex to prevent races with the final phase of
5339 * a device replace operation that replaces the device object associated
5340 * with the map's stripes, because the device object's id can change
5341 * at any time during that final phase of the device replace operation
5342 * (dev-replace.c:btrfs_dev_replace_finishing()).
5343 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005344 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005345 for (i = 0; i < map->num_stripes; i++) {
5346 device = map->stripes[i].dev;
5347 dev_offset = map->stripes[i].physical;
5348
Yan Zheng2b820322008-11-17 21:11:30 -05005349 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005350 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005351 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005352 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5353 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005354 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005355 break;
5356 }
5357 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005358 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005359 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005360 }
5361
Yan Zheng2b820322008-11-17 21:11:30 -05005362 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005363 for (i = 0; i < map->num_stripes; i++) {
5364 device = map->stripes[i].dev;
5365 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005366
5367 btrfs_set_stack_stripe_devid(stripe, device->devid);
5368 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5369 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5370 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005371 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005372 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005373
5374 btrfs_set_stack_chunk_length(chunk, chunk_size);
5375 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5376 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5377 btrfs_set_stack_chunk_type(chunk, map->type);
5378 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5379 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5380 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005381 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005382 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5383
5384 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5385 key.type = BTRFS_CHUNK_ITEM_KEY;
5386 key.offset = chunk_offset;
5387
5388 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005389 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5390 /*
5391 * TODO: Cleanup of inserted chunk root in case of
5392 * failure.
5393 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005394 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005395 }
liubo1abe9b82011-03-24 11:18:59 +00005396
Josef Bacik6df9a952013-06-27 13:22:46 -04005397out:
Yan Zheng2b820322008-11-17 21:11:30 -05005398 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005399 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005400 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005401}
5402
5403/*
5404 * Chunk allocation falls into two parts. The first part does works
5405 * that make the new allocated chunk useable, but not do any operation
5406 * that modifies the chunk tree. The second part does the works that
5407 * require modifying the chunk tree. This division is important for the
5408 * bootstrap process of adding storage to a seed btrfs.
5409 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005410int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005411{
5412 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005413
Nikolay Borisovc216b202018-06-20 15:49:06 +03005414 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5415 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005416 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005417}
5418
Chris Masond3977122009-01-05 21:25:51 -05005419static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005420 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005421{
5422 u64 chunk_offset;
5423 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005424 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005425 int ret;
5426
Josef Bacik6df9a952013-06-27 13:22:46 -04005427 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005428 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005429 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005430 if (ret)
5431 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005432
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005433 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005434 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005435 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005436 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005437}
5438
Miao Xied20983b2014-07-03 18:22:13 +08005439static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5440{
5441 int max_errors;
5442
5443 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5444 BTRFS_BLOCK_GROUP_RAID10 |
5445 BTRFS_BLOCK_GROUP_RAID5 |
5446 BTRFS_BLOCK_GROUP_DUP)) {
5447 max_errors = 1;
5448 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5449 max_errors = 2;
5450 } else {
5451 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005452 }
5453
Miao Xied20983b2014-07-03 18:22:13 +08005454 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005455}
5456
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005457int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005458{
5459 struct extent_map *em;
5460 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005461 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005462 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005463 int i;
5464
Omar Sandoval60ca8422018-05-16 16:34:31 -07005465 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005466 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005467 return 1;
5468
Jeff Mahoney95617d62015-06-03 10:55:48 -04005469 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005470 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005471 if (test_bit(BTRFS_DEV_STATE_MISSING,
5472 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005473 miss_ndevs++;
5474 continue;
5475 }
Anand Jainebbede42017-12-04 12:54:52 +08005476 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5477 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005478 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005479 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005480 }
5481 }
Miao Xied20983b2014-07-03 18:22:13 +08005482
5483 /*
5484 * If the number of missing devices is larger than max errors,
5485 * we can not write the data into that chunk successfully, so
5486 * set it readonly.
5487 */
5488 if (miss_ndevs > btrfs_chunk_max_errors(map))
5489 readonly = 1;
5490end:
Yan Zheng2b820322008-11-17 21:11:30 -05005491 free_extent_map(em);
5492 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005493}
5494
5495void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5496{
David Sterbaa8067e02011-04-21 00:34:43 +02005497 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005498}
5499
5500void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5501{
5502 struct extent_map *em;
5503
Chris Masond3977122009-01-05 21:25:51 -05005504 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005505 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005506 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5507 if (em)
5508 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005509 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005510 if (!em)
5511 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005512 /* once for us */
5513 free_extent_map(em);
5514 /* once for the tree */
5515 free_extent_map(em);
5516 }
5517}
5518
Stefan Behrens5d964052012-11-05 14:59:07 +01005519int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005520{
5521 struct extent_map *em;
5522 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005523 int ret;
5524
Omar Sandoval60ca8422018-05-16 16:34:31 -07005525 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005526 if (IS_ERR(em))
5527 /*
5528 * We could return errors for these cases, but that could get
5529 * ugly and we'd probably do the same thing which is just not do
5530 * anything else and exit, so return 1 so the callers don't try
5531 * to use other copies.
5532 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005533 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005534
Jeff Mahoney95617d62015-06-03 10:55:48 -04005535 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005536 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5537 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005538 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5539 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005540 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5541 ret = 2;
5542 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005543 /*
5544 * There could be two corrupted data stripes, we need
5545 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005546 *
Liu Bo8810f752018-01-02 13:36:41 -07005547 * Fail a stripe at a time on every retry except the
5548 * stripe under reconstruction.
5549 */
5550 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005551 else
5552 ret = 1;
5553 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005554
David Sterba7e79cb82018-03-24 02:11:38 +01005555 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005556 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5557 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005558 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005559 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005560
Chris Masonf1885912008-04-09 16:28:12 -04005561 return ret;
5562}
5563
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005564unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005565 u64 logical)
5566{
5567 struct extent_map *em;
5568 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005569 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005570
Omar Sandoval60ca8422018-05-16 16:34:31 -07005571 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005572
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005573 if (!WARN_ON(IS_ERR(em))) {
5574 map = em->map_lookup;
5575 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5576 len = map->stripe_len * nr_data_stripes(map);
5577 free_extent_map(em);
5578 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005579 return len;
5580}
5581
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005582int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005583{
5584 struct extent_map *em;
5585 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005586 int ret = 0;
5587
Omar Sandoval60ca8422018-05-16 16:34:31 -07005588 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005589
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005590 if(!WARN_ON(IS_ERR(em))) {
5591 map = em->map_lookup;
5592 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5593 ret = 1;
5594 free_extent_map(em);
5595 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005596 return ret;
5597}
5598
Stefan Behrens30d98612012-11-06 14:52:18 +01005599static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005600 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005601 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005602{
5603 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005604 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005605 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005606 int tolerance;
5607 struct btrfs_device *srcdev;
5608
Anand Jain99f92a72018-03-14 16:29:12 +08005609 ASSERT((map->type &
5610 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5611
5612 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5613 num_stripes = map->sub_stripes;
5614 else
5615 num_stripes = map->num_stripes;
5616
Anand Jain8ba0ae72018-03-14 16:29:13 +08005617 preferred_mirror = first + current->pid % num_stripes;
5618
Stefan Behrens30d98612012-11-06 14:52:18 +01005619 if (dev_replace_is_ongoing &&
5620 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5621 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5622 srcdev = fs_info->dev_replace.srcdev;
5623 else
5624 srcdev = NULL;
5625
5626 /*
5627 * try to avoid the drive that is the source drive for a
5628 * dev-replace procedure, only choose it if no other non-missing
5629 * mirror is available
5630 */
5631 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005632 if (map->stripes[preferred_mirror].dev->bdev &&
5633 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5634 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005635 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005636 if (map->stripes[i].dev->bdev &&
5637 (tolerance || map->stripes[i].dev != srcdev))
5638 return i;
5639 }
Chris Masondfe25022008-05-13 13:46:40 -04005640 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005641
Chris Masondfe25022008-05-13 13:46:40 -04005642 /* we couldn't find one that doesn't fail. Just return something
5643 * and the io error handling code will clean up eventually
5644 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005645 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005646}
5647
David Woodhouse53b381b2013-01-29 18:40:14 -05005648static inline int parity_smaller(u64 a, u64 b)
5649{
5650 return a > b;
5651}
5652
5653/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005654static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005655{
5656 struct btrfs_bio_stripe s;
5657 int i;
5658 u64 l;
5659 int again = 1;
5660
5661 while (again) {
5662 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005663 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005664 if (parity_smaller(bbio->raid_map[i],
5665 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005666 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005667 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005668 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005669 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005670 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005671 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005672
David Woodhouse53b381b2013-01-29 18:40:14 -05005673 again = 1;
5674 }
5675 }
5676 }
5677}
5678
Zhao Lei6e9606d2015-01-20 15:11:34 +08005679static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5680{
5681 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005682 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005683 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005684 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005685 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005686 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005687 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005688 /*
5689 * plus the raid_map, which includes both the tgt dev
5690 * and the stripes
5691 */
5692 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005693 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005694
5695 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005696 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005697
5698 return bbio;
5699}
5700
5701void btrfs_get_bbio(struct btrfs_bio *bbio)
5702{
Elena Reshetova140475a2017-03-03 10:55:10 +02005703 WARN_ON(!refcount_read(&bbio->refs));
5704 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005705}
5706
5707void btrfs_put_bbio(struct btrfs_bio *bbio)
5708{
5709 if (!bbio)
5710 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005711 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005712 kfree(bbio);
5713}
5714
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005715/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5716/*
5717 * Please note that, discard won't be sent to target device of device
5718 * replace.
5719 */
5720static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5721 u64 logical, u64 length,
5722 struct btrfs_bio **bbio_ret)
5723{
5724 struct extent_map *em;
5725 struct map_lookup *map;
5726 struct btrfs_bio *bbio;
5727 u64 offset;
5728 u64 stripe_nr;
5729 u64 stripe_nr_end;
5730 u64 stripe_end_offset;
5731 u64 stripe_cnt;
5732 u64 stripe_len;
5733 u64 stripe_offset;
5734 u64 num_stripes;
5735 u32 stripe_index;
5736 u32 factor = 0;
5737 u32 sub_stripes = 0;
5738 u64 stripes_per_dev = 0;
5739 u32 remaining_stripes = 0;
5740 u32 last_stripe = 0;
5741 int ret = 0;
5742 int i;
5743
5744 /* discard always return a bbio */
5745 ASSERT(bbio_ret);
5746
Omar Sandoval60ca8422018-05-16 16:34:31 -07005747 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005748 if (IS_ERR(em))
5749 return PTR_ERR(em);
5750
5751 map = em->map_lookup;
5752 /* we don't discard raid56 yet */
5753 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5754 ret = -EOPNOTSUPP;
5755 goto out;
5756 }
5757
5758 offset = logical - em->start;
5759 length = min_t(u64, em->len - offset, length);
5760
5761 stripe_len = map->stripe_len;
5762 /*
5763 * stripe_nr counts the total number of stripes we have to stride
5764 * to get to this block
5765 */
5766 stripe_nr = div64_u64(offset, stripe_len);
5767
5768 /* stripe_offset is the offset of this block in its stripe */
5769 stripe_offset = offset - stripe_nr * stripe_len;
5770
5771 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005772 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005773 stripe_cnt = stripe_nr_end - stripe_nr;
5774 stripe_end_offset = stripe_nr_end * map->stripe_len -
5775 (offset + length);
5776 /*
5777 * after this, stripe_nr is the number of stripes on this
5778 * device we have to walk to find the data, and stripe_index is
5779 * the number of our device in the stripe array
5780 */
5781 num_stripes = 1;
5782 stripe_index = 0;
5783 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5784 BTRFS_BLOCK_GROUP_RAID10)) {
5785 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5786 sub_stripes = 1;
5787 else
5788 sub_stripes = map->sub_stripes;
5789
5790 factor = map->num_stripes / sub_stripes;
5791 num_stripes = min_t(u64, map->num_stripes,
5792 sub_stripes * stripe_cnt);
5793 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5794 stripe_index *= sub_stripes;
5795 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5796 &remaining_stripes);
5797 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5798 last_stripe *= sub_stripes;
5799 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5800 BTRFS_BLOCK_GROUP_DUP)) {
5801 num_stripes = map->num_stripes;
5802 } else {
5803 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5804 &stripe_index);
5805 }
5806
5807 bbio = alloc_btrfs_bio(num_stripes, 0);
5808 if (!bbio) {
5809 ret = -ENOMEM;
5810 goto out;
5811 }
5812
5813 for (i = 0; i < num_stripes; i++) {
5814 bbio->stripes[i].physical =
5815 map->stripes[stripe_index].physical +
5816 stripe_offset + stripe_nr * map->stripe_len;
5817 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5818
5819 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5820 BTRFS_BLOCK_GROUP_RAID10)) {
5821 bbio->stripes[i].length = stripes_per_dev *
5822 map->stripe_len;
5823
5824 if (i / sub_stripes < remaining_stripes)
5825 bbio->stripes[i].length +=
5826 map->stripe_len;
5827
5828 /*
5829 * Special for the first stripe and
5830 * the last stripe:
5831 *
5832 * |-------|...|-------|
5833 * |----------|
5834 * off end_off
5835 */
5836 if (i < sub_stripes)
5837 bbio->stripes[i].length -=
5838 stripe_offset;
5839
5840 if (stripe_index >= last_stripe &&
5841 stripe_index <= (last_stripe +
5842 sub_stripes - 1))
5843 bbio->stripes[i].length -=
5844 stripe_end_offset;
5845
5846 if (i == sub_stripes - 1)
5847 stripe_offset = 0;
5848 } else {
5849 bbio->stripes[i].length = length;
5850 }
5851
5852 stripe_index++;
5853 if (stripe_index == map->num_stripes) {
5854 stripe_index = 0;
5855 stripe_nr++;
5856 }
5857 }
5858
5859 *bbio_ret = bbio;
5860 bbio->map_type = map->type;
5861 bbio->num_stripes = num_stripes;
5862out:
5863 free_extent_map(em);
5864 return ret;
5865}
5866
Liu Bo5ab56092017-03-14 13:33:57 -07005867/*
5868 * In dev-replace case, for repair case (that's the only case where the mirror
5869 * is selected explicitly when calling btrfs_map_block), blocks left of the
5870 * left cursor can also be read from the target drive.
5871 *
5872 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5873 * array of stripes.
5874 * For READ, it also needs to be supported using the same mirror number.
5875 *
5876 * If the requested block is not left of the left cursor, EIO is returned. This
5877 * can happen because btrfs_num_copies() returns one more in the dev-replace
5878 * case.
5879 */
5880static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5881 u64 logical, u64 length,
5882 u64 srcdev_devid, int *mirror_num,
5883 u64 *physical)
5884{
5885 struct btrfs_bio *bbio = NULL;
5886 int num_stripes;
5887 int index_srcdev = 0;
5888 int found = 0;
5889 u64 physical_of_found = 0;
5890 int i;
5891 int ret = 0;
5892
5893 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5894 logical, &length, &bbio, 0, 0);
5895 if (ret) {
5896 ASSERT(bbio == NULL);
5897 return ret;
5898 }
5899
5900 num_stripes = bbio->num_stripes;
5901 if (*mirror_num > num_stripes) {
5902 /*
5903 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5904 * that means that the requested area is not left of the left
5905 * cursor
5906 */
5907 btrfs_put_bbio(bbio);
5908 return -EIO;
5909 }
5910
5911 /*
5912 * process the rest of the function using the mirror_num of the source
5913 * drive. Therefore look it up first. At the end, patch the device
5914 * pointer to the one of the target drive.
5915 */
5916 for (i = 0; i < num_stripes; i++) {
5917 if (bbio->stripes[i].dev->devid != srcdev_devid)
5918 continue;
5919
5920 /*
5921 * In case of DUP, in order to keep it simple, only add the
5922 * mirror with the lowest physical address
5923 */
5924 if (found &&
5925 physical_of_found <= bbio->stripes[i].physical)
5926 continue;
5927
5928 index_srcdev = i;
5929 found = 1;
5930 physical_of_found = bbio->stripes[i].physical;
5931 }
5932
5933 btrfs_put_bbio(bbio);
5934
5935 ASSERT(found);
5936 if (!found)
5937 return -EIO;
5938
5939 *mirror_num = index_srcdev + 1;
5940 *physical = physical_of_found;
5941 return ret;
5942}
5943
Liu Bo73c0f222017-03-14 13:33:58 -07005944static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5945 struct btrfs_bio **bbio_ret,
5946 struct btrfs_dev_replace *dev_replace,
5947 int *num_stripes_ret, int *max_errors_ret)
5948{
5949 struct btrfs_bio *bbio = *bbio_ret;
5950 u64 srcdev_devid = dev_replace->srcdev->devid;
5951 int tgtdev_indexes = 0;
5952 int num_stripes = *num_stripes_ret;
5953 int max_errors = *max_errors_ret;
5954 int i;
5955
5956 if (op == BTRFS_MAP_WRITE) {
5957 int index_where_to_add;
5958
5959 /*
5960 * duplicate the write operations while the dev replace
5961 * procedure is running. Since the copying of the old disk to
5962 * the new disk takes place at run time while the filesystem is
5963 * mounted writable, the regular write operations to the old
5964 * disk have to be duplicated to go to the new disk as well.
5965 *
5966 * Note that device->missing is handled by the caller, and that
5967 * the write to the old disk is already set up in the stripes
5968 * array.
5969 */
5970 index_where_to_add = num_stripes;
5971 for (i = 0; i < num_stripes; i++) {
5972 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5973 /* write to new disk, too */
5974 struct btrfs_bio_stripe *new =
5975 bbio->stripes + index_where_to_add;
5976 struct btrfs_bio_stripe *old =
5977 bbio->stripes + i;
5978
5979 new->physical = old->physical;
5980 new->length = old->length;
5981 new->dev = dev_replace->tgtdev;
5982 bbio->tgtdev_map[i] = index_where_to_add;
5983 index_where_to_add++;
5984 max_errors++;
5985 tgtdev_indexes++;
5986 }
5987 }
5988 num_stripes = index_where_to_add;
5989 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5990 int index_srcdev = 0;
5991 int found = 0;
5992 u64 physical_of_found = 0;
5993
5994 /*
5995 * During the dev-replace procedure, the target drive can also
5996 * be used to read data in case it is needed to repair a corrupt
5997 * block elsewhere. This is possible if the requested area is
5998 * left of the left cursor. In this area, the target drive is a
5999 * full copy of the source drive.
6000 */
6001 for (i = 0; i < num_stripes; i++) {
6002 if (bbio->stripes[i].dev->devid == srcdev_devid) {
6003 /*
6004 * In case of DUP, in order to keep it simple,
6005 * only add the mirror with the lowest physical
6006 * address
6007 */
6008 if (found &&
6009 physical_of_found <=
6010 bbio->stripes[i].physical)
6011 continue;
6012 index_srcdev = i;
6013 found = 1;
6014 physical_of_found = bbio->stripes[i].physical;
6015 }
6016 }
6017 if (found) {
6018 struct btrfs_bio_stripe *tgtdev_stripe =
6019 bbio->stripes + num_stripes;
6020
6021 tgtdev_stripe->physical = physical_of_found;
6022 tgtdev_stripe->length =
6023 bbio->stripes[index_srcdev].length;
6024 tgtdev_stripe->dev = dev_replace->tgtdev;
6025 bbio->tgtdev_map[index_srcdev] = num_stripes;
6026
6027 tgtdev_indexes++;
6028 num_stripes++;
6029 }
6030 }
6031
6032 *num_stripes_ret = num_stripes;
6033 *max_errors_ret = max_errors;
6034 bbio->num_tgtdevs = tgtdev_indexes;
6035 *bbio_ret = bbio;
6036}
6037
Liu Bo2b19a1f2017-03-14 13:34:00 -07006038static bool need_full_stripe(enum btrfs_map_op op)
6039{
6040 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
6041}
6042
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006043static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
6044 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006045 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006046 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006047 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04006048{
6049 struct extent_map *em;
6050 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04006051 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04006052 u64 stripe_offset;
6053 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006054 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006055 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04006056 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006057 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006058 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006059 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006060 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006061 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006062 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6063 int dev_replace_is_ongoing = 0;
6064 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006065 int patch_the_first_stripe_for_dev_replace = 0;
6066 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006067 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04006068
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006069 if (op == BTRFS_MAP_DISCARD)
6070 return __btrfs_map_block_for_discard(fs_info, logical,
6071 *length, bbio_ret);
6072
Omar Sandoval60ca8422018-05-16 16:34:31 -07006073 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07006074 if (IS_ERR(em))
6075 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04006076
Jeff Mahoney95617d62015-06-03 10:55:48 -04006077 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04006078 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04006079
David Woodhouse53b381b2013-01-29 18:40:14 -05006080 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04006081 stripe_nr = offset;
6082 /*
6083 * stripe_nr counts the total number of stripes we have to stride
6084 * to get to this block
6085 */
David Sterba47c57132015-02-20 18:43:47 +01006086 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04006087
David Woodhouse53b381b2013-01-29 18:40:14 -05006088 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04006089 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006090 btrfs_crit(fs_info,
6091 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04006092 stripe_offset, offset, em->start, logical,
6093 stripe_len);
6094 free_extent_map(em);
6095 return -EINVAL;
6096 }
Chris Mason593060d2008-03-25 16:50:33 -04006097
6098 /* stripe_offset is the offset of this block in its stripe*/
6099 stripe_offset = offset - stripe_offset;
6100
David Woodhouse53b381b2013-01-29 18:40:14 -05006101 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08006102 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006103 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
6104 raid56_full_stripe_start = offset;
6105
6106 /* allow a write of a full stripe, but make sure we don't
6107 * allow straddling of stripes
6108 */
David Sterba47c57132015-02-20 18:43:47 +01006109 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
6110 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05006111 raid56_full_stripe_start *= full_stripe_len;
6112 }
6113
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006114 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006115 u64 max_len;
6116 /* For writes to RAID[56], allow a full stripeset across all disks.
6117 For other RAID types and for RAID[56] reads, just allow a single
6118 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08006119 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006120 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006121 max_len = stripe_len * nr_data_stripes(map) -
6122 (offset - raid56_full_stripe_start);
6123 } else {
6124 /* we limit the length of each bio to what fits in a stripe */
6125 max_len = stripe_len - stripe_offset;
6126 }
6127 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04006128 } else {
6129 *length = em->len - offset;
6130 }
Chris Masonf2d8d742008-04-21 10:03:05 -04006131
David Woodhouse53b381b2013-01-29 18:40:14 -05006132 /* This is for when we're called from btrfs_merge_bio_hook() and all
6133 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006134 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04006135 goto out;
6136
David Sterba7e79cb82018-03-24 02:11:38 +01006137 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01006138 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
6139 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01006140 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08006141 else
6142 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01006143
Stefan Behrensad6d6202012-11-06 15:06:47 +01006144 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006145 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006146 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6147 dev_replace->srcdev->devid,
6148 &mirror_num,
6149 &physical_to_patch_in_first_stripe);
6150 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006151 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006152 else
6153 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006154 } else if (mirror_num > map->num_stripes) {
6155 mirror_num = 0;
6156 }
6157
Chris Masonf2d8d742008-04-21 10:03:05 -04006158 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006159 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006160 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006161 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6162 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006163 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006164 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006165 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08006166 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006167 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006168 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006169 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006170 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006171 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006172 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006173 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006174 }
Chris Mason2fff7342008-04-29 14:12:09 -04006175
Chris Mason611f0e02008-04-03 16:29:03 -04006176 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006177 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006178 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006179 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006180 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006181 } else {
6182 mirror_num = 1;
6183 }
Chris Mason2fff7342008-04-29 14:12:09 -04006184
Chris Mason321aecc2008-04-16 10:49:51 -04006185 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006186 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006187
David Sterba47c57132015-02-20 18:43:47 +01006188 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006189 stripe_index *= map->sub_stripes;
6190
Anand Jainde483732017-10-12 16:43:00 +08006191 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006192 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006193 else if (mirror_num)
6194 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006195 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006196 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006197 stripe_index = find_live_mirror(fs_info, map,
6198 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006199 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006200 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006201 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006202
Zhao Leiffe2d202015-01-20 15:11:44 +08006203 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006204 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006205 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006206 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01006207 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006208
6209 /* RAID[56] write or recovery. Return all stripes */
6210 num_stripes = map->num_stripes;
6211 max_errors = nr_parity_stripes(map);
6212
David Woodhouse53b381b2013-01-29 18:40:14 -05006213 *length = map->stripe_len;
6214 stripe_index = 0;
6215 stripe_offset = 0;
6216 } else {
6217 /*
6218 * Mirror #0 or #1 means the original data block.
6219 * Mirror #2 is RAID5 parity block.
6220 * Mirror #3 is RAID6 Q block.
6221 */
David Sterba47c57132015-02-20 18:43:47 +01006222 stripe_nr = div_u64_rem(stripe_nr,
6223 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006224 if (mirror_num > 1)
6225 stripe_index = nr_data_stripes(map) +
6226 mirror_num - 2;
6227
6228 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006229 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6230 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006231 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006232 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006233 }
Chris Mason8790d502008-04-03 16:29:03 -04006234 } else {
6235 /*
David Sterba47c57132015-02-20 18:43:47 +01006236 * after this, stripe_nr is the number of stripes on this
6237 * device we have to walk to find the data, and stripe_index is
6238 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006239 */
David Sterba47c57132015-02-20 18:43:47 +01006240 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6241 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006242 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006243 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006244 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006245 btrfs_crit(fs_info,
6246 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006247 stripe_index, map->num_stripes);
6248 ret = -EINVAL;
6249 goto out;
6250 }
Chris Mason593060d2008-03-25 16:50:33 -04006251
Stefan Behrens472262f2012-11-06 14:43:46 +01006252 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006253 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006254 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006255 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006256 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006257 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006258 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006259 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006260
Zhao Lei6e9606d2015-01-20 15:11:34 +08006261 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006262 if (!bbio) {
6263 ret = -ENOMEM;
6264 goto out;
6265 }
Liu Bo6fad8232017-03-14 13:33:59 -07006266 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006267 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006268
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006269 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006270 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6271 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006272 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006273 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006274
6275 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6276 sizeof(struct btrfs_bio_stripe) *
6277 num_alloc_stripes +
6278 sizeof(int) * tgtdev_indexes);
6279
6280 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006281 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006282
6283 /* Fill in the logical address of each stripe */
6284 tmp = stripe_nr * nr_data_stripes(map);
6285 for (i = 0; i < nr_data_stripes(map); i++)
6286 bbio->raid_map[(i+rot) % num_stripes] =
6287 em->start + (tmp + i) * map->stripe_len;
6288
6289 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6290 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6291 bbio->raid_map[(i+rot+1) % num_stripes] =
6292 RAID6_Q_STRIPE;
6293 }
6294
Li Zefanec9ef7a2011-12-01 14:06:42 +08006295
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006296 for (i = 0; i < num_stripes; i++) {
6297 bbio->stripes[i].physical =
6298 map->stripes[stripe_index].physical +
6299 stripe_offset +
6300 stripe_nr * map->stripe_len;
6301 bbio->stripes[i].dev =
6302 map->stripes[stripe_index].dev;
6303 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006304 }
Li Zefande11cc12011-12-01 12:55:47 +08006305
Liu Bo2b19a1f2017-03-14 13:34:00 -07006306 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006307 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006308
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006309 if (bbio->raid_map)
6310 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006311
Liu Bo73c0f222017-03-14 13:33:58 -07006312 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006313 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006314 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6315 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006316 }
6317
Li Zefande11cc12011-12-01 12:55:47 +08006318 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006319 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006320 bbio->num_stripes = num_stripes;
6321 bbio->max_errors = max_errors;
6322 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006323
6324 /*
6325 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6326 * mirror_num == num_stripes + 1 && dev_replace target drive is
6327 * available as a mirror
6328 */
6329 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6330 WARN_ON(num_stripes > 1);
6331 bbio->stripes[0].dev = dev_replace->tgtdev;
6332 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6333 bbio->mirror_num = map->num_stripes + 1;
6334 }
Chris Masoncea9e442008-04-09 16:28:12 -04006335out:
Liu Bo73beece2015-07-17 16:49:19 +08006336 if (dev_replace_is_ongoing) {
David Sterba7fb2ece2018-08-24 17:33:58 +02006337 ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
6338 btrfs_dev_replace_read_lock(dev_replace);
6339 /* Barrier implied by atomic_dec_and_test */
6340 if (atomic_dec_and_test(&dev_replace->blocking_readers))
6341 cond_wake_up_nomb(&dev_replace->read_lock_wq);
David Sterba7e79cb82018-03-24 02:11:38 +01006342 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08006343 }
Chris Mason0b86a832008-03-24 15:01:56 -04006344 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006345 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006346}
6347
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006348int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006349 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006350 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006351{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006352 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006353 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006354}
6355
Miao Xieaf8e2d12014-10-23 14:42:50 +08006356/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006357int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006358 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006359 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006360{
David Sterba825ad4c2017-03-28 14:45:22 +02006361 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006362}
6363
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006364int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6365 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006366{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006367 struct extent_map *em;
6368 struct map_lookup *map;
6369 u64 *buf;
6370 u64 bytenr;
6371 u64 length;
6372 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006373 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006374 int i, j, nr = 0;
6375
Omar Sandoval60ca8422018-05-16 16:34:31 -07006376 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006377 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006378 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006379
Jeff Mahoney95617d62015-06-03 10:55:48 -04006380 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006381 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006382 rmap_len = map->stripe_len;
6383
Yan Zhenga512bbf2008-12-08 16:46:26 -05006384 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006385 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006386 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006387 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006388 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006389 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006390 rmap_len = map->stripe_len * nr_data_stripes(map);
6391 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006392
David Sterba31e818f2015-02-20 18:00:26 +01006393 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006394 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006395
6396 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006397 if (map->stripes[i].physical > physical ||
6398 map->stripes[i].physical + length <= physical)
6399 continue;
6400
6401 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006402 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006403
6404 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6405 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006406 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006407 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6408 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006409 } /* else if RAID[56], multiply by nr_data_stripes().
6410 * Alternatively, just use rmap_len below instead of
6411 * map->stripe_len */
6412
6413 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006414 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006415 for (j = 0; j < nr; j++) {
6416 if (buf[j] == bytenr)
6417 break;
6418 }
Chris Mason934d3752008-12-08 16:43:10 -05006419 if (j == nr) {
6420 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006421 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006422 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006423 }
6424
Yan Zhenga512bbf2008-12-08 16:46:26 -05006425 *logical = buf;
6426 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006427 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006428
6429 free_extent_map(em);
6430 return 0;
6431}
6432
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006433static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006434{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006435 bio->bi_private = bbio->private;
6436 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006437 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006438
Zhao Lei6e9606d2015-01-20 15:11:34 +08006439 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006440}
6441
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006442static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006443{
Chris Mason9be33952013-05-17 18:30:14 -04006444 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006445 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006446
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006447 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006448 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006449 if (bio->bi_status == BLK_STS_IOERR ||
6450 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006451 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006452 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006453 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006454
6455 BUG_ON(stripe_index >= bbio->num_stripes);
6456 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006457 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006458 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006459 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006460 BTRFS_DEV_STAT_WRITE_ERRS);
6461 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006462 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006463 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006464 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006465 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006466 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006467 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006468 }
6469 }
Chris Mason8790d502008-04-03 16:29:03 -04006470
Jan Schmidta1d3c472011-08-04 17:15:33 +02006471 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006472 is_orig_bio = 1;
6473
Miao Xiec404e0d2014-01-30 16:46:55 +08006474 btrfs_bio_counter_dec(bbio->fs_info);
6475
Jan Schmidta1d3c472011-08-04 17:15:33 +02006476 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006477 if (!is_orig_bio) {
6478 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006479 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006480 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006481
Chris Mason9be33952013-05-17 18:30:14 -04006482 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006483 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006484 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006485 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006486 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006487 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006488 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006489 /*
6490 * this bio is actually up to date, we didn't
6491 * go over the max number of errors
6492 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006493 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006494 }
Miao Xiec55f1392014-06-19 10:42:54 +08006495
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006496 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006497 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006498 bio_put(bio);
6499 }
Chris Mason8790d502008-04-03 16:29:03 -04006500}
6501
Chris Mason8b712842008-06-11 16:50:36 -04006502/*
6503 * see run_scheduled_bios for a description of why bios are collected for
6504 * async submit.
6505 *
6506 * This will add one bio to the pending list for a device and make sure
6507 * the work struct is scheduled.
6508 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006509static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006510 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006511{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006512 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006513 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006514 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006515
6516 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006517 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006518 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006519 return;
Chris Mason8b712842008-06-11 16:50:36 -04006520 }
6521
Chris Mason492bb6de2008-07-31 16:29:02 -04006522 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006523 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006524
6525 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006526 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006527 pending_bios = &device->pending_sync_bios;
6528 else
6529 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006530
Chris Masonffbd5172009-04-20 15:50:09 -04006531 if (pending_bios->tail)
6532 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006533
Chris Masonffbd5172009-04-20 15:50:09 -04006534 pending_bios->tail = bio;
6535 if (!pending_bios->head)
6536 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006537 if (device->running_pending)
6538 should_queue = 0;
6539
6540 spin_unlock(&device->io_lock);
6541
6542 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006543 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006544}
6545
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006546static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6547 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006548{
6549 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006550 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006551
6552 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006553 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006554 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006555 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006556 btrfs_debug_in_rcu(fs_info,
6557 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6558 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6559 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6560 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006561 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006562
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006563 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006564
Josef Bacikde1ee922012-10-19 16:50:56 -04006565 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006566 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006567 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006568 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006569}
6570
Josef Bacikde1ee922012-10-19 16:50:56 -04006571static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6572{
6573 atomic_inc(&bbio->error);
6574 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006575 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006576 WARN_ON(bio != bbio->orig_bio);
6577
Chris Mason9be33952013-05-17 18:30:14 -04006578 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006579 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006580 if (atomic_read(&bbio->error) > bbio->max_errors)
6581 bio->bi_status = BLK_STS_IOERR;
6582 else
6583 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006584 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006585 }
6586}
6587
Omar Sandoval58efbc92017-08-22 23:45:59 -07006588blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6589 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006590{
Chris Mason0b86a832008-03-24 15:01:56 -04006591 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006592 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006593 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006594 u64 length = 0;
6595 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006596 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006597 int dev_nr;
6598 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006599 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006600
Kent Overstreet4f024f32013-10-11 15:44:27 -07006601 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006602 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006603
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006604 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006605 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006606 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006607 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006608 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006609 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006610 }
Chris Masoncea9e442008-04-09 16:28:12 -04006611
Jan Schmidta1d3c472011-08-04 17:15:33 +02006612 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006613 bbio->orig_bio = first_bio;
6614 bbio->private = first_bio->bi_private;
6615 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006616 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006617 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6618
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006619 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006620 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006621 /* In this case, map_length has been set to the length of
6622 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006623 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006624 ret = raid56_parity_write(fs_info, bio, bbio,
6625 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006626 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006627 ret = raid56_parity_recover(fs_info, bio, bbio,
6628 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006629 }
Miao Xie42452152014-11-25 16:39:28 +08006630
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006631 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006632 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006633 }
6634
Chris Masoncea9e442008-04-09 16:28:12 -04006635 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006636 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006637 "mapping failed logical %llu bio len %llu len %llu",
6638 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006639 BUG();
6640 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006641
Zhao Lei08da7572015-02-12 15:42:16 +08006642 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006643 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006644 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6645 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006646 (bio_op(first_bio) == REQ_OP_WRITE &&
6647 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006648 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006649 continue;
6650 }
6651
David Sterba3aa8e072017-06-02 17:38:30 +02006652 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006653 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006654 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006655 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006656
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006657 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6658 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006659 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006660 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006661 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006662}
6663
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006664struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006665 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006666{
Yan Zheng2b820322008-11-17 21:11:30 -05006667 struct btrfs_device *device;
6668 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006669
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006670 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006671 while (cur_devices) {
6672 if (!fsid ||
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006673 !memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006674 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006675 if (device)
6676 return device;
6677 }
6678 cur_devices = cur_devices->seed;
6679 }
6680 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006681}
6682
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006683static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006684 u64 devid, u8 *dev_uuid)
6685{
6686 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006687
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006688 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6689 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006690 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006691
6692 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006693 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006694 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006695
Anand Jaine6e674b2017-12-04 12:54:54 +08006696 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006697 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006698
Chris Masondfe25022008-05-13 13:46:40 -04006699 return device;
6700}
6701
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006702/**
6703 * btrfs_alloc_device - allocate struct btrfs_device
6704 * @fs_info: used only for generating a new devid, can be NULL if
6705 * devid is provided (i.e. @devid != NULL).
6706 * @devid: a pointer to devid for this device. If NULL a new devid
6707 * is generated.
6708 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6709 * is generated.
6710 *
6711 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006712 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006713 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006714 */
6715struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6716 const u64 *devid,
6717 const u8 *uuid)
6718{
6719 struct btrfs_device *dev;
6720 u64 tmp;
6721
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306722 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006723 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006724
6725 dev = __alloc_device();
6726 if (IS_ERR(dev))
6727 return dev;
6728
6729 if (devid)
6730 tmp = *devid;
6731 else {
6732 int ret;
6733
6734 ret = find_next_devid(fs_info, &tmp);
6735 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006736 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006737 return ERR_PTR(ret);
6738 }
6739 }
6740 dev->devid = tmp;
6741
6742 if (uuid)
6743 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6744 else
6745 generate_random_uuid(dev->uuid);
6746
Liu Bo9e0af232014-08-15 23:36:53 +08006747 btrfs_init_work(&dev->work, btrfs_submit_helper,
6748 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006749
6750 return dev;
6751}
6752
Liu Boe06cd3d2016-06-03 12:05:15 -07006753/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006754static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006755 struct extent_buffer *leaf,
6756 struct btrfs_chunk *chunk, u64 logical)
6757{
6758 u64 length;
6759 u64 stripe_len;
6760 u16 num_stripes;
6761 u16 sub_stripes;
6762 u64 type;
Gu Jinxiang315409b2018-07-04 18:16:39 +08006763 u64 features;
6764 bool mixed = false;
Liu Boe06cd3d2016-06-03 12:05:15 -07006765
6766 length = btrfs_chunk_length(leaf, chunk);
6767 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6768 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6769 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6770 type = btrfs_chunk_type(leaf, chunk);
6771
6772 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006773 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006774 num_stripes);
6775 return -EIO;
6776 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006777 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6778 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006779 return -EIO;
6780 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006781 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6782 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006783 btrfs_chunk_sector_size(leaf, chunk));
6784 return -EIO;
6785 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006786 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6787 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006788 return -EIO;
6789 }
6790 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006791 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006792 stripe_len);
6793 return -EIO;
6794 }
6795 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6796 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006797 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006798 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6799 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6800 btrfs_chunk_type(leaf, chunk));
6801 return -EIO;
6802 }
Gu Jinxiang315409b2018-07-04 18:16:39 +08006803
6804 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
6805 btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
6806 return -EIO;
6807 }
6808
6809 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
6810 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
6811 btrfs_err(fs_info,
6812 "system chunk with data or metadata type: 0x%llx", type);
6813 return -EIO;
6814 }
6815
6816 features = btrfs_super_incompat_flags(fs_info->super_copy);
6817 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
6818 mixed = true;
6819
6820 if (!mixed) {
6821 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
6822 (type & BTRFS_BLOCK_GROUP_DATA)) {
6823 btrfs_err(fs_info,
6824 "mixed chunk type in non-mixed mode: 0x%llx", type);
6825 return -EIO;
6826 }
6827 }
6828
Liu Boe06cd3d2016-06-03 12:05:15 -07006829 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6830 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6831 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6832 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6833 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6834 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6835 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006836 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006837 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6838 num_stripes, sub_stripes,
6839 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6840 return -EIO;
6841 }
6842
6843 return 0;
6844}
6845
Anand Jain5a2b8e62017-10-09 11:07:45 +08006846static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006847 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006848{
Anand Jain2b902df2017-10-09 11:07:46 +08006849 if (error)
6850 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6851 devid, uuid);
6852 else
6853 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6854 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006855}
6856
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006857static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006858 struct extent_buffer *leaf,
6859 struct btrfs_chunk *chunk)
6860{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006861 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006862 struct map_lookup *map;
6863 struct extent_map *em;
6864 u64 logical;
6865 u64 length;
6866 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006867 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006868 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006869 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006870 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006871
Chris Masone17cade2008-04-15 15:41:47 -04006872 logical = key->offset;
6873 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006874 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006875
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006876 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006877 if (ret)
6878 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006879
Chris Mason890871b2009-09-02 16:24:52 -04006880 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006881 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006882 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006883
6884 /* already mapped? */
6885 if (em && em->start <= logical && em->start + em->len > logical) {
6886 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006887 return 0;
6888 } else if (em) {
6889 free_extent_map(em);
6890 }
Chris Mason0b86a832008-03-24 15:01:56 -04006891
David Sterba172ddd62011-04-21 00:48:27 +02006892 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006893 if (!em)
6894 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006895 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006896 if (!map) {
6897 free_extent_map(em);
6898 return -ENOMEM;
6899 }
6900
Wang Shilong298a8f92014-06-19 10:42:52 +08006901 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006902 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006903 em->start = logical;
6904 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006905 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006906 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006907 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006908
Chris Mason593060d2008-03-25 16:50:33 -04006909 map->num_stripes = num_stripes;
6910 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6911 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006912 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6913 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006914 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006915 map->verified_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04006916 for (i = 0; i < num_stripes; i++) {
6917 map->stripes[i].physical =
6918 btrfs_stripe_offset_nr(leaf, chunk, i);
6919 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006920 read_extent_buffer(leaf, uuid, (unsigned long)
6921 btrfs_stripe_dev_uuid_nr(chunk, i),
6922 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006923 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006924 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006925 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006926 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006927 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006928 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006929 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006930 }
Chris Masondfe25022008-05-13 13:46:40 -04006931 if (!map->stripes[i].dev) {
6932 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006933 add_missing_dev(fs_info->fs_devices, devid,
6934 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006935 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006936 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006937 btrfs_err(fs_info,
6938 "failed to init missing dev %llu: %ld",
6939 devid, PTR_ERR(map->stripes[i].dev));
6940 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006941 }
Anand Jain2b902df2017-10-09 11:07:46 +08006942 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006943 }
Anand Jaine12c9622017-12-04 12:54:53 +08006944 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6945 &(map->stripes[i].dev->dev_state));
6946
Chris Mason0b86a832008-03-24 15:01:56 -04006947 }
6948
Chris Mason890871b2009-09-02 16:24:52 -04006949 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006950 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006951 write_unlock(&map_tree->map_tree.lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006952 if (ret < 0) {
6953 btrfs_err(fs_info,
6954 "failed to add chunk map, start=%llu len=%llu: %d",
6955 em->start, em->len, ret);
6956 }
Chris Mason0b86a832008-03-24 15:01:56 -04006957 free_extent_map(em);
6958
Qu Wenruo64f64f42018-08-01 10:37:20 +08006959 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006960}
6961
Jeff Mahoney143bede2012-03-01 14:56:26 +01006962static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006963 struct btrfs_dev_item *dev_item,
6964 struct btrfs_device *device)
6965{
6966 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006967
6968 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006969 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6970 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006971 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006972 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006973 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006974 device->type = btrfs_device_type(leaf, dev_item);
6975 device->io_align = btrfs_device_io_align(leaf, dev_item);
6976 device->io_width = btrfs_device_io_width(leaf, dev_item);
6977 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006978 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006979 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006980
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006981 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006982 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006983}
6984
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006985static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006986 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006987{
6988 struct btrfs_fs_devices *fs_devices;
6989 int ret;
6990
David Sterbaa32bf9a2018-03-16 02:21:22 +01006991 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006992 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006993
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006994 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006995 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006996 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006997 return fs_devices;
6998
Yan Zheng2b820322008-11-17 21:11:30 -05006999 fs_devices = fs_devices->seed;
7000 }
7001
Nikolay Borisov7239ff42018-10-30 16:43:23 +02007002 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05007003 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007004 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08007005 return ERR_PTR(-ENOENT);
7006
Nikolay Borisov7239ff42018-10-30 16:43:23 +02007007 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08007008 if (IS_ERR(fs_devices))
7009 return fs_devices;
7010
7011 fs_devices->seeding = 1;
7012 fs_devices->opened = 1;
7013 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05007014 }
Yan Zhenge4404d62008-12-12 10:03:26 -05007015
7016 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08007017 if (IS_ERR(fs_devices))
7018 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05007019
Anand Jain897fb572018-04-12 10:29:28 +08007020 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02007021 if (ret) {
7022 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08007023 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05007024 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02007025 }
Yan Zheng2b820322008-11-17 21:11:30 -05007026
7027 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08007028 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05007029 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08007030 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05007031 goto out;
7032 }
7033
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007034 fs_devices->seed = fs_info->fs_devices->seed;
7035 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05007036out:
Miao Xie5f375832014-09-03 21:35:46 +08007037 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05007038}
7039
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007040static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04007041 struct extent_buffer *leaf,
7042 struct btrfs_dev_item *dev_item)
7043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007044 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04007045 struct btrfs_device *device;
7046 u64 devid;
7047 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08007048 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04007049 u8 dev_uuid[BTRFS_UUID_SIZE];
7050
Chris Mason0b86a832008-03-24 15:01:56 -04007051 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02007052 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04007053 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02007054 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08007055 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05007056
Nikolay Borisovde37aa52018-10-30 16:43:24 +02007057 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007058 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007059 if (IS_ERR(fs_devices))
7060 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05007061 }
7062
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007063 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007064 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08007065 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08007066 btrfs_report_missing_device(fs_info, devid,
7067 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007068 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08007069 }
Yan Zheng2b820322008-11-17 21:11:30 -05007070
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007071 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08007072 if (IS_ERR(device)) {
7073 btrfs_err(fs_info,
7074 "failed to add missing dev %llu: %ld",
7075 devid, PTR_ERR(device));
7076 return PTR_ERR(device);
7077 }
Anand Jain2b902df2017-10-09 11:07:46 +08007078 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08007079 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08007080 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08007081 if (!btrfs_test_opt(fs_info, DEGRADED)) {
7082 btrfs_report_missing_device(fs_info,
7083 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007084 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08007085 }
7086 btrfs_report_missing_device(fs_info, devid,
7087 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08007088 }
Miao Xie5f375832014-09-03 21:35:46 +08007089
Anand Jaine6e674b2017-12-04 12:54:54 +08007090 if (!device->bdev &&
7091 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05007092 /*
7093 * this happens when a device that was properly setup
7094 * in the device info lists suddenly goes bad.
7095 * device->bdev is NULL, and so we have to set
7096 * device->missing to one here
7097 */
Miao Xie5f375832014-09-03 21:35:46 +08007098 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08007099 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05007100 }
Miao Xie5f375832014-09-03 21:35:46 +08007101
7102 /* Move the device to its own fs_devices */
7103 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08007104 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
7105 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08007106
7107 list_move(&device->dev_list, &fs_devices->devices);
7108 device->fs_devices->num_devices--;
7109 fs_devices->num_devices++;
7110
7111 device->fs_devices->missing_devices--;
7112 fs_devices->missing_devices++;
7113
7114 device->fs_devices = fs_devices;
7115 }
Yan Zheng2b820322008-11-17 21:11:30 -05007116 }
7117
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007118 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08007119 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05007120 if (device->generation !=
7121 btrfs_device_generation(leaf, dev_item))
7122 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04007123 }
Chris Mason0b86a832008-03-24 15:01:56 -04007124
7125 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08007126 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08007127 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08007128 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05007129 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03007130 atomic64_add(device->total_bytes - device->bytes_used,
7131 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04007132 }
Chris Mason0b86a832008-03-24 15:01:56 -04007133 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007134 return ret;
7135}
7136
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007137int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007138{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007139 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007140 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04007141 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04007142 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04007143 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01007144 u8 *array_ptr;
7145 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04007146 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007147 u32 num_stripes;
7148 u32 array_size;
7149 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01007150 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07007151 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04007152 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04007153
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007154 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02007155 /*
7156 * This will create extent buffer of nodesize, superblock size is
7157 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7158 * overallocate but we can keep it as-is, only the first page is used.
7159 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007160 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07007161 if (IS_ERR(sb))
7162 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01007163 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04007164 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02007165 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04007166 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01007167 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02007168 * pages up-to-date when the page is larger: extent does not cover the
7169 * whole page and consequently check_page_uptodate does not find all
7170 * the page's extents up-to-date (the hole beyond sb),
7171 * write_extent_buffer then triggers a WARN_ON.
7172 *
7173 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7174 * but sb spans only this function. Add an explicit SetPageUptodate call
7175 * to silence the warning eg. on PowerPC 64.
7176 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007177 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007178 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007179
Chris Masona061fc82008-05-07 11:43:44 -04007180 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007181 array_size = btrfs_super_sys_array_size(super_copy);
7182
David Sterba1ffb22c2014-10-31 19:02:42 +01007183 array_ptr = super_copy->sys_chunk_array;
7184 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7185 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007186
David Sterba1ffb22c2014-10-31 19:02:42 +01007187 while (cur_offset < array_size) {
7188 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007189 len = sizeof(*disk_key);
7190 if (cur_offset + len > array_size)
7191 goto out_short_read;
7192
Chris Mason0b86a832008-03-24 15:01:56 -04007193 btrfs_disk_key_to_cpu(&key, disk_key);
7194
David Sterba1ffb22c2014-10-31 19:02:42 +01007195 array_ptr += len;
7196 sb_array_offset += len;
7197 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007198
Chris Mason0d81ba52008-03-24 15:02:07 -04007199 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01007200 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01007201 /*
7202 * At least one btrfs_chunk with one stripe must be
7203 * present, exact stripe count check comes afterwards
7204 */
7205 len = btrfs_chunk_item_size(1);
7206 if (cur_offset + len > array_size)
7207 goto out_short_read;
7208
7209 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01007210 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007211 btrfs_err(fs_info,
7212 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01007213 num_stripes, cur_offset);
7214 ret = -EIO;
7215 break;
7216 }
7217
Liu Boe06cd3d2016-06-03 12:05:15 -07007218 type = btrfs_chunk_type(sb, chunk);
7219 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007220 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07007221 "invalid chunk type %llu in sys_array at offset %u",
7222 type, cur_offset);
7223 ret = -EIO;
7224 break;
7225 }
7226
David Sterbae3540ea2014-11-05 15:24:51 +01007227 len = btrfs_chunk_item_size(num_stripes);
7228 if (cur_offset + len > array_size)
7229 goto out_short_read;
7230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007231 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04007232 if (ret)
7233 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007234 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007235 btrfs_err(fs_info,
7236 "unexpected item type %u in sys_array at offset %u",
7237 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007238 ret = -EIO;
7239 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007240 }
David Sterba1ffb22c2014-10-31 19:02:42 +01007241 array_ptr += len;
7242 sb_array_offset += len;
7243 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007244 }
Liu Bod8651772016-06-03 17:41:42 -07007245 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007246 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007247 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007248
7249out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007250 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007251 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007252 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007253 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007254 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007255}
7256
Qu Wenruo21634a12017-03-09 09:34:36 +08007257/*
7258 * Check if all chunks in the fs are OK for read-write degraded mount
7259 *
Anand Jain6528b992017-12-18 17:08:59 +08007260 * If the @failing_dev is specified, it's accounted as missing.
7261 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007262 * Return true if all chunks meet the minimal RW mount requirements.
7263 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7264 */
Anand Jain6528b992017-12-18 17:08:59 +08007265bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7266 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007267{
7268 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
7269 struct extent_map *em;
7270 u64 next_start = 0;
7271 bool ret = true;
7272
7273 read_lock(&map_tree->map_tree.lock);
7274 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
7275 read_unlock(&map_tree->map_tree.lock);
7276 /* No chunk at all? Return false anyway */
7277 if (!em) {
7278 ret = false;
7279 goto out;
7280 }
7281 while (em) {
7282 struct map_lookup *map;
7283 int missing = 0;
7284 int max_tolerated;
7285 int i;
7286
7287 map = em->map_lookup;
7288 max_tolerated =
7289 btrfs_get_num_tolerated_disk_barrier_failures(
7290 map->type);
7291 for (i = 0; i < map->num_stripes; i++) {
7292 struct btrfs_device *dev = map->stripes[i].dev;
7293
Anand Jaine6e674b2017-12-04 12:54:54 +08007294 if (!dev || !dev->bdev ||
7295 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007296 dev->last_flush_error)
7297 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007298 else if (failing_dev && failing_dev == dev)
7299 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007300 }
7301 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007302 if (!failing_dev)
7303 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08007304 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
7305 em->start, missing, max_tolerated);
7306 free_extent_map(em);
7307 ret = false;
7308 goto out;
7309 }
7310 next_start = extent_map_end(em);
7311 free_extent_map(em);
7312
7313 read_lock(&map_tree->map_tree.lock);
7314 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
7315 (u64)(-1) - next_start);
7316 read_unlock(&map_tree->map_tree.lock);
7317 }
7318out:
7319 return ret;
7320}
7321
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007322int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007323{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007324 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007325 struct btrfs_path *path;
7326 struct extent_buffer *leaf;
7327 struct btrfs_key key;
7328 struct btrfs_key found_key;
7329 int ret;
7330 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007331 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007332
Chris Mason0b86a832008-03-24 15:01:56 -04007333 path = btrfs_alloc_path();
7334 if (!path)
7335 return -ENOMEM;
7336
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007337 /*
7338 * uuid_mutex is needed only if we are mounting a sprout FS
7339 * otherwise we don't need it.
7340 */
Li Zefanb367e472011-12-07 11:38:24 +08007341 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007342 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007343
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007344 /*
7345 * Read all device items, and then all the chunk items. All
7346 * device items are found before any chunk item (their object id
7347 * is smaller than the lowest possible object id for a chunk
7348 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007349 */
7350 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7351 key.offset = 0;
7352 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007353 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007354 if (ret < 0)
7355 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007356 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007357 leaf = path->nodes[0];
7358 slot = path->slots[0];
7359 if (slot >= btrfs_header_nritems(leaf)) {
7360 ret = btrfs_next_leaf(root, path);
7361 if (ret == 0)
7362 continue;
7363 if (ret < 0)
7364 goto error;
7365 break;
7366 }
7367 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007368 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7369 struct btrfs_dev_item *dev_item;
7370 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007371 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007372 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007373 if (ret)
7374 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007375 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007376 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7377 struct btrfs_chunk *chunk;
7378 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007379 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007380 if (ret)
7381 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007382 }
7383 path->slots[0]++;
7384 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007385
7386 /*
7387 * After loading chunk tree, we've got all device information,
7388 * do another round of validation checks.
7389 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007390 if (total_dev != fs_info->fs_devices->total_devices) {
7391 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007392 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007393 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007394 total_dev);
7395 ret = -EINVAL;
7396 goto error;
7397 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007398 if (btrfs_super_total_bytes(fs_info->super_copy) <
7399 fs_info->fs_devices->total_rw_bytes) {
7400 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007401 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007402 btrfs_super_total_bytes(fs_info->super_copy),
7403 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007404 ret = -EINVAL;
7405 goto error;
7406 }
Chris Mason0b86a832008-03-24 15:01:56 -04007407 ret = 0;
7408error:
David Sterba34441362016-10-04 19:34:27 +02007409 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007410 mutex_unlock(&uuid_mutex);
7411
Yan Zheng2b820322008-11-17 21:11:30 -05007412 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007413 return ret;
7414}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007415
Miao Xiecb517ea2013-05-15 07:48:19 +00007416void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7417{
7418 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7419 struct btrfs_device *device;
7420
Liu Bo29cc83f2014-05-11 23:14:59 +08007421 while (fs_devices) {
7422 mutex_lock(&fs_devices->device_list_mutex);
7423 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007424 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007425 mutex_unlock(&fs_devices->device_list_mutex);
7426
7427 fs_devices = fs_devices->seed;
7428 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007429}
7430
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007431static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7432{
7433 int i;
7434
7435 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7436 btrfs_dev_stat_reset(dev, i);
7437}
7438
7439int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7440{
7441 struct btrfs_key key;
7442 struct btrfs_key found_key;
7443 struct btrfs_root *dev_root = fs_info->dev_root;
7444 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7445 struct extent_buffer *eb;
7446 int slot;
7447 int ret = 0;
7448 struct btrfs_device *device;
7449 struct btrfs_path *path = NULL;
7450 int i;
7451
7452 path = btrfs_alloc_path();
7453 if (!path) {
7454 ret = -ENOMEM;
7455 goto out;
7456 }
7457
7458 mutex_lock(&fs_devices->device_list_mutex);
7459 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7460 int item_size;
7461 struct btrfs_dev_stats_item *ptr;
7462
David Sterba242e2952016-01-25 17:51:31 +01007463 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7464 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007465 key.offset = device->devid;
7466 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7467 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007468 __btrfs_reset_dev_stats(device);
7469 device->dev_stats_valid = 1;
7470 btrfs_release_path(path);
7471 continue;
7472 }
7473 slot = path->slots[0];
7474 eb = path->nodes[0];
7475 btrfs_item_key_to_cpu(eb, &found_key, slot);
7476 item_size = btrfs_item_size_nr(eb, slot);
7477
7478 ptr = btrfs_item_ptr(eb, slot,
7479 struct btrfs_dev_stats_item);
7480
7481 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7482 if (item_size >= (1 + i) * sizeof(__le64))
7483 btrfs_dev_stat_set(device, i,
7484 btrfs_dev_stats_value(eb, ptr, i));
7485 else
7486 btrfs_dev_stat_reset(device, i);
7487 }
7488
7489 device->dev_stats_valid = 1;
7490 btrfs_dev_stat_print_on_load(device);
7491 btrfs_release_path(path);
7492 }
7493 mutex_unlock(&fs_devices->device_list_mutex);
7494
7495out:
7496 btrfs_free_path(path);
7497 return ret < 0 ? ret : 0;
7498}
7499
7500static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007501 struct btrfs_device *device)
7502{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007503 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007504 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007505 struct btrfs_path *path;
7506 struct btrfs_key key;
7507 struct extent_buffer *eb;
7508 struct btrfs_dev_stats_item *ptr;
7509 int ret;
7510 int i;
7511
David Sterba242e2952016-01-25 17:51:31 +01007512 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7513 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007514 key.offset = device->devid;
7515
7516 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007517 if (!path)
7518 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007519 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7520 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007521 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007522 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007523 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007524 goto out;
7525 }
7526
7527 if (ret == 0 &&
7528 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7529 /* need to delete old one and insert a new one */
7530 ret = btrfs_del_item(trans, dev_root, path);
7531 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007532 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007533 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007534 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007535 goto out;
7536 }
7537 ret = 1;
7538 }
7539
7540 if (ret == 1) {
7541 /* need to insert a new item */
7542 btrfs_release_path(path);
7543 ret = btrfs_insert_empty_item(trans, dev_root, path,
7544 &key, sizeof(*ptr));
7545 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007546 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007547 "insert dev_stats item for device %s failed %d",
7548 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007549 goto out;
7550 }
7551 }
7552
7553 eb = path->nodes[0];
7554 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7555 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7556 btrfs_set_dev_stats_value(eb, ptr, i,
7557 btrfs_dev_stat_read(device, i));
7558 btrfs_mark_buffer_dirty(eb);
7559
7560out:
7561 btrfs_free_path(path);
7562 return ret;
7563}
7564
7565/*
7566 * called from commit_transaction. Writes all changed device stats to disk.
7567 */
7568int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7569 struct btrfs_fs_info *fs_info)
7570{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007571 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7572 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007573 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007574 int ret = 0;
7575
7576 mutex_lock(&fs_devices->device_list_mutex);
7577 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007578 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7579 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007580 continue;
7581
Nikolay Borisov9deae962017-10-24 13:47:37 +03007582
7583 /*
7584 * There is a LOAD-LOAD control dependency between the value of
7585 * dev_stats_ccnt and updating the on-disk values which requires
7586 * reading the in-memory counters. Such control dependencies
7587 * require explicit read memory barriers.
7588 *
7589 * This memory barriers pairs with smp_mb__before_atomic in
7590 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7591 * barrier implied by atomic_xchg in
7592 * btrfs_dev_stats_read_and_reset
7593 */
7594 smp_rmb();
7595
Nikolay Borisov5495f192018-07-20 19:37:49 +03007596 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007597 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007598 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007599 }
7600 mutex_unlock(&fs_devices->device_list_mutex);
7601
7602 return ret;
7603}
7604
Stefan Behrens442a4f62012-05-25 16:06:08 +02007605void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7606{
7607 btrfs_dev_stat_inc(dev, index);
7608 btrfs_dev_stat_print_on_error(dev);
7609}
7610
Eric Sandeen48a3b632013-04-25 20:41:01 +00007611static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007612{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007613 if (!dev->dev_stats_valid)
7614 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007615 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007616 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007617 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007618 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7619 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7620 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007621 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7622 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007623}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007624
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007625static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7626{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007627 int i;
7628
7629 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7630 if (btrfs_dev_stat_read(dev, i) != 0)
7631 break;
7632 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7633 return; /* all values == 0, suppress message */
7634
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007635 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007636 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007637 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007638 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7639 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7640 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7641 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7642 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7643}
7644
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007645int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007646 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007647{
7648 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007649 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007650 int i;
7651
7652 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007653 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007654 mutex_unlock(&fs_devices->device_list_mutex);
7655
7656 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007657 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007658 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007659 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007660 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007661 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007662 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007663 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7664 if (stats->nr_items > i)
7665 stats->values[i] =
7666 btrfs_dev_stat_read_and_reset(dev, i);
7667 else
7668 btrfs_dev_stat_reset(dev, i);
7669 }
7670 } else {
7671 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7672 if (stats->nr_items > i)
7673 stats->values[i] = btrfs_dev_stat_read(dev, i);
7674 }
7675 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7676 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7677 return 0;
7678}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007679
David Sterbada353f62017-02-14 17:55:53 +01007680void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007681{
7682 struct buffer_head *bh;
7683 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007684 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007685
Anand Jain12b1c262015-08-14 18:32:59 +08007686 if (!bdev)
7687 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007688
Anand Jain12b1c262015-08-14 18:32:59 +08007689 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7690 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007691
Anand Jain12b1c262015-08-14 18:32:59 +08007692 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7693 continue;
7694
7695 disk_super = (struct btrfs_super_block *)bh->b_data;
7696
7697 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7698 set_buffer_dirty(bh);
7699 sync_dirty_buffer(bh);
7700 brelse(bh);
7701 }
7702
7703 /* Notify udev that device has changed */
7704 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7705
7706 /* Update ctime/mtime for device path for libblkid */
7707 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007708}
Miao Xie935e5cc2014-09-03 21:35:33 +08007709
7710/*
7711 * Update the size of all devices, which is used for writing out the
7712 * super blocks.
7713 */
7714void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7715{
7716 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7717 struct btrfs_device *curr, *next;
7718
7719 if (list_empty(&fs_devices->resized_devices))
7720 return;
7721
7722 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007723 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007724 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7725 resized_list) {
7726 list_del_init(&curr->resized_list);
7727 curr->commit_total_bytes = curr->disk_total_bytes;
7728 }
David Sterba34441362016-10-04 19:34:27 +02007729 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007730 mutex_unlock(&fs_devices->device_list_mutex);
7731}
Miao Xiece7213c2014-09-03 21:35:34 +08007732
7733/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007734void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007735{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007736 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007737 struct extent_map *em;
7738 struct map_lookup *map;
7739 struct btrfs_device *dev;
7740 int i;
7741
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007742 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007743 return;
7744
7745 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007746 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007747 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007748 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007749
7750 for (i = 0; i < map->num_stripes; i++) {
7751 dev = map->stripes[i].dev;
7752 dev->commit_bytes_used = dev->bytes_used;
7753 }
7754 }
David Sterba34441362016-10-04 19:34:27 +02007755 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007756}
Anand Jain5a13f432015-03-10 06:38:31 +08007757
7758void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7759{
7760 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7761 while (fs_devices) {
7762 fs_devices->fs_info = fs_info;
7763 fs_devices = fs_devices->seed;
7764 }
7765}
7766
7767void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7768{
7769 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7770 while (fs_devices) {
7771 fs_devices->fs_info = NULL;
7772 fs_devices = fs_devices->seed;
7773 }
7774}
David Sterba46df06b2018-07-13 20:46:30 +02007775
7776/*
7777 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7778 */
7779int btrfs_bg_type_to_factor(u64 flags)
7780{
7781 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7782 BTRFS_BLOCK_GROUP_RAID10))
7783 return 2;
7784 return 1;
7785}
Qu Wenruocf90d882018-08-01 10:37:19 +08007786
7787
7788static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7789{
7790 int index = btrfs_bg_flags_to_raid_index(type);
7791 int ncopies = btrfs_raid_array[index].ncopies;
7792 int data_stripes;
7793
7794 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7795 case BTRFS_BLOCK_GROUP_RAID5:
7796 data_stripes = num_stripes - 1;
7797 break;
7798 case BTRFS_BLOCK_GROUP_RAID6:
7799 data_stripes = num_stripes - 2;
7800 break;
7801 default:
7802 data_stripes = num_stripes / ncopies;
7803 break;
7804 }
7805 return div_u64(chunk_len, data_stripes);
7806}
7807
7808static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7809 u64 chunk_offset, u64 devid,
7810 u64 physical_offset, u64 physical_len)
7811{
7812 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7813 struct extent_map *em;
7814 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007815 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007816 u64 stripe_len;
7817 bool found = false;
7818 int ret = 0;
7819 int i;
7820
7821 read_lock(&em_tree->lock);
7822 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7823 read_unlock(&em_tree->lock);
7824
7825 if (!em) {
7826 btrfs_err(fs_info,
7827"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7828 physical_offset, devid);
7829 ret = -EUCLEAN;
7830 goto out;
7831 }
7832
7833 map = em->map_lookup;
7834 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7835 if (physical_len != stripe_len) {
7836 btrfs_err(fs_info,
7837"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7838 physical_offset, devid, em->start, physical_len,
7839 stripe_len);
7840 ret = -EUCLEAN;
7841 goto out;
7842 }
7843
7844 for (i = 0; i < map->num_stripes; i++) {
7845 if (map->stripes[i].dev->devid == devid &&
7846 map->stripes[i].physical == physical_offset) {
7847 found = true;
7848 if (map->verified_stripes >= map->num_stripes) {
7849 btrfs_err(fs_info,
7850 "too many dev extents for chunk %llu found",
7851 em->start);
7852 ret = -EUCLEAN;
7853 goto out;
7854 }
7855 map->verified_stripes++;
7856 break;
7857 }
7858 }
7859 if (!found) {
7860 btrfs_err(fs_info,
7861 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7862 physical_offset, devid);
7863 ret = -EUCLEAN;
7864 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007865
7866 /* Make sure no dev extent is beyond device bondary */
7867 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
7868 if (!dev) {
7869 btrfs_err(fs_info, "failed to find devid %llu", devid);
7870 ret = -EUCLEAN;
7871 goto out;
7872 }
7873 if (physical_offset + physical_len > dev->disk_total_bytes) {
7874 btrfs_err(fs_info,
7875"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7876 devid, physical_offset, physical_len,
7877 dev->disk_total_bytes);
7878 ret = -EUCLEAN;
7879 goto out;
7880 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007881out:
7882 free_extent_map(em);
7883 return ret;
7884}
7885
7886static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7887{
7888 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7889 struct extent_map *em;
7890 struct rb_node *node;
7891 int ret = 0;
7892
7893 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007894 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007895 em = rb_entry(node, struct extent_map, rb_node);
7896 if (em->map_lookup->num_stripes !=
7897 em->map_lookup->verified_stripes) {
7898 btrfs_err(fs_info,
7899 "chunk %llu has missing dev extent, have %d expect %d",
7900 em->start, em->map_lookup->verified_stripes,
7901 em->map_lookup->num_stripes);
7902 ret = -EUCLEAN;
7903 goto out;
7904 }
7905 }
7906out:
7907 read_unlock(&em_tree->lock);
7908 return ret;
7909}
7910
7911/*
7912 * Ensure that all dev extents are mapped to correct chunk, otherwise
7913 * later chunk allocation/free would cause unexpected behavior.
7914 *
7915 * NOTE: This will iterate through the whole device tree, which should be of
7916 * the same size level as the chunk tree. This slightly increases mount time.
7917 */
7918int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7919{
7920 struct btrfs_path *path;
7921 struct btrfs_root *root = fs_info->dev_root;
7922 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007923 u64 prev_devid = 0;
7924 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007925 int ret = 0;
7926
7927 key.objectid = 1;
7928 key.type = BTRFS_DEV_EXTENT_KEY;
7929 key.offset = 0;
7930
7931 path = btrfs_alloc_path();
7932 if (!path)
7933 return -ENOMEM;
7934
7935 path->reada = READA_FORWARD;
7936 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7937 if (ret < 0)
7938 goto out;
7939
7940 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7941 ret = btrfs_next_item(root, path);
7942 if (ret < 0)
7943 goto out;
7944 /* No dev extents at all? Not good */
7945 if (ret > 0) {
7946 ret = -EUCLEAN;
7947 goto out;
7948 }
7949 }
7950 while (1) {
7951 struct extent_buffer *leaf = path->nodes[0];
7952 struct btrfs_dev_extent *dext;
7953 int slot = path->slots[0];
7954 u64 chunk_offset;
7955 u64 physical_offset;
7956 u64 physical_len;
7957 u64 devid;
7958
7959 btrfs_item_key_to_cpu(leaf, &key, slot);
7960 if (key.type != BTRFS_DEV_EXTENT_KEY)
7961 break;
7962 devid = key.objectid;
7963 physical_offset = key.offset;
7964
7965 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7966 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7967 physical_len = btrfs_dev_extent_length(leaf, dext);
7968
Qu Wenruo5eb19382018-10-05 17:45:54 +08007969 /* Check if this dev extent overlaps with the previous one */
7970 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7971 btrfs_err(fs_info,
7972"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7973 devid, physical_offset, prev_dev_ext_end);
7974 ret = -EUCLEAN;
7975 goto out;
7976 }
7977
Qu Wenruocf90d882018-08-01 10:37:19 +08007978 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7979 physical_offset, physical_len);
7980 if (ret < 0)
7981 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007982 prev_devid = devid;
7983 prev_dev_ext_end = physical_offset + physical_len;
7984
Qu Wenruocf90d882018-08-01 10:37:19 +08007985 ret = btrfs_next_item(root, path);
7986 if (ret < 0)
7987 goto out;
7988 if (ret > 0) {
7989 ret = 0;
7990 break;
7991 }
7992 }
7993
7994 /* Ensure all chunks have corresponding dev extents */
7995 ret = verify_chunk_dev_extent_mapping(fs_info);
7996out:
7997 btrfs_free_path(path);
7998 return ret;
7999}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07008000
8001/*
8002 * Check whether the given block group or device is pinned by any inode being
8003 * used as a swapfile.
8004 */
8005bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
8006{
8007 struct btrfs_swapfile_pin *sp;
8008 struct rb_node *node;
8009
8010 spin_lock(&fs_info->swapfile_pins_lock);
8011 node = fs_info->swapfile_pins.rb_node;
8012 while (node) {
8013 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
8014 if (ptr < sp->ptr)
8015 node = node->rb_left;
8016 else if (ptr > sp->ptr)
8017 node = node->rb_right;
8018 else
8019 break;
8020 }
8021 spin_unlock(&fs_info->swapfile_pins_lock);
8022 return node != NULL;
8023}