blob: a447d3ec48d506b848e1ab228fc0ce55fb0f87c7 [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"
Josef Bacik8719aaa2019-06-18 16:09:16 -040031#include "space-info.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032
Zhao Leiaf902042015-09-15 21:08:06 +080033const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
34 [BTRFS_RAID_RAID10] = {
35 .sub_stripes = 2,
36 .dev_stripes = 1,
37 .devs_max = 0, /* 0 == as many as possible */
38 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080039 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080040 .devs_increment = 2,
41 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020042 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080043 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080044 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080045 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080046 },
47 [BTRFS_RAID_RAID1] = {
48 .sub_stripes = 1,
49 .dev_stripes = 1,
50 .devs_max = 2,
51 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080052 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080053 .devs_increment = 2,
54 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020055 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080056 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080057 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080058 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080059 },
60 [BTRFS_RAID_DUP] = {
61 .sub_stripes = 1,
62 .dev_stripes = 2,
63 .devs_max = 1,
64 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080065 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080066 .devs_increment = 1,
67 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020068 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080069 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080070 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080071 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080072 },
73 [BTRFS_RAID_RAID0] = {
74 .sub_stripes = 1,
75 .dev_stripes = 1,
76 .devs_max = 0,
77 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080078 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080079 .devs_increment = 1,
80 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020081 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080082 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +080083 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080084 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080085 },
86 [BTRFS_RAID_SINGLE] = {
87 .sub_stripes = 1,
88 .dev_stripes = 1,
89 .devs_max = 1,
90 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080091 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080092 .devs_increment = 1,
93 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020094 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080095 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +080096 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080097 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080098 },
99 [BTRFS_RAID_RAID5] = {
100 .sub_stripes = 1,
101 .dev_stripes = 1,
102 .devs_max = 0,
103 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800104 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800105 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200106 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200107 .nparity = 1,
Anand Jained234672018-04-25 19:01:42 +0800108 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800109 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800110 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800111 },
112 [BTRFS_RAID_RAID6] = {
113 .sub_stripes = 1,
114 .dev_stripes = 1,
115 .devs_max = 0,
116 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800117 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800118 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200119 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200120 .nparity = 2,
Anand Jained234672018-04-25 19:01:42 +0800121 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800122 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800123 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800124 },
125};
126
David Sterba158da512019-05-17 11:43:41 +0200127const char *btrfs_bg_type_to_raid_name(u64 flags)
Anand Jained234672018-04-25 19:01:42 +0800128{
David Sterba158da512019-05-17 11:43:41 +0200129 const int index = btrfs_bg_flags_to_raid_index(flags);
130
131 if (index >= BTRFS_NR_RAID_TYPES)
Anand Jained234672018-04-25 19:01:42 +0800132 return NULL;
133
David Sterba158da512019-05-17 11:43:41 +0200134 return btrfs_raid_array[index].raid_name;
Anand Jained234672018-04-25 19:01:42 +0800135}
136
Anand Jainf89e09c2018-11-20 16:12:55 +0800137/*
138 * Fill @buf with textual description of @bg_flags, no more than @size_buf
139 * bytes including terminating null byte.
140 */
141void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
142{
143 int i;
144 int ret;
145 char *bp = buf;
146 u64 flags = bg_flags;
147 u32 size_bp = size_buf;
148
149 if (!flags) {
150 strcpy(bp, "NONE");
151 return;
152 }
153
154#define DESCRIBE_FLAG(flag, desc) \
155 do { \
156 if (flags & (flag)) { \
157 ret = snprintf(bp, size_bp, "%s|", (desc)); \
158 if (ret < 0 || ret >= size_bp) \
159 goto out_overflow; \
160 size_bp -= ret; \
161 bp += ret; \
162 flags &= ~(flag); \
163 } \
164 } while (0)
165
166 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
167 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
168 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
169
170 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
171 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
172 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
173 btrfs_raid_array[i].raid_name);
174#undef DESCRIBE_FLAG
175
176 if (flags) {
177 ret = snprintf(bp, size_bp, "0x%llx|", flags);
178 size_bp -= ret;
179 }
180
181 if (size_bp < size_buf)
182 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
183
184 /*
185 * The text is trimmed, it's up to the caller to provide sufficiently
186 * large buffer
187 */
188out_overflow:;
189}
190
David Sterba6f8e0fc2019-03-20 16:29:13 +0100191static int init_first_rw_device(struct btrfs_trans_handle *trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400192static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200193static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000194static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200195static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700196static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
197 enum btrfs_map_op op,
198 u64 logical, u64 *length,
199 struct btrfs_bio **bbio_ret,
200 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500201
David Sterba9c6b1c42017-06-16 22:30:00 +0200202/*
203 * Device locking
204 * ==============
205 *
206 * There are several mutexes that protect manipulation of devices and low-level
207 * structures like chunks but not block groups, extents or files
208 *
209 * uuid_mutex (global lock)
210 * ------------------------
211 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
212 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
213 * device) or requested by the device= mount option
214 *
215 * the mutex can be very coarse and can cover long-running operations
216 *
217 * protects: updates to fs_devices counters like missing devices, rw devices,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100218 * seeding, structure cloning, opening/closing devices at mount/umount time
David Sterba9c6b1c42017-06-16 22:30:00 +0200219 *
220 * global::fs_devs - add, remove, updates to the global list
221 *
222 * does not protect: manipulation of the fs_devices::devices list!
223 *
224 * btrfs_device::name - renames (write side), read is RCU
225 *
226 * fs_devices::device_list_mutex (per-fs, with RCU)
227 * ------------------------------------------------
228 * protects updates to fs_devices::devices, ie. adding and deleting
229 *
230 * simple list traversal with read-only actions can be done with RCU protection
231 *
232 * may be used to exclude some operations from running concurrently without any
233 * modifications to the list (see write_all_supers)
234 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200235 * balance_mutex
236 * -------------
237 * protects balance structures (status, state) and context accessed from
238 * several places (internally, ioctl)
239 *
240 * chunk_mutex
241 * -----------
242 * protects chunks, adding or removing during allocation, trim or when a new
Nikolay Borisov0b6f5d42019-05-09 18:11:11 +0300243 * device is added/removed. Additionally it also protects post_commit_list of
244 * individual devices, since they can be added to the transaction's
245 * post_commit_list only with chunk_mutex held.
David Sterba9c6b1c42017-06-16 22:30:00 +0200246 *
247 * cleaner_mutex
248 * -------------
249 * a big lock that is held by the cleaner thread and prevents running subvolume
250 * cleaning together with relocation or delayed iputs
251 *
252 *
253 * Lock nesting
254 * ============
255 *
256 * uuid_mutex
257 * volume_mutex
258 * device_list_mutex
259 * chunk_mutex
260 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800261 *
262 *
263 * Exclusive operations, BTRFS_FS_EXCL_OP
264 * ======================================
265 *
266 * Maintains the exclusivity of the following operations that apply to the
267 * whole filesystem and cannot run in parallel.
268 *
269 * - Balance (*)
270 * - Device add
271 * - Device remove
272 * - Device replace (*)
273 * - Resize
274 *
275 * The device operations (as above) can be in one of the following states:
276 *
277 * - Running state
278 * - Paused state
279 * - Completed state
280 *
281 * Only device operations marked with (*) can go into the Paused state for the
282 * following reasons:
283 *
284 * - ioctl (only Balance can be Paused through ioctl)
285 * - filesystem remounted as read-only
286 * - filesystem unmounted and mounted as read-only
287 * - system power-cycle and filesystem mounted as read-only
288 * - filesystem or device errors leading to forced read-only
289 *
290 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
291 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
292 * A device operation in Paused or Running state can be canceled or resumed
293 * either by ioctl (Balance only) or when remounted as read-write.
294 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
295 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200296 */
297
Miao Xie67a2c452014-09-03 21:35:43 +0800298DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400299static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800300struct list_head *btrfs_get_fs_uuids(void)
301{
302 return &fs_uuids;
303}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400304
David Sterba2dfeca92017-06-14 02:48:07 +0200305/*
306 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200307 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
308 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200309 *
310 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
311 * The returned struct is not linked onto any lists and can be destroyed with
312 * kfree() right away.
313 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200314static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
315 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300316{
317 struct btrfs_fs_devices *fs_devs;
318
David Sterba78f2c9e2016-02-11 14:25:38 +0100319 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300320 if (!fs_devs)
321 return ERR_PTR(-ENOMEM);
322
323 mutex_init(&fs_devs->device_list_mutex);
324
325 INIT_LIST_HEAD(&fs_devs->devices);
326 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800327 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300328 if (fsid)
329 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300330
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200331 if (metadata_fsid)
332 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
333 else if (fsid)
334 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
335
Ilya Dryomov2208a372013-08-12 14:33:03 +0300336 return fs_devs;
337}
338
David Sterbaa425f9d2018-03-20 15:47:33 +0100339void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100340{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200341 WARN_ON(!list_empty(&device->post_commit_list));
David Sterba48dae9c2017-10-30 18:10:25 +0100342 rcu_string_free(device->name);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200343 extent_io_tree_release(&device->alloc_state);
David Sterba48dae9c2017-10-30 18:10:25 +0100344 bio_put(device->flush_bio);
345 kfree(device);
346}
347
Yan Zhenge4404d62008-12-12 10:03:26 -0500348static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
349{
350 struct btrfs_device *device;
351 WARN_ON(fs_devices->opened);
352 while (!list_empty(&fs_devices->devices)) {
353 device = list_entry(fs_devices->devices.next,
354 struct btrfs_device, dev_list);
355 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100356 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500357 }
358 kfree(fs_devices);
359}
360
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000361static void btrfs_kobject_uevent(struct block_device *bdev,
362 enum kobject_action action)
363{
364 int ret;
365
366 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
367 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500368 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000369 action,
370 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
371 &disk_to_dev(bdev->bd_disk)->kobj);
372}
373
David Sterbaffc5a372018-02-19 17:24:15 +0100374void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375{
376 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400377
Yan Zheng2b820322008-11-17 21:11:30 -0500378 while (!list_empty(&fs_uuids)) {
379 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800380 struct btrfs_fs_devices, fs_list);
381 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500382 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400383 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400384}
385
David Sterba48dae9c2017-10-30 18:10:25 +0100386/*
387 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
388 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100389 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100390 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300391static struct btrfs_device *__alloc_device(void)
392{
393 struct btrfs_device *dev;
394
David Sterba78f2c9e2016-02-11 14:25:38 +0100395 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300396 if (!dev)
397 return ERR_PTR(-ENOMEM);
398
David Sterbae0ae9992017-06-06 17:06:06 +0200399 /*
400 * Preallocate a bio that's always going to be used for flushing device
401 * barriers and matches the device lifespan
402 */
403 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
404 if (!dev->flush_bio) {
405 kfree(dev);
406 return ERR_PTR(-ENOMEM);
407 }
David Sterbae0ae9992017-06-06 17:06:06 +0200408
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300409 INIT_LIST_HEAD(&dev->dev_list);
410 INIT_LIST_HEAD(&dev->dev_alloc_list);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200411 INIT_LIST_HEAD(&dev->post_commit_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300412
413 spin_lock_init(&dev->io_lock);
414
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300415 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800416 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100417 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700418 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800419 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200420 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300421
422 return dev;
423}
424
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200425static noinline struct btrfs_fs_devices *find_fsid(
426 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400428 struct btrfs_fs_devices *fs_devices;
429
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200430 ASSERT(fsid);
431
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200432 if (metadata_fsid) {
433 /*
434 * Handle scanned device having completed its fsid change but
435 * belonging to a fs_devices that was created by first scanning
436 * a device which didn't have its fsid/metadata_uuid changed
437 * at all and the CHANGING_FSID_V2 flag set.
438 */
439 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
440 if (fs_devices->fsid_change &&
441 memcmp(metadata_fsid, fs_devices->fsid,
442 BTRFS_FSID_SIZE) == 0 &&
443 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
444 BTRFS_FSID_SIZE) == 0) {
445 return fs_devices;
446 }
447 }
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200448 /*
449 * Handle scanned device having completed its fsid change but
450 * belonging to a fs_devices that was created by a device that
451 * has an outdated pair of fsid/metadata_uuid and
452 * CHANGING_FSID_V2 flag set.
453 */
454 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
455 if (fs_devices->fsid_change &&
456 memcmp(fs_devices->metadata_uuid,
457 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
458 memcmp(metadata_fsid, fs_devices->metadata_uuid,
459 BTRFS_FSID_SIZE) == 0) {
460 return fs_devices;
461 }
462 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200463 }
464
465 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800466 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200467 if (metadata_fsid) {
468 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
469 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
470 BTRFS_FSID_SIZE) == 0)
471 return fs_devices;
472 } else {
473 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
474 return fs_devices;
475 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400476 }
477 return NULL;
478}
479
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100480static int
481btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
482 int flush, struct block_device **bdev,
483 struct buffer_head **bh)
484{
485 int ret;
486
487 *bdev = blkdev_get_by_path(device_path, flags, holder);
488
489 if (IS_ERR(*bdev)) {
490 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100491 goto error;
492 }
493
494 if (flush)
495 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200496 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100497 if (ret) {
498 blkdev_put(*bdev, flags);
499 goto error;
500 }
501 invalidate_bdev(*bdev);
502 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800503 if (IS_ERR(*bh)) {
504 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100505 blkdev_put(*bdev, flags);
506 goto error;
507 }
508
509 return 0;
510
511error:
512 *bdev = NULL;
513 *bh = NULL;
514 return ret;
515}
516
Chris Masonffbd5172009-04-20 15:50:09 -0400517static void requeue_list(struct btrfs_pending_bios *pending_bios,
518 struct bio *head, struct bio *tail)
519{
520
521 struct bio *old_head;
522
523 old_head = pending_bios->head;
524 pending_bios->head = head;
525 if (pending_bios->tail)
526 tail->bi_next = old_head;
527 else
528 pending_bios->tail = tail;
529}
530
Chris Mason8b712842008-06-11 16:50:36 -0400531/*
532 * we try to collect pending bios for a device so we don't get a large
533 * number of procs sending bios down to the same device. This greatly
534 * improves the schedulers ability to collect and merge the bios.
535 *
536 * But, it also turns into a long list of bios to process and that is sure
537 * to eventually make the worker thread block. The solution here is to
538 * make some progress and then put this work struct back at the end of
539 * the list if the block device is congested. This way, multiple devices
540 * can make progress from a single worker thread.
541 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100542static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400543{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400544 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400545 struct bio *pending;
546 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400547 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400548 struct bio *tail;
549 struct bio *cur;
550 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400551 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400552 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400553 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400554 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000555 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400556 struct blk_plug plug;
557
558 /*
559 * this function runs all the bios we've collected for
560 * a particular device. We don't want to wander off to
561 * another device without first sending all of these down.
562 * So, setup a plug here and finish it off before we return
563 */
564 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400565
Jan Karaefa7c9f2017-02-02 15:56:53 +0100566 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400567
Chris Mason8b712842008-06-11 16:50:36 -0400568loop:
569 spin_lock(&device->io_lock);
570
Chris Masona6837052009-02-04 09:19:41 -0500571loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400572 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400573
Chris Mason8b712842008-06-11 16:50:36 -0400574 /* take all the bios off the list at once and process them
575 * later on (without the lock held). But, remember the
576 * tail and other pointers so the bios can be properly reinserted
577 * into the list if we hit congestion
578 */
Chris Masond84275c2009-06-09 15:39:08 -0400579 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400580 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400581 force_reg = 1;
582 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400583 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400584 force_reg = 0;
585 }
Chris Masonffbd5172009-04-20 15:50:09 -0400586
587 pending = pending_bios->head;
588 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400589 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400590
591 /*
592 * if pending was null this time around, no bios need processing
593 * at all and we can stop. Otherwise it'll loop back up again
594 * and do an additional check so no bios are missed.
595 *
596 * device->running_pending is used to synchronize with the
597 * schedule_bio code.
598 */
Chris Masonffbd5172009-04-20 15:50:09 -0400599 if (device->pending_sync_bios.head == NULL &&
600 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400601 again = 0;
602 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400603 } else {
604 again = 1;
605 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400606 }
Chris Masonffbd5172009-04-20 15:50:09 -0400607
608 pending_bios->head = NULL;
609 pending_bios->tail = NULL;
610
Chris Mason8b712842008-06-11 16:50:36 -0400611 spin_unlock(&device->io_lock);
612
Chris Masond3977122009-01-05 21:25:51 -0500613 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400614
615 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400616 /* we want to work on both lists, but do more bios on the
617 * sync list than the regular list
618 */
619 if ((num_run > 32 &&
620 pending_bios != &device->pending_sync_bios &&
621 device->pending_sync_bios.head) ||
622 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
623 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400624 spin_lock(&device->io_lock);
625 requeue_list(pending_bios, pending, tail);
626 goto loop_lock;
627 }
628
Chris Mason8b712842008-06-11 16:50:36 -0400629 cur = pending;
630 pending = pending->bi_next;
631 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400632
Jens Axboedac56212015-04-17 16:23:59 -0600633 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400634
Chris Mason2ab1ba62011-08-04 14:28:36 -0400635 /*
636 * if we're doing the sync list, record that our
637 * plug has some sync requests on it
638 *
639 * If we're doing the regular list and there are
640 * sync requests sitting around, unplug before
641 * we add more
642 */
643 if (pending_bios == &device->pending_sync_bios) {
644 sync_pending = 1;
645 } else if (sync_pending) {
646 blk_finish_plug(&plug);
647 blk_start_plug(&plug);
648 sync_pending = 0;
649 }
650
Mike Christie4e49ea42016-06-05 14:31:41 -0500651 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400652 num_run++;
653 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100654
655 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400656
657 /*
658 * we made progress, there is more work to do and the bdi
659 * is now congested. Back off and let other work structs
660 * run instead
661 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400662 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500663 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400664 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400665
Chris Masonb765ead2009-04-03 10:27:10 -0400666 ioc = current->io_context;
667
668 /*
669 * the main goal here is that we don't want to
670 * block if we're going to be able to submit
671 * more requests without blocking.
672 *
673 * This code does two great things, it pokes into
674 * the elevator code from a filesystem _and_
675 * it makes assumptions about how batching works.
676 */
677 if (ioc && ioc->nr_batch_requests > 0 &&
678 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
679 (last_waited == 0 ||
680 ioc->last_waited == last_waited)) {
681 /*
682 * we want to go through our batch of
683 * requests and stop. So, we copy out
684 * the ioc->last_waited time and test
685 * against it before looping
686 */
687 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100688 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400689 continue;
690 }
Chris Mason8b712842008-06-11 16:50:36 -0400691 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400692 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500693 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400694
695 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800696 btrfs_queue_work(fs_info->submit_workers,
697 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400698 goto done;
699 }
700 }
Chris Masonffbd5172009-04-20 15:50:09 -0400701
Chris Mason51684082010-03-10 15:33:32 -0500702 cond_resched();
703 if (again)
704 goto loop;
705
706 spin_lock(&device->io_lock);
707 if (device->pending_bios.head || device->pending_sync_bios.head)
708 goto loop_lock;
709 spin_unlock(&device->io_lock);
710
Chris Mason8b712842008-06-11 16:50:36 -0400711done:
Chris Mason211588a2011-04-19 20:12:40 -0400712 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400713}
714
Christoph Hellwigb2950862008-12-02 09:54:17 -0500715static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400716{
717 struct btrfs_device *device;
718
719 device = container_of(work, struct btrfs_device, work);
720 run_scheduled_bios(device);
721}
722
Anand Jain70bc7082019-01-04 13:31:53 +0800723static bool device_path_matched(const char *path, struct btrfs_device *device)
724{
725 int found;
726
727 rcu_read_lock();
728 found = strcmp(rcu_str_deref(device->name), path);
729 rcu_read_unlock();
730
731 return found == 0;
732}
733
Anand Jaind8367db2018-01-18 22:00:37 +0800734/*
735 * Search and remove all stale (devices which are not mounted) devices.
736 * When both inputs are NULL, it will search and release all stale devices.
737 * path: Optional. When provided will it release all unmounted devices
738 * matching this path only.
739 * skip_dev: Optional. Will skip this device when searching for the stale
740 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800741 * Return: 0 for success or if @path is NULL.
742 * -EBUSY if @path is a mounted device.
743 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800744 */
Anand Jain70bc7082019-01-04 13:31:53 +0800745static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800746 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800747{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800748 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
749 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800750 int ret = 0;
751
752 if (path)
753 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800754
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800755 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800756
Anand Jain70bc7082019-01-04 13:31:53 +0800757 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800758 list_for_each_entry_safe(device, tmp_device,
759 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800760 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800761 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800762 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800763 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800764 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800765 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800766 if (fs_devices->opened) {
767 /* for an already deleted device return 0 */
768 if (path && ret != 0)
769 ret = -EBUSY;
770 break;
771 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800772
Anand Jain4fde46f2015-06-17 21:10:48 +0800773 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800774 fs_devices->num_devices--;
775 list_del(&device->dev_list);
776 btrfs_free_device(device);
777
Anand Jain70bc7082019-01-04 13:31:53 +0800778 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800779 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200780 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800781 }
782 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800783
Anand Jain7bcb8162018-05-29 17:23:20 +0800784 if (fs_devices->num_devices == 0) {
785 btrfs_sysfs_remove_fsid(fs_devices);
786 list_del(&fs_devices->fs_list);
787 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800788 }
789 }
Anand Jain70bc7082019-01-04 13:31:53 +0800790
791 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800792}
793
Anand Jain0fb08bc2017-11-09 23:45:24 +0800794static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
795 struct btrfs_device *device, fmode_t flags,
796 void *holder)
797{
798 struct request_queue *q;
799 struct block_device *bdev;
800 struct buffer_head *bh;
801 struct btrfs_super_block *disk_super;
802 u64 devid;
803 int ret;
804
805 if (device->bdev)
806 return -EINVAL;
807 if (!device->name)
808 return -EINVAL;
809
810 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
811 &bdev, &bh);
812 if (ret)
813 return ret;
814
815 disk_super = (struct btrfs_super_block *)bh->b_data;
816 devid = btrfs_stack_device_id(&disk_super->dev_item);
817 if (devid != device->devid)
818 goto error_brelse;
819
820 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
821 goto error_brelse;
822
823 device->generation = btrfs_super_generation(disk_super);
824
825 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200826 if (btrfs_super_incompat_flags(disk_super) &
827 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
828 pr_err(
829 "BTRFS: Invalid seeding and uuid-changed device detected\n");
830 goto error_brelse;
831 }
832
Anand Jainebbede42017-12-04 12:54:52 +0800833 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800834 fs_devices->seeding = 1;
835 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800836 if (bdev_read_only(bdev))
837 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
838 else
839 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800840 }
841
842 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800843 if (!blk_queue_nonrot(q))
844 fs_devices->rotating = 1;
845
846 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800847 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800848 device->mode = flags;
849
850 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800851 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
852 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800853 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800854 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800855 }
856 brelse(bh);
857
858 return 0;
859
860error_brelse:
861 brelse(bh);
862 blkdev_put(bdev, flags);
863
864 return -EINVAL;
865}
866
David Sterba60999ca2014-03-26 18:26:36 +0100867/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200868 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
869 * being created with a disk that has already completed its fsid change.
870 */
871static struct btrfs_fs_devices *find_fsid_inprogress(
872 struct btrfs_super_block *disk_super)
873{
874 struct btrfs_fs_devices *fs_devices;
875
876 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
877 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
878 BTRFS_FSID_SIZE) != 0 &&
879 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
880 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
881 return fs_devices;
882 }
883 }
884
885 return NULL;
886}
887
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200888
889static struct btrfs_fs_devices *find_fsid_changed(
890 struct btrfs_super_block *disk_super)
891{
892 struct btrfs_fs_devices *fs_devices;
893
894 /*
895 * Handles the case where scanned device is part of an fs that had
896 * multiple successful changes of FSID but curently device didn't
897 * observe it. Meaning our fsid will be different than theirs.
898 */
899 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
900 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
901 BTRFS_FSID_SIZE) != 0 &&
902 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
903 BTRFS_FSID_SIZE) == 0 &&
904 memcmp(fs_devices->fsid, disk_super->fsid,
905 BTRFS_FSID_SIZE) != 0) {
906 return fs_devices;
907 }
908 }
909
910 return NULL;
911}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200912/*
David Sterba60999ca2014-03-26 18:26:36 +0100913 * Add new device to list of registered devices
914 *
915 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800916 * device pointer which was just added or updated when successful
917 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100918 */
Anand Jaine124ece2018-01-18 22:02:35 +0800919static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800920 struct btrfs_super_block *disk_super,
921 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400922{
923 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200924 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400925 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400926 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800927 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200928 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
929 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200930 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
931 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400932
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200933 if (fsid_change_in_progress) {
934 if (!has_metadata_uuid) {
935 /*
936 * When we have an image which has CHANGING_FSID_V2 set
937 * it might belong to either a filesystem which has
938 * disks with completed fsid change or it might belong
939 * to fs with no UUID changes in effect, handle both.
940 */
941 fs_devices = find_fsid_inprogress(disk_super);
942 if (!fs_devices)
943 fs_devices = find_fsid(disk_super->fsid, NULL);
944 } else {
945 fs_devices = find_fsid_changed(disk_super);
946 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200947 } else if (has_metadata_uuid) {
948 fs_devices = find_fsid(disk_super->fsid,
949 disk_super->metadata_uuid);
950 } else {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200951 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200952 }
953
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200954
Chris Mason8a4b83c2008-03-24 15:02:07 -0400955 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200956 if (has_metadata_uuid)
957 fs_devices = alloc_fs_devices(disk_super->fsid,
958 disk_super->metadata_uuid);
959 else
960 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
961
Ilya Dryomov2208a372013-08-12 14:33:03 +0300962 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800963 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300964
Al Viro92900e52019-01-27 04:58:00 +0000965 fs_devices->fsid_change = fsid_change_in_progress;
966
Anand Jain9c6d1732018-05-29 14:10:20 +0800967 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800968 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300969
Chris Mason8a4b83c2008-03-24 15:02:07 -0400970 device = NULL;
971 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800972 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800973 device = btrfs_find_device(fs_devices, devid,
974 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200975
976 /*
977 * If this disk has been pulled into an fs devices created by
978 * a device which had the CHANGING_FSID_V2 flag then replace the
979 * metadata_uuid/fsid values of the fs_devices.
980 */
981 if (has_metadata_uuid && fs_devices->fsid_change &&
982 found_transid > fs_devices->latest_generation) {
983 memcpy(fs_devices->fsid, disk_super->fsid,
984 BTRFS_FSID_SIZE);
985 memcpy(fs_devices->metadata_uuid,
986 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
987
988 fs_devices->fsid_change = false;
989 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400990 }
Miao Xie443f24f2014-07-24 11:37:15 +0800991
Chris Mason8a4b83c2008-03-24 15:02:07 -0400992 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800993 if (fs_devices->opened) {
994 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800995 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800996 }
Yan Zheng2b820322008-11-17 21:11:30 -0500997
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300998 device = btrfs_alloc_device(NULL, &devid,
999 disk_super->dev_item.uuid);
1000 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +08001001 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001002 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +08001003 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001004 }
Josef Bacik606686e2012-06-04 14:03:51 -04001005
1006 name = rcu_string_strdup(path, GFP_NOFS);
1007 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001008 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +08001009 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001010 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001011 }
Josef Bacik606686e2012-06-04 14:03:51 -04001012 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +02001013
Xiao Guangrong1f781602011-04-20 10:09:16 +00001014 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001015 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -04001016
Yan Zheng2b820322008-11-17 21:11:30 -05001017 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001018 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +08001019
1020 if (disk_super->label[0])
1021 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1022 disk_super->label, devid, found_transid, path);
1023 else
1024 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1025 disk_super->fsid, devid, found_transid, path);
1026
Josef Bacik606686e2012-06-04 14:03:51 -04001027 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +08001028 /*
1029 * When FS is already mounted.
1030 * 1. If you are here and if the device->name is NULL that
1031 * means this device was missing at time of FS mount.
1032 * 2. If you are here and if the device->name is different
1033 * from 'path' that means either
1034 * a. The same device disappeared and reappeared with
1035 * different name. or
1036 * b. The missing-disk-which-was-replaced, has
1037 * reappeared now.
1038 *
1039 * We must allow 1 and 2a above. But 2b would be a spurious
1040 * and unintentional.
1041 *
1042 * Further in case of 1 and 2a above, the disk at 'path'
1043 * would have missed some transaction when it was away and
1044 * in case of 2a the stale bdev has to be updated as well.
1045 * 2b must not be allowed at all time.
1046 */
1047
1048 /*
Chris Mason0f23ae72014-09-18 07:49:05 -07001049 * For now, we do allow update to btrfs_fs_device through the
1050 * btrfs dev scan cli after FS has been mounted. We're still
1051 * tracking a problem where systems fail mount by subvolume id
1052 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +08001053 */
Chris Mason0f23ae72014-09-18 07:49:05 -07001054 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +08001055 /*
1056 * That is if the FS is _not_ mounted and if you
1057 * are here, that means there is more than one
1058 * disk with same uuid and devid.We keep the one
1059 * with larger generation number or the last-in if
1060 * generation are equal.
1061 */
Anand Jain9c6d1732018-05-29 14:10:20 +08001062 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001063 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +08001064 }
Anand Jainb96de002014-07-03 18:22:05 +08001065
Anand Jaina9261d42018-10-15 10:45:17 +08001066 /*
1067 * We are going to replace the device path for a given devid,
1068 * make sure it's the same device if the device is mounted
1069 */
1070 if (device->bdev) {
1071 struct block_device *path_bdev;
1072
1073 path_bdev = lookup_bdev(path);
1074 if (IS_ERR(path_bdev)) {
1075 mutex_unlock(&fs_devices->device_list_mutex);
1076 return ERR_CAST(path_bdev);
1077 }
1078
1079 if (device->bdev != path_bdev) {
1080 bdput(path_bdev);
1081 mutex_unlock(&fs_devices->device_list_mutex);
1082 btrfs_warn_in_rcu(device->fs_info,
1083 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1084 disk_super->fsid, devid,
1085 rcu_str_deref(device->name), path);
1086 return ERR_PTR(-EEXIST);
1087 }
1088 bdput(path_bdev);
1089 btrfs_info_in_rcu(device->fs_info,
1090 "device fsid %pU devid %llu moved old:%s new:%s",
1091 disk_super->fsid, devid,
1092 rcu_str_deref(device->name), path);
1093 }
1094
Josef Bacik606686e2012-06-04 14:03:51 -04001095 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +08001096 if (!name) {
1097 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001098 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +08001099 }
Josef Bacik606686e2012-06-04 14:03:51 -04001100 rcu_string_free(device->name);
1101 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +08001102 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05001103 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08001104 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05001105 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001106 }
1107
Anand Jain77bdae42014-07-03 18:22:06 +08001108 /*
1109 * Unmount does not free the btrfs_device struct but would zero
1110 * generation along with most of the other members. So just update
1111 * it back. We need it to pick the disk with largest generation
1112 * (as above).
1113 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001114 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +08001115 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001116 fs_devices->latest_generation = max_t(u64, found_transid,
1117 fs_devices->latest_generation);
1118 }
Anand Jain77bdae42014-07-03 18:22:06 +08001119
Anand Jainf2788d22018-01-18 22:02:34 +08001120 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1121
Anand Jain9c6d1732018-05-29 14:10:20 +08001122 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001123 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001124}
1125
Yan Zhenge4404d62008-12-12 10:03:26 -05001126static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1127{
1128 struct btrfs_fs_devices *fs_devices;
1129 struct btrfs_device *device;
1130 struct btrfs_device *orig_dev;
1131
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001132 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001133 if (IS_ERR(fs_devices))
1134 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001135
Miao Xieadbbb862014-09-03 21:35:42 +08001136 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001137 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001138
1139 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001140 struct rcu_string *name;
1141
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001142 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1143 orig_dev->uuid);
1144 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -05001145 goto error;
1146
Josef Bacik606686e2012-06-04 14:03:51 -04001147 /*
1148 * This is ok to do without rcu read locked because we hold the
1149 * uuid mutex so nothing we touch in here is going to disappear.
1150 */
Anand Jaine755f782014-06-30 17:12:47 +08001151 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001152 name = rcu_string_strdup(orig_dev->name->str,
1153 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001154 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001155 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +08001156 goto error;
1157 }
1158 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001159 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001160
Yan Zhenge4404d62008-12-12 10:03:26 -05001161 list_add(&device->dev_list, &fs_devices->devices);
1162 device->fs_devices = fs_devices;
1163 fs_devices->num_devices++;
1164 }
Miao Xieadbbb862014-09-03 21:35:42 +08001165 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001166 return fs_devices;
1167error:
Miao Xieadbbb862014-09-03 21:35:42 +08001168 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001169 free_fs_devices(fs_devices);
1170 return ERR_PTR(-ENOMEM);
1171}
1172
Anand Jain9b99b112018-02-27 12:41:59 +08001173/*
1174 * After we have read the system tree and know devids belonging to
1175 * this filesystem, remove the device which does not belong there.
1176 */
1177void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001178{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001179 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001180 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001181
Chris Masondfe25022008-05-13 13:46:40 -04001182 mutex_lock(&uuid_mutex);
1183again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001184 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001185 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001186 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1187 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001188 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1189 &device->dev_state) &&
1190 (!latest_dev ||
1191 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001192 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001193 }
Yan Zheng2b820322008-11-17 21:11:30 -05001194 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001195 }
Yan Zheng2b820322008-11-17 21:11:30 -05001196
Stefan Behrens8dabb742012-11-06 13:15:27 +01001197 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1198 /*
1199 * In the first step, keep the device which has
1200 * the correct fsid and the devid that is used
1201 * for the dev_replace procedure.
1202 * In the second step, the dev_replace state is
1203 * read from the device tree and it is known
1204 * whether the procedure is really active or
1205 * not, which means whether this device is
1206 * used or whether it should be removed.
1207 */
Anand Jain401e29c2017-12-04 12:54:55 +08001208 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1209 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001210 continue;
1211 }
1212 }
Yan Zheng2b820322008-11-17 21:11:30 -05001213 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001214 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001215 device->bdev = NULL;
1216 fs_devices->open_devices--;
1217 }
Anand Jainebbede42017-12-04 12:54:52 +08001218 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001219 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001220 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001221 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1222 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001223 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001224 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001225 list_del_init(&device->dev_list);
1226 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001227 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001228 }
Yan Zheng2b820322008-11-17 21:11:30 -05001229
1230 if (fs_devices->seed) {
1231 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001232 goto again;
1233 }
1234
Miao Xie443f24f2014-07-24 11:37:15 +08001235 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001236
Chris Masondfe25022008-05-13 13:46:40 -04001237 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001238}
Chris Masona0af4692008-05-13 16:03:06 -04001239
Anand Jain14238812016-07-22 06:04:53 +08001240static void btrfs_close_bdev(struct btrfs_device *device)
1241{
David Sterba08ffcae2017-06-19 16:55:35 +02001242 if (!device->bdev)
1243 return;
1244
Anand Jainebbede42017-12-04 12:54:52 +08001245 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001246 sync_blockdev(device->bdev);
1247 invalidate_bdev(device->bdev);
1248 }
1249
David Sterba08ffcae2017-06-19 16:55:35 +02001250 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001251}
1252
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001253static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001254{
1255 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1256 struct btrfs_device *new_device;
1257 struct rcu_string *name;
1258
1259 if (device->bdev)
1260 fs_devices->open_devices--;
1261
Anand Jainebbede42017-12-04 12:54:52 +08001262 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001263 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1264 list_del_init(&device->dev_alloc_list);
1265 fs_devices->rw_devices--;
1266 }
1267
Anand Jaine6e674b2017-12-04 12:54:54 +08001268 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001269 fs_devices->missing_devices--;
1270
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001271 btrfs_close_bdev(device);
1272
Anand Jainf448341a2016-06-14 18:55:25 +08001273 new_device = btrfs_alloc_device(NULL, &device->devid,
1274 device->uuid);
1275 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1276
1277 /* Safe because we are under uuid_mutex */
1278 if (device->name) {
1279 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1280 BUG_ON(!name); /* -ENOMEM */
1281 rcu_assign_pointer(new_device->name, name);
1282 }
1283
1284 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1285 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001286
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02001287 synchronize_rcu();
1288 btrfs_free_device(device);
Anand Jainf448341a2016-06-14 18:55:25 +08001289}
1290
Anand Jain0226e0e2018-04-12 10:29:27 +08001291static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001292{
Sasha Levin2037a092015-05-12 19:31:37 -04001293 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001294
Yan Zheng2b820322008-11-17 21:11:30 -05001295 if (--fs_devices->opened > 0)
1296 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001297
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001298 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001299 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001300 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001301 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001302 mutex_unlock(&fs_devices->device_list_mutex);
1303
Yan Zhenge4404d62008-12-12 10:03:26 -05001304 WARN_ON(fs_devices->open_devices);
1305 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001306 fs_devices->opened = 0;
1307 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001308
Chris Mason8a4b83c2008-03-24 15:02:07 -04001309 return 0;
1310}
1311
Yan Zheng2b820322008-11-17 21:11:30 -05001312int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1313{
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001315 int ret;
1316
1317 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001318 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001319 if (!fs_devices->opened) {
1320 seed_devices = fs_devices->seed;
1321 fs_devices->seed = NULL;
1322 }
Yan Zheng2b820322008-11-17 21:11:30 -05001323 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001324
1325 while (seed_devices) {
1326 fs_devices = seed_devices;
1327 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001328 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001329 free_fs_devices(fs_devices);
1330 }
Yan Zheng2b820322008-11-17 21:11:30 -05001331 return ret;
1332}
1333
Anand Jain897fb572018-04-12 10:29:28 +08001334static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001335 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001336{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001337 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001338 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001339 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001340
Tejun Heod4d77622010-11-13 11:55:18 +01001341 flags |= FMODE_EXCL;
1342
Anand Jainf117e292018-04-12 10:29:26 +08001343 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001344 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001345 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001346 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001347
Anand Jain9f050db2017-11-09 23:45:25 +08001348 if (!latest_dev ||
1349 device->generation > latest_dev->generation)
1350 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001351 }
Chris Masona0af4692008-05-13 16:03:06 -04001352 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001353 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001354 goto out;
1355 }
Yan Zheng2b820322008-11-17 21:11:30 -05001356 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001357 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001358 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001359out:
Yan Zheng2b820322008-11-17 21:11:30 -05001360 return ret;
1361}
1362
Anand Jainf8e10cd2018-01-22 14:49:36 -08001363static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1364{
1365 struct btrfs_device *dev1, *dev2;
1366
1367 dev1 = list_entry(a, struct btrfs_device, dev_list);
1368 dev2 = list_entry(b, struct btrfs_device, dev_list);
1369
1370 if (dev1->devid < dev2->devid)
1371 return -1;
1372 else if (dev1->devid > dev2->devid)
1373 return 1;
1374 return 0;
1375}
1376
Yan Zheng2b820322008-11-17 21:11:30 -05001377int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001378 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001379{
1380 int ret;
1381
David Sterbaf5194e32018-06-19 17:09:47 +02001382 lockdep_assert_held(&uuid_mutex);
1383
Anand Jain542c5902018-04-12 10:29:34 +08001384 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001385 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001386 fs_devices->opened++;
1387 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001388 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001389 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001390 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001391 }
Anand Jain542c5902018-04-12 10:29:34 +08001392 mutex_unlock(&fs_devices->device_list_mutex);
1393
Chris Mason8a4b83c2008-03-24 15:02:07 -04001394 return ret;
1395}
1396
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001397static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001398{
1399 kunmap(page);
1400 put_page(page);
1401}
1402
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001403static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1404 struct page **page,
1405 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001406{
1407 void *p;
1408 pgoff_t index;
1409
1410 /* make sure our super fits in the device */
1411 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1412 return 1;
1413
1414 /* make sure our super fits in the page */
1415 if (sizeof(**disk_super) > PAGE_SIZE)
1416 return 1;
1417
1418 /* make sure our super doesn't straddle pages on disk */
1419 index = bytenr >> PAGE_SHIFT;
1420 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1421 return 1;
1422
1423 /* pull in the page with our super */
1424 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1425 index, GFP_KERNEL);
1426
1427 if (IS_ERR_OR_NULL(*page))
1428 return 1;
1429
1430 p = kmap(*page);
1431
1432 /* align our pointer to the offset of the super block */
Johannes Thumshirn70730172018-12-05 15:23:03 +01001433 *disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001434
1435 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1436 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1437 btrfs_release_disk_super(*page);
1438 return 1;
1439 }
1440
1441 if ((*disk_super)->label[0] &&
1442 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1443 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1444
1445 return 0;
1446}
1447
Anand Jain228a73a2019-01-04 13:31:54 +08001448int btrfs_forget_devices(const char *path)
1449{
1450 int ret;
1451
1452 mutex_lock(&uuid_mutex);
1453 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1454 mutex_unlock(&uuid_mutex);
1455
1456 return ret;
1457}
1458
David Sterba6f60cbd2013-02-15 11:31:02 -07001459/*
1460 * Look for a btrfs signature on a device. This may be called out of the mount path
1461 * and we are not allowed to call set_blocksize during the scan. The superblock
1462 * is read via pagecache
1463 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001464struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1465 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001466{
1467 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001468 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001469 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001470 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001471 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001472 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001473
David Sterba899f9302018-06-19 16:37:36 +02001474 lockdep_assert_held(&uuid_mutex);
1475
David Sterba6f60cbd2013-02-15 11:31:02 -07001476 /*
1477 * we would like to check all the supers, but that would make
1478 * a btrfs mount succeed after a mkfs from a different FS.
1479 * So, we need to add a special mount option to scan for
1480 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1481 */
1482 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001483 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001484
1485 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001486 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001487 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001488
Anand Jain05a5c552017-12-15 15:40:16 +08001489 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001490 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001491 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001492 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001493
Anand Jain4306a972018-05-29 12:28:37 +08001494 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001495 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001496 if (new_device_added)
1497 btrfs_free_stale_devices(path, device);
1498 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001499
Anand Jain6cf86a002016-02-13 10:01:29 +08001500 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001501
1502error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001503 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001504
Gu Jinxiang36350e92018-07-12 14:23:16 +08001505 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001506}
Chris Mason0b86a832008-03-24 15:01:56 -04001507
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001508/*
1509 * Try to find a chunk that intersects [start, start + len] range and when one
1510 * such is found, record the end of it in *start
1511 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001512static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1513 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001514{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001515 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001516
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001517 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001518
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001519 if (!find_first_extent_bit(&device->alloc_state, *start,
1520 &physical_start, &physical_end,
1521 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001522
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001523 if (in_range(physical_start, *start, len) ||
1524 in_range(*start, physical_start,
1525 physical_end - physical_start)) {
1526 *start = physical_end + 1;
1527 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001528 }
1529 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001530 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001531}
1532
1533
Chris Mason0b86a832008-03-24 15:01:56 -04001534/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001535 * find_free_dev_extent_start - find free space in the specified device
1536 * @device: the device which we search the free space in
1537 * @num_bytes: the size of the free space that we need
1538 * @search_start: the position from which to begin the search
1539 * @start: store the start of the free space.
1540 * @len: the size of the free space. that we find, or the size
1541 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001542 *
Chris Mason0b86a832008-03-24 15:01:56 -04001543 * this uses a pretty simple search, the expectation is that it is
1544 * called very infrequently and that a given device has a small number
1545 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001546 *
1547 * @start is used to store the start of the free space if we find. But if we
1548 * don't find suitable free space, it will be used to store the start position
1549 * of the max free space.
1550 *
1551 * @len is used to store the size of the free space that we find.
1552 * But if we don't find suitable free space, it is used to store the size of
1553 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001554 */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001555int find_free_dev_extent_start(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001556 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001558 struct btrfs_fs_info *fs_info = device->fs_info;
1559 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001560 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001561 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001562 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001563 u64 hole_size;
1564 u64 max_hole_start;
1565 u64 max_hole_size;
1566 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001567 u64 search_end = device->total_bytes;
1568 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001569 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001570 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001571
1572 /*
1573 * We don't want to overwrite the superblock on the drive nor any area
1574 * used by the boot loader (grub for example), so we make sure to start
1575 * at an offset of at least 1MB.
1576 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001577 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001578
Josef Bacik6df9a952013-06-27 13:22:46 -04001579 path = btrfs_alloc_path();
1580 if (!path)
1581 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001582
Miao Xie7bfc8372011-01-05 10:07:26 +00001583 max_hole_start = search_start;
1584 max_hole_size = 0;
1585
Zhao Leif2ab7612015-02-16 18:52:17 +08001586again:
Anand Jain401e29c2017-12-04 12:54:55 +08001587 if (search_start >= search_end ||
1588 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001589 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001590 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001591 }
1592
David Sterbae4058b52015-11-27 16:31:35 +01001593 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001594 path->search_commit_root = 1;
1595 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001596
Chris Mason0b86a832008-03-24 15:01:56 -04001597 key.objectid = device->devid;
1598 key.offset = search_start;
1599 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001600
Li Zefan125ccb02011-12-08 15:07:24 +08001601 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001602 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001603 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001604 if (ret > 0) {
1605 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1606 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001607 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001608 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001609
Chris Mason0b86a832008-03-24 15:01:56 -04001610 while (1) {
1611 l = path->nodes[0];
1612 slot = path->slots[0];
1613 if (slot >= btrfs_header_nritems(l)) {
1614 ret = btrfs_next_leaf(root, path);
1615 if (ret == 0)
1616 continue;
1617 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001618 goto out;
1619
1620 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001621 }
1622 btrfs_item_key_to_cpu(l, &key, slot);
1623
1624 if (key.objectid < device->devid)
1625 goto next;
1626
1627 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001628 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001629
David Sterba962a2982014-06-04 18:41:45 +02001630 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001631 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001632
Miao Xie7bfc8372011-01-05 10:07:26 +00001633 if (key.offset > search_start) {
1634 hole_size = key.offset - search_start;
1635
Josef Bacik6df9a952013-06-27 13:22:46 -04001636 /*
1637 * Have to check before we set max_hole_start, otherwise
1638 * we could end up sending back this offset anyway.
1639 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001640 if (contains_pending_extent(device, &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001641 hole_size)) {
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001642 if (key.offset >= search_start)
Forrest Liu1b984502015-02-09 17:30:47 +08001643 hole_size = key.offset - search_start;
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001644 else
Forrest Liu1b984502015-02-09 17:30:47 +08001645 hole_size = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001646 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001647
Miao Xie7bfc8372011-01-05 10:07:26 +00001648 if (hole_size > max_hole_size) {
1649 max_hole_start = search_start;
1650 max_hole_size = hole_size;
1651 }
1652
1653 /*
1654 * If this free space is greater than which we need,
1655 * it must be the max free space that we have found
1656 * until now, so max_hole_start must point to the start
1657 * of this free space and the length of this free space
1658 * is stored in max_hole_size. Thus, we return
1659 * max_hole_start and max_hole_size and go back to the
1660 * caller.
1661 */
1662 if (hole_size >= num_bytes) {
1663 ret = 0;
1664 goto out;
1665 }
1666 }
1667
Chris Mason0b86a832008-03-24 15:01:56 -04001668 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001669 extent_end = key.offset + btrfs_dev_extent_length(l,
1670 dev_extent);
1671 if (extent_end > search_start)
1672 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001673next:
1674 path->slots[0]++;
1675 cond_resched();
1676 }
Chris Mason0b86a832008-03-24 15:01:56 -04001677
liubo38c01b92011-08-02 02:39:03 +00001678 /*
1679 * At this point, search_start should be the end of
1680 * allocated dev extents, and when shrinking the device,
1681 * search_end may be smaller than search_start.
1682 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001683 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001684 hole_size = search_end - search_start;
1685
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001686 if (contains_pending_extent(device, &search_start, hole_size)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001687 btrfs_release_path(path);
1688 goto again;
1689 }
Chris Mason0b86a832008-03-24 15:01:56 -04001690
Zhao Leif2ab7612015-02-16 18:52:17 +08001691 if (hole_size > max_hole_size) {
1692 max_hole_start = search_start;
1693 max_hole_size = hole_size;
1694 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001695 }
1696
Miao Xie7bfc8372011-01-05 10:07:26 +00001697 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001698 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001699 ret = -ENOSPC;
1700 else
1701 ret = 0;
1702
1703out:
Yan Zheng2b820322008-11-17 21:11:30 -05001704 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001705 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001706 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001707 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001708 return ret;
1709}
1710
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001711int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001712 u64 *start, u64 *len)
1713{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001714 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001715 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001716}
1717
Christoph Hellwigb2950862008-12-02 09:54:17 -05001718static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001719 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001720 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001721{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001722 struct btrfs_fs_info *fs_info = device->fs_info;
1723 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001724 int ret;
1725 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001726 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001727 struct btrfs_key found_key;
1728 struct extent_buffer *leaf = NULL;
1729 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001730
1731 path = btrfs_alloc_path();
1732 if (!path)
1733 return -ENOMEM;
1734
1735 key.objectid = device->devid;
1736 key.offset = start;
1737 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001738again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001739 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001740 if (ret > 0) {
1741 ret = btrfs_previous_item(root, path, key.objectid,
1742 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001743 if (ret)
1744 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001745 leaf = path->nodes[0];
1746 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1747 extent = btrfs_item_ptr(leaf, path->slots[0],
1748 struct btrfs_dev_extent);
1749 BUG_ON(found_key.offset > start || found_key.offset +
1750 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001751 key = found_key;
1752 btrfs_release_path(path);
1753 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001754 } else if (ret == 0) {
1755 leaf = path->nodes[0];
1756 extent = btrfs_item_ptr(leaf, path->slots[0],
1757 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001758 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001759 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001760 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001761 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001762
Miao Xie2196d6e2014-09-03 21:35:41 +08001763 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1764
Chris Mason8f18cf12008-04-25 16:53:30 -04001765 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001766 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001767 btrfs_handle_fs_error(fs_info, ret,
1768 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001769 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001770 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001771 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001772out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001773 btrfs_free_path(path);
1774 return ret;
1775}
1776
Eric Sandeen48a3b632013-04-25 20:41:01 +00001777static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1778 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001779 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001780{
1781 int ret;
1782 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001783 struct btrfs_fs_info *fs_info = device->fs_info;
1784 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001785 struct btrfs_dev_extent *extent;
1786 struct extent_buffer *leaf;
1787 struct btrfs_key key;
1788
Anand Jaine12c9622017-12-04 12:54:53 +08001789 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001790 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001791 path = btrfs_alloc_path();
1792 if (!path)
1793 return -ENOMEM;
1794
Chris Mason0b86a832008-03-24 15:01:56 -04001795 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001796 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001797 key.type = BTRFS_DEV_EXTENT_KEY;
1798 ret = btrfs_insert_empty_item(trans, root, path, &key,
1799 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001800 if (ret)
1801 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001802
1803 leaf = path->nodes[0];
1804 extent = btrfs_item_ptr(leaf, path->slots[0],
1805 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001806 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1807 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001808 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1809 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001810 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1811
Chris Mason0b86a832008-03-24 15:01:56 -04001812 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1813 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001814out:
Chris Mason0b86a832008-03-24 15:01:56 -04001815 btrfs_free_path(path);
1816 return ret;
1817}
1818
Josef Bacik6df9a952013-06-27 13:22:46 -04001819static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001820{
Josef Bacik6df9a952013-06-27 13:22:46 -04001821 struct extent_map_tree *em_tree;
1822 struct extent_map *em;
1823 struct rb_node *n;
1824 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001825
David Sterbac8bf1b62019-05-17 11:43:17 +02001826 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001827 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001828 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001829 if (n) {
1830 em = rb_entry(n, struct extent_map, rb_node);
1831 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001832 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001833 read_unlock(&em_tree->lock);
1834
Chris Mason0b86a832008-03-24 15:01:56 -04001835 return ret;
1836}
1837
Ilya Dryomov53f10652013-08-12 14:33:01 +03001838static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1839 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001840{
1841 int ret;
1842 struct btrfs_key key;
1843 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001844 struct btrfs_path *path;
1845
Yan Zheng2b820322008-11-17 21:11:30 -05001846 path = btrfs_alloc_path();
1847 if (!path)
1848 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001849
1850 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1851 key.type = BTRFS_DEV_ITEM_KEY;
1852 key.offset = (u64)-1;
1853
Ilya Dryomov53f10652013-08-12 14:33:01 +03001854 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001855 if (ret < 0)
1856 goto error;
1857
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001858 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001859
Ilya Dryomov53f10652013-08-12 14:33:01 +03001860 ret = btrfs_previous_item(fs_info->chunk_root, path,
1861 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001862 BTRFS_DEV_ITEM_KEY);
1863 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001864 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001865 } else {
1866 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1867 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001868 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001869 }
1870 ret = 0;
1871error:
Yan Zheng2b820322008-11-17 21:11:30 -05001872 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001873 return ret;
1874}
1875
1876/*
1877 * the device information is stored in the chunk root
1878 * the btrfs_device struct should be fully filled in
1879 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001880static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001881 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001882{
1883 int ret;
1884 struct btrfs_path *path;
1885 struct btrfs_dev_item *dev_item;
1886 struct extent_buffer *leaf;
1887 struct btrfs_key key;
1888 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001889
Chris Mason0b86a832008-03-24 15:01:56 -04001890 path = btrfs_alloc_path();
1891 if (!path)
1892 return -ENOMEM;
1893
Chris Mason0b86a832008-03-24 15:01:56 -04001894 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1895 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001896 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001897
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001898 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1899 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001900 if (ret)
1901 goto out;
1902
1903 leaf = path->nodes[0];
1904 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1905
1906 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001907 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001908 btrfs_set_device_type(leaf, dev_item, device->type);
1909 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1910 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1911 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001912 btrfs_set_device_total_bytes(leaf, dev_item,
1913 btrfs_device_get_disk_total_bytes(device));
1914 btrfs_set_device_bytes_used(leaf, dev_item,
1915 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001916 btrfs_set_device_group(leaf, dev_item, 0);
1917 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1918 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001919 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001920
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001921 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001922 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001923 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001924 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1925 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001926 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001927
Yan Zheng2b820322008-11-17 21:11:30 -05001928 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001929out:
1930 btrfs_free_path(path);
1931 return ret;
1932}
Chris Mason8f18cf12008-04-25 16:53:30 -04001933
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001934/*
1935 * Function to update ctime/mtime for a given device path.
1936 * Mainly used for ctime/mtime based probe like libblkid.
1937 */
David Sterbada353f62017-02-14 17:55:53 +01001938static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001939{
1940 struct file *filp;
1941
1942 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001943 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001944 return;
1945 file_update_time(filp);
1946 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001947}
1948
David Sterbaf331a952019-03-20 16:31:53 +01001949static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001950{
David Sterbaf331a952019-03-20 16:31:53 +01001951 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001952 int ret;
1953 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001954 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001955 struct btrfs_trans_handle *trans;
1956
Chris Masona061fc82008-05-07 11:43:44 -04001957 path = btrfs_alloc_path();
1958 if (!path)
1959 return -ENOMEM;
1960
Yan, Zhenga22285a2010-05-16 10:48:46 -04001961 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001962 if (IS_ERR(trans)) {
1963 btrfs_free_path(path);
1964 return PTR_ERR(trans);
1965 }
Chris Masona061fc82008-05-07 11:43:44 -04001966 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1967 key.type = BTRFS_DEV_ITEM_KEY;
1968 key.offset = device->devid;
1969
1970 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001971 if (ret) {
1972 if (ret > 0)
1973 ret = -ENOENT;
1974 btrfs_abort_transaction(trans, ret);
1975 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001976 goto out;
1977 }
1978
1979 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001980 if (ret) {
1981 btrfs_abort_transaction(trans, ret);
1982 btrfs_end_transaction(trans);
1983 }
1984
Chris Masona061fc82008-05-07 11:43:44 -04001985out:
1986 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001987 if (!ret)
1988 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001989 return ret;
1990}
1991
David Sterba3cc31a02016-02-15 16:00:26 +01001992/*
1993 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1994 * filesystem. It's up to the caller to adjust that number regarding eg. device
1995 * replace.
1996 */
1997static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1998 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001999{
Chris Masona061fc82008-05-07 11:43:44 -04002000 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00002001 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01002002 int i;
Chris Masona061fc82008-05-07 11:43:44 -04002003
Miao Xiede98ced2013-01-29 10:13:12 +00002004 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08002005 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002006
Anand Jainbd45ffb2016-02-13 10:01:34 +08002007 all_avail = fs_info->avail_data_alloc_bits |
2008 fs_info->avail_system_alloc_bits |
2009 fs_info->avail_metadata_alloc_bits;
2010 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002011
David Sterba418775a2016-02-15 16:28:14 +01002012 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002013 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002014 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002015
David Sterba418775a2016-02-15 16:28:14 +01002016 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002017 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002018
David Sterba418775a2016-02-15 16:28:14 +01002019 if (ret)
2020 return ret;
2021 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002022 }
2023
2024 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002025}
2026
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002027static struct btrfs_device * btrfs_find_next_active_device(
2028 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002029{
2030 struct btrfs_device *next_device;
2031
2032 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2033 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002034 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2035 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002036 return next_device;
2037 }
2038
2039 return NULL;
2040}
2041
2042/*
2043 * Helper function to check if the given device is part of s_bdev / latest_bdev
2044 * and replace it with the provided or the next active device, in the context
2045 * where this function called, there should be always be another device (or
2046 * this_dev) which is active.
2047 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002048void btrfs_assign_next_active_device(struct btrfs_device *device,
2049 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08002050{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002051 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002052 struct btrfs_device *next_device;
2053
2054 if (this_dev)
2055 next_device = this_dev;
2056 else
2057 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2058 device);
2059 ASSERT(next_device);
2060
2061 if (fs_info->sb->s_bdev &&
2062 (fs_info->sb->s_bdev == device->bdev))
2063 fs_info->sb->s_bdev = next_device->bdev;
2064
2065 if (fs_info->fs_devices->latest_bdev == device->bdev)
2066 fs_info->fs_devices->latest_bdev = next_device->bdev;
2067}
2068
Anand Jain1da73962018-08-10 13:53:21 +08002069/*
2070 * Return btrfs_fs_devices::num_devices excluding the device that's being
2071 * currently replaced.
2072 */
2073static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2074{
2075 u64 num_devices = fs_info->fs_devices->num_devices;
2076
David Sterbacb5583d2018-09-07 16:11:23 +02002077 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002078 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2079 ASSERT(num_devices > 1);
2080 num_devices--;
2081 }
David Sterbacb5583d2018-09-07 16:11:23 +02002082 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002083
2084 return num_devices;
2085}
2086
David Sterbada353f62017-02-14 17:55:53 +01002087int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2088 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002089{
2090 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002091 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002092 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002093 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002094 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002095
2096 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002097
Anand Jain1da73962018-08-10 13:53:21 +08002098 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002099
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002100 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002101 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002102 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002103
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002104 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2105
2106 if (IS_ERR(device)) {
2107 if (PTR_ERR(device) == -ENOENT &&
2108 strcmp(device_path, "missing") == 0)
2109 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2110 else
2111 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002112 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002113 }
Yan Zheng2b820322008-11-17 21:11:30 -05002114
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002115 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2116 btrfs_warn_in_rcu(fs_info,
2117 "cannot remove device %s (devid %llu) due to active swapfile",
2118 rcu_str_deref(device->name), device->devid);
2119 ret = -ETXTBSY;
2120 goto out;
2121 }
2122
Anand Jain401e29c2017-12-04 12:54:55 +08002123 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002124 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002125 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002126 }
2127
Anand Jainebbede42017-12-04 12:54:52 +08002128 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2129 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002130 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002131 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002132 }
2133
Anand Jainebbede42017-12-04 12:54:52 +08002134 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002135 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002136 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002137 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002138 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002139 }
Chris Masona061fc82008-05-07 11:43:44 -04002140
Carey Underwoodd7901552013-03-04 16:37:06 -06002141 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002142 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002143 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002144 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002145 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002146
Stefan Behrens63a212a2012-11-05 18:29:28 +01002147 /*
2148 * TODO: the superblock still includes this device in its num_devices
2149 * counter although write_all_supers() is not locked out. This
2150 * could give a filesystem state which requires a degraded mount.
2151 */
David Sterbaf331a952019-03-20 16:31:53 +01002152 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002153 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002154 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002155
Anand Jaine12c9622017-12-04 12:54:53 +08002156 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002157 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002158
2159 /*
2160 * the device list mutex makes sure that we don't change
2161 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002162 * the device supers. Whoever is writing all supers, should
2163 * lock the device list mutex before getting the number of
2164 * devices in the super block (super_copy). Conversely,
2165 * whoever updates the number of devices in the super block
2166 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002167 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002168
Anand Jain41a52a02018-04-12 10:29:31 +08002169 /*
2170 * In normal cases the cur_devices == fs_devices. But in case
2171 * of deleting a seed device, the cur_devices should point to
2172 * its own fs_devices listed under the fs_devices->seed.
2173 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002174 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002175 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002176 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002177
Anand Jain41a52a02018-04-12 10:29:31 +08002178 cur_devices->num_devices--;
2179 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002180 /* Update total_devices of the parent fs_devices if it's seed */
2181 if (cur_devices != fs_devices)
2182 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002183
Anand Jaine6e674b2017-12-04 12:54:54 +08002184 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002185 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002186
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002187 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002188
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002189 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002190 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002191 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002192 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002193 }
Anand Jain99994cd2014-06-03 11:36:00 +08002194
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002195 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2196 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002197 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002198
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002199 /*
2200 * at this point, the device is zero sized and detached from
2201 * the devices list. All that's left is to zero out the old
2202 * supers and free the device.
2203 */
Anand Jainebbede42017-12-04 12:54:52 +08002204 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002205 btrfs_scratch_superblocks(device->bdev, device->name->str);
2206
2207 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002208 synchronize_rcu();
2209 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002210
Xiao Guangrong1f781602011-04-20 10:09:16 +00002211 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002212 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002213 if (fs_devices->seed == cur_devices) {
2214 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002215 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002216 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002217 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002218 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002219 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002220 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002221 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002222 }
2223
Chris Masona061fc82008-05-07 11:43:44 -04002224out:
2225 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002226 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002227
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002228error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002229 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002230 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002231 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002232 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002233 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002234 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002235 }
Anand Jain24fc5722016-02-13 10:01:36 +08002236 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002237}
2238
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002239void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002240{
Anand Jaind51908c2014-08-13 14:24:19 +08002241 struct btrfs_fs_devices *fs_devices;
2242
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002243 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002244
Anand Jain25e8e912014-08-20 10:56:56 +08002245 /*
2246 * in case of fs with no seed, srcdev->fs_devices will point
2247 * to fs_devices of fs_info. However when the dev being replaced is
2248 * a seed dev it will point to the seed's local fs_devices. In short
2249 * srcdev will have its correct fs_devices in both the cases.
2250 */
2251 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002252
Stefan Behrense93c89c2012-11-05 17:33:06 +01002253 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002254 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002255 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002256 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002257 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002258
Anand Jainebbede42017-12-04 12:54:52 +08002259 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002260 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002261
Miao Xie82372bc2014-09-03 21:35:44 +08002262 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002263 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002264}
2265
David Sterba65237ee2019-03-20 16:34:54 +01002266void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002267{
David Sterba65237ee2019-03-20 16:34:54 +01002268 struct btrfs_fs_info *fs_info = srcdev->fs_info;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002269 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002270
Anand Jainebbede42017-12-04 12:54:52 +08002271 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002272 /* zero out the old super if it is writable */
2273 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2274 }
Anand Jain14238812016-07-22 06:04:53 +08002275
2276 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002277 synchronize_rcu();
2278 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002279
Anand Jain94d5f0c2014-08-13 14:24:22 +08002280 /* if this is no devs we rather delete the fs_devices */
2281 if (!fs_devices->num_devices) {
2282 struct btrfs_fs_devices *tmp_fs_devices;
2283
Anand Jain6dd38f82017-10-17 06:53:50 +08002284 /*
2285 * On a mounted FS, num_devices can't be zero unless it's a
2286 * seed. In case of a seed device being replaced, the replace
2287 * target added to the sprout FS, so there will be no more
2288 * device left under the seed FS.
2289 */
2290 ASSERT(fs_devices->seeding);
2291
Anand Jain94d5f0c2014-08-13 14:24:22 +08002292 tmp_fs_devices = fs_info->fs_devices;
2293 while (tmp_fs_devices) {
2294 if (tmp_fs_devices->seed == fs_devices) {
2295 tmp_fs_devices->seed = fs_devices->seed;
2296 break;
2297 }
2298 tmp_fs_devices = tmp_fs_devices->seed;
2299 }
2300 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002301 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002302 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002303 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002304}
2305
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002306void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002307{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002308 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002309
Anand Jaind9a071f2018-04-12 10:29:38 +08002310 WARN_ON(!tgtdev);
2311 mutex_lock(&fs_devices->device_list_mutex);
2312
2313 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002314
Anand Jain779bf3fe2016-04-18 16:51:23 +08002315 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002316 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002317
Anand Jaind9a071f2018-04-12 10:29:38 +08002318 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002319
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002320 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002321
Stefan Behrense93c89c2012-11-05 17:33:06 +01002322 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002323
Anand Jaind9a071f2018-04-12 10:29:38 +08002324 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002325
2326 /*
2327 * The update_dev_time() with in btrfs_scratch_superblocks()
2328 * may lead to a call to btrfs_show_devname() which will try
2329 * to hold device_list_mutex. And here this device
2330 * is already out of device list, so we don't have to hold
2331 * the device_list_mutex lock.
2332 */
2333 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002334
2335 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002336 synchronize_rcu();
2337 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002338}
2339
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002340static struct btrfs_device *btrfs_find_device_by_path(
2341 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002342{
2343 int ret = 0;
2344 struct btrfs_super_block *disk_super;
2345 u64 devid;
2346 u8 *dev_uuid;
2347 struct block_device *bdev;
2348 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002349 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002350
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002351 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002352 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002353 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002354 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002355 disk_super = (struct btrfs_super_block *)bh->b_data;
2356 devid = btrfs_stack_device_id(&disk_super->dev_item);
2357 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002358 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002359 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002360 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002361 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002362 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002363 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002364
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002365 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002366 if (!device)
2367 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002368 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002369 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002370}
2371
Yan Zheng2b820322008-11-17 21:11:30 -05002372/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002373 * Lookup a device given by device id, or the path if the id is 0.
2374 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002375struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002376 struct btrfs_fs_info *fs_info, u64 devid,
2377 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002378{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002379 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002380
David Sterba5c5c0df2016-02-15 16:39:55 +01002381 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002382 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002383 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002384 if (!device)
2385 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002386 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002387 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002388
2389 if (!device_path || !device_path[0])
2390 return ERR_PTR(-EINVAL);
2391
2392 if (strcmp(device_path, "missing") == 0) {
2393 /* Find first missing device */
2394 list_for_each_entry(device, &fs_info->fs_devices->devices,
2395 dev_list) {
2396 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2397 &device->dev_state) && !device->bdev)
2398 return device;
2399 }
2400 return ERR_PTR(-ENOENT);
2401 }
2402
2403 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002404}
2405
Yan Zheng2b820322008-11-17 21:11:30 -05002406/*
2407 * does all the dirty work required for changing file system's UUID.
2408 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002409static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002410{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002411 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002412 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002413 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002414 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002415 struct btrfs_device *device;
2416 u64 super_flags;
2417
David Sterbaa32bf9a2018-03-16 02:21:22 +01002418 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002419 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002420 return -EINVAL;
2421
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002422 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002423 if (IS_ERR(seed_devices))
2424 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002425
Yan Zhenge4404d62008-12-12 10:03:26 -05002426 old_devices = clone_fs_devices(fs_devices);
2427 if (IS_ERR(old_devices)) {
2428 kfree(seed_devices);
2429 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002430 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002431
Anand Jainc4babc52018-04-12 10:29:25 +08002432 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002433
Yan Zhenge4404d62008-12-12 10:03:26 -05002434 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2435 seed_devices->opened = 1;
2436 INIT_LIST_HEAD(&seed_devices->devices);
2437 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002438 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002439
Anand Jain321a4bf2018-07-16 22:58:09 +08002440 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002441 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2442 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002443 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002444 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002445
David Sterba34441362016-10-04 19:34:27 +02002446 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002447 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002448 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002449
Yan Zheng2b820322008-11-17 21:11:30 -05002450 fs_devices->seeding = 0;
2451 fs_devices->num_devices = 0;
2452 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002453 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002454 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002455 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002456
2457 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002458 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002459 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002460 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002461
Yan Zheng2b820322008-11-17 21:11:30 -05002462 super_flags = btrfs_super_flags(disk_super) &
2463 ~BTRFS_SUPER_FLAG_SEEDING;
2464 btrfs_set_super_flags(disk_super, super_flags);
2465
2466 return 0;
2467}
2468
2469/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002470 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002471 */
David Sterba5c4666292019-03-20 16:36:39 +01002472static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002473{
David Sterba5c4666292019-03-20 16:36:39 +01002474 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002475 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002476 struct btrfs_path *path;
2477 struct extent_buffer *leaf;
2478 struct btrfs_dev_item *dev_item;
2479 struct btrfs_device *device;
2480 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002481 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002482 u8 dev_uuid[BTRFS_UUID_SIZE];
2483 u64 devid;
2484 int ret;
2485
2486 path = btrfs_alloc_path();
2487 if (!path)
2488 return -ENOMEM;
2489
Yan Zheng2b820322008-11-17 21:11:30 -05002490 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2491 key.offset = 0;
2492 key.type = BTRFS_DEV_ITEM_KEY;
2493
2494 while (1) {
2495 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2496 if (ret < 0)
2497 goto error;
2498
2499 leaf = path->nodes[0];
2500next_slot:
2501 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2502 ret = btrfs_next_leaf(root, path);
2503 if (ret > 0)
2504 break;
2505 if (ret < 0)
2506 goto error;
2507 leaf = path->nodes[0];
2508 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002509 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002510 continue;
2511 }
2512
2513 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2514 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2515 key.type != BTRFS_DEV_ITEM_KEY)
2516 break;
2517
2518 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2519 struct btrfs_dev_item);
2520 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002521 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002522 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002523 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002524 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002525 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002526 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002527 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002528
2529 if (device->fs_devices->seeding) {
2530 btrfs_set_device_generation(leaf, dev_item,
2531 device->generation);
2532 btrfs_mark_buffer_dirty(leaf);
2533 }
2534
2535 path->slots[0]++;
2536 goto next_slot;
2537 }
2538 ret = 0;
2539error:
2540 btrfs_free_path(path);
2541 return ret;
2542}
2543
David Sterbada353f62017-02-14 17:55:53 +01002544int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002545{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002546 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002547 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002548 struct btrfs_trans_handle *trans;
2549 struct btrfs_device *device;
2550 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002551 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002552 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002553 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002554 u64 orig_super_total_bytes;
2555 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002556 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002557 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002558 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002559
Anand Jain5da54bc2018-07-03 13:14:50 +08002560 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002561 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002562
Li Zefana5d16332011-12-07 20:08:40 -05002563 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002564 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002565 if (IS_ERR(bdev))
2566 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002567
Anand Jain5da54bc2018-07-03 13:14:50 +08002568 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002569 seeding_dev = 1;
2570 down_write(&sb->s_umount);
2571 mutex_lock(&uuid_mutex);
2572 }
2573
Chris Mason8c8bee12008-09-29 11:19:10 -04002574 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002575
Anand Jain5da54bc2018-07-03 13:14:50 +08002576 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002577 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002578 if (device->bdev == bdev) {
2579 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002580 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002581 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002582 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002583 }
2584 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002585 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002586
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002587 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002588 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002589 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002590 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002591 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002592 }
2593
David Sterba78f2c9e2016-02-11 14:25:38 +01002594 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002595 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002596 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002597 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002598 }
Josef Bacik606686e2012-06-04 14:03:51 -04002599 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002600
Yan, Zhenga22285a2010-05-16 10:48:46 -04002601 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002602 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002603 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002604 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002605 }
2606
Josef Bacikd5e20032011-08-04 14:52:27 +00002607 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002608 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002609 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002610 device->io_width = fs_info->sectorsize;
2611 device->io_align = fs_info->sectorsize;
2612 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002613 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2614 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002615 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002616 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002617 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002618 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002619 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002620 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002621 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002622 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002623 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002624
Yan Zheng2b820322008-11-17 21:11:30 -05002625 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002626 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002627 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002628 if (ret) {
2629 btrfs_abort_transaction(trans, ret);
2630 goto error_trans;
2631 }
Yan Zheng2b820322008-11-17 21:11:30 -05002632 }
2633
Anand Jain5da54bc2018-07-03 13:14:50 +08002634 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002635
Anand Jain5da54bc2018-07-03 13:14:50 +08002636 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002637 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002638 list_add_rcu(&device->dev_list, &fs_devices->devices);
2639 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2640 fs_devices->num_devices++;
2641 fs_devices->open_devices++;
2642 fs_devices->rw_devices++;
2643 fs_devices->total_devices++;
2644 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002645
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002646 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002647
Anand Jaine884f4f2017-04-04 18:40:19 +08002648 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002649 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002650
Naohiro Aota39379fa2018-07-27 09:04:55 +09002651 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002652 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002653 round_down(orig_super_total_bytes + device->total_bytes,
2654 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002655
Naohiro Aota39379fa2018-07-27 09:04:55 +09002656 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2657 btrfs_set_super_num_devices(fs_info->super_copy,
2658 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002659
2660 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002661 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002662
Miao Xie2196d6e2014-09-03 21:35:41 +08002663 /*
2664 * we've got more storage, clear any full flags on the space
2665 * infos
2666 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002668
David Sterba34441362016-10-04 19:34:27 +02002669 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002670 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002671
Yan Zheng2b820322008-11-17 21:11:30 -05002672 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002673 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002674 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002675 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002676 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002677 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002678 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002679 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002680 }
2681
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002682 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002683 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002684 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002685 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002686 }
2687
2688 if (seeding_dev) {
2689 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2690
David Sterba5c4666292019-03-20 16:36:39 +01002691 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002692 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002693 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002694 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002695 }
Anand Jainb2373f22014-06-03 11:36:03 +08002696
2697 /* Sprouting would change fsid of the mounted root,
2698 * so rename the fsid on the sysfs
2699 */
2700 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002701 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002702 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002703 btrfs_warn(fs_info,
2704 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002705 }
2706
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002707 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002708
2709 if (seeding_dev) {
2710 mutex_unlock(&uuid_mutex);
2711 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002712 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002713
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002714 if (ret) /* transaction commit */
2715 return ret;
2716
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002717 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002718 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002719 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002720 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002721 trans = btrfs_attach_transaction(root);
2722 if (IS_ERR(trans)) {
2723 if (PTR_ERR(trans) == -ENOENT)
2724 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002725 ret = PTR_ERR(trans);
2726 trans = NULL;
2727 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002728 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002729 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002730 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002731
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002732 /* Update ctime/mtime for libblkid */
2733 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002734 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002735
Anand Jaind31c32f2017-09-28 14:51:10 +08002736error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002737 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002738 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2739 mutex_lock(&fs_info->chunk_mutex);
2740 list_del_rcu(&device->dev_list);
2741 list_del(&device->dev_alloc_list);
2742 fs_info->fs_devices->num_devices--;
2743 fs_info->fs_devices->open_devices--;
2744 fs_info->fs_devices->rw_devices--;
2745 fs_info->fs_devices->total_devices--;
2746 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2747 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2748 btrfs_set_super_total_bytes(fs_info->super_copy,
2749 orig_super_total_bytes);
2750 btrfs_set_super_num_devices(fs_info->super_copy,
2751 orig_super_num_devices);
2752 mutex_unlock(&fs_info->chunk_mutex);
2753 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002754error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002755 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002756 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002757 if (trans)
2758 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002759error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002760 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002761error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002762 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002763 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002764 mutex_unlock(&uuid_mutex);
2765 up_write(&sb->s_umount);
2766 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002767 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002768}
2769
Chris Masond3977122009-01-05 21:25:51 -05002770static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2771 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002772{
2773 int ret;
2774 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002775 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002776 struct btrfs_dev_item *dev_item;
2777 struct extent_buffer *leaf;
2778 struct btrfs_key key;
2779
Chris Mason0b86a832008-03-24 15:01:56 -04002780 path = btrfs_alloc_path();
2781 if (!path)
2782 return -ENOMEM;
2783
2784 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2785 key.type = BTRFS_DEV_ITEM_KEY;
2786 key.offset = device->devid;
2787
2788 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2789 if (ret < 0)
2790 goto out;
2791
2792 if (ret > 0) {
2793 ret = -ENOENT;
2794 goto out;
2795 }
2796
2797 leaf = path->nodes[0];
2798 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2799
2800 btrfs_set_device_id(leaf, dev_item, device->devid);
2801 btrfs_set_device_type(leaf, dev_item, device->type);
2802 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2803 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2804 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002805 btrfs_set_device_total_bytes(leaf, dev_item,
2806 btrfs_device_get_disk_total_bytes(device));
2807 btrfs_set_device_bytes_used(leaf, dev_item,
2808 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002809 btrfs_mark_buffer_dirty(leaf);
2810
2811out:
2812 btrfs_free_path(path);
2813 return ret;
2814}
2815
Miao Xie2196d6e2014-09-03 21:35:41 +08002816int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002817 struct btrfs_device *device, u64 new_size)
2818{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002819 struct btrfs_fs_info *fs_info = device->fs_info;
2820 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002821 u64 old_total;
2822 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002823
Anand Jainebbede42017-12-04 12:54:52 +08002824 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002825 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002826
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002827 new_size = round_down(new_size, fs_info->sectorsize);
2828
David Sterba34441362016-10-04 19:34:27 +02002829 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002830 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002831 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002832
Stefan Behrens63a212a2012-11-05 18:29:28 +01002833 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002834 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002835 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002836 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002837 }
Yan Zheng2b820322008-11-17 21:11:30 -05002838
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002839 btrfs_set_super_total_bytes(super_copy,
2840 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002841 device->fs_devices->total_rw_bytes += diff;
2842
Miao Xie7cc8e582014-09-03 21:35:38 +08002843 btrfs_device_set_total_bytes(device, new_size);
2844 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002845 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002846 if (list_empty(&device->post_commit_list))
2847 list_add_tail(&device->post_commit_list,
2848 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002849 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002850
Chris Mason8f18cf12008-04-25 16:53:30 -04002851 return btrfs_update_device(trans, device);
2852}
2853
Nikolay Borisovf4208792018-07-20 19:37:52 +03002854static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002855{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002856 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002857 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002858 int ret;
2859 struct btrfs_path *path;
2860 struct btrfs_key key;
2861
Chris Mason8f18cf12008-04-25 16:53:30 -04002862 path = btrfs_alloc_path();
2863 if (!path)
2864 return -ENOMEM;
2865
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002866 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002867 key.offset = chunk_offset;
2868 key.type = BTRFS_CHUNK_ITEM_KEY;
2869
2870 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002871 if (ret < 0)
2872 goto out;
2873 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 btrfs_handle_fs_error(fs_info, -ENOENT,
2875 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002876 ret = -ENOENT;
2877 goto out;
2878 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002879
2880 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002881 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002882 btrfs_handle_fs_error(fs_info, ret,
2883 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002884out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002885 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002886 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002887}
2888
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002889static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002890{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002891 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002892 struct btrfs_disk_key *disk_key;
2893 struct btrfs_chunk *chunk;
2894 u8 *ptr;
2895 int ret = 0;
2896 u32 num_stripes;
2897 u32 array_size;
2898 u32 len = 0;
2899 u32 cur;
2900 struct btrfs_key key;
2901
David Sterba34441362016-10-04 19:34:27 +02002902 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002903 array_size = btrfs_super_sys_array_size(super_copy);
2904
2905 ptr = super_copy->sys_chunk_array;
2906 cur = 0;
2907
2908 while (cur < array_size) {
2909 disk_key = (struct btrfs_disk_key *)ptr;
2910 btrfs_disk_key_to_cpu(&key, disk_key);
2911
2912 len = sizeof(*disk_key);
2913
2914 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2915 chunk = (struct btrfs_chunk *)(ptr + len);
2916 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2917 len += btrfs_chunk_item_size(num_stripes);
2918 } else {
2919 ret = -EIO;
2920 break;
2921 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002922 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002923 key.offset == chunk_offset) {
2924 memmove(ptr, ptr + len, array_size - (cur + len));
2925 array_size -= len;
2926 btrfs_set_super_sys_array_size(super_copy, array_size);
2927 } else {
2928 ptr += len;
2929 cur += len;
2930 }
2931 }
David Sterba34441362016-10-04 19:34:27 +02002932 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002933 return ret;
2934}
2935
Omar Sandoval60ca8422018-05-16 16:34:31 -07002936/*
2937 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2938 * @logical: Logical block offset in bytes.
2939 * @length: Length of extent in bytes.
2940 *
2941 * Return: Chunk mapping or ERR_PTR.
2942 */
2943struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2944 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002945{
2946 struct extent_map_tree *em_tree;
2947 struct extent_map *em;
2948
David Sterbac8bf1b62019-05-17 11:43:17 +02002949 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002950 read_lock(&em_tree->lock);
2951 em = lookup_extent_mapping(em_tree, logical, length);
2952 read_unlock(&em_tree->lock);
2953
2954 if (!em) {
2955 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2956 logical, length);
2957 return ERR_PTR(-EINVAL);
2958 }
2959
2960 if (em->start > logical || em->start + em->len < logical) {
2961 btrfs_crit(fs_info,
2962 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2963 logical, length, em->start, em->start + em->len);
2964 free_extent_map(em);
2965 return ERR_PTR(-EINVAL);
2966 }
2967
2968 /* callers are responsible for dropping em's ref. */
2969 return em;
2970}
2971
Nikolay Borisov97aff912018-07-20 19:37:53 +03002972int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002973{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002974 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002975 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002976 struct map_lookup *map;
2977 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002978 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002979 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002980
Omar Sandoval60ca8422018-05-16 16:34:31 -07002981 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002982 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002983 /*
2984 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002985 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002986 * do anything we still error out.
2987 */
2988 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002989 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002990 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002991 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002992 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002993 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002994 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002995
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002996 /*
2997 * Take the device list mutex to prevent races with the final phase of
2998 * a device replace operation that replaces the device object associated
2999 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3000 */
3001 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003002 for (i = 0; i < map->num_stripes; i++) {
3003 struct btrfs_device *device = map->stripes[i].dev;
3004 ret = btrfs_free_dev_extent(trans, device,
3005 map->stripes[i].physical,
3006 &dev_extent_len);
3007 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003008 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04003009 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003010 goto out;
3011 }
3012
3013 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003014 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003015 btrfs_device_set_bytes_used(device,
3016 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003017 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003018 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003019 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003020 }
3021
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003022 ret = btrfs_update_device(trans, device);
3023 if (ret) {
3024 mutex_unlock(&fs_devices->device_list_mutex);
3025 btrfs_abort_transaction(trans, ret);
3026 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003027 }
3028 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003029 mutex_unlock(&fs_devices->device_list_mutex);
3030
Nikolay Borisovf4208792018-07-20 19:37:52 +03003031 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003032 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003033 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003034 goto out;
3035 }
3036
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003037 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003038
3039 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003040 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003041 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003042 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003043 goto out;
3044 }
3045 }
3046
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003047 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003048 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003049 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003050 goto out;
3051 }
3052
Josef Bacik47ab2a62014-09-18 11:20:02 -04003053out:
3054 /* once for us */
3055 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003056 return ret;
3057}
3058
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003059static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003060{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003061 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003062 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04003063 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003064
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003065 /*
3066 * Prevent races with automatic removal of unused block groups.
3067 * After we relocate and before we remove the chunk with offset
3068 * chunk_offset, automatic removal of the block group can kick in,
3069 * resulting in a failure when calling btrfs_remove_chunk() below.
3070 *
3071 * Make sure to acquire this mutex before doing a tree search (dev
3072 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3073 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3074 * we release the path used to search the chunk/dev tree and before
3075 * the current task acquires this mutex and calls us.
3076 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003077 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003078
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003080 if (ret)
3081 return -ENOSPC;
3082
Chris Mason8f18cf12008-04-25 16:53:30 -04003083 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003084 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003085 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003086 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003087 if (ret)
3088 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003089
Chris Mason19c4d2f2016-10-10 13:43:31 -07003090 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3091 chunk_offset);
3092 if (IS_ERR(trans)) {
3093 ret = PTR_ERR(trans);
3094 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3095 return ret;
3096 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003097
Chris Mason19c4d2f2016-10-10 13:43:31 -07003098 /*
3099 * step two, delete the device extents and the
3100 * chunk tree entries
3101 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003102 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003103 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003104 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003105}
3106
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003107static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003108{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003109 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003110 struct btrfs_path *path;
3111 struct extent_buffer *leaf;
3112 struct btrfs_chunk *chunk;
3113 struct btrfs_key key;
3114 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003115 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003116 bool retried = false;
3117 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003118 int ret;
3119
3120 path = btrfs_alloc_path();
3121 if (!path)
3122 return -ENOMEM;
3123
Josef Bacikba1bf482009-09-11 16:11:19 -04003124again:
Yan Zheng2b820322008-11-17 21:11:30 -05003125 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3126 key.offset = (u64)-1;
3127 key.type = BTRFS_CHUNK_ITEM_KEY;
3128
3129 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003130 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003131 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003132 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003134 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003135 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003136 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003137
3138 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3139 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003140 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003142 if (ret < 0)
3143 goto error;
3144 if (ret > 0)
3145 break;
3146
3147 leaf = path->nodes[0];
3148 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3149
3150 chunk = btrfs_item_ptr(leaf, path->slots[0],
3151 struct btrfs_chunk);
3152 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003153 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003154
3155 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003156 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003157 if (ret == -ENOSPC)
3158 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303159 else
3160 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003161 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003162 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003163
3164 if (found_key.offset == 0)
3165 break;
3166 key.offset = found_key.offset - 1;
3167 }
3168 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003169 if (failed && !retried) {
3170 failed = 0;
3171 retried = true;
3172 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303173 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003174 ret = -ENOSPC;
3175 }
Yan Zheng2b820322008-11-17 21:11:30 -05003176error:
3177 btrfs_free_path(path);
3178 return ret;
3179}
3180
Liu Boa6f93c72017-11-15 16:28:11 -07003181/*
3182 * return 1 : allocate a data chunk successfully,
3183 * return <0: errors during allocating a data chunk,
3184 * return 0 : no need to allocate a data chunk.
3185 */
3186static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3187 u64 chunk_offset)
3188{
3189 struct btrfs_block_group_cache *cache;
3190 u64 bytes_used;
3191 u64 chunk_type;
3192
3193 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3194 ASSERT(cache);
3195 chunk_type = cache->flags;
3196 btrfs_put_block_group(cache);
3197
3198 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3199 spin_lock(&fs_info->data_sinfo->lock);
3200 bytes_used = fs_info->data_sinfo->bytes_used;
3201 spin_unlock(&fs_info->data_sinfo->lock);
3202
3203 if (!bytes_used) {
3204 struct btrfs_trans_handle *trans;
3205 int ret;
3206
3207 trans = btrfs_join_transaction(fs_info->tree_root);
3208 if (IS_ERR(trans))
3209 return PTR_ERR(trans);
3210
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003211 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003212 BTRFS_BLOCK_GROUP_DATA);
3213 btrfs_end_transaction(trans);
3214 if (ret < 0)
3215 return ret;
Liu Boa6f93c72017-11-15 16:28:11 -07003216 return 1;
3217 }
3218 }
3219 return 0;
3220}
3221
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003222static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003223 struct btrfs_balance_control *bctl)
3224{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003225 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003226 struct btrfs_trans_handle *trans;
3227 struct btrfs_balance_item *item;
3228 struct btrfs_disk_balance_args disk_bargs;
3229 struct btrfs_path *path;
3230 struct extent_buffer *leaf;
3231 struct btrfs_key key;
3232 int ret, err;
3233
3234 path = btrfs_alloc_path();
3235 if (!path)
3236 return -ENOMEM;
3237
3238 trans = btrfs_start_transaction(root, 0);
3239 if (IS_ERR(trans)) {
3240 btrfs_free_path(path);
3241 return PTR_ERR(trans);
3242 }
3243
3244 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003245 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003246 key.offset = 0;
3247
3248 ret = btrfs_insert_empty_item(trans, root, path, &key,
3249 sizeof(*item));
3250 if (ret)
3251 goto out;
3252
3253 leaf = path->nodes[0];
3254 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3255
David Sterbab159fa22016-11-08 18:09:03 +01003256 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003257
3258 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3259 btrfs_set_balance_data(leaf, item, &disk_bargs);
3260 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3261 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3262 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3263 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3264
3265 btrfs_set_balance_flags(leaf, item, bctl->flags);
3266
3267 btrfs_mark_buffer_dirty(leaf);
3268out:
3269 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003270 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003271 if (err && !ret)
3272 ret = err;
3273 return ret;
3274}
3275
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003276static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003277{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003278 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003279 struct btrfs_trans_handle *trans;
3280 struct btrfs_path *path;
3281 struct btrfs_key key;
3282 int ret, err;
3283
3284 path = btrfs_alloc_path();
3285 if (!path)
3286 return -ENOMEM;
3287
3288 trans = btrfs_start_transaction(root, 0);
3289 if (IS_ERR(trans)) {
3290 btrfs_free_path(path);
3291 return PTR_ERR(trans);
3292 }
3293
3294 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003295 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003296 key.offset = 0;
3297
3298 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3299 if (ret < 0)
3300 goto out;
3301 if (ret > 0) {
3302 ret = -ENOENT;
3303 goto out;
3304 }
3305
3306 ret = btrfs_del_item(trans, root, path);
3307out:
3308 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003309 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003310 if (err && !ret)
3311 ret = err;
3312 return ret;
3313}
3314
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003315/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003316 * This is a heuristic used to reduce the number of chunks balanced on
3317 * resume after balance was interrupted.
3318 */
3319static void update_balance_args(struct btrfs_balance_control *bctl)
3320{
3321 /*
3322 * Turn on soft mode for chunk types that were being converted.
3323 */
3324 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3325 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3326 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3327 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3328 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3329 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3330
3331 /*
3332 * Turn on usage filter if is not already used. The idea is
3333 * that chunks that we have already balanced should be
3334 * reasonably full. Don't do it for chunks that are being
3335 * converted - that will keep us from relocating unconverted
3336 * (albeit full) chunks.
3337 */
3338 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003339 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003340 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3341 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3342 bctl->data.usage = 90;
3343 }
3344 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003345 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003346 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3347 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3348 bctl->sys.usage = 90;
3349 }
3350 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003351 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003352 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3353 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3354 bctl->meta.usage = 90;
3355 }
3356}
3357
3358/*
David Sterba149196a2018-03-20 20:23:09 +01003359 * Clear the balance status in fs_info and delete the balance item from disk.
3360 */
3361static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003362{
3363 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003364 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003365
3366 BUG_ON(!fs_info->balance_ctl);
3367
3368 spin_lock(&fs_info->balance_lock);
3369 fs_info->balance_ctl = NULL;
3370 spin_unlock(&fs_info->balance_lock);
3371
3372 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003373 ret = del_balance_item(fs_info);
3374 if (ret)
3375 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003376}
3377
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003378/*
3379 * Balance filters. Return 1 if chunk should be filtered out
3380 * (should not be balanced).
3381 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003382static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003383 struct btrfs_balance_args *bargs)
3384{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003385 chunk_type = chunk_to_extended(chunk_type) &
3386 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003387
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003388 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003389 return 0;
3390
3391 return 1;
3392}
3393
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003394static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003395 struct btrfs_balance_args *bargs)
3396{
3397 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003398 u64 chunk_used;
3399 u64 user_thresh_min;
3400 u64 user_thresh_max;
3401 int ret = 1;
3402
3403 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3404 chunk_used = btrfs_block_group_used(&cache->item);
3405
3406 if (bargs->usage_min == 0)
3407 user_thresh_min = 0;
3408 else
3409 user_thresh_min = div_factor_fine(cache->key.offset,
3410 bargs->usage_min);
3411
3412 if (bargs->usage_max == 0)
3413 user_thresh_max = 1;
3414 else if (bargs->usage_max > 100)
3415 user_thresh_max = cache->key.offset;
3416 else
3417 user_thresh_max = div_factor_fine(cache->key.offset,
3418 bargs->usage_max);
3419
3420 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3421 ret = 0;
3422
3423 btrfs_put_block_group(cache);
3424 return ret;
3425}
3426
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003427static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003428 u64 chunk_offset, struct btrfs_balance_args *bargs)
3429{
3430 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003431 u64 chunk_used, user_thresh;
3432 int ret = 1;
3433
3434 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3435 chunk_used = btrfs_block_group_used(&cache->item);
3436
David Sterbabc309462015-10-20 18:22:13 +02003437 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003438 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003439 else if (bargs->usage > 100)
3440 user_thresh = cache->key.offset;
3441 else
3442 user_thresh = div_factor_fine(cache->key.offset,
3443 bargs->usage);
3444
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003445 if (chunk_used < user_thresh)
3446 ret = 0;
3447
3448 btrfs_put_block_group(cache);
3449 return ret;
3450}
3451
Ilya Dryomov409d4042012-01-16 22:04:47 +02003452static int chunk_devid_filter(struct extent_buffer *leaf,
3453 struct btrfs_chunk *chunk,
3454 struct btrfs_balance_args *bargs)
3455{
3456 struct btrfs_stripe *stripe;
3457 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3458 int i;
3459
3460 for (i = 0; i < num_stripes; i++) {
3461 stripe = btrfs_stripe_nr(chunk, i);
3462 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3463 return 0;
3464 }
3465
3466 return 1;
3467}
3468
David Sterba946c9252019-05-17 11:43:34 +02003469static u64 calc_data_stripes(u64 type, int num_stripes)
3470{
3471 const int index = btrfs_bg_flags_to_raid_index(type);
3472 const int ncopies = btrfs_raid_array[index].ncopies;
3473 const int nparity = btrfs_raid_array[index].nparity;
3474
3475 if (nparity)
3476 return num_stripes - nparity;
3477 else
3478 return num_stripes / ncopies;
3479}
3480
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003481/* [pstart, pend) */
3482static int chunk_drange_filter(struct extent_buffer *leaf,
3483 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003484 struct btrfs_balance_args *bargs)
3485{
3486 struct btrfs_stripe *stripe;
3487 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3488 u64 stripe_offset;
3489 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003490 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003491 int factor;
3492 int i;
3493
3494 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3495 return 0;
3496
David Sterba946c9252019-05-17 11:43:34 +02003497 type = btrfs_chunk_type(leaf, chunk);
3498 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003499
3500 for (i = 0; i < num_stripes; i++) {
3501 stripe = btrfs_stripe_nr(chunk, i);
3502 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3503 continue;
3504
3505 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3506 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003507 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003508
3509 if (stripe_offset < bargs->pend &&
3510 stripe_offset + stripe_length > bargs->pstart)
3511 return 0;
3512 }
3513
3514 return 1;
3515}
3516
Ilya Dryomovea671762012-01-16 22:04:48 +02003517/* [vstart, vend) */
3518static int chunk_vrange_filter(struct extent_buffer *leaf,
3519 struct btrfs_chunk *chunk,
3520 u64 chunk_offset,
3521 struct btrfs_balance_args *bargs)
3522{
3523 if (chunk_offset < bargs->vend &&
3524 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3525 /* at least part of the chunk is inside this vrange */
3526 return 0;
3527
3528 return 1;
3529}
3530
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003531static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3532 struct btrfs_chunk *chunk,
3533 struct btrfs_balance_args *bargs)
3534{
3535 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3536
3537 if (bargs->stripes_min <= num_stripes
3538 && num_stripes <= bargs->stripes_max)
3539 return 0;
3540
3541 return 1;
3542}
3543
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003544static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003545 struct btrfs_balance_args *bargs)
3546{
3547 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3548 return 0;
3549
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003550 chunk_type = chunk_to_extended(chunk_type) &
3551 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003552
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003553 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003554 return 1;
3555
3556 return 0;
3557}
3558
David Sterba6ec08962019-03-20 16:38:52 +01003559static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003560 struct btrfs_chunk *chunk, u64 chunk_offset)
3561{
David Sterba6ec08962019-03-20 16:38:52 +01003562 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003563 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003564 struct btrfs_balance_args *bargs = NULL;
3565 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3566
3567 /* type filter */
3568 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3569 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3570 return 0;
3571 }
3572
3573 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3574 bargs = &bctl->data;
3575 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3576 bargs = &bctl->sys;
3577 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3578 bargs = &bctl->meta;
3579
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003580 /* profiles filter */
3581 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3582 chunk_profiles_filter(chunk_type, bargs)) {
3583 return 0;
3584 }
3585
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003586 /* usage filter */
3587 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003588 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003589 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003590 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003591 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003592 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003593 }
3594
Ilya Dryomov409d4042012-01-16 22:04:47 +02003595 /* devid filter */
3596 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3597 chunk_devid_filter(leaf, chunk, bargs)) {
3598 return 0;
3599 }
3600
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003601 /* drange filter, makes sense only with devid filter */
3602 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003603 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003604 return 0;
3605 }
3606
Ilya Dryomovea671762012-01-16 22:04:48 +02003607 /* vrange filter */
3608 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3609 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3610 return 0;
3611 }
3612
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003613 /* stripes filter */
3614 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3615 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3616 return 0;
3617 }
3618
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003619 /* soft profile changing mode */
3620 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3621 chunk_soft_convert_filter(chunk_type, bargs)) {
3622 return 0;
3623 }
3624
David Sterba7d824b62014-05-07 17:37:51 +02003625 /*
3626 * limited by count, must be the last filter
3627 */
3628 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3629 if (bargs->limit == 0)
3630 return 0;
3631 else
3632 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003633 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3634 /*
3635 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003636 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003637 * about the count of all chunks that satisfy the filters.
3638 */
3639 if (bargs->limit_max == 0)
3640 return 0;
3641 else
3642 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003643 }
3644
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003645 return 1;
3646}
3647
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003648static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003649{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003650 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003651 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003652 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003653 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003654 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003655 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003656 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003657 struct extent_buffer *leaf;
3658 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003659 int ret;
3660 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003661 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003662 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003663 u64 limit_data = bctl->data.limit;
3664 u64 limit_meta = bctl->meta.limit;
3665 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003666 u32 count_data = 0;
3667 u32 count_meta = 0;
3668 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003669 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003670
Chris Masonec44a352008-04-28 15:29:52 -04003671 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003672 if (!path) {
3673 ret = -ENOMEM;
3674 goto error;
3675 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003676
3677 /* zero out stat counters */
3678 spin_lock(&fs_info->balance_lock);
3679 memset(&bctl->stat, 0, sizeof(bctl->stat));
3680 spin_unlock(&fs_info->balance_lock);
3681again:
David Sterba7d824b62014-05-07 17:37:51 +02003682 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003683 /*
3684 * The single value limit and min/max limits use the same bytes
3685 * in the
3686 */
David Sterba7d824b62014-05-07 17:37:51 +02003687 bctl->data.limit = limit_data;
3688 bctl->meta.limit = limit_meta;
3689 bctl->sys.limit = limit_sys;
3690 }
Chris Masonec44a352008-04-28 15:29:52 -04003691 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3692 key.offset = (u64)-1;
3693 key.type = BTRFS_CHUNK_ITEM_KEY;
3694
Chris Masond3977122009-01-05 21:25:51 -05003695 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003696 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003697 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003698 ret = -ECANCELED;
3699 goto error;
3700 }
3701
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003702 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003703 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003704 if (ret < 0) {
3705 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003706 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003707 }
Chris Masonec44a352008-04-28 15:29:52 -04003708
3709 /*
3710 * this shouldn't happen, it means the last relocate
3711 * failed
3712 */
3713 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003714 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003715
3716 ret = btrfs_previous_item(chunk_root, path, 0,
3717 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003718 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003719 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003720 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003721 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003722 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003723
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003724 leaf = path->nodes[0];
3725 slot = path->slots[0];
3726 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3727
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003728 if (found_key.objectid != key.objectid) {
3729 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003730 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003731 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003732
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003733 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003734 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003735
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003736 if (!counting) {
3737 spin_lock(&fs_info->balance_lock);
3738 bctl->stat.considered++;
3739 spin_unlock(&fs_info->balance_lock);
3740 }
3741
David Sterba6ec08962019-03-20 16:38:52 +01003742 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003743
David Sterbab3b4aa72011-04-21 01:20:15 +02003744 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003745 if (!ret) {
3746 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003747 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003748 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003749
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003750 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003751 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003752 spin_lock(&fs_info->balance_lock);
3753 bctl->stat.expected++;
3754 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003755
3756 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3757 count_data++;
3758 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3759 count_sys++;
3760 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3761 count_meta++;
3762
3763 goto loop;
3764 }
3765
3766 /*
3767 * Apply limit_min filter, no need to check if the LIMITS
3768 * filter is used, limit_min is 0 by default
3769 */
3770 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3771 count_data < bctl->data.limit_min)
3772 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3773 count_meta < bctl->meta.limit_min)
3774 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3775 count_sys < bctl->sys.limit_min)) {
3776 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003777 goto loop;
3778 }
3779
Liu Boa6f93c72017-11-15 16:28:11 -07003780 if (!chunk_reserved) {
3781 /*
3782 * We may be relocating the only data chunk we have,
3783 * which could potentially end up with losing data's
3784 * raid profile, so lets allocate an empty one in
3785 * advance.
3786 */
3787 ret = btrfs_may_alloc_data_chunk(fs_info,
3788 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003789 if (ret < 0) {
3790 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3791 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003792 } else if (ret == 1) {
3793 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003794 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003795 }
3796
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003797 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003798 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003799 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003800 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003801 } else if (ret == -ETXTBSY) {
3802 btrfs_info(fs_info,
3803 "skipping relocation of block group %llu due to active swapfile",
3804 found_key.offset);
3805 ret = 0;
3806 } else if (ret) {
3807 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003808 } else {
3809 spin_lock(&fs_info->balance_lock);
3810 bctl->stat.completed++;
3811 spin_unlock(&fs_info->balance_lock);
3812 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003813loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003814 if (found_key.offset == 0)
3815 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003816 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003817 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003818
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003819 if (counting) {
3820 btrfs_release_path(path);
3821 counting = false;
3822 goto again;
3823 }
Chris Masonec44a352008-04-28 15:29:52 -04003824error:
3825 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003826 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003827 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003828 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003829 if (!ret)
3830 ret = -ENOSPC;
3831 }
3832
Chris Masonec44a352008-04-28 15:29:52 -04003833 return ret;
3834}
3835
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003836/**
3837 * alloc_profile_is_valid - see if a given profile is valid and reduced
3838 * @flags: profile to validate
3839 * @extended: if true @flags is treated as an extended profile
3840 */
3841static int alloc_profile_is_valid(u64 flags, int extended)
3842{
3843 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3844 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3845
3846 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3847
3848 /* 1) check that all other bits are zeroed */
3849 if (flags & ~mask)
3850 return 0;
3851
3852 /* 2) see if profile is reduced */
3853 if (flags == 0)
3854 return !extended; /* "0" is valid for usual profiles */
3855
3856 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003857 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003858}
3859
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003860static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3861{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003862 /* cancel requested || normal exit path */
3863 return atomic_read(&fs_info->balance_cancel_req) ||
3864 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3865 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003866}
3867
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003868/* Non-zero return value signifies invalidity */
3869static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3870 u64 allowed)
3871{
3872 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3873 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3874 (bctl_arg->target & ~allowed)));
3875}
3876
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003877/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003878 * Fill @buf with textual description of balance filter flags @bargs, up to
3879 * @size_buf including the terminating null. The output may be trimmed if it
3880 * does not fit into the provided buffer.
3881 */
3882static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3883 u32 size_buf)
3884{
3885 int ret;
3886 u32 size_bp = size_buf;
3887 char *bp = buf;
3888 u64 flags = bargs->flags;
3889 char tmp_buf[128] = {'\0'};
3890
3891 if (!flags)
3892 return;
3893
3894#define CHECK_APPEND_NOARG(a) \
3895 do { \
3896 ret = snprintf(bp, size_bp, (a)); \
3897 if (ret < 0 || ret >= size_bp) \
3898 goto out_overflow; \
3899 size_bp -= ret; \
3900 bp += ret; \
3901 } while (0)
3902
3903#define CHECK_APPEND_1ARG(a, v1) \
3904 do { \
3905 ret = snprintf(bp, size_bp, (a), (v1)); \
3906 if (ret < 0 || ret >= size_bp) \
3907 goto out_overflow; \
3908 size_bp -= ret; \
3909 bp += ret; \
3910 } while (0)
3911
3912#define CHECK_APPEND_2ARG(a, v1, v2) \
3913 do { \
3914 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3915 if (ret < 0 || ret >= size_bp) \
3916 goto out_overflow; \
3917 size_bp -= ret; \
3918 bp += ret; \
3919 } while (0)
3920
David Sterba158da512019-05-17 11:43:41 +02003921 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3922 CHECK_APPEND_1ARG("convert=%s,",
3923 btrfs_bg_type_to_raid_name(bargs->target));
Anand Jain56fc37d2018-11-20 16:12:56 +08003924
3925 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3926 CHECK_APPEND_NOARG("soft,");
3927
3928 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3929 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3930 sizeof(tmp_buf));
3931 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3932 }
3933
3934 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3935 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3936
3937 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3938 CHECK_APPEND_2ARG("usage=%u..%u,",
3939 bargs->usage_min, bargs->usage_max);
3940
3941 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3942 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3943
3944 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3945 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3946 bargs->pstart, bargs->pend);
3947
3948 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3949 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3950 bargs->vstart, bargs->vend);
3951
3952 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3953 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3954
3955 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3956 CHECK_APPEND_2ARG("limit=%u..%u,",
3957 bargs->limit_min, bargs->limit_max);
3958
3959 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3960 CHECK_APPEND_2ARG("stripes=%u..%u,",
3961 bargs->stripes_min, bargs->stripes_max);
3962
3963#undef CHECK_APPEND_2ARG
3964#undef CHECK_APPEND_1ARG
3965#undef CHECK_APPEND_NOARG
3966
3967out_overflow:
3968
3969 if (size_bp < size_buf)
3970 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3971 else
3972 buf[0] = '\0';
3973}
3974
3975static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3976{
3977 u32 size_buf = 1024;
3978 char tmp_buf[192] = {'\0'};
3979 char *buf;
3980 char *bp;
3981 u32 size_bp = size_buf;
3982 int ret;
3983 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3984
3985 buf = kzalloc(size_buf, GFP_KERNEL);
3986 if (!buf)
3987 return;
3988
3989 bp = buf;
3990
3991#define CHECK_APPEND_1ARG(a, v1) \
3992 do { \
3993 ret = snprintf(bp, size_bp, (a), (v1)); \
3994 if (ret < 0 || ret >= size_bp) \
3995 goto out_overflow; \
3996 size_bp -= ret; \
3997 bp += ret; \
3998 } while (0)
3999
4000 if (bctl->flags & BTRFS_BALANCE_FORCE)
4001 CHECK_APPEND_1ARG("%s", "-f ");
4002
4003 if (bctl->flags & BTRFS_BALANCE_DATA) {
4004 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4005 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4006 }
4007
4008 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4009 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4010 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4011 }
4012
4013 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4014 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4015 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4016 }
4017
4018#undef CHECK_APPEND_1ARG
4019
4020out_overflow:
4021
4022 if (size_bp < size_buf)
4023 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4024 btrfs_info(fs_info, "balance: %s %s",
4025 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4026 "resume" : "start", buf);
4027
4028 kfree(buf);
4029}
4030
4031/*
David Sterbadccdb072018-03-21 00:20:05 +01004032 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004033 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004034int btrfs_balance(struct btrfs_fs_info *fs_info,
4035 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004036 struct btrfs_ioctl_balance_args *bargs)
4037{
Adam Borowski14506122017-03-07 23:34:44 +01004038 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004039 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004040 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004041 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004042 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004043 unsigned seq;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004044 bool reducing_integrity;
David Sterba081db892019-05-17 11:43:27 +02004045 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004046
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004047 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004048 atomic_read(&fs_info->balance_pause_req) ||
4049 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004050 ret = -EINVAL;
4051 goto out;
4052 }
4053
Ilya Dryomove4837f82012-03-27 17:09:17 +03004054 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4055 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4056 mixed = 1;
4057
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004058 /*
4059 * In case of mixed groups both data and meta should be picked,
4060 * and identical options should be given for both of them.
4061 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004062 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4063 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004064 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4065 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4066 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004067 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004068 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004069 ret = -EINVAL;
4070 goto out;
4071 }
4072 }
4073
Anand Jain1da73962018-08-10 13:53:21 +08004074 num_devices = btrfs_num_devices(fs_info);
David Sterba081db892019-05-17 11:43:27 +02004075 allowed = 0;
4076 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4077 if (num_devices >= btrfs_raid_array[i].devs_min)
4078 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08004079
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004080 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004081 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004082 "balance: invalid convert data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004083 btrfs_bg_type_to_raid_name(bctl->data.target));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004084 ret = -EINVAL;
4085 goto out;
4086 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004087 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004088 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004089 "balance: invalid convert metadata profile %s",
David Sterba158da512019-05-17 11:43:41 +02004090 btrfs_bg_type_to_raid_name(bctl->meta.target));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004091 ret = -EINVAL;
4092 goto out;
4093 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004094 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004095 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004096 "balance: invalid convert system profile %s",
David Sterba158da512019-05-17 11:43:41 +02004097 btrfs_bg_type_to_raid_name(bctl->sys.target));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004098 ret = -EINVAL;
4099 goto out;
4100 }
4101
David Sterba6079e122019-05-17 11:43:29 +02004102 /*
4103 * Allow to reduce metadata or system integrity only if force set for
4104 * profiles with redundancy (copies, parity)
4105 */
4106 allowed = 0;
4107 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4108 if (btrfs_raid_array[i].ncopies >= 2 ||
4109 btrfs_raid_array[i].tolerated_failures >= 1)
4110 allowed |= btrfs_raid_array[i].bg_flag;
4111 }
Miao Xiede98ced2013-01-29 10:13:12 +00004112 do {
4113 seq = read_seqbegin(&fs_info->profiles_lock);
4114
4115 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4116 (fs_info->avail_system_alloc_bits & allowed) &&
4117 !(bctl->sys.target & allowed)) ||
4118 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4119 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004120 !(bctl->meta.target & allowed)))
4121 reducing_integrity = true;
4122 else
4123 reducing_integrity = false;
4124
4125 /* if we're not converting, the target field is uninitialized */
4126 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4127 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4128 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4129 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004130 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004131
Filipe Manana5a8067c2018-11-19 09:48:12 +00004132 if (reducing_integrity) {
4133 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4134 btrfs_info(fs_info,
4135 "balance: force reducing metadata integrity");
4136 } else {
4137 btrfs_err(fs_info,
4138 "balance: reduces metadata integrity, use --force if you want this");
4139 ret = -EINVAL;
4140 goto out;
4141 }
4142 }
4143
Adam Borowski14506122017-03-07 23:34:44 +01004144 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4145 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00004146 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004147 "balance: metadata profile %s has lower redundancy than data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004148 btrfs_bg_type_to_raid_name(meta_target),
4149 btrfs_bg_type_to_raid_name(data_target));
Sam Tygieree592d02016-01-06 08:46:12 +00004150 }
4151
Filipe Manana9e967492019-04-22 16:44:09 +01004152 if (fs_info->send_in_progress) {
4153 btrfs_warn_rl(fs_info,
4154"cannot run balance while send operations are in progress (%d in progress)",
4155 fs_info->send_in_progress);
4156 ret = -EAGAIN;
4157 goto out;
4158 }
4159
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004160 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004161 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004162 goto out;
4163
Ilya Dryomov59641012012-01-16 22:04:48 +02004164 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4165 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004166 BUG_ON(fs_info->balance_ctl);
4167 spin_lock(&fs_info->balance_lock);
4168 fs_info->balance_ctl = bctl;
4169 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004170 } else {
4171 BUG_ON(ret != -EEXIST);
4172 spin_lock(&fs_info->balance_lock);
4173 update_balance_args(bctl);
4174 spin_unlock(&fs_info->balance_lock);
4175 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004176
David Sterba3009a622018-03-21 01:31:04 +01004177 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4178 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004179 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004180 mutex_unlock(&fs_info->balance_mutex);
4181
4182 ret = __btrfs_balance(fs_info);
4183
4184 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004185 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4186 btrfs_info(fs_info, "balance: paused");
4187 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4188 btrfs_info(fs_info, "balance: canceled");
4189 else
4190 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4191
David Sterba3009a622018-03-21 01:31:04 +01004192 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004193
4194 if (bargs) {
4195 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004196 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004197 }
4198
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004199 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4200 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004201 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004202 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004203 }
4204
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004205 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004206
4207 return ret;
4208out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004209 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004210 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004211 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004212 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004213 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4214
Ilya Dryomov59641012012-01-16 22:04:48 +02004215 return ret;
4216}
4217
4218static int balance_kthread(void *data)
4219{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004220 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004221 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004222
Ilya Dryomov59641012012-01-16 22:04:48 +02004223 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004224 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004225 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004226 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004227
Ilya Dryomov59641012012-01-16 22:04:48 +02004228 return ret;
4229}
4230
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004231int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4232{
4233 struct task_struct *tsk;
4234
David Sterba1354e1a2018-03-21 02:29:13 +01004235 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004236 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004237 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004238 return 0;
4239 }
David Sterba1354e1a2018-03-21 02:29:13 +01004240 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004241
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004242 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004243 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004244 return 0;
4245 }
4246
Anand Jain02ee6542018-05-17 15:16:51 +08004247 /*
4248 * A ro->rw remount sequence should continue with the paused balance
4249 * regardless of who pauses it, system or the user as of now, so set
4250 * the resume flag.
4251 */
4252 spin_lock(&fs_info->balance_lock);
4253 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4254 spin_unlock(&fs_info->balance_lock);
4255
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004256 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304257 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004258}
4259
Ilya Dryomov68310a52012-06-22 12:24:12 -06004260int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004261{
Ilya Dryomov59641012012-01-16 22:04:48 +02004262 struct btrfs_balance_control *bctl;
4263 struct btrfs_balance_item *item;
4264 struct btrfs_disk_balance_args disk_bargs;
4265 struct btrfs_path *path;
4266 struct extent_buffer *leaf;
4267 struct btrfs_key key;
4268 int ret;
4269
4270 path = btrfs_alloc_path();
4271 if (!path)
4272 return -ENOMEM;
4273
Ilya Dryomov68310a52012-06-22 12:24:12 -06004274 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004275 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004276 key.offset = 0;
4277
4278 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4279 if (ret < 0)
4280 goto out;
4281 if (ret > 0) { /* ret = -ENOENT; */
4282 ret = 0;
4283 goto out;
4284 }
4285
Ilya Dryomov59641012012-01-16 22:04:48 +02004286 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4287 if (!bctl) {
4288 ret = -ENOMEM;
4289 goto out;
4290 }
4291
Ilya Dryomov59641012012-01-16 22:04:48 +02004292 leaf = path->nodes[0];
4293 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4294
Ilya Dryomov68310a52012-06-22 12:24:12 -06004295 bctl->flags = btrfs_balance_flags(leaf, item);
4296 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004297
4298 btrfs_balance_data(leaf, item, &disk_bargs);
4299 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4300 btrfs_balance_meta(leaf, item, &disk_bargs);
4301 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4302 btrfs_balance_sys(leaf, item, &disk_bargs);
4303 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4304
David Sterbaeee95e32018-03-20 20:07:58 +01004305 /*
4306 * This should never happen, as the paused balance state is recovered
4307 * during mount without any chance of other exclusive ops to collide.
4308 *
4309 * This gives the exclusive op status to balance and keeps in paused
4310 * state until user intervention (cancel or umount). If the ownership
4311 * cannot be assigned, show a message but do not fail. The balance
4312 * is in a paused state and must have fs_info::balance_ctl properly
4313 * set up.
4314 */
4315 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4316 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004317 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004318
Ilya Dryomov68310a52012-06-22 12:24:12 -06004319 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004320 BUG_ON(fs_info->balance_ctl);
4321 spin_lock(&fs_info->balance_lock);
4322 fs_info->balance_ctl = bctl;
4323 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004324 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004325out:
4326 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004327 return ret;
4328}
4329
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004330int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4331{
4332 int ret = 0;
4333
4334 mutex_lock(&fs_info->balance_mutex);
4335 if (!fs_info->balance_ctl) {
4336 mutex_unlock(&fs_info->balance_mutex);
4337 return -ENOTCONN;
4338 }
4339
David Sterba3009a622018-03-21 01:31:04 +01004340 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004341 atomic_inc(&fs_info->balance_pause_req);
4342 mutex_unlock(&fs_info->balance_mutex);
4343
4344 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004345 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004346
4347 mutex_lock(&fs_info->balance_mutex);
4348 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004349 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004350 atomic_dec(&fs_info->balance_pause_req);
4351 } else {
4352 ret = -ENOTCONN;
4353 }
4354
4355 mutex_unlock(&fs_info->balance_mutex);
4356 return ret;
4357}
4358
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004359int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4360{
4361 mutex_lock(&fs_info->balance_mutex);
4362 if (!fs_info->balance_ctl) {
4363 mutex_unlock(&fs_info->balance_mutex);
4364 return -ENOTCONN;
4365 }
4366
David Sterbacf7d20f2018-03-21 01:45:32 +01004367 /*
4368 * A paused balance with the item stored on disk can be resumed at
4369 * mount time if the mount is read-write. Otherwise it's still paused
4370 * and we must not allow cancelling as it deletes the item.
4371 */
4372 if (sb_rdonly(fs_info->sb)) {
4373 mutex_unlock(&fs_info->balance_mutex);
4374 return -EROFS;
4375 }
4376
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004377 atomic_inc(&fs_info->balance_cancel_req);
4378 /*
4379 * if we are running just wait and return, balance item is
4380 * deleted in btrfs_balance in this case
4381 */
David Sterba3009a622018-03-21 01:31:04 +01004382 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004383 mutex_unlock(&fs_info->balance_mutex);
4384 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004385 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004386 mutex_lock(&fs_info->balance_mutex);
4387 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004388 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004389 /*
4390 * Lock released to allow other waiters to continue, we'll
4391 * reexamine the status again.
4392 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004393 mutex_lock(&fs_info->balance_mutex);
4394
David Sterbaa17c95d2018-03-20 17:28:05 +01004395 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004396 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004397 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004398 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004399 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004400 }
4401
David Sterba3009a622018-03-21 01:31:04 +01004402 BUG_ON(fs_info->balance_ctl ||
4403 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004404 atomic_dec(&fs_info->balance_cancel_req);
4405 mutex_unlock(&fs_info->balance_mutex);
4406 return 0;
4407}
4408
Stefan Behrens803b2f52013-08-15 17:11:21 +02004409static int btrfs_uuid_scan_kthread(void *data)
4410{
4411 struct btrfs_fs_info *fs_info = data;
4412 struct btrfs_root *root = fs_info->tree_root;
4413 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004414 struct btrfs_path *path = NULL;
4415 int ret = 0;
4416 struct extent_buffer *eb;
4417 int slot;
4418 struct btrfs_root_item root_item;
4419 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004420 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004421
4422 path = btrfs_alloc_path();
4423 if (!path) {
4424 ret = -ENOMEM;
4425 goto out;
4426 }
4427
4428 key.objectid = 0;
4429 key.type = BTRFS_ROOT_ITEM_KEY;
4430 key.offset = 0;
4431
Stefan Behrens803b2f52013-08-15 17:11:21 +02004432 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004433 ret = btrfs_search_forward(root, &key, path,
4434 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004435 if (ret) {
4436 if (ret > 0)
4437 ret = 0;
4438 break;
4439 }
4440
4441 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4442 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4443 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4444 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4445 goto skip;
4446
4447 eb = path->nodes[0];
4448 slot = path->slots[0];
4449 item_size = btrfs_item_size_nr(eb, slot);
4450 if (item_size < sizeof(root_item))
4451 goto skip;
4452
Stefan Behrens803b2f52013-08-15 17:11:21 +02004453 read_extent_buffer(eb, &root_item,
4454 btrfs_item_ptr_offset(eb, slot),
4455 (int)sizeof(root_item));
4456 if (btrfs_root_refs(&root_item) == 0)
4457 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004458
4459 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4460 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4461 if (trans)
4462 goto update_tree;
4463
4464 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004465 /*
4466 * 1 - subvol uuid item
4467 * 1 - received_subvol uuid item
4468 */
4469 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4470 if (IS_ERR(trans)) {
4471 ret = PTR_ERR(trans);
4472 break;
4473 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004474 continue;
4475 } else {
4476 goto skip;
4477 }
4478update_tree:
4479 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004480 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004481 BTRFS_UUID_KEY_SUBVOL,
4482 key.objectid);
4483 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004484 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004485 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004486 break;
4487 }
4488 }
4489
4490 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004491 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004492 root_item.received_uuid,
4493 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4494 key.objectid);
4495 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004496 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004497 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004498 break;
4499 }
4500 }
4501
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004502skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004503 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004504 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004505 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004506 if (ret)
4507 break;
4508 }
4509
Stefan Behrens803b2f52013-08-15 17:11:21 +02004510 btrfs_release_path(path);
4511 if (key.offset < (u64)-1) {
4512 key.offset++;
4513 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4514 key.offset = 0;
4515 key.type = BTRFS_ROOT_ITEM_KEY;
4516 } else if (key.objectid < (u64)-1) {
4517 key.offset = 0;
4518 key.type = BTRFS_ROOT_ITEM_KEY;
4519 key.objectid++;
4520 } else {
4521 break;
4522 }
4523 cond_resched();
4524 }
4525
4526out:
4527 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004528 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004529 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004530 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004531 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004532 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004533 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004534 up(&fs_info->uuid_tree_rescan_sem);
4535 return 0;
4536}
4537
Stefan Behrens70f80172013-08-15 17:11:23 +02004538/*
4539 * Callback for btrfs_uuid_tree_iterate().
4540 * returns:
4541 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004542 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004543 * > 0 if the check failed, which means the caller shall remove the entry.
4544 */
4545static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4546 u8 *uuid, u8 type, u64 subid)
4547{
4548 struct btrfs_key key;
4549 int ret = 0;
4550 struct btrfs_root *subvol_root;
4551
4552 if (type != BTRFS_UUID_KEY_SUBVOL &&
4553 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4554 goto out;
4555
4556 key.objectid = subid;
4557 key.type = BTRFS_ROOT_ITEM_KEY;
4558 key.offset = (u64)-1;
4559 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4560 if (IS_ERR(subvol_root)) {
4561 ret = PTR_ERR(subvol_root);
4562 if (ret == -ENOENT)
4563 ret = 1;
4564 goto out;
4565 }
4566
4567 switch (type) {
4568 case BTRFS_UUID_KEY_SUBVOL:
4569 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4570 ret = 1;
4571 break;
4572 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4573 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4574 BTRFS_UUID_SIZE))
4575 ret = 1;
4576 break;
4577 }
4578
4579out:
4580 return ret;
4581}
4582
4583static int btrfs_uuid_rescan_kthread(void *data)
4584{
4585 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4586 int ret;
4587
4588 /*
4589 * 1st step is to iterate through the existing UUID tree and
4590 * to delete all entries that contain outdated data.
4591 * 2nd step is to add all missing entries to the UUID tree.
4592 */
4593 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4594 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004595 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004596 up(&fs_info->uuid_tree_rescan_sem);
4597 return ret;
4598 }
4599 return btrfs_uuid_scan_kthread(data);
4600}
4601
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004602int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4603{
4604 struct btrfs_trans_handle *trans;
4605 struct btrfs_root *tree_root = fs_info->tree_root;
4606 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004607 struct task_struct *task;
4608 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004609
4610 /*
4611 * 1 - root node
4612 * 1 - root item
4613 */
4614 trans = btrfs_start_transaction(tree_root, 2);
4615 if (IS_ERR(trans))
4616 return PTR_ERR(trans);
4617
David Sterba9b7a2442019-03-20 13:20:49 +01004618 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004619 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004620 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004621 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004622 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004623 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004624 }
4625
4626 fs_info->uuid_root = uuid_root;
4627
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004628 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004629 if (ret)
4630 return ret;
4631
4632 down(&fs_info->uuid_tree_rescan_sem);
4633 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4634 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004635 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004636 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004637 up(&fs_info->uuid_tree_rescan_sem);
4638 return PTR_ERR(task);
4639 }
4640
4641 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004642}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004643
Stefan Behrens70f80172013-08-15 17:11:23 +02004644int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4645{
4646 struct task_struct *task;
4647
4648 down(&fs_info->uuid_tree_rescan_sem);
4649 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4650 if (IS_ERR(task)) {
4651 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004652 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004653 up(&fs_info->uuid_tree_rescan_sem);
4654 return PTR_ERR(task);
4655 }
4656
4657 return 0;
4658}
4659
Chris Mason8f18cf12008-04-25 16:53:30 -04004660/*
4661 * shrinking a device means finding all of the device extents past
4662 * the new size, and then following the back refs to the chunks.
4663 * The chunk relocation code actually frees the device extent
4664 */
4665int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4666{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 struct btrfs_fs_info *fs_info = device->fs_info;
4668 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004669 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004670 struct btrfs_dev_extent *dev_extent = NULL;
4671 struct btrfs_path *path;
4672 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004673 u64 chunk_offset;
4674 int ret;
4675 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004676 int failed = 0;
4677 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004678 struct extent_buffer *l;
4679 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004680 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004681 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004682 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004683 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004684 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004685
4686 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004687 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004688 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004689
Anand Jain401e29c2017-12-04 12:54:55 +08004690 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004691 return -EINVAL;
4692
Chris Mason8f18cf12008-04-25 16:53:30 -04004693 path = btrfs_alloc_path();
4694 if (!path)
4695 return -ENOMEM;
4696
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004697 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004698
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004699 trans = btrfs_start_transaction(root, 0);
4700 if (IS_ERR(trans)) {
4701 btrfs_free_path(path);
4702 return PTR_ERR(trans);
4703 }
4704
David Sterba34441362016-10-04 19:34:27 +02004705 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004706
Miao Xie7cc8e582014-09-03 21:35:38 +08004707 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004708 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004709 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004710 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004711 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004712
4713 /*
4714 * Once the device's size has been set to the new size, ensure all
4715 * in-memory chunks are synced to disk so that the loop below sees them
4716 * and relocates them accordingly.
4717 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004718 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004719 mutex_unlock(&fs_info->chunk_mutex);
4720 ret = btrfs_commit_transaction(trans);
4721 if (ret)
4722 goto done;
4723 } else {
4724 mutex_unlock(&fs_info->chunk_mutex);
4725 btrfs_end_transaction(trans);
4726 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004727
Josef Bacikba1bf482009-09-11 16:11:19 -04004728again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004729 key.objectid = device->devid;
4730 key.offset = (u64)-1;
4731 key.type = BTRFS_DEV_EXTENT_KEY;
4732
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004733 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004734 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004735 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004736 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004737 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004738 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004739 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004740
4741 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004742 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004743 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004744 if (ret < 0)
4745 goto done;
4746 if (ret) {
4747 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004748 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004749 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004750 }
4751
4752 l = path->nodes[0];
4753 slot = path->slots[0];
4754 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4755
Josef Bacikba1bf482009-09-11 16:11:19 -04004756 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004757 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004758 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004759 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004760 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004761
4762 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4763 length = btrfs_dev_extent_length(l, dev_extent);
4764
Josef Bacikba1bf482009-09-11 16:11:19 -04004765 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004767 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004768 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004769 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004770
Chris Mason8f18cf12008-04-25 16:53:30 -04004771 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004772 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004773
Liu Boa6f93c72017-11-15 16:28:11 -07004774 /*
4775 * We may be relocating the only data chunk we have,
4776 * which could potentially end up with losing data's
4777 * raid profile, so lets allocate an empty one in
4778 * advance.
4779 */
4780 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4781 if (ret < 0) {
4782 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4783 goto done;
4784 }
4785
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004786 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4787 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004788 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004789 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004790 } else if (ret) {
4791 if (ret == -ETXTBSY) {
4792 btrfs_warn(fs_info,
4793 "could not shrink block group %llu due to active swapfile",
4794 chunk_offset);
4795 }
4796 goto done;
4797 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004798 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004799
4800 if (failed && !retried) {
4801 failed = 0;
4802 retried = true;
4803 goto again;
4804 } else if (failed && retried) {
4805 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004806 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004807 }
4808
Chris Balld6397ba2009-04-27 07:29:03 -04004809 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004810 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004811 if (IS_ERR(trans)) {
4812 ret = PTR_ERR(trans);
4813 goto done;
4814 }
4815
David Sterba34441362016-10-04 19:34:27 +02004816 mutex_lock(&fs_info->chunk_mutex);
Miao Xie7cc8e582014-09-03 21:35:38 +08004817 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004818 if (list_empty(&device->post_commit_list))
4819 list_add_tail(&device->post_commit_list,
4820 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004821
Chris Balld6397ba2009-04-27 07:29:03 -04004822 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004823 btrfs_set_super_total_bytes(super_copy,
4824 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004825 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004826
4827 /* Now btrfs_update_device() will change the on-disk size. */
4828 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004829 if (ret < 0) {
4830 btrfs_abort_transaction(trans, ret);
4831 btrfs_end_transaction(trans);
4832 } else {
4833 ret = btrfs_commit_transaction(trans);
4834 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004835done:
4836 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004837 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004838 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004839 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004840 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004841 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004842 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004843 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004844 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004845 return ret;
4846}
4847
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004848static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004849 struct btrfs_key *key,
4850 struct btrfs_chunk *chunk, int item_size)
4851{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004852 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004853 struct btrfs_disk_key disk_key;
4854 u32 array_size;
4855 u8 *ptr;
4856
David Sterba34441362016-10-04 19:34:27 +02004857 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004858 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004859 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004860 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004861 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004862 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004863 }
Chris Mason0b86a832008-03-24 15:01:56 -04004864
4865 ptr = super_copy->sys_chunk_array + array_size;
4866 btrfs_cpu_key_to_disk(&disk_key, key);
4867 memcpy(ptr, &disk_key, sizeof(disk_key));
4868 ptr += sizeof(disk_key);
4869 memcpy(ptr, chunk, item_size);
4870 item_size += sizeof(disk_key);
4871 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004872 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004873
Chris Mason0b86a832008-03-24 15:01:56 -04004874 return 0;
4875}
4876
Miao Xieb2117a32011-01-05 10:07:28 +00004877/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004878 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004879 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004880static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004881{
Arne Jansen73c5de02011-04-12 12:07:57 +02004882 const struct btrfs_device_info *di_a = a;
4883 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004884
Arne Jansen73c5de02011-04-12 12:07:57 +02004885 if (di_a->max_avail > di_b->max_avail)
4886 return -1;
4887 if (di_a->max_avail < di_b->max_avail)
4888 return 1;
4889 if (di_a->total_avail > di_b->total_avail)
4890 return -1;
4891 if (di_a->total_avail < di_b->total_avail)
4892 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004893 return 0;
4894}
4895
David Woodhouse53b381b2013-01-29 18:40:14 -05004896static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4897{
Zhao Leiffe2d202015-01-20 15:11:44 +08004898 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004899 return;
4900
Miao Xieceda0862013-04-11 10:30:16 +00004901 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004902}
4903
Miao Xieb2117a32011-01-05 10:07:28 +00004904static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004905 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004906{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004907 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004908 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004909 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004910 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004911 struct extent_map_tree *em_tree;
4912 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004913 struct btrfs_device_info *devices_info = NULL;
4914 u64 total_avail;
4915 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004916 int data_stripes; /* number of stripes that count for
4917 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004918 int sub_stripes; /* sub_stripes info for map */
4919 int dev_stripes; /* stripes per dev */
4920 int devs_max; /* max devs to use */
4921 int devs_min; /* min devs needed */
4922 int devs_increment; /* ndevs has to be a multiple of this */
4923 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004924 int nparity; /* number of stripes worth of bytes to
4925 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00004926 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004927 u64 max_stripe_size;
4928 u64 max_chunk_size;
4929 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004930 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004931 int ndevs;
4932 int i;
4933 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004934 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004935
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004936 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004937
Qu Wenruo4117f202018-01-22 13:50:54 +08004938 if (list_empty(&fs_devices->alloc_list)) {
4939 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4940 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004941 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004942 }
Miao Xieb2117a32011-01-05 10:07:28 +00004943
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004944 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004945
Liu Bo31e50222012-11-21 14:18:10 +00004946 sub_stripes = btrfs_raid_array[index].sub_stripes;
4947 dev_stripes = btrfs_raid_array[index].dev_stripes;
4948 devs_max = btrfs_raid_array[index].devs_max;
David Sterbae3ecdb32019-05-17 11:43:38 +02004949 if (!devs_max)
4950 devs_max = BTRFS_MAX_DEVS(info);
Liu Bo31e50222012-11-21 14:18:10 +00004951 devs_min = btrfs_raid_array[index].devs_min;
4952 devs_increment = btrfs_raid_array[index].devs_increment;
4953 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004954 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02004955
4956 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004957 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08004958 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Arne Jansen73c5de02011-04-12 12:07:57 +02004959 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004960 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004961 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4962 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004963 else
Byongho Leeee221842015-12-15 01:42:10 +09004964 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004965 max_chunk_size = max_stripe_size;
4966 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004967 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004968 max_chunk_size = 2 * max_stripe_size;
4969 } else {
David Sterba351fd352014-05-15 16:48:20 +02004970 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004971 type);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004972 BUG();
Arne Jansen73c5de02011-04-12 12:07:57 +02004973 }
4974
Andrea Gelmini52042d82018-11-28 12:05:13 +01004975 /* We don't want a chunk larger than 10% of writable space */
Arne Jansen73c5de02011-04-12 12:07:57 +02004976 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4977 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004978
David Sterba31e818f2015-02-20 18:00:26 +01004979 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004980 GFP_NOFS);
4981 if (!devices_info)
4982 return -ENOMEM;
4983
Arne Jansen73c5de02011-04-12 12:07:57 +02004984 /*
4985 * in the first pass through the devices list, we gather information
4986 * about the available holes on each device.
4987 */
4988 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004989 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004990 u64 max_avail;
4991 u64 dev_offset;
4992
Anand Jainebbede42017-12-04 12:54:52 +08004993 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004994 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004995 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004996 continue;
4997 }
4998
Anand Jaine12c9622017-12-04 12:54:53 +08004999 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5000 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08005001 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02005002 continue;
5003
5004 if (device->total_bytes > device->bytes_used)
5005 total_avail = device->total_bytes - device->bytes_used;
5006 else
5007 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00005008
5009 /* If there is no space on this device, skip it. */
5010 if (total_avail == 0)
5011 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02005012
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02005013 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02005014 max_stripe_size * dev_stripes,
5015 &dev_offset, &max_avail);
5016 if (ret && ret != -ENOSPC)
5017 goto error;
5018
5019 if (ret == 0)
5020 max_avail = max_stripe_size * dev_stripes;
5021
Qu Wenruo4117f202018-01-22 13:50:54 +08005022 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5023 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5024 btrfs_debug(info,
5025 "%s: devid %llu has no free space, have=%llu want=%u",
5026 __func__, device->devid, max_avail,
5027 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005028 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08005029 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005030
Eric Sandeen063d0062013-01-31 00:55:01 +00005031 if (ndevs == fs_devices->rw_devices) {
5032 WARN(1, "%s: found more than %llu devices\n",
5033 __func__, fs_devices->rw_devices);
5034 break;
5035 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005036 devices_info[ndevs].dev_offset = dev_offset;
5037 devices_info[ndevs].max_avail = max_avail;
5038 devices_info[ndevs].total_avail = total_avail;
5039 devices_info[ndevs].dev = device;
5040 ++ndevs;
5041 }
5042
5043 /*
5044 * now sort the devices by hole size / available space
5045 */
5046 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5047 btrfs_cmp_device_info, NULL);
5048
5049 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03005050 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02005051
Qu Wenruoba89b802018-01-31 13:56:15 +08005052 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005053 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005054 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5055 btrfs_debug(info,
5056 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08005057 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08005058 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005059 goto error;
5060 }
5061
Nikolay Borisovf148ef42017-06-27 10:02:26 +03005062 ndevs = min(ndevs, devs_max);
5063
Arne Jansen73c5de02011-04-12 12:07:57 +02005064 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005065 * The primary goal is to maximize the number of stripes, so use as
5066 * many devices as possible, even if the stripes are not maximum sized.
5067 *
5068 * The DUP profile stores more than one stripe per device, the
5069 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02005070 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005071 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005072 num_stripes = ndevs * dev_stripes;
5073
David Woodhouse53b381b2013-01-29 18:40:14 -05005074 /*
5075 * this will have to be fixed for RAID1 and RAID10 over
5076 * more drives
5077 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005078 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05005079
5080 /*
5081 * Use the number of data stripes to figure out how big this chunk
5082 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005083 * and compare that answer with the max chunk size. If it's higher,
5084 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05005085 */
5086 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005087 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005088 * Reduce stripe_size, round it up to a 16MB boundary again and
5089 * then use it, unless it ends up being even bigger than the
5090 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05005091 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005092 stripe_size = min(round_up(div_u64(max_chunk_size,
5093 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005094 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05005095 }
5096
Ilya Dryomov37db63a2012-04-13 17:05:08 +03005097 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005098 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02005099
Miao Xieb2117a32011-01-05 10:07:28 +00005100 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5101 if (!map) {
5102 ret = -ENOMEM;
5103 goto error;
5104 }
5105 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04005106
Arne Jansen73c5de02011-04-12 12:07:57 +02005107 for (i = 0; i < ndevs; ++i) {
5108 for (j = 0; j < dev_stripes; ++j) {
5109 int s = i * dev_stripes + j;
5110 map->stripes[s].dev = devices_info[i].dev;
5111 map->stripes[s].physical = devices_info[i].dev_offset +
5112 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04005113 }
Chris Mason6324fbf2008-03-24 15:01:59 -04005114 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005115 map->stripe_len = BTRFS_STRIPE_LEN;
5116 map->io_align = BTRFS_STRIPE_LEN;
5117 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04005118 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04005119 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005120
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005121 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005122
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005123 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005124
David Sterba172ddd62011-04-21 00:48:27 +02005125 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005126 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005127 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005128 ret = -ENOMEM;
5129 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005130 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005131 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005132 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005133 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005134 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005135 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005136 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005137 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005138
David Sterbac8bf1b62019-05-17 11:43:17 +02005139 em_tree = &info->mapping_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005140 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005141 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005142 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005143 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005144 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005145 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005146 }
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005147 write_unlock(&em_tree->lock);
5148
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005149 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005150 if (ret)
5151 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005152
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02005153 for (i = 0; i < map->num_stripes; i++) {
5154 struct btrfs_device *dev = map->stripes[i].dev;
5155
5156 btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size);
5157 if (list_empty(&dev->post_commit_list))
5158 list_add_tail(&dev->post_commit_list,
5159 &trans->transaction->dev_update_list);
5160 }
Miao Xie43530c42014-09-03 21:35:36 +08005161
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005162 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005163
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005164 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005165 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005166
Miao Xieb2117a32011-01-05 10:07:28 +00005167 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005168 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005169
Josef Bacik6df9a952013-06-27 13:22:46 -04005170error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005171 write_lock(&em_tree->lock);
5172 remove_extent_mapping(em_tree, em);
5173 write_unlock(&em_tree->lock);
5174
5175 /* One for our allocation */
5176 free_extent_map(em);
5177 /* One for the tree reference */
5178 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005179error:
Miao Xieb2117a32011-01-05 10:07:28 +00005180 kfree(devices_info);
5181 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005182}
5183
Josef Bacik6df9a952013-06-27 13:22:46 -04005184int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005185 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005186{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005187 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005188 struct btrfs_root *extent_root = fs_info->extent_root;
5189 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005190 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005191 struct btrfs_device *device;
5192 struct btrfs_chunk *chunk;
5193 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005194 struct extent_map *em;
5195 struct map_lookup *map;
5196 size_t item_size;
5197 u64 dev_offset;
5198 u64 stripe_size;
5199 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005200 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005201
Omar Sandoval60ca8422018-05-16 16:34:31 -07005202 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005203 if (IS_ERR(em))
5204 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005205
Jeff Mahoney95617d62015-06-03 10:55:48 -04005206 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005207 item_size = btrfs_chunk_item_size(map->num_stripes);
5208 stripe_size = em->orig_block_len;
5209
5210 chunk = kzalloc(item_size, GFP_NOFS);
5211 if (!chunk) {
5212 ret = -ENOMEM;
5213 goto out;
5214 }
5215
Filipe Manana50460e32015-11-20 10:42:47 +00005216 /*
5217 * Take the device list mutex to prevent races with the final phase of
5218 * a device replace operation that replaces the device object associated
5219 * with the map's stripes, because the device object's id can change
5220 * at any time during that final phase of the device replace operation
5221 * (dev-replace.c:btrfs_dev_replace_finishing()).
5222 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005223 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005224 for (i = 0; i < map->num_stripes; i++) {
5225 device = map->stripes[i].dev;
5226 dev_offset = map->stripes[i].physical;
5227
Yan Zheng2b820322008-11-17 21:11:30 -05005228 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005229 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005230 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005231 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5232 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005233 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005234 break;
5235 }
5236 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005237 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005238 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005239 }
5240
Yan Zheng2b820322008-11-17 21:11:30 -05005241 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005242 for (i = 0; i < map->num_stripes; i++) {
5243 device = map->stripes[i].dev;
5244 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005245
5246 btrfs_set_stack_stripe_devid(stripe, device->devid);
5247 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5248 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5249 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005250 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005251 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005252
5253 btrfs_set_stack_chunk_length(chunk, chunk_size);
5254 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5255 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5256 btrfs_set_stack_chunk_type(chunk, map->type);
5257 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5258 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5259 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005260 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005261 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5262
5263 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5264 key.type = BTRFS_CHUNK_ITEM_KEY;
5265 key.offset = chunk_offset;
5266
5267 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005268 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5269 /*
5270 * TODO: Cleanup of inserted chunk root in case of
5271 * failure.
5272 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005273 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005274 }
liubo1abe9b82011-03-24 11:18:59 +00005275
Josef Bacik6df9a952013-06-27 13:22:46 -04005276out:
Yan Zheng2b820322008-11-17 21:11:30 -05005277 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005278 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005279 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005280}
5281
5282/*
Andrea Gelmini52042d82018-11-28 12:05:13 +01005283 * Chunk allocation falls into two parts. The first part does work
5284 * that makes the new allocated chunk usable, but does not do any operation
5285 * that modifies the chunk tree. The second part does the work that
5286 * requires modifying the chunk tree. This division is important for the
Yan Zheng2b820322008-11-17 21:11:30 -05005287 * bootstrap process of adding storage to a seed btrfs.
5288 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005289int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005290{
5291 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005292
Nikolay Borisovc216b202018-06-20 15:49:06 +03005293 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5294 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005295 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005296}
5297
David Sterba6f8e0fc2019-03-20 16:29:13 +01005298static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005299{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005300 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005301 u64 chunk_offset;
5302 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005303 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005304 int ret;
5305
Josef Bacik6df9a952013-06-27 13:22:46 -04005306 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005307 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005308 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005309 if (ret)
5310 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005311
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005312 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005313 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005314 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005315 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005316}
5317
Miao Xied20983b2014-07-03 18:22:13 +08005318static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5319{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005320 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005321
David Sterbafc9a2ac2019-05-17 11:43:22 +02005322 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005323}
5324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005325int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005326{
5327 struct extent_map *em;
5328 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005329 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005330 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005331 int i;
5332
Omar Sandoval60ca8422018-05-16 16:34:31 -07005333 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005334 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005335 return 1;
5336
Jeff Mahoney95617d62015-06-03 10:55:48 -04005337 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005338 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005339 if (test_bit(BTRFS_DEV_STATE_MISSING,
5340 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005341 miss_ndevs++;
5342 continue;
5343 }
Anand Jainebbede42017-12-04 12:54:52 +08005344 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5345 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005346 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005347 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005348 }
5349 }
Miao Xied20983b2014-07-03 18:22:13 +08005350
5351 /*
5352 * If the number of missing devices is larger than max errors,
5353 * we can not write the data into that chunk successfully, so
5354 * set it readonly.
5355 */
5356 if (miss_ndevs > btrfs_chunk_max_errors(map))
5357 readonly = 1;
5358end:
Yan Zheng2b820322008-11-17 21:11:30 -05005359 free_extent_map(em);
5360 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005361}
5362
David Sterbac8bf1b62019-05-17 11:43:17 +02005363void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005364{
5365 struct extent_map *em;
5366
Chris Masond3977122009-01-05 21:25:51 -05005367 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005368 write_lock(&tree->lock);
5369 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005370 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005371 remove_extent_mapping(tree, em);
5372 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005373 if (!em)
5374 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005375 /* once for us */
5376 free_extent_map(em);
5377 /* once for the tree */
5378 free_extent_map(em);
5379 }
5380}
5381
Stefan Behrens5d964052012-11-05 14:59:07 +01005382int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005383{
5384 struct extent_map *em;
5385 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005386 int ret;
5387
Omar Sandoval60ca8422018-05-16 16:34:31 -07005388 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005389 if (IS_ERR(em))
5390 /*
5391 * We could return errors for these cases, but that could get
5392 * ugly and we'd probably do the same thing which is just not do
5393 * anything else and exit, so return 1 so the callers don't try
5394 * to use other copies.
5395 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005396 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005397
Jeff Mahoney95617d62015-06-03 10:55:48 -04005398 map = em->map_lookup;
David Sterbac7369b32019-05-31 15:39:31 +02005399 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
Chris Masonf1885912008-04-09 16:28:12 -04005400 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005401 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5402 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005403 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5404 ret = 2;
5405 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005406 /*
5407 * There could be two corrupted data stripes, we need
5408 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005409 *
Liu Bo8810f752018-01-02 13:36:41 -07005410 * Fail a stripe at a time on every retry except the
5411 * stripe under reconstruction.
5412 */
5413 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005414 else
5415 ret = 1;
5416 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005417
David Sterbacb5583d2018-09-07 16:11:23 +02005418 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005419 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5420 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005421 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005422 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005423
Chris Masonf1885912008-04-09 16:28:12 -04005424 return ret;
5425}
5426
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005427unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005428 u64 logical)
5429{
5430 struct extent_map *em;
5431 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005432 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005433
Omar Sandoval60ca8422018-05-16 16:34:31 -07005434 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005435
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005436 if (!WARN_ON(IS_ERR(em))) {
5437 map = em->map_lookup;
5438 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5439 len = map->stripe_len * nr_data_stripes(map);
5440 free_extent_map(em);
5441 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005442 return len;
5443}
5444
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005445int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005446{
5447 struct extent_map *em;
5448 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005449 int ret = 0;
5450
Omar Sandoval60ca8422018-05-16 16:34:31 -07005451 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005452
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005453 if(!WARN_ON(IS_ERR(em))) {
5454 map = em->map_lookup;
5455 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5456 ret = 1;
5457 free_extent_map(em);
5458 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005459 return ret;
5460}
5461
Stefan Behrens30d98612012-11-06 14:52:18 +01005462static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005463 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005464 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005465{
5466 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005467 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005468 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005469 int tolerance;
5470 struct btrfs_device *srcdev;
5471
Anand Jain99f92a72018-03-14 16:29:12 +08005472 ASSERT((map->type &
David Sterbac7369b32019-05-31 15:39:31 +02005473 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
Anand Jain99f92a72018-03-14 16:29:12 +08005474
5475 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5476 num_stripes = map->sub_stripes;
5477 else
5478 num_stripes = map->num_stripes;
5479
Anand Jain8ba0ae72018-03-14 16:29:13 +08005480 preferred_mirror = first + current->pid % num_stripes;
5481
Stefan Behrens30d98612012-11-06 14:52:18 +01005482 if (dev_replace_is_ongoing &&
5483 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5484 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5485 srcdev = fs_info->dev_replace.srcdev;
5486 else
5487 srcdev = NULL;
5488
5489 /*
5490 * try to avoid the drive that is the source drive for a
5491 * dev-replace procedure, only choose it if no other non-missing
5492 * mirror is available
5493 */
5494 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005495 if (map->stripes[preferred_mirror].dev->bdev &&
5496 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5497 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005498 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005499 if (map->stripes[i].dev->bdev &&
5500 (tolerance || map->stripes[i].dev != srcdev))
5501 return i;
5502 }
Chris Masondfe25022008-05-13 13:46:40 -04005503 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005504
Chris Masondfe25022008-05-13 13:46:40 -04005505 /* we couldn't find one that doesn't fail. Just return something
5506 * and the io error handling code will clean up eventually
5507 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005508 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005509}
5510
David Woodhouse53b381b2013-01-29 18:40:14 -05005511static inline int parity_smaller(u64 a, u64 b)
5512{
5513 return a > b;
5514}
5515
5516/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005517static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005518{
5519 struct btrfs_bio_stripe s;
5520 int i;
5521 u64 l;
5522 int again = 1;
5523
5524 while (again) {
5525 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005526 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005527 if (parity_smaller(bbio->raid_map[i],
5528 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005529 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005530 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005531 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005532 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005533 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005534 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005535
David Woodhouse53b381b2013-01-29 18:40:14 -05005536 again = 1;
5537 }
5538 }
5539 }
5540}
5541
Zhao Lei6e9606d2015-01-20 15:11:34 +08005542static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5543{
5544 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005545 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005546 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005547 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005548 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005549 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005550 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005551 /*
5552 * plus the raid_map, which includes both the tgt dev
5553 * and the stripes
5554 */
5555 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005556 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005557
5558 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005559 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005560
5561 return bbio;
5562}
5563
5564void btrfs_get_bbio(struct btrfs_bio *bbio)
5565{
Elena Reshetova140475a2017-03-03 10:55:10 +02005566 WARN_ON(!refcount_read(&bbio->refs));
5567 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005568}
5569
5570void btrfs_put_bbio(struct btrfs_bio *bbio)
5571{
5572 if (!bbio)
5573 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005574 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005575 kfree(bbio);
5576}
5577
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005578/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5579/*
5580 * Please note that, discard won't be sent to target device of device
5581 * replace.
5582 */
5583static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5584 u64 logical, u64 length,
5585 struct btrfs_bio **bbio_ret)
5586{
5587 struct extent_map *em;
5588 struct map_lookup *map;
5589 struct btrfs_bio *bbio;
5590 u64 offset;
5591 u64 stripe_nr;
5592 u64 stripe_nr_end;
5593 u64 stripe_end_offset;
5594 u64 stripe_cnt;
5595 u64 stripe_len;
5596 u64 stripe_offset;
5597 u64 num_stripes;
5598 u32 stripe_index;
5599 u32 factor = 0;
5600 u32 sub_stripes = 0;
5601 u64 stripes_per_dev = 0;
5602 u32 remaining_stripes = 0;
5603 u32 last_stripe = 0;
5604 int ret = 0;
5605 int i;
5606
5607 /* discard always return a bbio */
5608 ASSERT(bbio_ret);
5609
Omar Sandoval60ca8422018-05-16 16:34:31 -07005610 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005611 if (IS_ERR(em))
5612 return PTR_ERR(em);
5613
5614 map = em->map_lookup;
5615 /* we don't discard raid56 yet */
5616 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5617 ret = -EOPNOTSUPP;
5618 goto out;
5619 }
5620
5621 offset = logical - em->start;
5622 length = min_t(u64, em->len - offset, length);
5623
5624 stripe_len = map->stripe_len;
5625 /*
5626 * stripe_nr counts the total number of stripes we have to stride
5627 * to get to this block
5628 */
5629 stripe_nr = div64_u64(offset, stripe_len);
5630
5631 /* stripe_offset is the offset of this block in its stripe */
5632 stripe_offset = offset - stripe_nr * stripe_len;
5633
5634 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005635 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005636 stripe_cnt = stripe_nr_end - stripe_nr;
5637 stripe_end_offset = stripe_nr_end * map->stripe_len -
5638 (offset + length);
5639 /*
5640 * after this, stripe_nr is the number of stripes on this
5641 * device we have to walk to find the data, and stripe_index is
5642 * the number of our device in the stripe array
5643 */
5644 num_stripes = 1;
5645 stripe_index = 0;
5646 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5647 BTRFS_BLOCK_GROUP_RAID10)) {
5648 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5649 sub_stripes = 1;
5650 else
5651 sub_stripes = map->sub_stripes;
5652
5653 factor = map->num_stripes / sub_stripes;
5654 num_stripes = min_t(u64, map->num_stripes,
5655 sub_stripes * stripe_cnt);
5656 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5657 stripe_index *= sub_stripes;
5658 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5659 &remaining_stripes);
5660 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5661 last_stripe *= sub_stripes;
David Sterbac7369b32019-05-31 15:39:31 +02005662 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005663 BTRFS_BLOCK_GROUP_DUP)) {
5664 num_stripes = map->num_stripes;
5665 } else {
5666 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5667 &stripe_index);
5668 }
5669
5670 bbio = alloc_btrfs_bio(num_stripes, 0);
5671 if (!bbio) {
5672 ret = -ENOMEM;
5673 goto out;
5674 }
5675
5676 for (i = 0; i < num_stripes; i++) {
5677 bbio->stripes[i].physical =
5678 map->stripes[stripe_index].physical +
5679 stripe_offset + stripe_nr * map->stripe_len;
5680 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5681
5682 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5683 BTRFS_BLOCK_GROUP_RAID10)) {
5684 bbio->stripes[i].length = stripes_per_dev *
5685 map->stripe_len;
5686
5687 if (i / sub_stripes < remaining_stripes)
5688 bbio->stripes[i].length +=
5689 map->stripe_len;
5690
5691 /*
5692 * Special for the first stripe and
5693 * the last stripe:
5694 *
5695 * |-------|...|-------|
5696 * |----------|
5697 * off end_off
5698 */
5699 if (i < sub_stripes)
5700 bbio->stripes[i].length -=
5701 stripe_offset;
5702
5703 if (stripe_index >= last_stripe &&
5704 stripe_index <= (last_stripe +
5705 sub_stripes - 1))
5706 bbio->stripes[i].length -=
5707 stripe_end_offset;
5708
5709 if (i == sub_stripes - 1)
5710 stripe_offset = 0;
5711 } else {
5712 bbio->stripes[i].length = length;
5713 }
5714
5715 stripe_index++;
5716 if (stripe_index == map->num_stripes) {
5717 stripe_index = 0;
5718 stripe_nr++;
5719 }
5720 }
5721
5722 *bbio_ret = bbio;
5723 bbio->map_type = map->type;
5724 bbio->num_stripes = num_stripes;
5725out:
5726 free_extent_map(em);
5727 return ret;
5728}
5729
Liu Bo5ab56092017-03-14 13:33:57 -07005730/*
5731 * In dev-replace case, for repair case (that's the only case where the mirror
5732 * is selected explicitly when calling btrfs_map_block), blocks left of the
5733 * left cursor can also be read from the target drive.
5734 *
5735 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5736 * array of stripes.
5737 * For READ, it also needs to be supported using the same mirror number.
5738 *
5739 * If the requested block is not left of the left cursor, EIO is returned. This
5740 * can happen because btrfs_num_copies() returns one more in the dev-replace
5741 * case.
5742 */
5743static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5744 u64 logical, u64 length,
5745 u64 srcdev_devid, int *mirror_num,
5746 u64 *physical)
5747{
5748 struct btrfs_bio *bbio = NULL;
5749 int num_stripes;
5750 int index_srcdev = 0;
5751 int found = 0;
5752 u64 physical_of_found = 0;
5753 int i;
5754 int ret = 0;
5755
5756 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5757 logical, &length, &bbio, 0, 0);
5758 if (ret) {
5759 ASSERT(bbio == NULL);
5760 return ret;
5761 }
5762
5763 num_stripes = bbio->num_stripes;
5764 if (*mirror_num > num_stripes) {
5765 /*
5766 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5767 * that means that the requested area is not left of the left
5768 * cursor
5769 */
5770 btrfs_put_bbio(bbio);
5771 return -EIO;
5772 }
5773
5774 /*
5775 * process the rest of the function using the mirror_num of the source
5776 * drive. Therefore look it up first. At the end, patch the device
5777 * pointer to the one of the target drive.
5778 */
5779 for (i = 0; i < num_stripes; i++) {
5780 if (bbio->stripes[i].dev->devid != srcdev_devid)
5781 continue;
5782
5783 /*
5784 * In case of DUP, in order to keep it simple, only add the
5785 * mirror with the lowest physical address
5786 */
5787 if (found &&
5788 physical_of_found <= bbio->stripes[i].physical)
5789 continue;
5790
5791 index_srcdev = i;
5792 found = 1;
5793 physical_of_found = bbio->stripes[i].physical;
5794 }
5795
5796 btrfs_put_bbio(bbio);
5797
5798 ASSERT(found);
5799 if (!found)
5800 return -EIO;
5801
5802 *mirror_num = index_srcdev + 1;
5803 *physical = physical_of_found;
5804 return ret;
5805}
5806
Liu Bo73c0f222017-03-14 13:33:58 -07005807static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5808 struct btrfs_bio **bbio_ret,
5809 struct btrfs_dev_replace *dev_replace,
5810 int *num_stripes_ret, int *max_errors_ret)
5811{
5812 struct btrfs_bio *bbio = *bbio_ret;
5813 u64 srcdev_devid = dev_replace->srcdev->devid;
5814 int tgtdev_indexes = 0;
5815 int num_stripes = *num_stripes_ret;
5816 int max_errors = *max_errors_ret;
5817 int i;
5818
5819 if (op == BTRFS_MAP_WRITE) {
5820 int index_where_to_add;
5821
5822 /*
5823 * duplicate the write operations while the dev replace
5824 * procedure is running. Since the copying of the old disk to
5825 * the new disk takes place at run time while the filesystem is
5826 * mounted writable, the regular write operations to the old
5827 * disk have to be duplicated to go to the new disk as well.
5828 *
5829 * Note that device->missing is handled by the caller, and that
5830 * the write to the old disk is already set up in the stripes
5831 * array.
5832 */
5833 index_where_to_add = num_stripes;
5834 for (i = 0; i < num_stripes; i++) {
5835 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5836 /* write to new disk, too */
5837 struct btrfs_bio_stripe *new =
5838 bbio->stripes + index_where_to_add;
5839 struct btrfs_bio_stripe *old =
5840 bbio->stripes + i;
5841
5842 new->physical = old->physical;
5843 new->length = old->length;
5844 new->dev = dev_replace->tgtdev;
5845 bbio->tgtdev_map[i] = index_where_to_add;
5846 index_where_to_add++;
5847 max_errors++;
5848 tgtdev_indexes++;
5849 }
5850 }
5851 num_stripes = index_where_to_add;
5852 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5853 int index_srcdev = 0;
5854 int found = 0;
5855 u64 physical_of_found = 0;
5856
5857 /*
5858 * During the dev-replace procedure, the target drive can also
5859 * be used to read data in case it is needed to repair a corrupt
5860 * block elsewhere. This is possible if the requested area is
5861 * left of the left cursor. In this area, the target drive is a
5862 * full copy of the source drive.
5863 */
5864 for (i = 0; i < num_stripes; i++) {
5865 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5866 /*
5867 * In case of DUP, in order to keep it simple,
5868 * only add the mirror with the lowest physical
5869 * address
5870 */
5871 if (found &&
5872 physical_of_found <=
5873 bbio->stripes[i].physical)
5874 continue;
5875 index_srcdev = i;
5876 found = 1;
5877 physical_of_found = bbio->stripes[i].physical;
5878 }
5879 }
5880 if (found) {
5881 struct btrfs_bio_stripe *tgtdev_stripe =
5882 bbio->stripes + num_stripes;
5883
5884 tgtdev_stripe->physical = physical_of_found;
5885 tgtdev_stripe->length =
5886 bbio->stripes[index_srcdev].length;
5887 tgtdev_stripe->dev = dev_replace->tgtdev;
5888 bbio->tgtdev_map[index_srcdev] = num_stripes;
5889
5890 tgtdev_indexes++;
5891 num_stripes++;
5892 }
5893 }
5894
5895 *num_stripes_ret = num_stripes;
5896 *max_errors_ret = max_errors;
5897 bbio->num_tgtdevs = tgtdev_indexes;
5898 *bbio_ret = bbio;
5899}
5900
Liu Bo2b19a1f2017-03-14 13:34:00 -07005901static bool need_full_stripe(enum btrfs_map_op op)
5902{
5903 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5904}
5905
Nikolay Borisov5f141122019-06-03 12:05:03 +03005906/*
5907 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5908 * tuple. This information is used to calculate how big a
5909 * particular bio can get before it straddles a stripe.
5910 *
5911 * @fs_info - the filesystem
5912 * @logical - address that we want to figure out the geometry of
5913 * @len - the length of IO we are going to perform, starting at @logical
5914 * @op - type of operation - write or read
5915 * @io_geom - pointer used to return values
5916 *
5917 * Returns < 0 in case a chunk for the given logical address cannot be found,
5918 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5919 */
5920int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005921 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
Nikolay Borisov5f141122019-06-03 12:05:03 +03005922{
5923 struct extent_map *em;
5924 struct map_lookup *map;
5925 u64 offset;
5926 u64 stripe_offset;
5927 u64 stripe_nr;
5928 u64 stripe_len;
5929 u64 raid56_full_stripe_start = (u64)-1;
5930 int data_stripes;
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005931 int ret = 0;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005932
5933 ASSERT(op != BTRFS_MAP_DISCARD);
5934
5935 em = btrfs_get_chunk_map(fs_info, logical, len);
5936 if (IS_ERR(em))
5937 return PTR_ERR(em);
5938
5939 map = em->map_lookup;
5940 /* Offset of this logical address in the chunk */
5941 offset = logical - em->start;
5942 /* Len of a stripe in a chunk */
5943 stripe_len = map->stripe_len;
5944 /* Stripe wher this block falls in */
5945 stripe_nr = div64_u64(offset, stripe_len);
5946 /* Offset of stripe in the chunk */
5947 stripe_offset = stripe_nr * stripe_len;
5948 if (offset < stripe_offset) {
5949 btrfs_crit(fs_info,
5950"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5951 stripe_offset, offset, em->start, logical, stripe_len);
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005952 ret = -EINVAL;
5953 goto out;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005954 }
5955
5956 /* stripe_offset is the offset of this block in its stripe */
5957 stripe_offset = offset - stripe_offset;
5958 data_stripes = nr_data_stripes(map);
5959
5960 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5961 u64 max_len = stripe_len - stripe_offset;
5962
5963 /*
5964 * In case of raid56, we need to know the stripe aligned start
5965 */
5966 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5967 unsigned long full_stripe_len = stripe_len * data_stripes;
5968 raid56_full_stripe_start = offset;
5969
5970 /*
5971 * Allow a write of a full stripe, but make sure we
5972 * don't allow straddling of stripes
5973 */
5974 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5975 full_stripe_len);
5976 raid56_full_stripe_start *= full_stripe_len;
5977
5978 /*
5979 * For writes to RAID[56], allow a full stripeset across
5980 * all disks. For other RAID types and for RAID[56]
5981 * reads, just allow a single stripe (on a single disk).
5982 */
5983 if (op == BTRFS_MAP_WRITE) {
5984 max_len = stripe_len * data_stripes -
5985 (offset - raid56_full_stripe_start);
5986 }
5987 }
5988 len = min_t(u64, em->len - offset, max_len);
5989 } else {
5990 len = em->len - offset;
5991 }
5992
5993 io_geom->len = len;
5994 io_geom->offset = offset;
5995 io_geom->stripe_len = stripe_len;
5996 io_geom->stripe_nr = stripe_nr;
5997 io_geom->stripe_offset = stripe_offset;
5998 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5999
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02006000out:
6001 /* once for us */
6002 free_extent_map(em);
6003 return ret;
Nikolay Borisov5f141122019-06-03 12:05:03 +03006004}
6005
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006006static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
6007 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006008 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006009 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006010 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04006011{
6012 struct extent_map *em;
6013 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04006014 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04006015 u64 stripe_offset;
6016 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006017 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006018 u32 stripe_index;
David Sterbacff82672019-05-17 11:43:45 +02006019 int data_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04006020 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006021 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006022 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006023 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006024 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006025 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006026 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6027 int dev_replace_is_ongoing = 0;
6028 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006029 int patch_the_first_stripe_for_dev_replace = 0;
6030 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006031 u64 raid56_full_stripe_start = (u64)-1;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006032 struct btrfs_io_geometry geom;
6033
6034 ASSERT(bbio_ret);
Chris Mason0b86a832008-03-24 15:01:56 -04006035
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006036 if (op == BTRFS_MAP_DISCARD)
6037 return __btrfs_map_block_for_discard(fs_info, logical,
6038 *length, bbio_ret);
6039
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006040 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
6041 if (ret < 0)
6042 return ret;
6043
Omar Sandoval60ca8422018-05-16 16:34:31 -07006044 em = btrfs_get_chunk_map(fs_info, logical, *length);
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006045 ASSERT(em);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006046 map = em->map_lookup;
Chris Mason593060d2008-03-25 16:50:33 -04006047
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006048 *length = geom.len;
6049 offset = geom.offset;
6050 stripe_len = geom.stripe_len;
6051 stripe_nr = geom.stripe_nr;
6052 stripe_offset = geom.stripe_offset;
6053 raid56_full_stripe_start = geom.raid56_stripe_offset;
David Sterbacff82672019-05-17 11:43:45 +02006054 data_stripes = nr_data_stripes(map);
Chris Mason593060d2008-03-25 16:50:33 -04006055
David Sterbacb5583d2018-09-07 16:11:23 +02006056 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006057 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006058 /*
6059 * Hold the semaphore for read during the whole operation, write is
6060 * requested at commit time but must wait.
6061 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006062 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006063 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006064
Stefan Behrensad6d6202012-11-06 15:06:47 +01006065 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006066 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006067 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6068 dev_replace->srcdev->devid,
6069 &mirror_num,
6070 &physical_to_patch_in_first_stripe);
6071 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006072 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006073 else
6074 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006075 } else if (mirror_num > map->num_stripes) {
6076 mirror_num = 0;
6077 }
6078
Chris Masonf2d8d742008-04-21 10:03:05 -04006079 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006080 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006081 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006082 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6083 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006084 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006085 mirror_num = 1;
David Sterbac7369b32019-05-31 15:39:31 +02006086 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006087 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006088 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006089 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006090 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006091 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006092 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006093 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006094 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006095 }
Chris Mason2fff7342008-04-29 14:12:09 -04006096
Chris Mason611f0e02008-04-03 16:29:03 -04006097 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006098 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006099 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006100 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006101 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006102 } else {
6103 mirror_num = 1;
6104 }
Chris Mason2fff7342008-04-29 14:12:09 -04006105
Chris Mason321aecc2008-04-16 10:49:51 -04006106 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006107 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006108
David Sterba47c57132015-02-20 18:43:47 +01006109 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006110 stripe_index *= map->sub_stripes;
6111
Anand Jainde483732017-10-12 16:43:00 +08006112 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006113 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006114 else if (mirror_num)
6115 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006116 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006117 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006118 stripe_index = find_live_mirror(fs_info, map,
6119 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006120 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006121 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006122 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006123
Zhao Leiffe2d202015-01-20 15:11:44 +08006124 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006125 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006126 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006127 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbacff82672019-05-17 11:43:45 +02006128 stripe_len * data_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05006129
6130 /* RAID[56] write or recovery. Return all stripes */
6131 num_stripes = map->num_stripes;
6132 max_errors = nr_parity_stripes(map);
6133
David Woodhouse53b381b2013-01-29 18:40:14 -05006134 *length = map->stripe_len;
6135 stripe_index = 0;
6136 stripe_offset = 0;
6137 } else {
6138 /*
6139 * Mirror #0 or #1 means the original data block.
6140 * Mirror #2 is RAID5 parity block.
6141 * Mirror #3 is RAID6 Q block.
6142 */
David Sterba47c57132015-02-20 18:43:47 +01006143 stripe_nr = div_u64_rem(stripe_nr,
David Sterbacff82672019-05-17 11:43:45 +02006144 data_stripes, &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006145 if (mirror_num > 1)
David Sterbacff82672019-05-17 11:43:45 +02006146 stripe_index = data_stripes + mirror_num - 2;
David Woodhouse53b381b2013-01-29 18:40:14 -05006147
6148 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006149 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6150 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006151 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006152 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006153 }
Chris Mason8790d502008-04-03 16:29:03 -04006154 } else {
6155 /*
David Sterba47c57132015-02-20 18:43:47 +01006156 * after this, stripe_nr is the number of stripes on this
6157 * device we have to walk to find the data, and stripe_index is
6158 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006159 */
David Sterba47c57132015-02-20 18:43:47 +01006160 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6161 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006162 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006163 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006164 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006165 btrfs_crit(fs_info,
6166 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006167 stripe_index, map->num_stripes);
6168 ret = -EINVAL;
6169 goto out;
6170 }
Chris Mason593060d2008-03-25 16:50:33 -04006171
Stefan Behrens472262f2012-11-06 14:43:46 +01006172 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006173 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006174 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006175 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006176 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006177 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006178 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006179 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006180
Zhao Lei6e9606d2015-01-20 15:11:34 +08006181 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006182 if (!bbio) {
6183 ret = -ENOMEM;
6184 goto out;
6185 }
Liu Bo6fad8232017-03-14 13:33:59 -07006186 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006187 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006188
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006189 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006190 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6191 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006192 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006193 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006194
6195 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6196 sizeof(struct btrfs_bio_stripe) *
6197 num_alloc_stripes +
6198 sizeof(int) * tgtdev_indexes);
6199
6200 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006201 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006202
6203 /* Fill in the logical address of each stripe */
David Sterbacff82672019-05-17 11:43:45 +02006204 tmp = stripe_nr * data_stripes;
6205 for (i = 0; i < data_stripes; i++)
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006206 bbio->raid_map[(i+rot) % num_stripes] =
6207 em->start + (tmp + i) * map->stripe_len;
6208
6209 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6210 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6211 bbio->raid_map[(i+rot+1) % num_stripes] =
6212 RAID6_Q_STRIPE;
6213 }
6214
Li Zefanec9ef7a2011-12-01 14:06:42 +08006215
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006216 for (i = 0; i < num_stripes; i++) {
6217 bbio->stripes[i].physical =
6218 map->stripes[stripe_index].physical +
6219 stripe_offset +
6220 stripe_nr * map->stripe_len;
6221 bbio->stripes[i].dev =
6222 map->stripes[stripe_index].dev;
6223 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006224 }
Li Zefande11cc12011-12-01 12:55:47 +08006225
Liu Bo2b19a1f2017-03-14 13:34:00 -07006226 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006227 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006228
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006229 if (bbio->raid_map)
6230 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006231
Liu Bo73c0f222017-03-14 13:33:58 -07006232 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006233 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006234 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6235 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006236 }
6237
Li Zefande11cc12011-12-01 12:55:47 +08006238 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006239 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006240 bbio->num_stripes = num_stripes;
6241 bbio->max_errors = max_errors;
6242 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006243
6244 /*
6245 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6246 * mirror_num == num_stripes + 1 && dev_replace target drive is
6247 * available as a mirror
6248 */
6249 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6250 WARN_ON(num_stripes > 1);
6251 bbio->stripes[0].dev = dev_replace->tgtdev;
6252 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6253 bbio->mirror_num = map->num_stripes + 1;
6254 }
Chris Masoncea9e442008-04-09 16:28:12 -04006255out:
Liu Bo73beece2015-07-17 16:49:19 +08006256 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006257 lockdep_assert_held(&dev_replace->rwsem);
6258 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006259 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006260 }
Chris Mason0b86a832008-03-24 15:01:56 -04006261 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006262 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006263}
6264
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006265int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006266 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006267 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006268{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006269 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006270 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006271}
6272
Miao Xieaf8e2d12014-10-23 14:42:50 +08006273/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006274int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006275 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006276 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006277{
David Sterba825ad4c2017-03-28 14:45:22 +02006278 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006279}
6280
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006281int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6282 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006283{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006284 struct extent_map *em;
6285 struct map_lookup *map;
6286 u64 *buf;
6287 u64 bytenr;
6288 u64 length;
6289 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006290 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006291 int i, j, nr = 0;
6292
Omar Sandoval60ca8422018-05-16 16:34:31 -07006293 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006294 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006295 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006296
Jeff Mahoney95617d62015-06-03 10:55:48 -04006297 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006298 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006299 rmap_len = map->stripe_len;
6300
Yan Zhenga512bbf2008-12-08 16:46:26 -05006301 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006302 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006303 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006304 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006305 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006306 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006307 rmap_len = map->stripe_len * nr_data_stripes(map);
6308 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006309
David Sterba31e818f2015-02-20 18:00:26 +01006310 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006311 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006312
6313 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006314 if (map->stripes[i].physical > physical ||
6315 map->stripes[i].physical + length <= physical)
6316 continue;
6317
6318 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006319 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006320
6321 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6322 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006323 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006324 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6325 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006326 } /* else if RAID[56], multiply by nr_data_stripes().
6327 * Alternatively, just use rmap_len below instead of
6328 * map->stripe_len */
6329
6330 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006331 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006332 for (j = 0; j < nr; j++) {
6333 if (buf[j] == bytenr)
6334 break;
6335 }
Chris Mason934d3752008-12-08 16:43:10 -05006336 if (j == nr) {
6337 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006338 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006339 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006340 }
6341
Yan Zhenga512bbf2008-12-08 16:46:26 -05006342 *logical = buf;
6343 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006344 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006345
6346 free_extent_map(em);
6347 return 0;
6348}
6349
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006350static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006351{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006352 bio->bi_private = bbio->private;
6353 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006354 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006355
Zhao Lei6e9606d2015-01-20 15:11:34 +08006356 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006357}
6358
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006359static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006360{
Chris Mason9be33952013-05-17 18:30:14 -04006361 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006362 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006363
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006364 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006365 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006366 if (bio->bi_status == BLK_STS_IOERR ||
6367 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006368 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006369 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006370 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006371
6372 BUG_ON(stripe_index >= bbio->num_stripes);
6373 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006374 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006375 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006376 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006377 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterba0cc068e2019-03-07 15:40:50 +01006378 else if (!(bio->bi_opf & REQ_RAHEAD))
Anand Jain1cb34c82017-10-21 01:45:33 +08006379 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006380 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006381 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006382 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006383 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006384 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006385 }
6386 }
Chris Mason8790d502008-04-03 16:29:03 -04006387
Jan Schmidta1d3c472011-08-04 17:15:33 +02006388 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006389 is_orig_bio = 1;
6390
Miao Xiec404e0d2014-01-30 16:46:55 +08006391 btrfs_bio_counter_dec(bbio->fs_info);
6392
Jan Schmidta1d3c472011-08-04 17:15:33 +02006393 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006394 if (!is_orig_bio) {
6395 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006396 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006397 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006398
Chris Mason9be33952013-05-17 18:30:14 -04006399 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006400 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006401 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006402 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006403 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006404 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006405 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006406 /*
6407 * this bio is actually up to date, we didn't
6408 * go over the max number of errors
6409 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006410 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006411 }
Miao Xiec55f1392014-06-19 10:42:54 +08006412
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006413 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006414 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006415 bio_put(bio);
6416 }
Chris Mason8790d502008-04-03 16:29:03 -04006417}
6418
Chris Mason8b712842008-06-11 16:50:36 -04006419/*
6420 * see run_scheduled_bios for a description of why bios are collected for
6421 * async submit.
6422 *
6423 * This will add one bio to the pending list for a device and make sure
6424 * the work struct is scheduled.
6425 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006426static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006427 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006429 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006430 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006431 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006432
6433 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006434 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006435 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006436 return;
Chris Mason8b712842008-06-11 16:50:36 -04006437 }
6438
Chris Mason492bb6de2008-07-31 16:29:02 -04006439 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006440 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006441
6442 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006443 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006444 pending_bios = &device->pending_sync_bios;
6445 else
6446 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006447
Chris Masonffbd5172009-04-20 15:50:09 -04006448 if (pending_bios->tail)
6449 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006450
Chris Masonffbd5172009-04-20 15:50:09 -04006451 pending_bios->tail = bio;
6452 if (!pending_bios->head)
6453 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006454 if (device->running_pending)
6455 should_queue = 0;
6456
6457 spin_unlock(&device->io_lock);
6458
6459 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006460 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006461}
6462
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006463static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6464 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006465{
6466 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006468
6469 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006470 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006471 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006472 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006473 btrfs_debug_in_rcu(fs_info,
6474 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6475 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6476 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6477 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006478 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006480 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006481
Josef Bacikde1ee922012-10-19 16:50:56 -04006482 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006483 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006484 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006485 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006486}
6487
Josef Bacikde1ee922012-10-19 16:50:56 -04006488static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6489{
6490 atomic_inc(&bbio->error);
6491 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006492 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006493 WARN_ON(bio != bbio->orig_bio);
6494
Chris Mason9be33952013-05-17 18:30:14 -04006495 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006496 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006497 if (atomic_read(&bbio->error) > bbio->max_errors)
6498 bio->bi_status = BLK_STS_IOERR;
6499 else
6500 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006501 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006502 }
6503}
6504
Omar Sandoval58efbc92017-08-22 23:45:59 -07006505blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6506 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006507{
Chris Mason0b86a832008-03-24 15:01:56 -04006508 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006509 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006510 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006511 u64 length = 0;
6512 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006513 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006514 int dev_nr;
6515 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006516 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006517
Kent Overstreet4f024f32013-10-11 15:44:27 -07006518 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006519 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006520
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006521 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006522 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006523 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006524 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006525 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006526 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006527 }
Chris Masoncea9e442008-04-09 16:28:12 -04006528
Jan Schmidta1d3c472011-08-04 17:15:33 +02006529 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006530 bbio->orig_bio = first_bio;
6531 bbio->private = first_bio->bi_private;
6532 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006533 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006534 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6535
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006536 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006537 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006538 /* In this case, map_length has been set to the length of
6539 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006540 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006541 ret = raid56_parity_write(fs_info, bio, bbio,
6542 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006543 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006544 ret = raid56_parity_recover(fs_info, bio, bbio,
6545 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006546 }
Miao Xie42452152014-11-25 16:39:28 +08006547
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006548 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006549 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006550 }
6551
Chris Masoncea9e442008-04-09 16:28:12 -04006552 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006553 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006554 "mapping failed logical %llu bio len %llu len %llu",
6555 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006556 BUG();
6557 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006558
Zhao Lei08da7572015-02-12 15:42:16 +08006559 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006560 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006561 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6562 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006563 (bio_op(first_bio) == REQ_OP_WRITE &&
6564 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006565 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006566 continue;
6567 }
6568
David Sterba3aa8e072017-06-02 17:38:30 +02006569 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006570 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006571 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006572 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006573
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006574 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6575 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006576 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006577 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006578 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006579}
6580
Anand Jain09ba3bc2019-01-19 14:48:55 +08006581/*
6582 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6583 * return NULL.
6584 *
6585 * If devid and uuid are both specified, the match must be exact, otherwise
6586 * only devid is used.
6587 *
6588 * If @seed is true, traverse through the seed devices.
6589 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006590struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006591 u64 devid, u8 *uuid, u8 *fsid,
6592 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006593{
Yan Zheng2b820322008-11-17 21:11:30 -05006594 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006595
Anand Jaine4319cd2019-01-17 23:32:31 +08006596 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006597 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006598 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08006599 list_for_each_entry(device, &fs_devices->devices,
6600 dev_list) {
6601 if (device->devid == devid &&
6602 (!uuid || memcmp(device->uuid, uuid,
6603 BTRFS_UUID_SIZE) == 0))
6604 return device;
6605 }
Yan Zheng2b820322008-11-17 21:11:30 -05006606 }
Anand Jain09ba3bc2019-01-19 14:48:55 +08006607 if (seed)
6608 fs_devices = fs_devices->seed;
6609 else
6610 return NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05006611 }
6612 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006613}
6614
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006615static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006616 u64 devid, u8 *dev_uuid)
6617{
6618 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006619
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006620 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6621 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006622 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006623
6624 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006625 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006626 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006627
Anand Jaine6e674b2017-12-04 12:54:54 +08006628 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006629 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006630
Chris Masondfe25022008-05-13 13:46:40 -04006631 return device;
6632}
6633
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006634/**
6635 * btrfs_alloc_device - allocate struct btrfs_device
6636 * @fs_info: used only for generating a new devid, can be NULL if
6637 * devid is provided (i.e. @devid != NULL).
6638 * @devid: a pointer to devid for this device. If NULL a new devid
6639 * is generated.
6640 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6641 * is generated.
6642 *
6643 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006644 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006645 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006646 */
6647struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6648 const u64 *devid,
6649 const u8 *uuid)
6650{
6651 struct btrfs_device *dev;
6652 u64 tmp;
6653
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306654 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006655 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006656
6657 dev = __alloc_device();
6658 if (IS_ERR(dev))
6659 return dev;
6660
6661 if (devid)
6662 tmp = *devid;
6663 else {
6664 int ret;
6665
6666 ret = find_next_devid(fs_info, &tmp);
6667 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006668 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006669 return ERR_PTR(ret);
6670 }
6671 }
6672 dev->devid = tmp;
6673
6674 if (uuid)
6675 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6676 else
6677 generate_random_uuid(dev->uuid);
6678
Liu Bo9e0af232014-08-15 23:36:53 +08006679 btrfs_init_work(&dev->work, btrfs_submit_helper,
6680 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006681
6682 return dev;
6683}
6684
Anand Jain5a2b8e62017-10-09 11:07:45 +08006685static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006686 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006687{
Anand Jain2b902df2017-10-09 11:07:46 +08006688 if (error)
6689 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6690 devid, uuid);
6691 else
6692 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6693 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006694}
6695
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006696static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6697{
6698 int index = btrfs_bg_flags_to_raid_index(type);
6699 int ncopies = btrfs_raid_array[index].ncopies;
6700 int data_stripes;
6701
6702 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6703 case BTRFS_BLOCK_GROUP_RAID5:
6704 data_stripes = num_stripes - 1;
6705 break;
6706 case BTRFS_BLOCK_GROUP_RAID6:
6707 data_stripes = num_stripes - 2;
6708 break;
6709 default:
6710 data_stripes = num_stripes / ncopies;
6711 break;
6712 }
6713 return div_u64(chunk_len, data_stripes);
6714}
6715
David Sterba9690ac02019-03-20 16:43:07 +01006716static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006717 struct btrfs_chunk *chunk)
6718{
David Sterba9690ac02019-03-20 16:43:07 +01006719 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006720 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006721 struct map_lookup *map;
6722 struct extent_map *em;
6723 u64 logical;
6724 u64 length;
6725 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006726 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006727 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006728 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006729 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006730
Chris Masone17cade2008-04-15 15:41:47 -04006731 logical = key->offset;
6732 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006733 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006734
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006735 /*
6736 * Only need to verify chunk item if we're reading from sys chunk array,
6737 * as chunk item in tree block is already verified by tree-checker.
6738 */
6739 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006740 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006741 if (ret)
6742 return ret;
6743 }
Chris Masona061fc82008-05-07 11:43:44 -04006744
David Sterbac8bf1b62019-05-17 11:43:17 +02006745 read_lock(&map_tree->lock);
6746 em = lookup_extent_mapping(map_tree, logical, 1);
6747 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006748
6749 /* already mapped? */
6750 if (em && em->start <= logical && em->start + em->len > logical) {
6751 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006752 return 0;
6753 } else if (em) {
6754 free_extent_map(em);
6755 }
Chris Mason0b86a832008-03-24 15:01:56 -04006756
David Sterba172ddd62011-04-21 00:48:27 +02006757 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006758 if (!em)
6759 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006760 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006761 if (!map) {
6762 free_extent_map(em);
6763 return -ENOMEM;
6764 }
6765
Wang Shilong298a8f92014-06-19 10:42:52 +08006766 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006767 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006768 em->start = logical;
6769 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006770 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006771 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006772 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006773
Chris Mason593060d2008-03-25 16:50:33 -04006774 map->num_stripes = num_stripes;
6775 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6776 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006777 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6778 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006779 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006780 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006781 em->orig_block_len = calc_stripe_length(map->type, em->len,
6782 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006783 for (i = 0; i < num_stripes; i++) {
6784 map->stripes[i].physical =
6785 btrfs_stripe_offset_nr(leaf, chunk, i);
6786 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006787 read_extent_buffer(leaf, uuid, (unsigned long)
6788 btrfs_stripe_dev_uuid_nr(chunk, i),
6789 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006790 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006791 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006792 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006793 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006794 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006795 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006796 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006797 }
Chris Masondfe25022008-05-13 13:46:40 -04006798 if (!map->stripes[i].dev) {
6799 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006800 add_missing_dev(fs_info->fs_devices, devid,
6801 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006802 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006803 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006804 btrfs_err(fs_info,
6805 "failed to init missing dev %llu: %ld",
6806 devid, PTR_ERR(map->stripes[i].dev));
6807 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006808 }
Anand Jain2b902df2017-10-09 11:07:46 +08006809 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006810 }
Anand Jaine12c9622017-12-04 12:54:53 +08006811 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6812 &(map->stripes[i].dev->dev_state));
6813
Chris Mason0b86a832008-03-24 15:01:56 -04006814 }
6815
David Sterbac8bf1b62019-05-17 11:43:17 +02006816 write_lock(&map_tree->lock);
6817 ret = add_extent_mapping(map_tree, em, 0);
6818 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006819 if (ret < 0) {
6820 btrfs_err(fs_info,
6821 "failed to add chunk map, start=%llu len=%llu: %d",
6822 em->start, em->len, ret);
6823 }
Chris Mason0b86a832008-03-24 15:01:56 -04006824 free_extent_map(em);
6825
Qu Wenruo64f64f42018-08-01 10:37:20 +08006826 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006827}
6828
Jeff Mahoney143bede2012-03-01 14:56:26 +01006829static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006830 struct btrfs_dev_item *dev_item,
6831 struct btrfs_device *device)
6832{
6833 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006834
6835 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006836 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6837 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006838 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006839 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006840 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006841 device->type = btrfs_device_type(leaf, dev_item);
6842 device->io_align = btrfs_device_io_align(leaf, dev_item);
6843 device->io_width = btrfs_device_io_width(leaf, dev_item);
6844 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006845 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006846 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006847
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006848 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006849 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006850}
6851
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006852static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006853 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006854{
6855 struct btrfs_fs_devices *fs_devices;
6856 int ret;
6857
David Sterbaa32bf9a2018-03-16 02:21:22 +01006858 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006859 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006860
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006861 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006862 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006863 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006864 return fs_devices;
6865
Yan Zheng2b820322008-11-17 21:11:30 -05006866 fs_devices = fs_devices->seed;
6867 }
6868
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006869 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006870 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006871 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006872 return ERR_PTR(-ENOENT);
6873
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006874 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006875 if (IS_ERR(fs_devices))
6876 return fs_devices;
6877
6878 fs_devices->seeding = 1;
6879 fs_devices->opened = 1;
6880 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006881 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006882
6883 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006884 if (IS_ERR(fs_devices))
6885 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006886
Anand Jain897fb572018-04-12 10:29:28 +08006887 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006888 if (ret) {
6889 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006890 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006891 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006892 }
Yan Zheng2b820322008-11-17 21:11:30 -05006893
6894 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006895 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006896 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006897 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006898 goto out;
6899 }
6900
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006901 fs_devices->seed = fs_info->fs_devices->seed;
6902 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006903out:
Miao Xie5f375832014-09-03 21:35:46 +08006904 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006905}
6906
David Sterba17850752019-03-20 16:45:15 +01006907static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006908 struct btrfs_dev_item *dev_item)
6909{
David Sterba17850752019-03-20 16:45:15 +01006910 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006911 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006912 struct btrfs_device *device;
6913 u64 devid;
6914 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006915 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006916 u8 dev_uuid[BTRFS_UUID_SIZE];
6917
Chris Mason0b86a832008-03-24 15:01:56 -04006918 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006919 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006920 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006921 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006922 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006923
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006924 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006925 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006926 if (IS_ERR(fs_devices))
6927 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006928 }
6929
Anand Jaine4319cd2019-01-17 23:32:31 +08006930 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006931 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006932 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006933 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006934 btrfs_report_missing_device(fs_info, devid,
6935 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006936 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006937 }
Yan Zheng2b820322008-11-17 21:11:30 -05006938
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006939 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006940 if (IS_ERR(device)) {
6941 btrfs_err(fs_info,
6942 "failed to add missing dev %llu: %ld",
6943 devid, PTR_ERR(device));
6944 return PTR_ERR(device);
6945 }
Anand Jain2b902df2017-10-09 11:07:46 +08006946 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006947 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006948 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006949 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6950 btrfs_report_missing_device(fs_info,
6951 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006952 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006953 }
6954 btrfs_report_missing_device(fs_info, devid,
6955 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006956 }
Miao Xie5f375832014-09-03 21:35:46 +08006957
Anand Jaine6e674b2017-12-04 12:54:54 +08006958 if (!device->bdev &&
6959 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006960 /*
6961 * this happens when a device that was properly setup
6962 * in the device info lists suddenly goes bad.
6963 * device->bdev is NULL, and so we have to set
6964 * device->missing to one here
6965 */
Miao Xie5f375832014-09-03 21:35:46 +08006966 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006967 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006968 }
Miao Xie5f375832014-09-03 21:35:46 +08006969
6970 /* Move the device to its own fs_devices */
6971 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006972 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6973 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006974
6975 list_move(&device->dev_list, &fs_devices->devices);
6976 device->fs_devices->num_devices--;
6977 fs_devices->num_devices++;
6978
6979 device->fs_devices->missing_devices--;
6980 fs_devices->missing_devices++;
6981
6982 device->fs_devices = fs_devices;
6983 }
Yan Zheng2b820322008-11-17 21:11:30 -05006984 }
6985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006986 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006987 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006988 if (device->generation !=
6989 btrfs_device_generation(leaf, dev_item))
6990 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006991 }
Chris Mason0b86a832008-03-24 15:01:56 -04006992
6993 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006994 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006995 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006996 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006997 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006998 atomic64_add(device->total_bytes - device->bytes_used,
6999 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04007000 }
Chris Mason0b86a832008-03-24 15:01:56 -04007001 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007002 return ret;
7003}
7004
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007005int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007006{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007007 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007008 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04007009 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04007010 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04007011 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01007012 u8 *array_ptr;
7013 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04007014 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007015 u32 num_stripes;
7016 u32 array_size;
7017 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01007018 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07007019 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04007020 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04007021
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007022 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02007023 /*
7024 * This will create extent buffer of nodesize, superblock size is
7025 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7026 * overallocate but we can keep it as-is, only the first page is used.
7027 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007028 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07007029 if (IS_ERR(sb))
7030 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01007031 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04007032 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02007033 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04007034 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01007035 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02007036 * pages up-to-date when the page is larger: extent does not cover the
7037 * whole page and consequently check_page_uptodate does not find all
7038 * the page's extents up-to-date (the hole beyond sb),
7039 * write_extent_buffer then triggers a WARN_ON.
7040 *
7041 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7042 * but sb spans only this function. Add an explicit SetPageUptodate call
7043 * to silence the warning eg. on PowerPC 64.
7044 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007045 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007046 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007047
Chris Masona061fc82008-05-07 11:43:44 -04007048 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007049 array_size = btrfs_super_sys_array_size(super_copy);
7050
David Sterba1ffb22c2014-10-31 19:02:42 +01007051 array_ptr = super_copy->sys_chunk_array;
7052 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7053 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007054
David Sterba1ffb22c2014-10-31 19:02:42 +01007055 while (cur_offset < array_size) {
7056 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007057 len = sizeof(*disk_key);
7058 if (cur_offset + len > array_size)
7059 goto out_short_read;
7060
Chris Mason0b86a832008-03-24 15:01:56 -04007061 btrfs_disk_key_to_cpu(&key, disk_key);
7062
David Sterba1ffb22c2014-10-31 19:02:42 +01007063 array_ptr += len;
7064 sb_array_offset += len;
7065 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007066
Chris Mason0d81ba52008-03-24 15:02:07 -04007067 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01007068 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01007069 /*
7070 * At least one btrfs_chunk with one stripe must be
7071 * present, exact stripe count check comes afterwards
7072 */
7073 len = btrfs_chunk_item_size(1);
7074 if (cur_offset + len > array_size)
7075 goto out_short_read;
7076
7077 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01007078 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007079 btrfs_err(fs_info,
7080 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01007081 num_stripes, cur_offset);
7082 ret = -EIO;
7083 break;
7084 }
7085
Liu Boe06cd3d2016-06-03 12:05:15 -07007086 type = btrfs_chunk_type(sb, chunk);
7087 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007088 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07007089 "invalid chunk type %llu in sys_array at offset %u",
7090 type, cur_offset);
7091 ret = -EIO;
7092 break;
7093 }
7094
David Sterbae3540ea2014-11-05 15:24:51 +01007095 len = btrfs_chunk_item_size(num_stripes);
7096 if (cur_offset + len > array_size)
7097 goto out_short_read;
7098
David Sterba9690ac02019-03-20 16:43:07 +01007099 ret = read_one_chunk(&key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04007100 if (ret)
7101 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007102 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007103 btrfs_err(fs_info,
7104 "unexpected item type %u in sys_array at offset %u",
7105 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007106 ret = -EIO;
7107 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007108 }
David Sterba1ffb22c2014-10-31 19:02:42 +01007109 array_ptr += len;
7110 sb_array_offset += len;
7111 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007112 }
Liu Bod8651772016-06-03 17:41:42 -07007113 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007114 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007115 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007116
7117out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007118 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007119 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007120 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007121 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007122 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007123}
7124
Qu Wenruo21634a12017-03-09 09:34:36 +08007125/*
7126 * Check if all chunks in the fs are OK for read-write degraded mount
7127 *
Anand Jain6528b992017-12-18 17:08:59 +08007128 * If the @failing_dev is specified, it's accounted as missing.
7129 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007130 * Return true if all chunks meet the minimal RW mount requirements.
7131 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7132 */
Anand Jain6528b992017-12-18 17:08:59 +08007133bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7134 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007135{
David Sterbac8bf1b62019-05-17 11:43:17 +02007136 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08007137 struct extent_map *em;
7138 u64 next_start = 0;
7139 bool ret = true;
7140
David Sterbac8bf1b62019-05-17 11:43:17 +02007141 read_lock(&map_tree->lock);
7142 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7143 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007144 /* No chunk at all? Return false anyway */
7145 if (!em) {
7146 ret = false;
7147 goto out;
7148 }
7149 while (em) {
7150 struct map_lookup *map;
7151 int missing = 0;
7152 int max_tolerated;
7153 int i;
7154
7155 map = em->map_lookup;
7156 max_tolerated =
7157 btrfs_get_num_tolerated_disk_barrier_failures(
7158 map->type);
7159 for (i = 0; i < map->num_stripes; i++) {
7160 struct btrfs_device *dev = map->stripes[i].dev;
7161
Anand Jaine6e674b2017-12-04 12:54:54 +08007162 if (!dev || !dev->bdev ||
7163 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007164 dev->last_flush_error)
7165 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007166 else if (failing_dev && failing_dev == dev)
7167 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007168 }
7169 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007170 if (!failing_dev)
7171 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007172 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007173 em->start, missing, max_tolerated);
7174 free_extent_map(em);
7175 ret = false;
7176 goto out;
7177 }
7178 next_start = extent_map_end(em);
7179 free_extent_map(em);
7180
David Sterbac8bf1b62019-05-17 11:43:17 +02007181 read_lock(&map_tree->lock);
7182 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08007183 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02007184 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007185 }
7186out:
7187 return ret;
7188}
7189
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007190int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007191{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007192 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007193 struct btrfs_path *path;
7194 struct extent_buffer *leaf;
7195 struct btrfs_key key;
7196 struct btrfs_key found_key;
7197 int ret;
7198 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007199 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007200
Chris Mason0b86a832008-03-24 15:01:56 -04007201 path = btrfs_alloc_path();
7202 if (!path)
7203 return -ENOMEM;
7204
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007205 /*
7206 * uuid_mutex is needed only if we are mounting a sprout FS
7207 * otherwise we don't need it.
7208 */
Li Zefanb367e472011-12-07 11:38:24 +08007209 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007210 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007211
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007212 /*
7213 * Read all device items, and then all the chunk items. All
7214 * device items are found before any chunk item (their object id
7215 * is smaller than the lowest possible object id for a chunk
7216 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007217 */
7218 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7219 key.offset = 0;
7220 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007221 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007222 if (ret < 0)
7223 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007224 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007225 leaf = path->nodes[0];
7226 slot = path->slots[0];
7227 if (slot >= btrfs_header_nritems(leaf)) {
7228 ret = btrfs_next_leaf(root, path);
7229 if (ret == 0)
7230 continue;
7231 if (ret < 0)
7232 goto error;
7233 break;
7234 }
7235 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007236 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7237 struct btrfs_dev_item *dev_item;
7238 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007239 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01007240 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007241 if (ret)
7242 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007243 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007244 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7245 struct btrfs_chunk *chunk;
7246 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba9690ac02019-03-20 16:43:07 +01007247 ret = read_one_chunk(&found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007248 if (ret)
7249 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007250 }
7251 path->slots[0]++;
7252 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007253
7254 /*
7255 * After loading chunk tree, we've got all device information,
7256 * do another round of validation checks.
7257 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007258 if (total_dev != fs_info->fs_devices->total_devices) {
7259 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007260 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007261 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007262 total_dev);
7263 ret = -EINVAL;
7264 goto error;
7265 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007266 if (btrfs_super_total_bytes(fs_info->super_copy) <
7267 fs_info->fs_devices->total_rw_bytes) {
7268 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007269 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007270 btrfs_super_total_bytes(fs_info->super_copy),
7271 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007272 ret = -EINVAL;
7273 goto error;
7274 }
Chris Mason0b86a832008-03-24 15:01:56 -04007275 ret = 0;
7276error:
David Sterba34441362016-10-04 19:34:27 +02007277 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007278 mutex_unlock(&uuid_mutex);
7279
Yan Zheng2b820322008-11-17 21:11:30 -05007280 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007281 return ret;
7282}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007283
Miao Xiecb517ea2013-05-15 07:48:19 +00007284void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7285{
7286 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7287 struct btrfs_device *device;
7288
Liu Bo29cc83f2014-05-11 23:14:59 +08007289 while (fs_devices) {
7290 mutex_lock(&fs_devices->device_list_mutex);
7291 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007292 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007293 mutex_unlock(&fs_devices->device_list_mutex);
7294
7295 fs_devices = fs_devices->seed;
7296 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007297}
7298
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007299static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7300{
7301 int i;
7302
7303 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7304 btrfs_dev_stat_reset(dev, i);
7305}
7306
7307int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7308{
7309 struct btrfs_key key;
7310 struct btrfs_key found_key;
7311 struct btrfs_root *dev_root = fs_info->dev_root;
7312 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7313 struct extent_buffer *eb;
7314 int slot;
7315 int ret = 0;
7316 struct btrfs_device *device;
7317 struct btrfs_path *path = NULL;
7318 int i;
7319
7320 path = btrfs_alloc_path();
7321 if (!path) {
7322 ret = -ENOMEM;
7323 goto out;
7324 }
7325
7326 mutex_lock(&fs_devices->device_list_mutex);
7327 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7328 int item_size;
7329 struct btrfs_dev_stats_item *ptr;
7330
David Sterba242e2952016-01-25 17:51:31 +01007331 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7332 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007333 key.offset = device->devid;
7334 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7335 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007336 __btrfs_reset_dev_stats(device);
7337 device->dev_stats_valid = 1;
7338 btrfs_release_path(path);
7339 continue;
7340 }
7341 slot = path->slots[0];
7342 eb = path->nodes[0];
7343 btrfs_item_key_to_cpu(eb, &found_key, slot);
7344 item_size = btrfs_item_size_nr(eb, slot);
7345
7346 ptr = btrfs_item_ptr(eb, slot,
7347 struct btrfs_dev_stats_item);
7348
7349 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7350 if (item_size >= (1 + i) * sizeof(__le64))
7351 btrfs_dev_stat_set(device, i,
7352 btrfs_dev_stats_value(eb, ptr, i));
7353 else
7354 btrfs_dev_stat_reset(device, i);
7355 }
7356
7357 device->dev_stats_valid = 1;
7358 btrfs_dev_stat_print_on_load(device);
7359 btrfs_release_path(path);
7360 }
7361 mutex_unlock(&fs_devices->device_list_mutex);
7362
7363out:
7364 btrfs_free_path(path);
7365 return ret < 0 ? ret : 0;
7366}
7367
7368static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007369 struct btrfs_device *device)
7370{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007371 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007372 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007373 struct btrfs_path *path;
7374 struct btrfs_key key;
7375 struct extent_buffer *eb;
7376 struct btrfs_dev_stats_item *ptr;
7377 int ret;
7378 int i;
7379
David Sterba242e2952016-01-25 17:51:31 +01007380 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7381 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007382 key.offset = device->devid;
7383
7384 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007385 if (!path)
7386 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007387 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7388 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007389 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007390 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007391 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007392 goto out;
7393 }
7394
7395 if (ret == 0 &&
7396 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7397 /* need to delete old one and insert a new one */
7398 ret = btrfs_del_item(trans, dev_root, path);
7399 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007400 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007401 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007402 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007403 goto out;
7404 }
7405 ret = 1;
7406 }
7407
7408 if (ret == 1) {
7409 /* need to insert a new item */
7410 btrfs_release_path(path);
7411 ret = btrfs_insert_empty_item(trans, dev_root, path,
7412 &key, sizeof(*ptr));
7413 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007414 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007415 "insert dev_stats item for device %s failed %d",
7416 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007417 goto out;
7418 }
7419 }
7420
7421 eb = path->nodes[0];
7422 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7423 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7424 btrfs_set_dev_stats_value(eb, ptr, i,
7425 btrfs_dev_stat_read(device, i));
7426 btrfs_mark_buffer_dirty(eb);
7427
7428out:
7429 btrfs_free_path(path);
7430 return ret;
7431}
7432
7433/*
7434 * called from commit_transaction. Writes all changed device stats to disk.
7435 */
David Sterba196c9d82019-03-20 16:50:38 +01007436int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007437{
David Sterba196c9d82019-03-20 16:50:38 +01007438 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007439 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7440 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007441 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007442 int ret = 0;
7443
7444 mutex_lock(&fs_devices->device_list_mutex);
7445 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007446 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7447 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007448 continue;
7449
Nikolay Borisov9deae962017-10-24 13:47:37 +03007450
7451 /*
7452 * There is a LOAD-LOAD control dependency between the value of
7453 * dev_stats_ccnt and updating the on-disk values which requires
7454 * reading the in-memory counters. Such control dependencies
7455 * require explicit read memory barriers.
7456 *
7457 * This memory barriers pairs with smp_mb__before_atomic in
7458 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7459 * barrier implied by atomic_xchg in
7460 * btrfs_dev_stats_read_and_reset
7461 */
7462 smp_rmb();
7463
Nikolay Borisov5495f192018-07-20 19:37:49 +03007464 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007465 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007466 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007467 }
7468 mutex_unlock(&fs_devices->device_list_mutex);
7469
7470 return ret;
7471}
7472
Stefan Behrens442a4f62012-05-25 16:06:08 +02007473void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7474{
7475 btrfs_dev_stat_inc(dev, index);
7476 btrfs_dev_stat_print_on_error(dev);
7477}
7478
Eric Sandeen48a3b632013-04-25 20:41:01 +00007479static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007480{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007481 if (!dev->dev_stats_valid)
7482 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007483 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007484 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007485 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007486 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7487 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7488 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007489 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7490 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007491}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007492
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007493static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7494{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007495 int i;
7496
7497 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7498 if (btrfs_dev_stat_read(dev, i) != 0)
7499 break;
7500 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7501 return; /* all values == 0, suppress message */
7502
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007503 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007504 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007505 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007506 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7507 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7508 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7509 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7510 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7511}
7512
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007513int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007514 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007515{
7516 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007517 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007518 int i;
7519
7520 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007521 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7522 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007523 mutex_unlock(&fs_devices->device_list_mutex);
7524
7525 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007526 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007527 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007528 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007529 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007530 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007531 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007532 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7533 if (stats->nr_items > i)
7534 stats->values[i] =
7535 btrfs_dev_stat_read_and_reset(dev, i);
7536 else
7537 btrfs_dev_stat_reset(dev, i);
7538 }
7539 } else {
7540 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7541 if (stats->nr_items > i)
7542 stats->values[i] = btrfs_dev_stat_read(dev, i);
7543 }
7544 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7545 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7546 return 0;
7547}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007548
David Sterbada353f62017-02-14 17:55:53 +01007549void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007550{
7551 struct buffer_head *bh;
7552 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007553 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007554
Anand Jain12b1c262015-08-14 18:32:59 +08007555 if (!bdev)
7556 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007557
Anand Jain12b1c262015-08-14 18:32:59 +08007558 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7559 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007560
Anand Jain12b1c262015-08-14 18:32:59 +08007561 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7562 continue;
7563
7564 disk_super = (struct btrfs_super_block *)bh->b_data;
7565
7566 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7567 set_buffer_dirty(bh);
7568 sync_dirty_buffer(bh);
7569 brelse(bh);
7570 }
7571
7572 /* Notify udev that device has changed */
7573 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7574
7575 /* Update ctime/mtime for device path for libblkid */
7576 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007577}
Miao Xie935e5cc2014-09-03 21:35:33 +08007578
7579/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007580 * Update the size and bytes used for each device where it changed. This is
7581 * delayed since we would otherwise get errors while writing out the
7582 * superblocks.
7583 *
7584 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007585 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007586void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007587{
Miao Xie935e5cc2014-09-03 21:35:33 +08007588 struct btrfs_device *curr, *next;
7589
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007590 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7591
7592 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007593 return;
7594
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007595 /*
7596 * We don't need the device_list_mutex here. This list is owned by the
7597 * transaction and the transaction must complete before the device is
7598 * released.
7599 */
7600 mutex_lock(&trans->fs_info->chunk_mutex);
7601 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7602 post_commit_list) {
7603 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007604 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007605 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007606 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007607 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007608}
Anand Jain5a13f432015-03-10 06:38:31 +08007609
7610void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7611{
7612 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7613 while (fs_devices) {
7614 fs_devices->fs_info = fs_info;
7615 fs_devices = fs_devices->seed;
7616 }
7617}
7618
7619void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7620{
7621 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7622 while (fs_devices) {
7623 fs_devices->fs_info = NULL;
7624 fs_devices = fs_devices->seed;
7625 }
7626}
David Sterba46df06b2018-07-13 20:46:30 +02007627
7628/*
7629 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7630 */
7631int btrfs_bg_type_to_factor(u64 flags)
7632{
David Sterba44b28ad2019-05-17 11:43:31 +02007633 const int index = btrfs_bg_flags_to_raid_index(flags);
7634
7635 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007636}
Qu Wenruocf90d882018-08-01 10:37:19 +08007637
7638
Qu Wenruocf90d882018-08-01 10:37:19 +08007639
7640static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7641 u64 chunk_offset, u64 devid,
7642 u64 physical_offset, u64 physical_len)
7643{
David Sterbac8bf1b62019-05-17 11:43:17 +02007644 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007645 struct extent_map *em;
7646 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007647 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007648 u64 stripe_len;
7649 bool found = false;
7650 int ret = 0;
7651 int i;
7652
7653 read_lock(&em_tree->lock);
7654 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7655 read_unlock(&em_tree->lock);
7656
7657 if (!em) {
7658 btrfs_err(fs_info,
7659"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7660 physical_offset, devid);
7661 ret = -EUCLEAN;
7662 goto out;
7663 }
7664
7665 map = em->map_lookup;
7666 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7667 if (physical_len != stripe_len) {
7668 btrfs_err(fs_info,
7669"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7670 physical_offset, devid, em->start, physical_len,
7671 stripe_len);
7672 ret = -EUCLEAN;
7673 goto out;
7674 }
7675
7676 for (i = 0; i < map->num_stripes; i++) {
7677 if (map->stripes[i].dev->devid == devid &&
7678 map->stripes[i].physical == physical_offset) {
7679 found = true;
7680 if (map->verified_stripes >= map->num_stripes) {
7681 btrfs_err(fs_info,
7682 "too many dev extents for chunk %llu found",
7683 em->start);
7684 ret = -EUCLEAN;
7685 goto out;
7686 }
7687 map->verified_stripes++;
7688 break;
7689 }
7690 }
7691 if (!found) {
7692 btrfs_err(fs_info,
7693 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7694 physical_offset, devid);
7695 ret = -EUCLEAN;
7696 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007697
7698 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007699 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007700 if (!dev) {
7701 btrfs_err(fs_info, "failed to find devid %llu", devid);
7702 ret = -EUCLEAN;
7703 goto out;
7704 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007705
7706 /* It's possible this device is a dummy for seed device */
7707 if (dev->disk_total_bytes == 0) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08007708 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7709 NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007710 if (!dev) {
7711 btrfs_err(fs_info, "failed to find seed devid %llu",
7712 devid);
7713 ret = -EUCLEAN;
7714 goto out;
7715 }
7716 }
7717
Qu Wenruo05a37c42018-10-05 17:45:55 +08007718 if (physical_offset + physical_len > dev->disk_total_bytes) {
7719 btrfs_err(fs_info,
7720"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7721 devid, physical_offset, physical_len,
7722 dev->disk_total_bytes);
7723 ret = -EUCLEAN;
7724 goto out;
7725 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007726out:
7727 free_extent_map(em);
7728 return ret;
7729}
7730
7731static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7732{
David Sterbac8bf1b62019-05-17 11:43:17 +02007733 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007734 struct extent_map *em;
7735 struct rb_node *node;
7736 int ret = 0;
7737
7738 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007739 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007740 em = rb_entry(node, struct extent_map, rb_node);
7741 if (em->map_lookup->num_stripes !=
7742 em->map_lookup->verified_stripes) {
7743 btrfs_err(fs_info,
7744 "chunk %llu has missing dev extent, have %d expect %d",
7745 em->start, em->map_lookup->verified_stripes,
7746 em->map_lookup->num_stripes);
7747 ret = -EUCLEAN;
7748 goto out;
7749 }
7750 }
7751out:
7752 read_unlock(&em_tree->lock);
7753 return ret;
7754}
7755
7756/*
7757 * Ensure that all dev extents are mapped to correct chunk, otherwise
7758 * later chunk allocation/free would cause unexpected behavior.
7759 *
7760 * NOTE: This will iterate through the whole device tree, which should be of
7761 * the same size level as the chunk tree. This slightly increases mount time.
7762 */
7763int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7764{
7765 struct btrfs_path *path;
7766 struct btrfs_root *root = fs_info->dev_root;
7767 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007768 u64 prev_devid = 0;
7769 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007770 int ret = 0;
7771
7772 key.objectid = 1;
7773 key.type = BTRFS_DEV_EXTENT_KEY;
7774 key.offset = 0;
7775
7776 path = btrfs_alloc_path();
7777 if (!path)
7778 return -ENOMEM;
7779
7780 path->reada = READA_FORWARD;
7781 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7782 if (ret < 0)
7783 goto out;
7784
7785 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7786 ret = btrfs_next_item(root, path);
7787 if (ret < 0)
7788 goto out;
7789 /* No dev extents at all? Not good */
7790 if (ret > 0) {
7791 ret = -EUCLEAN;
7792 goto out;
7793 }
7794 }
7795 while (1) {
7796 struct extent_buffer *leaf = path->nodes[0];
7797 struct btrfs_dev_extent *dext;
7798 int slot = path->slots[0];
7799 u64 chunk_offset;
7800 u64 physical_offset;
7801 u64 physical_len;
7802 u64 devid;
7803
7804 btrfs_item_key_to_cpu(leaf, &key, slot);
7805 if (key.type != BTRFS_DEV_EXTENT_KEY)
7806 break;
7807 devid = key.objectid;
7808 physical_offset = key.offset;
7809
7810 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7811 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7812 physical_len = btrfs_dev_extent_length(leaf, dext);
7813
Qu Wenruo5eb19382018-10-05 17:45:54 +08007814 /* Check if this dev extent overlaps with the previous one */
7815 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7816 btrfs_err(fs_info,
7817"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7818 devid, physical_offset, prev_dev_ext_end);
7819 ret = -EUCLEAN;
7820 goto out;
7821 }
7822
Qu Wenruocf90d882018-08-01 10:37:19 +08007823 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7824 physical_offset, physical_len);
7825 if (ret < 0)
7826 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007827 prev_devid = devid;
7828 prev_dev_ext_end = physical_offset + physical_len;
7829
Qu Wenruocf90d882018-08-01 10:37:19 +08007830 ret = btrfs_next_item(root, path);
7831 if (ret < 0)
7832 goto out;
7833 if (ret > 0) {
7834 ret = 0;
7835 break;
7836 }
7837 }
7838
7839 /* Ensure all chunks have corresponding dev extents */
7840 ret = verify_chunk_dev_extent_mapping(fs_info);
7841out:
7842 btrfs_free_path(path);
7843 return ret;
7844}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007845
7846/*
7847 * Check whether the given block group or device is pinned by any inode being
7848 * used as a swapfile.
7849 */
7850bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7851{
7852 struct btrfs_swapfile_pin *sp;
7853 struct rb_node *node;
7854
7855 spin_lock(&fs_info->swapfile_pins_lock);
7856 node = fs_info->swapfile_pins.rb_node;
7857 while (node) {
7858 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7859 if (ptr < sp->ptr)
7860 node = node->rb_left;
7861 else if (ptr > sp->ptr)
7862 node = node->rb_right;
7863 else
7864 break;
7865 }
7866 spin_unlock(&fs_info->swapfile_pins_lock);
7867 return node != NULL;
7868}