blob: 5f39d86d41ec2b64b4d30131eb980152fe261ab1 [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"
Qu Wenruo82fc28f2019-03-20 13:16:42 +080030#include "tree-checker.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031
Zhao Leiaf902042015-09-15 21:08:06 +080032const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
33 [BTRFS_RAID_RAID10] = {
34 .sub_stripes = 2,
35 .dev_stripes = 1,
36 .devs_max = 0, /* 0 == as many as possible */
37 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080038 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080039 .devs_increment = 2,
40 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020041 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080042 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080043 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080044 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080045 },
46 [BTRFS_RAID_RAID1] = {
47 .sub_stripes = 1,
48 .dev_stripes = 1,
49 .devs_max = 2,
50 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080051 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080052 .devs_increment = 2,
53 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020054 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080055 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080056 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080057 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080058 },
59 [BTRFS_RAID_DUP] = {
60 .sub_stripes = 1,
61 .dev_stripes = 2,
62 .devs_max = 1,
63 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080064 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080065 .devs_increment = 1,
66 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020067 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080068 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080069 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080070 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080071 },
72 [BTRFS_RAID_RAID0] = {
73 .sub_stripes = 1,
74 .dev_stripes = 1,
75 .devs_max = 0,
76 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080077 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080078 .devs_increment = 1,
79 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020080 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080081 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +080082 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080083 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080084 },
85 [BTRFS_RAID_SINGLE] = {
86 .sub_stripes = 1,
87 .dev_stripes = 1,
88 .devs_max = 1,
89 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080090 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080091 .devs_increment = 1,
92 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020093 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080094 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +080095 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080096 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080097 },
98 [BTRFS_RAID_RAID5] = {
99 .sub_stripes = 1,
100 .dev_stripes = 1,
101 .devs_max = 0,
102 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800103 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800104 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200105 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200106 .nparity = 1,
Anand Jained234672018-04-25 19:01:42 +0800107 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800108 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800109 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800110 },
111 [BTRFS_RAID_RAID6] = {
112 .sub_stripes = 1,
113 .dev_stripes = 1,
114 .devs_max = 0,
115 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800116 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800117 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200118 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200119 .nparity = 2,
Anand Jained234672018-04-25 19:01:42 +0800120 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800121 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800122 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800123 },
124};
125
Anand Jained234672018-04-25 19:01:42 +0800126const char *get_raid_name(enum btrfs_raid_types type)
127{
128 if (type >= BTRFS_NR_RAID_TYPES)
129 return NULL;
130
131 return btrfs_raid_array[type].raid_name;
132}
133
Anand Jainf89e09c2018-11-20 16:12:55 +0800134/*
135 * Fill @buf with textual description of @bg_flags, no more than @size_buf
136 * bytes including terminating null byte.
137 */
138void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
139{
140 int i;
141 int ret;
142 char *bp = buf;
143 u64 flags = bg_flags;
144 u32 size_bp = size_buf;
145
146 if (!flags) {
147 strcpy(bp, "NONE");
148 return;
149 }
150
151#define DESCRIBE_FLAG(flag, desc) \
152 do { \
153 if (flags & (flag)) { \
154 ret = snprintf(bp, size_bp, "%s|", (desc)); \
155 if (ret < 0 || ret >= size_bp) \
156 goto out_overflow; \
157 size_bp -= ret; \
158 bp += ret; \
159 flags &= ~(flag); \
160 } \
161 } while (0)
162
163 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
164 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
165 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
166
167 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
168 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
169 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
170 btrfs_raid_array[i].raid_name);
171#undef DESCRIBE_FLAG
172
173 if (flags) {
174 ret = snprintf(bp, size_bp, "0x%llx|", flags);
175 size_bp -= ret;
176 }
177
178 if (size_bp < size_buf)
179 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
180
181 /*
182 * The text is trimmed, it's up to the caller to provide sufficiently
183 * large buffer
184 */
185out_overflow:;
186}
187
Yan Zheng2b820322008-11-17 21:11:30 -0500188static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100189 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400190static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200191static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000192static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200193static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700194static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
195 enum btrfs_map_op op,
196 u64 logical, u64 *length,
197 struct btrfs_bio **bbio_ret,
198 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500199
David Sterba9c6b1c42017-06-16 22:30:00 +0200200/*
201 * Device locking
202 * ==============
203 *
204 * There are several mutexes that protect manipulation of devices and low-level
205 * structures like chunks but not block groups, extents or files
206 *
207 * uuid_mutex (global lock)
208 * ------------------------
209 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
210 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
211 * device) or requested by the device= mount option
212 *
213 * the mutex can be very coarse and can cover long-running operations
214 *
215 * protects: updates to fs_devices counters like missing devices, rw devices,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100216 * seeding, structure cloning, opening/closing devices at mount/umount time
David Sterba9c6b1c42017-06-16 22:30:00 +0200217 *
218 * global::fs_devs - add, remove, updates to the global list
219 *
220 * does not protect: manipulation of the fs_devices::devices list!
221 *
222 * btrfs_device::name - renames (write side), read is RCU
223 *
224 * fs_devices::device_list_mutex (per-fs, with RCU)
225 * ------------------------------------------------
226 * protects updates to fs_devices::devices, ie. adding and deleting
227 *
228 * simple list traversal with read-only actions can be done with RCU protection
229 *
230 * may be used to exclude some operations from running concurrently without any
231 * modifications to the list (see write_all_supers)
232 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200233 * balance_mutex
234 * -------------
235 * protects balance structures (status, state) and context accessed from
236 * several places (internally, ioctl)
237 *
238 * chunk_mutex
239 * -----------
240 * protects chunks, adding or removing during allocation, trim or when a new
241 * device is added/removed
242 *
243 * cleaner_mutex
244 * -------------
245 * a big lock that is held by the cleaner thread and prevents running subvolume
246 * cleaning together with relocation or delayed iputs
247 *
248 *
249 * Lock nesting
250 * ============
251 *
252 * uuid_mutex
253 * volume_mutex
254 * device_list_mutex
255 * chunk_mutex
256 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800257 *
258 *
259 * Exclusive operations, BTRFS_FS_EXCL_OP
260 * ======================================
261 *
262 * Maintains the exclusivity of the following operations that apply to the
263 * whole filesystem and cannot run in parallel.
264 *
265 * - Balance (*)
266 * - Device add
267 * - Device remove
268 * - Device replace (*)
269 * - Resize
270 *
271 * The device operations (as above) can be in one of the following states:
272 *
273 * - Running state
274 * - Paused state
275 * - Completed state
276 *
277 * Only device operations marked with (*) can go into the Paused state for the
278 * following reasons:
279 *
280 * - ioctl (only Balance can be Paused through ioctl)
281 * - filesystem remounted as read-only
282 * - filesystem unmounted and mounted as read-only
283 * - system power-cycle and filesystem mounted as read-only
284 * - filesystem or device errors leading to forced read-only
285 *
286 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
287 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
288 * A device operation in Paused or Running state can be canceled or resumed
289 * either by ioctl (Balance only) or when remounted as read-write.
290 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
291 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200292 */
293
Miao Xie67a2c452014-09-03 21:35:43 +0800294DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400295static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800296struct list_head *btrfs_get_fs_uuids(void)
297{
298 return &fs_uuids;
299}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400300
David Sterba2dfeca92017-06-14 02:48:07 +0200301/*
302 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200303 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
304 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200305 *
306 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
307 * The returned struct is not linked onto any lists and can be destroyed with
308 * kfree() right away.
309 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200310static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
311 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300312{
313 struct btrfs_fs_devices *fs_devs;
314
David Sterba78f2c9e2016-02-11 14:25:38 +0100315 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300316 if (!fs_devs)
317 return ERR_PTR(-ENOMEM);
318
319 mutex_init(&fs_devs->device_list_mutex);
320
321 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800322 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300323 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800324 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300325 if (fsid)
326 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300327
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200328 if (metadata_fsid)
329 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
330 else if (fsid)
331 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
332
Ilya Dryomov2208a372013-08-12 14:33:03 +0300333 return fs_devs;
334}
335
David Sterbaa425f9d2018-03-20 15:47:33 +0100336void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100337{
338 rcu_string_free(device->name);
339 bio_put(device->flush_bio);
340 kfree(device);
341}
342
Yan Zhenge4404d62008-12-12 10:03:26 -0500343static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
344{
345 struct btrfs_device *device;
346 WARN_ON(fs_devices->opened);
347 while (!list_empty(&fs_devices->devices)) {
348 device = list_entry(fs_devices->devices.next,
349 struct btrfs_device, dev_list);
350 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100351 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500352 }
353 kfree(fs_devices);
354}
355
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000356static void btrfs_kobject_uevent(struct block_device *bdev,
357 enum kobject_action action)
358{
359 int ret;
360
361 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
362 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500363 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000364 action,
365 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
366 &disk_to_dev(bdev->bd_disk)->kobj);
367}
368
David Sterbaffc5a372018-02-19 17:24:15 +0100369void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370{
371 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372
Yan Zheng2b820322008-11-17 21:11:30 -0500373 while (!list_empty(&fs_uuids)) {
374 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800375 struct btrfs_fs_devices, fs_list);
376 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500377 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400378 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400379}
380
David Sterba48dae9c2017-10-30 18:10:25 +0100381/*
382 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
383 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100384 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100385 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300386static struct btrfs_device *__alloc_device(void)
387{
388 struct btrfs_device *dev;
389
David Sterba78f2c9e2016-02-11 14:25:38 +0100390 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300391 if (!dev)
392 return ERR_PTR(-ENOMEM);
393
David Sterbae0ae9992017-06-06 17:06:06 +0200394 /*
395 * Preallocate a bio that's always going to be used for flushing device
396 * barriers and matches the device lifespan
397 */
398 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
399 if (!dev->flush_bio) {
400 kfree(dev);
401 return ERR_PTR(-ENOMEM);
402 }
David Sterbae0ae9992017-06-06 17:06:06 +0200403
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300404 INIT_LIST_HEAD(&dev->dev_list);
405 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800406 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300407
408 spin_lock_init(&dev->io_lock);
409
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300410 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800411 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100412 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700413 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800414 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300415
416 return dev;
417}
418
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200419static noinline struct btrfs_fs_devices *find_fsid(
420 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400422 struct btrfs_fs_devices *fs_devices;
423
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200424 ASSERT(fsid);
425
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200426 if (metadata_fsid) {
427 /*
428 * Handle scanned device having completed its fsid change but
429 * belonging to a fs_devices that was created by first scanning
430 * a device which didn't have its fsid/metadata_uuid changed
431 * at all and the CHANGING_FSID_V2 flag set.
432 */
433 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
434 if (fs_devices->fsid_change &&
435 memcmp(metadata_fsid, fs_devices->fsid,
436 BTRFS_FSID_SIZE) == 0 &&
437 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
438 BTRFS_FSID_SIZE) == 0) {
439 return fs_devices;
440 }
441 }
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200442 /*
443 * Handle scanned device having completed its fsid change but
444 * belonging to a fs_devices that was created by a device that
445 * has an outdated pair of fsid/metadata_uuid and
446 * CHANGING_FSID_V2 flag set.
447 */
448 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
449 if (fs_devices->fsid_change &&
450 memcmp(fs_devices->metadata_uuid,
451 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
452 memcmp(metadata_fsid, fs_devices->metadata_uuid,
453 BTRFS_FSID_SIZE) == 0) {
454 return fs_devices;
455 }
456 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200457 }
458
459 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800460 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200461 if (metadata_fsid) {
462 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
463 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
464 BTRFS_FSID_SIZE) == 0)
465 return fs_devices;
466 } else {
467 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
468 return fs_devices;
469 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400470 }
471 return NULL;
472}
473
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100474static int
475btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
476 int flush, struct block_device **bdev,
477 struct buffer_head **bh)
478{
479 int ret;
480
481 *bdev = blkdev_get_by_path(device_path, flags, holder);
482
483 if (IS_ERR(*bdev)) {
484 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100485 goto error;
486 }
487
488 if (flush)
489 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200490 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100491 if (ret) {
492 blkdev_put(*bdev, flags);
493 goto error;
494 }
495 invalidate_bdev(*bdev);
496 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800497 if (IS_ERR(*bh)) {
498 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100499 blkdev_put(*bdev, flags);
500 goto error;
501 }
502
503 return 0;
504
505error:
506 *bdev = NULL;
507 *bh = NULL;
508 return ret;
509}
510
Chris Masonffbd5172009-04-20 15:50:09 -0400511static void requeue_list(struct btrfs_pending_bios *pending_bios,
512 struct bio *head, struct bio *tail)
513{
514
515 struct bio *old_head;
516
517 old_head = pending_bios->head;
518 pending_bios->head = head;
519 if (pending_bios->tail)
520 tail->bi_next = old_head;
521 else
522 pending_bios->tail = tail;
523}
524
Chris Mason8b712842008-06-11 16:50:36 -0400525/*
526 * we try to collect pending bios for a device so we don't get a large
527 * number of procs sending bios down to the same device. This greatly
528 * improves the schedulers ability to collect and merge the bios.
529 *
530 * But, it also turns into a long list of bios to process and that is sure
531 * to eventually make the worker thread block. The solution here is to
532 * make some progress and then put this work struct back at the end of
533 * the list if the block device is congested. This way, multiple devices
534 * can make progress from a single worker thread.
535 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100536static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400537{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400538 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400539 struct bio *pending;
540 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400541 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400542 struct bio *tail;
543 struct bio *cur;
544 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400545 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400546 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400547 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400548 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000549 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400550 struct blk_plug plug;
551
552 /*
553 * this function runs all the bios we've collected for
554 * a particular device. We don't want to wander off to
555 * another device without first sending all of these down.
556 * So, setup a plug here and finish it off before we return
557 */
558 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400559
Jan Karaefa7c9f2017-02-02 15:56:53 +0100560 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400561
Chris Mason8b712842008-06-11 16:50:36 -0400562loop:
563 spin_lock(&device->io_lock);
564
Chris Masona6837052009-02-04 09:19:41 -0500565loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400566 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400567
Chris Mason8b712842008-06-11 16:50:36 -0400568 /* take all the bios off the list at once and process them
569 * later on (without the lock held). But, remember the
570 * tail and other pointers so the bios can be properly reinserted
571 * into the list if we hit congestion
572 */
Chris Masond84275c2009-06-09 15:39:08 -0400573 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400574 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400575 force_reg = 1;
576 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400577 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400578 force_reg = 0;
579 }
Chris Masonffbd5172009-04-20 15:50:09 -0400580
581 pending = pending_bios->head;
582 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400583 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400584
585 /*
586 * if pending was null this time around, no bios need processing
587 * at all and we can stop. Otherwise it'll loop back up again
588 * and do an additional check so no bios are missed.
589 *
590 * device->running_pending is used to synchronize with the
591 * schedule_bio code.
592 */
Chris Masonffbd5172009-04-20 15:50:09 -0400593 if (device->pending_sync_bios.head == NULL &&
594 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400595 again = 0;
596 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400597 } else {
598 again = 1;
599 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400600 }
Chris Masonffbd5172009-04-20 15:50:09 -0400601
602 pending_bios->head = NULL;
603 pending_bios->tail = NULL;
604
Chris Mason8b712842008-06-11 16:50:36 -0400605 spin_unlock(&device->io_lock);
606
Chris Masond3977122009-01-05 21:25:51 -0500607 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400608
609 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400610 /* we want to work on both lists, but do more bios on the
611 * sync list than the regular list
612 */
613 if ((num_run > 32 &&
614 pending_bios != &device->pending_sync_bios &&
615 device->pending_sync_bios.head) ||
616 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
617 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400618 spin_lock(&device->io_lock);
619 requeue_list(pending_bios, pending, tail);
620 goto loop_lock;
621 }
622
Chris Mason8b712842008-06-11 16:50:36 -0400623 cur = pending;
624 pending = pending->bi_next;
625 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400626
Jens Axboedac56212015-04-17 16:23:59 -0600627 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400628
Chris Mason2ab1ba62011-08-04 14:28:36 -0400629 /*
630 * if we're doing the sync list, record that our
631 * plug has some sync requests on it
632 *
633 * If we're doing the regular list and there are
634 * sync requests sitting around, unplug before
635 * we add more
636 */
637 if (pending_bios == &device->pending_sync_bios) {
638 sync_pending = 1;
639 } else if (sync_pending) {
640 blk_finish_plug(&plug);
641 blk_start_plug(&plug);
642 sync_pending = 0;
643 }
644
Mike Christie4e49ea42016-06-05 14:31:41 -0500645 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400646 num_run++;
647 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100648
649 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400650
651 /*
652 * we made progress, there is more work to do and the bdi
653 * is now congested. Back off and let other work structs
654 * run instead
655 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400656 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500657 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400658 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400659
Chris Masonb765ead2009-04-03 10:27:10 -0400660 ioc = current->io_context;
661
662 /*
663 * the main goal here is that we don't want to
664 * block if we're going to be able to submit
665 * more requests without blocking.
666 *
667 * This code does two great things, it pokes into
668 * the elevator code from a filesystem _and_
669 * it makes assumptions about how batching works.
670 */
671 if (ioc && ioc->nr_batch_requests > 0 &&
672 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
673 (last_waited == 0 ||
674 ioc->last_waited == last_waited)) {
675 /*
676 * we want to go through our batch of
677 * requests and stop. So, we copy out
678 * the ioc->last_waited time and test
679 * against it before looping
680 */
681 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100682 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400683 continue;
684 }
Chris Mason8b712842008-06-11 16:50:36 -0400685 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400686 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500687 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400688
689 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800690 btrfs_queue_work(fs_info->submit_workers,
691 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400692 goto done;
693 }
694 }
Chris Masonffbd5172009-04-20 15:50:09 -0400695
Chris Mason51684082010-03-10 15:33:32 -0500696 cond_resched();
697 if (again)
698 goto loop;
699
700 spin_lock(&device->io_lock);
701 if (device->pending_bios.head || device->pending_sync_bios.head)
702 goto loop_lock;
703 spin_unlock(&device->io_lock);
704
Chris Mason8b712842008-06-11 16:50:36 -0400705done:
Chris Mason211588a2011-04-19 20:12:40 -0400706 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400707}
708
Christoph Hellwigb2950862008-12-02 09:54:17 -0500709static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400710{
711 struct btrfs_device *device;
712
713 device = container_of(work, struct btrfs_device, work);
714 run_scheduled_bios(device);
715}
716
Anand Jain70bc7082019-01-04 13:31:53 +0800717static bool device_path_matched(const char *path, struct btrfs_device *device)
718{
719 int found;
720
721 rcu_read_lock();
722 found = strcmp(rcu_str_deref(device->name), path);
723 rcu_read_unlock();
724
725 return found == 0;
726}
727
Anand Jaind8367db2018-01-18 22:00:37 +0800728/*
729 * Search and remove all stale (devices which are not mounted) devices.
730 * When both inputs are NULL, it will search and release all stale devices.
731 * path: Optional. When provided will it release all unmounted devices
732 * matching this path only.
733 * skip_dev: Optional. Will skip this device when searching for the stale
734 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800735 * Return: 0 for success or if @path is NULL.
736 * -EBUSY if @path is a mounted device.
737 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800738 */
Anand Jain70bc7082019-01-04 13:31:53 +0800739static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800740 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800741{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800742 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
743 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800744 int ret = 0;
745
746 if (path)
747 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800748
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800749 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800750
Anand Jain70bc7082019-01-04 13:31:53 +0800751 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800752 list_for_each_entry_safe(device, tmp_device,
753 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800754 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800755 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800756 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800757 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800758 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800759 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800760 if (fs_devices->opened) {
761 /* for an already deleted device return 0 */
762 if (path && ret != 0)
763 ret = -EBUSY;
764 break;
765 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800766
Anand Jain4fde46f2015-06-17 21:10:48 +0800767 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800768 fs_devices->num_devices--;
769 list_del(&device->dev_list);
770 btrfs_free_device(device);
771
Anand Jain70bc7082019-01-04 13:31:53 +0800772 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800773 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200774 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800775 }
776 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800777
Anand Jain7bcb8162018-05-29 17:23:20 +0800778 if (fs_devices->num_devices == 0) {
779 btrfs_sysfs_remove_fsid(fs_devices);
780 list_del(&fs_devices->fs_list);
781 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800782 }
783 }
Anand Jain70bc7082019-01-04 13:31:53 +0800784
785 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800786}
787
Anand Jain0fb08bc2017-11-09 23:45:24 +0800788static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
789 struct btrfs_device *device, fmode_t flags,
790 void *holder)
791{
792 struct request_queue *q;
793 struct block_device *bdev;
794 struct buffer_head *bh;
795 struct btrfs_super_block *disk_super;
796 u64 devid;
797 int ret;
798
799 if (device->bdev)
800 return -EINVAL;
801 if (!device->name)
802 return -EINVAL;
803
804 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
805 &bdev, &bh);
806 if (ret)
807 return ret;
808
809 disk_super = (struct btrfs_super_block *)bh->b_data;
810 devid = btrfs_stack_device_id(&disk_super->dev_item);
811 if (devid != device->devid)
812 goto error_brelse;
813
814 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
815 goto error_brelse;
816
817 device->generation = btrfs_super_generation(disk_super);
818
819 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200820 if (btrfs_super_incompat_flags(disk_super) &
821 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
822 pr_err(
823 "BTRFS: Invalid seeding and uuid-changed device detected\n");
824 goto error_brelse;
825 }
826
Anand Jainebbede42017-12-04 12:54:52 +0800827 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800828 fs_devices->seeding = 1;
829 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800830 if (bdev_read_only(bdev))
831 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
832 else
833 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800834 }
835
836 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800837 if (!blk_queue_nonrot(q))
838 fs_devices->rotating = 1;
839
840 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800841 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800842 device->mode = flags;
843
844 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800845 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
846 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800847 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800848 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800849 }
850 brelse(bh);
851
852 return 0;
853
854error_brelse:
855 brelse(bh);
856 blkdev_put(bdev, flags);
857
858 return -EINVAL;
859}
860
David Sterba60999ca2014-03-26 18:26:36 +0100861/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200862 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
863 * being created with a disk that has already completed its fsid change.
864 */
865static struct btrfs_fs_devices *find_fsid_inprogress(
866 struct btrfs_super_block *disk_super)
867{
868 struct btrfs_fs_devices *fs_devices;
869
870 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
871 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
872 BTRFS_FSID_SIZE) != 0 &&
873 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
874 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
875 return fs_devices;
876 }
877 }
878
879 return NULL;
880}
881
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200882
883static struct btrfs_fs_devices *find_fsid_changed(
884 struct btrfs_super_block *disk_super)
885{
886 struct btrfs_fs_devices *fs_devices;
887
888 /*
889 * Handles the case where scanned device is part of an fs that had
890 * multiple successful changes of FSID but curently device didn't
891 * observe it. Meaning our fsid will be different than theirs.
892 */
893 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
894 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
895 BTRFS_FSID_SIZE) != 0 &&
896 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
897 BTRFS_FSID_SIZE) == 0 &&
898 memcmp(fs_devices->fsid, disk_super->fsid,
899 BTRFS_FSID_SIZE) != 0) {
900 return fs_devices;
901 }
902 }
903
904 return NULL;
905}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200906/*
David Sterba60999ca2014-03-26 18:26:36 +0100907 * Add new device to list of registered devices
908 *
909 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800910 * device pointer which was just added or updated when successful
911 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100912 */
Anand Jaine124ece2018-01-18 22:02:35 +0800913static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800914 struct btrfs_super_block *disk_super,
915 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400916{
917 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200918 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400919 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400920 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800921 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200922 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
923 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200924 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
925 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400926
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200927 if (fsid_change_in_progress) {
928 if (!has_metadata_uuid) {
929 /*
930 * When we have an image which has CHANGING_FSID_V2 set
931 * it might belong to either a filesystem which has
932 * disks with completed fsid change or it might belong
933 * to fs with no UUID changes in effect, handle both.
934 */
935 fs_devices = find_fsid_inprogress(disk_super);
936 if (!fs_devices)
937 fs_devices = find_fsid(disk_super->fsid, NULL);
938 } else {
939 fs_devices = find_fsid_changed(disk_super);
940 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200941 } else if (has_metadata_uuid) {
942 fs_devices = find_fsid(disk_super->fsid,
943 disk_super->metadata_uuid);
944 } else {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200945 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200946 }
947
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200948
Chris Mason8a4b83c2008-03-24 15:02:07 -0400949 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200950 if (has_metadata_uuid)
951 fs_devices = alloc_fs_devices(disk_super->fsid,
952 disk_super->metadata_uuid);
953 else
954 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
955
Ilya Dryomov2208a372013-08-12 14:33:03 +0300956 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800957 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300958
Al Viro92900e52019-01-27 04:58:00 +0000959 fs_devices->fsid_change = fsid_change_in_progress;
960
Anand Jain9c6d1732018-05-29 14:10:20 +0800961 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800962 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300963
Chris Mason8a4b83c2008-03-24 15:02:07 -0400964 device = NULL;
965 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800966 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800967 device = btrfs_find_device(fs_devices, devid,
968 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200969
970 /*
971 * If this disk has been pulled into an fs devices created by
972 * a device which had the CHANGING_FSID_V2 flag then replace the
973 * metadata_uuid/fsid values of the fs_devices.
974 */
975 if (has_metadata_uuid && fs_devices->fsid_change &&
976 found_transid > fs_devices->latest_generation) {
977 memcpy(fs_devices->fsid, disk_super->fsid,
978 BTRFS_FSID_SIZE);
979 memcpy(fs_devices->metadata_uuid,
980 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
981
982 fs_devices->fsid_change = false;
983 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400984 }
Miao Xie443f24f2014-07-24 11:37:15 +0800985
Chris Mason8a4b83c2008-03-24 15:02:07 -0400986 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800987 if (fs_devices->opened) {
988 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800989 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800990 }
Yan Zheng2b820322008-11-17 21:11:30 -0500991
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300992 device = btrfs_alloc_device(NULL, &devid,
993 disk_super->dev_item.uuid);
994 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800995 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400996 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800997 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400998 }
Josef Bacik606686e2012-06-04 14:03:51 -0400999
1000 name = rcu_string_strdup(path, GFP_NOFS);
1001 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001002 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +08001003 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001004 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001005 }
Josef Bacik606686e2012-06-04 14:03:51 -04001006 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +02001007
Xiao Guangrong1f781602011-04-20 10:09:16 +00001008 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001009 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -04001010
Yan Zheng2b820322008-11-17 21:11:30 -05001011 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001012 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +08001013
1014 if (disk_super->label[0])
1015 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1016 disk_super->label, devid, found_transid, path);
1017 else
1018 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1019 disk_super->fsid, devid, found_transid, path);
1020
Josef Bacik606686e2012-06-04 14:03:51 -04001021 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +08001022 /*
1023 * When FS is already mounted.
1024 * 1. If you are here and if the device->name is NULL that
1025 * means this device was missing at time of FS mount.
1026 * 2. If you are here and if the device->name is different
1027 * from 'path' that means either
1028 * a. The same device disappeared and reappeared with
1029 * different name. or
1030 * b. The missing-disk-which-was-replaced, has
1031 * reappeared now.
1032 *
1033 * We must allow 1 and 2a above. But 2b would be a spurious
1034 * and unintentional.
1035 *
1036 * Further in case of 1 and 2a above, the disk at 'path'
1037 * would have missed some transaction when it was away and
1038 * in case of 2a the stale bdev has to be updated as well.
1039 * 2b must not be allowed at all time.
1040 */
1041
1042 /*
Chris Mason0f23ae72014-09-18 07:49:05 -07001043 * For now, we do allow update to btrfs_fs_device through the
1044 * btrfs dev scan cli after FS has been mounted. We're still
1045 * tracking a problem where systems fail mount by subvolume id
1046 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +08001047 */
Chris Mason0f23ae72014-09-18 07:49:05 -07001048 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +08001049 /*
1050 * That is if the FS is _not_ mounted and if you
1051 * are here, that means there is more than one
1052 * disk with same uuid and devid.We keep the one
1053 * with larger generation number or the last-in if
1054 * generation are equal.
1055 */
Anand Jain9c6d1732018-05-29 14:10:20 +08001056 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001057 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +08001058 }
Anand Jainb96de002014-07-03 18:22:05 +08001059
Anand Jaina9261d42018-10-15 10:45:17 +08001060 /*
1061 * We are going to replace the device path for a given devid,
1062 * make sure it's the same device if the device is mounted
1063 */
1064 if (device->bdev) {
1065 struct block_device *path_bdev;
1066
1067 path_bdev = lookup_bdev(path);
1068 if (IS_ERR(path_bdev)) {
1069 mutex_unlock(&fs_devices->device_list_mutex);
1070 return ERR_CAST(path_bdev);
1071 }
1072
1073 if (device->bdev != path_bdev) {
1074 bdput(path_bdev);
1075 mutex_unlock(&fs_devices->device_list_mutex);
1076 btrfs_warn_in_rcu(device->fs_info,
1077 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1078 disk_super->fsid, devid,
1079 rcu_str_deref(device->name), path);
1080 return ERR_PTR(-EEXIST);
1081 }
1082 bdput(path_bdev);
1083 btrfs_info_in_rcu(device->fs_info,
1084 "device fsid %pU devid %llu moved old:%s new:%s",
1085 disk_super->fsid, devid,
1086 rcu_str_deref(device->name), path);
1087 }
1088
Josef Bacik606686e2012-06-04 14:03:51 -04001089 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +08001090 if (!name) {
1091 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001092 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +08001093 }
Josef Bacik606686e2012-06-04 14:03:51 -04001094 rcu_string_free(device->name);
1095 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +08001096 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05001097 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08001098 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05001099 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001100 }
1101
Anand Jain77bdae42014-07-03 18:22:06 +08001102 /*
1103 * Unmount does not free the btrfs_device struct but would zero
1104 * generation along with most of the other members. So just update
1105 * it back. We need it to pick the disk with largest generation
1106 * (as above).
1107 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001108 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +08001109 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001110 fs_devices->latest_generation = max_t(u64, found_transid,
1111 fs_devices->latest_generation);
1112 }
Anand Jain77bdae42014-07-03 18:22:06 +08001113
Anand Jainf2788d22018-01-18 22:02:34 +08001114 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1115
Anand Jain9c6d1732018-05-29 14:10:20 +08001116 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001117 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001118}
1119
Yan Zhenge4404d62008-12-12 10:03:26 -05001120static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1121{
1122 struct btrfs_fs_devices *fs_devices;
1123 struct btrfs_device *device;
1124 struct btrfs_device *orig_dev;
1125
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001126 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001127 if (IS_ERR(fs_devices))
1128 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001129
Miao Xieadbbb862014-09-03 21:35:42 +08001130 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001131 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001132
1133 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001134 struct rcu_string *name;
1135
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001136 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1137 orig_dev->uuid);
1138 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -05001139 goto error;
1140
Josef Bacik606686e2012-06-04 14:03:51 -04001141 /*
1142 * This is ok to do without rcu read locked because we hold the
1143 * uuid mutex so nothing we touch in here is going to disappear.
1144 */
Anand Jaine755f782014-06-30 17:12:47 +08001145 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001146 name = rcu_string_strdup(orig_dev->name->str,
1147 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001148 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001149 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +08001150 goto error;
1151 }
1152 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001153 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001154
Yan Zhenge4404d62008-12-12 10:03:26 -05001155 list_add(&device->dev_list, &fs_devices->devices);
1156 device->fs_devices = fs_devices;
1157 fs_devices->num_devices++;
1158 }
Miao Xieadbbb862014-09-03 21:35:42 +08001159 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001160 return fs_devices;
1161error:
Miao Xieadbbb862014-09-03 21:35:42 +08001162 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001163 free_fs_devices(fs_devices);
1164 return ERR_PTR(-ENOMEM);
1165}
1166
Anand Jain9b99b112018-02-27 12:41:59 +08001167/*
1168 * After we have read the system tree and know devids belonging to
1169 * this filesystem, remove the device which does not belong there.
1170 */
1171void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001172{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001173 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001174 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001175
Chris Masondfe25022008-05-13 13:46:40 -04001176 mutex_lock(&uuid_mutex);
1177again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001178 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001179 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001180 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1181 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001182 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1183 &device->dev_state) &&
1184 (!latest_dev ||
1185 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001186 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001187 }
Yan Zheng2b820322008-11-17 21:11:30 -05001188 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001189 }
Yan Zheng2b820322008-11-17 21:11:30 -05001190
Stefan Behrens8dabb742012-11-06 13:15:27 +01001191 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1192 /*
1193 * In the first step, keep the device which has
1194 * the correct fsid and the devid that is used
1195 * for the dev_replace procedure.
1196 * In the second step, the dev_replace state is
1197 * read from the device tree and it is known
1198 * whether the procedure is really active or
1199 * not, which means whether this device is
1200 * used or whether it should be removed.
1201 */
Anand Jain401e29c2017-12-04 12:54:55 +08001202 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1203 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001204 continue;
1205 }
1206 }
Yan Zheng2b820322008-11-17 21:11:30 -05001207 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001208 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001209 device->bdev = NULL;
1210 fs_devices->open_devices--;
1211 }
Anand Jainebbede42017-12-04 12:54:52 +08001212 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001213 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001214 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001215 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1216 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001217 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001218 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001219 list_del_init(&device->dev_list);
1220 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001221 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001222 }
Yan Zheng2b820322008-11-17 21:11:30 -05001223
1224 if (fs_devices->seed) {
1225 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001226 goto again;
1227 }
1228
Miao Xie443f24f2014-07-24 11:37:15 +08001229 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001230
Chris Masondfe25022008-05-13 13:46:40 -04001231 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001232}
Chris Masona0af4692008-05-13 16:03:06 -04001233
David Sterbaf06c5962017-06-06 17:08:23 +02001234static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +00001235{
1236 struct btrfs_device *device;
1237
1238 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +01001239 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001240}
1241
Anand Jain14238812016-07-22 06:04:53 +08001242static void btrfs_close_bdev(struct btrfs_device *device)
1243{
David Sterba08ffcae2017-06-19 16:55:35 +02001244 if (!device->bdev)
1245 return;
1246
Anand Jainebbede42017-12-04 12:54:52 +08001247 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001248 sync_blockdev(device->bdev);
1249 invalidate_bdev(device->bdev);
1250 }
1251
David Sterba08ffcae2017-06-19 16:55:35 +02001252 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001253}
1254
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001255static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001256{
1257 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1258 struct btrfs_device *new_device;
1259 struct rcu_string *name;
1260
1261 if (device->bdev)
1262 fs_devices->open_devices--;
1263
Anand Jainebbede42017-12-04 12:54:52 +08001264 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001265 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1266 list_del_init(&device->dev_alloc_list);
1267 fs_devices->rw_devices--;
1268 }
1269
Anand Jaine6e674b2017-12-04 12:54:54 +08001270 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001271 fs_devices->missing_devices--;
1272
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001273 btrfs_close_bdev(device);
1274
Anand Jainf448341a2016-06-14 18:55:25 +08001275 new_device = btrfs_alloc_device(NULL, &device->devid,
1276 device->uuid);
1277 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1278
1279 /* Safe because we are under uuid_mutex */
1280 if (device->name) {
1281 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1282 BUG_ON(!name); /* -ENOMEM */
1283 rcu_assign_pointer(new_device->name, name);
1284 }
1285
1286 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1287 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001288
1289 call_rcu(&device->rcu, free_device_rcu);
Anand Jainf448341a2016-06-14 18:55:25 +08001290}
1291
Anand Jain0226e0e2018-04-12 10:29:27 +08001292static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001293{
Sasha Levin2037a092015-05-12 19:31:37 -04001294 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001295
Yan Zheng2b820322008-11-17 21:11:30 -05001296 if (--fs_devices->opened > 0)
1297 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001298
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001299 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001300 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001301 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001302 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001303 mutex_unlock(&fs_devices->device_list_mutex);
1304
Yan Zhenge4404d62008-12-12 10:03:26 -05001305 WARN_ON(fs_devices->open_devices);
1306 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001307 fs_devices->opened = 0;
1308 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001309
Chris Mason8a4b83c2008-03-24 15:02:07 -04001310 return 0;
1311}
1312
Yan Zheng2b820322008-11-17 21:11:30 -05001313int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1314{
Yan Zhenge4404d62008-12-12 10:03:26 -05001315 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001316 int ret;
1317
1318 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001319 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001320 if (!fs_devices->opened) {
1321 seed_devices = fs_devices->seed;
1322 fs_devices->seed = NULL;
1323 }
Yan Zheng2b820322008-11-17 21:11:30 -05001324 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001325
1326 while (seed_devices) {
1327 fs_devices = seed_devices;
1328 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001329 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001330 free_fs_devices(fs_devices);
1331 }
Yan Zheng2b820322008-11-17 21:11:30 -05001332 return ret;
1333}
1334
Anand Jain897fb572018-04-12 10:29:28 +08001335static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001336 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001337{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001338 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001339 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001340 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001341
Tejun Heod4d77622010-11-13 11:55:18 +01001342 flags |= FMODE_EXCL;
1343
Anand Jainf117e292018-04-12 10:29:26 +08001344 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001345 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001346 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001347 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001348
Anand Jain9f050db2017-11-09 23:45:25 +08001349 if (!latest_dev ||
1350 device->generation > latest_dev->generation)
1351 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001352 }
Chris Masona0af4692008-05-13 16:03:06 -04001353 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001354 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001355 goto out;
1356 }
Yan Zheng2b820322008-11-17 21:11:30 -05001357 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001358 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001359 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001360out:
Yan Zheng2b820322008-11-17 21:11:30 -05001361 return ret;
1362}
1363
Anand Jainf8e10cd2018-01-22 14:49:36 -08001364static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1365{
1366 struct btrfs_device *dev1, *dev2;
1367
1368 dev1 = list_entry(a, struct btrfs_device, dev_list);
1369 dev2 = list_entry(b, struct btrfs_device, dev_list);
1370
1371 if (dev1->devid < dev2->devid)
1372 return -1;
1373 else if (dev1->devid > dev2->devid)
1374 return 1;
1375 return 0;
1376}
1377
Yan Zheng2b820322008-11-17 21:11:30 -05001378int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001379 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001380{
1381 int ret;
1382
David Sterbaf5194e32018-06-19 17:09:47 +02001383 lockdep_assert_held(&uuid_mutex);
1384
Anand Jain542c5902018-04-12 10:29:34 +08001385 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001386 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001387 fs_devices->opened++;
1388 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001389 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001390 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001391 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001392 }
Anand Jain542c5902018-04-12 10:29:34 +08001393 mutex_unlock(&fs_devices->device_list_mutex);
1394
Chris Mason8a4b83c2008-03-24 15:02:07 -04001395 return ret;
1396}
1397
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001398static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001399{
1400 kunmap(page);
1401 put_page(page);
1402}
1403
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001404static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1405 struct page **page,
1406 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001407{
1408 void *p;
1409 pgoff_t index;
1410
1411 /* make sure our super fits in the device */
1412 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1413 return 1;
1414
1415 /* make sure our super fits in the page */
1416 if (sizeof(**disk_super) > PAGE_SIZE)
1417 return 1;
1418
1419 /* make sure our super doesn't straddle pages on disk */
1420 index = bytenr >> PAGE_SHIFT;
1421 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1422 return 1;
1423
1424 /* pull in the page with our super */
1425 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1426 index, GFP_KERNEL);
1427
1428 if (IS_ERR_OR_NULL(*page))
1429 return 1;
1430
1431 p = kmap(*page);
1432
1433 /* align our pointer to the offset of the super block */
Johannes Thumshirn70730172018-12-05 15:23:03 +01001434 *disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001435
1436 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1437 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1438 btrfs_release_disk_super(*page);
1439 return 1;
1440 }
1441
1442 if ((*disk_super)->label[0] &&
1443 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1444 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1445
1446 return 0;
1447}
1448
Anand Jain228a73a2019-01-04 13:31:54 +08001449int btrfs_forget_devices(const char *path)
1450{
1451 int ret;
1452
1453 mutex_lock(&uuid_mutex);
1454 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1455 mutex_unlock(&uuid_mutex);
1456
1457 return ret;
1458}
1459
David Sterba6f60cbd2013-02-15 11:31:02 -07001460/*
1461 * Look for a btrfs signature on a device. This may be called out of the mount path
1462 * and we are not allowed to call set_blocksize during the scan. The superblock
1463 * is read via pagecache
1464 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001465struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1466 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001467{
1468 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001469 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001470 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001471 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001472 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001473 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001474
David Sterba899f9302018-06-19 16:37:36 +02001475 lockdep_assert_held(&uuid_mutex);
1476
David Sterba6f60cbd2013-02-15 11:31:02 -07001477 /*
1478 * we would like to check all the supers, but that would make
1479 * a btrfs mount succeed after a mkfs from a different FS.
1480 * So, we need to add a special mount option to scan for
1481 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1482 */
1483 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001484 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001485
1486 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001487 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001488 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001489
Anand Jain05a5c552017-12-15 15:40:16 +08001490 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001491 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001492 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001493 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001494
Anand Jain4306a972018-05-29 12:28:37 +08001495 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001496 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001497 if (new_device_added)
1498 btrfs_free_stale_devices(path, device);
1499 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001500
Anand Jain6cf86a002016-02-13 10:01:29 +08001501 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001502
1503error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001504 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001505
Gu Jinxiang36350e92018-07-12 14:23:16 +08001506 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001507}
Chris Mason0b86a832008-03-24 15:01:56 -04001508
Jeff Mahoney499f3772015-06-15 09:41:17 -04001509static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001510 struct btrfs_device *device,
1511 u64 *start, u64 len)
1512{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001513 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001514 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001515 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001516 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001517 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001518
Jeff Mahoney499f3772015-06-15 09:41:17 -04001519 if (transaction)
1520 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001521again:
1522 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001523 struct map_lookup *map;
1524 int i;
1525
Jeff Mahoney95617d62015-06-03 10:55:48 -04001526 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001527 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001528 u64 end;
1529
Josef Bacik6df9a952013-06-27 13:22:46 -04001530 if (map->stripes[i].dev != device)
1531 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001532 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001533 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001534 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001535 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001536 /*
1537 * Make sure that while processing the pinned list we do
1538 * not override our *start with a lower value, because
1539 * we can have pinned chunks that fall within this
1540 * device hole and that have lower physical addresses
1541 * than the pending chunks we processed before. If we
1542 * do not take this special care we can end up getting
1543 * 2 pending chunks that start at the same physical
1544 * device offsets because the end offset of a pinned
1545 * chunk can be equal to the start offset of some
1546 * pending chunk.
1547 */
1548 end = map->stripes[i].physical + em->orig_block_len;
1549 if (end > *start) {
1550 *start = end;
1551 ret = 1;
1552 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001553 }
1554 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001555 if (search_list != &fs_info->pinned_chunks) {
1556 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001557 goto again;
1558 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001559
1560 return ret;
1561}
1562
1563
Chris Mason0b86a832008-03-24 15:01:56 -04001564/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001565 * find_free_dev_extent_start - find free space in the specified device
1566 * @device: the device which we search the free space in
1567 * @num_bytes: the size of the free space that we need
1568 * @search_start: the position from which to begin the search
1569 * @start: store the start of the free space.
1570 * @len: the size of the free space. that we find, or the size
1571 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001572 *
Chris Mason0b86a832008-03-24 15:01:56 -04001573 * this uses a pretty simple search, the expectation is that it is
1574 * called very infrequently and that a given device has a small number
1575 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001576 *
1577 * @start is used to store the start of the free space if we find. But if we
1578 * don't find suitable free space, it will be used to store the start position
1579 * of the max free space.
1580 *
1581 * @len is used to store the size of the free space that we find.
1582 * But if we don't find suitable free space, it is used to store the size of
1583 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001584 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001585int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1586 struct btrfs_device *device, u64 num_bytes,
1587 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001589 struct btrfs_fs_info *fs_info = device->fs_info;
1590 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001591 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001592 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001593 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001594 u64 hole_size;
1595 u64 max_hole_start;
1596 u64 max_hole_size;
1597 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001598 u64 search_end = device->total_bytes;
1599 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001600 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001601 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001602
1603 /*
1604 * We don't want to overwrite the superblock on the drive nor any area
1605 * used by the boot loader (grub for example), so we make sure to start
1606 * at an offset of at least 1MB.
1607 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001608 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001609
Josef Bacik6df9a952013-06-27 13:22:46 -04001610 path = btrfs_alloc_path();
1611 if (!path)
1612 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001613
Miao Xie7bfc8372011-01-05 10:07:26 +00001614 max_hole_start = search_start;
1615 max_hole_size = 0;
1616
Zhao Leif2ab7612015-02-16 18:52:17 +08001617again:
Anand Jain401e29c2017-12-04 12:54:55 +08001618 if (search_start >= search_end ||
1619 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001620 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001621 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001622 }
1623
David Sterbae4058b52015-11-27 16:31:35 +01001624 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001625 path->search_commit_root = 1;
1626 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001627
Chris Mason0b86a832008-03-24 15:01:56 -04001628 key.objectid = device->devid;
1629 key.offset = search_start;
1630 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001631
Li Zefan125ccb02011-12-08 15:07:24 +08001632 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001633 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001634 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001635 if (ret > 0) {
1636 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1637 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001638 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001639 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001640
Chris Mason0b86a832008-03-24 15:01:56 -04001641 while (1) {
1642 l = path->nodes[0];
1643 slot = path->slots[0];
1644 if (slot >= btrfs_header_nritems(l)) {
1645 ret = btrfs_next_leaf(root, path);
1646 if (ret == 0)
1647 continue;
1648 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001649 goto out;
1650
1651 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001652 }
1653 btrfs_item_key_to_cpu(l, &key, slot);
1654
1655 if (key.objectid < device->devid)
1656 goto next;
1657
1658 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001659 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001660
David Sterba962a2982014-06-04 18:41:45 +02001661 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001662 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001663
Miao Xie7bfc8372011-01-05 10:07:26 +00001664 if (key.offset > search_start) {
1665 hole_size = key.offset - search_start;
1666
Josef Bacik6df9a952013-06-27 13:22:46 -04001667 /*
1668 * Have to check before we set max_hole_start, otherwise
1669 * we could end up sending back this offset anyway.
1670 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001671 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001672 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001673 hole_size)) {
1674 if (key.offset >= search_start) {
1675 hole_size = key.offset - search_start;
1676 } else {
1677 WARN_ON_ONCE(1);
1678 hole_size = 0;
1679 }
1680 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001681
Miao Xie7bfc8372011-01-05 10:07:26 +00001682 if (hole_size > max_hole_size) {
1683 max_hole_start = search_start;
1684 max_hole_size = hole_size;
1685 }
1686
1687 /*
1688 * If this free space is greater than which we need,
1689 * it must be the max free space that we have found
1690 * until now, so max_hole_start must point to the start
1691 * of this free space and the length of this free space
1692 * is stored in max_hole_size. Thus, we return
1693 * max_hole_start and max_hole_size and go back to the
1694 * caller.
1695 */
1696 if (hole_size >= num_bytes) {
1697 ret = 0;
1698 goto out;
1699 }
1700 }
1701
Chris Mason0b86a832008-03-24 15:01:56 -04001702 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001703 extent_end = key.offset + btrfs_dev_extent_length(l,
1704 dev_extent);
1705 if (extent_end > search_start)
1706 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001707next:
1708 path->slots[0]++;
1709 cond_resched();
1710 }
Chris Mason0b86a832008-03-24 15:01:56 -04001711
liubo38c01b92011-08-02 02:39:03 +00001712 /*
1713 * At this point, search_start should be the end of
1714 * allocated dev extents, and when shrinking the device,
1715 * search_end may be smaller than search_start.
1716 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001717 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001718 hole_size = search_end - search_start;
1719
Jeff Mahoney499f3772015-06-15 09:41:17 -04001720 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001721 hole_size)) {
1722 btrfs_release_path(path);
1723 goto again;
1724 }
Chris Mason0b86a832008-03-24 15:01:56 -04001725
Zhao Leif2ab7612015-02-16 18:52:17 +08001726 if (hole_size > max_hole_size) {
1727 max_hole_start = search_start;
1728 max_hole_size = hole_size;
1729 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001730 }
1731
Miao Xie7bfc8372011-01-05 10:07:26 +00001732 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001733 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001734 ret = -ENOSPC;
1735 else
1736 ret = 0;
1737
1738out:
Yan Zheng2b820322008-11-17 21:11:30 -05001739 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001740 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001741 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001742 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001743 return ret;
1744}
1745
Jeff Mahoney499f3772015-06-15 09:41:17 -04001746int find_free_dev_extent(struct btrfs_trans_handle *trans,
1747 struct btrfs_device *device, u64 num_bytes,
1748 u64 *start, u64 *len)
1749{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001750 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001751 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001752 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001753}
1754
Christoph Hellwigb2950862008-12-02 09:54:17 -05001755static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001756 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001757 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001759 struct btrfs_fs_info *fs_info = device->fs_info;
1760 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001761 int ret;
1762 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001763 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001764 struct btrfs_key found_key;
1765 struct extent_buffer *leaf = NULL;
1766 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001767
1768 path = btrfs_alloc_path();
1769 if (!path)
1770 return -ENOMEM;
1771
1772 key.objectid = device->devid;
1773 key.offset = start;
1774 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001775again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001776 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001777 if (ret > 0) {
1778 ret = btrfs_previous_item(root, path, key.objectid,
1779 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001780 if (ret)
1781 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001782 leaf = path->nodes[0];
1783 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1784 extent = btrfs_item_ptr(leaf, path->slots[0],
1785 struct btrfs_dev_extent);
1786 BUG_ON(found_key.offset > start || found_key.offset +
1787 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001788 key = found_key;
1789 btrfs_release_path(path);
1790 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001791 } else if (ret == 0) {
1792 leaf = path->nodes[0];
1793 extent = btrfs_item_ptr(leaf, path->slots[0],
1794 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001795 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001796 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001797 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001798 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001799
Miao Xie2196d6e2014-09-03 21:35:41 +08001800 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1801
Chris Mason8f18cf12008-04-25 16:53:30 -04001802 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001803 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001804 btrfs_handle_fs_error(fs_info, ret,
1805 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001806 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001807 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001808 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001809out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001810 btrfs_free_path(path);
1811 return ret;
1812}
1813
Eric Sandeen48a3b632013-04-25 20:41:01 +00001814static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1815 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001816 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001817{
1818 int ret;
1819 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001820 struct btrfs_fs_info *fs_info = device->fs_info;
1821 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001822 struct btrfs_dev_extent *extent;
1823 struct extent_buffer *leaf;
1824 struct btrfs_key key;
1825
Anand Jaine12c9622017-12-04 12:54:53 +08001826 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001827 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001828 path = btrfs_alloc_path();
1829 if (!path)
1830 return -ENOMEM;
1831
Chris Mason0b86a832008-03-24 15:01:56 -04001832 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001833 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001834 key.type = BTRFS_DEV_EXTENT_KEY;
1835 ret = btrfs_insert_empty_item(trans, root, path, &key,
1836 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001837 if (ret)
1838 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001839
1840 leaf = path->nodes[0];
1841 extent = btrfs_item_ptr(leaf, path->slots[0],
1842 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001843 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1844 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001845 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1846 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001847 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1848
Chris Mason0b86a832008-03-24 15:01:56 -04001849 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1850 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001851out:
Chris Mason0b86a832008-03-24 15:01:56 -04001852 btrfs_free_path(path);
1853 return ret;
1854}
1855
Josef Bacik6df9a952013-06-27 13:22:46 -04001856static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001857{
Josef Bacik6df9a952013-06-27 13:22:46 -04001858 struct extent_map_tree *em_tree;
1859 struct extent_map *em;
1860 struct rb_node *n;
1861 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001862
Josef Bacik6df9a952013-06-27 13:22:46 -04001863 em_tree = &fs_info->mapping_tree.map_tree;
1864 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001865 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001866 if (n) {
1867 em = rb_entry(n, struct extent_map, rb_node);
1868 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001869 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001870 read_unlock(&em_tree->lock);
1871
Chris Mason0b86a832008-03-24 15:01:56 -04001872 return ret;
1873}
1874
Ilya Dryomov53f10652013-08-12 14:33:01 +03001875static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1876 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001877{
1878 int ret;
1879 struct btrfs_key key;
1880 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001881 struct btrfs_path *path;
1882
Yan Zheng2b820322008-11-17 21:11:30 -05001883 path = btrfs_alloc_path();
1884 if (!path)
1885 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001886
1887 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1888 key.type = BTRFS_DEV_ITEM_KEY;
1889 key.offset = (u64)-1;
1890
Ilya Dryomov53f10652013-08-12 14:33:01 +03001891 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001892 if (ret < 0)
1893 goto error;
1894
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001895 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001896
Ilya Dryomov53f10652013-08-12 14:33:01 +03001897 ret = btrfs_previous_item(fs_info->chunk_root, path,
1898 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001899 BTRFS_DEV_ITEM_KEY);
1900 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001901 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001902 } else {
1903 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1904 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001905 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001906 }
1907 ret = 0;
1908error:
Yan Zheng2b820322008-11-17 21:11:30 -05001909 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001910 return ret;
1911}
1912
1913/*
1914 * the device information is stored in the chunk root
1915 * the btrfs_device struct should be fully filled in
1916 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001917static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001918 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001919{
1920 int ret;
1921 struct btrfs_path *path;
1922 struct btrfs_dev_item *dev_item;
1923 struct extent_buffer *leaf;
1924 struct btrfs_key key;
1925 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001926
Chris Mason0b86a832008-03-24 15:01:56 -04001927 path = btrfs_alloc_path();
1928 if (!path)
1929 return -ENOMEM;
1930
Chris Mason0b86a832008-03-24 15:01:56 -04001931 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1932 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001933 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001934
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001935 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1936 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001937 if (ret)
1938 goto out;
1939
1940 leaf = path->nodes[0];
1941 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1942
1943 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001944 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001945 btrfs_set_device_type(leaf, dev_item, device->type);
1946 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1947 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1948 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001949 btrfs_set_device_total_bytes(leaf, dev_item,
1950 btrfs_device_get_disk_total_bytes(device));
1951 btrfs_set_device_bytes_used(leaf, dev_item,
1952 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001953 btrfs_set_device_group(leaf, dev_item, 0);
1954 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1955 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001956 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001957
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001958 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001959 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001960 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001961 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1962 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001963 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001964
Yan Zheng2b820322008-11-17 21:11:30 -05001965 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001966out:
1967 btrfs_free_path(path);
1968 return ret;
1969}
Chris Mason8f18cf12008-04-25 16:53:30 -04001970
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001971/*
1972 * Function to update ctime/mtime for a given device path.
1973 * Mainly used for ctime/mtime based probe like libblkid.
1974 */
David Sterbada353f62017-02-14 17:55:53 +01001975static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001976{
1977 struct file *filp;
1978
1979 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001980 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001981 return;
1982 file_update_time(filp);
1983 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001984}
1985
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001986static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001987 struct btrfs_device *device)
1988{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001989 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001990 int ret;
1991 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001992 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001993 struct btrfs_trans_handle *trans;
1994
Chris Masona061fc82008-05-07 11:43:44 -04001995 path = btrfs_alloc_path();
1996 if (!path)
1997 return -ENOMEM;
1998
Yan, Zhenga22285a2010-05-16 10:48:46 -04001999 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002000 if (IS_ERR(trans)) {
2001 btrfs_free_path(path);
2002 return PTR_ERR(trans);
2003 }
Chris Masona061fc82008-05-07 11:43:44 -04002004 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2005 key.type = BTRFS_DEV_ITEM_KEY;
2006 key.offset = device->devid;
2007
2008 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002009 if (ret) {
2010 if (ret > 0)
2011 ret = -ENOENT;
2012 btrfs_abort_transaction(trans, ret);
2013 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002014 goto out;
2015 }
2016
2017 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002018 if (ret) {
2019 btrfs_abort_transaction(trans, ret);
2020 btrfs_end_transaction(trans);
2021 }
2022
Chris Masona061fc82008-05-07 11:43:44 -04002023out:
2024 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03002025 if (!ret)
2026 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04002027 return ret;
2028}
2029
David Sterba3cc31a02016-02-15 16:00:26 +01002030/*
2031 * Verify that @num_devices satisfies the RAID profile constraints in the whole
2032 * filesystem. It's up to the caller to adjust that number regarding eg. device
2033 * replace.
2034 */
2035static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
2036 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04002037{
Chris Masona061fc82008-05-07 11:43:44 -04002038 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00002039 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01002040 int i;
Chris Masona061fc82008-05-07 11:43:44 -04002041
Miao Xiede98ced2013-01-29 10:13:12 +00002042 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08002043 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002044
Anand Jainbd45ffb2016-02-13 10:01:34 +08002045 all_avail = fs_info->avail_data_alloc_bits |
2046 fs_info->avail_system_alloc_bits |
2047 fs_info->avail_metadata_alloc_bits;
2048 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002049
David Sterba418775a2016-02-15 16:28:14 +01002050 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002051 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002052 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002053
David Sterba418775a2016-02-15 16:28:14 +01002054 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002055 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002056
David Sterba418775a2016-02-15 16:28:14 +01002057 if (ret)
2058 return ret;
2059 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002060 }
2061
2062 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002063}
2064
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002065static struct btrfs_device * btrfs_find_next_active_device(
2066 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002067{
2068 struct btrfs_device *next_device;
2069
2070 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2071 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002072 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2073 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002074 return next_device;
2075 }
2076
2077 return NULL;
2078}
2079
2080/*
2081 * Helper function to check if the given device is part of s_bdev / latest_bdev
2082 * and replace it with the provided or the next active device, in the context
2083 * where this function called, there should be always be another device (or
2084 * this_dev) which is active.
2085 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002086void btrfs_assign_next_active_device(struct btrfs_device *device,
2087 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08002088{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002089 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002090 struct btrfs_device *next_device;
2091
2092 if (this_dev)
2093 next_device = this_dev;
2094 else
2095 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2096 device);
2097 ASSERT(next_device);
2098
2099 if (fs_info->sb->s_bdev &&
2100 (fs_info->sb->s_bdev == device->bdev))
2101 fs_info->sb->s_bdev = next_device->bdev;
2102
2103 if (fs_info->fs_devices->latest_bdev == device->bdev)
2104 fs_info->fs_devices->latest_bdev = next_device->bdev;
2105}
2106
Anand Jain1da73962018-08-10 13:53:21 +08002107/*
2108 * Return btrfs_fs_devices::num_devices excluding the device that's being
2109 * currently replaced.
2110 */
2111static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2112{
2113 u64 num_devices = fs_info->fs_devices->num_devices;
2114
David Sterbacb5583d2018-09-07 16:11:23 +02002115 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002116 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2117 ASSERT(num_devices > 1);
2118 num_devices--;
2119 }
David Sterbacb5583d2018-09-07 16:11:23 +02002120 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002121
2122 return num_devices;
2123}
2124
David Sterbada353f62017-02-14 17:55:53 +01002125int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2126 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002127{
2128 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002129 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002130 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002131 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002132 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002133
2134 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002135
Anand Jain1da73962018-08-10 13:53:21 +08002136 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002137
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002138 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002139 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002140 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002141
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002142 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2143
2144 if (IS_ERR(device)) {
2145 if (PTR_ERR(device) == -ENOENT &&
2146 strcmp(device_path, "missing") == 0)
2147 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2148 else
2149 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002150 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002151 }
Yan Zheng2b820322008-11-17 21:11:30 -05002152
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002153 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2154 btrfs_warn_in_rcu(fs_info,
2155 "cannot remove device %s (devid %llu) due to active swapfile",
2156 rcu_str_deref(device->name), device->devid);
2157 ret = -ETXTBSY;
2158 goto out;
2159 }
2160
Anand Jain401e29c2017-12-04 12:54:55 +08002161 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002162 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002163 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002164 }
2165
Anand Jainebbede42017-12-04 12:54:52 +08002166 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2167 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002168 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002169 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002170 }
2171
Anand Jainebbede42017-12-04 12:54:52 +08002172 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002173 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002174 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002175 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002176 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002177 }
Chris Masona061fc82008-05-07 11:43:44 -04002178
Carey Underwoodd7901552013-03-04 16:37:06 -06002179 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002180 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002181 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002182 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002183 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002184
Stefan Behrens63a212a2012-11-05 18:29:28 +01002185 /*
2186 * TODO: the superblock still includes this device in its num_devices
2187 * counter although write_all_supers() is not locked out. This
2188 * could give a filesystem state which requires a degraded mount.
2189 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002191 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002192 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002193
Anand Jaine12c9622017-12-04 12:54:53 +08002194 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002195 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002196
2197 /*
2198 * the device list mutex makes sure that we don't change
2199 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002200 * the device supers. Whoever is writing all supers, should
2201 * lock the device list mutex before getting the number of
2202 * devices in the super block (super_copy). Conversely,
2203 * whoever updates the number of devices in the super block
2204 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002205 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002206
Anand Jain41a52a02018-04-12 10:29:31 +08002207 /*
2208 * In normal cases the cur_devices == fs_devices. But in case
2209 * of deleting a seed device, the cur_devices should point to
2210 * its own fs_devices listed under the fs_devices->seed.
2211 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002212 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002213 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002214 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002215
Anand Jain41a52a02018-04-12 10:29:31 +08002216 cur_devices->num_devices--;
2217 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002218 /* Update total_devices of the parent fs_devices if it's seed */
2219 if (cur_devices != fs_devices)
2220 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002221
Anand Jaine6e674b2017-12-04 12:54:54 +08002222 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002223 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002224
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002225 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002226
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002227 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002228 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002229 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002230 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002231 }
Anand Jain99994cd2014-06-03 11:36:00 +08002232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002233 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2234 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002235 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002236
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002237 /*
2238 * at this point, the device is zero sized and detached from
2239 * the devices list. All that's left is to zero out the old
2240 * supers and free the device.
2241 */
Anand Jainebbede42017-12-04 12:54:52 +08002242 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002243 btrfs_scratch_superblocks(device->bdev, device->name->str);
2244
2245 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002246 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002247
Xiao Guangrong1f781602011-04-20 10:09:16 +00002248 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002249 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002250 if (fs_devices->seed == cur_devices) {
2251 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002252 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002253 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002254 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002255 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002256 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002257 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002258 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002259 }
2260
Chris Masona061fc82008-05-07 11:43:44 -04002261out:
2262 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002263 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002264
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002265error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002266 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002267 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002268 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002269 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002270 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002271 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002272 }
Anand Jain24fc5722016-02-13 10:01:36 +08002273 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002274}
2275
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002276void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002277{
Anand Jaind51908c2014-08-13 14:24:19 +08002278 struct btrfs_fs_devices *fs_devices;
2279
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002280 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002281
Anand Jain25e8e912014-08-20 10:56:56 +08002282 /*
2283 * in case of fs with no seed, srcdev->fs_devices will point
2284 * to fs_devices of fs_info. However when the dev being replaced is
2285 * a seed dev it will point to the seed's local fs_devices. In short
2286 * srcdev will have its correct fs_devices in both the cases.
2287 */
2288 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002289
Stefan Behrense93c89c2012-11-05 17:33:06 +01002290 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002291 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002292 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002293 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002294 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002295
Anand Jainebbede42017-12-04 12:54:52 +08002296 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002297 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002298
Miao Xie82372bc2014-09-03 21:35:44 +08002299 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002300 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002301}
2302
2303void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2304 struct btrfs_device *srcdev)
2305{
2306 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002307
Anand Jainebbede42017-12-04 12:54:52 +08002308 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002309 /* zero out the old super if it is writable */
2310 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2311 }
Anand Jain14238812016-07-22 06:04:53 +08002312
2313 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002314 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002315
Anand Jain94d5f0c2014-08-13 14:24:22 +08002316 /* if this is no devs we rather delete the fs_devices */
2317 if (!fs_devices->num_devices) {
2318 struct btrfs_fs_devices *tmp_fs_devices;
2319
Anand Jain6dd38f82017-10-17 06:53:50 +08002320 /*
2321 * On a mounted FS, num_devices can't be zero unless it's a
2322 * seed. In case of a seed device being replaced, the replace
2323 * target added to the sprout FS, so there will be no more
2324 * device left under the seed FS.
2325 */
2326 ASSERT(fs_devices->seeding);
2327
Anand Jain94d5f0c2014-08-13 14:24:22 +08002328 tmp_fs_devices = fs_info->fs_devices;
2329 while (tmp_fs_devices) {
2330 if (tmp_fs_devices->seed == fs_devices) {
2331 tmp_fs_devices->seed = fs_devices->seed;
2332 break;
2333 }
2334 tmp_fs_devices = tmp_fs_devices->seed;
2335 }
2336 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002337 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002338 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002339 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002340}
2341
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002342void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002343{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002344 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002345
Anand Jaind9a071f2018-04-12 10:29:38 +08002346 WARN_ON(!tgtdev);
2347 mutex_lock(&fs_devices->device_list_mutex);
2348
2349 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002350
Anand Jain779bf3fe2016-04-18 16:51:23 +08002351 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002352 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002353
Anand Jaind9a071f2018-04-12 10:29:38 +08002354 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002355
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002356 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002357
Stefan Behrense93c89c2012-11-05 17:33:06 +01002358 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002359
Anand Jaind9a071f2018-04-12 10:29:38 +08002360 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002361
2362 /*
2363 * The update_dev_time() with in btrfs_scratch_superblocks()
2364 * may lead to a call to btrfs_show_devname() which will try
2365 * to hold device_list_mutex. And here this device
2366 * is already out of device list, so we don't have to hold
2367 * the device_list_mutex lock.
2368 */
2369 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002370
2371 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002372 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002373}
2374
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002375static struct btrfs_device *btrfs_find_device_by_path(
2376 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002377{
2378 int ret = 0;
2379 struct btrfs_super_block *disk_super;
2380 u64 devid;
2381 u8 *dev_uuid;
2382 struct block_device *bdev;
2383 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002384 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002385
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002386 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002387 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002388 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002389 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002390 disk_super = (struct btrfs_super_block *)bh->b_data;
2391 devid = btrfs_stack_device_id(&disk_super->dev_item);
2392 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002393 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002394 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002395 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002396 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002397 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002398 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002399
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002400 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002401 if (!device)
2402 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002403 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002404 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002405}
2406
Yan Zheng2b820322008-11-17 21:11:30 -05002407/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002408 * Lookup a device given by device id, or the path if the id is 0.
2409 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002410struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002411 struct btrfs_fs_info *fs_info, u64 devid,
2412 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002413{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002414 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002415
David Sterba5c5c0df2016-02-15 16:39:55 +01002416 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002417 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002418 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002419 if (!device)
2420 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002421 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002422 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002423
2424 if (!device_path || !device_path[0])
2425 return ERR_PTR(-EINVAL);
2426
2427 if (strcmp(device_path, "missing") == 0) {
2428 /* Find first missing device */
2429 list_for_each_entry(device, &fs_info->fs_devices->devices,
2430 dev_list) {
2431 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2432 &device->dev_state) && !device->bdev)
2433 return device;
2434 }
2435 return ERR_PTR(-ENOENT);
2436 }
2437
2438 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002439}
2440
Yan Zheng2b820322008-11-17 21:11:30 -05002441/*
2442 * does all the dirty work required for changing file system's UUID.
2443 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002444static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002446 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002447 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002448 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002449 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002450 struct btrfs_device *device;
2451 u64 super_flags;
2452
David Sterbaa32bf9a2018-03-16 02:21:22 +01002453 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002454 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002455 return -EINVAL;
2456
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002457 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002458 if (IS_ERR(seed_devices))
2459 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002460
Yan Zhenge4404d62008-12-12 10:03:26 -05002461 old_devices = clone_fs_devices(fs_devices);
2462 if (IS_ERR(old_devices)) {
2463 kfree(seed_devices);
2464 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002465 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002466
Anand Jainc4babc52018-04-12 10:29:25 +08002467 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002468
Yan Zhenge4404d62008-12-12 10:03:26 -05002469 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2470 seed_devices->opened = 1;
2471 INIT_LIST_HEAD(&seed_devices->devices);
2472 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002473 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002474
Anand Jain321a4bf2018-07-16 22:58:09 +08002475 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002476 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2477 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002478 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002479 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002480
David Sterba34441362016-10-04 19:34:27 +02002481 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002482 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002483 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002484
Yan Zheng2b820322008-11-17 21:11:30 -05002485 fs_devices->seeding = 0;
2486 fs_devices->num_devices = 0;
2487 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002488 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002489 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002490 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002491
2492 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002493 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002494 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002495 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002496
Yan Zheng2b820322008-11-17 21:11:30 -05002497 super_flags = btrfs_super_flags(disk_super) &
2498 ~BTRFS_SUPER_FLAG_SEEDING;
2499 btrfs_set_super_flags(disk_super, super_flags);
2500
2501 return 0;
2502}
2503
2504/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002505 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002506 */
2507static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002508 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002509{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002510 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002511 struct btrfs_path *path;
2512 struct extent_buffer *leaf;
2513 struct btrfs_dev_item *dev_item;
2514 struct btrfs_device *device;
2515 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002516 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002517 u8 dev_uuid[BTRFS_UUID_SIZE];
2518 u64 devid;
2519 int ret;
2520
2521 path = btrfs_alloc_path();
2522 if (!path)
2523 return -ENOMEM;
2524
Yan Zheng2b820322008-11-17 21:11:30 -05002525 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2526 key.offset = 0;
2527 key.type = BTRFS_DEV_ITEM_KEY;
2528
2529 while (1) {
2530 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2531 if (ret < 0)
2532 goto error;
2533
2534 leaf = path->nodes[0];
2535next_slot:
2536 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2537 ret = btrfs_next_leaf(root, path);
2538 if (ret > 0)
2539 break;
2540 if (ret < 0)
2541 goto error;
2542 leaf = path->nodes[0];
2543 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002544 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002545 continue;
2546 }
2547
2548 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2549 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2550 key.type != BTRFS_DEV_ITEM_KEY)
2551 break;
2552
2553 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2554 struct btrfs_dev_item);
2555 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002556 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002557 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002558 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002559 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002560 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002561 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002562 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002563
2564 if (device->fs_devices->seeding) {
2565 btrfs_set_device_generation(leaf, dev_item,
2566 device->generation);
2567 btrfs_mark_buffer_dirty(leaf);
2568 }
2569
2570 path->slots[0]++;
2571 goto next_slot;
2572 }
2573 ret = 0;
2574error:
2575 btrfs_free_path(path);
2576 return ret;
2577}
2578
David Sterbada353f62017-02-14 17:55:53 +01002579int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002580{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002581 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002582 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002583 struct btrfs_trans_handle *trans;
2584 struct btrfs_device *device;
2585 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002586 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002587 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002588 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002589 u64 orig_super_total_bytes;
2590 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002591 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002592 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002593 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002594
Anand Jain5da54bc2018-07-03 13:14:50 +08002595 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002596 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002597
Li Zefana5d16332011-12-07 20:08:40 -05002598 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002599 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002600 if (IS_ERR(bdev))
2601 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002602
Anand Jain5da54bc2018-07-03 13:14:50 +08002603 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002604 seeding_dev = 1;
2605 down_write(&sb->s_umount);
2606 mutex_lock(&uuid_mutex);
2607 }
2608
Chris Mason8c8bee12008-09-29 11:19:10 -04002609 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002610
Anand Jain5da54bc2018-07-03 13:14:50 +08002611 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002612 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002613 if (device->bdev == bdev) {
2614 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002615 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002616 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002617 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002618 }
2619 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002620 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002621
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002622 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002623 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002624 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002625 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002626 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002627 }
2628
David Sterba78f2c9e2016-02-11 14:25:38 +01002629 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002630 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002631 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002632 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002633 }
Josef Bacik606686e2012-06-04 14:03:51 -04002634 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002635
Yan, Zhenga22285a2010-05-16 10:48:46 -04002636 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002637 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002638 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002639 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002640 }
2641
Josef Bacikd5e20032011-08-04 14:52:27 +00002642 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002643 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002644 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002645 device->io_width = fs_info->sectorsize;
2646 device->io_align = fs_info->sectorsize;
2647 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002648 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2649 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002650 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002651 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002652 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002653 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002654 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002655 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002656 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002657 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002658 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002659
Yan Zheng2b820322008-11-17 21:11:30 -05002660 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002661 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002662 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002663 if (ret) {
2664 btrfs_abort_transaction(trans, ret);
2665 goto error_trans;
2666 }
Yan Zheng2b820322008-11-17 21:11:30 -05002667 }
2668
Anand Jain5da54bc2018-07-03 13:14:50 +08002669 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002670
Anand Jain5da54bc2018-07-03 13:14:50 +08002671 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002672 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002673 list_add_rcu(&device->dev_list, &fs_devices->devices);
2674 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2675 fs_devices->num_devices++;
2676 fs_devices->open_devices++;
2677 fs_devices->rw_devices++;
2678 fs_devices->total_devices++;
2679 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002680
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002681 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002682
Anand Jaine884f4f2017-04-04 18:40:19 +08002683 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002684 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002685
Naohiro Aota39379fa2018-07-27 09:04:55 +09002686 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002688 round_down(orig_super_total_bytes + device->total_bytes,
2689 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002690
Naohiro Aota39379fa2018-07-27 09:04:55 +09002691 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2692 btrfs_set_super_num_devices(fs_info->super_copy,
2693 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002694
2695 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002696 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002697
Miao Xie2196d6e2014-09-03 21:35:41 +08002698 /*
2699 * we've got more storage, clear any full flags on the space
2700 * infos
2701 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002702 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002703
David Sterba34441362016-10-04 19:34:27 +02002704 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002705 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002706
Yan Zheng2b820322008-11-17 21:11:30 -05002707 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002708 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002709 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002710 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002711 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002712 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002713 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002714 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002715 }
2716
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002717 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002718 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002719 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002720 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002721 }
2722
2723 if (seeding_dev) {
2724 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2725
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002726 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002727 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002728 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002729 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002730 }
Anand Jainb2373f22014-06-03 11:36:03 +08002731
2732 /* Sprouting would change fsid of the mounted root,
2733 * so rename the fsid on the sysfs
2734 */
2735 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002736 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002737 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 btrfs_warn(fs_info,
2739 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002740 }
2741
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002742 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002743
2744 if (seeding_dev) {
2745 mutex_unlock(&uuid_mutex);
2746 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002747 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002748
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002749 if (ret) /* transaction commit */
2750 return ret;
2751
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002752 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002753 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002754 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002755 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002756 trans = btrfs_attach_transaction(root);
2757 if (IS_ERR(trans)) {
2758 if (PTR_ERR(trans) == -ENOENT)
2759 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002760 ret = PTR_ERR(trans);
2761 trans = NULL;
2762 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002763 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002764 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002765 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002766
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002767 /* Update ctime/mtime for libblkid */
2768 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002769 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002770
Anand Jaind31c32f2017-09-28 14:51:10 +08002771error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002772 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002773 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2774 mutex_lock(&fs_info->chunk_mutex);
2775 list_del_rcu(&device->dev_list);
2776 list_del(&device->dev_alloc_list);
2777 fs_info->fs_devices->num_devices--;
2778 fs_info->fs_devices->open_devices--;
2779 fs_info->fs_devices->rw_devices--;
2780 fs_info->fs_devices->total_devices--;
2781 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2782 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2783 btrfs_set_super_total_bytes(fs_info->super_copy,
2784 orig_super_total_bytes);
2785 btrfs_set_super_num_devices(fs_info->super_copy,
2786 orig_super_num_devices);
2787 mutex_unlock(&fs_info->chunk_mutex);
2788 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002789error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002790 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002791 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002792 if (trans)
2793 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002794error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002795 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002796error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002797 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002798 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002799 mutex_unlock(&uuid_mutex);
2800 up_write(&sb->s_umount);
2801 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002802 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002803}
2804
Chris Masond3977122009-01-05 21:25:51 -05002805static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2806 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002807{
2808 int ret;
2809 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002810 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002811 struct btrfs_dev_item *dev_item;
2812 struct extent_buffer *leaf;
2813 struct btrfs_key key;
2814
Chris Mason0b86a832008-03-24 15:01:56 -04002815 path = btrfs_alloc_path();
2816 if (!path)
2817 return -ENOMEM;
2818
2819 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2820 key.type = BTRFS_DEV_ITEM_KEY;
2821 key.offset = device->devid;
2822
2823 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2824 if (ret < 0)
2825 goto out;
2826
2827 if (ret > 0) {
2828 ret = -ENOENT;
2829 goto out;
2830 }
2831
2832 leaf = path->nodes[0];
2833 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2834
2835 btrfs_set_device_id(leaf, dev_item, device->devid);
2836 btrfs_set_device_type(leaf, dev_item, device->type);
2837 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2838 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2839 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002840 btrfs_set_device_total_bytes(leaf, dev_item,
2841 btrfs_device_get_disk_total_bytes(device));
2842 btrfs_set_device_bytes_used(leaf, dev_item,
2843 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002844 btrfs_mark_buffer_dirty(leaf);
2845
2846out:
2847 btrfs_free_path(path);
2848 return ret;
2849}
2850
Miao Xie2196d6e2014-09-03 21:35:41 +08002851int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002852 struct btrfs_device *device, u64 new_size)
2853{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002854 struct btrfs_fs_info *fs_info = device->fs_info;
2855 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002856 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002857 u64 old_total;
2858 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002859
Anand Jainebbede42017-12-04 12:54:52 +08002860 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002861 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002862
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002863 new_size = round_down(new_size, fs_info->sectorsize);
2864
David Sterba34441362016-10-04 19:34:27 +02002865 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002866 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002867 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002868
Stefan Behrens63a212a2012-11-05 18:29:28 +01002869 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002870 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002871 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002872 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002873 }
Yan Zheng2b820322008-11-17 21:11:30 -05002874
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002876
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002877 btrfs_set_super_total_bytes(super_copy,
2878 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002879 device->fs_devices->total_rw_bytes += diff;
2880
Miao Xie7cc8e582014-09-03 21:35:38 +08002881 btrfs_device_set_total_bytes(device, new_size);
2882 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002883 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002884 if (list_empty(&device->resized_list))
2885 list_add_tail(&device->resized_list,
2886 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002887 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002888
Chris Mason8f18cf12008-04-25 16:53:30 -04002889 return btrfs_update_device(trans, device);
2890}
2891
Nikolay Borisovf4208792018-07-20 19:37:52 +03002892static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002893{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002894 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002895 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002896 int ret;
2897 struct btrfs_path *path;
2898 struct btrfs_key key;
2899
Chris Mason8f18cf12008-04-25 16:53:30 -04002900 path = btrfs_alloc_path();
2901 if (!path)
2902 return -ENOMEM;
2903
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002904 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002905 key.offset = chunk_offset;
2906 key.type = BTRFS_CHUNK_ITEM_KEY;
2907
2908 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002909 if (ret < 0)
2910 goto out;
2911 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 btrfs_handle_fs_error(fs_info, -ENOENT,
2913 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002914 ret = -ENOENT;
2915 goto out;
2916 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002917
2918 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002919 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 btrfs_handle_fs_error(fs_info, ret,
2921 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002922out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002923 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002924 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002925}
2926
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002927static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002929 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002930 struct btrfs_disk_key *disk_key;
2931 struct btrfs_chunk *chunk;
2932 u8 *ptr;
2933 int ret = 0;
2934 u32 num_stripes;
2935 u32 array_size;
2936 u32 len = 0;
2937 u32 cur;
2938 struct btrfs_key key;
2939
David Sterba34441362016-10-04 19:34:27 +02002940 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002941 array_size = btrfs_super_sys_array_size(super_copy);
2942
2943 ptr = super_copy->sys_chunk_array;
2944 cur = 0;
2945
2946 while (cur < array_size) {
2947 disk_key = (struct btrfs_disk_key *)ptr;
2948 btrfs_disk_key_to_cpu(&key, disk_key);
2949
2950 len = sizeof(*disk_key);
2951
2952 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2953 chunk = (struct btrfs_chunk *)(ptr + len);
2954 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2955 len += btrfs_chunk_item_size(num_stripes);
2956 } else {
2957 ret = -EIO;
2958 break;
2959 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002960 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002961 key.offset == chunk_offset) {
2962 memmove(ptr, ptr + len, array_size - (cur + len));
2963 array_size -= len;
2964 btrfs_set_super_sys_array_size(super_copy, array_size);
2965 } else {
2966 ptr += len;
2967 cur += len;
2968 }
2969 }
David Sterba34441362016-10-04 19:34:27 +02002970 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002971 return ret;
2972}
2973
Omar Sandoval60ca8422018-05-16 16:34:31 -07002974/*
2975 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2976 * @logical: Logical block offset in bytes.
2977 * @length: Length of extent in bytes.
2978 *
2979 * Return: Chunk mapping or ERR_PTR.
2980 */
2981struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2982 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002983{
2984 struct extent_map_tree *em_tree;
2985 struct extent_map *em;
2986
2987 em_tree = &fs_info->mapping_tree.map_tree;
2988 read_lock(&em_tree->lock);
2989 em = lookup_extent_mapping(em_tree, logical, length);
2990 read_unlock(&em_tree->lock);
2991
2992 if (!em) {
2993 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2994 logical, length);
2995 return ERR_PTR(-EINVAL);
2996 }
2997
2998 if (em->start > logical || em->start + em->len < logical) {
2999 btrfs_crit(fs_info,
3000 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
3001 logical, length, em->start, em->start + em->len);
3002 free_extent_map(em);
3003 return ERR_PTR(-EINVAL);
3004 }
3005
3006 /* callers are responsible for dropping em's ref. */
3007 return em;
3008}
3009
Nikolay Borisov97aff912018-07-20 19:37:53 +03003010int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04003011{
Nikolay Borisov97aff912018-07-20 19:37:53 +03003012 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003013 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003014 struct map_lookup *map;
3015 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003016 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003017 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003018
Omar Sandoval60ca8422018-05-16 16:34:31 -07003019 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07003020 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04003021 /*
3022 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08003023 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04003024 * do anything we still error out.
3025 */
3026 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07003027 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003028 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04003029 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02003030 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003031 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02003032 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003033
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003034 /*
3035 * Take the device list mutex to prevent races with the final phase of
3036 * a device replace operation that replaces the device object associated
3037 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3038 */
3039 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003040 for (i = 0; i < map->num_stripes; i++) {
3041 struct btrfs_device *device = map->stripes[i].dev;
3042 ret = btrfs_free_dev_extent(trans, device,
3043 map->stripes[i].physical,
3044 &dev_extent_len);
3045 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003046 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04003047 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003048 goto out;
3049 }
3050
3051 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003052 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003053 btrfs_device_set_bytes_used(device,
3054 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003055 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003056 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003057 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003058 }
3059
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003060 ret = btrfs_update_device(trans, device);
3061 if (ret) {
3062 mutex_unlock(&fs_devices->device_list_mutex);
3063 btrfs_abort_transaction(trans, ret);
3064 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003065 }
3066 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003067 mutex_unlock(&fs_devices->device_list_mutex);
3068
Nikolay Borisovf4208792018-07-20 19:37:52 +03003069 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003070 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003071 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003072 goto out;
3073 }
3074
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003075 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003076
3077 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003078 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003079 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003080 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003081 goto out;
3082 }
3083 }
3084
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003085 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003086 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003087 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003088 goto out;
3089 }
3090
Josef Bacik47ab2a62014-09-18 11:20:02 -04003091out:
3092 /* once for us */
3093 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003094 return ret;
3095}
3096
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003097static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003098{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003099 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003100 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04003101 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003102
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003103 /*
3104 * Prevent races with automatic removal of unused block groups.
3105 * After we relocate and before we remove the chunk with offset
3106 * chunk_offset, automatic removal of the block group can kick in,
3107 * resulting in a failure when calling btrfs_remove_chunk() below.
3108 *
3109 * Make sure to acquire this mutex before doing a tree search (dev
3110 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3111 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3112 * we release the path used to search the chunk/dev tree and before
3113 * the current task acquires this mutex and calls us.
3114 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003115 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003116
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003117 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003118 if (ret)
3119 return -ENOSPC;
3120
Chris Mason8f18cf12008-04-25 16:53:30 -04003121 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003122 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003123 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003124 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003125 if (ret)
3126 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003127
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003128 /*
3129 * We add the kobjects here (and after forcing data chunk creation)
3130 * since relocation is the only place we'll create chunks of a new
3131 * type at runtime. The only place where we'll remove the last
3132 * chunk of a type is the call immediately below this one. Even
3133 * so, we're protected against races with the cleaner thread since
3134 * we're covered by the delete_unused_bgs_mutex.
3135 */
3136 btrfs_add_raid_kobjects(fs_info);
3137
Chris Mason19c4d2f2016-10-10 13:43:31 -07003138 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3139 chunk_offset);
3140 if (IS_ERR(trans)) {
3141 ret = PTR_ERR(trans);
3142 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3143 return ret;
3144 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003145
Chris Mason19c4d2f2016-10-10 13:43:31 -07003146 /*
3147 * step two, delete the device extents and the
3148 * chunk tree entries
3149 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003150 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003151 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003152 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003153}
3154
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003155static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003156{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003157 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003158 struct btrfs_path *path;
3159 struct extent_buffer *leaf;
3160 struct btrfs_chunk *chunk;
3161 struct btrfs_key key;
3162 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003163 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003164 bool retried = false;
3165 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003166 int ret;
3167
3168 path = btrfs_alloc_path();
3169 if (!path)
3170 return -ENOMEM;
3171
Josef Bacikba1bf482009-09-11 16:11:19 -04003172again:
Yan Zheng2b820322008-11-17 21:11:30 -05003173 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3174 key.offset = (u64)-1;
3175 key.type = BTRFS_CHUNK_ITEM_KEY;
3176
3177 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003178 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003179 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003180 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003181 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003182 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003183 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003184 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003185
3186 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3187 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003188 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003189 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003190 if (ret < 0)
3191 goto error;
3192 if (ret > 0)
3193 break;
3194
3195 leaf = path->nodes[0];
3196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3197
3198 chunk = btrfs_item_ptr(leaf, path->slots[0],
3199 struct btrfs_chunk);
3200 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003201 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003202
3203 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003204 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003205 if (ret == -ENOSPC)
3206 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303207 else
3208 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003209 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003210 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003211
3212 if (found_key.offset == 0)
3213 break;
3214 key.offset = found_key.offset - 1;
3215 }
3216 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003217 if (failed && !retried) {
3218 failed = 0;
3219 retried = true;
3220 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303221 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003222 ret = -ENOSPC;
3223 }
Yan Zheng2b820322008-11-17 21:11:30 -05003224error:
3225 btrfs_free_path(path);
3226 return ret;
3227}
3228
Liu Boa6f93c72017-11-15 16:28:11 -07003229/*
3230 * return 1 : allocate a data chunk successfully,
3231 * return <0: errors during allocating a data chunk,
3232 * return 0 : no need to allocate a data chunk.
3233 */
3234static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3235 u64 chunk_offset)
3236{
3237 struct btrfs_block_group_cache *cache;
3238 u64 bytes_used;
3239 u64 chunk_type;
3240
3241 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3242 ASSERT(cache);
3243 chunk_type = cache->flags;
3244 btrfs_put_block_group(cache);
3245
3246 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3247 spin_lock(&fs_info->data_sinfo->lock);
3248 bytes_used = fs_info->data_sinfo->bytes_used;
3249 spin_unlock(&fs_info->data_sinfo->lock);
3250
3251 if (!bytes_used) {
3252 struct btrfs_trans_handle *trans;
3253 int ret;
3254
3255 trans = btrfs_join_transaction(fs_info->tree_root);
3256 if (IS_ERR(trans))
3257 return PTR_ERR(trans);
3258
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003259 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003260 BTRFS_BLOCK_GROUP_DATA);
3261 btrfs_end_transaction(trans);
3262 if (ret < 0)
3263 return ret;
3264
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003265 btrfs_add_raid_kobjects(fs_info);
3266
Liu Boa6f93c72017-11-15 16:28:11 -07003267 return 1;
3268 }
3269 }
3270 return 0;
3271}
3272
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003273static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003274 struct btrfs_balance_control *bctl)
3275{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003276 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003277 struct btrfs_trans_handle *trans;
3278 struct btrfs_balance_item *item;
3279 struct btrfs_disk_balance_args disk_bargs;
3280 struct btrfs_path *path;
3281 struct extent_buffer *leaf;
3282 struct btrfs_key key;
3283 int ret, err;
3284
3285 path = btrfs_alloc_path();
3286 if (!path)
3287 return -ENOMEM;
3288
3289 trans = btrfs_start_transaction(root, 0);
3290 if (IS_ERR(trans)) {
3291 btrfs_free_path(path);
3292 return PTR_ERR(trans);
3293 }
3294
3295 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003296 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003297 key.offset = 0;
3298
3299 ret = btrfs_insert_empty_item(trans, root, path, &key,
3300 sizeof(*item));
3301 if (ret)
3302 goto out;
3303
3304 leaf = path->nodes[0];
3305 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3306
David Sterbab159fa22016-11-08 18:09:03 +01003307 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003308
3309 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3310 btrfs_set_balance_data(leaf, item, &disk_bargs);
3311 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3312 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3313 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3314 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3315
3316 btrfs_set_balance_flags(leaf, item, bctl->flags);
3317
3318 btrfs_mark_buffer_dirty(leaf);
3319out:
3320 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003321 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003322 if (err && !ret)
3323 ret = err;
3324 return ret;
3325}
3326
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003327static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003328{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003329 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003330 struct btrfs_trans_handle *trans;
3331 struct btrfs_path *path;
3332 struct btrfs_key key;
3333 int ret, err;
3334
3335 path = btrfs_alloc_path();
3336 if (!path)
3337 return -ENOMEM;
3338
3339 trans = btrfs_start_transaction(root, 0);
3340 if (IS_ERR(trans)) {
3341 btrfs_free_path(path);
3342 return PTR_ERR(trans);
3343 }
3344
3345 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003346 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003347 key.offset = 0;
3348
3349 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3350 if (ret < 0)
3351 goto out;
3352 if (ret > 0) {
3353 ret = -ENOENT;
3354 goto out;
3355 }
3356
3357 ret = btrfs_del_item(trans, root, path);
3358out:
3359 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003360 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003361 if (err && !ret)
3362 ret = err;
3363 return ret;
3364}
3365
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003366/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003367 * This is a heuristic used to reduce the number of chunks balanced on
3368 * resume after balance was interrupted.
3369 */
3370static void update_balance_args(struct btrfs_balance_control *bctl)
3371{
3372 /*
3373 * Turn on soft mode for chunk types that were being converted.
3374 */
3375 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3376 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3377 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3378 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3379 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3380 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3381
3382 /*
3383 * Turn on usage filter if is not already used. The idea is
3384 * that chunks that we have already balanced should be
3385 * reasonably full. Don't do it for chunks that are being
3386 * converted - that will keep us from relocating unconverted
3387 * (albeit full) chunks.
3388 */
3389 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003390 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003391 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3392 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3393 bctl->data.usage = 90;
3394 }
3395 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003396 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003397 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3398 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3399 bctl->sys.usage = 90;
3400 }
3401 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003402 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003403 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3404 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3405 bctl->meta.usage = 90;
3406 }
3407}
3408
3409/*
David Sterba149196a2018-03-20 20:23:09 +01003410 * Clear the balance status in fs_info and delete the balance item from disk.
3411 */
3412static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003413{
3414 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003415 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416
3417 BUG_ON(!fs_info->balance_ctl);
3418
3419 spin_lock(&fs_info->balance_lock);
3420 fs_info->balance_ctl = NULL;
3421 spin_unlock(&fs_info->balance_lock);
3422
3423 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003424 ret = del_balance_item(fs_info);
3425 if (ret)
3426 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003427}
3428
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003429/*
3430 * Balance filters. Return 1 if chunk should be filtered out
3431 * (should not be balanced).
3432 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003433static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003434 struct btrfs_balance_args *bargs)
3435{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003436 chunk_type = chunk_to_extended(chunk_type) &
3437 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003438
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003439 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003440 return 0;
3441
3442 return 1;
3443}
3444
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003445static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003446 struct btrfs_balance_args *bargs)
3447{
3448 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003449 u64 chunk_used;
3450 u64 user_thresh_min;
3451 u64 user_thresh_max;
3452 int ret = 1;
3453
3454 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3455 chunk_used = btrfs_block_group_used(&cache->item);
3456
3457 if (bargs->usage_min == 0)
3458 user_thresh_min = 0;
3459 else
3460 user_thresh_min = div_factor_fine(cache->key.offset,
3461 bargs->usage_min);
3462
3463 if (bargs->usage_max == 0)
3464 user_thresh_max = 1;
3465 else if (bargs->usage_max > 100)
3466 user_thresh_max = cache->key.offset;
3467 else
3468 user_thresh_max = div_factor_fine(cache->key.offset,
3469 bargs->usage_max);
3470
3471 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3472 ret = 0;
3473
3474 btrfs_put_block_group(cache);
3475 return ret;
3476}
3477
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003478static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003479 u64 chunk_offset, struct btrfs_balance_args *bargs)
3480{
3481 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003482 u64 chunk_used, user_thresh;
3483 int ret = 1;
3484
3485 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3486 chunk_used = btrfs_block_group_used(&cache->item);
3487
David Sterbabc309462015-10-20 18:22:13 +02003488 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003489 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003490 else if (bargs->usage > 100)
3491 user_thresh = cache->key.offset;
3492 else
3493 user_thresh = div_factor_fine(cache->key.offset,
3494 bargs->usage);
3495
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003496 if (chunk_used < user_thresh)
3497 ret = 0;
3498
3499 btrfs_put_block_group(cache);
3500 return ret;
3501}
3502
Ilya Dryomov409d4042012-01-16 22:04:47 +02003503static int chunk_devid_filter(struct extent_buffer *leaf,
3504 struct btrfs_chunk *chunk,
3505 struct btrfs_balance_args *bargs)
3506{
3507 struct btrfs_stripe *stripe;
3508 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3509 int i;
3510
3511 for (i = 0; i < num_stripes; i++) {
3512 stripe = btrfs_stripe_nr(chunk, i);
3513 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3514 return 0;
3515 }
3516
3517 return 1;
3518}
3519
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003520/* [pstart, pend) */
3521static int chunk_drange_filter(struct extent_buffer *leaf,
3522 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003523 struct btrfs_balance_args *bargs)
3524{
3525 struct btrfs_stripe *stripe;
3526 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3527 u64 stripe_offset;
3528 u64 stripe_length;
3529 int factor;
3530 int i;
3531
3532 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3533 return 0;
3534
3535 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003536 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3537 factor = num_stripes / 2;
3538 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3539 factor = num_stripes - 1;
3540 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3541 factor = num_stripes - 2;
3542 } else {
3543 factor = num_stripes;
3544 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003545
3546 for (i = 0; i < num_stripes; i++) {
3547 stripe = btrfs_stripe_nr(chunk, i);
3548 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3549 continue;
3550
3551 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3552 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003553 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003554
3555 if (stripe_offset < bargs->pend &&
3556 stripe_offset + stripe_length > bargs->pstart)
3557 return 0;
3558 }
3559
3560 return 1;
3561}
3562
Ilya Dryomovea671762012-01-16 22:04:48 +02003563/* [vstart, vend) */
3564static int chunk_vrange_filter(struct extent_buffer *leaf,
3565 struct btrfs_chunk *chunk,
3566 u64 chunk_offset,
3567 struct btrfs_balance_args *bargs)
3568{
3569 if (chunk_offset < bargs->vend &&
3570 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3571 /* at least part of the chunk is inside this vrange */
3572 return 0;
3573
3574 return 1;
3575}
3576
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003577static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3578 struct btrfs_chunk *chunk,
3579 struct btrfs_balance_args *bargs)
3580{
3581 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3582
3583 if (bargs->stripes_min <= num_stripes
3584 && num_stripes <= bargs->stripes_max)
3585 return 0;
3586
3587 return 1;
3588}
3589
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003590static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003591 struct btrfs_balance_args *bargs)
3592{
3593 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3594 return 0;
3595
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003596 chunk_type = chunk_to_extended(chunk_type) &
3597 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003598
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003599 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003600 return 1;
3601
3602 return 0;
3603}
3604
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003605static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003606 struct extent_buffer *leaf,
3607 struct btrfs_chunk *chunk, u64 chunk_offset)
3608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003609 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003610 struct btrfs_balance_args *bargs = NULL;
3611 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3612
3613 /* type filter */
3614 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3615 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3616 return 0;
3617 }
3618
3619 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3620 bargs = &bctl->data;
3621 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3622 bargs = &bctl->sys;
3623 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3624 bargs = &bctl->meta;
3625
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003626 /* profiles filter */
3627 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3628 chunk_profiles_filter(chunk_type, bargs)) {
3629 return 0;
3630 }
3631
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003632 /* usage filter */
3633 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003634 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003635 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003636 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003637 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003638 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003639 }
3640
Ilya Dryomov409d4042012-01-16 22:04:47 +02003641 /* devid filter */
3642 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3643 chunk_devid_filter(leaf, chunk, bargs)) {
3644 return 0;
3645 }
3646
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003647 /* drange filter, makes sense only with devid filter */
3648 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003649 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003650 return 0;
3651 }
3652
Ilya Dryomovea671762012-01-16 22:04:48 +02003653 /* vrange filter */
3654 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3655 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3656 return 0;
3657 }
3658
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003659 /* stripes filter */
3660 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3661 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3662 return 0;
3663 }
3664
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003665 /* soft profile changing mode */
3666 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3667 chunk_soft_convert_filter(chunk_type, bargs)) {
3668 return 0;
3669 }
3670
David Sterba7d824b62014-05-07 17:37:51 +02003671 /*
3672 * limited by count, must be the last filter
3673 */
3674 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3675 if (bargs->limit == 0)
3676 return 0;
3677 else
3678 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003679 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3680 /*
3681 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003682 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003683 * about the count of all chunks that satisfy the filters.
3684 */
3685 if (bargs->limit_max == 0)
3686 return 0;
3687 else
3688 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003689 }
3690
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003691 return 1;
3692}
3693
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003694static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003695{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003696 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003697 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003698 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003699 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003700 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003701 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003702 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003703 struct extent_buffer *leaf;
3704 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003705 int ret;
3706 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003707 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003708 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003709 u64 limit_data = bctl->data.limit;
3710 u64 limit_meta = bctl->meta.limit;
3711 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003712 u32 count_data = 0;
3713 u32 count_meta = 0;
3714 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003715 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003716
Chris Masonec44a352008-04-28 15:29:52 -04003717 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003718 if (!path) {
3719 ret = -ENOMEM;
3720 goto error;
3721 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003722
3723 /* zero out stat counters */
3724 spin_lock(&fs_info->balance_lock);
3725 memset(&bctl->stat, 0, sizeof(bctl->stat));
3726 spin_unlock(&fs_info->balance_lock);
3727again:
David Sterba7d824b62014-05-07 17:37:51 +02003728 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003729 /*
3730 * The single value limit and min/max limits use the same bytes
3731 * in the
3732 */
David Sterba7d824b62014-05-07 17:37:51 +02003733 bctl->data.limit = limit_data;
3734 bctl->meta.limit = limit_meta;
3735 bctl->sys.limit = limit_sys;
3736 }
Chris Masonec44a352008-04-28 15:29:52 -04003737 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3738 key.offset = (u64)-1;
3739 key.type = BTRFS_CHUNK_ITEM_KEY;
3740
Chris Masond3977122009-01-05 21:25:51 -05003741 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003742 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003743 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003744 ret = -ECANCELED;
3745 goto error;
3746 }
3747
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003748 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003749 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003750 if (ret < 0) {
3751 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003752 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003753 }
Chris Masonec44a352008-04-28 15:29:52 -04003754
3755 /*
3756 * this shouldn't happen, it means the last relocate
3757 * failed
3758 */
3759 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003760 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003761
3762 ret = btrfs_previous_item(chunk_root, path, 0,
3763 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003764 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003765 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003766 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003767 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003768 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003769
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003770 leaf = path->nodes[0];
3771 slot = path->slots[0];
3772 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3773
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003774 if (found_key.objectid != key.objectid) {
3775 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003776 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003777 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003778
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003779 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003780 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003781
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003782 if (!counting) {
3783 spin_lock(&fs_info->balance_lock);
3784 bctl->stat.considered++;
3785 spin_unlock(&fs_info->balance_lock);
3786 }
3787
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003788 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003789 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003790
David Sterbab3b4aa72011-04-21 01:20:15 +02003791 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003792 if (!ret) {
3793 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003794 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003795 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003796
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003797 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003798 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003799 spin_lock(&fs_info->balance_lock);
3800 bctl->stat.expected++;
3801 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003802
3803 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3804 count_data++;
3805 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3806 count_sys++;
3807 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3808 count_meta++;
3809
3810 goto loop;
3811 }
3812
3813 /*
3814 * Apply limit_min filter, no need to check if the LIMITS
3815 * filter is used, limit_min is 0 by default
3816 */
3817 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3818 count_data < bctl->data.limit_min)
3819 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3820 count_meta < bctl->meta.limit_min)
3821 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3822 count_sys < bctl->sys.limit_min)) {
3823 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003824 goto loop;
3825 }
3826
Liu Boa6f93c72017-11-15 16:28:11 -07003827 if (!chunk_reserved) {
3828 /*
3829 * We may be relocating the only data chunk we have,
3830 * which could potentially end up with losing data's
3831 * raid profile, so lets allocate an empty one in
3832 * advance.
3833 */
3834 ret = btrfs_may_alloc_data_chunk(fs_info,
3835 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003836 if (ret < 0) {
3837 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3838 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003839 } else if (ret == 1) {
3840 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003841 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003842 }
3843
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003844 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003845 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003846 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003847 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003848 } else if (ret == -ETXTBSY) {
3849 btrfs_info(fs_info,
3850 "skipping relocation of block group %llu due to active swapfile",
3851 found_key.offset);
3852 ret = 0;
3853 } else if (ret) {
3854 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003855 } else {
3856 spin_lock(&fs_info->balance_lock);
3857 bctl->stat.completed++;
3858 spin_unlock(&fs_info->balance_lock);
3859 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003860loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003861 if (found_key.offset == 0)
3862 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003863 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003864 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003865
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003866 if (counting) {
3867 btrfs_release_path(path);
3868 counting = false;
3869 goto again;
3870 }
Chris Masonec44a352008-04-28 15:29:52 -04003871error:
3872 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003873 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003874 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003875 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003876 if (!ret)
3877 ret = -ENOSPC;
3878 }
3879
Chris Masonec44a352008-04-28 15:29:52 -04003880 return ret;
3881}
3882
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003883/**
3884 * alloc_profile_is_valid - see if a given profile is valid and reduced
3885 * @flags: profile to validate
3886 * @extended: if true @flags is treated as an extended profile
3887 */
3888static int alloc_profile_is_valid(u64 flags, int extended)
3889{
3890 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3891 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3892
3893 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3894
3895 /* 1) check that all other bits are zeroed */
3896 if (flags & ~mask)
3897 return 0;
3898
3899 /* 2) see if profile is reduced */
3900 if (flags == 0)
3901 return !extended; /* "0" is valid for usual profiles */
3902
3903 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003904 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003905}
3906
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003907static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3908{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003909 /* cancel requested || normal exit path */
3910 return atomic_read(&fs_info->balance_cancel_req) ||
3911 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3912 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003913}
3914
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003915/* Non-zero return value signifies invalidity */
3916static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3917 u64 allowed)
3918{
3919 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3920 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3921 (bctl_arg->target & ~allowed)));
3922}
3923
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003924/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003925 * Fill @buf with textual description of balance filter flags @bargs, up to
3926 * @size_buf including the terminating null. The output may be trimmed if it
3927 * does not fit into the provided buffer.
3928 */
3929static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3930 u32 size_buf)
3931{
3932 int ret;
3933 u32 size_bp = size_buf;
3934 char *bp = buf;
3935 u64 flags = bargs->flags;
3936 char tmp_buf[128] = {'\0'};
3937
3938 if (!flags)
3939 return;
3940
3941#define CHECK_APPEND_NOARG(a) \
3942 do { \
3943 ret = snprintf(bp, size_bp, (a)); \
3944 if (ret < 0 || ret >= size_bp) \
3945 goto out_overflow; \
3946 size_bp -= ret; \
3947 bp += ret; \
3948 } while (0)
3949
3950#define CHECK_APPEND_1ARG(a, v1) \
3951 do { \
3952 ret = snprintf(bp, size_bp, (a), (v1)); \
3953 if (ret < 0 || ret >= size_bp) \
3954 goto out_overflow; \
3955 size_bp -= ret; \
3956 bp += ret; \
3957 } while (0)
3958
3959#define CHECK_APPEND_2ARG(a, v1, v2) \
3960 do { \
3961 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3962 if (ret < 0 || ret >= size_bp) \
3963 goto out_overflow; \
3964 size_bp -= ret; \
3965 bp += ret; \
3966 } while (0)
3967
3968 if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
3969 int index = btrfs_bg_flags_to_raid_index(bargs->target);
3970
3971 CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
3972 }
3973
3974 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3975 CHECK_APPEND_NOARG("soft,");
3976
3977 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3978 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3979 sizeof(tmp_buf));
3980 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3981 }
3982
3983 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3984 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3985
3986 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3987 CHECK_APPEND_2ARG("usage=%u..%u,",
3988 bargs->usage_min, bargs->usage_max);
3989
3990 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3991 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3992
3993 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3994 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3995 bargs->pstart, bargs->pend);
3996
3997 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3998 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3999 bargs->vstart, bargs->vend);
4000
4001 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
4002 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
4003
4004 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
4005 CHECK_APPEND_2ARG("limit=%u..%u,",
4006 bargs->limit_min, bargs->limit_max);
4007
4008 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
4009 CHECK_APPEND_2ARG("stripes=%u..%u,",
4010 bargs->stripes_min, bargs->stripes_max);
4011
4012#undef CHECK_APPEND_2ARG
4013#undef CHECK_APPEND_1ARG
4014#undef CHECK_APPEND_NOARG
4015
4016out_overflow:
4017
4018 if (size_bp < size_buf)
4019 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
4020 else
4021 buf[0] = '\0';
4022}
4023
4024static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
4025{
4026 u32 size_buf = 1024;
4027 char tmp_buf[192] = {'\0'};
4028 char *buf;
4029 char *bp;
4030 u32 size_bp = size_buf;
4031 int ret;
4032 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4033
4034 buf = kzalloc(size_buf, GFP_KERNEL);
4035 if (!buf)
4036 return;
4037
4038 bp = buf;
4039
4040#define CHECK_APPEND_1ARG(a, v1) \
4041 do { \
4042 ret = snprintf(bp, size_bp, (a), (v1)); \
4043 if (ret < 0 || ret >= size_bp) \
4044 goto out_overflow; \
4045 size_bp -= ret; \
4046 bp += ret; \
4047 } while (0)
4048
4049 if (bctl->flags & BTRFS_BALANCE_FORCE)
4050 CHECK_APPEND_1ARG("%s", "-f ");
4051
4052 if (bctl->flags & BTRFS_BALANCE_DATA) {
4053 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4054 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4055 }
4056
4057 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4058 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4059 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4060 }
4061
4062 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4063 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4064 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4065 }
4066
4067#undef CHECK_APPEND_1ARG
4068
4069out_overflow:
4070
4071 if (size_bp < size_buf)
4072 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4073 btrfs_info(fs_info, "balance: %s %s",
4074 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4075 "resume" : "start", buf);
4076
4077 kfree(buf);
4078}
4079
4080/*
David Sterbadccdb072018-03-21 00:20:05 +01004081 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004082 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004083int btrfs_balance(struct btrfs_fs_info *fs_info,
4084 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004085 struct btrfs_ioctl_balance_args *bargs)
4086{
Adam Borowski14506122017-03-07 23:34:44 +01004087 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004088 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004089 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004090 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004091 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004092 unsigned seq;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004093 bool reducing_integrity;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004094
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004095 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004096 atomic_read(&fs_info->balance_pause_req) ||
4097 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004098 ret = -EINVAL;
4099 goto out;
4100 }
4101
Ilya Dryomove4837f82012-03-27 17:09:17 +03004102 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4103 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4104 mixed = 1;
4105
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004106 /*
4107 * In case of mixed groups both data and meta should be picked,
4108 * and identical options should be given for both of them.
4109 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004110 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4111 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004112 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4113 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4114 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004115 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004116 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004117 ret = -EINVAL;
4118 goto out;
4119 }
4120 }
4121
Anand Jain1da73962018-08-10 13:53:21 +08004122 num_devices = btrfs_num_devices(fs_info);
4123
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04004124 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
4125 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004126 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00004127 if (num_devices > 2)
4128 allowed |= BTRFS_BLOCK_GROUP_RAID5;
4129 if (num_devices > 3)
4130 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
4131 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004132 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004133 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
4134
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004135 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004136 "balance: invalid convert data profile %s",
4137 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004138 ret = -EINVAL;
4139 goto out;
4140 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004141 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004142 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
4143
Frank Holtonefe120a2013-12-20 11:37:06 -05004144 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004145 "balance: invalid convert metadata profile %s",
4146 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004147 ret = -EINVAL;
4148 goto out;
4149 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004150 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004151 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
4152
Frank Holtonefe120a2013-12-20 11:37:06 -05004153 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004154 "balance: invalid convert system profile %s",
4155 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004156 ret = -EINVAL;
4157 goto out;
4158 }
4159
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004160 /* allow to reduce meta or sys integrity only if force set */
4161 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05004162 BTRFS_BLOCK_GROUP_RAID10 |
4163 BTRFS_BLOCK_GROUP_RAID5 |
4164 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00004165 do {
4166 seq = read_seqbegin(&fs_info->profiles_lock);
4167
4168 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4169 (fs_info->avail_system_alloc_bits & allowed) &&
4170 !(bctl->sys.target & allowed)) ||
4171 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4172 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004173 !(bctl->meta.target & allowed)))
4174 reducing_integrity = true;
4175 else
4176 reducing_integrity = false;
4177
4178 /* if we're not converting, the target field is uninitialized */
4179 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4180 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4181 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4182 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004183 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004184
Filipe Manana5a8067c2018-11-19 09:48:12 +00004185 if (reducing_integrity) {
4186 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4187 btrfs_info(fs_info,
4188 "balance: force reducing metadata integrity");
4189 } else {
4190 btrfs_err(fs_info,
4191 "balance: reduces metadata integrity, use --force if you want this");
4192 ret = -EINVAL;
4193 goto out;
4194 }
4195 }
4196
Adam Borowski14506122017-03-07 23:34:44 +01004197 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4198 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004199 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
4200 int data_index = btrfs_bg_flags_to_raid_index(data_target);
4201
Sam Tygieree592d02016-01-06 08:46:12 +00004202 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004203 "balance: metadata profile %s has lower redundancy than data profile %s",
4204 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00004205 }
4206
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004207 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004208 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004209 goto out;
4210
Ilya Dryomov59641012012-01-16 22:04:48 +02004211 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4212 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004213 BUG_ON(fs_info->balance_ctl);
4214 spin_lock(&fs_info->balance_lock);
4215 fs_info->balance_ctl = bctl;
4216 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004217 } else {
4218 BUG_ON(ret != -EEXIST);
4219 spin_lock(&fs_info->balance_lock);
4220 update_balance_args(bctl);
4221 spin_unlock(&fs_info->balance_lock);
4222 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004223
David Sterba3009a622018-03-21 01:31:04 +01004224 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4225 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004226 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004227 mutex_unlock(&fs_info->balance_mutex);
4228
4229 ret = __btrfs_balance(fs_info);
4230
4231 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004232 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4233 btrfs_info(fs_info, "balance: paused");
4234 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4235 btrfs_info(fs_info, "balance: canceled");
4236 else
4237 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4238
David Sterba3009a622018-03-21 01:31:04 +01004239 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004240
4241 if (bargs) {
4242 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004243 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004244 }
4245
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004246 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4247 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004248 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004249 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004250 }
4251
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004252 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004253
4254 return ret;
4255out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004256 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004257 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004258 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004259 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004260 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4261
Ilya Dryomov59641012012-01-16 22:04:48 +02004262 return ret;
4263}
4264
4265static int balance_kthread(void *data)
4266{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004267 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004268 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004269
Ilya Dryomov59641012012-01-16 22:04:48 +02004270 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004271 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004272 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004273 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004274
Ilya Dryomov59641012012-01-16 22:04:48 +02004275 return ret;
4276}
4277
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004278int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4279{
4280 struct task_struct *tsk;
4281
David Sterba1354e1a2018-03-21 02:29:13 +01004282 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004283 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004284 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004285 return 0;
4286 }
David Sterba1354e1a2018-03-21 02:29:13 +01004287 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004288
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004289 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004290 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004291 return 0;
4292 }
4293
Anand Jain02ee6542018-05-17 15:16:51 +08004294 /*
4295 * A ro->rw remount sequence should continue with the paused balance
4296 * regardless of who pauses it, system or the user as of now, so set
4297 * the resume flag.
4298 */
4299 spin_lock(&fs_info->balance_lock);
4300 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4301 spin_unlock(&fs_info->balance_lock);
4302
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004303 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304304 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004305}
4306
Ilya Dryomov68310a52012-06-22 12:24:12 -06004307int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004308{
Ilya Dryomov59641012012-01-16 22:04:48 +02004309 struct btrfs_balance_control *bctl;
4310 struct btrfs_balance_item *item;
4311 struct btrfs_disk_balance_args disk_bargs;
4312 struct btrfs_path *path;
4313 struct extent_buffer *leaf;
4314 struct btrfs_key key;
4315 int ret;
4316
4317 path = btrfs_alloc_path();
4318 if (!path)
4319 return -ENOMEM;
4320
Ilya Dryomov68310a52012-06-22 12:24:12 -06004321 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004322 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004323 key.offset = 0;
4324
4325 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4326 if (ret < 0)
4327 goto out;
4328 if (ret > 0) { /* ret = -ENOENT; */
4329 ret = 0;
4330 goto out;
4331 }
4332
Ilya Dryomov59641012012-01-16 22:04:48 +02004333 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4334 if (!bctl) {
4335 ret = -ENOMEM;
4336 goto out;
4337 }
4338
Ilya Dryomov59641012012-01-16 22:04:48 +02004339 leaf = path->nodes[0];
4340 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4341
Ilya Dryomov68310a52012-06-22 12:24:12 -06004342 bctl->flags = btrfs_balance_flags(leaf, item);
4343 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004344
4345 btrfs_balance_data(leaf, item, &disk_bargs);
4346 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4347 btrfs_balance_meta(leaf, item, &disk_bargs);
4348 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4349 btrfs_balance_sys(leaf, item, &disk_bargs);
4350 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4351
David Sterbaeee95e32018-03-20 20:07:58 +01004352 /*
4353 * This should never happen, as the paused balance state is recovered
4354 * during mount without any chance of other exclusive ops to collide.
4355 *
4356 * This gives the exclusive op status to balance and keeps in paused
4357 * state until user intervention (cancel or umount). If the ownership
4358 * cannot be assigned, show a message but do not fail. The balance
4359 * is in a paused state and must have fs_info::balance_ctl properly
4360 * set up.
4361 */
4362 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4363 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004364 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004365
Ilya Dryomov68310a52012-06-22 12:24:12 -06004366 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004367 BUG_ON(fs_info->balance_ctl);
4368 spin_lock(&fs_info->balance_lock);
4369 fs_info->balance_ctl = bctl;
4370 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004371 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004372out:
4373 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004374 return ret;
4375}
4376
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004377int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4378{
4379 int ret = 0;
4380
4381 mutex_lock(&fs_info->balance_mutex);
4382 if (!fs_info->balance_ctl) {
4383 mutex_unlock(&fs_info->balance_mutex);
4384 return -ENOTCONN;
4385 }
4386
David Sterba3009a622018-03-21 01:31:04 +01004387 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004388 atomic_inc(&fs_info->balance_pause_req);
4389 mutex_unlock(&fs_info->balance_mutex);
4390
4391 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004392 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004393
4394 mutex_lock(&fs_info->balance_mutex);
4395 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004396 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004397 atomic_dec(&fs_info->balance_pause_req);
4398 } else {
4399 ret = -ENOTCONN;
4400 }
4401
4402 mutex_unlock(&fs_info->balance_mutex);
4403 return ret;
4404}
4405
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004406int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4407{
4408 mutex_lock(&fs_info->balance_mutex);
4409 if (!fs_info->balance_ctl) {
4410 mutex_unlock(&fs_info->balance_mutex);
4411 return -ENOTCONN;
4412 }
4413
David Sterbacf7d20f2018-03-21 01:45:32 +01004414 /*
4415 * A paused balance with the item stored on disk can be resumed at
4416 * mount time if the mount is read-write. Otherwise it's still paused
4417 * and we must not allow cancelling as it deletes the item.
4418 */
4419 if (sb_rdonly(fs_info->sb)) {
4420 mutex_unlock(&fs_info->balance_mutex);
4421 return -EROFS;
4422 }
4423
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004424 atomic_inc(&fs_info->balance_cancel_req);
4425 /*
4426 * if we are running just wait and return, balance item is
4427 * deleted in btrfs_balance in this case
4428 */
David Sterba3009a622018-03-21 01:31:04 +01004429 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004430 mutex_unlock(&fs_info->balance_mutex);
4431 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004432 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004433 mutex_lock(&fs_info->balance_mutex);
4434 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004435 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004436 /*
4437 * Lock released to allow other waiters to continue, we'll
4438 * reexamine the status again.
4439 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004440 mutex_lock(&fs_info->balance_mutex);
4441
David Sterbaa17c95d2018-03-20 17:28:05 +01004442 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004443 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004444 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004445 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004446 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004447 }
4448
David Sterba3009a622018-03-21 01:31:04 +01004449 BUG_ON(fs_info->balance_ctl ||
4450 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004451 atomic_dec(&fs_info->balance_cancel_req);
4452 mutex_unlock(&fs_info->balance_mutex);
4453 return 0;
4454}
4455
Stefan Behrens803b2f52013-08-15 17:11:21 +02004456static int btrfs_uuid_scan_kthread(void *data)
4457{
4458 struct btrfs_fs_info *fs_info = data;
4459 struct btrfs_root *root = fs_info->tree_root;
4460 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004461 struct btrfs_path *path = NULL;
4462 int ret = 0;
4463 struct extent_buffer *eb;
4464 int slot;
4465 struct btrfs_root_item root_item;
4466 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004467 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004468
4469 path = btrfs_alloc_path();
4470 if (!path) {
4471 ret = -ENOMEM;
4472 goto out;
4473 }
4474
4475 key.objectid = 0;
4476 key.type = BTRFS_ROOT_ITEM_KEY;
4477 key.offset = 0;
4478
Stefan Behrens803b2f52013-08-15 17:11:21 +02004479 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004480 ret = btrfs_search_forward(root, &key, path,
4481 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004482 if (ret) {
4483 if (ret > 0)
4484 ret = 0;
4485 break;
4486 }
4487
4488 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4489 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4490 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4491 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4492 goto skip;
4493
4494 eb = path->nodes[0];
4495 slot = path->slots[0];
4496 item_size = btrfs_item_size_nr(eb, slot);
4497 if (item_size < sizeof(root_item))
4498 goto skip;
4499
Stefan Behrens803b2f52013-08-15 17:11:21 +02004500 read_extent_buffer(eb, &root_item,
4501 btrfs_item_ptr_offset(eb, slot),
4502 (int)sizeof(root_item));
4503 if (btrfs_root_refs(&root_item) == 0)
4504 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004505
4506 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4507 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4508 if (trans)
4509 goto update_tree;
4510
4511 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004512 /*
4513 * 1 - subvol uuid item
4514 * 1 - received_subvol uuid item
4515 */
4516 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4517 if (IS_ERR(trans)) {
4518 ret = PTR_ERR(trans);
4519 break;
4520 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004521 continue;
4522 } else {
4523 goto skip;
4524 }
4525update_tree:
4526 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004527 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004528 BTRFS_UUID_KEY_SUBVOL,
4529 key.objectid);
4530 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004531 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004532 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004533 break;
4534 }
4535 }
4536
4537 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004538 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004539 root_item.received_uuid,
4540 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4541 key.objectid);
4542 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004543 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004544 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004545 break;
4546 }
4547 }
4548
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004549skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004550 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004551 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004552 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004553 if (ret)
4554 break;
4555 }
4556
Stefan Behrens803b2f52013-08-15 17:11:21 +02004557 btrfs_release_path(path);
4558 if (key.offset < (u64)-1) {
4559 key.offset++;
4560 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4561 key.offset = 0;
4562 key.type = BTRFS_ROOT_ITEM_KEY;
4563 } else if (key.objectid < (u64)-1) {
4564 key.offset = 0;
4565 key.type = BTRFS_ROOT_ITEM_KEY;
4566 key.objectid++;
4567 } else {
4568 break;
4569 }
4570 cond_resched();
4571 }
4572
4573out:
4574 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004575 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004576 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004577 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004578 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004579 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004580 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004581 up(&fs_info->uuid_tree_rescan_sem);
4582 return 0;
4583}
4584
Stefan Behrens70f80172013-08-15 17:11:23 +02004585/*
4586 * Callback for btrfs_uuid_tree_iterate().
4587 * returns:
4588 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004589 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004590 * > 0 if the check failed, which means the caller shall remove the entry.
4591 */
4592static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4593 u8 *uuid, u8 type, u64 subid)
4594{
4595 struct btrfs_key key;
4596 int ret = 0;
4597 struct btrfs_root *subvol_root;
4598
4599 if (type != BTRFS_UUID_KEY_SUBVOL &&
4600 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4601 goto out;
4602
4603 key.objectid = subid;
4604 key.type = BTRFS_ROOT_ITEM_KEY;
4605 key.offset = (u64)-1;
4606 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4607 if (IS_ERR(subvol_root)) {
4608 ret = PTR_ERR(subvol_root);
4609 if (ret == -ENOENT)
4610 ret = 1;
4611 goto out;
4612 }
4613
4614 switch (type) {
4615 case BTRFS_UUID_KEY_SUBVOL:
4616 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4617 ret = 1;
4618 break;
4619 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4620 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4621 BTRFS_UUID_SIZE))
4622 ret = 1;
4623 break;
4624 }
4625
4626out:
4627 return ret;
4628}
4629
4630static int btrfs_uuid_rescan_kthread(void *data)
4631{
4632 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4633 int ret;
4634
4635 /*
4636 * 1st step is to iterate through the existing UUID tree and
4637 * to delete all entries that contain outdated data.
4638 * 2nd step is to add all missing entries to the UUID tree.
4639 */
4640 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4641 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004642 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004643 up(&fs_info->uuid_tree_rescan_sem);
4644 return ret;
4645 }
4646 return btrfs_uuid_scan_kthread(data);
4647}
4648
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004649int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4650{
4651 struct btrfs_trans_handle *trans;
4652 struct btrfs_root *tree_root = fs_info->tree_root;
4653 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004654 struct task_struct *task;
4655 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004656
4657 /*
4658 * 1 - root node
4659 * 1 - root item
4660 */
4661 trans = btrfs_start_transaction(tree_root, 2);
4662 if (IS_ERR(trans))
4663 return PTR_ERR(trans);
4664
4665 uuid_root = btrfs_create_tree(trans, fs_info,
4666 BTRFS_UUID_TREE_OBJECTID);
4667 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004668 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004669 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004670 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004671 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004672 }
4673
4674 fs_info->uuid_root = uuid_root;
4675
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004676 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004677 if (ret)
4678 return ret;
4679
4680 down(&fs_info->uuid_tree_rescan_sem);
4681 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4682 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004683 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004684 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004685 up(&fs_info->uuid_tree_rescan_sem);
4686 return PTR_ERR(task);
4687 }
4688
4689 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004690}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004691
Stefan Behrens70f80172013-08-15 17:11:23 +02004692int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4693{
4694 struct task_struct *task;
4695
4696 down(&fs_info->uuid_tree_rescan_sem);
4697 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4698 if (IS_ERR(task)) {
4699 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004700 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004701 up(&fs_info->uuid_tree_rescan_sem);
4702 return PTR_ERR(task);
4703 }
4704
4705 return 0;
4706}
4707
Chris Mason8f18cf12008-04-25 16:53:30 -04004708/*
4709 * shrinking a device means finding all of the device extents past
4710 * the new size, and then following the back refs to the chunks.
4711 * The chunk relocation code actually frees the device extent
4712 */
4713int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4714{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004715 struct btrfs_fs_info *fs_info = device->fs_info;
4716 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004717 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004718 struct btrfs_dev_extent *dev_extent = NULL;
4719 struct btrfs_path *path;
4720 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004721 u64 chunk_offset;
4722 int ret;
4723 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004724 int failed = 0;
4725 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004726 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004727 struct extent_buffer *l;
4728 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004729 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004730 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004731 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004732 u64 diff;
4733
4734 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004735 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004736
Anand Jain401e29c2017-12-04 12:54:55 +08004737 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004738 return -EINVAL;
4739
Chris Mason8f18cf12008-04-25 16:53:30 -04004740 path = btrfs_alloc_path();
4741 if (!path)
4742 return -ENOMEM;
4743
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004744 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004745
David Sterba34441362016-10-04 19:34:27 +02004746 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004747
Miao Xie7cc8e582014-09-03 21:35:38 +08004748 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004749 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004750 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004751 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004752 }
David Sterba34441362016-10-04 19:34:27 +02004753 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004754
Josef Bacikba1bf482009-09-11 16:11:19 -04004755again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004756 key.objectid = device->devid;
4757 key.offset = (u64)-1;
4758 key.type = BTRFS_DEV_EXTENT_KEY;
4759
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004760 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004761 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004762 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004763 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004764 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004765 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004766 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004767
4768 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004769 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004770 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004771 if (ret < 0)
4772 goto done;
4773 if (ret) {
4774 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004775 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004776 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004777 }
4778
4779 l = path->nodes[0];
4780 slot = path->slots[0];
4781 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4782
Josef Bacikba1bf482009-09-11 16:11:19 -04004783 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004784 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004785 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004786 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004787 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004788
4789 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4790 length = btrfs_dev_extent_length(l, dev_extent);
4791
Josef Bacikba1bf482009-09-11 16:11:19 -04004792 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004793 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004794 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004795 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004796 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004797
Chris Mason8f18cf12008-04-25 16:53:30 -04004798 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004799 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004800
Liu Boa6f93c72017-11-15 16:28:11 -07004801 /*
4802 * We may be relocating the only data chunk we have,
4803 * which could potentially end up with losing data's
4804 * raid profile, so lets allocate an empty one in
4805 * advance.
4806 */
4807 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4808 if (ret < 0) {
4809 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4810 goto done;
4811 }
4812
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004813 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4814 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004815 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004816 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004817 } else if (ret) {
4818 if (ret == -ETXTBSY) {
4819 btrfs_warn(fs_info,
4820 "could not shrink block group %llu due to active swapfile",
4821 chunk_offset);
4822 }
4823 goto done;
4824 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004825 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004826
4827 if (failed && !retried) {
4828 failed = 0;
4829 retried = true;
4830 goto again;
4831 } else if (failed && retried) {
4832 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004833 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004834 }
4835
Chris Balld6397ba2009-04-27 07:29:03 -04004836 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004837 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004838 if (IS_ERR(trans)) {
4839 ret = PTR_ERR(trans);
4840 goto done;
4841 }
4842
David Sterba34441362016-10-04 19:34:27 +02004843 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004844
4845 /*
4846 * We checked in the above loop all device extents that were already in
4847 * the device tree. However before we have updated the device's
4848 * total_bytes to the new size, we might have had chunk allocations that
4849 * have not complete yet (new block groups attached to transaction
4850 * handles), and therefore their device extents were not yet in the
4851 * device tree and we missed them in the loop above. So if we have any
4852 * pending chunk using a device extent that overlaps the device range
4853 * that we can not use anymore, commit the current transaction and
4854 * repeat the search on the device tree - this way we guarantee we will
4855 * not have chunks using device extents that end beyond 'new_size'.
4856 */
4857 if (!checked_pending_chunks) {
4858 u64 start = new_size;
4859 u64 len = old_size - new_size;
4860
Jeff Mahoney499f3772015-06-15 09:41:17 -04004861 if (contains_pending_extent(trans->transaction, device,
4862 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004863 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004864 checked_pending_chunks = true;
4865 failed = 0;
4866 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004867 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004868 if (ret)
4869 goto done;
4870 goto again;
4871 }
4872 }
4873
Miao Xie7cc8e582014-09-03 21:35:38 +08004874 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004875 if (list_empty(&device->resized_list))
4876 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004877 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004878
Chris Balld6397ba2009-04-27 07:29:03 -04004879 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004880 btrfs_set_super_total_bytes(super_copy,
4881 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004882 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004883
4884 /* Now btrfs_update_device() will change the on-disk size. */
4885 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004886 if (ret < 0) {
4887 btrfs_abort_transaction(trans, ret);
4888 btrfs_end_transaction(trans);
4889 } else {
4890 ret = btrfs_commit_transaction(trans);
4891 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004892done:
4893 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004894 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004895 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004896 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004897 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004898 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004899 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004900 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004901 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004902 return ret;
4903}
4904
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004905static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004906 struct btrfs_key *key,
4907 struct btrfs_chunk *chunk, int item_size)
4908{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004909 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004910 struct btrfs_disk_key disk_key;
4911 u32 array_size;
4912 u8 *ptr;
4913
David Sterba34441362016-10-04 19:34:27 +02004914 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004915 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004916 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004917 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004918 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004919 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004920 }
Chris Mason0b86a832008-03-24 15:01:56 -04004921
4922 ptr = super_copy->sys_chunk_array + array_size;
4923 btrfs_cpu_key_to_disk(&disk_key, key);
4924 memcpy(ptr, &disk_key, sizeof(disk_key));
4925 ptr += sizeof(disk_key);
4926 memcpy(ptr, chunk, item_size);
4927 item_size += sizeof(disk_key);
4928 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004929 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004930
Chris Mason0b86a832008-03-24 15:01:56 -04004931 return 0;
4932}
4933
Miao Xieb2117a32011-01-05 10:07:28 +00004934/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004935 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004936 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004937static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004938{
Arne Jansen73c5de02011-04-12 12:07:57 +02004939 const struct btrfs_device_info *di_a = a;
4940 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004941
Arne Jansen73c5de02011-04-12 12:07:57 +02004942 if (di_a->max_avail > di_b->max_avail)
4943 return -1;
4944 if (di_a->max_avail < di_b->max_avail)
4945 return 1;
4946 if (di_a->total_avail > di_b->total_avail)
4947 return -1;
4948 if (di_a->total_avail < di_b->total_avail)
4949 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004950 return 0;
4951}
4952
David Woodhouse53b381b2013-01-29 18:40:14 -05004953static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4954{
Zhao Leiffe2d202015-01-20 15:11:44 +08004955 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004956 return;
4957
Miao Xieceda0862013-04-11 10:30:16 +00004958 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004959}
4960
Qu Wenruo062d4d12018-01-30 18:20:46 +08004961#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004962 - sizeof(struct btrfs_chunk)) \
4963 / sizeof(struct btrfs_stripe) + 1)
4964
4965#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4966 - 2 * sizeof(struct btrfs_disk_key) \
4967 - 2 * sizeof(struct btrfs_chunk)) \
4968 / sizeof(struct btrfs_stripe) + 1)
4969
Miao Xieb2117a32011-01-05 10:07:28 +00004970static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004971 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004972{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004973 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004974 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004975 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004976 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004977 struct extent_map_tree *em_tree;
4978 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004979 struct btrfs_device_info *devices_info = NULL;
4980 u64 total_avail;
4981 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004982 int data_stripes; /* number of stripes that count for
4983 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004984 int sub_stripes; /* sub_stripes info for map */
4985 int dev_stripes; /* stripes per dev */
4986 int devs_max; /* max devs to use */
4987 int devs_min; /* min devs needed */
4988 int devs_increment; /* ndevs has to be a multiple of this */
4989 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004990 int nparity; /* number of stripes worth of bytes to
4991 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00004992 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004993 u64 max_stripe_size;
4994 u64 max_chunk_size;
4995 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004996 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004997 int ndevs;
4998 int i;
4999 int j;
Liu Bo31e50222012-11-21 14:18:10 +00005000 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00005001
Ilya Dryomov0c460c02012-03-27 17:09:17 +03005002 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02005003
Qu Wenruo4117f202018-01-22 13:50:54 +08005004 if (list_empty(&fs_devices->alloc_list)) {
5005 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5006 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005007 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005008 }
Miao Xieb2117a32011-01-05 10:07:28 +00005009
Qu Wenruo3e72ee82018-01-30 18:20:45 +08005010 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02005011
Liu Bo31e50222012-11-21 14:18:10 +00005012 sub_stripes = btrfs_raid_array[index].sub_stripes;
5013 dev_stripes = btrfs_raid_array[index].dev_stripes;
5014 devs_max = btrfs_raid_array[index].devs_max;
5015 devs_min = btrfs_raid_array[index].devs_min;
5016 devs_increment = btrfs_raid_array[index].devs_increment;
5017 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005018 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02005019
5020 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09005021 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08005022 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005023 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005024 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005025 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05005026 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09005027 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
5028 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05005029 else
Byongho Leeee221842015-12-15 01:42:10 +09005030 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005031 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005032 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08005033 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02005034 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09005035 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02005036 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08005037 if (!devs_max)
5038 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02005039 } else {
David Sterba351fd352014-05-15 16:48:20 +02005040 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02005041 type);
Arnd Bergmann290342f2019-03-25 14:02:25 +01005042 BUG();
Arne Jansen73c5de02011-04-12 12:07:57 +02005043 }
5044
Andrea Gelmini52042d82018-11-28 12:05:13 +01005045 /* We don't want a chunk larger than 10% of writable space */
Arne Jansen73c5de02011-04-12 12:07:57 +02005046 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
5047 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00005048
David Sterba31e818f2015-02-20 18:00:26 +01005049 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005050 GFP_NOFS);
5051 if (!devices_info)
5052 return -ENOMEM;
5053
Arne Jansen73c5de02011-04-12 12:07:57 +02005054 /*
5055 * in the first pass through the devices list, we gather information
5056 * about the available holes on each device.
5057 */
5058 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03005059 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005060 u64 max_avail;
5061 u64 dev_offset;
5062
Anand Jainebbede42017-12-04 12:54:52 +08005063 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005064 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05005065 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02005066 continue;
5067 }
5068
Anand Jaine12c9622017-12-04 12:54:53 +08005069 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5070 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08005071 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02005072 continue;
5073
5074 if (device->total_bytes > device->bytes_used)
5075 total_avail = device->total_bytes - device->bytes_used;
5076 else
5077 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00005078
5079 /* If there is no space on this device, skip it. */
5080 if (total_avail == 0)
5081 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02005082
Josef Bacik6df9a952013-06-27 13:22:46 -04005083 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02005084 max_stripe_size * dev_stripes,
5085 &dev_offset, &max_avail);
5086 if (ret && ret != -ENOSPC)
5087 goto error;
5088
5089 if (ret == 0)
5090 max_avail = max_stripe_size * dev_stripes;
5091
Qu Wenruo4117f202018-01-22 13:50:54 +08005092 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5093 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5094 btrfs_debug(info,
5095 "%s: devid %llu has no free space, have=%llu want=%u",
5096 __func__, device->devid, max_avail,
5097 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005098 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08005099 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005100
Eric Sandeen063d0062013-01-31 00:55:01 +00005101 if (ndevs == fs_devices->rw_devices) {
5102 WARN(1, "%s: found more than %llu devices\n",
5103 __func__, fs_devices->rw_devices);
5104 break;
5105 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005106 devices_info[ndevs].dev_offset = dev_offset;
5107 devices_info[ndevs].max_avail = max_avail;
5108 devices_info[ndevs].total_avail = total_avail;
5109 devices_info[ndevs].dev = device;
5110 ++ndevs;
5111 }
5112
5113 /*
5114 * now sort the devices by hole size / available space
5115 */
5116 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5117 btrfs_cmp_device_info, NULL);
5118
5119 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03005120 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02005121
Qu Wenruoba89b802018-01-31 13:56:15 +08005122 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005123 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005124 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5125 btrfs_debug(info,
5126 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08005127 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08005128 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005129 goto error;
5130 }
5131
Nikolay Borisovf148ef42017-06-27 10:02:26 +03005132 ndevs = min(ndevs, devs_max);
5133
Arne Jansen73c5de02011-04-12 12:07:57 +02005134 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005135 * The primary goal is to maximize the number of stripes, so use as
5136 * many devices as possible, even if the stripes are not maximum sized.
5137 *
5138 * The DUP profile stores more than one stripe per device, the
5139 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02005140 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005141 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005142 num_stripes = ndevs * dev_stripes;
5143
David Woodhouse53b381b2013-01-29 18:40:14 -05005144 /*
5145 * this will have to be fixed for RAID1 and RAID10 over
5146 * more drives
5147 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005148 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05005149
5150 /*
5151 * Use the number of data stripes to figure out how big this chunk
5152 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005153 * and compare that answer with the max chunk size. If it's higher,
5154 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05005155 */
5156 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005157 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005158 * Reduce stripe_size, round it up to a 16MB boundary again and
5159 * then use it, unless it ends up being even bigger than the
5160 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05005161 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005162 stripe_size = min(round_up(div_u64(max_chunk_size,
5163 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005164 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05005165 }
5166
Ilya Dryomov37db63a2012-04-13 17:05:08 +03005167 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005168 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02005169
Miao Xieb2117a32011-01-05 10:07:28 +00005170 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5171 if (!map) {
5172 ret = -ENOMEM;
5173 goto error;
5174 }
5175 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04005176
Arne Jansen73c5de02011-04-12 12:07:57 +02005177 for (i = 0; i < ndevs; ++i) {
5178 for (j = 0; j < dev_stripes; ++j) {
5179 int s = i * dev_stripes + j;
5180 map->stripes[s].dev = devices_info[i].dev;
5181 map->stripes[s].physical = devices_info[i].dev_offset +
5182 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04005183 }
Chris Mason6324fbf2008-03-24 15:01:59 -04005184 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005185 map->stripe_len = BTRFS_STRIPE_LEN;
5186 map->io_align = BTRFS_STRIPE_LEN;
5187 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04005188 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04005189 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005190
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005191 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005192
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005193 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005194
David Sterba172ddd62011-04-21 00:48:27 +02005195 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005196 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005197 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005198 ret = -ENOMEM;
5199 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005200 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005201 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005202 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005203 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005204 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005205 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005206 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005207 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005208
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005209 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005210 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005211 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005212 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005213 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005214 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005215 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005216 }
Yan Zheng2b820322008-11-17 21:11:30 -05005217
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005218 list_add_tail(&em->list, &trans->transaction->pending_chunks);
5219 refcount_inc(&em->refs);
5220 write_unlock(&em_tree->lock);
5221
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005222 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005223 if (ret)
5224 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005225
Hans van Kranenburg2f29df42018-10-04 23:24:38 +02005226 for (i = 0; i < map->num_stripes; i++)
5227 btrfs_device_set_bytes_used(map->stripes[i].dev,
5228 map->stripes[i].dev->bytes_used + stripe_size);
Miao Xie43530c42014-09-03 21:35:36 +08005229
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005230 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005231
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005232 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005233 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005234
Miao Xieb2117a32011-01-05 10:07:28 +00005235 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005236 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005237
Josef Bacik6df9a952013-06-27 13:22:46 -04005238error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005239 write_lock(&em_tree->lock);
5240 remove_extent_mapping(em_tree, em);
5241 write_unlock(&em_tree->lock);
5242
5243 /* One for our allocation */
5244 free_extent_map(em);
5245 /* One for the tree reference */
5246 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00005247 /* One for the pending_chunks list reference */
5248 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005249error:
Miao Xieb2117a32011-01-05 10:07:28 +00005250 kfree(devices_info);
5251 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005252}
5253
Josef Bacik6df9a952013-06-27 13:22:46 -04005254int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005255 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005256{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005257 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005258 struct btrfs_root *extent_root = fs_info->extent_root;
5259 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005260 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005261 struct btrfs_device *device;
5262 struct btrfs_chunk *chunk;
5263 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005264 struct extent_map *em;
5265 struct map_lookup *map;
5266 size_t item_size;
5267 u64 dev_offset;
5268 u64 stripe_size;
5269 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005270 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005271
Omar Sandoval60ca8422018-05-16 16:34:31 -07005272 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005273 if (IS_ERR(em))
5274 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005275
Jeff Mahoney95617d62015-06-03 10:55:48 -04005276 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005277 item_size = btrfs_chunk_item_size(map->num_stripes);
5278 stripe_size = em->orig_block_len;
5279
5280 chunk = kzalloc(item_size, GFP_NOFS);
5281 if (!chunk) {
5282 ret = -ENOMEM;
5283 goto out;
5284 }
5285
Filipe Manana50460e32015-11-20 10:42:47 +00005286 /*
5287 * Take the device list mutex to prevent races with the final phase of
5288 * a device replace operation that replaces the device object associated
5289 * with the map's stripes, because the device object's id can change
5290 * at any time during that final phase of the device replace operation
5291 * (dev-replace.c:btrfs_dev_replace_finishing()).
5292 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005293 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005294 for (i = 0; i < map->num_stripes; i++) {
5295 device = map->stripes[i].dev;
5296 dev_offset = map->stripes[i].physical;
5297
Yan Zheng2b820322008-11-17 21:11:30 -05005298 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005299 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005300 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005301 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5302 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005303 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005304 break;
5305 }
5306 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005307 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005308 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005309 }
5310
Yan Zheng2b820322008-11-17 21:11:30 -05005311 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005312 for (i = 0; i < map->num_stripes; i++) {
5313 device = map->stripes[i].dev;
5314 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005315
5316 btrfs_set_stack_stripe_devid(stripe, device->devid);
5317 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5318 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5319 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005320 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005321 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005322
5323 btrfs_set_stack_chunk_length(chunk, chunk_size);
5324 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5325 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5326 btrfs_set_stack_chunk_type(chunk, map->type);
5327 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5328 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5329 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005330 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005331 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5332
5333 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5334 key.type = BTRFS_CHUNK_ITEM_KEY;
5335 key.offset = chunk_offset;
5336
5337 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005338 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5339 /*
5340 * TODO: Cleanup of inserted chunk root in case of
5341 * failure.
5342 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005343 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005344 }
liubo1abe9b82011-03-24 11:18:59 +00005345
Josef Bacik6df9a952013-06-27 13:22:46 -04005346out:
Yan Zheng2b820322008-11-17 21:11:30 -05005347 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005348 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005349 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005350}
5351
5352/*
Andrea Gelmini52042d82018-11-28 12:05:13 +01005353 * Chunk allocation falls into two parts. The first part does work
5354 * that makes the new allocated chunk usable, but does not do any operation
5355 * that modifies the chunk tree. The second part does the work that
5356 * requires modifying the chunk tree. This division is important for the
Yan Zheng2b820322008-11-17 21:11:30 -05005357 * bootstrap process of adding storage to a seed btrfs.
5358 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005359int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005360{
5361 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005362
Nikolay Borisovc216b202018-06-20 15:49:06 +03005363 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5364 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005365 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005366}
5367
Chris Masond3977122009-01-05 21:25:51 -05005368static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005369 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005370{
5371 u64 chunk_offset;
5372 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005373 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005374 int ret;
5375
Josef Bacik6df9a952013-06-27 13:22:46 -04005376 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005377 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005378 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005379 if (ret)
5380 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005381
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005382 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005383 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005384 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005385 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005386}
5387
Miao Xied20983b2014-07-03 18:22:13 +08005388static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5389{
5390 int max_errors;
5391
5392 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5393 BTRFS_BLOCK_GROUP_RAID10 |
5394 BTRFS_BLOCK_GROUP_RAID5 |
5395 BTRFS_BLOCK_GROUP_DUP)) {
5396 max_errors = 1;
5397 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5398 max_errors = 2;
5399 } else {
5400 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005401 }
5402
Miao Xied20983b2014-07-03 18:22:13 +08005403 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005404}
5405
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005406int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005407{
5408 struct extent_map *em;
5409 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005410 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005411 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005412 int i;
5413
Omar Sandoval60ca8422018-05-16 16:34:31 -07005414 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005415 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005416 return 1;
5417
Jeff Mahoney95617d62015-06-03 10:55:48 -04005418 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005419 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005420 if (test_bit(BTRFS_DEV_STATE_MISSING,
5421 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005422 miss_ndevs++;
5423 continue;
5424 }
Anand Jainebbede42017-12-04 12:54:52 +08005425 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5426 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005427 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005428 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005429 }
5430 }
Miao Xied20983b2014-07-03 18:22:13 +08005431
5432 /*
5433 * If the number of missing devices is larger than max errors,
5434 * we can not write the data into that chunk successfully, so
5435 * set it readonly.
5436 */
5437 if (miss_ndevs > btrfs_chunk_max_errors(map))
5438 readonly = 1;
5439end:
Yan Zheng2b820322008-11-17 21:11:30 -05005440 free_extent_map(em);
5441 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005442}
5443
5444void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5445{
David Sterbaa8067e02011-04-21 00:34:43 +02005446 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005447}
5448
5449void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5450{
5451 struct extent_map *em;
5452
Chris Masond3977122009-01-05 21:25:51 -05005453 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005454 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005455 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5456 if (em)
5457 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005458 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005459 if (!em)
5460 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005461 /* once for us */
5462 free_extent_map(em);
5463 /* once for the tree */
5464 free_extent_map(em);
5465 }
5466}
5467
Stefan Behrens5d964052012-11-05 14:59:07 +01005468int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005469{
5470 struct extent_map *em;
5471 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005472 int ret;
5473
Omar Sandoval60ca8422018-05-16 16:34:31 -07005474 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005475 if (IS_ERR(em))
5476 /*
5477 * We could return errors for these cases, but that could get
5478 * ugly and we'd probably do the same thing which is just not do
5479 * anything else and exit, so return 1 so the callers don't try
5480 * to use other copies.
5481 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005482 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005483
Jeff Mahoney95617d62015-06-03 10:55:48 -04005484 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005485 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5486 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005487 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5488 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005489 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5490 ret = 2;
5491 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005492 /*
5493 * There could be two corrupted data stripes, we need
5494 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005495 *
Liu Bo8810f752018-01-02 13:36:41 -07005496 * Fail a stripe at a time on every retry except the
5497 * stripe under reconstruction.
5498 */
5499 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005500 else
5501 ret = 1;
5502 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005503
David Sterbacb5583d2018-09-07 16:11:23 +02005504 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005505 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5506 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005507 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005508 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005509
Chris Masonf1885912008-04-09 16:28:12 -04005510 return ret;
5511}
5512
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005513unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005514 u64 logical)
5515{
5516 struct extent_map *em;
5517 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005518 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005519
Omar Sandoval60ca8422018-05-16 16:34:31 -07005520 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005521
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005522 if (!WARN_ON(IS_ERR(em))) {
5523 map = em->map_lookup;
5524 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5525 len = map->stripe_len * nr_data_stripes(map);
5526 free_extent_map(em);
5527 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005528 return len;
5529}
5530
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005531int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005532{
5533 struct extent_map *em;
5534 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005535 int ret = 0;
5536
Omar Sandoval60ca8422018-05-16 16:34:31 -07005537 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005538
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005539 if(!WARN_ON(IS_ERR(em))) {
5540 map = em->map_lookup;
5541 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5542 ret = 1;
5543 free_extent_map(em);
5544 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005545 return ret;
5546}
5547
Stefan Behrens30d98612012-11-06 14:52:18 +01005548static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005549 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005550 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005551{
5552 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005553 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005554 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005555 int tolerance;
5556 struct btrfs_device *srcdev;
5557
Anand Jain99f92a72018-03-14 16:29:12 +08005558 ASSERT((map->type &
5559 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5560
5561 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5562 num_stripes = map->sub_stripes;
5563 else
5564 num_stripes = map->num_stripes;
5565
Anand Jain8ba0ae72018-03-14 16:29:13 +08005566 preferred_mirror = first + current->pid % num_stripes;
5567
Stefan Behrens30d98612012-11-06 14:52:18 +01005568 if (dev_replace_is_ongoing &&
5569 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5570 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5571 srcdev = fs_info->dev_replace.srcdev;
5572 else
5573 srcdev = NULL;
5574
5575 /*
5576 * try to avoid the drive that is the source drive for a
5577 * dev-replace procedure, only choose it if no other non-missing
5578 * mirror is available
5579 */
5580 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005581 if (map->stripes[preferred_mirror].dev->bdev &&
5582 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5583 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005584 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005585 if (map->stripes[i].dev->bdev &&
5586 (tolerance || map->stripes[i].dev != srcdev))
5587 return i;
5588 }
Chris Masondfe25022008-05-13 13:46:40 -04005589 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005590
Chris Masondfe25022008-05-13 13:46:40 -04005591 /* we couldn't find one that doesn't fail. Just return something
5592 * and the io error handling code will clean up eventually
5593 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005594 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005595}
5596
David Woodhouse53b381b2013-01-29 18:40:14 -05005597static inline int parity_smaller(u64 a, u64 b)
5598{
5599 return a > b;
5600}
5601
5602/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005603static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005604{
5605 struct btrfs_bio_stripe s;
5606 int i;
5607 u64 l;
5608 int again = 1;
5609
5610 while (again) {
5611 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005612 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005613 if (parity_smaller(bbio->raid_map[i],
5614 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005615 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005616 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005617 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005618 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005619 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005620 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005621
David Woodhouse53b381b2013-01-29 18:40:14 -05005622 again = 1;
5623 }
5624 }
5625 }
5626}
5627
Zhao Lei6e9606d2015-01-20 15:11:34 +08005628static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5629{
5630 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005631 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005632 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005633 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005634 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005635 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005636 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005637 /*
5638 * plus the raid_map, which includes both the tgt dev
5639 * and the stripes
5640 */
5641 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005642 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005643
5644 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005645 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005646
5647 return bbio;
5648}
5649
5650void btrfs_get_bbio(struct btrfs_bio *bbio)
5651{
Elena Reshetova140475a2017-03-03 10:55:10 +02005652 WARN_ON(!refcount_read(&bbio->refs));
5653 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005654}
5655
5656void btrfs_put_bbio(struct btrfs_bio *bbio)
5657{
5658 if (!bbio)
5659 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005660 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005661 kfree(bbio);
5662}
5663
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005664/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5665/*
5666 * Please note that, discard won't be sent to target device of device
5667 * replace.
5668 */
5669static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5670 u64 logical, u64 length,
5671 struct btrfs_bio **bbio_ret)
5672{
5673 struct extent_map *em;
5674 struct map_lookup *map;
5675 struct btrfs_bio *bbio;
5676 u64 offset;
5677 u64 stripe_nr;
5678 u64 stripe_nr_end;
5679 u64 stripe_end_offset;
5680 u64 stripe_cnt;
5681 u64 stripe_len;
5682 u64 stripe_offset;
5683 u64 num_stripes;
5684 u32 stripe_index;
5685 u32 factor = 0;
5686 u32 sub_stripes = 0;
5687 u64 stripes_per_dev = 0;
5688 u32 remaining_stripes = 0;
5689 u32 last_stripe = 0;
5690 int ret = 0;
5691 int i;
5692
5693 /* discard always return a bbio */
5694 ASSERT(bbio_ret);
5695
Omar Sandoval60ca8422018-05-16 16:34:31 -07005696 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005697 if (IS_ERR(em))
5698 return PTR_ERR(em);
5699
5700 map = em->map_lookup;
5701 /* we don't discard raid56 yet */
5702 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5703 ret = -EOPNOTSUPP;
5704 goto out;
5705 }
5706
5707 offset = logical - em->start;
5708 length = min_t(u64, em->len - offset, length);
5709
5710 stripe_len = map->stripe_len;
5711 /*
5712 * stripe_nr counts the total number of stripes we have to stride
5713 * to get to this block
5714 */
5715 stripe_nr = div64_u64(offset, stripe_len);
5716
5717 /* stripe_offset is the offset of this block in its stripe */
5718 stripe_offset = offset - stripe_nr * stripe_len;
5719
5720 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005721 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005722 stripe_cnt = stripe_nr_end - stripe_nr;
5723 stripe_end_offset = stripe_nr_end * map->stripe_len -
5724 (offset + length);
5725 /*
5726 * after this, stripe_nr is the number of stripes on this
5727 * device we have to walk to find the data, and stripe_index is
5728 * the number of our device in the stripe array
5729 */
5730 num_stripes = 1;
5731 stripe_index = 0;
5732 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5733 BTRFS_BLOCK_GROUP_RAID10)) {
5734 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5735 sub_stripes = 1;
5736 else
5737 sub_stripes = map->sub_stripes;
5738
5739 factor = map->num_stripes / sub_stripes;
5740 num_stripes = min_t(u64, map->num_stripes,
5741 sub_stripes * stripe_cnt);
5742 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5743 stripe_index *= sub_stripes;
5744 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5745 &remaining_stripes);
5746 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5747 last_stripe *= sub_stripes;
5748 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5749 BTRFS_BLOCK_GROUP_DUP)) {
5750 num_stripes = map->num_stripes;
5751 } else {
5752 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5753 &stripe_index);
5754 }
5755
5756 bbio = alloc_btrfs_bio(num_stripes, 0);
5757 if (!bbio) {
5758 ret = -ENOMEM;
5759 goto out;
5760 }
5761
5762 for (i = 0; i < num_stripes; i++) {
5763 bbio->stripes[i].physical =
5764 map->stripes[stripe_index].physical +
5765 stripe_offset + stripe_nr * map->stripe_len;
5766 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5767
5768 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5769 BTRFS_BLOCK_GROUP_RAID10)) {
5770 bbio->stripes[i].length = stripes_per_dev *
5771 map->stripe_len;
5772
5773 if (i / sub_stripes < remaining_stripes)
5774 bbio->stripes[i].length +=
5775 map->stripe_len;
5776
5777 /*
5778 * Special for the first stripe and
5779 * the last stripe:
5780 *
5781 * |-------|...|-------|
5782 * |----------|
5783 * off end_off
5784 */
5785 if (i < sub_stripes)
5786 bbio->stripes[i].length -=
5787 stripe_offset;
5788
5789 if (stripe_index >= last_stripe &&
5790 stripe_index <= (last_stripe +
5791 sub_stripes - 1))
5792 bbio->stripes[i].length -=
5793 stripe_end_offset;
5794
5795 if (i == sub_stripes - 1)
5796 stripe_offset = 0;
5797 } else {
5798 bbio->stripes[i].length = length;
5799 }
5800
5801 stripe_index++;
5802 if (stripe_index == map->num_stripes) {
5803 stripe_index = 0;
5804 stripe_nr++;
5805 }
5806 }
5807
5808 *bbio_ret = bbio;
5809 bbio->map_type = map->type;
5810 bbio->num_stripes = num_stripes;
5811out:
5812 free_extent_map(em);
5813 return ret;
5814}
5815
Liu Bo5ab56092017-03-14 13:33:57 -07005816/*
5817 * In dev-replace case, for repair case (that's the only case where the mirror
5818 * is selected explicitly when calling btrfs_map_block), blocks left of the
5819 * left cursor can also be read from the target drive.
5820 *
5821 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5822 * array of stripes.
5823 * For READ, it also needs to be supported using the same mirror number.
5824 *
5825 * If the requested block is not left of the left cursor, EIO is returned. This
5826 * can happen because btrfs_num_copies() returns one more in the dev-replace
5827 * case.
5828 */
5829static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5830 u64 logical, u64 length,
5831 u64 srcdev_devid, int *mirror_num,
5832 u64 *physical)
5833{
5834 struct btrfs_bio *bbio = NULL;
5835 int num_stripes;
5836 int index_srcdev = 0;
5837 int found = 0;
5838 u64 physical_of_found = 0;
5839 int i;
5840 int ret = 0;
5841
5842 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5843 logical, &length, &bbio, 0, 0);
5844 if (ret) {
5845 ASSERT(bbio == NULL);
5846 return ret;
5847 }
5848
5849 num_stripes = bbio->num_stripes;
5850 if (*mirror_num > num_stripes) {
5851 /*
5852 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5853 * that means that the requested area is not left of the left
5854 * cursor
5855 */
5856 btrfs_put_bbio(bbio);
5857 return -EIO;
5858 }
5859
5860 /*
5861 * process the rest of the function using the mirror_num of the source
5862 * drive. Therefore look it up first. At the end, patch the device
5863 * pointer to the one of the target drive.
5864 */
5865 for (i = 0; i < num_stripes; i++) {
5866 if (bbio->stripes[i].dev->devid != srcdev_devid)
5867 continue;
5868
5869 /*
5870 * In case of DUP, in order to keep it simple, only add the
5871 * mirror with the lowest physical address
5872 */
5873 if (found &&
5874 physical_of_found <= bbio->stripes[i].physical)
5875 continue;
5876
5877 index_srcdev = i;
5878 found = 1;
5879 physical_of_found = bbio->stripes[i].physical;
5880 }
5881
5882 btrfs_put_bbio(bbio);
5883
5884 ASSERT(found);
5885 if (!found)
5886 return -EIO;
5887
5888 *mirror_num = index_srcdev + 1;
5889 *physical = physical_of_found;
5890 return ret;
5891}
5892
Liu Bo73c0f222017-03-14 13:33:58 -07005893static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5894 struct btrfs_bio **bbio_ret,
5895 struct btrfs_dev_replace *dev_replace,
5896 int *num_stripes_ret, int *max_errors_ret)
5897{
5898 struct btrfs_bio *bbio = *bbio_ret;
5899 u64 srcdev_devid = dev_replace->srcdev->devid;
5900 int tgtdev_indexes = 0;
5901 int num_stripes = *num_stripes_ret;
5902 int max_errors = *max_errors_ret;
5903 int i;
5904
5905 if (op == BTRFS_MAP_WRITE) {
5906 int index_where_to_add;
5907
5908 /*
5909 * duplicate the write operations while the dev replace
5910 * procedure is running. Since the copying of the old disk to
5911 * the new disk takes place at run time while the filesystem is
5912 * mounted writable, the regular write operations to the old
5913 * disk have to be duplicated to go to the new disk as well.
5914 *
5915 * Note that device->missing is handled by the caller, and that
5916 * the write to the old disk is already set up in the stripes
5917 * array.
5918 */
5919 index_where_to_add = num_stripes;
5920 for (i = 0; i < num_stripes; i++) {
5921 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5922 /* write to new disk, too */
5923 struct btrfs_bio_stripe *new =
5924 bbio->stripes + index_where_to_add;
5925 struct btrfs_bio_stripe *old =
5926 bbio->stripes + i;
5927
5928 new->physical = old->physical;
5929 new->length = old->length;
5930 new->dev = dev_replace->tgtdev;
5931 bbio->tgtdev_map[i] = index_where_to_add;
5932 index_where_to_add++;
5933 max_errors++;
5934 tgtdev_indexes++;
5935 }
5936 }
5937 num_stripes = index_where_to_add;
5938 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5939 int index_srcdev = 0;
5940 int found = 0;
5941 u64 physical_of_found = 0;
5942
5943 /*
5944 * During the dev-replace procedure, the target drive can also
5945 * be used to read data in case it is needed to repair a corrupt
5946 * block elsewhere. This is possible if the requested area is
5947 * left of the left cursor. In this area, the target drive is a
5948 * full copy of the source drive.
5949 */
5950 for (i = 0; i < num_stripes; i++) {
5951 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5952 /*
5953 * In case of DUP, in order to keep it simple,
5954 * only add the mirror with the lowest physical
5955 * address
5956 */
5957 if (found &&
5958 physical_of_found <=
5959 bbio->stripes[i].physical)
5960 continue;
5961 index_srcdev = i;
5962 found = 1;
5963 physical_of_found = bbio->stripes[i].physical;
5964 }
5965 }
5966 if (found) {
5967 struct btrfs_bio_stripe *tgtdev_stripe =
5968 bbio->stripes + num_stripes;
5969
5970 tgtdev_stripe->physical = physical_of_found;
5971 tgtdev_stripe->length =
5972 bbio->stripes[index_srcdev].length;
5973 tgtdev_stripe->dev = dev_replace->tgtdev;
5974 bbio->tgtdev_map[index_srcdev] = num_stripes;
5975
5976 tgtdev_indexes++;
5977 num_stripes++;
5978 }
5979 }
5980
5981 *num_stripes_ret = num_stripes;
5982 *max_errors_ret = max_errors;
5983 bbio->num_tgtdevs = tgtdev_indexes;
5984 *bbio_ret = bbio;
5985}
5986
Liu Bo2b19a1f2017-03-14 13:34:00 -07005987static bool need_full_stripe(enum btrfs_map_op op)
5988{
5989 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5990}
5991
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005992static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5993 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005994 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005995 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005996 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005997{
5998 struct extent_map *em;
5999 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04006000 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04006001 u64 stripe_offset;
6002 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006003 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006004 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04006005 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006006 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006007 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006008 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006009 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006010 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006011 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6012 int dev_replace_is_ongoing = 0;
6013 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006014 int patch_the_first_stripe_for_dev_replace = 0;
6015 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006016 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04006017
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006018 if (op == BTRFS_MAP_DISCARD)
6019 return __btrfs_map_block_for_discard(fs_info, logical,
6020 *length, bbio_ret);
6021
Omar Sandoval60ca8422018-05-16 16:34:31 -07006022 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07006023 if (IS_ERR(em))
6024 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04006025
Jeff Mahoney95617d62015-06-03 10:55:48 -04006026 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04006027 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04006028
David Woodhouse53b381b2013-01-29 18:40:14 -05006029 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04006030 stripe_nr = offset;
6031 /*
6032 * stripe_nr counts the total number of stripes we have to stride
6033 * to get to this block
6034 */
David Sterba47c57132015-02-20 18:43:47 +01006035 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04006036
David Woodhouse53b381b2013-01-29 18:40:14 -05006037 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04006038 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006039 btrfs_crit(fs_info,
6040 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04006041 stripe_offset, offset, em->start, logical,
6042 stripe_len);
6043 free_extent_map(em);
6044 return -EINVAL;
6045 }
Chris Mason593060d2008-03-25 16:50:33 -04006046
6047 /* stripe_offset is the offset of this block in its stripe*/
6048 stripe_offset = offset - stripe_offset;
6049
David Woodhouse53b381b2013-01-29 18:40:14 -05006050 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08006051 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006052 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
6053 raid56_full_stripe_start = offset;
6054
6055 /* allow a write of a full stripe, but make sure we don't
6056 * allow straddling of stripes
6057 */
David Sterba47c57132015-02-20 18:43:47 +01006058 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
6059 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05006060 raid56_full_stripe_start *= full_stripe_len;
6061 }
6062
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006063 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006064 u64 max_len;
6065 /* For writes to RAID[56], allow a full stripeset across all disks.
6066 For other RAID types and for RAID[56] reads, just allow a single
6067 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08006068 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006069 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006070 max_len = stripe_len * nr_data_stripes(map) -
6071 (offset - raid56_full_stripe_start);
6072 } else {
6073 /* we limit the length of each bio to what fits in a stripe */
6074 max_len = stripe_len - stripe_offset;
6075 }
6076 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04006077 } else {
6078 *length = em->len - offset;
6079 }
Chris Masonf2d8d742008-04-21 10:03:05 -04006080
Nikolay Borisovda12fe52018-11-27 20:57:58 +02006081 /*
6082 * This is for when we're called from btrfs_bio_fits_in_stripe and all
6083 * it cares about is the length
6084 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006085 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04006086 goto out;
6087
David Sterbacb5583d2018-09-07 16:11:23 +02006088 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006089 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006090 /*
6091 * Hold the semaphore for read during the whole operation, write is
6092 * requested at commit time but must wait.
6093 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006094 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006095 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006096
Stefan Behrensad6d6202012-11-06 15:06:47 +01006097 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006098 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006099 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6100 dev_replace->srcdev->devid,
6101 &mirror_num,
6102 &physical_to_patch_in_first_stripe);
6103 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006104 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006105 else
6106 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006107 } else if (mirror_num > map->num_stripes) {
6108 mirror_num = 0;
6109 }
6110
Chris Masonf2d8d742008-04-21 10:03:05 -04006111 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006112 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006113 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006114 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6115 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006116 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006117 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006118 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08006119 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006120 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006121 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006122 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006123 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006124 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006125 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006126 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006127 }
Chris Mason2fff7342008-04-29 14:12:09 -04006128
Chris Mason611f0e02008-04-03 16:29:03 -04006129 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006130 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006131 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006132 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006133 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006134 } else {
6135 mirror_num = 1;
6136 }
Chris Mason2fff7342008-04-29 14:12:09 -04006137
Chris Mason321aecc2008-04-16 10:49:51 -04006138 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006139 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006140
David Sterba47c57132015-02-20 18:43:47 +01006141 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006142 stripe_index *= map->sub_stripes;
6143
Anand Jainde483732017-10-12 16:43:00 +08006144 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006145 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006146 else if (mirror_num)
6147 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006148 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006149 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006150 stripe_index = find_live_mirror(fs_info, map,
6151 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006152 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006153 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006154 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006155
Zhao Leiffe2d202015-01-20 15:11:44 +08006156 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006157 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006158 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006159 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01006160 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006161
6162 /* RAID[56] write or recovery. Return all stripes */
6163 num_stripes = map->num_stripes;
6164 max_errors = nr_parity_stripes(map);
6165
David Woodhouse53b381b2013-01-29 18:40:14 -05006166 *length = map->stripe_len;
6167 stripe_index = 0;
6168 stripe_offset = 0;
6169 } else {
6170 /*
6171 * Mirror #0 or #1 means the original data block.
6172 * Mirror #2 is RAID5 parity block.
6173 * Mirror #3 is RAID6 Q block.
6174 */
David Sterba47c57132015-02-20 18:43:47 +01006175 stripe_nr = div_u64_rem(stripe_nr,
6176 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006177 if (mirror_num > 1)
6178 stripe_index = nr_data_stripes(map) +
6179 mirror_num - 2;
6180
6181 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006182 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6183 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006184 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006185 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006186 }
Chris Mason8790d502008-04-03 16:29:03 -04006187 } else {
6188 /*
David Sterba47c57132015-02-20 18:43:47 +01006189 * after this, stripe_nr is the number of stripes on this
6190 * device we have to walk to find the data, and stripe_index is
6191 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006192 */
David Sterba47c57132015-02-20 18:43:47 +01006193 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6194 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006195 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006196 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006197 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006198 btrfs_crit(fs_info,
6199 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006200 stripe_index, map->num_stripes);
6201 ret = -EINVAL;
6202 goto out;
6203 }
Chris Mason593060d2008-03-25 16:50:33 -04006204
Stefan Behrens472262f2012-11-06 14:43:46 +01006205 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006206 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006207 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006208 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006209 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006210 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006211 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006212 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006213
Zhao Lei6e9606d2015-01-20 15:11:34 +08006214 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006215 if (!bbio) {
6216 ret = -ENOMEM;
6217 goto out;
6218 }
Liu Bo6fad8232017-03-14 13:33:59 -07006219 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006220 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006221
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006222 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006223 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6224 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006225 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006226 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006227
6228 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6229 sizeof(struct btrfs_bio_stripe) *
6230 num_alloc_stripes +
6231 sizeof(int) * tgtdev_indexes);
6232
6233 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006234 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006235
6236 /* Fill in the logical address of each stripe */
6237 tmp = stripe_nr * nr_data_stripes(map);
6238 for (i = 0; i < nr_data_stripes(map); i++)
6239 bbio->raid_map[(i+rot) % num_stripes] =
6240 em->start + (tmp + i) * map->stripe_len;
6241
6242 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6243 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6244 bbio->raid_map[(i+rot+1) % num_stripes] =
6245 RAID6_Q_STRIPE;
6246 }
6247
Li Zefanec9ef7a2011-12-01 14:06:42 +08006248
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006249 for (i = 0; i < num_stripes; i++) {
6250 bbio->stripes[i].physical =
6251 map->stripes[stripe_index].physical +
6252 stripe_offset +
6253 stripe_nr * map->stripe_len;
6254 bbio->stripes[i].dev =
6255 map->stripes[stripe_index].dev;
6256 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006257 }
Li Zefande11cc12011-12-01 12:55:47 +08006258
Liu Bo2b19a1f2017-03-14 13:34:00 -07006259 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006260 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006261
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006262 if (bbio->raid_map)
6263 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006264
Liu Bo73c0f222017-03-14 13:33:58 -07006265 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006266 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006267 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6268 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006269 }
6270
Li Zefande11cc12011-12-01 12:55:47 +08006271 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006272 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006273 bbio->num_stripes = num_stripes;
6274 bbio->max_errors = max_errors;
6275 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006276
6277 /*
6278 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6279 * mirror_num == num_stripes + 1 && dev_replace target drive is
6280 * available as a mirror
6281 */
6282 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6283 WARN_ON(num_stripes > 1);
6284 bbio->stripes[0].dev = dev_replace->tgtdev;
6285 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6286 bbio->mirror_num = map->num_stripes + 1;
6287 }
Chris Masoncea9e442008-04-09 16:28:12 -04006288out:
Liu Bo73beece2015-07-17 16:49:19 +08006289 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006290 lockdep_assert_held(&dev_replace->rwsem);
6291 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006292 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006293 }
Chris Mason0b86a832008-03-24 15:01:56 -04006294 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006295 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006296}
6297
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006298int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006299 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006300 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006301{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006302 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006303 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006304}
6305
Miao Xieaf8e2d12014-10-23 14:42:50 +08006306/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006307int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006308 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006309 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006310{
David Sterba825ad4c2017-03-28 14:45:22 +02006311 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006312}
6313
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006314int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6315 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006316{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006317 struct extent_map *em;
6318 struct map_lookup *map;
6319 u64 *buf;
6320 u64 bytenr;
6321 u64 length;
6322 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006323 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006324 int i, j, nr = 0;
6325
Omar Sandoval60ca8422018-05-16 16:34:31 -07006326 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006327 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006328 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006329
Jeff Mahoney95617d62015-06-03 10:55:48 -04006330 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006331 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006332 rmap_len = map->stripe_len;
6333
Yan Zhenga512bbf2008-12-08 16:46:26 -05006334 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006335 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006336 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006337 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006338 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006339 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006340 rmap_len = map->stripe_len * nr_data_stripes(map);
6341 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006342
David Sterba31e818f2015-02-20 18:00:26 +01006343 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006344 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006345
6346 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006347 if (map->stripes[i].physical > physical ||
6348 map->stripes[i].physical + length <= physical)
6349 continue;
6350
6351 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006352 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006353
6354 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6355 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006356 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006357 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6358 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006359 } /* else if RAID[56], multiply by nr_data_stripes().
6360 * Alternatively, just use rmap_len below instead of
6361 * map->stripe_len */
6362
6363 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006364 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006365 for (j = 0; j < nr; j++) {
6366 if (buf[j] == bytenr)
6367 break;
6368 }
Chris Mason934d3752008-12-08 16:43:10 -05006369 if (j == nr) {
6370 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006371 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006372 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006373 }
6374
Yan Zhenga512bbf2008-12-08 16:46:26 -05006375 *logical = buf;
6376 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006377 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006378
6379 free_extent_map(em);
6380 return 0;
6381}
6382
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006383static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006384{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006385 bio->bi_private = bbio->private;
6386 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006387 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006388
Zhao Lei6e9606d2015-01-20 15:11:34 +08006389 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006390}
6391
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006392static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006393{
Chris Mason9be33952013-05-17 18:30:14 -04006394 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006395 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006396
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006397 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006398 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006399 if (bio->bi_status == BLK_STS_IOERR ||
6400 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006401 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006402 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006403 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006404
6405 BUG_ON(stripe_index >= bbio->num_stripes);
6406 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006407 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006408 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006409 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006410 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterba0cc068e2019-03-07 15:40:50 +01006411 else if (!(bio->bi_opf & REQ_RAHEAD))
Anand Jain1cb34c82017-10-21 01:45:33 +08006412 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006413 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006414 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006415 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006416 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006417 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006418 }
6419 }
Chris Mason8790d502008-04-03 16:29:03 -04006420
Jan Schmidta1d3c472011-08-04 17:15:33 +02006421 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006422 is_orig_bio = 1;
6423
Miao Xiec404e0d2014-01-30 16:46:55 +08006424 btrfs_bio_counter_dec(bbio->fs_info);
6425
Jan Schmidta1d3c472011-08-04 17:15:33 +02006426 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006427 if (!is_orig_bio) {
6428 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006429 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006430 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006431
Chris Mason9be33952013-05-17 18:30:14 -04006432 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006433 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006434 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006435 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006436 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006437 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006438 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006439 /*
6440 * this bio is actually up to date, we didn't
6441 * go over the max number of errors
6442 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006443 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006444 }
Miao Xiec55f1392014-06-19 10:42:54 +08006445
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006446 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006447 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006448 bio_put(bio);
6449 }
Chris Mason8790d502008-04-03 16:29:03 -04006450}
6451
Chris Mason8b712842008-06-11 16:50:36 -04006452/*
6453 * see run_scheduled_bios for a description of why bios are collected for
6454 * async submit.
6455 *
6456 * This will add one bio to the pending list for a device and make sure
6457 * the work struct is scheduled.
6458 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006459static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006460 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006461{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006462 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006463 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006464 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006465
6466 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006467 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006468 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006469 return;
Chris Mason8b712842008-06-11 16:50:36 -04006470 }
6471
Chris Mason492bb6de2008-07-31 16:29:02 -04006472 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006473 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006474
6475 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006476 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006477 pending_bios = &device->pending_sync_bios;
6478 else
6479 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006480
Chris Masonffbd5172009-04-20 15:50:09 -04006481 if (pending_bios->tail)
6482 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006483
Chris Masonffbd5172009-04-20 15:50:09 -04006484 pending_bios->tail = bio;
6485 if (!pending_bios->head)
6486 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006487 if (device->running_pending)
6488 should_queue = 0;
6489
6490 spin_unlock(&device->io_lock);
6491
6492 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006493 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006494}
6495
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006496static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6497 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006498{
6499 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006500 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006501
6502 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006503 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006504 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006505 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006506 btrfs_debug_in_rcu(fs_info,
6507 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6508 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6509 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6510 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006511 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006512
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006513 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006514
Josef Bacikde1ee922012-10-19 16:50:56 -04006515 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006516 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006517 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006518 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006519}
6520
Josef Bacikde1ee922012-10-19 16:50:56 -04006521static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6522{
6523 atomic_inc(&bbio->error);
6524 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006525 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006526 WARN_ON(bio != bbio->orig_bio);
6527
Chris Mason9be33952013-05-17 18:30:14 -04006528 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006529 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006530 if (atomic_read(&bbio->error) > bbio->max_errors)
6531 bio->bi_status = BLK_STS_IOERR;
6532 else
6533 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006534 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006535 }
6536}
6537
Omar Sandoval58efbc92017-08-22 23:45:59 -07006538blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6539 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006540{
Chris Mason0b86a832008-03-24 15:01:56 -04006541 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006542 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006543 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006544 u64 length = 0;
6545 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006546 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006547 int dev_nr;
6548 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006549 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006550
Kent Overstreet4f024f32013-10-11 15:44:27 -07006551 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006552 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006553
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006554 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006555 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006556 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006557 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006558 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006559 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006560 }
Chris Masoncea9e442008-04-09 16:28:12 -04006561
Jan Schmidta1d3c472011-08-04 17:15:33 +02006562 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006563 bbio->orig_bio = first_bio;
6564 bbio->private = first_bio->bi_private;
6565 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006566 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006567 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6568
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006569 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006570 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006571 /* In this case, map_length has been set to the length of
6572 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006573 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006574 ret = raid56_parity_write(fs_info, bio, bbio,
6575 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006576 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006577 ret = raid56_parity_recover(fs_info, bio, bbio,
6578 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006579 }
Miao Xie42452152014-11-25 16:39:28 +08006580
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006581 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006582 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006583 }
6584
Chris Masoncea9e442008-04-09 16:28:12 -04006585 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006586 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006587 "mapping failed logical %llu bio len %llu len %llu",
6588 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006589 BUG();
6590 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006591
Zhao Lei08da7572015-02-12 15:42:16 +08006592 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006593 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006594 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6595 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006596 (bio_op(first_bio) == REQ_OP_WRITE &&
6597 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006598 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006599 continue;
6600 }
6601
David Sterba3aa8e072017-06-02 17:38:30 +02006602 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006603 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006604 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006605 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006606
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006607 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6608 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006609 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006610 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006611 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006612}
6613
Anand Jain09ba3bc2019-01-19 14:48:55 +08006614/*
6615 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6616 * return NULL.
6617 *
6618 * If devid and uuid are both specified, the match must be exact, otherwise
6619 * only devid is used.
6620 *
6621 * If @seed is true, traverse through the seed devices.
6622 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006623struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006624 u64 devid, u8 *uuid, u8 *fsid,
6625 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006626{
Yan Zheng2b820322008-11-17 21:11:30 -05006627 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006628
Anand Jaine4319cd2019-01-17 23:32:31 +08006629 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006630 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006631 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08006632 list_for_each_entry(device, &fs_devices->devices,
6633 dev_list) {
6634 if (device->devid == devid &&
6635 (!uuid || memcmp(device->uuid, uuid,
6636 BTRFS_UUID_SIZE) == 0))
6637 return device;
6638 }
Yan Zheng2b820322008-11-17 21:11:30 -05006639 }
Anand Jain09ba3bc2019-01-19 14:48:55 +08006640 if (seed)
6641 fs_devices = fs_devices->seed;
6642 else
6643 return NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05006644 }
6645 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006646}
6647
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006648static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006649 u64 devid, u8 *dev_uuid)
6650{
6651 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006652
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006653 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6654 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006655 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006656
6657 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006658 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006659 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006660
Anand Jaine6e674b2017-12-04 12:54:54 +08006661 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006662 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006663
Chris Masondfe25022008-05-13 13:46:40 -04006664 return device;
6665}
6666
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006667/**
6668 * btrfs_alloc_device - allocate struct btrfs_device
6669 * @fs_info: used only for generating a new devid, can be NULL if
6670 * devid is provided (i.e. @devid != NULL).
6671 * @devid: a pointer to devid for this device. If NULL a new devid
6672 * is generated.
6673 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6674 * is generated.
6675 *
6676 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006677 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006678 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006679 */
6680struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6681 const u64 *devid,
6682 const u8 *uuid)
6683{
6684 struct btrfs_device *dev;
6685 u64 tmp;
6686
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306687 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006688 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006689
6690 dev = __alloc_device();
6691 if (IS_ERR(dev))
6692 return dev;
6693
6694 if (devid)
6695 tmp = *devid;
6696 else {
6697 int ret;
6698
6699 ret = find_next_devid(fs_info, &tmp);
6700 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006701 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006702 return ERR_PTR(ret);
6703 }
6704 }
6705 dev->devid = tmp;
6706
6707 if (uuid)
6708 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6709 else
6710 generate_random_uuid(dev->uuid);
6711
Liu Bo9e0af232014-08-15 23:36:53 +08006712 btrfs_init_work(&dev->work, btrfs_submit_helper,
6713 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006714
6715 return dev;
6716}
6717
Anand Jain5a2b8e62017-10-09 11:07:45 +08006718static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006719 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006720{
Anand Jain2b902df2017-10-09 11:07:46 +08006721 if (error)
6722 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6723 devid, uuid);
6724 else
6725 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6726 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006727}
6728
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006729static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006730 struct extent_buffer *leaf,
6731 struct btrfs_chunk *chunk)
6732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006733 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006734 struct map_lookup *map;
6735 struct extent_map *em;
6736 u64 logical;
6737 u64 length;
6738 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006739 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006740 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006741 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006742 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006743
Chris Masone17cade2008-04-15 15:41:47 -04006744 logical = key->offset;
6745 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006746 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006748 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006749 if (ret)
6750 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006751
Chris Mason890871b2009-09-02 16:24:52 -04006752 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006753 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006754 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006755
6756 /* already mapped? */
6757 if (em && em->start <= logical && em->start + em->len > logical) {
6758 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006759 return 0;
6760 } else if (em) {
6761 free_extent_map(em);
6762 }
Chris Mason0b86a832008-03-24 15:01:56 -04006763
David Sterba172ddd62011-04-21 00:48:27 +02006764 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006765 if (!em)
6766 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006767 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006768 if (!map) {
6769 free_extent_map(em);
6770 return -ENOMEM;
6771 }
6772
Wang Shilong298a8f92014-06-19 10:42:52 +08006773 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006774 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006775 em->start = logical;
6776 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006777 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006778 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006779 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006780
Chris Mason593060d2008-03-25 16:50:33 -04006781 map->num_stripes = num_stripes;
6782 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6783 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006784 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6785 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006786 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006787 map->verified_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04006788 for (i = 0; i < num_stripes; i++) {
6789 map->stripes[i].physical =
6790 btrfs_stripe_offset_nr(leaf, chunk, i);
6791 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006792 read_extent_buffer(leaf, uuid, (unsigned long)
6793 btrfs_stripe_dev_uuid_nr(chunk, i),
6794 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006795 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006796 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006797 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006798 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006799 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006800 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006801 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006802 }
Chris Masondfe25022008-05-13 13:46:40 -04006803 if (!map->stripes[i].dev) {
6804 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006805 add_missing_dev(fs_info->fs_devices, devid,
6806 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006807 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006808 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006809 btrfs_err(fs_info,
6810 "failed to init missing dev %llu: %ld",
6811 devid, PTR_ERR(map->stripes[i].dev));
6812 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006813 }
Anand Jain2b902df2017-10-09 11:07:46 +08006814 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006815 }
Anand Jaine12c9622017-12-04 12:54:53 +08006816 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6817 &(map->stripes[i].dev->dev_state));
6818
Chris Mason0b86a832008-03-24 15:01:56 -04006819 }
6820
Chris Mason890871b2009-09-02 16:24:52 -04006821 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006822 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006823 write_unlock(&map_tree->map_tree.lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006824 if (ret < 0) {
6825 btrfs_err(fs_info,
6826 "failed to add chunk map, start=%llu len=%llu: %d",
6827 em->start, em->len, ret);
6828 }
Chris Mason0b86a832008-03-24 15:01:56 -04006829 free_extent_map(em);
6830
Qu Wenruo64f64f42018-08-01 10:37:20 +08006831 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006832}
6833
Jeff Mahoney143bede2012-03-01 14:56:26 +01006834static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006835 struct btrfs_dev_item *dev_item,
6836 struct btrfs_device *device)
6837{
6838 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006839
6840 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006841 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6842 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006843 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006844 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006845 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006846 device->type = btrfs_device_type(leaf, dev_item);
6847 device->io_align = btrfs_device_io_align(leaf, dev_item);
6848 device->io_width = btrfs_device_io_width(leaf, dev_item);
6849 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006850 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006851 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006852
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006853 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006854 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006855}
6856
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006857static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006858 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006859{
6860 struct btrfs_fs_devices *fs_devices;
6861 int ret;
6862
David Sterbaa32bf9a2018-03-16 02:21:22 +01006863 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006864 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006865
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006866 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006867 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006868 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006869 return fs_devices;
6870
Yan Zheng2b820322008-11-17 21:11:30 -05006871 fs_devices = fs_devices->seed;
6872 }
6873
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006874 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006875 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006876 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006877 return ERR_PTR(-ENOENT);
6878
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006879 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006880 if (IS_ERR(fs_devices))
6881 return fs_devices;
6882
6883 fs_devices->seeding = 1;
6884 fs_devices->opened = 1;
6885 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006886 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006887
6888 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006889 if (IS_ERR(fs_devices))
6890 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006891
Anand Jain897fb572018-04-12 10:29:28 +08006892 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006893 if (ret) {
6894 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006895 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006896 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006897 }
Yan Zheng2b820322008-11-17 21:11:30 -05006898
6899 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006900 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006901 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006902 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006903 goto out;
6904 }
6905
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006906 fs_devices->seed = fs_info->fs_devices->seed;
6907 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006908out:
Miao Xie5f375832014-09-03 21:35:46 +08006909 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006910}
6911
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006912static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006913 struct extent_buffer *leaf,
6914 struct btrfs_dev_item *dev_item)
6915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006916 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006917 struct btrfs_device *device;
6918 u64 devid;
6919 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006920 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006921 u8 dev_uuid[BTRFS_UUID_SIZE];
6922
Chris Mason0b86a832008-03-24 15:01:56 -04006923 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006924 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006925 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006926 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006927 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006928
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006929 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006930 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006931 if (IS_ERR(fs_devices))
6932 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006933 }
6934
Anand Jaine4319cd2019-01-17 23:32:31 +08006935 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006936 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006937 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006938 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006939 btrfs_report_missing_device(fs_info, devid,
6940 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006941 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006942 }
Yan Zheng2b820322008-11-17 21:11:30 -05006943
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006944 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006945 if (IS_ERR(device)) {
6946 btrfs_err(fs_info,
6947 "failed to add missing dev %llu: %ld",
6948 devid, PTR_ERR(device));
6949 return PTR_ERR(device);
6950 }
Anand Jain2b902df2017-10-09 11:07:46 +08006951 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006952 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006953 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006954 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6955 btrfs_report_missing_device(fs_info,
6956 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006957 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006958 }
6959 btrfs_report_missing_device(fs_info, devid,
6960 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006961 }
Miao Xie5f375832014-09-03 21:35:46 +08006962
Anand Jaine6e674b2017-12-04 12:54:54 +08006963 if (!device->bdev &&
6964 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006965 /*
6966 * this happens when a device that was properly setup
6967 * in the device info lists suddenly goes bad.
6968 * device->bdev is NULL, and so we have to set
6969 * device->missing to one here
6970 */
Miao Xie5f375832014-09-03 21:35:46 +08006971 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006972 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006973 }
Miao Xie5f375832014-09-03 21:35:46 +08006974
6975 /* Move the device to its own fs_devices */
6976 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006977 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6978 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006979
6980 list_move(&device->dev_list, &fs_devices->devices);
6981 device->fs_devices->num_devices--;
6982 fs_devices->num_devices++;
6983
6984 device->fs_devices->missing_devices--;
6985 fs_devices->missing_devices++;
6986
6987 device->fs_devices = fs_devices;
6988 }
Yan Zheng2b820322008-11-17 21:11:30 -05006989 }
6990
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006991 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006992 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006993 if (device->generation !=
6994 btrfs_device_generation(leaf, dev_item))
6995 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006996 }
Chris Mason0b86a832008-03-24 15:01:56 -04006997
6998 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006999 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08007000 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08007001 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05007002 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03007003 atomic64_add(device->total_bytes - device->bytes_used,
7004 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04007005 }
Chris Mason0b86a832008-03-24 15:01:56 -04007006 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007007 return ret;
7008}
7009
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007010int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007011{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007012 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007013 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04007014 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04007015 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04007016 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01007017 u8 *array_ptr;
7018 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04007019 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007020 u32 num_stripes;
7021 u32 array_size;
7022 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01007023 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07007024 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04007025 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04007026
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007027 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02007028 /*
7029 * This will create extent buffer of nodesize, superblock size is
7030 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7031 * overallocate but we can keep it as-is, only the first page is used.
7032 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007033 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07007034 if (IS_ERR(sb))
7035 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01007036 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04007037 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02007038 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04007039 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01007040 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02007041 * pages up-to-date when the page is larger: extent does not cover the
7042 * whole page and consequently check_page_uptodate does not find all
7043 * the page's extents up-to-date (the hole beyond sb),
7044 * write_extent_buffer then triggers a WARN_ON.
7045 *
7046 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7047 * but sb spans only this function. Add an explicit SetPageUptodate call
7048 * to silence the warning eg. on PowerPC 64.
7049 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007050 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007051 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007052
Chris Masona061fc82008-05-07 11:43:44 -04007053 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007054 array_size = btrfs_super_sys_array_size(super_copy);
7055
David Sterba1ffb22c2014-10-31 19:02:42 +01007056 array_ptr = super_copy->sys_chunk_array;
7057 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7058 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007059
David Sterba1ffb22c2014-10-31 19:02:42 +01007060 while (cur_offset < array_size) {
7061 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007062 len = sizeof(*disk_key);
7063 if (cur_offset + len > array_size)
7064 goto out_short_read;
7065
Chris Mason0b86a832008-03-24 15:01:56 -04007066 btrfs_disk_key_to_cpu(&key, disk_key);
7067
David Sterba1ffb22c2014-10-31 19:02:42 +01007068 array_ptr += len;
7069 sb_array_offset += len;
7070 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007071
Chris Mason0d81ba52008-03-24 15:02:07 -04007072 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01007073 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01007074 /*
7075 * At least one btrfs_chunk with one stripe must be
7076 * present, exact stripe count check comes afterwards
7077 */
7078 len = btrfs_chunk_item_size(1);
7079 if (cur_offset + len > array_size)
7080 goto out_short_read;
7081
7082 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01007083 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007084 btrfs_err(fs_info,
7085 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01007086 num_stripes, cur_offset);
7087 ret = -EIO;
7088 break;
7089 }
7090
Liu Boe06cd3d2016-06-03 12:05:15 -07007091 type = btrfs_chunk_type(sb, chunk);
7092 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007093 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07007094 "invalid chunk type %llu in sys_array at offset %u",
7095 type, cur_offset);
7096 ret = -EIO;
7097 break;
7098 }
7099
David Sterbae3540ea2014-11-05 15:24:51 +01007100 len = btrfs_chunk_item_size(num_stripes);
7101 if (cur_offset + len > array_size)
7102 goto out_short_read;
7103
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007104 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04007105 if (ret)
7106 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007107 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007108 btrfs_err(fs_info,
7109 "unexpected item type %u in sys_array at offset %u",
7110 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007111 ret = -EIO;
7112 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007113 }
David Sterba1ffb22c2014-10-31 19:02:42 +01007114 array_ptr += len;
7115 sb_array_offset += len;
7116 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007117 }
Liu Bod8651772016-06-03 17:41:42 -07007118 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007119 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007120 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007121
7122out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007123 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007124 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007125 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007126 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007127 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007128}
7129
Qu Wenruo21634a12017-03-09 09:34:36 +08007130/*
7131 * Check if all chunks in the fs are OK for read-write degraded mount
7132 *
Anand Jain6528b992017-12-18 17:08:59 +08007133 * If the @failing_dev is specified, it's accounted as missing.
7134 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007135 * Return true if all chunks meet the minimal RW mount requirements.
7136 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7137 */
Anand Jain6528b992017-12-18 17:08:59 +08007138bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7139 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007140{
7141 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
7142 struct extent_map *em;
7143 u64 next_start = 0;
7144 bool ret = true;
7145
7146 read_lock(&map_tree->map_tree.lock);
7147 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
7148 read_unlock(&map_tree->map_tree.lock);
7149 /* No chunk at all? Return false anyway */
7150 if (!em) {
7151 ret = false;
7152 goto out;
7153 }
7154 while (em) {
7155 struct map_lookup *map;
7156 int missing = 0;
7157 int max_tolerated;
7158 int i;
7159
7160 map = em->map_lookup;
7161 max_tolerated =
7162 btrfs_get_num_tolerated_disk_barrier_failures(
7163 map->type);
7164 for (i = 0; i < map->num_stripes; i++) {
7165 struct btrfs_device *dev = map->stripes[i].dev;
7166
Anand Jaine6e674b2017-12-04 12:54:54 +08007167 if (!dev || !dev->bdev ||
7168 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007169 dev->last_flush_error)
7170 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007171 else if (failing_dev && failing_dev == dev)
7172 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007173 }
7174 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007175 if (!failing_dev)
7176 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007177 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007178 em->start, missing, max_tolerated);
7179 free_extent_map(em);
7180 ret = false;
7181 goto out;
7182 }
7183 next_start = extent_map_end(em);
7184 free_extent_map(em);
7185
7186 read_lock(&map_tree->map_tree.lock);
7187 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
7188 (u64)(-1) - next_start);
7189 read_unlock(&map_tree->map_tree.lock);
7190 }
7191out:
7192 return ret;
7193}
7194
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007195int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007196{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007197 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007198 struct btrfs_path *path;
7199 struct extent_buffer *leaf;
7200 struct btrfs_key key;
7201 struct btrfs_key found_key;
7202 int ret;
7203 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007204 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007205
Chris Mason0b86a832008-03-24 15:01:56 -04007206 path = btrfs_alloc_path();
7207 if (!path)
7208 return -ENOMEM;
7209
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007210 /*
7211 * uuid_mutex is needed only if we are mounting a sprout FS
7212 * otherwise we don't need it.
7213 */
Li Zefanb367e472011-12-07 11:38:24 +08007214 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007215 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007216
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007217 /*
7218 * Read all device items, and then all the chunk items. All
7219 * device items are found before any chunk item (their object id
7220 * is smaller than the lowest possible object id for a chunk
7221 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007222 */
7223 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7224 key.offset = 0;
7225 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007226 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007227 if (ret < 0)
7228 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007229 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007230 leaf = path->nodes[0];
7231 slot = path->slots[0];
7232 if (slot >= btrfs_header_nritems(leaf)) {
7233 ret = btrfs_next_leaf(root, path);
7234 if (ret == 0)
7235 continue;
7236 if (ret < 0)
7237 goto error;
7238 break;
7239 }
7240 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007241 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7242 struct btrfs_dev_item *dev_item;
7243 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007244 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007245 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007246 if (ret)
7247 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007248 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007249 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7250 struct btrfs_chunk *chunk;
7251 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007252 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007253 if (ret)
7254 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007255 }
7256 path->slots[0]++;
7257 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007258
7259 /*
7260 * After loading chunk tree, we've got all device information,
7261 * do another round of validation checks.
7262 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007263 if (total_dev != fs_info->fs_devices->total_devices) {
7264 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007265 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007266 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007267 total_dev);
7268 ret = -EINVAL;
7269 goto error;
7270 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007271 if (btrfs_super_total_bytes(fs_info->super_copy) <
7272 fs_info->fs_devices->total_rw_bytes) {
7273 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007274 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007275 btrfs_super_total_bytes(fs_info->super_copy),
7276 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007277 ret = -EINVAL;
7278 goto error;
7279 }
Chris Mason0b86a832008-03-24 15:01:56 -04007280 ret = 0;
7281error:
David Sterba34441362016-10-04 19:34:27 +02007282 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007283 mutex_unlock(&uuid_mutex);
7284
Yan Zheng2b820322008-11-17 21:11:30 -05007285 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007286 return ret;
7287}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007288
Miao Xiecb517ea2013-05-15 07:48:19 +00007289void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7290{
7291 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7292 struct btrfs_device *device;
7293
Liu Bo29cc83f2014-05-11 23:14:59 +08007294 while (fs_devices) {
7295 mutex_lock(&fs_devices->device_list_mutex);
7296 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007297 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007298 mutex_unlock(&fs_devices->device_list_mutex);
7299
7300 fs_devices = fs_devices->seed;
7301 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007302}
7303
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007304static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7305{
7306 int i;
7307
7308 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7309 btrfs_dev_stat_reset(dev, i);
7310}
7311
7312int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7313{
7314 struct btrfs_key key;
7315 struct btrfs_key found_key;
7316 struct btrfs_root *dev_root = fs_info->dev_root;
7317 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7318 struct extent_buffer *eb;
7319 int slot;
7320 int ret = 0;
7321 struct btrfs_device *device;
7322 struct btrfs_path *path = NULL;
7323 int i;
7324
7325 path = btrfs_alloc_path();
7326 if (!path) {
7327 ret = -ENOMEM;
7328 goto out;
7329 }
7330
7331 mutex_lock(&fs_devices->device_list_mutex);
7332 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7333 int item_size;
7334 struct btrfs_dev_stats_item *ptr;
7335
David Sterba242e2952016-01-25 17:51:31 +01007336 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7337 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007338 key.offset = device->devid;
7339 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7340 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007341 __btrfs_reset_dev_stats(device);
7342 device->dev_stats_valid = 1;
7343 btrfs_release_path(path);
7344 continue;
7345 }
7346 slot = path->slots[0];
7347 eb = path->nodes[0];
7348 btrfs_item_key_to_cpu(eb, &found_key, slot);
7349 item_size = btrfs_item_size_nr(eb, slot);
7350
7351 ptr = btrfs_item_ptr(eb, slot,
7352 struct btrfs_dev_stats_item);
7353
7354 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7355 if (item_size >= (1 + i) * sizeof(__le64))
7356 btrfs_dev_stat_set(device, i,
7357 btrfs_dev_stats_value(eb, ptr, i));
7358 else
7359 btrfs_dev_stat_reset(device, i);
7360 }
7361
7362 device->dev_stats_valid = 1;
7363 btrfs_dev_stat_print_on_load(device);
7364 btrfs_release_path(path);
7365 }
7366 mutex_unlock(&fs_devices->device_list_mutex);
7367
7368out:
7369 btrfs_free_path(path);
7370 return ret < 0 ? ret : 0;
7371}
7372
7373static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007374 struct btrfs_device *device)
7375{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007376 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007377 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007378 struct btrfs_path *path;
7379 struct btrfs_key key;
7380 struct extent_buffer *eb;
7381 struct btrfs_dev_stats_item *ptr;
7382 int ret;
7383 int i;
7384
David Sterba242e2952016-01-25 17:51:31 +01007385 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7386 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007387 key.offset = device->devid;
7388
7389 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007390 if (!path)
7391 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007392 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7393 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007394 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007395 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007396 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007397 goto out;
7398 }
7399
7400 if (ret == 0 &&
7401 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7402 /* need to delete old one and insert a new one */
7403 ret = btrfs_del_item(trans, dev_root, path);
7404 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007405 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007406 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007407 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007408 goto out;
7409 }
7410 ret = 1;
7411 }
7412
7413 if (ret == 1) {
7414 /* need to insert a new item */
7415 btrfs_release_path(path);
7416 ret = btrfs_insert_empty_item(trans, dev_root, path,
7417 &key, sizeof(*ptr));
7418 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007419 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007420 "insert dev_stats item for device %s failed %d",
7421 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007422 goto out;
7423 }
7424 }
7425
7426 eb = path->nodes[0];
7427 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7428 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7429 btrfs_set_dev_stats_value(eb, ptr, i,
7430 btrfs_dev_stat_read(device, i));
7431 btrfs_mark_buffer_dirty(eb);
7432
7433out:
7434 btrfs_free_path(path);
7435 return ret;
7436}
7437
7438/*
7439 * called from commit_transaction. Writes all changed device stats to disk.
7440 */
7441int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7442 struct btrfs_fs_info *fs_info)
7443{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007444 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7445 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007446 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007447 int ret = 0;
7448
7449 mutex_lock(&fs_devices->device_list_mutex);
7450 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007451 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7452 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007453 continue;
7454
Nikolay Borisov9deae962017-10-24 13:47:37 +03007455
7456 /*
7457 * There is a LOAD-LOAD control dependency between the value of
7458 * dev_stats_ccnt and updating the on-disk values which requires
7459 * reading the in-memory counters. Such control dependencies
7460 * require explicit read memory barriers.
7461 *
7462 * This memory barriers pairs with smp_mb__before_atomic in
7463 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7464 * barrier implied by atomic_xchg in
7465 * btrfs_dev_stats_read_and_reset
7466 */
7467 smp_rmb();
7468
Nikolay Borisov5495f192018-07-20 19:37:49 +03007469 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007470 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007471 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007472 }
7473 mutex_unlock(&fs_devices->device_list_mutex);
7474
7475 return ret;
7476}
7477
Stefan Behrens442a4f62012-05-25 16:06:08 +02007478void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7479{
7480 btrfs_dev_stat_inc(dev, index);
7481 btrfs_dev_stat_print_on_error(dev);
7482}
7483
Eric Sandeen48a3b632013-04-25 20:41:01 +00007484static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007485{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007486 if (!dev->dev_stats_valid)
7487 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007488 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007489 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007490 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007491 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7492 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7493 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007494 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7495 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007496}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007497
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007498static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7499{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007500 int i;
7501
7502 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7503 if (btrfs_dev_stat_read(dev, i) != 0)
7504 break;
7505 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7506 return; /* all values == 0, suppress message */
7507
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007508 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007509 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007510 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007511 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7512 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7513 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7514 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7515 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7516}
7517
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007518int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007519 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007520{
7521 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007522 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007523 int i;
7524
7525 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007526 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7527 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007528 mutex_unlock(&fs_devices->device_list_mutex);
7529
7530 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007531 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007532 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007533 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007534 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007535 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007536 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007537 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7538 if (stats->nr_items > i)
7539 stats->values[i] =
7540 btrfs_dev_stat_read_and_reset(dev, i);
7541 else
7542 btrfs_dev_stat_reset(dev, i);
7543 }
7544 } else {
7545 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7546 if (stats->nr_items > i)
7547 stats->values[i] = btrfs_dev_stat_read(dev, i);
7548 }
7549 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7550 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7551 return 0;
7552}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007553
David Sterbada353f62017-02-14 17:55:53 +01007554void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007555{
7556 struct buffer_head *bh;
7557 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007558 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007559
Anand Jain12b1c262015-08-14 18:32:59 +08007560 if (!bdev)
7561 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007562
Anand Jain12b1c262015-08-14 18:32:59 +08007563 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7564 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007565
Anand Jain12b1c262015-08-14 18:32:59 +08007566 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7567 continue;
7568
7569 disk_super = (struct btrfs_super_block *)bh->b_data;
7570
7571 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7572 set_buffer_dirty(bh);
7573 sync_dirty_buffer(bh);
7574 brelse(bh);
7575 }
7576
7577 /* Notify udev that device has changed */
7578 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7579
7580 /* Update ctime/mtime for device path for libblkid */
7581 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007582}
Miao Xie935e5cc2014-09-03 21:35:33 +08007583
7584/*
7585 * Update the size of all devices, which is used for writing out the
7586 * super blocks.
7587 */
7588void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7589{
7590 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7591 struct btrfs_device *curr, *next;
7592
7593 if (list_empty(&fs_devices->resized_devices))
7594 return;
7595
7596 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007597 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007598 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7599 resized_list) {
7600 list_del_init(&curr->resized_list);
7601 curr->commit_total_bytes = curr->disk_total_bytes;
7602 }
David Sterba34441362016-10-04 19:34:27 +02007603 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007604 mutex_unlock(&fs_devices->device_list_mutex);
7605}
Miao Xiece7213c2014-09-03 21:35:34 +08007606
7607/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007608void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007609{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007610 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007611 struct extent_map *em;
7612 struct map_lookup *map;
7613 struct btrfs_device *dev;
7614 int i;
7615
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007616 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007617 return;
7618
7619 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007620 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007621 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007622 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007623
7624 for (i = 0; i < map->num_stripes; i++) {
7625 dev = map->stripes[i].dev;
7626 dev->commit_bytes_used = dev->bytes_used;
7627 }
7628 }
David Sterba34441362016-10-04 19:34:27 +02007629 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007630}
Anand Jain5a13f432015-03-10 06:38:31 +08007631
7632void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7633{
7634 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7635 while (fs_devices) {
7636 fs_devices->fs_info = fs_info;
7637 fs_devices = fs_devices->seed;
7638 }
7639}
7640
7641void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7642{
7643 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7644 while (fs_devices) {
7645 fs_devices->fs_info = NULL;
7646 fs_devices = fs_devices->seed;
7647 }
7648}
David Sterba46df06b2018-07-13 20:46:30 +02007649
7650/*
7651 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7652 */
7653int btrfs_bg_type_to_factor(u64 flags)
7654{
7655 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7656 BTRFS_BLOCK_GROUP_RAID10))
7657 return 2;
7658 return 1;
7659}
Qu Wenruocf90d882018-08-01 10:37:19 +08007660
7661
7662static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7663{
7664 int index = btrfs_bg_flags_to_raid_index(type);
7665 int ncopies = btrfs_raid_array[index].ncopies;
7666 int data_stripes;
7667
7668 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7669 case BTRFS_BLOCK_GROUP_RAID5:
7670 data_stripes = num_stripes - 1;
7671 break;
7672 case BTRFS_BLOCK_GROUP_RAID6:
7673 data_stripes = num_stripes - 2;
7674 break;
7675 default:
7676 data_stripes = num_stripes / ncopies;
7677 break;
7678 }
7679 return div_u64(chunk_len, data_stripes);
7680}
7681
7682static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7683 u64 chunk_offset, u64 devid,
7684 u64 physical_offset, u64 physical_len)
7685{
7686 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7687 struct extent_map *em;
7688 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007689 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007690 u64 stripe_len;
7691 bool found = false;
7692 int ret = 0;
7693 int i;
7694
7695 read_lock(&em_tree->lock);
7696 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7697 read_unlock(&em_tree->lock);
7698
7699 if (!em) {
7700 btrfs_err(fs_info,
7701"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7702 physical_offset, devid);
7703 ret = -EUCLEAN;
7704 goto out;
7705 }
7706
7707 map = em->map_lookup;
7708 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7709 if (physical_len != stripe_len) {
7710 btrfs_err(fs_info,
7711"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7712 physical_offset, devid, em->start, physical_len,
7713 stripe_len);
7714 ret = -EUCLEAN;
7715 goto out;
7716 }
7717
7718 for (i = 0; i < map->num_stripes; i++) {
7719 if (map->stripes[i].dev->devid == devid &&
7720 map->stripes[i].physical == physical_offset) {
7721 found = true;
7722 if (map->verified_stripes >= map->num_stripes) {
7723 btrfs_err(fs_info,
7724 "too many dev extents for chunk %llu found",
7725 em->start);
7726 ret = -EUCLEAN;
7727 goto out;
7728 }
7729 map->verified_stripes++;
7730 break;
7731 }
7732 }
7733 if (!found) {
7734 btrfs_err(fs_info,
7735 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7736 physical_offset, devid);
7737 ret = -EUCLEAN;
7738 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007739
7740 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007741 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007742 if (!dev) {
7743 btrfs_err(fs_info, "failed to find devid %llu", devid);
7744 ret = -EUCLEAN;
7745 goto out;
7746 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007747
7748 /* It's possible this device is a dummy for seed device */
7749 if (dev->disk_total_bytes == 0) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08007750 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7751 NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007752 if (!dev) {
7753 btrfs_err(fs_info, "failed to find seed devid %llu",
7754 devid);
7755 ret = -EUCLEAN;
7756 goto out;
7757 }
7758 }
7759
Qu Wenruo05a37c42018-10-05 17:45:55 +08007760 if (physical_offset + physical_len > dev->disk_total_bytes) {
7761 btrfs_err(fs_info,
7762"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7763 devid, physical_offset, physical_len,
7764 dev->disk_total_bytes);
7765 ret = -EUCLEAN;
7766 goto out;
7767 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007768out:
7769 free_extent_map(em);
7770 return ret;
7771}
7772
7773static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7774{
7775 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7776 struct extent_map *em;
7777 struct rb_node *node;
7778 int ret = 0;
7779
7780 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007781 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007782 em = rb_entry(node, struct extent_map, rb_node);
7783 if (em->map_lookup->num_stripes !=
7784 em->map_lookup->verified_stripes) {
7785 btrfs_err(fs_info,
7786 "chunk %llu has missing dev extent, have %d expect %d",
7787 em->start, em->map_lookup->verified_stripes,
7788 em->map_lookup->num_stripes);
7789 ret = -EUCLEAN;
7790 goto out;
7791 }
7792 }
7793out:
7794 read_unlock(&em_tree->lock);
7795 return ret;
7796}
7797
7798/*
7799 * Ensure that all dev extents are mapped to correct chunk, otherwise
7800 * later chunk allocation/free would cause unexpected behavior.
7801 *
7802 * NOTE: This will iterate through the whole device tree, which should be of
7803 * the same size level as the chunk tree. This slightly increases mount time.
7804 */
7805int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7806{
7807 struct btrfs_path *path;
7808 struct btrfs_root *root = fs_info->dev_root;
7809 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007810 u64 prev_devid = 0;
7811 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007812 int ret = 0;
7813
7814 key.objectid = 1;
7815 key.type = BTRFS_DEV_EXTENT_KEY;
7816 key.offset = 0;
7817
7818 path = btrfs_alloc_path();
7819 if (!path)
7820 return -ENOMEM;
7821
7822 path->reada = READA_FORWARD;
7823 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7824 if (ret < 0)
7825 goto out;
7826
7827 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7828 ret = btrfs_next_item(root, path);
7829 if (ret < 0)
7830 goto out;
7831 /* No dev extents at all? Not good */
7832 if (ret > 0) {
7833 ret = -EUCLEAN;
7834 goto out;
7835 }
7836 }
7837 while (1) {
7838 struct extent_buffer *leaf = path->nodes[0];
7839 struct btrfs_dev_extent *dext;
7840 int slot = path->slots[0];
7841 u64 chunk_offset;
7842 u64 physical_offset;
7843 u64 physical_len;
7844 u64 devid;
7845
7846 btrfs_item_key_to_cpu(leaf, &key, slot);
7847 if (key.type != BTRFS_DEV_EXTENT_KEY)
7848 break;
7849 devid = key.objectid;
7850 physical_offset = key.offset;
7851
7852 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7853 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7854 physical_len = btrfs_dev_extent_length(leaf, dext);
7855
Qu Wenruo5eb19382018-10-05 17:45:54 +08007856 /* Check if this dev extent overlaps with the previous one */
7857 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7858 btrfs_err(fs_info,
7859"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7860 devid, physical_offset, prev_dev_ext_end);
7861 ret = -EUCLEAN;
7862 goto out;
7863 }
7864
Qu Wenruocf90d882018-08-01 10:37:19 +08007865 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7866 physical_offset, physical_len);
7867 if (ret < 0)
7868 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007869 prev_devid = devid;
7870 prev_dev_ext_end = physical_offset + physical_len;
7871
Qu Wenruocf90d882018-08-01 10:37:19 +08007872 ret = btrfs_next_item(root, path);
7873 if (ret < 0)
7874 goto out;
7875 if (ret > 0) {
7876 ret = 0;
7877 break;
7878 }
7879 }
7880
7881 /* Ensure all chunks have corresponding dev extents */
7882 ret = verify_chunk_dev_extent_mapping(fs_info);
7883out:
7884 btrfs_free_path(path);
7885 return ret;
7886}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007887
7888/*
7889 * Check whether the given block group or device is pinned by any inode being
7890 * used as a swapfile.
7891 */
7892bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7893{
7894 struct btrfs_swapfile_pin *sp;
7895 struct rb_node *node;
7896
7897 spin_lock(&fs_info->swapfile_pins_lock);
7898 node = fs_info->swapfile_pins.rb_node;
7899 while (node) {
7900 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7901 if (ptr < sp->ptr)
7902 node = node->rb_left;
7903 else if (ptr > sp->ptr)
7904 node = node->rb_right;
7905 else
7906 break;
7907 }
7908 spin_unlock(&fs_info->swapfile_pins_lock);
7909 return node != NULL;
7910}