blob: 912dd8b9f156c1ebb6a7565ec07e6b9603290cf5 [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>
Josef Bacikfccc0002020-08-31 10:52:42 -04007#include <linux/sched/mm.h>
Chris Mason0b86a832008-03-24 15:01:56 -04008#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.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>
David Sterba784352f2019-08-21 18:54:28 +020017#include "misc.h"
Chris Mason0b86a832008-03-24 15:01:56 -040018#include "ctree.h"
19#include "extent_map.h"
20#include "disk-io.h"
21#include "transaction.h"
22#include "print-tree.h"
23#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050024#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040025#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010026#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040027#include "rcu-string.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"
Josef Bacikaac00232019-06-20 15:37:44 -040032#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080033#include "discard.h"
Naohiro Aota5b316462020-11-10 20:26:07 +090034#include "zoned.h"
Chris Mason0b86a832008-03-24 15:01:56 -040035
Zhao Leiaf902042015-09-15 21:08:06 +080036const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
37 [BTRFS_RAID_RAID10] = {
38 .sub_stripes = 2,
39 .dev_stripes = 1,
40 .devs_max = 0, /* 0 == as many as possible */
41 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080042 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080043 .devs_increment = 2,
44 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020045 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080046 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080047 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080048 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080049 },
50 [BTRFS_RAID_RAID1] = {
51 .sub_stripes = 1,
52 .dev_stripes = 1,
53 .devs_max = 2,
54 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080055 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080056 .devs_increment = 2,
57 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020058 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080059 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080060 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080061 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080062 },
David Sterba47e6f742018-03-02 22:56:53 +010063 [BTRFS_RAID_RAID1C3] = {
64 .sub_stripes = 1,
65 .dev_stripes = 1,
David Sterbacf93e152019-11-27 16:10:54 +010066 .devs_max = 3,
David Sterba47e6f742018-03-02 22:56:53 +010067 .devs_min = 3,
68 .tolerated_failures = 2,
69 .devs_increment = 3,
70 .ncopies = 3,
David Sterbadb26a022019-11-25 15:34:48 +010071 .nparity = 0,
David Sterba47e6f742018-03-02 22:56:53 +010072 .raid_name = "raid1c3",
73 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
74 .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET,
75 },
David Sterba8d6fac02018-03-02 22:56:53 +010076 [BTRFS_RAID_RAID1C4] = {
77 .sub_stripes = 1,
78 .dev_stripes = 1,
David Sterbacf93e152019-11-27 16:10:54 +010079 .devs_max = 4,
David Sterba8d6fac02018-03-02 22:56:53 +010080 .devs_min = 4,
81 .tolerated_failures = 3,
82 .devs_increment = 4,
83 .ncopies = 4,
David Sterbadb26a022019-11-25 15:34:48 +010084 .nparity = 0,
David Sterba8d6fac02018-03-02 22:56:53 +010085 .raid_name = "raid1c4",
86 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
87 .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
88 },
Zhao Leiaf902042015-09-15 21:08:06 +080089 [BTRFS_RAID_DUP] = {
90 .sub_stripes = 1,
91 .dev_stripes = 2,
92 .devs_max = 1,
93 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080094 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080095 .devs_increment = 1,
96 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020097 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080098 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080099 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800100 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800101 },
102 [BTRFS_RAID_RAID0] = {
103 .sub_stripes = 1,
104 .dev_stripes = 1,
105 .devs_max = 0,
106 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800107 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800108 .devs_increment = 1,
109 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200110 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +0800111 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +0800112 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800113 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800114 },
115 [BTRFS_RAID_SINGLE] = {
116 .sub_stripes = 1,
117 .dev_stripes = 1,
118 .devs_max = 1,
119 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800120 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800121 .devs_increment = 1,
122 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200123 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +0800124 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +0800125 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800126 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800127 },
128 [BTRFS_RAID_RAID5] = {
129 .sub_stripes = 1,
130 .dev_stripes = 1,
131 .devs_max = 0,
132 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800133 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800134 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200135 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200136 .nparity = 1,
Anand Jained234672018-04-25 19:01:42 +0800137 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800138 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800139 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800140 },
141 [BTRFS_RAID_RAID6] = {
142 .sub_stripes = 1,
143 .dev_stripes = 1,
144 .devs_max = 0,
145 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800146 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800147 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200148 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200149 .nparity = 2,
Anand Jained234672018-04-25 19:01:42 +0800150 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800151 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800152 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800153 },
154};
155
David Sterba158da512019-05-17 11:43:41 +0200156const char *btrfs_bg_type_to_raid_name(u64 flags)
Anand Jained234672018-04-25 19:01:42 +0800157{
David Sterba158da512019-05-17 11:43:41 +0200158 const int index = btrfs_bg_flags_to_raid_index(flags);
159
160 if (index >= BTRFS_NR_RAID_TYPES)
Anand Jained234672018-04-25 19:01:42 +0800161 return NULL;
162
David Sterba158da512019-05-17 11:43:41 +0200163 return btrfs_raid_array[index].raid_name;
Anand Jained234672018-04-25 19:01:42 +0800164}
165
Anand Jainf89e09c2018-11-20 16:12:55 +0800166/*
167 * Fill @buf with textual description of @bg_flags, no more than @size_buf
168 * bytes including terminating null byte.
169 */
170void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
171{
172 int i;
173 int ret;
174 char *bp = buf;
175 u64 flags = bg_flags;
176 u32 size_bp = size_buf;
177
178 if (!flags) {
179 strcpy(bp, "NONE");
180 return;
181 }
182
183#define DESCRIBE_FLAG(flag, desc) \
184 do { \
185 if (flags & (flag)) { \
186 ret = snprintf(bp, size_bp, "%s|", (desc)); \
187 if (ret < 0 || ret >= size_bp) \
188 goto out_overflow; \
189 size_bp -= ret; \
190 bp += ret; \
191 flags &= ~(flag); \
192 } \
193 } while (0)
194
195 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
196 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
197 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
198
199 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
200 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
201 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
202 btrfs_raid_array[i].raid_name);
203#undef DESCRIBE_FLAG
204
205 if (flags) {
206 ret = snprintf(bp, size_bp, "0x%llx|", flags);
207 size_bp -= ret;
208 }
209
210 if (size_bp < size_buf)
211 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
212
213 /*
214 * The text is trimmed, it's up to the caller to provide sufficiently
215 * large buffer
216 */
217out_overflow:;
218}
219
David Sterba6f8e0fc2019-03-20 16:29:13 +0100220static int init_first_rw_device(struct btrfs_trans_handle *trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400221static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000222static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200223static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700224static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
225 enum btrfs_map_op op,
226 u64 logical, u64 *length,
227 struct btrfs_bio **bbio_ret,
228 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500229
David Sterba9c6b1c42017-06-16 22:30:00 +0200230/*
231 * Device locking
232 * ==============
233 *
234 * There are several mutexes that protect manipulation of devices and low-level
235 * structures like chunks but not block groups, extents or files
236 *
237 * uuid_mutex (global lock)
238 * ------------------------
239 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
240 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
241 * device) or requested by the device= mount option
242 *
243 * the mutex can be very coarse and can cover long-running operations
244 *
245 * protects: updates to fs_devices counters like missing devices, rw devices,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100246 * seeding, structure cloning, opening/closing devices at mount/umount time
David Sterba9c6b1c42017-06-16 22:30:00 +0200247 *
248 * global::fs_devs - add, remove, updates to the global list
249 *
Josef Bacik18c850f2020-07-17 15:12:27 -0400250 * does not protect: manipulation of the fs_devices::devices list in general
251 * but in mount context it could be used to exclude list modifications by eg.
252 * scan ioctl
David Sterba9c6b1c42017-06-16 22:30:00 +0200253 *
254 * btrfs_device::name - renames (write side), read is RCU
255 *
256 * fs_devices::device_list_mutex (per-fs, with RCU)
257 * ------------------------------------------------
258 * protects updates to fs_devices::devices, ie. adding and deleting
259 *
260 * simple list traversal with read-only actions can be done with RCU protection
261 *
262 * may be used to exclude some operations from running concurrently without any
263 * modifications to the list (see write_all_supers)
264 *
Josef Bacik18c850f2020-07-17 15:12:27 -0400265 * Is not required at mount and close times, because our device list is
266 * protected by the uuid_mutex at that point.
267 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200268 * balance_mutex
269 * -------------
270 * protects balance structures (status, state) and context accessed from
271 * several places (internally, ioctl)
272 *
273 * chunk_mutex
274 * -----------
275 * protects chunks, adding or removing during allocation, trim or when a new
Nikolay Borisov0b6f5d42019-05-09 18:11:11 +0300276 * device is added/removed. Additionally it also protects post_commit_list of
277 * individual devices, since they can be added to the transaction's
278 * post_commit_list only with chunk_mutex held.
David Sterba9c6b1c42017-06-16 22:30:00 +0200279 *
280 * cleaner_mutex
281 * -------------
282 * a big lock that is held by the cleaner thread and prevents running subvolume
283 * cleaning together with relocation or delayed iputs
284 *
285 *
286 * Lock nesting
287 * ============
288 *
289 * uuid_mutex
Anand Jainae3e7152020-05-14 01:42:45 +0800290 * device_list_mutex
291 * chunk_mutex
292 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800293 *
294 *
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500295 * Exclusive operations
296 * ====================
Anand Jain89595e82018-04-18 14:59:25 +0800297 *
298 * Maintains the exclusivity of the following operations that apply to the
299 * whole filesystem and cannot run in parallel.
300 *
301 * - Balance (*)
302 * - Device add
303 * - Device remove
304 * - Device replace (*)
305 * - Resize
306 *
307 * The device operations (as above) can be in one of the following states:
308 *
309 * - Running state
310 * - Paused state
311 * - Completed state
312 *
313 * Only device operations marked with (*) can go into the Paused state for the
314 * following reasons:
315 *
316 * - ioctl (only Balance can be Paused through ioctl)
317 * - filesystem remounted as read-only
318 * - filesystem unmounted and mounted as read-only
319 * - system power-cycle and filesystem mounted as read-only
320 * - filesystem or device errors leading to forced read-only
321 *
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500322 * The status of exclusive operation is set and cleared atomically.
323 * During the course of Paused state, fs_info::exclusive_operation remains set.
Anand Jain89595e82018-04-18 14:59:25 +0800324 * A device operation in Paused or Running state can be canceled or resumed
325 * either by ioctl (Balance only) or when remounted as read-write.
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500326 * The exclusive status is cleared when the device operation is canceled or
Anand Jain89595e82018-04-18 14:59:25 +0800327 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200328 */
329
Miao Xie67a2c452014-09-03 21:35:43 +0800330DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400331static LIST_HEAD(fs_uuids);
David Sterba4143cb82019-10-01 19:57:37 +0200332struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
Anand Jainc73eccf2015-03-10 06:38:30 +0800333{
334 return &fs_uuids;
335}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400336
David Sterba2dfeca92017-06-14 02:48:07 +0200337/*
338 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200339 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
340 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200341 *
342 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
343 * The returned struct is not linked onto any lists and can be destroyed with
344 * kfree() right away.
345 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200346static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
347 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300348{
349 struct btrfs_fs_devices *fs_devs;
350
David Sterba78f2c9e2016-02-11 14:25:38 +0100351 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300352 if (!fs_devs)
353 return ERR_PTR(-ENOMEM);
354
355 mutex_init(&fs_devs->device_list_mutex);
356
357 INIT_LIST_HEAD(&fs_devs->devices);
358 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800359 INIT_LIST_HEAD(&fs_devs->fs_list);
Nikolay Borisov944d3f92020-07-16 10:25:33 +0300360 INIT_LIST_HEAD(&fs_devs->seed_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300361 if (fsid)
362 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300363
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200364 if (metadata_fsid)
365 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
366 else if (fsid)
367 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
368
Ilya Dryomov2208a372013-08-12 14:33:03 +0300369 return fs_devs;
370}
371
David Sterbaa425f9d2018-03-20 15:47:33 +0100372void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100373{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200374 WARN_ON(!list_empty(&device->post_commit_list));
David Sterba48dae9c2017-10-30 18:10:25 +0100375 rcu_string_free(device->name);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200376 extent_io_tree_release(&device->alloc_state);
David Sterba48dae9c2017-10-30 18:10:25 +0100377 bio_put(device->flush_bio);
Naohiro Aota5b316462020-11-10 20:26:07 +0900378 btrfs_destroy_dev_zone_info(device);
David Sterba48dae9c2017-10-30 18:10:25 +0100379 kfree(device);
380}
381
Yan Zhenge4404d62008-12-12 10:03:26 -0500382static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
383{
384 struct btrfs_device *device;
385 WARN_ON(fs_devices->opened);
386 while (!list_empty(&fs_devices->devices)) {
387 device = list_entry(fs_devices->devices.next,
388 struct btrfs_device, dev_list);
389 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100390 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500391 }
392 kfree(fs_devices);
393}
394
David Sterbaffc5a372018-02-19 17:24:15 +0100395void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400396{
397 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400398
Yan Zheng2b820322008-11-17 21:11:30 -0500399 while (!list_empty(&fs_uuids)) {
400 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800401 struct btrfs_fs_devices, fs_list);
402 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500403 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400404 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400405}
406
David Sterba48dae9c2017-10-30 18:10:25 +0100407/*
408 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
409 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100410 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100411 */
Qu Wenruo154f7cb2020-08-20 15:42:46 +0800412static struct btrfs_device *__alloc_device(struct btrfs_fs_info *fs_info)
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300413{
414 struct btrfs_device *dev;
415
David Sterba78f2c9e2016-02-11 14:25:38 +0100416 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300417 if (!dev)
418 return ERR_PTR(-ENOMEM);
419
David Sterbae0ae9992017-06-06 17:06:06 +0200420 /*
421 * Preallocate a bio that's always going to be used for flushing device
422 * barriers and matches the device lifespan
423 */
Christoph Hellwig616c6a62021-01-26 15:52:32 +0100424 dev->flush_bio = bio_kmalloc(GFP_KERNEL, 0);
David Sterbae0ae9992017-06-06 17:06:06 +0200425 if (!dev->flush_bio) {
426 kfree(dev);
427 return ERR_PTR(-ENOMEM);
428 }
David Sterbae0ae9992017-06-06 17:06:06 +0200429
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300430 INIT_LIST_HEAD(&dev->dev_list);
431 INIT_LIST_HEAD(&dev->dev_alloc_list);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200432 INIT_LIST_HEAD(&dev->post_commit_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300433
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300434 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800435 atomic_set(&dev->dev_stats_ccnt, 0);
Su Yuec41ec452021-01-21 19:39:10 +0800436 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700437 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800438 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Qu Wenruo154f7cb2020-08-20 15:42:46 +0800439 extent_io_tree_init(fs_info, &dev->alloc_state,
440 IO_TREE_DEVICE_ALLOC_STATE, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300441
442 return dev;
443}
444
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200445static noinline struct btrfs_fs_devices *find_fsid(
446 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400447{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400448 struct btrfs_fs_devices *fs_devices;
449
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200450 ASSERT(fsid);
451
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200452 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800453 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200454 if (metadata_fsid) {
455 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
456 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
457 BTRFS_FSID_SIZE) == 0)
458 return fs_devices;
459 } else {
460 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
461 return fs_devices;
462 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400463 }
464 return NULL;
465}
466
Su Yuec6730a02020-01-10 14:11:33 +0200467static struct btrfs_fs_devices *find_fsid_with_metadata_uuid(
468 struct btrfs_super_block *disk_super)
469{
470
471 struct btrfs_fs_devices *fs_devices;
472
473 /*
474 * Handle scanned device having completed its fsid change but
475 * belonging to a fs_devices that was created by first scanning
476 * a device which didn't have its fsid/metadata_uuid changed
477 * at all and the CHANGING_FSID_V2 flag set.
478 */
479 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
480 if (fs_devices->fsid_change &&
481 memcmp(disk_super->metadata_uuid, fs_devices->fsid,
482 BTRFS_FSID_SIZE) == 0 &&
483 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
484 BTRFS_FSID_SIZE) == 0) {
485 return fs_devices;
486 }
487 }
488 /*
489 * Handle scanned device having completed its fsid change but
490 * belonging to a fs_devices that was created by a device that
491 * has an outdated pair of fsid/metadata_uuid and
492 * CHANGING_FSID_V2 flag set.
493 */
494 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
495 if (fs_devices->fsid_change &&
496 memcmp(fs_devices->metadata_uuid,
497 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
498 memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid,
499 BTRFS_FSID_SIZE) == 0) {
500 return fs_devices;
501 }
502 }
503
504 return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
505}
506
507
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100508static int
509btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
510 int flush, struct block_device **bdev,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900511 struct btrfs_super_block **disk_super)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100512{
513 int ret;
514
515 *bdev = blkdev_get_by_path(device_path, flags, holder);
516
517 if (IS_ERR(*bdev)) {
518 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100519 goto error;
520 }
521
522 if (flush)
523 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200524 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100525 if (ret) {
526 blkdev_put(*bdev, flags);
527 goto error;
528 }
529 invalidate_bdev(*bdev);
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900530 *disk_super = btrfs_read_dev_super(*bdev);
531 if (IS_ERR(*disk_super)) {
532 ret = PTR_ERR(*disk_super);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100533 blkdev_put(*bdev, flags);
534 goto error;
535 }
536
537 return 0;
538
539error:
540 *bdev = NULL;
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100541 return ret;
542}
543
Anand Jain70bc7082019-01-04 13:31:53 +0800544static bool device_path_matched(const char *path, struct btrfs_device *device)
545{
546 int found;
547
548 rcu_read_lock();
549 found = strcmp(rcu_str_deref(device->name), path);
550 rcu_read_unlock();
551
552 return found == 0;
553}
554
Anand Jaind8367db2018-01-18 22:00:37 +0800555/*
556 * Search and remove all stale (devices which are not mounted) devices.
557 * When both inputs are NULL, it will search and release all stale devices.
558 * path: Optional. When provided will it release all unmounted devices
559 * matching this path only.
560 * skip_dev: Optional. Will skip this device when searching for the stale
561 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800562 * Return: 0 for success or if @path is NULL.
563 * -EBUSY if @path is a mounted device.
564 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800565 */
Anand Jain70bc7082019-01-04 13:31:53 +0800566static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800567 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800568{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800569 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
570 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800571 int ret = 0;
572
573 if (path)
574 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800575
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800576 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800577
Anand Jain70bc7082019-01-04 13:31:53 +0800578 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800579 list_for_each_entry_safe(device, tmp_device,
580 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800581 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800582 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800583 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800584 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800585 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800586 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800587 if (fs_devices->opened) {
588 /* for an already deleted device return 0 */
589 if (path && ret != 0)
590 ret = -EBUSY;
591 break;
592 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800593
Anand Jain4fde46f2015-06-17 21:10:48 +0800594 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800595 fs_devices->num_devices--;
596 list_del(&device->dev_list);
597 btrfs_free_device(device);
598
Anand Jain70bc7082019-01-04 13:31:53 +0800599 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800600 }
601 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800602
Anand Jain7bcb8162018-05-29 17:23:20 +0800603 if (fs_devices->num_devices == 0) {
604 btrfs_sysfs_remove_fsid(fs_devices);
605 list_del(&fs_devices->fs_list);
606 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800607 }
608 }
Anand Jain70bc7082019-01-04 13:31:53 +0800609
610 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800611}
612
Josef Bacik18c850f2020-07-17 15:12:27 -0400613/*
614 * This is only used on mount, and we are protected from competing things
615 * messing with our fs_devices by the uuid_mutex, thus we do not need the
616 * fs_devices->device_list_mutex here.
617 */
Anand Jain0fb08bc2017-11-09 23:45:24 +0800618static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
619 struct btrfs_device *device, fmode_t flags,
620 void *holder)
621{
622 struct request_queue *q;
623 struct block_device *bdev;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800624 struct btrfs_super_block *disk_super;
625 u64 devid;
626 int ret;
627
628 if (device->bdev)
629 return -EINVAL;
630 if (!device->name)
631 return -EINVAL;
632
633 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900634 &bdev, &disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800635 if (ret)
636 return ret;
637
Anand Jain0fb08bc2017-11-09 23:45:24 +0800638 devid = btrfs_stack_device_id(&disk_super->dev_item);
639 if (devid != device->devid)
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900640 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800641
642 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900643 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800644
645 device->generation = btrfs_super_generation(disk_super);
646
647 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200648 if (btrfs_super_incompat_flags(disk_super) &
649 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
650 pr_err(
651 "BTRFS: Invalid seeding and uuid-changed device detected\n");
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900652 goto error_free_page;
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200653 }
654
Anand Jainebbede42017-12-04 12:54:52 +0800655 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Johannes Thumshirn0395d842019-11-13 11:27:27 +0100656 fs_devices->seeding = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800657 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800658 if (bdev_read_only(bdev))
659 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
660 else
661 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800662 }
663
664 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800665 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +0100666 fs_devices->rotating = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800667
668 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800669 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800670 device->mode = flags;
671
672 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800673 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
674 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800675 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800676 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800677 }
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900678 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800679
680 return 0;
681
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900682error_free_page:
683 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800684 blkdev_put(bdev, flags);
685
686 return -EINVAL;
687}
688
David Sterba60999ca2014-03-26 18:26:36 +0100689/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200690 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
Su Yuec0d81c72020-01-10 14:11:32 +0200691 * being created with a disk that has already completed its fsid change. Such
692 * disk can belong to an fs which has its FSID changed or to one which doesn't.
693 * Handle both cases here.
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200694 */
695static struct btrfs_fs_devices *find_fsid_inprogress(
696 struct btrfs_super_block *disk_super)
697{
698 struct btrfs_fs_devices *fs_devices;
699
700 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
701 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
702 BTRFS_FSID_SIZE) != 0 &&
703 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
704 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
705 return fs_devices;
706 }
707 }
708
Su Yuec0d81c72020-01-10 14:11:32 +0200709 return find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200710}
711
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200712
713static struct btrfs_fs_devices *find_fsid_changed(
714 struct btrfs_super_block *disk_super)
715{
716 struct btrfs_fs_devices *fs_devices;
717
718 /*
719 * Handles the case where scanned device is part of an fs that had
720 * multiple successful changes of FSID but curently device didn't
Nikolay Borisov05840712020-01-10 14:11:34 +0200721 * observe it. Meaning our fsid will be different than theirs. We need
722 * to handle two subcases :
723 * 1 - The fs still continues to have different METADATA/FSID uuids.
724 * 2 - The fs is switched back to its original FSID (METADATA/FSID
725 * are equal).
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200726 */
727 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov05840712020-01-10 14:11:34 +0200728 /* Changed UUIDs */
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200729 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
730 BTRFS_FSID_SIZE) != 0 &&
731 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
732 BTRFS_FSID_SIZE) == 0 &&
733 memcmp(fs_devices->fsid, disk_super->fsid,
Nikolay Borisov05840712020-01-10 14:11:34 +0200734 BTRFS_FSID_SIZE) != 0)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200735 return fs_devices;
Nikolay Borisov05840712020-01-10 14:11:34 +0200736
737 /* Unchanged UUIDs */
738 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
739 BTRFS_FSID_SIZE) == 0 &&
740 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
741 BTRFS_FSID_SIZE) == 0)
742 return fs_devices;
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200743 }
744
745 return NULL;
746}
Nikolay Borisov13620892020-01-10 14:11:35 +0200747
748static struct btrfs_fs_devices *find_fsid_reverted_metadata(
749 struct btrfs_super_block *disk_super)
750{
751 struct btrfs_fs_devices *fs_devices;
752
753 /*
754 * Handle the case where the scanned device is part of an fs whose last
755 * metadata UUID change reverted it to the original FSID. At the same
756 * time * fs_devices was first created by another constitutent device
757 * which didn't fully observe the operation. This results in an
758 * btrfs_fs_devices created with metadata/fsid different AND
759 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
760 * fs_devices equal to the FSID of the disk.
761 */
762 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
763 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
764 BTRFS_FSID_SIZE) != 0 &&
765 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
766 BTRFS_FSID_SIZE) == 0 &&
767 fs_devices->fsid_change)
768 return fs_devices;
769 }
770
771 return NULL;
772}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200773/*
David Sterba60999ca2014-03-26 18:26:36 +0100774 * Add new device to list of registered devices
775 *
776 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800777 * device pointer which was just added or updated when successful
778 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100779 */
Anand Jaine124ece2018-01-18 22:02:35 +0800780static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800781 struct btrfs_super_block *disk_super,
782 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400783{
784 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200785 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400786 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400787 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800788 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200789 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
790 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200791 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
792 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400793
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200794 if (fsid_change_in_progress) {
Su Yuec0d81c72020-01-10 14:11:32 +0200795 if (!has_metadata_uuid)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200796 fs_devices = find_fsid_inprogress(disk_super);
Su Yuec0d81c72020-01-10 14:11:32 +0200797 else
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200798 fs_devices = find_fsid_changed(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200799 } else if (has_metadata_uuid) {
Su Yuec6730a02020-01-10 14:11:33 +0200800 fs_devices = find_fsid_with_metadata_uuid(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200801 } else {
Nikolay Borisov13620892020-01-10 14:11:35 +0200802 fs_devices = find_fsid_reverted_metadata(disk_super);
803 if (!fs_devices)
804 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200805 }
806
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200807
Chris Mason8a4b83c2008-03-24 15:02:07 -0400808 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200809 if (has_metadata_uuid)
810 fs_devices = alloc_fs_devices(disk_super->fsid,
811 disk_super->metadata_uuid);
812 else
813 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
814
Ilya Dryomov2208a372013-08-12 14:33:03 +0300815 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800816 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300817
Al Viro92900e52019-01-27 04:58:00 +0000818 fs_devices->fsid_change = fsid_change_in_progress;
819
Anand Jain9c6d1732018-05-29 14:10:20 +0800820 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800821 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300822
Chris Mason8a4b83c2008-03-24 15:02:07 -0400823 device = NULL;
824 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800825 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800826 device = btrfs_find_device(fs_devices, devid,
Anand Jainb2598ed2020-11-03 13:49:43 +0800827 disk_super->dev_item.uuid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200828
829 /*
830 * If this disk has been pulled into an fs devices created by
831 * a device which had the CHANGING_FSID_V2 flag then replace the
832 * metadata_uuid/fsid values of the fs_devices.
833 */
Nikolay Borisov13620892020-01-10 14:11:35 +0200834 if (fs_devices->fsid_change &&
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200835 found_transid > fs_devices->latest_generation) {
836 memcpy(fs_devices->fsid, disk_super->fsid,
837 BTRFS_FSID_SIZE);
Nikolay Borisov13620892020-01-10 14:11:35 +0200838
839 if (has_metadata_uuid)
840 memcpy(fs_devices->metadata_uuid,
841 disk_super->metadata_uuid,
842 BTRFS_FSID_SIZE);
843 else
844 memcpy(fs_devices->metadata_uuid,
845 disk_super->fsid, BTRFS_FSID_SIZE);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200846
847 fs_devices->fsid_change = false;
848 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849 }
Miao Xie443f24f2014-07-24 11:37:15 +0800850
Chris Mason8a4b83c2008-03-24 15:02:07 -0400851 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800852 if (fs_devices->opened) {
853 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800854 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800855 }
Yan Zheng2b820322008-11-17 21:11:30 -0500856
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300857 device = btrfs_alloc_device(NULL, &devid,
858 disk_super->dev_item.uuid);
859 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800860 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400861 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800862 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400863 }
Josef Bacik606686e2012-06-04 14:03:51 -0400864
865 name = rcu_string_strdup(path, GFP_NOFS);
866 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100867 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800868 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800869 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400870 }
Josef Bacik606686e2012-06-04 14:03:51 -0400871 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200872
Xiao Guangrong1f781602011-04-20 10:09:16 +0000873 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100874 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400875
Yan Zheng2b820322008-11-17 21:11:30 -0500876 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800877 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800878
879 if (disk_super->label[0])
Anand Jainaa6c0df2019-10-02 18:30:48 +0800880 pr_info(
881 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
882 disk_super->label, devid, found_transid, path,
883 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800884 else
Anand Jainaa6c0df2019-10-02 18:30:48 +0800885 pr_info(
886 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
887 disk_super->fsid, devid, found_transid, path,
888 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800889
Josef Bacik606686e2012-06-04 14:03:51 -0400890 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800891 /*
892 * When FS is already mounted.
893 * 1. If you are here and if the device->name is NULL that
894 * means this device was missing at time of FS mount.
895 * 2. If you are here and if the device->name is different
896 * from 'path' that means either
897 * a. The same device disappeared and reappeared with
898 * different name. or
899 * b. The missing-disk-which-was-replaced, has
900 * reappeared now.
901 *
902 * We must allow 1 and 2a above. But 2b would be a spurious
903 * and unintentional.
904 *
905 * Further in case of 1 and 2a above, the disk at 'path'
906 * would have missed some transaction when it was away and
907 * in case of 2a the stale bdev has to be updated as well.
908 * 2b must not be allowed at all time.
909 */
910
911 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700912 * For now, we do allow update to btrfs_fs_device through the
913 * btrfs dev scan cli after FS has been mounted. We're still
914 * tracking a problem where systems fail mount by subvolume id
915 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800916 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700917 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800918 /*
919 * That is if the FS is _not_ mounted and if you
920 * are here, that means there is more than one
921 * disk with same uuid and devid.We keep the one
922 * with larger generation number or the last-in if
923 * generation are equal.
924 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800925 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800926 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800927 }
Anand Jainb96de002014-07-03 18:22:05 +0800928
Anand Jaina9261d42018-10-15 10:45:17 +0800929 /*
930 * We are going to replace the device path for a given devid,
931 * make sure it's the same device if the device is mounted
932 */
933 if (device->bdev) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100934 int error;
935 dev_t path_dev;
Anand Jaina9261d42018-10-15 10:45:17 +0800936
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100937 error = lookup_bdev(path, &path_dev);
938 if (error) {
Anand Jaina9261d42018-10-15 10:45:17 +0800939 mutex_unlock(&fs_devices->device_list_mutex);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100940 return ERR_PTR(error);
Anand Jaina9261d42018-10-15 10:45:17 +0800941 }
942
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100943 if (device->bdev->bd_dev != path_dev) {
Anand Jaina9261d42018-10-15 10:45:17 +0800944 mutex_unlock(&fs_devices->device_list_mutex);
Johannes Thumshirn0697d9a2020-11-18 18:03:26 +0900945 /*
946 * device->fs_info may not be reliable here, so
947 * pass in a NULL instead. This avoids a
948 * possible use-after-free when the fs_info and
949 * fs_info->sb are already torn down.
950 */
951 btrfs_warn_in_rcu(NULL,
Anand Jain79dae172020-09-03 21:30:12 +0800952 "duplicate device %s devid %llu generation %llu scanned by %s (%d)",
953 path, devid, found_transid,
954 current->comm,
955 task_pid_nr(current));
Anand Jaina9261d42018-10-15 10:45:17 +0800956 return ERR_PTR(-EEXIST);
957 }
Anand Jaina9261d42018-10-15 10:45:17 +0800958 btrfs_info_in_rcu(device->fs_info,
Anand Jain79dae172020-09-03 21:30:12 +0800959 "devid %llu device path %s changed to %s scanned by %s (%d)",
960 devid, rcu_str_deref(device->name),
961 path, current->comm,
962 task_pid_nr(current));
Anand Jaina9261d42018-10-15 10:45:17 +0800963 }
964
Josef Bacik606686e2012-06-04 14:03:51 -0400965 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800966 if (!name) {
967 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800968 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800969 }
Josef Bacik606686e2012-06-04 14:03:51 -0400970 rcu_string_free(device->name);
971 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800972 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500973 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800974 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500975 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400976 }
977
Anand Jain77bdae42014-07-03 18:22:06 +0800978 /*
979 * Unmount does not free the btrfs_device struct but would zero
980 * generation along with most of the other members. So just update
981 * it back. We need it to pick the disk with largest generation
982 * (as above).
983 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200984 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +0800985 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200986 fs_devices->latest_generation = max_t(u64, found_transid,
987 fs_devices->latest_generation);
988 }
Anand Jain77bdae42014-07-03 18:22:06 +0800989
Anand Jainf2788d22018-01-18 22:02:34 +0800990 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
991
Anand Jain9c6d1732018-05-29 14:10:20 +0800992 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800993 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400994}
995
Yan Zhenge4404d62008-12-12 10:03:26 -0500996static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
997{
998 struct btrfs_fs_devices *fs_devices;
999 struct btrfs_device *device;
1000 struct btrfs_device *orig_dev;
Anand Jaind2979aa2019-08-27 15:40:45 +08001001 int ret = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001002
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001003 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001004 if (IS_ERR(fs_devices))
1005 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001006
Miao Xieadbbb862014-09-03 21:35:42 +08001007 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001008 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001009
1010 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001011 struct rcu_string *name;
1012
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001013 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1014 orig_dev->uuid);
Anand Jaind2979aa2019-08-27 15:40:45 +08001015 if (IS_ERR(device)) {
1016 ret = PTR_ERR(device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001017 goto error;
Anand Jaind2979aa2019-08-27 15:40:45 +08001018 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001019
Josef Bacik606686e2012-06-04 14:03:51 -04001020 /*
1021 * This is ok to do without rcu read locked because we hold the
1022 * uuid mutex so nothing we touch in here is going to disappear.
1023 */
Anand Jaine755f782014-06-30 17:12:47 +08001024 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001025 name = rcu_string_strdup(orig_dev->name->str,
1026 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001027 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001028 btrfs_free_device(device);
Anand Jaind2979aa2019-08-27 15:40:45 +08001029 ret = -ENOMEM;
Anand Jaine755f782014-06-30 17:12:47 +08001030 goto error;
1031 }
1032 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001033 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001034
Yan Zhenge4404d62008-12-12 10:03:26 -05001035 list_add(&device->dev_list, &fs_devices->devices);
1036 device->fs_devices = fs_devices;
1037 fs_devices->num_devices++;
1038 }
Miao Xieadbbb862014-09-03 21:35:42 +08001039 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001040 return fs_devices;
1041error:
Miao Xieadbbb862014-09-03 21:35:42 +08001042 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001043 free_fs_devices(fs_devices);
Anand Jaind2979aa2019-08-27 15:40:45 +08001044 return ERR_PTR(ret);
Yan Zhenge4404d62008-12-12 10:03:26 -05001045}
1046
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001047static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,
Anand Jainbacce862020-11-06 16:06:33 +08001048 struct btrfs_device **latest_dev)
Chris Masondfe25022008-05-13 13:46:40 -04001049{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001050 struct btrfs_device *device, *next;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001051
Xiao Guangrong46224702011-04-20 10:08:47 +00001052 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001053 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001054 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001055 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001056 &device->dev_state) &&
Anand Jain998a0672020-05-05 02:58:25 +08001057 !test_bit(BTRFS_DEV_STATE_MISSING,
1058 &device->dev_state) &&
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001059 (!*latest_dev ||
1060 device->generation > (*latest_dev)->generation)) {
1061 *latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001062 }
Yan Zheng2b820322008-11-17 21:11:30 -05001063 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001064 }
Yan Zheng2b820322008-11-17 21:11:30 -05001065
Anand Jaincf89af12020-10-30 06:53:56 +08001066 /*
1067 * We have already validated the presence of BTRFS_DEV_REPLACE_DEVID,
1068 * in btrfs_init_dev_replace() so just continue.
1069 */
1070 if (device->devid == BTRFS_DEV_REPLACE_DEVID)
1071 continue;
1072
Yan Zheng2b820322008-11-17 21:11:30 -05001073 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001074 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001075 device->bdev = NULL;
1076 fs_devices->open_devices--;
1077 }
Anand Jainebbede42017-12-04 12:54:52 +08001078 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001079 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001080 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05001081 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001082 list_del_init(&device->dev_list);
1083 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001084 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001085 }
Yan Zheng2b820322008-11-17 21:11:30 -05001086
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001087}
1088
1089/*
1090 * After we have read the system tree and know devids belonging to this
1091 * filesystem, remove the device which does not belong there.
1092 */
Anand Jainbacce862020-11-06 16:06:33 +08001093void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices)
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001094{
1095 struct btrfs_device *latest_dev = NULL;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001096 struct btrfs_fs_devices *seed_dev;
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001097
1098 mutex_lock(&uuid_mutex);
Anand Jainbacce862020-11-06 16:06:33 +08001099 __btrfs_free_extra_devids(fs_devices, &latest_dev);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001100
1101 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list)
Anand Jainbacce862020-11-06 16:06:33 +08001102 __btrfs_free_extra_devids(seed_dev, &latest_dev);
Yan Zheng2b820322008-11-17 21:11:30 -05001103
Miao Xie443f24f2014-07-24 11:37:15 +08001104 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001105
Chris Masondfe25022008-05-13 13:46:40 -04001106 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001107}
Chris Masona0af4692008-05-13 16:03:06 -04001108
Anand Jain14238812016-07-22 06:04:53 +08001109static void btrfs_close_bdev(struct btrfs_device *device)
1110{
David Sterba08ffcae2017-06-19 16:55:35 +02001111 if (!device->bdev)
1112 return;
1113
Anand Jainebbede42017-12-04 12:54:52 +08001114 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001115 sync_blockdev(device->bdev);
1116 invalidate_bdev(device->bdev);
1117 }
1118
David Sterba08ffcae2017-06-19 16:55:35 +02001119 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001120}
1121
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001122static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001123{
1124 struct btrfs_fs_devices *fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001125
Anand Jainebbede42017-12-04 12:54:52 +08001126 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001127 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1128 list_del_init(&device->dev_alloc_list);
1129 fs_devices->rw_devices--;
1130 }
1131
Anand Jaine6e674b2017-12-04 12:54:54 +08001132 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001133 fs_devices->missing_devices--;
1134
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001135 btrfs_close_bdev(device);
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001136 if (device->bdev) {
Johannes Thumshirn3fff3972019-11-26 09:40:05 +01001137 fs_devices->open_devices--;
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001138 device->bdev = NULL;
Anand Jainf448341a2016-06-14 18:55:25 +08001139 }
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001140 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Naohiro Aota5b316462020-11-10 20:26:07 +09001141 btrfs_destroy_dev_zone_info(device);
Anand Jainf448341a2016-06-14 18:55:25 +08001142
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001143 device->fs_info = NULL;
1144 atomic_set(&device->dev_stats_ccnt, 0);
1145 extent_io_tree_release(&device->alloc_state);
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001146
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001147 /* Verify the device is back in a pristine state */
1148 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1149 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1150 ASSERT(list_empty(&device->dev_alloc_list));
1151 ASSERT(list_empty(&device->post_commit_list));
1152 ASSERT(atomic_read(&device->reada_in_flight) == 0);
Anand Jainf448341a2016-06-14 18:55:25 +08001153}
1154
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001155static void close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001156{
Sasha Levin2037a092015-05-12 19:31:37 -04001157 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001158
Josef Bacik425c6ed2020-08-10 11:42:28 -04001159 lockdep_assert_held(&uuid_mutex);
1160
Yan Zheng2b820322008-11-17 21:11:30 -05001161 if (--fs_devices->opened > 0)
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001162 return;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001163
Josef Bacik425c6ed2020-08-10 11:42:28 -04001164 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list)
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001165 btrfs_close_one_device(device);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001166
Yan Zhenge4404d62008-12-12 10:03:26 -05001167 WARN_ON(fs_devices->open_devices);
1168 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001169 fs_devices->opened = 0;
Johannes Thumshirn0395d842019-11-13 11:27:27 +01001170 fs_devices->seeding = false;
Nikolay Borisovc4989c2f2020-07-15 13:48:49 +03001171 fs_devices->fs_info = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001172}
1173
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001174void btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001175{
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001176 LIST_HEAD(list);
1177 struct btrfs_fs_devices *tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05001178
1179 mutex_lock(&uuid_mutex);
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001180 close_fs_devices(fs_devices);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001181 if (!fs_devices->opened)
1182 list_splice_init(&fs_devices->seed_list, &list);
Yan Zhenge4404d62008-12-12 10:03:26 -05001183
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001184 list_for_each_entry_safe(fs_devices, tmp, &list, seed_list) {
Anand Jain0226e0e2018-04-12 10:29:27 +08001185 close_fs_devices(fs_devices);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001186 list_del(&fs_devices->seed_list);
Yan Zhenge4404d62008-12-12 10:03:26 -05001187 free_fs_devices(fs_devices);
1188 }
Josef Bacik425c6ed2020-08-10 11:42:28 -04001189 mutex_unlock(&uuid_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001190}
1191
Anand Jain897fb572018-04-12 10:29:28 +08001192static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001193 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001194{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001195 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001196 struct btrfs_device *latest_dev = NULL;
Anand Jain96c2e062020-09-30 21:09:52 +08001197 struct btrfs_device *tmp_device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001198
Tejun Heod4d77622010-11-13 11:55:18 +01001199 flags |= FMODE_EXCL;
1200
Anand Jain96c2e062020-09-30 21:09:52 +08001201 list_for_each_entry_safe(device, tmp_device, &fs_devices->devices,
1202 dev_list) {
1203 int ret;
Chris Masona0af4692008-05-13 16:03:06 -04001204
Anand Jain96c2e062020-09-30 21:09:52 +08001205 ret = btrfs_open_one_device(fs_devices, device, flags, holder);
1206 if (ret == 0 &&
1207 (!latest_dev || device->generation > latest_dev->generation)) {
Anand Jain9f050db2017-11-09 23:45:25 +08001208 latest_dev = device;
Anand Jain96c2e062020-09-30 21:09:52 +08001209 } else if (ret == -ENODATA) {
1210 fs_devices->num_devices--;
1211 list_del(&device->dev_list);
1212 btrfs_free_device(device);
1213 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001214 }
Anand Jain1ed802c2020-04-28 23:22:25 +08001215 if (fs_devices->open_devices == 0)
1216 return -EINVAL;
1217
Yan Zheng2b820322008-11-17 21:11:30 -05001218 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001219 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001220 fs_devices->total_rw_bytes = 0;
Naohiro Aotac4a816c2020-02-25 12:56:08 +09001221 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
Anand Jain33fd2f72020-10-28 21:14:46 +08001222 fs_devices->read_policy = BTRFS_READ_POLICY_PID;
Anand Jain1ed802c2020-04-28 23:22:25 +08001223
1224 return 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001225}
1226
Sami Tolvanen4f0f5862021-04-08 11:28:34 -07001227static int devid_cmp(void *priv, const struct list_head *a,
1228 const struct list_head *b)
Anand Jainf8e10cd2018-01-22 14:49:36 -08001229{
1230 struct btrfs_device *dev1, *dev2;
1231
1232 dev1 = list_entry(a, struct btrfs_device, dev_list);
1233 dev2 = list_entry(b, struct btrfs_device, dev_list);
1234
1235 if (dev1->devid < dev2->devid)
1236 return -1;
1237 else if (dev1->devid > dev2->devid)
1238 return 1;
1239 return 0;
1240}
1241
Yan Zheng2b820322008-11-17 21:11:30 -05001242int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001243 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001244{
1245 int ret;
1246
David Sterbaf5194e32018-06-19 17:09:47 +02001247 lockdep_assert_held(&uuid_mutex);
Josef Bacik18c850f2020-07-17 15:12:27 -04001248 /*
1249 * The device_list_mutex cannot be taken here in case opening the
1250 * underlying device takes further locks like bd_mutex.
1251 *
1252 * We also don't need the lock here as this is called during mount and
1253 * exclusion is provided by uuid_mutex
1254 */
David Sterbaf5194e32018-06-19 17:09:47 +02001255
Yan Zheng2b820322008-11-17 21:11:30 -05001256 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001257 fs_devices->opened++;
1258 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001259 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001260 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001261 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001262 }
Anand Jain542c5902018-04-12 10:29:34 +08001263
Chris Mason8a4b83c2008-03-24 15:02:07 -04001264 return ret;
1265}
1266
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001267void btrfs_release_disk_super(struct btrfs_super_block *super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001268{
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001269 struct page *page = virt_to_page(super);
1270
Anand Jain6cf86a002016-02-13 10:01:29 +08001271 put_page(page);
1272}
1273
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001274static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
Naohiro Aota12659252020-11-10 20:26:14 +09001275 u64 bytenr, u64 bytenr_orig)
Anand Jain6cf86a002016-02-13 10:01:29 +08001276{
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001277 struct btrfs_super_block *disk_super;
1278 struct page *page;
Anand Jain6cf86a002016-02-13 10:01:29 +08001279 void *p;
1280 pgoff_t index;
1281
1282 /* make sure our super fits in the device */
1283 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001284 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001285
1286 /* make sure our super fits in the page */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001287 if (sizeof(*disk_super) > PAGE_SIZE)
1288 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001289
1290 /* make sure our super doesn't straddle pages on disk */
1291 index = bytenr >> PAGE_SHIFT;
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001292 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
1293 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001294
1295 /* pull in the page with our super */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001296 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001297
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001298 if (IS_ERR(page))
1299 return ERR_CAST(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001300
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001301 p = page_address(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001302
1303 /* align our pointer to the offset of the super block */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001304 disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001305
Naohiro Aota12659252020-11-10 20:26:14 +09001306 if (btrfs_super_bytenr(disk_super) != bytenr_orig ||
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001307 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001308 btrfs_release_disk_super(p);
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001309 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001310 }
1311
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001312 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1])
1313 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0;
Anand Jain6cf86a002016-02-13 10:01:29 +08001314
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001315 return disk_super;
Anand Jain6cf86a002016-02-13 10:01:29 +08001316}
1317
Anand Jain228a73a2019-01-04 13:31:54 +08001318int btrfs_forget_devices(const char *path)
1319{
1320 int ret;
1321
1322 mutex_lock(&uuid_mutex);
1323 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1324 mutex_unlock(&uuid_mutex);
1325
1326 return ret;
1327}
1328
David Sterba6f60cbd2013-02-15 11:31:02 -07001329/*
1330 * Look for a btrfs signature on a device. This may be called out of the mount path
1331 * and we are not allowed to call set_blocksize during the scan. The superblock
1332 * is read via pagecache
1333 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001334struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1335 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001336{
1337 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001338 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001339 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001340 struct block_device *bdev;
Naohiro Aota12659252020-11-10 20:26:14 +09001341 u64 bytenr, bytenr_orig;
1342 int ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001343
David Sterba899f9302018-06-19 16:37:36 +02001344 lockdep_assert_held(&uuid_mutex);
1345
David Sterba6f60cbd2013-02-15 11:31:02 -07001346 /*
1347 * we would like to check all the supers, but that would make
1348 * a btrfs mount succeed after a mkfs from a different FS.
1349 * So, we need to add a special mount option to scan for
1350 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1351 */
Tejun Heod4d77622010-11-13 11:55:18 +01001352 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001353
1354 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001355 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001356 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001357
Naohiro Aota12659252020-11-10 20:26:14 +09001358 bytenr_orig = btrfs_sb_offset(0);
1359 ret = btrfs_sb_log_location_bdev(bdev, 0, READ, &bytenr);
1360 if (ret)
1361 return ERR_PTR(ret);
1362
1363 disk_super = btrfs_read_disk_super(bdev, bytenr, bytenr_orig);
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001364 if (IS_ERR(disk_super)) {
1365 device = ERR_CAST(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001366 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001367 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001368
Anand Jain4306a972018-05-29 12:28:37 +08001369 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001370 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001371 if (new_device_added)
1372 btrfs_free_stale_devices(path, device);
1373 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001374
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001375 btrfs_release_disk_super(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001376
1377error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001378 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001379
Gu Jinxiang36350e92018-07-12 14:23:16 +08001380 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001381}
Chris Mason0b86a832008-03-24 15:01:56 -04001382
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001383/*
1384 * Try to find a chunk that intersects [start, start + len] range and when one
1385 * such is found, record the end of it in *start
1386 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001387static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1388 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001389{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001390 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001391
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001392 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001393
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001394 if (!find_first_extent_bit(&device->alloc_state, *start,
1395 &physical_start, &physical_end,
1396 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001397
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001398 if (in_range(physical_start, *start, len) ||
1399 in_range(*start, physical_start,
1400 physical_end - physical_start)) {
1401 *start = physical_end + 1;
1402 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001403 }
1404 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001405 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001406}
1407
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001408static u64 dev_extent_search_start(struct btrfs_device *device, u64 start)
1409{
1410 switch (device->fs_devices->chunk_alloc_policy) {
1411 case BTRFS_CHUNK_ALLOC_REGULAR:
1412 /*
1413 * We don't want to overwrite the superblock on the drive nor
1414 * any area used by the boot loader (grub for example), so we
1415 * make sure to start at an offset of at least 1MB.
1416 */
1417 return max_t(u64, start, SZ_1M);
Naohiro Aota1cd61212021-02-04 19:21:48 +09001418 case BTRFS_CHUNK_ALLOC_ZONED:
1419 /*
1420 * We don't care about the starting region like regular
1421 * allocator, because we anyway use/reserve the first two zones
1422 * for superblock logging.
1423 */
1424 return ALIGN(start, device->zone_info->zone_size);
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001425 default:
1426 BUG();
1427 }
1428}
1429
Naohiro Aota1cd61212021-02-04 19:21:48 +09001430static bool dev_extent_hole_check_zoned(struct btrfs_device *device,
1431 u64 *hole_start, u64 *hole_size,
1432 u64 num_bytes)
1433{
1434 u64 zone_size = device->zone_info->zone_size;
1435 u64 pos;
1436 int ret;
1437 bool changed = false;
1438
1439 ASSERT(IS_ALIGNED(*hole_start, zone_size));
1440
1441 while (*hole_size > 0) {
1442 pos = btrfs_find_allocatable_zones(device, *hole_start,
1443 *hole_start + *hole_size,
1444 num_bytes);
1445 if (pos != *hole_start) {
1446 *hole_size = *hole_start + *hole_size - pos;
1447 *hole_start = pos;
1448 changed = true;
1449 if (*hole_size < num_bytes)
1450 break;
1451 }
1452
1453 ret = btrfs_ensure_empty_zones(device, pos, num_bytes);
1454
1455 /* Range is ensured to be empty */
1456 if (!ret)
1457 return changed;
1458
1459 /* Given hole range was invalid (outside of device) */
1460 if (ret == -ERANGE) {
1461 *hole_start += *hole_size;
1462 *hole_size = 0;
1463 return 1;
1464 }
1465
1466 *hole_start += zone_size;
1467 *hole_size -= zone_size;
1468 changed = true;
1469 }
1470
1471 return changed;
1472}
1473
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001474/**
1475 * dev_extent_hole_check - check if specified hole is suitable for allocation
1476 * @device: the device which we have the hole
1477 * @hole_start: starting position of the hole
1478 * @hole_size: the size of the hole
1479 * @num_bytes: the size of the free space that we need
1480 *
Naohiro Aota1cd61212021-02-04 19:21:48 +09001481 * This function may modify @hole_start and @hole_size to reflect the suitable
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001482 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1483 */
1484static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
1485 u64 *hole_size, u64 num_bytes)
1486{
1487 bool changed = false;
1488 u64 hole_end = *hole_start + *hole_size;
1489
Naohiro Aota1cd61212021-02-04 19:21:48 +09001490 for (;;) {
1491 /*
1492 * Check before we set max_hole_start, otherwise we could end up
1493 * sending back this offset anyway.
1494 */
1495 if (contains_pending_extent(device, hole_start, *hole_size)) {
1496 if (hole_end >= *hole_start)
1497 *hole_size = hole_end - *hole_start;
1498 else
1499 *hole_size = 0;
1500 changed = true;
1501 }
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001502
Naohiro Aota1cd61212021-02-04 19:21:48 +09001503 switch (device->fs_devices->chunk_alloc_policy) {
1504 case BTRFS_CHUNK_ALLOC_REGULAR:
1505 /* No extra check */
1506 break;
1507 case BTRFS_CHUNK_ALLOC_ZONED:
1508 if (dev_extent_hole_check_zoned(device, hole_start,
1509 hole_size, num_bytes)) {
1510 changed = true;
1511 /*
1512 * The changed hole can contain pending extent.
1513 * Loop again to check that.
1514 */
1515 continue;
1516 }
1517 break;
1518 default:
1519 BUG();
1520 }
1521
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001522 break;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001523 }
1524
1525 return changed;
1526}
Josef Bacik6df9a952013-06-27 13:22:46 -04001527
Chris Mason0b86a832008-03-24 15:01:56 -04001528/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001529 * find_free_dev_extent_start - find free space in the specified device
1530 * @device: the device which we search the free space in
1531 * @num_bytes: the size of the free space that we need
1532 * @search_start: the position from which to begin the search
1533 * @start: store the start of the free space.
1534 * @len: the size of the free space. that we find, or the size
1535 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001536 *
Chris Mason0b86a832008-03-24 15:01:56 -04001537 * this uses a pretty simple search, the expectation is that it is
1538 * called very infrequently and that a given device has a small number
1539 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001540 *
1541 * @start is used to store the start of the free space if we find. But if we
1542 * don't find suitable free space, it will be used to store the start position
1543 * of the max free space.
1544 *
1545 * @len is used to store the size of the free space that we find.
1546 * But if we don't find suitable free space, it is used to store the size of
1547 * the max free space.
Qu Wenruo135da972019-07-19 14:51:42 +08001548 *
1549 * NOTE: This function will search *commit* root of device tree, and does extra
1550 * check to ensure dev extents are not double allocated.
1551 * This makes the function safe to allocate dev extents but may not report
1552 * correct usable device space, as device extent freed in current transaction
1553 * is not reported as avaiable.
Chris Mason0b86a832008-03-24 15:01:56 -04001554 */
Qu Wenruo9e3246a2019-07-19 14:51:41 +08001555static int find_free_dev_extent_start(struct btrfs_device *device,
1556 u64 num_bytes, u64 search_start, u64 *start,
1557 u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001558{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001559 struct btrfs_fs_info *fs_info = device->fs_info;
1560 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001561 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001562 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001563 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001564 u64 hole_size;
1565 u64 max_hole_start;
1566 u64 max_hole_size;
1567 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001568 u64 search_end = device->total_bytes;
1569 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001570 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001571 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001572
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001573 search_start = dev_extent_search_start(device, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001574
Naohiro Aota1cd61212021-02-04 19:21:48 +09001575 WARN_ON(device->zone_info &&
1576 !IS_ALIGNED(num_bytes, device->zone_info->zone_size));
1577
Josef Bacik6df9a952013-06-27 13:22:46 -04001578 path = btrfs_alloc_path();
1579 if (!path)
1580 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001581
Miao Xie7bfc8372011-01-05 10:07:26 +00001582 max_hole_start = search_start;
1583 max_hole_size = 0;
1584
Zhao Leif2ab7612015-02-16 18:52:17 +08001585again:
Anand Jain401e29c2017-12-04 12:54:55 +08001586 if (search_start >= search_end ||
1587 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001588 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001589 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001590 }
1591
David Sterbae4058b52015-11-27 16:31:35 +01001592 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001593 path->search_commit_root = 1;
1594 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001595
Chris Mason0b86a832008-03-24 15:01:56 -04001596 key.objectid = device->devid;
1597 key.offset = search_start;
1598 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001599
Li Zefan125ccb02011-12-08 15:07:24 +08001600 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001601 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001602 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001603 if (ret > 0) {
1604 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1605 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001606 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001607 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001608
Chris Mason0b86a832008-03-24 15:01:56 -04001609 while (1) {
1610 l = path->nodes[0];
1611 slot = path->slots[0];
1612 if (slot >= btrfs_header_nritems(l)) {
1613 ret = btrfs_next_leaf(root, path);
1614 if (ret == 0)
1615 continue;
1616 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001617 goto out;
1618
1619 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001620 }
1621 btrfs_item_key_to_cpu(l, &key, slot);
1622
1623 if (key.objectid < device->devid)
1624 goto next;
1625
1626 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001627 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001628
David Sterba962a2982014-06-04 18:41:45 +02001629 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001630 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001631
Miao Xie7bfc8372011-01-05 10:07:26 +00001632 if (key.offset > search_start) {
1633 hole_size = key.offset - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001634 dev_extent_hole_check(device, &search_start, &hole_size,
1635 num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04001636
Miao Xie7bfc8372011-01-05 10:07:26 +00001637 if (hole_size > max_hole_size) {
1638 max_hole_start = search_start;
1639 max_hole_size = hole_size;
1640 }
1641
1642 /*
1643 * If this free space is greater than which we need,
1644 * it must be the max free space that we have found
1645 * until now, so max_hole_start must point to the start
1646 * of this free space and the length of this free space
1647 * is stored in max_hole_size. Thus, we return
1648 * max_hole_start and max_hole_size and go back to the
1649 * caller.
1650 */
1651 if (hole_size >= num_bytes) {
1652 ret = 0;
1653 goto out;
1654 }
1655 }
1656
Chris Mason0b86a832008-03-24 15:01:56 -04001657 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001658 extent_end = key.offset + btrfs_dev_extent_length(l,
1659 dev_extent);
1660 if (extent_end > search_start)
1661 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001662next:
1663 path->slots[0]++;
1664 cond_resched();
1665 }
Chris Mason0b86a832008-03-24 15:01:56 -04001666
liubo38c01b92011-08-02 02:39:03 +00001667 /*
1668 * At this point, search_start should be the end of
1669 * allocated dev extents, and when shrinking the device,
1670 * search_end may be smaller than search_start.
1671 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001672 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001673 hole_size = search_end - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001674 if (dev_extent_hole_check(device, &search_start, &hole_size,
1675 num_bytes)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001676 btrfs_release_path(path);
1677 goto again;
1678 }
Chris Mason0b86a832008-03-24 15:01:56 -04001679
Zhao Leif2ab7612015-02-16 18:52:17 +08001680 if (hole_size > max_hole_size) {
1681 max_hole_start = search_start;
1682 max_hole_size = hole_size;
1683 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001684 }
1685
Miao Xie7bfc8372011-01-05 10:07:26 +00001686 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001687 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001688 ret = -ENOSPC;
1689 else
1690 ret = 0;
1691
1692out:
Yan Zheng2b820322008-11-17 21:11:30 -05001693 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001694 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001695 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001696 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001697 return ret;
1698}
1699
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001700int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001701 u64 *start, u64 *len)
1702{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001703 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001704 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001705}
1706
Christoph Hellwigb2950862008-12-02 09:54:17 -05001707static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001708 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001709 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001711 struct btrfs_fs_info *fs_info = device->fs_info;
1712 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001713 int ret;
1714 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001715 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001716 struct btrfs_key found_key;
1717 struct extent_buffer *leaf = NULL;
1718 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001719
1720 path = btrfs_alloc_path();
1721 if (!path)
1722 return -ENOMEM;
1723
1724 key.objectid = device->devid;
1725 key.offset = start;
1726 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001727again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001728 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001729 if (ret > 0) {
1730 ret = btrfs_previous_item(root, path, key.objectid,
1731 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001732 if (ret)
1733 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001734 leaf = path->nodes[0];
1735 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1736 extent = btrfs_item_ptr(leaf, path->slots[0],
1737 struct btrfs_dev_extent);
1738 BUG_ON(found_key.offset > start || found_key.offset +
1739 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001740 key = found_key;
1741 btrfs_release_path(path);
1742 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001743 } else if (ret == 0) {
1744 leaf = path->nodes[0];
1745 extent = btrfs_item_ptr(leaf, path->slots[0],
1746 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001747 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001748 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001749 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001750 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001751
Miao Xie2196d6e2014-09-03 21:35:41 +08001752 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1753
Chris Mason8f18cf12008-04-25 16:53:30 -04001754 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001755 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001756 btrfs_handle_fs_error(fs_info, ret,
1757 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001758 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001759 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001760 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001761out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001762 btrfs_free_path(path);
1763 return ret;
1764}
1765
Eric Sandeen48a3b632013-04-25 20:41:01 +00001766static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1767 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001768 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001769{
1770 int ret;
1771 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001772 struct btrfs_fs_info *fs_info = device->fs_info;
1773 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001774 struct btrfs_dev_extent *extent;
1775 struct extent_buffer *leaf;
1776 struct btrfs_key key;
1777
Anand Jaine12c9622017-12-04 12:54:53 +08001778 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001779 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001780 path = btrfs_alloc_path();
1781 if (!path)
1782 return -ENOMEM;
1783
Chris Mason0b86a832008-03-24 15:01:56 -04001784 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001785 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001786 key.type = BTRFS_DEV_EXTENT_KEY;
1787 ret = btrfs_insert_empty_item(trans, root, path, &key,
1788 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001789 if (ret)
1790 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001791
1792 leaf = path->nodes[0];
1793 extent = btrfs_item_ptr(leaf, path->slots[0],
1794 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001795 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1796 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001797 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1798 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001799 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1800
Chris Mason0b86a832008-03-24 15:01:56 -04001801 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1802 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001803out:
Chris Mason0b86a832008-03-24 15:01:56 -04001804 btrfs_free_path(path);
1805 return ret;
1806}
1807
Josef Bacik6df9a952013-06-27 13:22:46 -04001808static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001809{
Josef Bacik6df9a952013-06-27 13:22:46 -04001810 struct extent_map_tree *em_tree;
1811 struct extent_map *em;
1812 struct rb_node *n;
1813 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001814
David Sterbac8bf1b62019-05-17 11:43:17 +02001815 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001816 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001817 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001818 if (n) {
1819 em = rb_entry(n, struct extent_map, rb_node);
1820 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001821 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001822 read_unlock(&em_tree->lock);
1823
Chris Mason0b86a832008-03-24 15:01:56 -04001824 return ret;
1825}
1826
Ilya Dryomov53f10652013-08-12 14:33:01 +03001827static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1828 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001829{
1830 int ret;
1831 struct btrfs_key key;
1832 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001833 struct btrfs_path *path;
1834
Yan Zheng2b820322008-11-17 21:11:30 -05001835 path = btrfs_alloc_path();
1836 if (!path)
1837 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001838
1839 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1840 key.type = BTRFS_DEV_ITEM_KEY;
1841 key.offset = (u64)-1;
1842
Ilya Dryomov53f10652013-08-12 14:33:01 +03001843 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001844 if (ret < 0)
1845 goto error;
1846
Anand Jaina06dee4d2019-08-27 15:40:44 +08001847 if (ret == 0) {
1848 /* Corruption */
1849 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1850 ret = -EUCLEAN;
1851 goto error;
1852 }
Chris Mason0b86a832008-03-24 15:01:56 -04001853
Ilya Dryomov53f10652013-08-12 14:33:01 +03001854 ret = btrfs_previous_item(fs_info->chunk_root, path,
1855 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001856 BTRFS_DEV_ITEM_KEY);
1857 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001858 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001859 } else {
1860 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1861 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001862 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001863 }
1864 ret = 0;
1865error:
Yan Zheng2b820322008-11-17 21:11:30 -05001866 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001867 return ret;
1868}
1869
1870/*
1871 * the device information is stored in the chunk root
1872 * the btrfs_device struct should be fully filled in
1873 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001874static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001875 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001876{
1877 int ret;
1878 struct btrfs_path *path;
1879 struct btrfs_dev_item *dev_item;
1880 struct extent_buffer *leaf;
1881 struct btrfs_key key;
1882 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001883
Chris Mason0b86a832008-03-24 15:01:56 -04001884 path = btrfs_alloc_path();
1885 if (!path)
1886 return -ENOMEM;
1887
Chris Mason0b86a832008-03-24 15:01:56 -04001888 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1889 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001890 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001891
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001892 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1893 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001894 if (ret)
1895 goto out;
1896
1897 leaf = path->nodes[0];
1898 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1899
1900 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001901 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001902 btrfs_set_device_type(leaf, dev_item, device->type);
1903 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1904 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1905 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001906 btrfs_set_device_total_bytes(leaf, dev_item,
1907 btrfs_device_get_disk_total_bytes(device));
1908 btrfs_set_device_bytes_used(leaf, dev_item,
1909 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001910 btrfs_set_device_group(leaf, dev_item, 0);
1911 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1912 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001913 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001914
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001915 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001916 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001917 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001918 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1919 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001920 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001921
Yan Zheng2b820322008-11-17 21:11:30 -05001922 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001923out:
1924 btrfs_free_path(path);
1925 return ret;
1926}
Chris Mason8f18cf12008-04-25 16:53:30 -04001927
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001928/*
1929 * Function to update ctime/mtime for a given device path.
1930 * Mainly used for ctime/mtime based probe like libblkid.
1931 */
David Sterbada353f62017-02-14 17:55:53 +01001932static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001933{
1934 struct file *filp;
1935
1936 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001937 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001938 return;
1939 file_update_time(filp);
1940 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001941}
1942
David Sterbaf331a952019-03-20 16:31:53 +01001943static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001944{
David Sterbaf331a952019-03-20 16:31:53 +01001945 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001946 int ret;
1947 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001948 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001949 struct btrfs_trans_handle *trans;
1950
Chris Masona061fc82008-05-07 11:43:44 -04001951 path = btrfs_alloc_path();
1952 if (!path)
1953 return -ENOMEM;
1954
Yan, Zhenga22285a2010-05-16 10:48:46 -04001955 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001956 if (IS_ERR(trans)) {
1957 btrfs_free_path(path);
1958 return PTR_ERR(trans);
1959 }
Chris Masona061fc82008-05-07 11:43:44 -04001960 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1961 key.type = BTRFS_DEV_ITEM_KEY;
1962 key.offset = device->devid;
1963
1964 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001965 if (ret) {
1966 if (ret > 0)
1967 ret = -ENOENT;
1968 btrfs_abort_transaction(trans, ret);
1969 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001970 goto out;
1971 }
1972
1973 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001974 if (ret) {
1975 btrfs_abort_transaction(trans, ret);
1976 btrfs_end_transaction(trans);
1977 }
1978
Chris Masona061fc82008-05-07 11:43:44 -04001979out:
1980 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001981 if (!ret)
1982 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001983 return ret;
1984}
1985
David Sterba3cc31a02016-02-15 16:00:26 +01001986/*
1987 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1988 * filesystem. It's up to the caller to adjust that number regarding eg. device
1989 * replace.
1990 */
1991static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1992 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001993{
Chris Masona061fc82008-05-07 11:43:44 -04001994 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001995 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001996 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001997
Miao Xiede98ced2013-01-29 10:13:12 +00001998 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001999 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002000
Anand Jainbd45ffb2016-02-13 10:01:34 +08002001 all_avail = fs_info->avail_data_alloc_bits |
2002 fs_info->avail_system_alloc_bits |
2003 fs_info->avail_metadata_alloc_bits;
2004 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002005
David Sterba418775a2016-02-15 16:28:14 +01002006 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002007 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002008 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002009
David Sterba418775a2016-02-15 16:28:14 +01002010 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002011 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002012
David Sterba418775a2016-02-15 16:28:14 +01002013 if (ret)
2014 return ret;
2015 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002016 }
2017
2018 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002019}
2020
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002021static struct btrfs_device * btrfs_find_next_active_device(
2022 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002023{
2024 struct btrfs_device *next_device;
2025
2026 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2027 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002028 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2029 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002030 return next_device;
2031 }
2032
2033 return NULL;
2034}
2035
2036/*
2037 * Helper function to check if the given device is part of s_bdev / latest_bdev
2038 * and replace it with the provided or the next active device, in the context
2039 * where this function called, there should be always be another device (or
2040 * this_dev) which is active.
2041 */
David Sterbab105e922019-10-01 19:57:35 +02002042void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
Anand Jaine493e8f2020-09-05 01:34:34 +08002043 struct btrfs_device *next_device)
Anand Jain88acff62016-05-03 17:44:43 +08002044{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002045 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002046
Anand Jaine493e8f2020-09-05 01:34:34 +08002047 if (!next_device)
Anand Jain88acff62016-05-03 17:44:43 +08002048 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
Anand Jaine493e8f2020-09-05 01:34:34 +08002049 device);
Anand Jain88acff62016-05-03 17:44:43 +08002050 ASSERT(next_device);
2051
2052 if (fs_info->sb->s_bdev &&
2053 (fs_info->sb->s_bdev == device->bdev))
2054 fs_info->sb->s_bdev = next_device->bdev;
2055
2056 if (fs_info->fs_devices->latest_bdev == device->bdev)
2057 fs_info->fs_devices->latest_bdev = next_device->bdev;
2058}
2059
Anand Jain1da73962018-08-10 13:53:21 +08002060/*
2061 * Return btrfs_fs_devices::num_devices excluding the device that's being
2062 * currently replaced.
2063 */
2064static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2065{
2066 u64 num_devices = fs_info->fs_devices->num_devices;
2067
David Sterbacb5583d2018-09-07 16:11:23 +02002068 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002069 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2070 ASSERT(num_devices > 1);
2071 num_devices--;
2072 }
David Sterbacb5583d2018-09-07 16:11:23 +02002073 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002074
2075 return num_devices;
2076}
2077
Josef Bacik313b0852020-08-20 11:18:26 -04002078void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
2079 struct block_device *bdev,
2080 const char *device_path)
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002081{
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002082 struct btrfs_super_block *disk_super;
2083 int copy_num;
2084
2085 if (!bdev)
2086 return;
2087
2088 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002089 struct page *page;
2090 int ret;
2091
2092 disk_super = btrfs_read_dev_one_super(bdev, copy_num);
2093 if (IS_ERR(disk_super))
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002094 continue;
2095
Naohiro Aota12659252020-11-10 20:26:14 +09002096 if (bdev_is_zoned(bdev)) {
2097 btrfs_reset_sb_log_zones(bdev, copy_num);
2098 continue;
2099 }
2100
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002101 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002102
2103 page = virt_to_page(disk_super);
2104 set_page_dirty(page);
2105 lock_page(page);
2106 /* write_on_page() unlocks the page */
2107 ret = write_one_page(page);
2108 if (ret)
2109 btrfs_warn(fs_info,
2110 "error clearing superblock number %d (%d)",
2111 copy_num, ret);
2112 btrfs_release_disk_super(disk_super);
2113
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002114 }
2115
2116 /* Notify udev that device has changed */
2117 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
2118
2119 /* Update ctime/mtime for device path for libblkid */
2120 update_dev_time(device_path);
2121}
2122
David Sterbada353f62017-02-14 17:55:53 +01002123int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
Anand Jain53f8a742020-09-05 01:34:27 +08002124 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002125{
2126 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002127 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002128 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002129 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002130 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002131
2132 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002133
Anand Jain1da73962018-08-10 13:53:21 +08002134 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002135
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002136 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002137 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002138 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002139
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002140 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2141
2142 if (IS_ERR(device)) {
2143 if (PTR_ERR(device) == -ENOENT &&
2144 strcmp(device_path, "missing") == 0)
2145 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2146 else
2147 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002148 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002149 }
Yan Zheng2b820322008-11-17 21:11:30 -05002150
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002151 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2152 btrfs_warn_in_rcu(fs_info,
2153 "cannot remove device %s (devid %llu) due to active swapfile",
2154 rcu_str_deref(device->name), device->devid);
2155 ret = -ETXTBSY;
2156 goto out;
2157 }
2158
Anand Jain401e29c2017-12-04 12:54:55 +08002159 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002160 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002161 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002162 }
2163
Anand Jainebbede42017-12-04 12:54:52 +08002164 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2165 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002166 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002167 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002168 }
2169
Anand Jainebbede42017-12-04 12:54:52 +08002170 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002171 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002172 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002173 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002174 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002175 }
Chris Masona061fc82008-05-07 11:43:44 -04002176
Carey Underwoodd7901552013-03-04 16:37:06 -06002177 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002178 ret = btrfs_shrink_device(device, 0);
Filipe Manana66d204a2020-10-12 11:55:24 +01002179 if (!ret)
2180 btrfs_reada_remove_dev(device);
Carey Underwoodd7901552013-03-04 16:37:06 -06002181 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002182 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002183 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002184
Stefan Behrens63a212a2012-11-05 18:29:28 +01002185 /*
2186 * TODO: the superblock still includes this device in its num_devices
2187 * counter although write_all_supers() is not locked out. This
2188 * could give a filesystem state which requires a degraded mount.
2189 */
David Sterbaf331a952019-03-20 16:31:53 +01002190 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002191 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002192 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002193
Anand Jaine12c9622017-12-04 12:54:53 +08002194 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002195 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002196
2197 /*
2198 * the device list mutex makes sure that we don't change
2199 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002200 * the device supers. Whoever is writing all supers, should
2201 * lock the device list mutex before getting the number of
2202 * devices in the super block (super_copy). Conversely,
2203 * whoever updates the number of devices in the super block
2204 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002205 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002206
Anand Jain41a52a02018-04-12 10:29:31 +08002207 /*
2208 * In normal cases the cur_devices == fs_devices. But in case
2209 * of deleting a seed device, the cur_devices should point to
2210 * its own fs_devices listed under the fs_devices->seed.
2211 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002212 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002213 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002214 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002215
Anand Jain41a52a02018-04-12 10:29:31 +08002216 cur_devices->num_devices--;
2217 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002218 /* Update total_devices of the parent fs_devices if it's seed */
2219 if (cur_devices != fs_devices)
2220 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002221
Anand Jaine6e674b2017-12-04 12:54:54 +08002222 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002223 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002224
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002225 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002226
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002227 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002228 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002229 /* remove sysfs entry */
Anand Jain53f8a742020-09-05 01:34:27 +08002230 btrfs_sysfs_remove_device(device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002231 }
Anand Jain99994cd2014-06-03 11:36:00 +08002232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002233 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2234 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002235 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002236
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002237 /*
2238 * at this point, the device is zero sized and detached from
2239 * the devices list. All that's left is to zero out the old
2240 * supers and free the device.
2241 */
Anand Jainebbede42017-12-04 12:54:52 +08002242 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002243 btrfs_scratch_superblocks(fs_info, device->bdev,
2244 device->name->str);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002245
2246 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002247 synchronize_rcu();
2248 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002249
Xiao Guangrong1f781602011-04-20 10:09:16 +00002250 if (cur_devices->open_devices == 0) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002251 list_del_init(&cur_devices->seed_list);
Anand Jain0226e0e2018-04-12 10:29:27 +08002252 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002253 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002254 }
2255
Chris Masona061fc82008-05-07 11:43:44 -04002256out:
2257 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002258 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002259
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002260error_undo:
Filipe Manana66d204a2020-10-12 11:55:24 +01002261 btrfs_reada_undo_remove_dev(device);
Anand Jainebbede42017-12-04 12:54:52 +08002262 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002263 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002264 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002265 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002266 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002267 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002268 }
Anand Jain24fc5722016-02-13 10:01:36 +08002269 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002270}
2271
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002272void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002273{
Anand Jaind51908c2014-08-13 14:24:19 +08002274 struct btrfs_fs_devices *fs_devices;
2275
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002276 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002277
Anand Jain25e8e912014-08-20 10:56:56 +08002278 /*
2279 * in case of fs with no seed, srcdev->fs_devices will point
2280 * to fs_devices of fs_info. However when the dev being replaced is
2281 * a seed dev it will point to the seed's local fs_devices. In short
2282 * srcdev will have its correct fs_devices in both the cases.
2283 */
2284 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002285
Stefan Behrense93c89c2012-11-05 17:33:06 +01002286 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002287 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002288 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002289 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002290 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002291
Anand Jainebbede42017-12-04 12:54:52 +08002292 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002293 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002294
Miao Xie82372bc2014-09-03 21:35:44 +08002295 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002296 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002297}
2298
David Sterba65237ee2019-03-20 16:34:54 +01002299void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002300{
2301 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002302
Josef Bacika466c852020-08-20 11:18:27 -04002303 mutex_lock(&uuid_mutex);
2304
Anand Jain14238812016-07-22 06:04:53 +08002305 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002306 synchronize_rcu();
2307 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002308
Anand Jain94d5f0c2014-08-13 14:24:22 +08002309 /* if this is no devs we rather delete the fs_devices */
2310 if (!fs_devices->num_devices) {
Anand Jain6dd38f82017-10-17 06:53:50 +08002311 /*
2312 * On a mounted FS, num_devices can't be zero unless it's a
2313 * seed. In case of a seed device being replaced, the replace
2314 * target added to the sprout FS, so there will be no more
2315 * device left under the seed FS.
2316 */
2317 ASSERT(fs_devices->seeding);
2318
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002319 list_del_init(&fs_devices->seed_list);
Anand Jain0226e0e2018-04-12 10:29:27 +08002320 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002321 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002322 }
Josef Bacika466c852020-08-20 11:18:27 -04002323 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002324}
2325
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002326void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002327{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002328 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002329
Anand Jaind9a071f2018-04-12 10:29:38 +08002330 mutex_lock(&fs_devices->device_list_mutex);
2331
Anand Jain53f8a742020-09-05 01:34:27 +08002332 btrfs_sysfs_remove_device(tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002333
Anand Jain779bf3fe2016-04-18 16:51:23 +08002334 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002335 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002336
Anand Jaind9a071f2018-04-12 10:29:38 +08002337 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002338
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002339 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002340
Stefan Behrense93c89c2012-11-05 17:33:06 +01002341 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002342
Anand Jaind9a071f2018-04-12 10:29:38 +08002343 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002344
2345 /*
2346 * The update_dev_time() with in btrfs_scratch_superblocks()
2347 * may lead to a call to btrfs_show_devname() which will try
2348 * to hold device_list_mutex. And here this device
2349 * is already out of device list, so we don't have to hold
2350 * the device_list_mutex lock.
2351 */
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002352 btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev,
2353 tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002354
2355 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002356 synchronize_rcu();
2357 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002358}
2359
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002360static struct btrfs_device *btrfs_find_device_by_path(
2361 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002362{
2363 int ret = 0;
2364 struct btrfs_super_block *disk_super;
2365 u64 devid;
2366 u8 *dev_uuid;
2367 struct block_device *bdev;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002368 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002369
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002370 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002371 fs_info->bdev_holder, 0, &bdev, &disk_super);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002372 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002373 return ERR_PTR(ret);
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002374
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002375 devid = btrfs_stack_device_id(&disk_super->dev_item);
2376 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002377 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002378 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jainb2598ed2020-11-03 13:49:43 +08002379 disk_super->metadata_uuid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002380 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002381 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jainb2598ed2020-11-03 13:49:43 +08002382 disk_super->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002383
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002384 btrfs_release_disk_super(disk_super);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002385 if (!device)
2386 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002387 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002388 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002389}
2390
Yan Zheng2b820322008-11-17 21:11:30 -05002391/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002392 * Lookup a device given by device id, or the path if the id is 0.
2393 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002394struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002395 struct btrfs_fs_info *fs_info, u64 devid,
2396 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002397{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002398 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002399
David Sterba5c5c0df2016-02-15 16:39:55 +01002400 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002401 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jainb2598ed2020-11-03 13:49:43 +08002402 NULL);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002403 if (!device)
2404 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002405 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002406 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002407
2408 if (!device_path || !device_path[0])
2409 return ERR_PTR(-EINVAL);
2410
2411 if (strcmp(device_path, "missing") == 0) {
2412 /* Find first missing device */
2413 list_for_each_entry(device, &fs_info->fs_devices->devices,
2414 dev_list) {
2415 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2416 &device->dev_state) && !device->bdev)
2417 return device;
2418 }
2419 return ERR_PTR(-ENOENT);
2420 }
2421
2422 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002423}
2424
Yan Zheng2b820322008-11-17 21:11:30 -05002425/*
2426 * does all the dirty work required for changing file system's UUID.
2427 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002428static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002430 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002431 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002432 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002433 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002434 struct btrfs_device *device;
2435 u64 super_flags;
2436
David Sterbaa32bf9a2018-03-16 02:21:22 +01002437 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002438 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002439 return -EINVAL;
2440
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03002441 /*
2442 * Private copy of the seed devices, anchored at
2443 * fs_info->fs_devices->seed_list
2444 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002445 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002446 if (IS_ERR(seed_devices))
2447 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002448
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03002449 /*
2450 * It's necessary to retain a copy of the original seed fs_devices in
2451 * fs_uuids so that filesystems which have been seeded can successfully
2452 * reference the seed device from open_seed_devices. This also supports
2453 * multiple fs seed.
2454 */
Yan Zhenge4404d62008-12-12 10:03:26 -05002455 old_devices = clone_fs_devices(fs_devices);
2456 if (IS_ERR(old_devices)) {
2457 kfree(seed_devices);
2458 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002459 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002460
Anand Jainc4babc52018-04-12 10:29:25 +08002461 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002462
Yan Zhenge4404d62008-12-12 10:03:26 -05002463 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2464 seed_devices->opened = 1;
2465 INIT_LIST_HEAD(&seed_devices->devices);
2466 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002467 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002468
Anand Jain321a4bf2018-07-16 22:58:09 +08002469 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002470 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2471 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002472 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002473 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002474
Johannes Thumshirn0395d842019-11-13 11:27:27 +01002475 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002476 fs_devices->num_devices = 0;
2477 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002478 fs_devices->missing_devices = 0;
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002479 fs_devices->rotating = false;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002480 list_add(&seed_devices->seed_list, &fs_devices->seed_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002481
2482 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002483 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002484 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002485 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002486
Yan Zheng2b820322008-11-17 21:11:30 -05002487 super_flags = btrfs_super_flags(disk_super) &
2488 ~BTRFS_SUPER_FLAG_SEEDING;
2489 btrfs_set_super_flags(disk_super, super_flags);
2490
2491 return 0;
2492}
2493
2494/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002495 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002496 */
David Sterba5c4666292019-03-20 16:36:39 +01002497static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002498{
David Sterba5c4666292019-03-20 16:36:39 +01002499 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002500 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002501 struct btrfs_path *path;
2502 struct extent_buffer *leaf;
2503 struct btrfs_dev_item *dev_item;
2504 struct btrfs_device *device;
2505 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002506 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002507 u8 dev_uuid[BTRFS_UUID_SIZE];
2508 u64 devid;
2509 int ret;
2510
2511 path = btrfs_alloc_path();
2512 if (!path)
2513 return -ENOMEM;
2514
Yan Zheng2b820322008-11-17 21:11:30 -05002515 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2516 key.offset = 0;
2517 key.type = BTRFS_DEV_ITEM_KEY;
2518
2519 while (1) {
2520 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2521 if (ret < 0)
2522 goto error;
2523
2524 leaf = path->nodes[0];
2525next_slot:
2526 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2527 ret = btrfs_next_leaf(root, path);
2528 if (ret > 0)
2529 break;
2530 if (ret < 0)
2531 goto error;
2532 leaf = path->nodes[0];
2533 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002534 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002535 continue;
2536 }
2537
2538 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2539 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2540 key.type != BTRFS_DEV_ITEM_KEY)
2541 break;
2542
2543 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2544 struct btrfs_dev_item);
2545 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002546 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002547 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002548 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002549 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002550 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jainb2598ed2020-11-03 13:49:43 +08002551 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002552 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002553
2554 if (device->fs_devices->seeding) {
2555 btrfs_set_device_generation(leaf, dev_item,
2556 device->generation);
2557 btrfs_mark_buffer_dirty(leaf);
2558 }
2559
2560 path->slots[0]++;
2561 goto next_slot;
2562 }
2563 ret = 0;
2564error:
2565 btrfs_free_path(path);
2566 return ret;
2567}
2568
David Sterbada353f62017-02-14 17:55:53 +01002569int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002570{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002571 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002572 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002573 struct btrfs_trans_handle *trans;
2574 struct btrfs_device *device;
2575 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002576 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002577 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002578 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002579 u64 orig_super_total_bytes;
2580 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002581 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002582 int ret = 0;
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002583 bool locked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002584
Anand Jain5da54bc2018-07-03 13:14:50 +08002585 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002586 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002587
Li Zefana5d163332011-12-07 20:08:40 -05002588 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002589 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002590 if (IS_ERR(bdev))
2591 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002592
Naohiro Aotab70f5092020-11-10 20:26:08 +09002593 if (!btrfs_check_device_zone_type(fs_info, bdev)) {
2594 ret = -EINVAL;
2595 goto error;
2596 }
2597
Anand Jain5da54bc2018-07-03 13:14:50 +08002598 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002599 seeding_dev = 1;
2600 down_write(&sb->s_umount);
2601 mutex_lock(&uuid_mutex);
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002602 locked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002603 }
2604
Nikolay Borisovb9ba0172020-07-22 11:09:25 +03002605 sync_blockdev(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002606
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002607 rcu_read_lock();
2608 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002609 if (device->bdev == bdev) {
2610 ret = -EEXIST;
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002611 rcu_read_unlock();
Yan Zheng2b820322008-11-17 21:11:30 -05002612 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002613 }
2614 }
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002615 rcu_read_unlock();
Chris Mason788f20e2008-04-28 15:29:42 -04002616
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002617 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002618 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002619 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002620 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002621 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002622 }
2623
David Sterba78f2c9e2016-02-11 14:25:38 +01002624 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002625 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002626 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002627 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002628 }
Josef Bacik606686e2012-06-04 14:03:51 -04002629 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002630
Naohiro Aota5b316462020-11-10 20:26:07 +09002631 device->fs_info = fs_info;
2632 device->bdev = bdev;
2633
2634 ret = btrfs_get_dev_zone_info(device);
2635 if (ret)
2636 goto error_free_device;
2637
Yan, Zhenga22285a2010-05-16 10:48:46 -04002638 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002639 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002640 ret = PTR_ERR(trans);
Naohiro Aota5b316462020-11-10 20:26:07 +09002641 goto error_free_zone;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002642 }
2643
Josef Bacikd5e20032011-08-04 14:52:27 +00002644 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002645 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002646 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002647 device->io_width = fs_info->sectorsize;
2648 device->io_align = fs_info->sectorsize;
2649 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002650 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2651 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002652 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002653 device->commit_total_bytes = device->total_bytes;
Anand Jaine12c9622017-12-04 12:54:53 +08002654 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002655 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002656 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002657 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002658 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002659
Yan Zheng2b820322008-11-17 21:11:30 -05002660 if (seeding_dev) {
Filipe Mananaa0a1db72020-12-14 10:10:47 +00002661 btrfs_clear_sb_rdonly(sb);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002662 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002663 if (ret) {
2664 btrfs_abort_transaction(trans, ret);
2665 goto error_trans;
2666 }
Yan Zheng2b820322008-11-17 21:11:30 -05002667 }
2668
Anand Jain5da54bc2018-07-03 13:14:50 +08002669 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002670
Anand Jain5da54bc2018-07-03 13:14:50 +08002671 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002672 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002673 list_add_rcu(&device->dev_list, &fs_devices->devices);
2674 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2675 fs_devices->num_devices++;
2676 fs_devices->open_devices++;
2677 fs_devices->rw_devices++;
2678 fs_devices->total_devices++;
2679 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002680
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002681 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002682
Anand Jaine884f4f2017-04-04 18:40:19 +08002683 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002684 fs_devices->rotating = true;
Chris Masonc2898112009-06-10 09:51:32 -04002685
Naohiro Aota39379fa2018-07-27 09:04:55 +09002686 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002687 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002688 round_down(orig_super_total_bytes + device->total_bytes,
2689 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002690
Naohiro Aota39379fa2018-07-27 09:04:55 +09002691 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2692 btrfs_set_super_num_devices(fs_info->super_copy,
2693 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002694
Miao Xie2196d6e2014-09-03 21:35:41 +08002695 /*
2696 * we've got more storage, clear any full flags on the space
2697 * infos
2698 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002700
David Sterba34441362016-10-04 19:34:27 +02002701 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacikca108452020-09-01 08:09:01 -04002702
2703 /* Add sysfs device entry */
Anand Jaincd36da22020-09-05 01:34:26 +08002704 btrfs_sysfs_add_device(device);
Josef Bacikca108452020-09-01 08:09:01 -04002705
Anand Jain5da54bc2018-07-03 13:14:50 +08002706 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002707
Yan Zheng2b820322008-11-17 21:11:30 -05002708 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002709 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002710 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002711 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002712 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002713 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002714 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002715 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002716 }
2717
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002718 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002719 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002720 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002721 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002722 }
2723
2724 if (seeding_dev) {
David Sterba5c4666292019-03-20 16:36:39 +01002725 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002726 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002727 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002728 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002729 }
Anand Jainb2373f22014-06-03 11:36:03 +08002730
Nikolay Borisov8e560082020-08-12 16:18:51 +03002731 /*
2732 * fs_devices now represents the newly sprouted filesystem and
2733 * its fsid has been changed by btrfs_prepare_sprout
2734 */
2735 btrfs_sysfs_update_sprout_fsid(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002736 }
2737
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002738 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002739
2740 if (seeding_dev) {
2741 mutex_unlock(&uuid_mutex);
2742 up_write(&sb->s_umount);
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002743 locked = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002744
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002745 if (ret) /* transaction commit */
2746 return ret;
2747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002748 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002749 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002750 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002751 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002752 trans = btrfs_attach_transaction(root);
2753 if (IS_ERR(trans)) {
2754 if (PTR_ERR(trans) == -ENOENT)
2755 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002756 ret = PTR_ERR(trans);
2757 trans = NULL;
2758 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002759 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002760 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002761 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002762
Anand Jain7f551d92020-05-05 02:58:26 +08002763 /*
2764 * Now that we have written a new super block to this device, check all
2765 * other fs_devices list if device_path alienates any other scanned
2766 * device.
2767 * We can ignore the return value as it typically returns -EINVAL and
2768 * only succeeds if the device was an alien.
2769 */
2770 btrfs_forget_devices(device_path);
2771
2772 /* Update ctime/mtime for blkid or udev */
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002773 update_dev_time(device_path);
Anand Jain7f551d92020-05-05 02:58:26 +08002774
Chris Mason788f20e2008-04-28 15:29:42 -04002775 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002776
Anand Jaind31c32f2017-09-28 14:51:10 +08002777error_sysfs:
Anand Jain53f8a742020-09-05 01:34:27 +08002778 btrfs_sysfs_remove_device(device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002779 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2780 mutex_lock(&fs_info->chunk_mutex);
2781 list_del_rcu(&device->dev_list);
2782 list_del(&device->dev_alloc_list);
2783 fs_info->fs_devices->num_devices--;
2784 fs_info->fs_devices->open_devices--;
2785 fs_info->fs_devices->rw_devices--;
2786 fs_info->fs_devices->total_devices--;
2787 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2788 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2789 btrfs_set_super_total_bytes(fs_info->super_copy,
2790 orig_super_total_bytes);
2791 btrfs_set_super_num_devices(fs_info->super_copy,
2792 orig_super_num_devices);
2793 mutex_unlock(&fs_info->chunk_mutex);
2794 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002795error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002796 if (seeding_dev)
Filipe Mananaa0a1db72020-12-14 10:10:47 +00002797 btrfs_set_sb_rdonly(sb);
Anand Jain7132a262017-09-28 14:51:11 +08002798 if (trans)
2799 btrfs_end_transaction(trans);
Naohiro Aota5b316462020-11-10 20:26:07 +09002800error_free_zone:
2801 btrfs_destroy_dev_zone_info(device);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002802error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002803 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002804error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002805 blkdev_put(bdev, FMODE_EXCL);
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002806 if (locked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002807 mutex_unlock(&uuid_mutex);
2808 up_write(&sb->s_umount);
2809 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002810 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002811}
2812
Chris Masond3977122009-01-05 21:25:51 -05002813static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2814 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002815{
2816 int ret;
2817 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002818 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002819 struct btrfs_dev_item *dev_item;
2820 struct extent_buffer *leaf;
2821 struct btrfs_key key;
2822
Chris Mason0b86a832008-03-24 15:01:56 -04002823 path = btrfs_alloc_path();
2824 if (!path)
2825 return -ENOMEM;
2826
2827 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2828 key.type = BTRFS_DEV_ITEM_KEY;
2829 key.offset = device->devid;
2830
2831 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2832 if (ret < 0)
2833 goto out;
2834
2835 if (ret > 0) {
2836 ret = -ENOENT;
2837 goto out;
2838 }
2839
2840 leaf = path->nodes[0];
2841 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2842
2843 btrfs_set_device_id(leaf, dev_item, device->devid);
2844 btrfs_set_device_type(leaf, dev_item, device->type);
2845 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2846 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2847 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002848 btrfs_set_device_total_bytes(leaf, dev_item,
2849 btrfs_device_get_disk_total_bytes(device));
2850 btrfs_set_device_bytes_used(leaf, dev_item,
2851 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002852 btrfs_mark_buffer_dirty(leaf);
2853
2854out:
2855 btrfs_free_path(path);
2856 return ret;
2857}
2858
Miao Xie2196d6e2014-09-03 21:35:41 +08002859int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002860 struct btrfs_device *device, u64 new_size)
2861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002862 struct btrfs_fs_info *fs_info = device->fs_info;
2863 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002864 u64 old_total;
2865 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002866
Anand Jainebbede42017-12-04 12:54:52 +08002867 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002868 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002869
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002870 new_size = round_down(new_size, fs_info->sectorsize);
2871
David Sterba34441362016-10-04 19:34:27 +02002872 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002873 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002874 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002875
Stefan Behrens63a212a2012-11-05 18:29:28 +01002876 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002877 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002878 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002879 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002880 }
Yan Zheng2b820322008-11-17 21:11:30 -05002881
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002882 btrfs_set_super_total_bytes(super_copy,
2883 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002884 device->fs_devices->total_rw_bytes += diff;
2885
Miao Xie7cc8e582014-09-03 21:35:38 +08002886 btrfs_device_set_total_bytes(device, new_size);
2887 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002888 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002889 if (list_empty(&device->post_commit_list))
2890 list_add_tail(&device->post_commit_list,
2891 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002892 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002893
Chris Mason8f18cf12008-04-25 16:53:30 -04002894 return btrfs_update_device(trans, device);
2895}
2896
Nikolay Borisovf4208792018-07-20 19:37:52 +03002897static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002898{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002899 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002900 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002901 int ret;
2902 struct btrfs_path *path;
2903 struct btrfs_key key;
2904
Chris Mason8f18cf12008-04-25 16:53:30 -04002905 path = btrfs_alloc_path();
2906 if (!path)
2907 return -ENOMEM;
2908
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002909 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002910 key.offset = chunk_offset;
2911 key.type = BTRFS_CHUNK_ITEM_KEY;
2912
2913 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002914 if (ret < 0)
2915 goto out;
2916 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 btrfs_handle_fs_error(fs_info, -ENOENT,
2918 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002919 ret = -ENOENT;
2920 goto out;
2921 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002922
2923 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002924 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 btrfs_handle_fs_error(fs_info, ret,
2926 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002927out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002928 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002929 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002930}
2931
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002932static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002935 struct btrfs_disk_key *disk_key;
2936 struct btrfs_chunk *chunk;
2937 u8 *ptr;
2938 int ret = 0;
2939 u32 num_stripes;
2940 u32 array_size;
2941 u32 len = 0;
2942 u32 cur;
2943 struct btrfs_key key;
2944
David Sterba34441362016-10-04 19:34:27 +02002945 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002946 array_size = btrfs_super_sys_array_size(super_copy);
2947
2948 ptr = super_copy->sys_chunk_array;
2949 cur = 0;
2950
2951 while (cur < array_size) {
2952 disk_key = (struct btrfs_disk_key *)ptr;
2953 btrfs_disk_key_to_cpu(&key, disk_key);
2954
2955 len = sizeof(*disk_key);
2956
2957 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2958 chunk = (struct btrfs_chunk *)(ptr + len);
2959 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2960 len += btrfs_chunk_item_size(num_stripes);
2961 } else {
2962 ret = -EIO;
2963 break;
2964 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002965 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002966 key.offset == chunk_offset) {
2967 memmove(ptr, ptr + len, array_size - (cur + len));
2968 array_size -= len;
2969 btrfs_set_super_sys_array_size(super_copy, array_size);
2970 } else {
2971 ptr += len;
2972 cur += len;
2973 }
2974 }
David Sterba34441362016-10-04 19:34:27 +02002975 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002976 return ret;
2977}
2978
Omar Sandoval60ca8422018-05-16 16:34:31 -07002979/*
2980 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2981 * @logical: Logical block offset in bytes.
2982 * @length: Length of extent in bytes.
2983 *
2984 * Return: Chunk mapping or ERR_PTR.
2985 */
2986struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2987 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002988{
2989 struct extent_map_tree *em_tree;
2990 struct extent_map *em;
2991
David Sterbac8bf1b62019-05-17 11:43:17 +02002992 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002993 read_lock(&em_tree->lock);
2994 em = lookup_extent_mapping(em_tree, logical, length);
2995 read_unlock(&em_tree->lock);
2996
2997 if (!em) {
2998 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2999 logical, length);
3000 return ERR_PTR(-EINVAL);
3001 }
3002
3003 if (em->start > logical || em->start + em->len < logical) {
3004 btrfs_crit(fs_info,
3005 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
3006 logical, length, em->start, em->start + em->len);
3007 free_extent_map(em);
3008 return ERR_PTR(-EINVAL);
3009 }
3010
3011 /* callers are responsible for dropping em's ref. */
3012 return em;
3013}
3014
Nikolay Borisov97aff912018-07-20 19:37:53 +03003015int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04003016{
Nikolay Borisov97aff912018-07-20 19:37:53 +03003017 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003018 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003019 struct map_lookup *map;
3020 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003021 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003022 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003023
Omar Sandoval60ca8422018-05-16 16:34:31 -07003024 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07003025 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04003026 /*
3027 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08003028 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04003029 * do anything we still error out.
3030 */
3031 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07003032 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003033 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04003034 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02003035 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03003036 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02003037 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003038
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003039 /*
3040 * Take the device list mutex to prevent races with the final phase of
3041 * a device replace operation that replaces the device object associated
3042 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
3043 */
3044 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003045 for (i = 0; i < map->num_stripes; i++) {
3046 struct btrfs_device *device = map->stripes[i].dev;
3047 ret = btrfs_free_dev_extent(trans, device,
3048 map->stripes[i].physical,
3049 &dev_extent_len);
3050 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003051 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04003052 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003053 goto out;
3054 }
3055
3056 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003057 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003058 btrfs_device_set_bytes_used(device,
3059 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003060 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003061 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003062 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003063 }
3064
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003065 ret = btrfs_update_device(trans, device);
3066 if (ret) {
3067 mutex_unlock(&fs_devices->device_list_mutex);
3068 btrfs_abort_transaction(trans, ret);
3069 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003070 }
3071 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003072 mutex_unlock(&fs_devices->device_list_mutex);
3073
Nikolay Borisovf4208792018-07-20 19:37:52 +03003074 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003075 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003076 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003077 goto out;
3078 }
3079
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003080 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003081
3082 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003083 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003084 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003085 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003086 goto out;
3087 }
3088 }
3089
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003090 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003091 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003092 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003093 goto out;
3094 }
3095
Josef Bacik47ab2a62014-09-18 11:20:02 -04003096out:
3097 /* once for us */
3098 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003099 return ret;
3100}
3101
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003102static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003103{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003104 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003105 struct btrfs_trans_handle *trans;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003106 struct btrfs_block_group *block_group;
Chris Mason8f18cf12008-04-25 16:53:30 -04003107 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003108
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003109 /*
3110 * Prevent races with automatic removal of unused block groups.
3111 * After we relocate and before we remove the chunk with offset
3112 * chunk_offset, automatic removal of the block group can kick in,
3113 * resulting in a failure when calling btrfs_remove_chunk() below.
3114 *
3115 * Make sure to acquire this mutex before doing a tree search (dev
3116 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3117 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3118 * we release the path used to search the chunk/dev tree and before
3119 * the current task acquires this mutex and calls us.
3120 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003121 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003122
Chris Mason8f18cf12008-04-25 16:53:30 -04003123 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003124 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003125 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003126 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003127 if (ret)
3128 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003129
Dennis Zhoub0643e52019-12-13 16:22:14 -08003130 block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
3131 if (!block_group)
3132 return -ENOENT;
3133 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
3134 btrfs_put_block_group(block_group);
3135
Chris Mason19c4d2f2016-10-10 13:43:31 -07003136 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3137 chunk_offset);
3138 if (IS_ERR(trans)) {
3139 ret = PTR_ERR(trans);
3140 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3141 return ret;
3142 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003143
Chris Mason19c4d2f2016-10-10 13:43:31 -07003144 /*
3145 * step two, delete the device extents and the
3146 * chunk tree entries
3147 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003148 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003149 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003150 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003151}
3152
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003153static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003154{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003155 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003156 struct btrfs_path *path;
3157 struct extent_buffer *leaf;
3158 struct btrfs_chunk *chunk;
3159 struct btrfs_key key;
3160 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003161 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003162 bool retried = false;
3163 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003164 int ret;
3165
3166 path = btrfs_alloc_path();
3167 if (!path)
3168 return -ENOMEM;
3169
Josef Bacikba1bf482009-09-11 16:11:19 -04003170again:
Yan Zheng2b820322008-11-17 21:11:30 -05003171 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3172 key.offset = (u64)-1;
3173 key.type = BTRFS_CHUNK_ITEM_KEY;
3174
3175 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003176 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003177 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003178 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003179 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003180 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003181 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003182 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003183
3184 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3185 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003186 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003187 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003188 if (ret < 0)
3189 goto error;
3190 if (ret > 0)
3191 break;
3192
3193 leaf = path->nodes[0];
3194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3195
3196 chunk = btrfs_item_ptr(leaf, path->slots[0],
3197 struct btrfs_chunk);
3198 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003199 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003200
3201 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003202 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003203 if (ret == -ENOSPC)
3204 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303205 else
3206 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003207 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003208 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003209
3210 if (found_key.offset == 0)
3211 break;
3212 key.offset = found_key.offset - 1;
3213 }
3214 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003215 if (failed && !retried) {
3216 failed = 0;
3217 retried = true;
3218 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303219 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003220 ret = -ENOSPC;
3221 }
Yan Zheng2b820322008-11-17 21:11:30 -05003222error:
3223 btrfs_free_path(path);
3224 return ret;
3225}
3226
Liu Boa6f93c72017-11-15 16:28:11 -07003227/*
3228 * return 1 : allocate a data chunk successfully,
3229 * return <0: errors during allocating a data chunk,
3230 * return 0 : no need to allocate a data chunk.
3231 */
3232static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3233 u64 chunk_offset)
3234{
David Sterba32da53862019-10-29 19:20:18 +01003235 struct btrfs_block_group *cache;
Liu Boa6f93c72017-11-15 16:28:11 -07003236 u64 bytes_used;
3237 u64 chunk_type;
3238
3239 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3240 ASSERT(cache);
3241 chunk_type = cache->flags;
3242 btrfs_put_block_group(cache);
3243
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003244 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3245 return 0;
Liu Boa6f93c72017-11-15 16:28:11 -07003246
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003247 spin_lock(&fs_info->data_sinfo->lock);
3248 bytes_used = fs_info->data_sinfo->bytes_used;
3249 spin_unlock(&fs_info->data_sinfo->lock);
Liu Boa6f93c72017-11-15 16:28:11 -07003250
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003251 if (!bytes_used) {
3252 struct btrfs_trans_handle *trans;
3253 int ret;
Liu Boa6f93c72017-11-15 16:28:11 -07003254
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003255 trans = btrfs_join_transaction(fs_info->tree_root);
3256 if (IS_ERR(trans))
3257 return PTR_ERR(trans);
3258
3259 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3260 btrfs_end_transaction(trans);
3261 if (ret < 0)
3262 return ret;
3263 return 1;
Liu Boa6f93c72017-11-15 16:28:11 -07003264 }
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003265
Liu Boa6f93c72017-11-15 16:28:11 -07003266 return 0;
3267}
3268
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003269static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003270 struct btrfs_balance_control *bctl)
3271{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003272 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003273 struct btrfs_trans_handle *trans;
3274 struct btrfs_balance_item *item;
3275 struct btrfs_disk_balance_args disk_bargs;
3276 struct btrfs_path *path;
3277 struct extent_buffer *leaf;
3278 struct btrfs_key key;
3279 int ret, err;
3280
3281 path = btrfs_alloc_path();
3282 if (!path)
3283 return -ENOMEM;
3284
3285 trans = btrfs_start_transaction(root, 0);
3286 if (IS_ERR(trans)) {
3287 btrfs_free_path(path);
3288 return PTR_ERR(trans);
3289 }
3290
3291 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003292 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003293 key.offset = 0;
3294
3295 ret = btrfs_insert_empty_item(trans, root, path, &key,
3296 sizeof(*item));
3297 if (ret)
3298 goto out;
3299
3300 leaf = path->nodes[0];
3301 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3302
David Sterbab159fa22016-11-08 18:09:03 +01003303 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003304
3305 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3306 btrfs_set_balance_data(leaf, item, &disk_bargs);
3307 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3308 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3309 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3310 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3311
3312 btrfs_set_balance_flags(leaf, item, bctl->flags);
3313
3314 btrfs_mark_buffer_dirty(leaf);
3315out:
3316 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003317 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003318 if (err && !ret)
3319 ret = err;
3320 return ret;
3321}
3322
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003323static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003324{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003325 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003326 struct btrfs_trans_handle *trans;
3327 struct btrfs_path *path;
3328 struct btrfs_key key;
3329 int ret, err;
3330
3331 path = btrfs_alloc_path();
3332 if (!path)
3333 return -ENOMEM;
3334
David Sterba3502a8c2020-06-25 12:35:28 +02003335 trans = btrfs_start_transaction_fallback_global_rsv(root, 0);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003336 if (IS_ERR(trans)) {
3337 btrfs_free_path(path);
3338 return PTR_ERR(trans);
3339 }
3340
3341 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003342 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003343 key.offset = 0;
3344
3345 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3346 if (ret < 0)
3347 goto out;
3348 if (ret > 0) {
3349 ret = -ENOENT;
3350 goto out;
3351 }
3352
3353 ret = btrfs_del_item(trans, root, path);
3354out:
3355 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003356 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003357 if (err && !ret)
3358 ret = err;
3359 return ret;
3360}
3361
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003362/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003363 * This is a heuristic used to reduce the number of chunks balanced on
3364 * resume after balance was interrupted.
3365 */
3366static void update_balance_args(struct btrfs_balance_control *bctl)
3367{
3368 /*
3369 * Turn on soft mode for chunk types that were being converted.
3370 */
3371 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3372 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3373 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3374 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3375 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3376 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3377
3378 /*
3379 * Turn on usage filter if is not already used. The idea is
3380 * that chunks that we have already balanced should be
3381 * reasonably full. Don't do it for chunks that are being
3382 * converted - that will keep us from relocating unconverted
3383 * (albeit full) chunks.
3384 */
3385 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003386 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003387 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3388 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3389 bctl->data.usage = 90;
3390 }
3391 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003392 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003393 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3394 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3395 bctl->sys.usage = 90;
3396 }
3397 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003398 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003399 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3400 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3401 bctl->meta.usage = 90;
3402 }
3403}
3404
3405/*
David Sterba149196a2018-03-20 20:23:09 +01003406 * Clear the balance status in fs_info and delete the balance item from disk.
3407 */
3408static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003409{
3410 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003411 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003412
3413 BUG_ON(!fs_info->balance_ctl);
3414
3415 spin_lock(&fs_info->balance_lock);
3416 fs_info->balance_ctl = NULL;
3417 spin_unlock(&fs_info->balance_lock);
3418
3419 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003420 ret = del_balance_item(fs_info);
3421 if (ret)
3422 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003423}
3424
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003425/*
3426 * Balance filters. Return 1 if chunk should be filtered out
3427 * (should not be balanced).
3428 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003429static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003430 struct btrfs_balance_args *bargs)
3431{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003432 chunk_type = chunk_to_extended(chunk_type) &
3433 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003434
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003435 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003436 return 0;
3437
3438 return 1;
3439}
3440
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003441static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003442 struct btrfs_balance_args *bargs)
3443{
David Sterba32da53862019-10-29 19:20:18 +01003444 struct btrfs_block_group *cache;
David Sterbabc309462015-10-20 18:22:13 +02003445 u64 chunk_used;
3446 u64 user_thresh_min;
3447 u64 user_thresh_max;
3448 int ret = 1;
3449
3450 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003451 chunk_used = cache->used;
David Sterbabc309462015-10-20 18:22:13 +02003452
3453 if (bargs->usage_min == 0)
3454 user_thresh_min = 0;
3455 else
David Sterbab3470b52019-10-23 18:48:22 +02003456 user_thresh_min = div_factor_fine(cache->length,
3457 bargs->usage_min);
David Sterbabc309462015-10-20 18:22:13 +02003458
3459 if (bargs->usage_max == 0)
3460 user_thresh_max = 1;
3461 else if (bargs->usage_max > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003462 user_thresh_max = cache->length;
David Sterbabc309462015-10-20 18:22:13 +02003463 else
David Sterbab3470b52019-10-23 18:48:22 +02003464 user_thresh_max = div_factor_fine(cache->length,
3465 bargs->usage_max);
David Sterbabc309462015-10-20 18:22:13 +02003466
3467 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3468 ret = 0;
3469
3470 btrfs_put_block_group(cache);
3471 return ret;
3472}
3473
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003474static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003475 u64 chunk_offset, struct btrfs_balance_args *bargs)
3476{
David Sterba32da53862019-10-29 19:20:18 +01003477 struct btrfs_block_group *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003478 u64 chunk_used, user_thresh;
3479 int ret = 1;
3480
3481 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003482 chunk_used = cache->used;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003483
David Sterbabc309462015-10-20 18:22:13 +02003484 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003485 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003486 else if (bargs->usage > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003487 user_thresh = cache->length;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003488 else
David Sterbab3470b52019-10-23 18:48:22 +02003489 user_thresh = div_factor_fine(cache->length, bargs->usage);
Ilya Dryomova105bb82013-01-21 15:15:56 +02003490
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003491 if (chunk_used < user_thresh)
3492 ret = 0;
3493
3494 btrfs_put_block_group(cache);
3495 return ret;
3496}
3497
Ilya Dryomov409d4042012-01-16 22:04:47 +02003498static int chunk_devid_filter(struct extent_buffer *leaf,
3499 struct btrfs_chunk *chunk,
3500 struct btrfs_balance_args *bargs)
3501{
3502 struct btrfs_stripe *stripe;
3503 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3504 int i;
3505
3506 for (i = 0; i < num_stripes; i++) {
3507 stripe = btrfs_stripe_nr(chunk, i);
3508 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3509 return 0;
3510 }
3511
3512 return 1;
3513}
3514
David Sterba946c9252019-05-17 11:43:34 +02003515static u64 calc_data_stripes(u64 type, int num_stripes)
3516{
3517 const int index = btrfs_bg_flags_to_raid_index(type);
3518 const int ncopies = btrfs_raid_array[index].ncopies;
3519 const int nparity = btrfs_raid_array[index].nparity;
3520
3521 if (nparity)
3522 return num_stripes - nparity;
3523 else
3524 return num_stripes / ncopies;
3525}
3526
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003527/* [pstart, pend) */
3528static int chunk_drange_filter(struct extent_buffer *leaf,
3529 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003530 struct btrfs_balance_args *bargs)
3531{
3532 struct btrfs_stripe *stripe;
3533 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3534 u64 stripe_offset;
3535 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003536 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003537 int factor;
3538 int i;
3539
3540 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3541 return 0;
3542
David Sterba946c9252019-05-17 11:43:34 +02003543 type = btrfs_chunk_type(leaf, chunk);
3544 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003545
3546 for (i = 0; i < num_stripes; i++) {
3547 stripe = btrfs_stripe_nr(chunk, i);
3548 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3549 continue;
3550
3551 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3552 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003553 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003554
3555 if (stripe_offset < bargs->pend &&
3556 stripe_offset + stripe_length > bargs->pstart)
3557 return 0;
3558 }
3559
3560 return 1;
3561}
3562
Ilya Dryomovea671762012-01-16 22:04:48 +02003563/* [vstart, vend) */
3564static int chunk_vrange_filter(struct extent_buffer *leaf,
3565 struct btrfs_chunk *chunk,
3566 u64 chunk_offset,
3567 struct btrfs_balance_args *bargs)
3568{
3569 if (chunk_offset < bargs->vend &&
3570 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3571 /* at least part of the chunk is inside this vrange */
3572 return 0;
3573
3574 return 1;
3575}
3576
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003577static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3578 struct btrfs_chunk *chunk,
3579 struct btrfs_balance_args *bargs)
3580{
3581 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3582
3583 if (bargs->stripes_min <= num_stripes
3584 && num_stripes <= bargs->stripes_max)
3585 return 0;
3586
3587 return 1;
3588}
3589
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003590static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003591 struct btrfs_balance_args *bargs)
3592{
3593 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3594 return 0;
3595
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003596 chunk_type = chunk_to_extended(chunk_type) &
3597 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003598
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003599 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003600 return 1;
3601
3602 return 0;
3603}
3604
David Sterba6ec08962019-03-20 16:38:52 +01003605static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003606 struct btrfs_chunk *chunk, u64 chunk_offset)
3607{
David Sterba6ec08962019-03-20 16:38:52 +01003608 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003609 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003610 struct btrfs_balance_args *bargs = NULL;
3611 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3612
3613 /* type filter */
3614 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3615 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3616 return 0;
3617 }
3618
3619 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3620 bargs = &bctl->data;
3621 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3622 bargs = &bctl->sys;
3623 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3624 bargs = &bctl->meta;
3625
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003626 /* profiles filter */
3627 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3628 chunk_profiles_filter(chunk_type, bargs)) {
3629 return 0;
3630 }
3631
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003632 /* usage filter */
3633 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003634 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003635 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003636 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003637 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003638 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003639 }
3640
Ilya Dryomov409d4042012-01-16 22:04:47 +02003641 /* devid filter */
3642 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3643 chunk_devid_filter(leaf, chunk, bargs)) {
3644 return 0;
3645 }
3646
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003647 /* drange filter, makes sense only with devid filter */
3648 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003649 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003650 return 0;
3651 }
3652
Ilya Dryomovea671762012-01-16 22:04:48 +02003653 /* vrange filter */
3654 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3655 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3656 return 0;
3657 }
3658
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003659 /* stripes filter */
3660 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3661 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3662 return 0;
3663 }
3664
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003665 /* soft profile changing mode */
3666 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3667 chunk_soft_convert_filter(chunk_type, bargs)) {
3668 return 0;
3669 }
3670
David Sterba7d824b62014-05-07 17:37:51 +02003671 /*
3672 * limited by count, must be the last filter
3673 */
3674 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3675 if (bargs->limit == 0)
3676 return 0;
3677 else
3678 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003679 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3680 /*
3681 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003682 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003683 * about the count of all chunks that satisfy the filters.
3684 */
3685 if (bargs->limit_max == 0)
3686 return 0;
3687 else
3688 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003689 }
3690
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003691 return 1;
3692}
3693
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003694static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003695{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003696 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003697 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003698 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003699 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003700 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003701 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003702 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003703 struct extent_buffer *leaf;
3704 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003705 int ret;
3706 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003707 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003708 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003709 u64 limit_data = bctl->data.limit;
3710 u64 limit_meta = bctl->meta.limit;
3711 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003712 u32 count_data = 0;
3713 u32 count_meta = 0;
3714 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003715 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003716
Chris Masonec44a352008-04-28 15:29:52 -04003717 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003718 if (!path) {
3719 ret = -ENOMEM;
3720 goto error;
3721 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003722
3723 /* zero out stat counters */
3724 spin_lock(&fs_info->balance_lock);
3725 memset(&bctl->stat, 0, sizeof(bctl->stat));
3726 spin_unlock(&fs_info->balance_lock);
3727again:
David Sterba7d824b62014-05-07 17:37:51 +02003728 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003729 /*
3730 * The single value limit and min/max limits use the same bytes
3731 * in the
3732 */
David Sterba7d824b62014-05-07 17:37:51 +02003733 bctl->data.limit = limit_data;
3734 bctl->meta.limit = limit_meta;
3735 bctl->sys.limit = limit_sys;
3736 }
Chris Masonec44a352008-04-28 15:29:52 -04003737 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3738 key.offset = (u64)-1;
3739 key.type = BTRFS_CHUNK_ITEM_KEY;
3740
Chris Masond3977122009-01-05 21:25:51 -05003741 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003742 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003743 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003744 ret = -ECANCELED;
3745 goto error;
3746 }
3747
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003748 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003749 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003750 if (ret < 0) {
3751 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003752 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003753 }
Chris Masonec44a352008-04-28 15:29:52 -04003754
3755 /*
3756 * this shouldn't happen, it means the last relocate
3757 * failed
3758 */
3759 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003760 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003761
3762 ret = btrfs_previous_item(chunk_root, path, 0,
3763 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003764 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003765 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003766 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003767 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003768 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003769
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003770 leaf = path->nodes[0];
3771 slot = path->slots[0];
3772 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3773
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003774 if (found_key.objectid != key.objectid) {
3775 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003776 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003777 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003778
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003779 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003780 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003781
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003782 if (!counting) {
3783 spin_lock(&fs_info->balance_lock);
3784 bctl->stat.considered++;
3785 spin_unlock(&fs_info->balance_lock);
3786 }
3787
David Sterba6ec08962019-03-20 16:38:52 +01003788 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003789
David Sterbab3b4aa72011-04-21 01:20:15 +02003790 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003791 if (!ret) {
3792 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003793 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003794 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003795
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003796 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003797 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003798 spin_lock(&fs_info->balance_lock);
3799 bctl->stat.expected++;
3800 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003801
3802 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3803 count_data++;
3804 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3805 count_sys++;
3806 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3807 count_meta++;
3808
3809 goto loop;
3810 }
3811
3812 /*
3813 * Apply limit_min filter, no need to check if the LIMITS
3814 * filter is used, limit_min is 0 by default
3815 */
3816 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3817 count_data < bctl->data.limit_min)
3818 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3819 count_meta < bctl->meta.limit_min)
3820 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3821 count_sys < bctl->sys.limit_min)) {
3822 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003823 goto loop;
3824 }
3825
Liu Boa6f93c72017-11-15 16:28:11 -07003826 if (!chunk_reserved) {
3827 /*
3828 * We may be relocating the only data chunk we have,
3829 * which could potentially end up with losing data's
3830 * raid profile, so lets allocate an empty one in
3831 * advance.
3832 */
3833 ret = btrfs_may_alloc_data_chunk(fs_info,
3834 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003835 if (ret < 0) {
3836 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3837 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003838 } else if (ret == 1) {
3839 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003840 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003841 }
3842
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003843 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003844 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003845 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003846 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003847 } else if (ret == -ETXTBSY) {
3848 btrfs_info(fs_info,
3849 "skipping relocation of block group %llu due to active swapfile",
3850 found_key.offset);
3851 ret = 0;
3852 } else if (ret) {
3853 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003854 } else {
3855 spin_lock(&fs_info->balance_lock);
3856 bctl->stat.completed++;
3857 spin_unlock(&fs_info->balance_lock);
3858 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003859loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003860 if (found_key.offset == 0)
3861 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003862 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003863 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003864
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003865 if (counting) {
3866 btrfs_release_path(path);
3867 counting = false;
3868 goto again;
3869 }
Chris Masonec44a352008-04-28 15:29:52 -04003870error:
3871 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003872 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003873 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003874 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003875 if (!ret)
3876 ret = -ENOSPC;
3877 }
3878
Chris Masonec44a352008-04-28 15:29:52 -04003879 return ret;
3880}
3881
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003882/**
3883 * alloc_profile_is_valid - see if a given profile is valid and reduced
3884 * @flags: profile to validate
3885 * @extended: if true @flags is treated as an extended profile
3886 */
3887static int alloc_profile_is_valid(u64 flags, int extended)
3888{
3889 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3890 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3891
3892 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3893
3894 /* 1) check that all other bits are zeroed */
3895 if (flags & ~mask)
3896 return 0;
3897
3898 /* 2) see if profile is reduced */
3899 if (flags == 0)
3900 return !extended; /* "0" is valid for usual profiles */
3901
David Sterbac1499162019-10-01 19:44:42 +02003902 return has_single_bit_set(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003903}
3904
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003905static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3906{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003907 /* cancel requested || normal exit path */
3908 return atomic_read(&fs_info->balance_cancel_req) ||
3909 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3910 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003911}
3912
David Sterba5ba366c2020-02-27 21:00:52 +01003913/*
3914 * Validate target profile against allowed profiles and return true if it's OK.
3915 * Otherwise print the error message and return false.
3916 */
3917static inline int validate_convert_profile(struct btrfs_fs_info *fs_info,
3918 const struct btrfs_balance_args *bargs,
3919 u64 allowed, const char *type)
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003920{
David Sterba5ba366c2020-02-27 21:00:52 +01003921 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3922 return true;
3923
3924 /* Profile is valid and does not have bits outside of the allowed set */
3925 if (alloc_profile_is_valid(bargs->target, 1) &&
3926 (bargs->target & ~allowed) == 0)
3927 return true;
3928
3929 btrfs_err(fs_info, "balance: invalid convert %s profile %s",
3930 type, btrfs_bg_type_to_raid_name(bargs->target));
3931 return false;
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003932}
3933
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003934/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003935 * Fill @buf with textual description of balance filter flags @bargs, up to
3936 * @size_buf including the terminating null. The output may be trimmed if it
3937 * does not fit into the provided buffer.
3938 */
3939static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3940 u32 size_buf)
3941{
3942 int ret;
3943 u32 size_bp = size_buf;
3944 char *bp = buf;
3945 u64 flags = bargs->flags;
3946 char tmp_buf[128] = {'\0'};
3947
3948 if (!flags)
3949 return;
3950
3951#define CHECK_APPEND_NOARG(a) \
3952 do { \
3953 ret = snprintf(bp, size_bp, (a)); \
3954 if (ret < 0 || ret >= size_bp) \
3955 goto out_overflow; \
3956 size_bp -= ret; \
3957 bp += ret; \
3958 } while (0)
3959
3960#define CHECK_APPEND_1ARG(a, v1) \
3961 do { \
3962 ret = snprintf(bp, size_bp, (a), (v1)); \
3963 if (ret < 0 || ret >= size_bp) \
3964 goto out_overflow; \
3965 size_bp -= ret; \
3966 bp += ret; \
3967 } while (0)
3968
3969#define CHECK_APPEND_2ARG(a, v1, v2) \
3970 do { \
3971 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3972 if (ret < 0 || ret >= size_bp) \
3973 goto out_overflow; \
3974 size_bp -= ret; \
3975 bp += ret; \
3976 } while (0)
3977
David Sterba158da512019-05-17 11:43:41 +02003978 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3979 CHECK_APPEND_1ARG("convert=%s,",
3980 btrfs_bg_type_to_raid_name(bargs->target));
Anand Jain56fc37d2018-11-20 16:12:56 +08003981
3982 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3983 CHECK_APPEND_NOARG("soft,");
3984
3985 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3986 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3987 sizeof(tmp_buf));
3988 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3989 }
3990
3991 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3992 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3993
3994 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3995 CHECK_APPEND_2ARG("usage=%u..%u,",
3996 bargs->usage_min, bargs->usage_max);
3997
3998 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3999 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
4000
4001 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
4002 CHECK_APPEND_2ARG("drange=%llu..%llu,",
4003 bargs->pstart, bargs->pend);
4004
4005 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
4006 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
4007 bargs->vstart, bargs->vend);
4008
4009 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
4010 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
4011
4012 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
4013 CHECK_APPEND_2ARG("limit=%u..%u,",
4014 bargs->limit_min, bargs->limit_max);
4015
4016 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
4017 CHECK_APPEND_2ARG("stripes=%u..%u,",
4018 bargs->stripes_min, bargs->stripes_max);
4019
4020#undef CHECK_APPEND_2ARG
4021#undef CHECK_APPEND_1ARG
4022#undef CHECK_APPEND_NOARG
4023
4024out_overflow:
4025
4026 if (size_bp < size_buf)
4027 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
4028 else
4029 buf[0] = '\0';
4030}
4031
4032static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
4033{
4034 u32 size_buf = 1024;
4035 char tmp_buf[192] = {'\0'};
4036 char *buf;
4037 char *bp;
4038 u32 size_bp = size_buf;
4039 int ret;
4040 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
4041
4042 buf = kzalloc(size_buf, GFP_KERNEL);
4043 if (!buf)
4044 return;
4045
4046 bp = buf;
4047
4048#define CHECK_APPEND_1ARG(a, v1) \
4049 do { \
4050 ret = snprintf(bp, size_bp, (a), (v1)); \
4051 if (ret < 0 || ret >= size_bp) \
4052 goto out_overflow; \
4053 size_bp -= ret; \
4054 bp += ret; \
4055 } while (0)
4056
4057 if (bctl->flags & BTRFS_BALANCE_FORCE)
4058 CHECK_APPEND_1ARG("%s", "-f ");
4059
4060 if (bctl->flags & BTRFS_BALANCE_DATA) {
4061 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4062 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4063 }
4064
4065 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4066 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4067 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4068 }
4069
4070 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4071 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4072 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4073 }
4074
4075#undef CHECK_APPEND_1ARG
4076
4077out_overflow:
4078
4079 if (size_bp < size_buf)
4080 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4081 btrfs_info(fs_info, "balance: %s %s",
4082 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4083 "resume" : "start", buf);
4084
4085 kfree(buf);
4086}
4087
4088/*
David Sterbadccdb072018-03-21 00:20:05 +01004089 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004090 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004091int btrfs_balance(struct btrfs_fs_info *fs_info,
4092 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004093 struct btrfs_ioctl_balance_args *bargs)
4094{
Adam Borowski14506122017-03-07 23:34:44 +01004095 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004096 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004097 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004098 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004099 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004100 unsigned seq;
Anand Jaine62869b2019-09-25 14:29:28 +08004101 bool reducing_redundancy;
David Sterba081db892019-05-17 11:43:27 +02004102 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004103
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004104 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004105 atomic_read(&fs_info->balance_pause_req) ||
Qu Wenruo726a3422020-02-17 14:16:52 +08004106 btrfs_should_cancel_balance(fs_info)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004107 ret = -EINVAL;
4108 goto out;
4109 }
4110
Ilya Dryomove4837f82012-03-27 17:09:17 +03004111 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4112 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4113 mixed = 1;
4114
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004115 /*
4116 * In case of mixed groups both data and meta should be picked,
4117 * and identical options should be given for both of them.
4118 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004119 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4120 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004121 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4122 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4123 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004124 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004125 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004126 ret = -EINVAL;
4127 goto out;
4128 }
4129 }
4130
Josef Bacikb35cf1f2020-01-10 11:11:24 -05004131 /*
4132 * rw_devices will not change at the moment, device add/delete/replace
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004133 * are exclusive
Josef Bacikb35cf1f2020-01-10 11:11:24 -05004134 */
4135 num_devices = fs_info->fs_devices->rw_devices;
Qu Wenruofab27352019-09-25 10:13:27 +08004136
4137 /*
4138 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4139 * special bit for it, to make it easier to distinguish. Thus we need
4140 * to set it manually, or balance would refuse the profile.
4141 */
4142 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
David Sterba081db892019-05-17 11:43:27 +02004143 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4144 if (num_devices >= btrfs_raid_array[i].devs_min)
4145 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08004146
David Sterba5ba366c2020-02-27 21:00:52 +01004147 if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") ||
4148 !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") ||
4149 !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004150 ret = -EINVAL;
4151 goto out;
4152 }
4153
David Sterba6079e122019-05-17 11:43:29 +02004154 /*
4155 * Allow to reduce metadata or system integrity only if force set for
4156 * profiles with redundancy (copies, parity)
4157 */
4158 allowed = 0;
4159 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4160 if (btrfs_raid_array[i].ncopies >= 2 ||
4161 btrfs_raid_array[i].tolerated_failures >= 1)
4162 allowed |= btrfs_raid_array[i].bg_flag;
4163 }
Miao Xiede98ced2013-01-29 10:13:12 +00004164 do {
4165 seq = read_seqbegin(&fs_info->profiles_lock);
4166
4167 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4168 (fs_info->avail_system_alloc_bits & allowed) &&
4169 !(bctl->sys.target & allowed)) ||
4170 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4171 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004172 !(bctl->meta.target & allowed)))
Anand Jaine62869b2019-09-25 14:29:28 +08004173 reducing_redundancy = true;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004174 else
Anand Jaine62869b2019-09-25 14:29:28 +08004175 reducing_redundancy = false;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004176
4177 /* if we're not converting, the target field is uninitialized */
4178 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4179 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4180 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4181 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004182 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004183
Anand Jaine62869b2019-09-25 14:29:28 +08004184 if (reducing_redundancy) {
Filipe Manana5a8067c2018-11-19 09:48:12 +00004185 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4186 btrfs_info(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004187 "balance: force reducing metadata redundancy");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004188 } else {
4189 btrfs_err(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004190 "balance: reduces metadata redundancy, use --force if you want this");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004191 ret = -EINVAL;
4192 goto out;
4193 }
4194 }
4195
Adam Borowski14506122017-03-07 23:34:44 +01004196 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4197 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00004198 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004199 "balance: metadata profile %s has lower redundancy than data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004200 btrfs_bg_type_to_raid_name(meta_target),
4201 btrfs_bg_type_to_raid_name(data_target));
Sam Tygieree592d02016-01-06 08:46:12 +00004202 }
4203
Filipe Manana9e967492019-04-22 16:44:09 +01004204 if (fs_info->send_in_progress) {
4205 btrfs_warn_rl(fs_info,
4206"cannot run balance while send operations are in progress (%d in progress)",
4207 fs_info->send_in_progress);
4208 ret = -EAGAIN;
4209 goto out;
4210 }
4211
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004212 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004213 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004214 goto out;
4215
Ilya Dryomov59641012012-01-16 22:04:48 +02004216 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4217 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004218 BUG_ON(fs_info->balance_ctl);
4219 spin_lock(&fs_info->balance_lock);
4220 fs_info->balance_ctl = bctl;
4221 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004222 } else {
4223 BUG_ON(ret != -EEXIST);
4224 spin_lock(&fs_info->balance_lock);
4225 update_balance_args(bctl);
4226 spin_unlock(&fs_info->balance_lock);
4227 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004228
David Sterba3009a622018-03-21 01:31:04 +01004229 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4230 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004231 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004232 mutex_unlock(&fs_info->balance_mutex);
4233
4234 ret = __btrfs_balance(fs_info);
4235
4236 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004237 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4238 btrfs_info(fs_info, "balance: paused");
Qu Wenruo44d354a2020-07-13 09:03:21 +08004239 /*
4240 * Balance can be canceled by:
4241 *
4242 * - Regular cancel request
4243 * Then ret == -ECANCELED and balance_cancel_req > 0
4244 *
4245 * - Fatal signal to "btrfs" process
4246 * Either the signal caught by wait_reserve_ticket() and callers
4247 * got -EINTR, or caught by btrfs_should_cancel_balance() and
4248 * got -ECANCELED.
4249 * Either way, in this case balance_cancel_req = 0, and
4250 * ret == -EINTR or ret == -ECANCELED.
4251 *
4252 * So here we only check the return value to catch canceled balance.
4253 */
4254 else if (ret == -ECANCELED || ret == -EINTR)
Anand Jain7333bd02018-11-20 16:12:57 +08004255 btrfs_info(fs_info, "balance: canceled");
4256 else
4257 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4258
David Sterba3009a622018-03-21 01:31:04 +01004259 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004260
4261 if (bargs) {
4262 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004263 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004264 }
4265
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004266 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4267 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004268 reset_balance_state(fs_info);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004269 btrfs_exclop_finish(fs_info);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004270 }
4271
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004272 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004273
4274 return ret;
4275out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004276 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004277 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004278 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004279 kfree(bctl);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004280 btrfs_exclop_finish(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004281
Ilya Dryomov59641012012-01-16 22:04:48 +02004282 return ret;
4283}
4284
4285static int balance_kthread(void *data)
4286{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004287 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004288 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004289
Ilya Dryomov59641012012-01-16 22:04:48 +02004290 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004291 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004292 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004293 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004294
Ilya Dryomov59641012012-01-16 22:04:48 +02004295 return ret;
4296}
4297
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004298int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4299{
4300 struct task_struct *tsk;
4301
David Sterba1354e1a2018-03-21 02:29:13 +01004302 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004303 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004304 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004305 return 0;
4306 }
David Sterba1354e1a2018-03-21 02:29:13 +01004307 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004308
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004309 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004310 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004311 return 0;
4312 }
4313
Anand Jain02ee6542018-05-17 15:16:51 +08004314 /*
4315 * A ro->rw remount sequence should continue with the paused balance
4316 * regardless of who pauses it, system or the user as of now, so set
4317 * the resume flag.
4318 */
4319 spin_lock(&fs_info->balance_lock);
4320 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4321 spin_unlock(&fs_info->balance_lock);
4322
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004323 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304324 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004325}
4326
Ilya Dryomov68310a52012-06-22 12:24:12 -06004327int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004328{
Ilya Dryomov59641012012-01-16 22:04:48 +02004329 struct btrfs_balance_control *bctl;
4330 struct btrfs_balance_item *item;
4331 struct btrfs_disk_balance_args disk_bargs;
4332 struct btrfs_path *path;
4333 struct extent_buffer *leaf;
4334 struct btrfs_key key;
4335 int ret;
4336
4337 path = btrfs_alloc_path();
4338 if (!path)
4339 return -ENOMEM;
4340
Ilya Dryomov68310a52012-06-22 12:24:12 -06004341 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004342 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004343 key.offset = 0;
4344
4345 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4346 if (ret < 0)
4347 goto out;
4348 if (ret > 0) { /* ret = -ENOENT; */
4349 ret = 0;
4350 goto out;
4351 }
4352
Ilya Dryomov59641012012-01-16 22:04:48 +02004353 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4354 if (!bctl) {
4355 ret = -ENOMEM;
4356 goto out;
4357 }
4358
Ilya Dryomov59641012012-01-16 22:04:48 +02004359 leaf = path->nodes[0];
4360 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4361
Ilya Dryomov68310a52012-06-22 12:24:12 -06004362 bctl->flags = btrfs_balance_flags(leaf, item);
4363 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004364
4365 btrfs_balance_data(leaf, item, &disk_bargs);
4366 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4367 btrfs_balance_meta(leaf, item, &disk_bargs);
4368 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4369 btrfs_balance_sys(leaf, item, &disk_bargs);
4370 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4371
David Sterbaeee95e32018-03-20 20:07:58 +01004372 /*
4373 * This should never happen, as the paused balance state is recovered
4374 * during mount without any chance of other exclusive ops to collide.
4375 *
4376 * This gives the exclusive op status to balance and keeps in paused
4377 * state until user intervention (cancel or umount). If the ownership
4378 * cannot be assigned, show a message but do not fail. The balance
4379 * is in a paused state and must have fs_info::balance_ctl properly
4380 * set up.
4381 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004382 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
David Sterbaeee95e32018-03-20 20:07:58 +01004383 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004384 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004385
Josef Bacikfb286102020-12-16 11:22:14 -05004386 btrfs_release_path(path);
4387
Ilya Dryomov68310a52012-06-22 12:24:12 -06004388 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004389 BUG_ON(fs_info->balance_ctl);
4390 spin_lock(&fs_info->balance_lock);
4391 fs_info->balance_ctl = bctl;
4392 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004393 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004394out:
4395 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004396 return ret;
4397}
4398
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004399int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4400{
4401 int ret = 0;
4402
4403 mutex_lock(&fs_info->balance_mutex);
4404 if (!fs_info->balance_ctl) {
4405 mutex_unlock(&fs_info->balance_mutex);
4406 return -ENOTCONN;
4407 }
4408
David Sterba3009a622018-03-21 01:31:04 +01004409 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004410 atomic_inc(&fs_info->balance_pause_req);
4411 mutex_unlock(&fs_info->balance_mutex);
4412
4413 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004414 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004415
4416 mutex_lock(&fs_info->balance_mutex);
4417 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004418 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004419 atomic_dec(&fs_info->balance_pause_req);
4420 } else {
4421 ret = -ENOTCONN;
4422 }
4423
4424 mutex_unlock(&fs_info->balance_mutex);
4425 return ret;
4426}
4427
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004428int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4429{
4430 mutex_lock(&fs_info->balance_mutex);
4431 if (!fs_info->balance_ctl) {
4432 mutex_unlock(&fs_info->balance_mutex);
4433 return -ENOTCONN;
4434 }
4435
David Sterbacf7d20f2018-03-21 01:45:32 +01004436 /*
4437 * A paused balance with the item stored on disk can be resumed at
4438 * mount time if the mount is read-write. Otherwise it's still paused
4439 * and we must not allow cancelling as it deletes the item.
4440 */
4441 if (sb_rdonly(fs_info->sb)) {
4442 mutex_unlock(&fs_info->balance_mutex);
4443 return -EROFS;
4444 }
4445
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004446 atomic_inc(&fs_info->balance_cancel_req);
4447 /*
4448 * if we are running just wait and return, balance item is
4449 * deleted in btrfs_balance in this case
4450 */
David Sterba3009a622018-03-21 01:31:04 +01004451 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004452 mutex_unlock(&fs_info->balance_mutex);
4453 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004454 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004455 mutex_lock(&fs_info->balance_mutex);
4456 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004457 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004458 /*
4459 * Lock released to allow other waiters to continue, we'll
4460 * reexamine the status again.
4461 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004462 mutex_lock(&fs_info->balance_mutex);
4463
David Sterbaa17c95d2018-03-20 17:28:05 +01004464 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004465 reset_balance_state(fs_info);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004466 btrfs_exclop_finish(fs_info);
Anand Jain6dac13f2018-05-16 10:51:26 +08004467 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004468 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004469 }
4470
David Sterba3009a622018-03-21 01:31:04 +01004471 BUG_ON(fs_info->balance_ctl ||
4472 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004473 atomic_dec(&fs_info->balance_cancel_req);
4474 mutex_unlock(&fs_info->balance_mutex);
4475 return 0;
4476}
4477
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02004478int btrfs_uuid_scan_kthread(void *data)
Stefan Behrens803b2f52013-08-15 17:11:21 +02004479{
4480 struct btrfs_fs_info *fs_info = data;
4481 struct btrfs_root *root = fs_info->tree_root;
4482 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004483 struct btrfs_path *path = NULL;
4484 int ret = 0;
4485 struct extent_buffer *eb;
4486 int slot;
4487 struct btrfs_root_item root_item;
4488 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004489 struct btrfs_trans_handle *trans = NULL;
Josef Bacikc94bec22020-02-14 15:05:01 -05004490 bool closing = false;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004491
4492 path = btrfs_alloc_path();
4493 if (!path) {
4494 ret = -ENOMEM;
4495 goto out;
4496 }
4497
4498 key.objectid = 0;
4499 key.type = BTRFS_ROOT_ITEM_KEY;
4500 key.offset = 0;
4501
Stefan Behrens803b2f52013-08-15 17:11:21 +02004502 while (1) {
Josef Bacikc94bec22020-02-14 15:05:01 -05004503 if (btrfs_fs_closing(fs_info)) {
4504 closing = true;
4505 break;
4506 }
Anand Jain7c829b72018-03-07 17:29:18 +08004507 ret = btrfs_search_forward(root, &key, path,
4508 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004509 if (ret) {
4510 if (ret > 0)
4511 ret = 0;
4512 break;
4513 }
4514
4515 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4516 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4517 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4518 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4519 goto skip;
4520
4521 eb = path->nodes[0];
4522 slot = path->slots[0];
4523 item_size = btrfs_item_size_nr(eb, slot);
4524 if (item_size < sizeof(root_item))
4525 goto skip;
4526
Stefan Behrens803b2f52013-08-15 17:11:21 +02004527 read_extent_buffer(eb, &root_item,
4528 btrfs_item_ptr_offset(eb, slot),
4529 (int)sizeof(root_item));
4530 if (btrfs_root_refs(&root_item) == 0)
4531 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004532
4533 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4534 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4535 if (trans)
4536 goto update_tree;
4537
4538 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004539 /*
4540 * 1 - subvol uuid item
4541 * 1 - received_subvol uuid item
4542 */
4543 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4544 if (IS_ERR(trans)) {
4545 ret = PTR_ERR(trans);
4546 break;
4547 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004548 continue;
4549 } else {
4550 goto skip;
4551 }
4552update_tree:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004553 btrfs_release_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004554 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004555 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004556 BTRFS_UUID_KEY_SUBVOL,
4557 key.objectid);
4558 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004559 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004560 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004561 break;
4562 }
4563 }
4564
4565 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004566 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004567 root_item.received_uuid,
4568 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4569 key.objectid);
4570 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004571 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004572 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004573 break;
4574 }
4575 }
4576
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004577skip:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004578 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004579 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004580 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004581 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004582 if (ret)
4583 break;
4584 }
4585
Stefan Behrens803b2f52013-08-15 17:11:21 +02004586 if (key.offset < (u64)-1) {
4587 key.offset++;
4588 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4589 key.offset = 0;
4590 key.type = BTRFS_ROOT_ITEM_KEY;
4591 } else if (key.objectid < (u64)-1) {
4592 key.offset = 0;
4593 key.type = BTRFS_ROOT_ITEM_KEY;
4594 key.objectid++;
4595 } else {
4596 break;
4597 }
4598 cond_resched();
4599 }
4600
4601out:
4602 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004603 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004604 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004605 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004606 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Josef Bacikc94bec22020-02-14 15:05:01 -05004607 else if (!closing)
Josef Bacikafcdd122016-09-02 15:40:02 -04004608 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004609 up(&fs_info->uuid_tree_rescan_sem);
4610 return 0;
4611}
4612
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004613int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4614{
4615 struct btrfs_trans_handle *trans;
4616 struct btrfs_root *tree_root = fs_info->tree_root;
4617 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004618 struct task_struct *task;
4619 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004620
4621 /*
4622 * 1 - root node
4623 * 1 - root item
4624 */
4625 trans = btrfs_start_transaction(tree_root, 2);
4626 if (IS_ERR(trans))
4627 return PTR_ERR(trans);
4628
David Sterba9b7a2442019-03-20 13:20:49 +01004629 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004630 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004631 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004632 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004633 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004634 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004635 }
4636
4637 fs_info->uuid_root = uuid_root;
4638
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004639 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004640 if (ret)
4641 return ret;
4642
4643 down(&fs_info->uuid_tree_rescan_sem);
4644 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4645 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004646 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004647 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004648 up(&fs_info->uuid_tree_rescan_sem);
4649 return PTR_ERR(task);
4650 }
4651
4652 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004653}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004654
Chris Mason8f18cf12008-04-25 16:53:30 -04004655/*
4656 * shrinking a device means finding all of the device extents past
4657 * the new size, and then following the back refs to the chunks.
4658 * The chunk relocation code actually frees the device extent
4659 */
4660int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4661{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004662 struct btrfs_fs_info *fs_info = device->fs_info;
4663 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004664 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004665 struct btrfs_dev_extent *dev_extent = NULL;
4666 struct btrfs_path *path;
4667 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004668 u64 chunk_offset;
4669 int ret;
4670 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004671 int failed = 0;
4672 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004673 struct extent_buffer *l;
4674 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004675 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004676 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004677 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004678 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004679 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004680
4681 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004682 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004683 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004684
Anand Jain401e29c2017-12-04 12:54:55 +08004685 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004686 return -EINVAL;
4687
Chris Mason8f18cf12008-04-25 16:53:30 -04004688 path = btrfs_alloc_path();
4689 if (!path)
4690 return -ENOMEM;
4691
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004692 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004693
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004694 trans = btrfs_start_transaction(root, 0);
4695 if (IS_ERR(trans)) {
4696 btrfs_free_path(path);
4697 return PTR_ERR(trans);
4698 }
4699
David Sterba34441362016-10-04 19:34:27 +02004700 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004701
Miao Xie7cc8e582014-09-03 21:35:38 +08004702 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004703 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004704 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004705 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004706 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004707
4708 /*
4709 * Once the device's size has been set to the new size, ensure all
4710 * in-memory chunks are synced to disk so that the loop below sees them
4711 * and relocates them accordingly.
4712 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004713 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004714 mutex_unlock(&fs_info->chunk_mutex);
4715 ret = btrfs_commit_transaction(trans);
4716 if (ret)
4717 goto done;
4718 } else {
4719 mutex_unlock(&fs_info->chunk_mutex);
4720 btrfs_end_transaction(trans);
4721 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004722
Josef Bacikba1bf482009-09-11 16:11:19 -04004723again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004724 key.objectid = device->devid;
4725 key.offset = (u64)-1;
4726 key.type = BTRFS_DEV_EXTENT_KEY;
4727
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004728 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004729 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004730 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004731 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004732 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004733 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004734 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004735
4736 ret = btrfs_previous_item(root, path, 0, key.type);
Chris Mason8f18cf12008-04-25 16:53:30 -04004737 if (ret) {
Nikolay Borisov7056bf62020-12-17 15:21:16 +02004738 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4739 if (ret < 0)
4740 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004741 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004742 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004743 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004744 }
4745
4746 l = path->nodes[0];
4747 slot = path->slots[0];
4748 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4749
Josef Bacikba1bf482009-09-11 16:11:19 -04004750 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004752 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004753 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004754 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004755
4756 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4757 length = btrfs_dev_extent_length(l, dev_extent);
4758
Josef Bacikba1bf482009-09-11 16:11:19 -04004759 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004760 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004761 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004762 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004763 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004764
Chris Mason8f18cf12008-04-25 16:53:30 -04004765 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004766 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004767
Liu Boa6f93c72017-11-15 16:28:11 -07004768 /*
4769 * We may be relocating the only data chunk we have,
4770 * which could potentially end up with losing data's
4771 * raid profile, so lets allocate an empty one in
4772 * advance.
4773 */
4774 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4775 if (ret < 0) {
4776 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4777 goto done;
4778 }
4779
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004780 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4781 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004782 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004783 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004784 } else if (ret) {
4785 if (ret == -ETXTBSY) {
4786 btrfs_warn(fs_info,
4787 "could not shrink block group %llu due to active swapfile",
4788 chunk_offset);
4789 }
4790 goto done;
4791 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004792 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004793
4794 if (failed && !retried) {
4795 failed = 0;
4796 retried = true;
4797 goto again;
4798 } else if (failed && retried) {
4799 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004800 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004801 }
4802
Chris Balld6397ba2009-04-27 07:29:03 -04004803 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004804 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004805 if (IS_ERR(trans)) {
4806 ret = PTR_ERR(trans);
4807 goto done;
4808 }
4809
David Sterba34441362016-10-04 19:34:27 +02004810 mutex_lock(&fs_info->chunk_mutex);
Qu Wenruoc57dd1f2020-07-31 19:29:11 +08004811 /* Clear all state bits beyond the shrunk device size */
4812 clear_extent_bits(&device->alloc_state, new_size, (u64)-1,
4813 CHUNK_STATE_MASK);
4814
Miao Xie7cc8e582014-09-03 21:35:38 +08004815 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004816 if (list_empty(&device->post_commit_list))
4817 list_add_tail(&device->post_commit_list,
4818 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004819
Chris Balld6397ba2009-04-27 07:29:03 -04004820 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004821 btrfs_set_super_total_bytes(super_copy,
4822 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004823 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004824
4825 /* Now btrfs_update_device() will change the on-disk size. */
4826 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004827 if (ret < 0) {
4828 btrfs_abort_transaction(trans, ret);
4829 btrfs_end_transaction(trans);
4830 } else {
4831 ret = btrfs_commit_transaction(trans);
4832 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004833done:
4834 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004835 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004836 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004837 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004838 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004839 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004840 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004841 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004842 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004843 return ret;
4844}
4845
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004846static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004847 struct btrfs_key *key,
4848 struct btrfs_chunk *chunk, int item_size)
4849{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004850 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004851 struct btrfs_disk_key disk_key;
4852 u32 array_size;
4853 u8 *ptr;
4854
David Sterba34441362016-10-04 19:34:27 +02004855 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004856 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004857 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004858 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004859 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004860 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004861 }
Chris Mason0b86a832008-03-24 15:01:56 -04004862
4863 ptr = super_copy->sys_chunk_array + array_size;
4864 btrfs_cpu_key_to_disk(&disk_key, key);
4865 memcpy(ptr, &disk_key, sizeof(disk_key));
4866 ptr += sizeof(disk_key);
4867 memcpy(ptr, chunk, item_size);
4868 item_size += sizeof(disk_key);
4869 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004870 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004871
Chris Mason0b86a832008-03-24 15:01:56 -04004872 return 0;
4873}
4874
Miao Xieb2117a32011-01-05 10:07:28 +00004875/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004876 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004877 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004878static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004879{
Arne Jansen73c5de02011-04-12 12:07:57 +02004880 const struct btrfs_device_info *di_a = a;
4881 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004882
Arne Jansen73c5de02011-04-12 12:07:57 +02004883 if (di_a->max_avail > di_b->max_avail)
4884 return -1;
4885 if (di_a->max_avail < di_b->max_avail)
4886 return 1;
4887 if (di_a->total_avail > di_b->total_avail)
4888 return -1;
4889 if (di_a->total_avail < di_b->total_avail)
4890 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004891 return 0;
4892}
4893
David Woodhouse53b381b2013-01-29 18:40:14 -05004894static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4895{
Zhao Leiffe2d202015-01-20 15:11:44 +08004896 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004897 return;
4898
Miao Xieceda0862013-04-11 10:30:16 +00004899 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004900}
4901
David Sterbacfbb8252018-07-10 18:15:05 +02004902static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4903{
4904 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4905 return;
4906
4907 btrfs_set_fs_incompat(info, RAID1C34);
4908}
4909
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004910/*
4911 * Structure used internally for __btrfs_alloc_chunk() function.
4912 * Wraps needed parameters.
4913 */
4914struct alloc_chunk_ctl {
4915 u64 start;
4916 u64 type;
4917 /* Total number of stripes to allocate */
4918 int num_stripes;
4919 /* sub_stripes info for map */
4920 int sub_stripes;
4921 /* Stripes per device */
4922 int dev_stripes;
4923 /* Maximum number of devices to use */
4924 int devs_max;
4925 /* Minimum number of devices to use */
4926 int devs_min;
4927 /* ndevs has to be a multiple of this */
4928 int devs_increment;
4929 /* Number of copies */
4930 int ncopies;
4931 /* Number of stripes worth of bytes to store parity information */
4932 int nparity;
4933 u64 max_stripe_size;
4934 u64 max_chunk_size;
Naohiro Aota6aafb302020-02-25 12:56:15 +09004935 u64 dev_extent_min;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004936 u64 stripe_size;
4937 u64 chunk_size;
4938 int ndevs;
4939};
4940
Naohiro Aota27c314d2020-02-25 12:56:11 +09004941static void init_alloc_chunk_ctl_policy_regular(
4942 struct btrfs_fs_devices *fs_devices,
4943 struct alloc_chunk_ctl *ctl)
4944{
4945 u64 type = ctl->type;
4946
4947 if (type & BTRFS_BLOCK_GROUP_DATA) {
4948 ctl->max_stripe_size = SZ_1G;
4949 ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4950 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4951 /* For larger filesystems, use larger metadata chunks */
4952 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4953 ctl->max_stripe_size = SZ_1G;
4954 else
4955 ctl->max_stripe_size = SZ_256M;
4956 ctl->max_chunk_size = ctl->max_stripe_size;
4957 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4958 ctl->max_stripe_size = SZ_32M;
4959 ctl->max_chunk_size = 2 * ctl->max_stripe_size;
4960 ctl->devs_max = min_t(int, ctl->devs_max,
4961 BTRFS_MAX_DEVS_SYS_CHUNK);
4962 } else {
4963 BUG();
4964 }
4965
4966 /* We don't want a chunk larger than 10% of writable space */
4967 ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4968 ctl->max_chunk_size);
Naohiro Aota6aafb302020-02-25 12:56:15 +09004969 ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
Naohiro Aota27c314d2020-02-25 12:56:11 +09004970}
4971
Naohiro Aota1cd61212021-02-04 19:21:48 +09004972static void init_alloc_chunk_ctl_policy_zoned(
4973 struct btrfs_fs_devices *fs_devices,
4974 struct alloc_chunk_ctl *ctl)
4975{
4976 u64 zone_size = fs_devices->fs_info->zone_size;
4977 u64 limit;
4978 int min_num_stripes = ctl->devs_min * ctl->dev_stripes;
4979 int min_data_stripes = (min_num_stripes - ctl->nparity) / ctl->ncopies;
4980 u64 min_chunk_size = min_data_stripes * zone_size;
4981 u64 type = ctl->type;
4982
4983 ctl->max_stripe_size = zone_size;
4984 if (type & BTRFS_BLOCK_GROUP_DATA) {
4985 ctl->max_chunk_size = round_down(BTRFS_MAX_DATA_CHUNK_SIZE,
4986 zone_size);
4987 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4988 ctl->max_chunk_size = ctl->max_stripe_size;
4989 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4990 ctl->max_chunk_size = 2 * ctl->max_stripe_size;
4991 ctl->devs_max = min_t(int, ctl->devs_max,
4992 BTRFS_MAX_DEVS_SYS_CHUNK);
4993 }
4994
4995 /* We don't want a chunk larger than 10% of writable space */
4996 limit = max(round_down(div_factor(fs_devices->total_rw_bytes, 1),
4997 zone_size),
4998 min_chunk_size);
4999 ctl->max_chunk_size = min(limit, ctl->max_chunk_size);
5000 ctl->dev_extent_min = zone_size * ctl->dev_stripes;
5001}
5002
Naohiro Aota27c314d2020-02-25 12:56:11 +09005003static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices,
5004 struct alloc_chunk_ctl *ctl)
5005{
5006 int index = btrfs_bg_flags_to_raid_index(ctl->type);
5007
5008 ctl->sub_stripes = btrfs_raid_array[index].sub_stripes;
5009 ctl->dev_stripes = btrfs_raid_array[index].dev_stripes;
5010 ctl->devs_max = btrfs_raid_array[index].devs_max;
5011 if (!ctl->devs_max)
5012 ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info);
5013 ctl->devs_min = btrfs_raid_array[index].devs_min;
5014 ctl->devs_increment = btrfs_raid_array[index].devs_increment;
5015 ctl->ncopies = btrfs_raid_array[index].ncopies;
5016 ctl->nparity = btrfs_raid_array[index].nparity;
5017 ctl->ndevs = 0;
5018
5019 switch (fs_devices->chunk_alloc_policy) {
5020 case BTRFS_CHUNK_ALLOC_REGULAR:
5021 init_alloc_chunk_ctl_policy_regular(fs_devices, ctl);
5022 break;
Naohiro Aota1cd61212021-02-04 19:21:48 +09005023 case BTRFS_CHUNK_ALLOC_ZONED:
5024 init_alloc_chunk_ctl_policy_zoned(fs_devices, ctl);
5025 break;
Naohiro Aota27c314d2020-02-25 12:56:11 +09005026 default:
5027 BUG();
5028 }
5029}
5030
Naohiro Aota560156c2020-02-25 12:56:12 +09005031static int gather_device_info(struct btrfs_fs_devices *fs_devices,
5032 struct alloc_chunk_ctl *ctl,
5033 struct btrfs_device_info *devices_info)
5034{
5035 struct btrfs_fs_info *info = fs_devices->fs_info;
5036 struct btrfs_device *device;
5037 u64 total_avail;
5038 u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes;
Naohiro Aota560156c2020-02-25 12:56:12 +09005039 int ret;
5040 int ndevs = 0;
5041 u64 max_avail;
5042 u64 dev_offset;
5043
5044 /*
5045 * in the first pass through the devices list, we gather information
5046 * about the available holes on each device.
5047 */
5048 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
5049 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
5050 WARN(1, KERN_ERR
5051 "BTRFS: read-only device in alloc_list\n");
5052 continue;
5053 }
5054
5055 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5056 &device->dev_state) ||
5057 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
5058 continue;
5059
5060 if (device->total_bytes > device->bytes_used)
5061 total_avail = device->total_bytes - device->bytes_used;
5062 else
5063 total_avail = 0;
5064
5065 /* If there is no space on this device, skip it. */
Naohiro Aota6aafb302020-02-25 12:56:15 +09005066 if (total_avail < ctl->dev_extent_min)
Naohiro Aota560156c2020-02-25 12:56:12 +09005067 continue;
5068
5069 ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
5070 &max_avail);
5071 if (ret && ret != -ENOSPC)
5072 return ret;
5073
5074 if (ret == 0)
5075 max_avail = dev_extent_want;
5076
Naohiro Aota6aafb302020-02-25 12:56:15 +09005077 if (max_avail < ctl->dev_extent_min) {
Naohiro Aota560156c2020-02-25 12:56:12 +09005078 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5079 btrfs_debug(info,
5080 "%s: devid %llu has no free space, have=%llu want=%llu",
5081 __func__, device->devid, max_avail,
Naohiro Aota6aafb302020-02-25 12:56:15 +09005082 ctl->dev_extent_min);
Naohiro Aota560156c2020-02-25 12:56:12 +09005083 continue;
5084 }
5085
5086 if (ndevs == fs_devices->rw_devices) {
5087 WARN(1, "%s: found more than %llu devices\n",
5088 __func__, fs_devices->rw_devices);
5089 break;
5090 }
5091 devices_info[ndevs].dev_offset = dev_offset;
5092 devices_info[ndevs].max_avail = max_avail;
5093 devices_info[ndevs].total_avail = total_avail;
5094 devices_info[ndevs].dev = device;
5095 ++ndevs;
5096 }
5097 ctl->ndevs = ndevs;
5098
5099 /*
5100 * now sort the devices by hole size / available space
5101 */
5102 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5103 btrfs_cmp_device_info, NULL);
5104
5105 return 0;
5106}
5107
Naohiro Aota5badf512020-02-25 12:56:13 +09005108static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
5109 struct btrfs_device_info *devices_info)
5110{
5111 /* Number of stripes that count for block group size */
5112 int data_stripes;
5113
5114 /*
5115 * The primary goal is to maximize the number of stripes, so use as
5116 * many devices as possible, even if the stripes are not maximum sized.
5117 *
5118 * The DUP profile stores more than one stripe per device, the
5119 * max_avail is the total size so we have to adjust.
5120 */
5121 ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail,
5122 ctl->dev_stripes);
5123 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
5124
5125 /* This will have to be fixed for RAID1 and RAID10 over more drives */
5126 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
5127
5128 /*
5129 * Use the number of data stripes to figure out how big this chunk is
5130 * really going to be in terms of logical address space, and compare
5131 * that answer with the max chunk size. If it's higher, we try to
5132 * reduce stripe_size.
5133 */
5134 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
5135 /*
5136 * Reduce stripe_size, round it up to a 16MB boundary again and
5137 * then use it, unless it ends up being even bigger than the
5138 * previous value we had already.
5139 */
5140 ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
5141 data_stripes), SZ_16M),
5142 ctl->stripe_size);
5143 }
5144
5145 /* Align to BTRFS_STRIPE_LEN */
5146 ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN);
5147 ctl->chunk_size = ctl->stripe_size * data_stripes;
5148
5149 return 0;
5150}
5151
Naohiro Aota1cd61212021-02-04 19:21:48 +09005152static int decide_stripe_size_zoned(struct alloc_chunk_ctl *ctl,
5153 struct btrfs_device_info *devices_info)
5154{
5155 u64 zone_size = devices_info[0].dev->zone_info->zone_size;
5156 /* Number of stripes that count for block group size */
5157 int data_stripes;
5158
5159 /*
5160 * It should hold because:
5161 * dev_extent_min == dev_extent_want == zone_size * dev_stripes
5162 */
5163 ASSERT(devices_info[ctl->ndevs - 1].max_avail == ctl->dev_extent_min);
5164
5165 ctl->stripe_size = zone_size;
5166 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
5167 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
5168
5169 /* stripe_size is fixed in zoned filesysmte. Reduce ndevs instead. */
5170 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
5171 ctl->ndevs = div_u64(div_u64(ctl->max_chunk_size * ctl->ncopies,
5172 ctl->stripe_size) + ctl->nparity,
5173 ctl->dev_stripes);
5174 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
5175 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
5176 ASSERT(ctl->stripe_size * data_stripes <= ctl->max_chunk_size);
5177 }
5178
5179 ctl->chunk_size = ctl->stripe_size * data_stripes;
5180
5181 return 0;
5182}
5183
Naohiro Aota5badf512020-02-25 12:56:13 +09005184static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
5185 struct alloc_chunk_ctl *ctl,
5186 struct btrfs_device_info *devices_info)
5187{
5188 struct btrfs_fs_info *info = fs_devices->fs_info;
5189
5190 /*
5191 * Round down to number of usable stripes, devs_increment can be any
5192 * number so we can't use round_down() that requires power of 2, while
5193 * rounddown is safe.
5194 */
5195 ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment);
5196
5197 if (ctl->ndevs < ctl->devs_min) {
5198 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5199 btrfs_debug(info,
5200 "%s: not enough devices with free space: have=%d minimum required=%d",
5201 __func__, ctl->ndevs, ctl->devs_min);
5202 }
5203 return -ENOSPC;
5204 }
5205
5206 ctl->ndevs = min(ctl->ndevs, ctl->devs_max);
5207
5208 switch (fs_devices->chunk_alloc_policy) {
5209 case BTRFS_CHUNK_ALLOC_REGULAR:
5210 return decide_stripe_size_regular(ctl, devices_info);
Naohiro Aota1cd61212021-02-04 19:21:48 +09005211 case BTRFS_CHUNK_ALLOC_ZONED:
5212 return decide_stripe_size_zoned(ctl, devices_info);
Naohiro Aota5badf512020-02-25 12:56:13 +09005213 default:
5214 BUG();
5215 }
5216}
5217
Naohiro Aotadce580c2020-02-25 12:56:14 +09005218static int create_chunk(struct btrfs_trans_handle *trans,
5219 struct alloc_chunk_ctl *ctl,
5220 struct btrfs_device_info *devices_info)
5221{
5222 struct btrfs_fs_info *info = trans->fs_info;
5223 struct map_lookup *map = NULL;
5224 struct extent_map_tree *em_tree;
5225 struct extent_map *em;
5226 u64 start = ctl->start;
5227 u64 type = ctl->type;
5228 int ret;
5229 int i;
5230 int j;
5231
5232 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);
5233 if (!map)
5234 return -ENOMEM;
5235 map->num_stripes = ctl->num_stripes;
5236
5237 for (i = 0; i < ctl->ndevs; ++i) {
5238 for (j = 0; j < ctl->dev_stripes; ++j) {
5239 int s = i * ctl->dev_stripes + j;
5240 map->stripes[s].dev = devices_info[i].dev;
5241 map->stripes[s].physical = devices_info[i].dev_offset +
5242 j * ctl->stripe_size;
5243 }
5244 }
5245 map->stripe_len = BTRFS_STRIPE_LEN;
5246 map->io_align = BTRFS_STRIPE_LEN;
5247 map->io_width = BTRFS_STRIPE_LEN;
5248 map->type = type;
5249 map->sub_stripes = ctl->sub_stripes;
5250
5251 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
5252
5253 em = alloc_extent_map();
5254 if (!em) {
5255 kfree(map);
5256 return -ENOMEM;
5257 }
5258 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5259 em->map_lookup = map;
5260 em->start = start;
5261 em->len = ctl->chunk_size;
5262 em->block_start = 0;
5263 em->block_len = em->len;
5264 em->orig_block_len = ctl->stripe_size;
5265
5266 em_tree = &info->mapping_tree;
5267 write_lock(&em_tree->lock);
5268 ret = add_extent_mapping(em_tree, em, 0);
5269 if (ret) {
5270 write_unlock(&em_tree->lock);
5271 free_extent_map(em);
5272 return ret;
5273 }
5274 write_unlock(&em_tree->lock);
5275
5276 ret = btrfs_make_block_group(trans, 0, type, start, ctl->chunk_size);
5277 if (ret)
5278 goto error_del_extent;
5279
5280 for (i = 0; i < map->num_stripes; i++) {
5281 struct btrfs_device *dev = map->stripes[i].dev;
5282
5283 btrfs_device_set_bytes_used(dev,
5284 dev->bytes_used + ctl->stripe_size);
5285 if (list_empty(&dev->post_commit_list))
5286 list_add_tail(&dev->post_commit_list,
5287 &trans->transaction->dev_update_list);
5288 }
5289
5290 atomic64_sub(ctl->stripe_size * map->num_stripes,
5291 &info->free_chunk_space);
5292
5293 free_extent_map(em);
5294 check_raid56_incompat_flag(info, type);
5295 check_raid1c34_incompat_flag(info, type);
5296
5297 return 0;
5298
5299error_del_extent:
5300 write_lock(&em_tree->lock);
5301 remove_extent_mapping(em_tree, em);
5302 write_unlock(&em_tree->lock);
5303
5304 /* One for our allocation */
5305 free_extent_map(em);
5306 /* One for the tree reference */
5307 free_extent_map(em);
5308
5309 return ret;
5310}
5311
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005312int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00005313{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005314 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00005315 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Arne Jansen73c5de02011-04-12 12:07:57 +02005316 struct btrfs_device_info *devices_info = NULL;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09005317 struct alloc_chunk_ctl ctl;
Miao Xieb2117a32011-01-05 10:07:28 +00005318 int ret;
Miao Xieb2117a32011-01-05 10:07:28 +00005319
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005320 lockdep_assert_held(&info->chunk_mutex);
5321
Naohiro Aotab25c19f2020-02-25 12:56:07 +09005322 if (!alloc_profile_is_valid(type, 0)) {
5323 ASSERT(0);
5324 return -EINVAL;
5325 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005326
Qu Wenruo4117f202018-01-22 13:50:54 +08005327 if (list_empty(&fs_devices->alloc_list)) {
5328 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5329 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005330 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005331 }
Miao Xieb2117a32011-01-05 10:07:28 +00005332
Naohiro Aota27c314d2020-02-25 12:56:11 +09005333 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
5334 btrfs_err(info, "invalid chunk type 0x%llx requested", type);
5335 ASSERT(0);
5336 return -EINVAL;
Arne Jansen73c5de02011-04-12 12:07:57 +02005337 }
5338
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005339 ctl.start = find_next_chunk(info);
Naohiro Aota27c314d2020-02-25 12:56:11 +09005340 ctl.type = type;
5341 init_alloc_chunk_ctl(fs_devices, &ctl);
Miao Xieb2117a32011-01-05 10:07:28 +00005342
David Sterba31e818f2015-02-20 18:00:26 +01005343 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005344 GFP_NOFS);
5345 if (!devices_info)
5346 return -ENOMEM;
5347
Naohiro Aota560156c2020-02-25 12:56:12 +09005348 ret = gather_device_info(fs_devices, &ctl, devices_info);
5349 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005350 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005351
Naohiro Aota5badf512020-02-25 12:56:13 +09005352 ret = decide_stripe_size(fs_devices, &ctl, devices_info);
5353 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005354 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005355
Naohiro Aotadce580c2020-02-25 12:56:14 +09005356 ret = create_chunk(trans, &ctl, devices_info);
Chris Mason9b3f68b2008-04-18 10:29:51 -04005357
Naohiro Aotadce580c2020-02-25 12:56:14 +09005358out:
Miao Xieb2117a32011-01-05 10:07:28 +00005359 kfree(devices_info);
5360 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005361}
5362
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005363/*
5364 * Chunk allocation falls into two parts. The first part does work
5365 * that makes the new allocated chunk usable, but does not do any operation
5366 * that modifies the chunk tree. The second part does the work that
5367 * requires modifying the chunk tree. This division is important for the
5368 * bootstrap process of adding storage to a seed btrfs.
5369 */
Josef Bacik6df9a952013-06-27 13:22:46 -04005370int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005371 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005372{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005373 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005374 struct btrfs_root *extent_root = fs_info->extent_root;
5375 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005376 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005377 struct btrfs_device *device;
5378 struct btrfs_chunk *chunk;
5379 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005380 struct extent_map *em;
5381 struct map_lookup *map;
5382 size_t item_size;
5383 u64 dev_offset;
5384 u64 stripe_size;
5385 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005386 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005387
Omar Sandoval60ca8422018-05-16 16:34:31 -07005388 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005389 if (IS_ERR(em))
5390 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005391
Jeff Mahoney95617d62015-06-03 10:55:48 -04005392 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005393 item_size = btrfs_chunk_item_size(map->num_stripes);
5394 stripe_size = em->orig_block_len;
5395
5396 chunk = kzalloc(item_size, GFP_NOFS);
5397 if (!chunk) {
5398 ret = -ENOMEM;
5399 goto out;
5400 }
5401
Filipe Manana50460e32015-11-20 10:42:47 +00005402 /*
5403 * Take the device list mutex to prevent races with the final phase of
5404 * a device replace operation that replaces the device object associated
5405 * with the map's stripes, because the device object's id can change
5406 * at any time during that final phase of the device replace operation
5407 * (dev-replace.c:btrfs_dev_replace_finishing()).
5408 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005410 for (i = 0; i < map->num_stripes; i++) {
5411 device = map->stripes[i].dev;
5412 dev_offset = map->stripes[i].physical;
5413
Yan Zheng2b820322008-11-17 21:11:30 -05005414 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005415 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005416 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005417 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5418 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005419 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005420 break;
5421 }
5422 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005423 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005424 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005425 }
5426
Yan Zheng2b820322008-11-17 21:11:30 -05005427 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005428 for (i = 0; i < map->num_stripes; i++) {
5429 device = map->stripes[i].dev;
5430 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005431
5432 btrfs_set_stack_stripe_devid(stripe, device->devid);
5433 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5434 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5435 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005436 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005437 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005438
5439 btrfs_set_stack_chunk_length(chunk, chunk_size);
5440 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5441 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5442 btrfs_set_stack_chunk_type(chunk, map->type);
5443 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5444 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5445 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005446 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005447 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5448
5449 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5450 key.type = BTRFS_CHUNK_ITEM_KEY;
5451 key.offset = chunk_offset;
5452
5453 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005454 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5455 /*
5456 * TODO: Cleanup of inserted chunk root in case of
5457 * failure.
5458 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005459 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005460 }
liubo1abe9b82011-03-24 11:18:59 +00005461
Josef Bacik6df9a952013-06-27 13:22:46 -04005462out:
Yan Zheng2b820322008-11-17 21:11:30 -05005463 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005464 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005465 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005466}
5467
David Sterba6f8e0fc2019-03-20 16:29:13 +01005468static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005469{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005470 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005471 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005472 int ret;
5473
Jeff Mahoney1b868262017-05-17 11:38:35 -04005474 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005475 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005476 if (ret)
5477 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005478
Jeff Mahoney1b868262017-05-17 11:38:35 -04005479 alloc_profile = btrfs_system_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005480 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005481 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005482}
5483
Miao Xied20983b2014-07-03 18:22:13 +08005484static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5485{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005486 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005487
David Sterbafc9a2ac2019-05-17 11:43:22 +02005488 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005489}
5490
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005491int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005492{
5493 struct extent_map *em;
5494 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005495 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005496 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005497 int i;
5498
Omar Sandoval60ca8422018-05-16 16:34:31 -07005499 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005500 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005501 return 1;
5502
Jeff Mahoney95617d62015-06-03 10:55:48 -04005503 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005504 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005505 if (test_bit(BTRFS_DEV_STATE_MISSING,
5506 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005507 miss_ndevs++;
5508 continue;
5509 }
Anand Jainebbede42017-12-04 12:54:52 +08005510 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5511 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005512 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005513 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005514 }
5515 }
Miao Xied20983b2014-07-03 18:22:13 +08005516
5517 /*
5518 * If the number of missing devices is larger than max errors,
5519 * we can not write the data into that chunk successfully, so
5520 * set it readonly.
5521 */
5522 if (miss_ndevs > btrfs_chunk_max_errors(map))
5523 readonly = 1;
5524end:
Yan Zheng2b820322008-11-17 21:11:30 -05005525 free_extent_map(em);
5526 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005527}
5528
David Sterbac8bf1b62019-05-17 11:43:17 +02005529void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005530{
5531 struct extent_map *em;
5532
Chris Masond3977122009-01-05 21:25:51 -05005533 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005534 write_lock(&tree->lock);
5535 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005536 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005537 remove_extent_mapping(tree, em);
5538 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005539 if (!em)
5540 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005541 /* once for us */
5542 free_extent_map(em);
5543 /* once for the tree */
5544 free_extent_map(em);
5545 }
5546}
5547
Stefan Behrens5d964052012-11-05 14:59:07 +01005548int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005549{
5550 struct extent_map *em;
5551 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005552 int ret;
5553
Omar Sandoval60ca8422018-05-16 16:34:31 -07005554 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005555 if (IS_ERR(em))
5556 /*
5557 * We could return errors for these cases, but that could get
5558 * ugly and we'd probably do the same thing which is just not do
5559 * anything else and exit, so return 1 so the callers don't try
5560 * to use other copies.
5561 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005562 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005563
Jeff Mahoney95617d62015-06-03 10:55:48 -04005564 map = em->map_lookup;
David Sterbac7369b32019-05-31 15:39:31 +02005565 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
Chris Masonf1885912008-04-09 16:28:12 -04005566 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005567 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5568 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005569 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5570 ret = 2;
5571 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005572 /*
5573 * There could be two corrupted data stripes, we need
5574 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005575 *
Liu Bo8810f752018-01-02 13:36:41 -07005576 * Fail a stripe at a time on every retry except the
5577 * stripe under reconstruction.
5578 */
5579 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005580 else
5581 ret = 1;
5582 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005583
David Sterbacb5583d2018-09-07 16:11:23 +02005584 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005585 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5586 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005587 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005588 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005589
Chris Masonf1885912008-04-09 16:28:12 -04005590 return ret;
5591}
5592
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005593unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005594 u64 logical)
5595{
5596 struct extent_map *em;
5597 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005598 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005599
Omar Sandoval60ca8422018-05-16 16:34:31 -07005600 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005601
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005602 if (!WARN_ON(IS_ERR(em))) {
5603 map = em->map_lookup;
5604 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5605 len = map->stripe_len * nr_data_stripes(map);
5606 free_extent_map(em);
5607 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005608 return len;
5609}
5610
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005611int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005612{
5613 struct extent_map *em;
5614 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005615 int ret = 0;
5616
Omar Sandoval60ca8422018-05-16 16:34:31 -07005617 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005618
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005619 if(!WARN_ON(IS_ERR(em))) {
5620 map = em->map_lookup;
5621 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5622 ret = 1;
5623 free_extent_map(em);
5624 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005625 return ret;
5626}
5627
Stefan Behrens30d98612012-11-06 14:52:18 +01005628static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005629 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005630 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005631{
5632 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005633 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005634 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005635 int tolerance;
5636 struct btrfs_device *srcdev;
5637
Anand Jain99f92a72018-03-14 16:29:12 +08005638 ASSERT((map->type &
David Sterbac7369b32019-05-31 15:39:31 +02005639 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
Anand Jain99f92a72018-03-14 16:29:12 +08005640
5641 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5642 num_stripes = map->sub_stripes;
5643 else
5644 num_stripes = map->num_stripes;
5645
Anand Jain33fd2f72020-10-28 21:14:46 +08005646 switch (fs_info->fs_devices->read_policy) {
5647 default:
5648 /* Shouldn't happen, just warn and use pid instead of failing */
5649 btrfs_warn_rl(fs_info,
5650 "unknown read_policy type %u, reset to pid",
5651 fs_info->fs_devices->read_policy);
5652 fs_info->fs_devices->read_policy = BTRFS_READ_POLICY_PID;
5653 fallthrough;
5654 case BTRFS_READ_POLICY_PID:
5655 preferred_mirror = first + (current->pid % num_stripes);
5656 break;
5657 }
Anand Jain8ba0ae72018-03-14 16:29:13 +08005658
Stefan Behrens30d98612012-11-06 14:52:18 +01005659 if (dev_replace_is_ongoing &&
5660 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5661 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5662 srcdev = fs_info->dev_replace.srcdev;
5663 else
5664 srcdev = NULL;
5665
5666 /*
5667 * try to avoid the drive that is the source drive for a
5668 * dev-replace procedure, only choose it if no other non-missing
5669 * mirror is available
5670 */
5671 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005672 if (map->stripes[preferred_mirror].dev->bdev &&
5673 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5674 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005675 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005676 if (map->stripes[i].dev->bdev &&
5677 (tolerance || map->stripes[i].dev != srcdev))
5678 return i;
5679 }
Chris Masondfe25022008-05-13 13:46:40 -04005680 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005681
Chris Masondfe25022008-05-13 13:46:40 -04005682 /* we couldn't find one that doesn't fail. Just return something
5683 * and the io error handling code will clean up eventually
5684 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005685 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005686}
5687
David Woodhouse53b381b2013-01-29 18:40:14 -05005688/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005689static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005690{
David Woodhouse53b381b2013-01-29 18:40:14 -05005691 int i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005692 int again = 1;
5693
5694 while (again) {
5695 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005696 for (i = 0; i < num_stripes - 1; i++) {
David Sterbaeeb6f172019-11-28 15:31:17 +01005697 /* Swap if parity is on a smaller index */
5698 if (bbio->raid_map[i] > bbio->raid_map[i + 1]) {
5699 swap(bbio->stripes[i], bbio->stripes[i + 1]);
5700 swap(bbio->raid_map[i], bbio->raid_map[i + 1]);
David Woodhouse53b381b2013-01-29 18:40:14 -05005701 again = 1;
5702 }
5703 }
5704 }
5705}
5706
Zhao Lei6e9606d2015-01-20 15:11:34 +08005707static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5708{
5709 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005710 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005711 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005712 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005713 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005714 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005715 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005716 /*
5717 * plus the raid_map, which includes both the tgt dev
5718 * and the stripes
5719 */
5720 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005721 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005722
5723 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005724 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005725
Nikolay Borisov608769a2020-07-02 16:46:41 +03005726 bbio->tgtdev_map = (int *)(bbio->stripes + total_stripes);
5727 bbio->raid_map = (u64 *)(bbio->tgtdev_map + real_stripes);
5728
Zhao Lei6e9606d2015-01-20 15:11:34 +08005729 return bbio;
5730}
5731
5732void btrfs_get_bbio(struct btrfs_bio *bbio)
5733{
Elena Reshetova140475a2017-03-03 10:55:10 +02005734 WARN_ON(!refcount_read(&bbio->refs));
5735 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005736}
5737
5738void btrfs_put_bbio(struct btrfs_bio *bbio)
5739{
5740 if (!bbio)
5741 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005742 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005743 kfree(bbio);
5744}
5745
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005746/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5747/*
5748 * Please note that, discard won't be sent to target device of device
5749 * replace.
5750 */
5751static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005752 u64 logical, u64 *length_ret,
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005753 struct btrfs_bio **bbio_ret)
5754{
5755 struct extent_map *em;
5756 struct map_lookup *map;
5757 struct btrfs_bio *bbio;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005758 u64 length = *length_ret;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005759 u64 offset;
5760 u64 stripe_nr;
5761 u64 stripe_nr_end;
5762 u64 stripe_end_offset;
5763 u64 stripe_cnt;
5764 u64 stripe_len;
5765 u64 stripe_offset;
5766 u64 num_stripes;
5767 u32 stripe_index;
5768 u32 factor = 0;
5769 u32 sub_stripes = 0;
5770 u64 stripes_per_dev = 0;
5771 u32 remaining_stripes = 0;
5772 u32 last_stripe = 0;
5773 int ret = 0;
5774 int i;
5775
5776 /* discard always return a bbio */
5777 ASSERT(bbio_ret);
5778
Omar Sandoval60ca8422018-05-16 16:34:31 -07005779 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005780 if (IS_ERR(em))
5781 return PTR_ERR(em);
5782
5783 map = em->map_lookup;
5784 /* we don't discard raid56 yet */
5785 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5786 ret = -EOPNOTSUPP;
5787 goto out;
5788 }
5789
5790 offset = logical - em->start;
Qu Wenruo2d974612019-10-23 21:57:26 +08005791 length = min_t(u64, em->start + em->len - logical, length);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005792 *length_ret = length;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005793
5794 stripe_len = map->stripe_len;
5795 /*
5796 * stripe_nr counts the total number of stripes we have to stride
5797 * to get to this block
5798 */
5799 stripe_nr = div64_u64(offset, stripe_len);
5800
5801 /* stripe_offset is the offset of this block in its stripe */
5802 stripe_offset = offset - stripe_nr * stripe_len;
5803
5804 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005805 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005806 stripe_cnt = stripe_nr_end - stripe_nr;
5807 stripe_end_offset = stripe_nr_end * map->stripe_len -
5808 (offset + length);
5809 /*
5810 * after this, stripe_nr is the number of stripes on this
5811 * device we have to walk to find the data, and stripe_index is
5812 * the number of our device in the stripe array
5813 */
5814 num_stripes = 1;
5815 stripe_index = 0;
5816 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5817 BTRFS_BLOCK_GROUP_RAID10)) {
5818 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5819 sub_stripes = 1;
5820 else
5821 sub_stripes = map->sub_stripes;
5822
5823 factor = map->num_stripes / sub_stripes;
5824 num_stripes = min_t(u64, map->num_stripes,
5825 sub_stripes * stripe_cnt);
5826 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5827 stripe_index *= sub_stripes;
5828 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5829 &remaining_stripes);
5830 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5831 last_stripe *= sub_stripes;
David Sterbac7369b32019-05-31 15:39:31 +02005832 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005833 BTRFS_BLOCK_GROUP_DUP)) {
5834 num_stripes = map->num_stripes;
5835 } else {
5836 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5837 &stripe_index);
5838 }
5839
5840 bbio = alloc_btrfs_bio(num_stripes, 0);
5841 if (!bbio) {
5842 ret = -ENOMEM;
5843 goto out;
5844 }
5845
5846 for (i = 0; i < num_stripes; i++) {
5847 bbio->stripes[i].physical =
5848 map->stripes[stripe_index].physical +
5849 stripe_offset + stripe_nr * map->stripe_len;
5850 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5851
5852 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5853 BTRFS_BLOCK_GROUP_RAID10)) {
5854 bbio->stripes[i].length = stripes_per_dev *
5855 map->stripe_len;
5856
5857 if (i / sub_stripes < remaining_stripes)
5858 bbio->stripes[i].length +=
5859 map->stripe_len;
5860
5861 /*
5862 * Special for the first stripe and
5863 * the last stripe:
5864 *
5865 * |-------|...|-------|
5866 * |----------|
5867 * off end_off
5868 */
5869 if (i < sub_stripes)
5870 bbio->stripes[i].length -=
5871 stripe_offset;
5872
5873 if (stripe_index >= last_stripe &&
5874 stripe_index <= (last_stripe +
5875 sub_stripes - 1))
5876 bbio->stripes[i].length -=
5877 stripe_end_offset;
5878
5879 if (i == sub_stripes - 1)
5880 stripe_offset = 0;
5881 } else {
5882 bbio->stripes[i].length = length;
5883 }
5884
5885 stripe_index++;
5886 if (stripe_index == map->num_stripes) {
5887 stripe_index = 0;
5888 stripe_nr++;
5889 }
5890 }
5891
5892 *bbio_ret = bbio;
5893 bbio->map_type = map->type;
5894 bbio->num_stripes = num_stripes;
5895out:
5896 free_extent_map(em);
5897 return ret;
5898}
5899
Liu Bo5ab56092017-03-14 13:33:57 -07005900/*
5901 * In dev-replace case, for repair case (that's the only case where the mirror
5902 * is selected explicitly when calling btrfs_map_block), blocks left of the
5903 * left cursor can also be read from the target drive.
5904 *
5905 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5906 * array of stripes.
5907 * For READ, it also needs to be supported using the same mirror number.
5908 *
5909 * If the requested block is not left of the left cursor, EIO is returned. This
5910 * can happen because btrfs_num_copies() returns one more in the dev-replace
5911 * case.
5912 */
5913static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5914 u64 logical, u64 length,
5915 u64 srcdev_devid, int *mirror_num,
5916 u64 *physical)
5917{
5918 struct btrfs_bio *bbio = NULL;
5919 int num_stripes;
5920 int index_srcdev = 0;
5921 int found = 0;
5922 u64 physical_of_found = 0;
5923 int i;
5924 int ret = 0;
5925
5926 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5927 logical, &length, &bbio, 0, 0);
5928 if (ret) {
5929 ASSERT(bbio == NULL);
5930 return ret;
5931 }
5932
5933 num_stripes = bbio->num_stripes;
5934 if (*mirror_num > num_stripes) {
5935 /*
5936 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5937 * that means that the requested area is not left of the left
5938 * cursor
5939 */
5940 btrfs_put_bbio(bbio);
5941 return -EIO;
5942 }
5943
5944 /*
5945 * process the rest of the function using the mirror_num of the source
5946 * drive. Therefore look it up first. At the end, patch the device
5947 * pointer to the one of the target drive.
5948 */
5949 for (i = 0; i < num_stripes; i++) {
5950 if (bbio->stripes[i].dev->devid != srcdev_devid)
5951 continue;
5952
5953 /*
5954 * In case of DUP, in order to keep it simple, only add the
5955 * mirror with the lowest physical address
5956 */
5957 if (found &&
5958 physical_of_found <= bbio->stripes[i].physical)
5959 continue;
5960
5961 index_srcdev = i;
5962 found = 1;
5963 physical_of_found = bbio->stripes[i].physical;
5964 }
5965
5966 btrfs_put_bbio(bbio);
5967
5968 ASSERT(found);
5969 if (!found)
5970 return -EIO;
5971
5972 *mirror_num = index_srcdev + 1;
5973 *physical = physical_of_found;
5974 return ret;
5975}
5976
Naohiro Aota6143c232021-02-04 19:22:12 +09005977static bool is_block_group_to_copy(struct btrfs_fs_info *fs_info, u64 logical)
5978{
5979 struct btrfs_block_group *cache;
5980 bool ret;
5981
Naohiro Aotade17add2021-02-04 19:22:13 +09005982 /* Non zoned filesystem does not use "to_copy" flag */
Naohiro Aota6143c232021-02-04 19:22:12 +09005983 if (!btrfs_is_zoned(fs_info))
5984 return false;
5985
5986 cache = btrfs_lookup_block_group(fs_info, logical);
5987
5988 spin_lock(&cache->lock);
5989 ret = cache->to_copy;
5990 spin_unlock(&cache->lock);
5991
5992 btrfs_put_block_group(cache);
5993 return ret;
5994}
5995
Liu Bo73c0f222017-03-14 13:33:58 -07005996static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5997 struct btrfs_bio **bbio_ret,
5998 struct btrfs_dev_replace *dev_replace,
Naohiro Aota6143c232021-02-04 19:22:12 +09005999 u64 logical,
Liu Bo73c0f222017-03-14 13:33:58 -07006000 int *num_stripes_ret, int *max_errors_ret)
6001{
6002 struct btrfs_bio *bbio = *bbio_ret;
6003 u64 srcdev_devid = dev_replace->srcdev->devid;
6004 int tgtdev_indexes = 0;
6005 int num_stripes = *num_stripes_ret;
6006 int max_errors = *max_errors_ret;
6007 int i;
6008
6009 if (op == BTRFS_MAP_WRITE) {
6010 int index_where_to_add;
6011
6012 /*
Naohiro Aota6143c232021-02-04 19:22:12 +09006013 * A block group which have "to_copy" set will eventually
6014 * copied by dev-replace process. We can avoid cloning IO here.
6015 */
6016 if (is_block_group_to_copy(dev_replace->srcdev->fs_info, logical))
6017 return;
6018
6019 /*
Liu Bo73c0f222017-03-14 13:33:58 -07006020 * duplicate the write operations while the dev replace
6021 * procedure is running. Since the copying of the old disk to
6022 * the new disk takes place at run time while the filesystem is
6023 * mounted writable, the regular write operations to the old
6024 * disk have to be duplicated to go to the new disk as well.
6025 *
6026 * Note that device->missing is handled by the caller, and that
6027 * the write to the old disk is already set up in the stripes
6028 * array.
6029 */
6030 index_where_to_add = num_stripes;
6031 for (i = 0; i < num_stripes; i++) {
6032 if (bbio->stripes[i].dev->devid == srcdev_devid) {
6033 /* write to new disk, too */
6034 struct btrfs_bio_stripe *new =
6035 bbio->stripes + index_where_to_add;
6036 struct btrfs_bio_stripe *old =
6037 bbio->stripes + i;
6038
6039 new->physical = old->physical;
6040 new->length = old->length;
6041 new->dev = dev_replace->tgtdev;
6042 bbio->tgtdev_map[i] = index_where_to_add;
6043 index_where_to_add++;
6044 max_errors++;
6045 tgtdev_indexes++;
6046 }
6047 }
6048 num_stripes = index_where_to_add;
6049 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
6050 int index_srcdev = 0;
6051 int found = 0;
6052 u64 physical_of_found = 0;
6053
6054 /*
6055 * During the dev-replace procedure, the target drive can also
6056 * be used to read data in case it is needed to repair a corrupt
6057 * block elsewhere. This is possible if the requested area is
6058 * left of the left cursor. In this area, the target drive is a
6059 * full copy of the source drive.
6060 */
6061 for (i = 0; i < num_stripes; i++) {
6062 if (bbio->stripes[i].dev->devid == srcdev_devid) {
6063 /*
6064 * In case of DUP, in order to keep it simple,
6065 * only add the mirror with the lowest physical
6066 * address
6067 */
6068 if (found &&
6069 physical_of_found <=
6070 bbio->stripes[i].physical)
6071 continue;
6072 index_srcdev = i;
6073 found = 1;
6074 physical_of_found = bbio->stripes[i].physical;
6075 }
6076 }
6077 if (found) {
6078 struct btrfs_bio_stripe *tgtdev_stripe =
6079 bbio->stripes + num_stripes;
6080
6081 tgtdev_stripe->physical = physical_of_found;
6082 tgtdev_stripe->length =
6083 bbio->stripes[index_srcdev].length;
6084 tgtdev_stripe->dev = dev_replace->tgtdev;
6085 bbio->tgtdev_map[index_srcdev] = num_stripes;
6086
6087 tgtdev_indexes++;
6088 num_stripes++;
6089 }
6090 }
6091
6092 *num_stripes_ret = num_stripes;
6093 *max_errors_ret = max_errors;
6094 bbio->num_tgtdevs = tgtdev_indexes;
6095 *bbio_ret = bbio;
6096}
6097
Liu Bo2b19a1f2017-03-14 13:34:00 -07006098static bool need_full_stripe(enum btrfs_map_op op)
6099{
6100 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
6101}
6102
Nikolay Borisov5f141122019-06-03 12:05:03 +03006103/*
Michal Rostecki42034312021-01-27 14:57:27 +01006104 * Calculate the geometry of a particular (address, len) tuple. This
6105 * information is used to calculate how big a particular bio can get before it
6106 * straddles a stripe.
Nikolay Borisov5f141122019-06-03 12:05:03 +03006107 *
Michal Rostecki42034312021-01-27 14:57:27 +01006108 * @fs_info: the filesystem
6109 * @em: mapping containing the logical extent
6110 * @op: type of operation - write or read
6111 * @logical: address that we want to figure out the geometry of
6112 * @len: the length of IO we are going to perform, starting at @logical
6113 * @io_geom: pointer used to return values
Nikolay Borisov5f141122019-06-03 12:05:03 +03006114 *
6115 * Returns < 0 in case a chunk for the given logical address cannot be found,
6116 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
6117 */
Michal Rostecki42034312021-01-27 14:57:27 +01006118int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *em,
6119 enum btrfs_map_op op, u64 logical, u64 len,
6120 struct btrfs_io_geometry *io_geom)
Nikolay Borisov5f141122019-06-03 12:05:03 +03006121{
Nikolay Borisov5f141122019-06-03 12:05:03 +03006122 struct map_lookup *map;
6123 u64 offset;
6124 u64 stripe_offset;
6125 u64 stripe_nr;
6126 u64 stripe_len;
6127 u64 raid56_full_stripe_start = (u64)-1;
6128 int data_stripes;
6129
6130 ASSERT(op != BTRFS_MAP_DISCARD);
6131
Nikolay Borisov5f141122019-06-03 12:05:03 +03006132 map = em->map_lookup;
6133 /* Offset of this logical address in the chunk */
6134 offset = logical - em->start;
6135 /* Len of a stripe in a chunk */
6136 stripe_len = map->stripe_len;
6137 /* Stripe wher this block falls in */
6138 stripe_nr = div64_u64(offset, stripe_len);
6139 /* Offset of stripe in the chunk */
6140 stripe_offset = stripe_nr * stripe_len;
6141 if (offset < stripe_offset) {
6142 btrfs_crit(fs_info,
6143"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
6144 stripe_offset, offset, em->start, logical, stripe_len);
Michal Rostecki42034312021-01-27 14:57:27 +01006145 return -EINVAL;
Nikolay Borisov5f141122019-06-03 12:05:03 +03006146 }
6147
6148 /* stripe_offset is the offset of this block in its stripe */
6149 stripe_offset = offset - stripe_offset;
6150 data_stripes = nr_data_stripes(map);
6151
6152 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6153 u64 max_len = stripe_len - stripe_offset;
6154
6155 /*
6156 * In case of raid56, we need to know the stripe aligned start
6157 */
6158 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
6159 unsigned long full_stripe_len = stripe_len * data_stripes;
6160 raid56_full_stripe_start = offset;
6161
6162 /*
6163 * Allow a write of a full stripe, but make sure we
6164 * don't allow straddling of stripes
6165 */
6166 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
6167 full_stripe_len);
6168 raid56_full_stripe_start *= full_stripe_len;
6169
6170 /*
6171 * For writes to RAID[56], allow a full stripeset across
6172 * all disks. For other RAID types and for RAID[56]
6173 * reads, just allow a single stripe (on a single disk).
6174 */
6175 if (op == BTRFS_MAP_WRITE) {
6176 max_len = stripe_len * data_stripes -
6177 (offset - raid56_full_stripe_start);
6178 }
6179 }
6180 len = min_t(u64, em->len - offset, max_len);
6181 } else {
6182 len = em->len - offset;
6183 }
6184
6185 io_geom->len = len;
6186 io_geom->offset = offset;
6187 io_geom->stripe_len = stripe_len;
6188 io_geom->stripe_nr = stripe_nr;
6189 io_geom->stripe_offset = stripe_offset;
6190 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
6191
Michal Rostecki42034312021-01-27 14:57:27 +01006192 return 0;
Nikolay Borisov5f141122019-06-03 12:05:03 +03006193}
6194
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006195static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
6196 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006197 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006198 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006199 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04006200{
6201 struct extent_map *em;
6202 struct map_lookup *map;
Chris Mason593060d2008-03-25 16:50:33 -04006203 u64 stripe_offset;
6204 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006205 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006206 u32 stripe_index;
David Sterbacff82672019-05-17 11:43:45 +02006207 int data_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04006208 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006209 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006210 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006211 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006212 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006213 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006214 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6215 int dev_replace_is_ongoing = 0;
6216 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006217 int patch_the_first_stripe_for_dev_replace = 0;
6218 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006219 u64 raid56_full_stripe_start = (u64)-1;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006220 struct btrfs_io_geometry geom;
6221
6222 ASSERT(bbio_ret);
David Sterba75fb2e92018-08-03 00:36:29 +02006223 ASSERT(op != BTRFS_MAP_DISCARD);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006224
Michal Rostecki42034312021-01-27 14:57:27 +01006225 em = btrfs_get_chunk_map(fs_info, logical, *length);
6226 ASSERT(!IS_ERR(em));
6227
6228 ret = btrfs_get_io_geometry(fs_info, em, op, logical, *length, &geom);
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006229 if (ret < 0)
6230 return ret;
6231
Jeff Mahoney95617d62015-06-03 10:55:48 -04006232 map = em->map_lookup;
Chris Mason593060d2008-03-25 16:50:33 -04006233
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006234 *length = geom.len;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006235 stripe_len = geom.stripe_len;
6236 stripe_nr = geom.stripe_nr;
6237 stripe_offset = geom.stripe_offset;
6238 raid56_full_stripe_start = geom.raid56_stripe_offset;
David Sterbacff82672019-05-17 11:43:45 +02006239 data_stripes = nr_data_stripes(map);
Chris Mason593060d2008-03-25 16:50:33 -04006240
David Sterbacb5583d2018-09-07 16:11:23 +02006241 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006242 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006243 /*
6244 * Hold the semaphore for read during the whole operation, write is
6245 * requested at commit time but must wait.
6246 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006247 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006248 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006249
Stefan Behrensad6d6202012-11-06 15:06:47 +01006250 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006251 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006252 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6253 dev_replace->srcdev->devid,
6254 &mirror_num,
6255 &physical_to_patch_in_first_stripe);
6256 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006257 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006258 else
6259 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006260 } else if (mirror_num > map->num_stripes) {
6261 mirror_num = 0;
6262 }
6263
Chris Masonf2d8d742008-04-21 10:03:05 -04006264 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006265 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006266 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006267 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6268 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006269 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006270 mirror_num = 1;
David Sterbac7369b32019-05-31 15:39:31 +02006271 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006272 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006273 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006274 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006275 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006276 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006277 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006278 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006279 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006280 }
Chris Mason2fff7342008-04-29 14:12:09 -04006281
Chris Mason611f0e02008-04-03 16:29:03 -04006282 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006283 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006284 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006285 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006286 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006287 } else {
6288 mirror_num = 1;
6289 }
Chris Mason2fff7342008-04-29 14:12:09 -04006290
Chris Mason321aecc2008-04-16 10:49:51 -04006291 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006292 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006293
David Sterba47c57132015-02-20 18:43:47 +01006294 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006295 stripe_index *= map->sub_stripes;
6296
Anand Jainde483732017-10-12 16:43:00 +08006297 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006298 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006299 else if (mirror_num)
6300 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006301 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006302 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006303 stripe_index = find_live_mirror(fs_info, map,
6304 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006305 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006306 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006307 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006308
Zhao Leiffe2d202015-01-20 15:11:44 +08006309 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006310 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006311 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006312 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbacff82672019-05-17 11:43:45 +02006313 stripe_len * data_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05006314
6315 /* RAID[56] write or recovery. Return all stripes */
6316 num_stripes = map->num_stripes;
6317 max_errors = nr_parity_stripes(map);
6318
David Woodhouse53b381b2013-01-29 18:40:14 -05006319 *length = map->stripe_len;
6320 stripe_index = 0;
6321 stripe_offset = 0;
6322 } else {
6323 /*
6324 * Mirror #0 or #1 means the original data block.
6325 * Mirror #2 is RAID5 parity block.
6326 * Mirror #3 is RAID6 Q block.
6327 */
David Sterba47c57132015-02-20 18:43:47 +01006328 stripe_nr = div_u64_rem(stripe_nr,
David Sterbacff82672019-05-17 11:43:45 +02006329 data_stripes, &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006330 if (mirror_num > 1)
David Sterbacff82672019-05-17 11:43:45 +02006331 stripe_index = data_stripes + mirror_num - 2;
David Woodhouse53b381b2013-01-29 18:40:14 -05006332
6333 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006334 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6335 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006336 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006337 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006338 }
Chris Mason8790d502008-04-03 16:29:03 -04006339 } else {
6340 /*
David Sterba47c57132015-02-20 18:43:47 +01006341 * after this, stripe_nr is the number of stripes on this
6342 * device we have to walk to find the data, and stripe_index is
6343 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006344 */
David Sterba47c57132015-02-20 18:43:47 +01006345 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6346 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006347 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006348 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006349 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006350 btrfs_crit(fs_info,
6351 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006352 stripe_index, map->num_stripes);
6353 ret = -EINVAL;
6354 goto out;
6355 }
Chris Mason593060d2008-03-25 16:50:33 -04006356
Stefan Behrens472262f2012-11-06 14:43:46 +01006357 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006358 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006359 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006360 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006361 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006362 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006363 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006364 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006365
Zhao Lei6e9606d2015-01-20 15:11:34 +08006366 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006367 if (!bbio) {
6368 ret = -ENOMEM;
6369 goto out;
6370 }
Nikolay Borisov608769a2020-07-02 16:46:41 +03006371
6372 for (i = 0; i < num_stripes; i++) {
6373 bbio->stripes[i].physical = map->stripes[stripe_index].physical +
6374 stripe_offset + stripe_nr * map->stripe_len;
6375 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
6376 stripe_index++;
6377 }
Li Zefande11cc12011-12-01 12:55:47 +08006378
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006379 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006380 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6381 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006382 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006383 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006384
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006385 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006386 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006387
6388 /* Fill in the logical address of each stripe */
David Sterbacff82672019-05-17 11:43:45 +02006389 tmp = stripe_nr * data_stripes;
6390 for (i = 0; i < data_stripes; i++)
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006391 bbio->raid_map[(i+rot) % num_stripes] =
6392 em->start + (tmp + i) * map->stripe_len;
6393
6394 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6395 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6396 bbio->raid_map[(i+rot+1) % num_stripes] =
6397 RAID6_Q_STRIPE;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006398
Nikolay Borisov608769a2020-07-02 16:46:41 +03006399 sort_parity_stripes(bbio, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006400 }
Li Zefande11cc12011-12-01 12:55:47 +08006401
Liu Bo2b19a1f2017-03-14 13:34:00 -07006402 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006403 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006404
Liu Bo73c0f222017-03-14 13:33:58 -07006405 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006406 need_full_stripe(op)) {
Naohiro Aota6143c232021-02-04 19:22:12 +09006407 handle_ops_on_dev_replace(op, &bbio, dev_replace, logical,
6408 &num_stripes, &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006409 }
6410
Li Zefande11cc12011-12-01 12:55:47 +08006411 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006412 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006413 bbio->num_stripes = num_stripes;
6414 bbio->max_errors = max_errors;
6415 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006416
6417 /*
6418 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6419 * mirror_num == num_stripes + 1 && dev_replace target drive is
6420 * available as a mirror
6421 */
6422 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6423 WARN_ON(num_stripes > 1);
6424 bbio->stripes[0].dev = dev_replace->tgtdev;
6425 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6426 bbio->mirror_num = map->num_stripes + 1;
6427 }
Chris Masoncea9e442008-04-09 16:28:12 -04006428out:
Liu Bo73beece2015-07-17 16:49:19 +08006429 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006430 lockdep_assert_held(&dev_replace->rwsem);
6431 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006432 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006433 }
Chris Mason0b86a832008-03-24 15:01:56 -04006434 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006435 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006436}
6437
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006438int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006439 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006440 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006441{
David Sterba75fb2e92018-08-03 00:36:29 +02006442 if (op == BTRFS_MAP_DISCARD)
6443 return __btrfs_map_block_for_discard(fs_info, logical,
6444 length, bbio_ret);
6445
Mike Christieb3d3fa52016-06-05 14:31:53 -05006446 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006447 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006448}
6449
Miao Xieaf8e2d12014-10-23 14:42:50 +08006450/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006451int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006452 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006453 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006454{
David Sterba825ad4c2017-03-28 14:45:22 +02006455 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006456}
6457
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006458static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006459{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006460 bio->bi_private = bbio->private;
6461 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006462 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006463
Zhao Lei6e9606d2015-01-20 15:11:34 +08006464 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006465}
6466
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006467static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006468{
Chris Mason9be33952013-05-17 18:30:14 -04006469 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006470 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006471
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006472 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006473 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006474 if (bio->bi_status == BLK_STS_IOERR ||
6475 bio->bi_status == BLK_STS_TARGET) {
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006476 struct btrfs_device *dev = btrfs_io_bio(bio)->device;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006477
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006478 ASSERT(dev->bdev);
Naohiro Aotacfe94442021-02-04 19:21:59 +09006479 if (btrfs_op(bio) == BTRFS_MAP_WRITE)
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006480 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006481 BTRFS_DEV_STAT_WRITE_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006482 else if (!(bio->bi_opf & REQ_RAHEAD))
6483 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006484 BTRFS_DEV_STAT_READ_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006485 if (bio->bi_opf & REQ_PREFLUSH)
6486 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006487 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens442a4f62012-05-25 16:06:08 +02006488 }
6489 }
Chris Mason8790d502008-04-03 16:29:03 -04006490
Jan Schmidta1d3c472011-08-04 17:15:33 +02006491 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006492 is_orig_bio = 1;
6493
Miao Xiec404e0d2014-01-30 16:46:55 +08006494 btrfs_bio_counter_dec(bbio->fs_info);
6495
Jan Schmidta1d3c472011-08-04 17:15:33 +02006496 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006497 if (!is_orig_bio) {
6498 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006499 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006500 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006501
Chris Mason9be33952013-05-17 18:30:14 -04006502 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006503 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006504 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006505 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006506 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006507 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006508 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006509 /*
6510 * this bio is actually up to date, we didn't
6511 * go over the max number of errors
6512 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006513 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006514 }
Miao Xiec55f1392014-06-19 10:42:54 +08006515
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006516 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006517 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006518 bio_put(bio);
6519 }
Chris Mason8790d502008-04-03 16:29:03 -04006520}
6521
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006522static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006523 u64 physical, struct btrfs_device *dev)
Josef Bacikde1ee922012-10-19 16:50:56 -04006524{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006525 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006526
6527 bio->bi_private = bbio;
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006528 btrfs_io_bio(bio)->device = dev;
Josef Bacikde1ee922012-10-19 16:50:56 -04006529 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006530 bio->bi_iter.bi_sector = physical >> 9;
Naohiro Aotad8e3fb12021-02-04 19:22:05 +09006531 /*
6532 * For zone append writing, bi_sector must point the beginning of the
6533 * zone
6534 */
6535 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
6536 if (btrfs_dev_is_sequential(dev, physical)) {
6537 u64 zone_start = round_down(physical, fs_info->zone_size);
6538
6539 bio->bi_iter.bi_sector = zone_start >> SECTOR_SHIFT;
6540 } else {
6541 bio->bi_opf &= ~REQ_OP_ZONE_APPEND;
6542 bio->bi_opf |= REQ_OP_WRITE;
6543 }
6544 }
Misono Tomohiro672d5992018-08-02 16:19:07 +09006545 btrfs_debug_in_rcu(fs_info,
6546 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
David Sterba1201b582020-11-26 15:41:27 +01006547 bio_op(bio), bio->bi_opf, bio->bi_iter.bi_sector,
David Sterba1db45a32019-11-28 15:31:46 +01006548 (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name),
6549 dev->devid, bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006550 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006551
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006552 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006553
Chris Mason08635ba2019-07-10 12:28:14 -07006554 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006555}
6556
Josef Bacikde1ee922012-10-19 16:50:56 -04006557static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6558{
6559 atomic_inc(&bbio->error);
6560 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006561 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006562 WARN_ON(bio != bbio->orig_bio);
6563
Chris Mason9be33952013-05-17 18:30:14 -04006564 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006565 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006566 if (atomic_read(&bbio->error) > bbio->max_errors)
6567 bio->bi_status = BLK_STS_IOERR;
6568 else
6569 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006570 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006571 }
6572}
6573
Omar Sandoval58efbc92017-08-22 23:45:59 -07006574blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Chris Mason08635ba2019-07-10 12:28:14 -07006575 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04006576{
Chris Mason0b86a832008-03-24 15:01:56 -04006577 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006578 struct bio *first_bio = bio;
David Sterba1201b582020-11-26 15:41:27 +01006579 u64 logical = bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006580 u64 length = 0;
6581 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006582 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006583 int dev_nr;
6584 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006585 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006586
Kent Overstreet4f024f32013-10-11 15:44:27 -07006587 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006588 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006589
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006590 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006591 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006592 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006593 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006594 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006595 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006596 }
Chris Masoncea9e442008-04-09 16:28:12 -04006597
Jan Schmidta1d3c472011-08-04 17:15:33 +02006598 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006599 bbio->orig_bio = first_bio;
6600 bbio->private = first_bio->bi_private;
6601 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006602 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006603 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6604
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006605 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Naohiro Aotacfe94442021-02-04 19:21:59 +09006606 ((btrfs_op(bio) == BTRFS_MAP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006607 /* In this case, map_length has been set to the length of
6608 a single stripe; not the whole write */
Naohiro Aotacfe94442021-02-04 19:21:59 +09006609 if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006610 ret = raid56_parity_write(fs_info, bio, bbio,
6611 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006612 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006613 ret = raid56_parity_recover(fs_info, bio, bbio,
6614 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006615 }
Miao Xie42452152014-11-25 16:39:28 +08006616
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006617 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006618 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006619 }
6620
Chris Masoncea9e442008-04-09 16:28:12 -04006621 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006622 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006623 "mapping failed logical %llu bio len %llu len %llu",
6624 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006625 BUG();
6626 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006627
Zhao Lei08da7572015-02-12 15:42:16 +08006628 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006629 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006630 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6631 &dev->dev_state) ||
Naohiro Aotacfe94442021-02-04 19:21:59 +09006632 (btrfs_op(first_bio) == BTRFS_MAP_WRITE &&
Anand Jainebbede42017-12-04 12:54:52 +08006633 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006634 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006635 continue;
6636 }
6637
David Sterba3aa8e072017-06-02 17:38:30 +02006638 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006639 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006640 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006641 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006642
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006643 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, dev);
Chris Mason239b14b2008-03-24 15:02:07 -04006644 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006645 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006646 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006647}
6648
Anand Jain09ba3bc2019-01-19 14:48:55 +08006649/*
6650 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6651 * return NULL.
6652 *
6653 * If devid and uuid are both specified, the match must be exact, otherwise
6654 * only devid is used.
6655 *
6656 * If @seed is true, traverse through the seed devices.
6657 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006658struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jainb2598ed2020-11-03 13:49:43 +08006659 u64 devid, u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006660{
Yan Zheng2b820322008-11-17 21:11:30 -05006661 struct btrfs_device *device;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006662 struct btrfs_fs_devices *seed_devs;
Chris Mason0b86a832008-03-24 15:01:56 -04006663
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006664 if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6665 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6666 if (device->devid == devid &&
6667 (!uuid || memcmp(device->uuid, uuid,
6668 BTRFS_UUID_SIZE) == 0))
6669 return device;
6670 }
6671 }
6672
6673 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
Yan Zheng2b820322008-11-17 21:11:30 -05006674 if (!fsid ||
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006675 !memcmp(seed_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6676 list_for_each_entry(device, &seed_devs->devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006677 dev_list) {
6678 if (device->devid == devid &&
6679 (!uuid || memcmp(device->uuid, uuid,
6680 BTRFS_UUID_SIZE) == 0))
6681 return device;
6682 }
Yan Zheng2b820322008-11-17 21:11:30 -05006683 }
Yan Zheng2b820322008-11-17 21:11:30 -05006684 }
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006685
Yan Zheng2b820322008-11-17 21:11:30 -05006686 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006687}
6688
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006689static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006690 u64 devid, u8 *dev_uuid)
6691{
6692 struct btrfs_device *device;
Josef Bacikfccc0002020-08-31 10:52:42 -04006693 unsigned int nofs_flag;
Chris Masondfe25022008-05-13 13:46:40 -04006694
Josef Bacikfccc0002020-08-31 10:52:42 -04006695 /*
6696 * We call this under the chunk_mutex, so we want to use NOFS for this
6697 * allocation, however we don't want to change btrfs_alloc_device() to
6698 * always do NOFS because we use it in a lot of other GFP_KERNEL safe
6699 * places.
6700 */
6701 nofs_flag = memalloc_nofs_save();
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006702 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
Josef Bacikfccc0002020-08-31 10:52:42 -04006703 memalloc_nofs_restore(nofs_flag);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006704 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006705 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006706
6707 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006708 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006709 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006710
Anand Jaine6e674b2017-12-04 12:54:54 +08006711 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006712 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006713
Chris Masondfe25022008-05-13 13:46:40 -04006714 return device;
6715}
6716
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006717/**
6718 * btrfs_alloc_device - allocate struct btrfs_device
6719 * @fs_info: used only for generating a new devid, can be NULL if
6720 * devid is provided (i.e. @devid != NULL).
6721 * @devid: a pointer to devid for this device. If NULL a new devid
6722 * is generated.
6723 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6724 * is generated.
6725 *
6726 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006727 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006728 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006729 */
6730struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6731 const u64 *devid,
6732 const u8 *uuid)
6733{
6734 struct btrfs_device *dev;
6735 u64 tmp;
6736
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306737 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006738 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006739
Qu Wenruo154f7cb2020-08-20 15:42:46 +08006740 dev = __alloc_device(fs_info);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006741 if (IS_ERR(dev))
6742 return dev;
6743
6744 if (devid)
6745 tmp = *devid;
6746 else {
6747 int ret;
6748
6749 ret = find_next_devid(fs_info, &tmp);
6750 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006751 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006752 return ERR_PTR(ret);
6753 }
6754 }
6755 dev->devid = tmp;
6756
6757 if (uuid)
6758 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6759 else
6760 generate_random_uuid(dev->uuid);
6761
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006762 return dev;
6763}
6764
Anand Jain5a2b8e62017-10-09 11:07:45 +08006765static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006766 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006767{
Anand Jain2b902df2017-10-09 11:07:46 +08006768 if (error)
6769 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6770 devid, uuid);
6771 else
6772 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6773 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006774}
6775
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006776static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6777{
6778 int index = btrfs_bg_flags_to_raid_index(type);
6779 int ncopies = btrfs_raid_array[index].ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006780 const int nparity = btrfs_raid_array[index].nparity;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006781 int data_stripes;
6782
David Sterbae4f6c6b2019-05-14 01:59:54 +02006783 if (nparity)
6784 data_stripes = num_stripes - nparity;
6785 else
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006786 data_stripes = num_stripes / ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006787
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006788 return div_u64(chunk_len, data_stripes);
6789}
6790
David Sterba9690ac02019-03-20 16:43:07 +01006791static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006792 struct btrfs_chunk *chunk)
6793{
David Sterba9690ac02019-03-20 16:43:07 +01006794 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006795 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006796 struct map_lookup *map;
6797 struct extent_map *em;
6798 u64 logical;
6799 u64 length;
6800 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006801 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006802 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006803 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006804 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006805
Chris Masone17cade2008-04-15 15:41:47 -04006806 logical = key->offset;
6807 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006808 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006809
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006810 /*
6811 * Only need to verify chunk item if we're reading from sys chunk array,
6812 * as chunk item in tree block is already verified by tree-checker.
6813 */
6814 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006815 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006816 if (ret)
6817 return ret;
6818 }
Chris Masona061fc82008-05-07 11:43:44 -04006819
David Sterbac8bf1b62019-05-17 11:43:17 +02006820 read_lock(&map_tree->lock);
6821 em = lookup_extent_mapping(map_tree, logical, 1);
6822 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006823
6824 /* already mapped? */
6825 if (em && em->start <= logical && em->start + em->len > logical) {
6826 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006827 return 0;
6828 } else if (em) {
6829 free_extent_map(em);
6830 }
Chris Mason0b86a832008-03-24 15:01:56 -04006831
David Sterba172ddd62011-04-21 00:48:27 +02006832 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006833 if (!em)
6834 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006835 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006836 if (!map) {
6837 free_extent_map(em);
6838 return -ENOMEM;
6839 }
6840
Wang Shilong298a8f92014-06-19 10:42:52 +08006841 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006842 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006843 em->start = logical;
6844 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006845 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006846 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006847 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006848
Chris Mason593060d2008-03-25 16:50:33 -04006849 map->num_stripes = num_stripes;
6850 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6851 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006852 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6853 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006854 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006855 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006856 em->orig_block_len = calc_stripe_length(map->type, em->len,
6857 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006858 for (i = 0; i < num_stripes; i++) {
6859 map->stripes[i].physical =
6860 btrfs_stripe_offset_nr(leaf, chunk, i);
6861 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006862 read_extent_buffer(leaf, uuid, (unsigned long)
6863 btrfs_stripe_dev_uuid_nr(chunk, i),
6864 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006865 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jainb2598ed2020-11-03 13:49:43 +08006866 devid, uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006867 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006868 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006869 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006870 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006871 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006872 }
Chris Masondfe25022008-05-13 13:46:40 -04006873 if (!map->stripes[i].dev) {
6874 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006875 add_missing_dev(fs_info->fs_devices, devid,
6876 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006877 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006878 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006879 btrfs_err(fs_info,
6880 "failed to init missing dev %llu: %ld",
6881 devid, PTR_ERR(map->stripes[i].dev));
6882 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006883 }
Anand Jain2b902df2017-10-09 11:07:46 +08006884 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006885 }
Anand Jaine12c9622017-12-04 12:54:53 +08006886 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6887 &(map->stripes[i].dev->dev_state));
6888
Chris Mason0b86a832008-03-24 15:01:56 -04006889 }
6890
David Sterbac8bf1b62019-05-17 11:43:17 +02006891 write_lock(&map_tree->lock);
6892 ret = add_extent_mapping(map_tree, em, 0);
6893 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006894 if (ret < 0) {
6895 btrfs_err(fs_info,
6896 "failed to add chunk map, start=%llu len=%llu: %d",
6897 em->start, em->len, ret);
6898 }
Chris Mason0b86a832008-03-24 15:01:56 -04006899 free_extent_map(em);
6900
Qu Wenruo64f64f42018-08-01 10:37:20 +08006901 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006902}
6903
Jeff Mahoney143bede2012-03-01 14:56:26 +01006904static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006905 struct btrfs_dev_item *dev_item,
6906 struct btrfs_device *device)
6907{
6908 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006909
6910 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006911 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6912 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006913 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006914 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006915 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006916 device->type = btrfs_device_type(leaf, dev_item);
6917 device->io_align = btrfs_device_io_align(leaf, dev_item);
6918 device->io_width = btrfs_device_io_width(leaf, dev_item);
6919 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006920 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006921 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006922
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006923 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006924 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006925}
6926
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006927static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006928 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006929{
6930 struct btrfs_fs_devices *fs_devices;
6931 int ret;
6932
David Sterbaa32bf9a2018-03-16 02:21:22 +01006933 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006934 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006935
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03006936 /* This will match only for multi-device seed fs */
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006937 list_for_each_entry(fs_devices, &fs_info->fs_devices->seed_list, seed_list)
Anand Jain44880fd2017-07-29 17:50:09 +08006938 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006939 return fs_devices;
6940
Yan Zheng2b820322008-11-17 21:11:30 -05006941
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006942 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006943 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006944 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006945 return ERR_PTR(-ENOENT);
6946
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006947 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006948 if (IS_ERR(fs_devices))
6949 return fs_devices;
6950
Johannes Thumshirn0395d842019-11-13 11:27:27 +01006951 fs_devices->seeding = true;
Miao Xie5f375832014-09-03 21:35:46 +08006952 fs_devices->opened = 1;
6953 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006954 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006955
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03006956 /*
6957 * Upon first call for a seed fs fsid, just create a private copy of the
6958 * respective fs_devices and anchor it at fs_info->fs_devices->seed_list
6959 */
Yan Zhenge4404d62008-12-12 10:03:26 -05006960 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006961 if (IS_ERR(fs_devices))
6962 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006963
Anand Jain897fb572018-04-12 10:29:28 +08006964 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006965 if (ret) {
6966 free_fs_devices(fs_devices);
Anand Jainc83b60c2020-09-05 01:34:35 +08006967 return ERR_PTR(ret);
Julia Lawall48d28232012-04-14 11:24:33 +02006968 }
Yan Zheng2b820322008-11-17 21:11:30 -05006969
6970 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006971 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006972 free_fs_devices(fs_devices);
Anand Jainc83b60c2020-09-05 01:34:35 +08006973 return ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006974 }
6975
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006976 list_add(&fs_devices->seed_list, &fs_info->fs_devices->seed_list);
Anand Jainc83b60c2020-09-05 01:34:35 +08006977
Miao Xie5f375832014-09-03 21:35:46 +08006978 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006979}
6980
David Sterba17850752019-03-20 16:45:15 +01006981static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006982 struct btrfs_dev_item *dev_item)
6983{
David Sterba17850752019-03-20 16:45:15 +01006984 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006985 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006986 struct btrfs_device *device;
6987 u64 devid;
6988 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006989 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006990 u8 dev_uuid[BTRFS_UUID_SIZE];
6991
Chris Mason0b86a832008-03-24 15:01:56 -04006992 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006993 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006994 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006995 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006996 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006997
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006998 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006999 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007000 if (IS_ERR(fs_devices))
7001 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05007002 }
7003
Anand Jaine4319cd2019-01-17 23:32:31 +08007004 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jainb2598ed2020-11-03 13:49:43 +08007005 fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08007006 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08007007 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08007008 btrfs_report_missing_device(fs_info, devid,
7009 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007010 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08007011 }
Yan Zheng2b820322008-11-17 21:11:30 -05007012
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007013 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08007014 if (IS_ERR(device)) {
7015 btrfs_err(fs_info,
7016 "failed to add missing dev %llu: %ld",
7017 devid, PTR_ERR(device));
7018 return PTR_ERR(device);
7019 }
Anand Jain2b902df2017-10-09 11:07:46 +08007020 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08007021 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08007022 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08007023 if (!btrfs_test_opt(fs_info, DEGRADED)) {
7024 btrfs_report_missing_device(fs_info,
7025 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08007026 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08007027 }
7028 btrfs_report_missing_device(fs_info, devid,
7029 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08007030 }
Miao Xie5f375832014-09-03 21:35:46 +08007031
Anand Jaine6e674b2017-12-04 12:54:54 +08007032 if (!device->bdev &&
7033 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05007034 /*
7035 * this happens when a device that was properly setup
7036 * in the device info lists suddenly goes bad.
7037 * device->bdev is NULL, and so we have to set
7038 * device->missing to one here
7039 */
Miao Xie5f375832014-09-03 21:35:46 +08007040 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08007041 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05007042 }
Miao Xie5f375832014-09-03 21:35:46 +08007043
7044 /* Move the device to its own fs_devices */
7045 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08007046 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
7047 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08007048
7049 list_move(&device->dev_list, &fs_devices->devices);
7050 device->fs_devices->num_devices--;
7051 fs_devices->num_devices++;
7052
7053 device->fs_devices->missing_devices--;
7054 fs_devices->missing_devices++;
7055
7056 device->fs_devices = fs_devices;
7057 }
Yan Zheng2b820322008-11-17 21:11:30 -05007058 }
7059
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007060 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08007061 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05007062 if (device->generation !=
7063 btrfs_device_generation(leaf, dev_item))
7064 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04007065 }
Chris Mason0b86a832008-03-24 15:01:56 -04007066
7067 fill_device_from_item(leaf, dev_item, device);
Anand Jain3a160a92020-11-03 13:49:42 +08007068 if (device->bdev) {
7069 u64 max_total_bytes = i_size_read(device->bdev->bd_inode);
7070
7071 if (device->total_bytes > max_total_bytes) {
7072 btrfs_err(fs_info,
7073 "device total_bytes should be at most %llu but found %llu",
7074 max_total_bytes, device->total_bytes);
7075 return -EINVAL;
7076 }
7077 }
Anand Jaine12c9622017-12-04 12:54:53 +08007078 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08007079 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08007080 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05007081 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03007082 atomic64_add(device->total_bytes - device->bytes_used,
7083 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04007084 }
Chris Mason0b86a832008-03-24 15:01:56 -04007085 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007086 return ret;
7087}
7088
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007089int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007090{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007091 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007092 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04007093 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04007094 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04007095 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01007096 u8 *array_ptr;
7097 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04007098 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007099 u32 num_stripes;
7100 u32 array_size;
7101 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01007102 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07007103 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04007104 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04007105
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007106 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02007107 /*
7108 * This will create extent buffer of nodesize, superblock size is
7109 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
7110 * overallocate but we can keep it as-is, only the first page is used.
7111 */
Josef Bacik3fbaf252020-11-05 10:45:20 -05007112 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET,
7113 root->root_key.objectid, 0);
Liu Boc871b0f2016-06-06 12:01:23 -07007114 if (IS_ERR(sb))
7115 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01007116 set_extent_buffer_uptodate(sb);
David Sterba8a334422011-10-07 18:06:13 +02007117 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04007118 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01007119 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02007120 * pages up-to-date when the page is larger: extent does not cover the
7121 * whole page and consequently check_page_uptodate does not find all
7122 * the page's extents up-to-date (the hole beyond sb),
7123 * write_extent_buffer then triggers a WARN_ON.
7124 *
7125 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
7126 * but sb spans only this function. Add an explicit SetPageUptodate call
7127 * to silence the warning eg. on PowerPC 64.
7128 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007129 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007130 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007131
Chris Masona061fc82008-05-07 11:43:44 -04007132 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007133 array_size = btrfs_super_sys_array_size(super_copy);
7134
David Sterba1ffb22c2014-10-31 19:02:42 +01007135 array_ptr = super_copy->sys_chunk_array;
7136 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7137 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007138
David Sterba1ffb22c2014-10-31 19:02:42 +01007139 while (cur_offset < array_size) {
7140 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007141 len = sizeof(*disk_key);
7142 if (cur_offset + len > array_size)
7143 goto out_short_read;
7144
Chris Mason0b86a832008-03-24 15:01:56 -04007145 btrfs_disk_key_to_cpu(&key, disk_key);
7146
David Sterba1ffb22c2014-10-31 19:02:42 +01007147 array_ptr += len;
7148 sb_array_offset += len;
7149 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007150
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02007151 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007152 btrfs_err(fs_info,
7153 "unexpected item type %u in sys_array at offset %u",
7154 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007155 ret = -EIO;
7156 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007157 }
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02007158
7159 chunk = (struct btrfs_chunk *)sb_array_offset;
7160 /*
7161 * At least one btrfs_chunk with one stripe must be present,
7162 * exact stripe count check comes afterwards
7163 */
7164 len = btrfs_chunk_item_size(1);
7165 if (cur_offset + len > array_size)
7166 goto out_short_read;
7167
7168 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
7169 if (!num_stripes) {
7170 btrfs_err(fs_info,
7171 "invalid number of stripes %u in sys_array at offset %u",
7172 num_stripes, cur_offset);
7173 ret = -EIO;
7174 break;
7175 }
7176
7177 type = btrfs_chunk_type(sb, chunk);
7178 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
7179 btrfs_err(fs_info,
7180 "invalid chunk type %llu in sys_array at offset %u",
7181 type, cur_offset);
7182 ret = -EIO;
7183 break;
7184 }
7185
7186 len = btrfs_chunk_item_size(num_stripes);
7187 if (cur_offset + len > array_size)
7188 goto out_short_read;
7189
7190 ret = read_one_chunk(&key, sb, chunk);
7191 if (ret)
7192 break;
7193
David Sterba1ffb22c2014-10-31 19:02:42 +01007194 array_ptr += len;
7195 sb_array_offset += len;
7196 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007197 }
Liu Bod8651772016-06-03 17:41:42 -07007198 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007199 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007200 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007201
7202out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007203 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007204 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007205 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007206 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007207 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007208}
7209
Qu Wenruo21634a12017-03-09 09:34:36 +08007210/*
7211 * Check if all chunks in the fs are OK for read-write degraded mount
7212 *
Anand Jain6528b992017-12-18 17:08:59 +08007213 * If the @failing_dev is specified, it's accounted as missing.
7214 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007215 * Return true if all chunks meet the minimal RW mount requirements.
7216 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7217 */
Anand Jain6528b992017-12-18 17:08:59 +08007218bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7219 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007220{
David Sterbac8bf1b62019-05-17 11:43:17 +02007221 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08007222 struct extent_map *em;
7223 u64 next_start = 0;
7224 bool ret = true;
7225
David Sterbac8bf1b62019-05-17 11:43:17 +02007226 read_lock(&map_tree->lock);
7227 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7228 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007229 /* No chunk at all? Return false anyway */
7230 if (!em) {
7231 ret = false;
7232 goto out;
7233 }
7234 while (em) {
7235 struct map_lookup *map;
7236 int missing = 0;
7237 int max_tolerated;
7238 int i;
7239
7240 map = em->map_lookup;
7241 max_tolerated =
7242 btrfs_get_num_tolerated_disk_barrier_failures(
7243 map->type);
7244 for (i = 0; i < map->num_stripes; i++) {
7245 struct btrfs_device *dev = map->stripes[i].dev;
7246
Anand Jaine6e674b2017-12-04 12:54:54 +08007247 if (!dev || !dev->bdev ||
7248 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007249 dev->last_flush_error)
7250 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007251 else if (failing_dev && failing_dev == dev)
7252 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007253 }
7254 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007255 if (!failing_dev)
7256 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007257 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007258 em->start, missing, max_tolerated);
7259 free_extent_map(em);
7260 ret = false;
7261 goto out;
7262 }
7263 next_start = extent_map_end(em);
7264 free_extent_map(em);
7265
David Sterbac8bf1b62019-05-17 11:43:17 +02007266 read_lock(&map_tree->lock);
7267 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08007268 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02007269 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007270 }
7271out:
7272 return ret;
7273}
7274
David Sterbad85327b12020-07-08 22:55:14 +02007275static void readahead_tree_node_children(struct extent_buffer *node)
7276{
7277 int i;
7278 const int nr_items = btrfs_header_nritems(node);
7279
Josef Bacikbfb484d2020-11-05 10:45:09 -05007280 for (i = 0; i < nr_items; i++)
7281 btrfs_readahead_node_child(node, i);
David Sterbad85327b12020-07-08 22:55:14 +02007282}
7283
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007284int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007285{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007286 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007287 struct btrfs_path *path;
7288 struct extent_buffer *leaf;
7289 struct btrfs_key key;
7290 struct btrfs_key found_key;
7291 int ret;
7292 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007293 u64 total_dev = 0;
David Sterbad85327b12020-07-08 22:55:14 +02007294 u64 last_ra_node = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007295
Chris Mason0b86a832008-03-24 15:01:56 -04007296 path = btrfs_alloc_path();
7297 if (!path)
7298 return -ENOMEM;
7299
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007300 /*
7301 * uuid_mutex is needed only if we are mounting a sprout FS
7302 * otherwise we don't need it.
7303 */
Li Zefanb367e472011-12-07 11:38:24 +08007304 mutex_lock(&uuid_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007305
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007306 /*
Boris Burkov48cfa612020-07-16 13:29:46 -07007307 * It is possible for mount and umount to race in such a way that
7308 * we execute this code path, but open_fs_devices failed to clear
7309 * total_rw_bytes. We certainly want it cleared before reading the
7310 * device items, so clear it here.
7311 */
7312 fs_info->fs_devices->total_rw_bytes = 0;
7313
7314 /*
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007315 * Read all device items, and then all the chunk items. All
7316 * device items are found before any chunk item (their object id
7317 * is smaller than the lowest possible object id for a chunk
7318 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007319 */
7320 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7321 key.offset = 0;
7322 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007323 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007324 if (ret < 0)
7325 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007326 while (1) {
David Sterbad85327b12020-07-08 22:55:14 +02007327 struct extent_buffer *node;
7328
Chris Mason0b86a832008-03-24 15:01:56 -04007329 leaf = path->nodes[0];
7330 slot = path->slots[0];
7331 if (slot >= btrfs_header_nritems(leaf)) {
7332 ret = btrfs_next_leaf(root, path);
7333 if (ret == 0)
7334 continue;
7335 if (ret < 0)
7336 goto error;
7337 break;
7338 }
David Sterbad85327b12020-07-08 22:55:14 +02007339 /*
7340 * The nodes on level 1 are not locked but we don't need to do
7341 * that during mount time as nothing else can access the tree
7342 */
7343 node = path->nodes[1];
7344 if (node) {
7345 if (last_ra_node != node->start) {
7346 readahead_tree_node_children(node);
7347 last_ra_node = node->start;
7348 }
7349 }
Chris Mason0b86a832008-03-24 15:01:56 -04007350 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007351 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7352 struct btrfs_dev_item *dev_item;
7353 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007354 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01007355 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007356 if (ret)
7357 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007358 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007359 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7360 struct btrfs_chunk *chunk;
7361 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007362 mutex_lock(&fs_info->chunk_mutex);
David Sterba9690ac02019-03-20 16:43:07 +01007363 ret = read_one_chunk(&found_key, leaf, chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007364 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05007365 if (ret)
7366 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007367 }
7368 path->slots[0]++;
7369 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007370
7371 /*
7372 * After loading chunk tree, we've got all device information,
7373 * do another round of validation checks.
7374 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007375 if (total_dev != fs_info->fs_devices->total_devices) {
7376 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007377 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007378 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007379 total_dev);
7380 ret = -EINVAL;
7381 goto error;
7382 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007383 if (btrfs_super_total_bytes(fs_info->super_copy) <
7384 fs_info->fs_devices->total_rw_bytes) {
7385 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007386 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007387 btrfs_super_total_bytes(fs_info->super_copy),
7388 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007389 ret = -EINVAL;
7390 goto error;
7391 }
Chris Mason0b86a832008-03-24 15:01:56 -04007392 ret = 0;
7393error:
Li Zefanb367e472011-12-07 11:38:24 +08007394 mutex_unlock(&uuid_mutex);
7395
Yan Zheng2b820322008-11-17 21:11:30 -05007396 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007397 return ret;
7398}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007399
Miao Xiecb517ea2013-05-15 07:48:19 +00007400void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7401{
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007402 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
Miao Xiecb517ea2013-05-15 07:48:19 +00007403 struct btrfs_device *device;
7404
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007405 fs_devices->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007406
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007407 mutex_lock(&fs_devices->device_list_mutex);
7408 list_for_each_entry(device, &fs_devices->devices, dev_list)
7409 device->fs_info = fs_info;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007410
7411 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007412 list_for_each_entry(device, &seed_devs->devices, dev_list)
7413 device->fs_info = fs_info;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007414
7415 seed_devs->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007416 }
Anand Jaine17125b2020-09-05 01:34:31 +08007417 mutex_unlock(&fs_devices->device_list_mutex);
Miao Xiecb517ea2013-05-15 07:48:19 +00007418}
7419
David Sterba1dc990d2019-08-21 20:05:32 +02007420static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7421 const struct btrfs_dev_stats_item *ptr,
7422 int index)
7423{
7424 u64 val;
7425
7426 read_extent_buffer(eb, &val,
7427 offsetof(struct btrfs_dev_stats_item, values) +
7428 ((unsigned long)ptr) + (index * sizeof(u64)),
7429 sizeof(val));
7430 return val;
7431}
7432
7433static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7434 struct btrfs_dev_stats_item *ptr,
7435 int index, u64 val)
7436{
7437 write_extent_buffer(eb, &val,
7438 offsetof(struct btrfs_dev_stats_item, values) +
7439 ((unsigned long)ptr) + (index * sizeof(u64)),
7440 sizeof(val));
7441}
7442
Josef Bacik92e26df2020-09-18 16:44:33 -04007443static int btrfs_device_init_dev_stats(struct btrfs_device *device,
7444 struct btrfs_path *path)
Josef Bacik124604e2020-09-18 16:44:32 -04007445{
7446 struct btrfs_dev_stats_item *ptr;
7447 struct extent_buffer *eb;
7448 struct btrfs_key key;
7449 int item_size;
7450 int i, ret, slot;
7451
Josef Bacik82d62d02021-03-11 11:23:15 -05007452 if (!device->fs_info->dev_root)
7453 return 0;
7454
Josef Bacik124604e2020-09-18 16:44:32 -04007455 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7456 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7457 key.offset = device->devid;
7458 ret = btrfs_search_slot(NULL, device->fs_info->dev_root, &key, path, 0, 0);
7459 if (ret) {
7460 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7461 btrfs_dev_stat_set(device, i, 0);
7462 device->dev_stats_valid = 1;
7463 btrfs_release_path(path);
Josef Bacik92e26df2020-09-18 16:44:33 -04007464 return ret < 0 ? ret : 0;
Josef Bacik124604e2020-09-18 16:44:32 -04007465 }
7466 slot = path->slots[0];
7467 eb = path->nodes[0];
7468 item_size = btrfs_item_size_nr(eb, slot);
7469
7470 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_stats_item);
7471
7472 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7473 if (item_size >= (1 + i) * sizeof(__le64))
7474 btrfs_dev_stat_set(device, i,
7475 btrfs_dev_stats_value(eb, ptr, i));
7476 else
7477 btrfs_dev_stat_set(device, i, 0);
7478 }
7479
7480 device->dev_stats_valid = 1;
7481 btrfs_dev_stat_print_on_load(device);
7482 btrfs_release_path(path);
Josef Bacik92e26df2020-09-18 16:44:33 -04007483
7484 return 0;
Josef Bacik124604e2020-09-18 16:44:32 -04007485}
7486
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007487int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7488{
Josef Bacik124604e2020-09-18 16:44:32 -04007489 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007490 struct btrfs_device *device;
7491 struct btrfs_path *path = NULL;
Josef Bacik92e26df2020-09-18 16:44:33 -04007492 int ret = 0;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007493
7494 path = btrfs_alloc_path();
Anand Jain3b80a9842019-08-21 17:26:32 +08007495 if (!path)
7496 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007497
7498 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik92e26df2020-09-18 16:44:33 -04007499 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7500 ret = btrfs_device_init_dev_stats(device, path);
7501 if (ret)
7502 goto out;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007503 }
Josef Bacik92e26df2020-09-18 16:44:33 -04007504 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
7505 list_for_each_entry(device, &seed_devs->devices, dev_list) {
7506 ret = btrfs_device_init_dev_stats(device, path);
7507 if (ret)
7508 goto out;
7509 }
7510 }
7511out:
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007512 mutex_unlock(&fs_devices->device_list_mutex);
7513
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007514 btrfs_free_path(path);
Josef Bacik92e26df2020-09-18 16:44:33 -04007515 return ret;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007516}
7517
7518static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007519 struct btrfs_device *device)
7520{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007521 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007522 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007523 struct btrfs_path *path;
7524 struct btrfs_key key;
7525 struct extent_buffer *eb;
7526 struct btrfs_dev_stats_item *ptr;
7527 int ret;
7528 int i;
7529
David Sterba242e2952016-01-25 17:51:31 +01007530 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7531 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007532 key.offset = device->devid;
7533
7534 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007535 if (!path)
7536 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007537 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7538 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007539 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007540 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007541 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007542 goto out;
7543 }
7544
7545 if (ret == 0 &&
7546 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7547 /* need to delete old one and insert a new one */
7548 ret = btrfs_del_item(trans, dev_root, path);
7549 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007550 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007551 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007552 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007553 goto out;
7554 }
7555 ret = 1;
7556 }
7557
7558 if (ret == 1) {
7559 /* need to insert a new item */
7560 btrfs_release_path(path);
7561 ret = btrfs_insert_empty_item(trans, dev_root, path,
7562 &key, sizeof(*ptr));
7563 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007564 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007565 "insert dev_stats item for device %s failed %d",
7566 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007567 goto out;
7568 }
7569 }
7570
7571 eb = path->nodes[0];
7572 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7573 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7574 btrfs_set_dev_stats_value(eb, ptr, i,
7575 btrfs_dev_stat_read(device, i));
7576 btrfs_mark_buffer_dirty(eb);
7577
7578out:
7579 btrfs_free_path(path);
7580 return ret;
7581}
7582
7583/*
7584 * called from commit_transaction. Writes all changed device stats to disk.
7585 */
David Sterba196c9d82019-03-20 16:50:38 +01007586int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007587{
David Sterba196c9d82019-03-20 16:50:38 +01007588 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007589 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7590 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007591 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007592 int ret = 0;
7593
7594 mutex_lock(&fs_devices->device_list_mutex);
7595 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007596 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7597 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007598 continue;
7599
Nikolay Borisov9deae962017-10-24 13:47:37 +03007600
7601 /*
7602 * There is a LOAD-LOAD control dependency between the value of
7603 * dev_stats_ccnt and updating the on-disk values which requires
7604 * reading the in-memory counters. Such control dependencies
7605 * require explicit read memory barriers.
7606 *
7607 * This memory barriers pairs with smp_mb__before_atomic in
7608 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7609 * barrier implied by atomic_xchg in
7610 * btrfs_dev_stats_read_and_reset
7611 */
7612 smp_rmb();
7613
Nikolay Borisov5495f192018-07-20 19:37:49 +03007614 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007615 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007616 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007617 }
7618 mutex_unlock(&fs_devices->device_list_mutex);
7619
7620 return ret;
7621}
7622
Stefan Behrens442a4f62012-05-25 16:06:08 +02007623void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7624{
7625 btrfs_dev_stat_inc(dev, index);
7626 btrfs_dev_stat_print_on_error(dev);
7627}
7628
Eric Sandeen48a3b632013-04-25 20:41:01 +00007629static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007630{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007631 if (!dev->dev_stats_valid)
7632 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007633 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007634 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007635 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007636 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7637 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7638 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007639 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7640 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007641}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007642
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007643static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7644{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007645 int i;
7646
7647 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7648 if (btrfs_dev_stat_read(dev, i) != 0)
7649 break;
7650 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7651 return; /* all values == 0, suppress message */
7652
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007653 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007654 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007655 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007656 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7657 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7658 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7659 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7660 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7661}
7662
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007663int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007664 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007665{
7666 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007667 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007668 int i;
7669
7670 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainb2598ed2020-11-03 13:49:43 +08007671 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007672 mutex_unlock(&fs_devices->device_list_mutex);
7673
7674 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007675 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007676 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007677 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007678 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007679 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007680 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007681 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7682 if (stats->nr_items > i)
7683 stats->values[i] =
7684 btrfs_dev_stat_read_and_reset(dev, i);
7685 else
Anand Jain4e411a72019-08-07 16:21:19 +08007686 btrfs_dev_stat_set(dev, i, 0);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007687 }
Anand Jaina69976b2020-01-10 12:26:34 +08007688 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7689 current->comm, task_pid_nr(current));
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007690 } else {
7691 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7692 if (stats->nr_items > i)
7693 stats->values[i] = btrfs_dev_stat_read(dev, i);
7694 }
7695 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7696 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7697 return 0;
7698}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007699
Miao Xie935e5cc2014-09-03 21:35:33 +08007700/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007701 * Update the size and bytes used for each device where it changed. This is
7702 * delayed since we would otherwise get errors while writing out the
7703 * superblocks.
7704 *
7705 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007706 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007707void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007708{
Miao Xie935e5cc2014-09-03 21:35:33 +08007709 struct btrfs_device *curr, *next;
7710
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007711 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7712
7713 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007714 return;
7715
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007716 /*
7717 * We don't need the device_list_mutex here. This list is owned by the
7718 * transaction and the transaction must complete before the device is
7719 * released.
7720 */
7721 mutex_lock(&trans->fs_info->chunk_mutex);
7722 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7723 post_commit_list) {
7724 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007725 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007726 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007727 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007728 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007729}
Anand Jain5a13f432015-03-10 06:38:31 +08007730
David Sterba46df06b2018-07-13 20:46:30 +02007731/*
7732 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7733 */
7734int btrfs_bg_type_to_factor(u64 flags)
7735{
David Sterba44b28ad2019-05-17 11:43:31 +02007736 const int index = btrfs_bg_flags_to_raid_index(flags);
7737
7738 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007739}
Qu Wenruocf90d882018-08-01 10:37:19 +08007740
7741
Qu Wenruocf90d882018-08-01 10:37:19 +08007742
7743static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7744 u64 chunk_offset, u64 devid,
7745 u64 physical_offset, u64 physical_len)
7746{
David Sterbac8bf1b62019-05-17 11:43:17 +02007747 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007748 struct extent_map *em;
7749 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007750 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007751 u64 stripe_len;
7752 bool found = false;
7753 int ret = 0;
7754 int i;
7755
7756 read_lock(&em_tree->lock);
7757 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7758 read_unlock(&em_tree->lock);
7759
7760 if (!em) {
7761 btrfs_err(fs_info,
7762"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7763 physical_offset, devid);
7764 ret = -EUCLEAN;
7765 goto out;
7766 }
7767
7768 map = em->map_lookup;
7769 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7770 if (physical_len != stripe_len) {
7771 btrfs_err(fs_info,
7772"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7773 physical_offset, devid, em->start, physical_len,
7774 stripe_len);
7775 ret = -EUCLEAN;
7776 goto out;
7777 }
7778
7779 for (i = 0; i < map->num_stripes; i++) {
7780 if (map->stripes[i].dev->devid == devid &&
7781 map->stripes[i].physical == physical_offset) {
7782 found = true;
7783 if (map->verified_stripes >= map->num_stripes) {
7784 btrfs_err(fs_info,
7785 "too many dev extents for chunk %llu found",
7786 em->start);
7787 ret = -EUCLEAN;
7788 goto out;
7789 }
7790 map->verified_stripes++;
7791 break;
7792 }
7793 }
7794 if (!found) {
7795 btrfs_err(fs_info,
7796 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7797 physical_offset, devid);
7798 ret = -EUCLEAN;
7799 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007800
7801 /* Make sure no dev extent is beyond device bondary */
Anand Jainb2598ed2020-11-03 13:49:43 +08007802 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007803 if (!dev) {
7804 btrfs_err(fs_info, "failed to find devid %llu", devid);
7805 ret = -EUCLEAN;
7806 goto out;
7807 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007808
Qu Wenruo05a37c42018-10-05 17:45:55 +08007809 if (physical_offset + physical_len > dev->disk_total_bytes) {
7810 btrfs_err(fs_info,
7811"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7812 devid, physical_offset, physical_len,
7813 dev->disk_total_bytes);
7814 ret = -EUCLEAN;
7815 goto out;
7816 }
Naohiro Aota381a6962021-02-04 19:21:49 +09007817
7818 if (dev->zone_info) {
7819 u64 zone_size = dev->zone_info->zone_size;
7820
7821 if (!IS_ALIGNED(physical_offset, zone_size) ||
7822 !IS_ALIGNED(physical_len, zone_size)) {
7823 btrfs_err(fs_info,
7824"zoned: dev extent devid %llu physical offset %llu len %llu is not aligned to device zone",
7825 devid, physical_offset, physical_len);
7826 ret = -EUCLEAN;
7827 goto out;
7828 }
7829 }
7830
Qu Wenruocf90d882018-08-01 10:37:19 +08007831out:
7832 free_extent_map(em);
7833 return ret;
7834}
7835
7836static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7837{
David Sterbac8bf1b62019-05-17 11:43:17 +02007838 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007839 struct extent_map *em;
7840 struct rb_node *node;
7841 int ret = 0;
7842
7843 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007844 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007845 em = rb_entry(node, struct extent_map, rb_node);
7846 if (em->map_lookup->num_stripes !=
7847 em->map_lookup->verified_stripes) {
7848 btrfs_err(fs_info,
7849 "chunk %llu has missing dev extent, have %d expect %d",
7850 em->start, em->map_lookup->verified_stripes,
7851 em->map_lookup->num_stripes);
7852 ret = -EUCLEAN;
7853 goto out;
7854 }
7855 }
7856out:
7857 read_unlock(&em_tree->lock);
7858 return ret;
7859}
7860
7861/*
7862 * Ensure that all dev extents are mapped to correct chunk, otherwise
7863 * later chunk allocation/free would cause unexpected behavior.
7864 *
7865 * NOTE: This will iterate through the whole device tree, which should be of
7866 * the same size level as the chunk tree. This slightly increases mount time.
7867 */
7868int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7869{
7870 struct btrfs_path *path;
7871 struct btrfs_root *root = fs_info->dev_root;
7872 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007873 u64 prev_devid = 0;
7874 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007875 int ret = 0;
7876
Josef Bacik42437a62020-10-16 11:29:18 -04007877 /*
7878 * We don't have a dev_root because we mounted with ignorebadroots and
7879 * failed to load the root, so we want to skip the verification in this
7880 * case for sure.
7881 *
7882 * However if the dev root is fine, but the tree itself is corrupted
7883 * we'd still fail to mount. This verification is only to make sure
7884 * writes can happen safely, so instead just bypass this check
7885 * completely in the case of IGNOREBADROOTS.
7886 */
7887 if (btrfs_test_opt(fs_info, IGNOREBADROOTS))
7888 return 0;
7889
Qu Wenruocf90d882018-08-01 10:37:19 +08007890 key.objectid = 1;
7891 key.type = BTRFS_DEV_EXTENT_KEY;
7892 key.offset = 0;
7893
7894 path = btrfs_alloc_path();
7895 if (!path)
7896 return -ENOMEM;
7897
7898 path->reada = READA_FORWARD;
7899 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7900 if (ret < 0)
7901 goto out;
7902
7903 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7904 ret = btrfs_next_item(root, path);
7905 if (ret < 0)
7906 goto out;
7907 /* No dev extents at all? Not good */
7908 if (ret > 0) {
7909 ret = -EUCLEAN;
7910 goto out;
7911 }
7912 }
7913 while (1) {
7914 struct extent_buffer *leaf = path->nodes[0];
7915 struct btrfs_dev_extent *dext;
7916 int slot = path->slots[0];
7917 u64 chunk_offset;
7918 u64 physical_offset;
7919 u64 physical_len;
7920 u64 devid;
7921
7922 btrfs_item_key_to_cpu(leaf, &key, slot);
7923 if (key.type != BTRFS_DEV_EXTENT_KEY)
7924 break;
7925 devid = key.objectid;
7926 physical_offset = key.offset;
7927
7928 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7929 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7930 physical_len = btrfs_dev_extent_length(leaf, dext);
7931
Qu Wenruo5eb19382018-10-05 17:45:54 +08007932 /* Check if this dev extent overlaps with the previous one */
7933 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7934 btrfs_err(fs_info,
7935"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7936 devid, physical_offset, prev_dev_ext_end);
7937 ret = -EUCLEAN;
7938 goto out;
7939 }
7940
Qu Wenruocf90d882018-08-01 10:37:19 +08007941 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7942 physical_offset, physical_len);
7943 if (ret < 0)
7944 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007945 prev_devid = devid;
7946 prev_dev_ext_end = physical_offset + physical_len;
7947
Qu Wenruocf90d882018-08-01 10:37:19 +08007948 ret = btrfs_next_item(root, path);
7949 if (ret < 0)
7950 goto out;
7951 if (ret > 0) {
7952 ret = 0;
7953 break;
7954 }
7955 }
7956
7957 /* Ensure all chunks have corresponding dev extents */
7958 ret = verify_chunk_dev_extent_mapping(fs_info);
7959out:
7960 btrfs_free_path(path);
7961 return ret;
7962}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007963
7964/*
7965 * Check whether the given block group or device is pinned by any inode being
7966 * used as a swapfile.
7967 */
7968bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7969{
7970 struct btrfs_swapfile_pin *sp;
7971 struct rb_node *node;
7972
7973 spin_lock(&fs_info->swapfile_pins_lock);
7974 node = fs_info->swapfile_pins.rb_node;
7975 while (node) {
7976 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7977 if (ptr < sp->ptr)
7978 node = node->rb_left;
7979 else if (ptr > sp->ptr)
7980 node = node->rb_right;
7981 else
7982 break;
7983 }
7984 spin_unlock(&fs_info->swapfile_pins_lock);
7985 return node != NULL;
7986}
Naohiro Aotaf7ef5282021-02-04 19:22:16 +09007987
7988static int relocating_repair_kthread(void *data)
7989{
7990 struct btrfs_block_group *cache = (struct btrfs_block_group *)data;
7991 struct btrfs_fs_info *fs_info = cache->fs_info;
7992 u64 target;
7993 int ret = 0;
7994
7995 target = cache->start;
7996 btrfs_put_block_group(cache);
7997
7998 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE)) {
7999 btrfs_info(fs_info,
8000 "zoned: skip relocating block group %llu to repair: EBUSY",
8001 target);
8002 return -EBUSY;
8003 }
8004
8005 mutex_lock(&fs_info->delete_unused_bgs_mutex);
8006
8007 /* Ensure block group still exists */
8008 cache = btrfs_lookup_block_group(fs_info, target);
8009 if (!cache)
8010 goto out;
8011
8012 if (!cache->relocating_repair)
8013 goto out;
8014
8015 ret = btrfs_may_alloc_data_chunk(fs_info, target);
8016 if (ret < 0)
8017 goto out;
8018
8019 btrfs_info(fs_info,
8020 "zoned: relocating block group %llu to repair IO failure",
8021 target);
8022 ret = btrfs_relocate_chunk(fs_info, target);
8023
8024out:
8025 if (cache)
8026 btrfs_put_block_group(cache);
8027 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
8028 btrfs_exclop_finish(fs_info);
8029
8030 return ret;
8031}
8032
8033int btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical)
8034{
8035 struct btrfs_block_group *cache;
8036
8037 /* Do not attempt to repair in degraded state */
8038 if (btrfs_test_opt(fs_info, DEGRADED))
8039 return 0;
8040
8041 cache = btrfs_lookup_block_group(fs_info, logical);
8042 if (!cache)
8043 return 0;
8044
8045 spin_lock(&cache->lock);
8046 if (cache->relocating_repair) {
8047 spin_unlock(&cache->lock);
8048 btrfs_put_block_group(cache);
8049 return 0;
8050 }
8051 cache->relocating_repair = 1;
8052 spin_unlock(&cache->lock);
8053
8054 kthread_run(relocating_repair_kthread, cache,
8055 "btrfs-relocating-repair");
8056
8057 return 0;
8058}