blob: 387f80656476cf0f04b14108b0cdb1990cad5a61 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason0b86a832008-03-24 15:01:56 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason0b86a832008-03-24 15:01:56 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Chris Mason0b86a832008-03-24 15:01:56 -04006#include <linux/sched.h>
7#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -04009#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040010#include <linux/blkdev.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020011#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020012#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050013#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020014#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070015#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080016#include <linux/list_sort.h>
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"
Chris Mason0b86a832008-03-24 15:01:56 -040034
Zhao Leiaf902042015-09-15 21:08:06 +080035const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
36 [BTRFS_RAID_RAID10] = {
37 .sub_stripes = 2,
38 .dev_stripes = 1,
39 .devs_max = 0, /* 0 == as many as possible */
40 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080041 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080042 .devs_increment = 2,
43 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020044 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080045 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080046 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080047 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080048 },
49 [BTRFS_RAID_RAID1] = {
50 .sub_stripes = 1,
51 .dev_stripes = 1,
52 .devs_max = 2,
53 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080054 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080055 .devs_increment = 2,
56 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020057 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080058 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080059 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080060 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080061 },
David Sterba47e6f742018-03-02 22:56:53 +010062 [BTRFS_RAID_RAID1C3] = {
63 .sub_stripes = 1,
64 .dev_stripes = 1,
David Sterbacf93e152019-11-27 16:10:54 +010065 .devs_max = 3,
David Sterba47e6f742018-03-02 22:56:53 +010066 .devs_min = 3,
67 .tolerated_failures = 2,
68 .devs_increment = 3,
69 .ncopies = 3,
David Sterbadb26a022019-11-25 15:34:48 +010070 .nparity = 0,
David Sterba47e6f742018-03-02 22:56:53 +010071 .raid_name = "raid1c3",
72 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C3,
73 .mindev_error = BTRFS_ERROR_DEV_RAID1C3_MIN_NOT_MET,
74 },
David Sterba8d6fac02018-03-02 22:56:53 +010075 [BTRFS_RAID_RAID1C4] = {
76 .sub_stripes = 1,
77 .dev_stripes = 1,
David Sterbacf93e152019-11-27 16:10:54 +010078 .devs_max = 4,
David Sterba8d6fac02018-03-02 22:56:53 +010079 .devs_min = 4,
80 .tolerated_failures = 3,
81 .devs_increment = 4,
82 .ncopies = 4,
David Sterbadb26a022019-11-25 15:34:48 +010083 .nparity = 0,
David Sterba8d6fac02018-03-02 22:56:53 +010084 .raid_name = "raid1c4",
85 .bg_flag = BTRFS_BLOCK_GROUP_RAID1C4,
86 .mindev_error = BTRFS_ERROR_DEV_RAID1C4_MIN_NOT_MET,
87 },
Zhao Leiaf902042015-09-15 21:08:06 +080088 [BTRFS_RAID_DUP] = {
89 .sub_stripes = 1,
90 .dev_stripes = 2,
91 .devs_max = 1,
92 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080093 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080094 .devs_increment = 1,
95 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020096 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080097 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080098 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080099 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800100 },
101 [BTRFS_RAID_RAID0] = {
102 .sub_stripes = 1,
103 .dev_stripes = 1,
104 .devs_max = 0,
105 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800106 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800107 .devs_increment = 1,
108 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200109 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +0800110 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +0800111 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800112 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800113 },
114 [BTRFS_RAID_SINGLE] = {
115 .sub_stripes = 1,
116 .dev_stripes = 1,
117 .devs_max = 1,
118 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800119 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800120 .devs_increment = 1,
121 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200122 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +0800123 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +0800124 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800125 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +0800126 },
127 [BTRFS_RAID_RAID5] = {
128 .sub_stripes = 1,
129 .dev_stripes = 1,
130 .devs_max = 0,
131 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800132 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800133 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200134 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200135 .nparity = 1,
Anand Jained234672018-04-25 19:01:42 +0800136 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800137 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800138 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800139 },
140 [BTRFS_RAID_RAID6] = {
141 .sub_stripes = 1,
142 .dev_stripes = 1,
143 .devs_max = 0,
144 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800145 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800146 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200147 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200148 .nparity = 2,
Anand Jained234672018-04-25 19:01:42 +0800149 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800150 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800151 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800152 },
153};
154
David Sterba158da512019-05-17 11:43:41 +0200155const char *btrfs_bg_type_to_raid_name(u64 flags)
Anand Jained234672018-04-25 19:01:42 +0800156{
David Sterba158da512019-05-17 11:43:41 +0200157 const int index = btrfs_bg_flags_to_raid_index(flags);
158
159 if (index >= BTRFS_NR_RAID_TYPES)
Anand Jained234672018-04-25 19:01:42 +0800160 return NULL;
161
David Sterba158da512019-05-17 11:43:41 +0200162 return btrfs_raid_array[index].raid_name;
Anand Jained234672018-04-25 19:01:42 +0800163}
164
Anand Jainf89e09c2018-11-20 16:12:55 +0800165/*
166 * Fill @buf with textual description of @bg_flags, no more than @size_buf
167 * bytes including terminating null byte.
168 */
169void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
170{
171 int i;
172 int ret;
173 char *bp = buf;
174 u64 flags = bg_flags;
175 u32 size_bp = size_buf;
176
177 if (!flags) {
178 strcpy(bp, "NONE");
179 return;
180 }
181
182#define DESCRIBE_FLAG(flag, desc) \
183 do { \
184 if (flags & (flag)) { \
185 ret = snprintf(bp, size_bp, "%s|", (desc)); \
186 if (ret < 0 || ret >= size_bp) \
187 goto out_overflow; \
188 size_bp -= ret; \
189 bp += ret; \
190 flags &= ~(flag); \
191 } \
192 } while (0)
193
194 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
195 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
196 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
197
198 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
199 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
200 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
201 btrfs_raid_array[i].raid_name);
202#undef DESCRIBE_FLAG
203
204 if (flags) {
205 ret = snprintf(bp, size_bp, "0x%llx|", flags);
206 size_bp -= ret;
207 }
208
209 if (size_bp < size_buf)
210 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
211
212 /*
213 * The text is trimmed, it's up to the caller to provide sufficiently
214 * large buffer
215 */
216out_overflow:;
217}
218
David Sterba6f8e0fc2019-03-20 16:29:13 +0100219static int init_first_rw_device(struct btrfs_trans_handle *trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400220static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000221static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200222static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700223static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
224 enum btrfs_map_op op,
225 u64 logical, u64 *length,
226 struct btrfs_bio **bbio_ret,
227 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500228
David Sterba9c6b1c42017-06-16 22:30:00 +0200229/*
230 * Device locking
231 * ==============
232 *
233 * There are several mutexes that protect manipulation of devices and low-level
234 * structures like chunks but not block groups, extents or files
235 *
236 * uuid_mutex (global lock)
237 * ------------------------
238 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
239 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
240 * device) or requested by the device= mount option
241 *
242 * the mutex can be very coarse and can cover long-running operations
243 *
244 * protects: updates to fs_devices counters like missing devices, rw devices,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100245 * seeding, structure cloning, opening/closing devices at mount/umount time
David Sterba9c6b1c42017-06-16 22:30:00 +0200246 *
247 * global::fs_devs - add, remove, updates to the global list
248 *
249 * does not protect: manipulation of the fs_devices::devices list!
250 *
251 * btrfs_device::name - renames (write side), read is RCU
252 *
253 * fs_devices::device_list_mutex (per-fs, with RCU)
254 * ------------------------------------------------
255 * protects updates to fs_devices::devices, ie. adding and deleting
256 *
257 * simple list traversal with read-only actions can be done with RCU protection
258 *
259 * may be used to exclude some operations from running concurrently without any
260 * modifications to the list (see write_all_supers)
261 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200262 * balance_mutex
263 * -------------
264 * protects balance structures (status, state) and context accessed from
265 * several places (internally, ioctl)
266 *
267 * chunk_mutex
268 * -----------
269 * protects chunks, adding or removing during allocation, trim or when a new
Nikolay Borisov0b6f5d42019-05-09 18:11:11 +0300270 * device is added/removed. Additionally it also protects post_commit_list of
271 * individual devices, since they can be added to the transaction's
272 * post_commit_list only with chunk_mutex held.
David Sterba9c6b1c42017-06-16 22:30:00 +0200273 *
274 * cleaner_mutex
275 * -------------
276 * a big lock that is held by the cleaner thread and prevents running subvolume
277 * cleaning together with relocation or delayed iputs
278 *
279 *
280 * Lock nesting
281 * ============
282 *
283 * uuid_mutex
284 * volume_mutex
285 * device_list_mutex
286 * chunk_mutex
287 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800288 *
289 *
290 * Exclusive operations, BTRFS_FS_EXCL_OP
291 * ======================================
292 *
293 * Maintains the exclusivity of the following operations that apply to the
294 * whole filesystem and cannot run in parallel.
295 *
296 * - Balance (*)
297 * - Device add
298 * - Device remove
299 * - Device replace (*)
300 * - Resize
301 *
302 * The device operations (as above) can be in one of the following states:
303 *
304 * - Running state
305 * - Paused state
306 * - Completed state
307 *
308 * Only device operations marked with (*) can go into the Paused state for the
309 * following reasons:
310 *
311 * - ioctl (only Balance can be Paused through ioctl)
312 * - filesystem remounted as read-only
313 * - filesystem unmounted and mounted as read-only
314 * - system power-cycle and filesystem mounted as read-only
315 * - filesystem or device errors leading to forced read-only
316 *
317 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
318 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
319 * A device operation in Paused or Running state can be canceled or resumed
320 * either by ioctl (Balance only) or when remounted as read-write.
321 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
322 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200323 */
324
Miao Xie67a2c452014-09-03 21:35:43 +0800325DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400326static LIST_HEAD(fs_uuids);
David Sterba4143cb82019-10-01 19:57:37 +0200327struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
Anand Jainc73eccf2015-03-10 06:38:30 +0800328{
329 return &fs_uuids;
330}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400331
David Sterba2dfeca92017-06-14 02:48:07 +0200332/*
333 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200334 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
335 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200336 *
337 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
338 * The returned struct is not linked onto any lists and can be destroyed with
339 * kfree() right away.
340 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200341static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
342 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300343{
344 struct btrfs_fs_devices *fs_devs;
345
David Sterba78f2c9e2016-02-11 14:25:38 +0100346 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300347 if (!fs_devs)
348 return ERR_PTR(-ENOMEM);
349
350 mutex_init(&fs_devs->device_list_mutex);
351
352 INIT_LIST_HEAD(&fs_devs->devices);
353 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800354 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300355 if (fsid)
356 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300357
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200358 if (metadata_fsid)
359 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
360 else if (fsid)
361 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
362
Ilya Dryomov2208a372013-08-12 14:33:03 +0300363 return fs_devs;
364}
365
David Sterbaa425f9d2018-03-20 15:47:33 +0100366void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100367{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200368 WARN_ON(!list_empty(&device->post_commit_list));
David Sterba48dae9c2017-10-30 18:10:25 +0100369 rcu_string_free(device->name);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200370 extent_io_tree_release(&device->alloc_state);
David Sterba48dae9c2017-10-30 18:10:25 +0100371 bio_put(device->flush_bio);
372 kfree(device);
373}
374
Yan Zhenge4404d62008-12-12 10:03:26 -0500375static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
376{
377 struct btrfs_device *device;
378 WARN_ON(fs_devices->opened);
379 while (!list_empty(&fs_devices->devices)) {
380 device = list_entry(fs_devices->devices.next,
381 struct btrfs_device, dev_list);
382 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100383 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500384 }
385 kfree(fs_devices);
386}
387
David Sterbaffc5a372018-02-19 17:24:15 +0100388void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400389{
390 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400391
Yan Zheng2b820322008-11-17 21:11:30 -0500392 while (!list_empty(&fs_uuids)) {
393 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800394 struct btrfs_fs_devices, fs_list);
395 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500396 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400397 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400398}
399
David Sterba48dae9c2017-10-30 18:10:25 +0100400/*
401 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
402 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100403 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100404 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300405static struct btrfs_device *__alloc_device(void)
406{
407 struct btrfs_device *dev;
408
David Sterba78f2c9e2016-02-11 14:25:38 +0100409 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300410 if (!dev)
411 return ERR_PTR(-ENOMEM);
412
David Sterbae0ae9992017-06-06 17:06:06 +0200413 /*
414 * Preallocate a bio that's always going to be used for flushing device
415 * barriers and matches the device lifespan
416 */
417 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
418 if (!dev->flush_bio) {
419 kfree(dev);
420 return ERR_PTR(-ENOMEM);
421 }
David Sterbae0ae9992017-06-06 17:06:06 +0200422
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300423 INIT_LIST_HEAD(&dev->dev_list);
424 INIT_LIST_HEAD(&dev->dev_alloc_list);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200425 INIT_LIST_HEAD(&dev->post_commit_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300426
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300427 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800428 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100429 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700430 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800431 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200432 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300433
434 return dev;
435}
436
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200437static noinline struct btrfs_fs_devices *find_fsid(
438 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400439{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400440 struct btrfs_fs_devices *fs_devices;
441
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200442 ASSERT(fsid);
443
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200444 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800445 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200446 if (metadata_fsid) {
447 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
448 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
449 BTRFS_FSID_SIZE) == 0)
450 return fs_devices;
451 } else {
452 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
453 return fs_devices;
454 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400455 }
456 return NULL;
457}
458
Su Yuec6730a02020-01-10 14:11:33 +0200459static struct btrfs_fs_devices *find_fsid_with_metadata_uuid(
460 struct btrfs_super_block *disk_super)
461{
462
463 struct btrfs_fs_devices *fs_devices;
464
465 /*
466 * Handle scanned device having completed its fsid change but
467 * belonging to a fs_devices that was created by first scanning
468 * a device which didn't have its fsid/metadata_uuid changed
469 * at all and the CHANGING_FSID_V2 flag set.
470 */
471 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
472 if (fs_devices->fsid_change &&
473 memcmp(disk_super->metadata_uuid, fs_devices->fsid,
474 BTRFS_FSID_SIZE) == 0 &&
475 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
476 BTRFS_FSID_SIZE) == 0) {
477 return fs_devices;
478 }
479 }
480 /*
481 * Handle scanned device having completed its fsid change but
482 * belonging to a fs_devices that was created by a device that
483 * has an outdated pair of fsid/metadata_uuid and
484 * CHANGING_FSID_V2 flag set.
485 */
486 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
487 if (fs_devices->fsid_change &&
488 memcmp(fs_devices->metadata_uuid,
489 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
490 memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid,
491 BTRFS_FSID_SIZE) == 0) {
492 return fs_devices;
493 }
494 }
495
496 return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
497}
498
499
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100500static int
501btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
502 int flush, struct block_device **bdev,
503 struct buffer_head **bh)
504{
505 int ret;
506
507 *bdev = blkdev_get_by_path(device_path, flags, holder);
508
509 if (IS_ERR(*bdev)) {
510 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100511 goto error;
512 }
513
514 if (flush)
515 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200516 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100517 if (ret) {
518 blkdev_put(*bdev, flags);
519 goto error;
520 }
521 invalidate_bdev(*bdev);
522 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800523 if (IS_ERR(*bh)) {
524 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100525 blkdev_put(*bdev, flags);
526 goto error;
527 }
528
529 return 0;
530
531error:
532 *bdev = NULL;
533 *bh = NULL;
534 return ret;
535}
536
Anand Jain70bc7082019-01-04 13:31:53 +0800537static bool device_path_matched(const char *path, struct btrfs_device *device)
538{
539 int found;
540
541 rcu_read_lock();
542 found = strcmp(rcu_str_deref(device->name), path);
543 rcu_read_unlock();
544
545 return found == 0;
546}
547
Anand Jaind8367db2018-01-18 22:00:37 +0800548/*
549 * Search and remove all stale (devices which are not mounted) devices.
550 * When both inputs are NULL, it will search and release all stale devices.
551 * path: Optional. When provided will it release all unmounted devices
552 * matching this path only.
553 * skip_dev: Optional. Will skip this device when searching for the stale
554 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800555 * Return: 0 for success or if @path is NULL.
556 * -EBUSY if @path is a mounted device.
557 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800558 */
Anand Jain70bc7082019-01-04 13:31:53 +0800559static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800560 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800561{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800562 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
563 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800564 int ret = 0;
565
566 if (path)
567 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800568
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800569 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800570
Anand Jain70bc7082019-01-04 13:31:53 +0800571 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800572 list_for_each_entry_safe(device, tmp_device,
573 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800574 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800575 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800576 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800577 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800578 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800579 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800580 if (fs_devices->opened) {
581 /* for an already deleted device return 0 */
582 if (path && ret != 0)
583 ret = -EBUSY;
584 break;
585 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800586
Anand Jain4fde46f2015-06-17 21:10:48 +0800587 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800588 fs_devices->num_devices--;
589 list_del(&device->dev_list);
590 btrfs_free_device(device);
591
Anand Jain70bc7082019-01-04 13:31:53 +0800592 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800593 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200594 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800595 }
596 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800597
Anand Jain7bcb8162018-05-29 17:23:20 +0800598 if (fs_devices->num_devices == 0) {
599 btrfs_sysfs_remove_fsid(fs_devices);
600 list_del(&fs_devices->fs_list);
601 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800602 }
603 }
Anand Jain70bc7082019-01-04 13:31:53 +0800604
605 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800606}
607
Anand Jain0fb08bc2017-11-09 23:45:24 +0800608static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
609 struct btrfs_device *device, fmode_t flags,
610 void *holder)
611{
612 struct request_queue *q;
613 struct block_device *bdev;
614 struct buffer_head *bh;
615 struct btrfs_super_block *disk_super;
616 u64 devid;
617 int ret;
618
619 if (device->bdev)
620 return -EINVAL;
621 if (!device->name)
622 return -EINVAL;
623
624 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
625 &bdev, &bh);
626 if (ret)
627 return ret;
628
629 disk_super = (struct btrfs_super_block *)bh->b_data;
630 devid = btrfs_stack_device_id(&disk_super->dev_item);
631 if (devid != device->devid)
632 goto error_brelse;
633
634 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
635 goto error_brelse;
636
637 device->generation = btrfs_super_generation(disk_super);
638
639 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200640 if (btrfs_super_incompat_flags(disk_super) &
641 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
642 pr_err(
643 "BTRFS: Invalid seeding and uuid-changed device detected\n");
644 goto error_brelse;
645 }
646
Anand Jainebbede42017-12-04 12:54:52 +0800647 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Johannes Thumshirn0395d842019-11-13 11:27:27 +0100648 fs_devices->seeding = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800649 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800650 if (bdev_read_only(bdev))
651 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
652 else
653 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800654 }
655
656 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800657 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +0100658 fs_devices->rotating = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800659
660 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800661 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800662 device->mode = flags;
663
664 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800665 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
666 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800667 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800668 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800669 }
670 brelse(bh);
671
672 return 0;
673
674error_brelse:
675 brelse(bh);
676 blkdev_put(bdev, flags);
677
678 return -EINVAL;
679}
680
David Sterba60999ca2014-03-26 18:26:36 +0100681/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200682 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
Su Yuec0d81c72020-01-10 14:11:32 +0200683 * being created with a disk that has already completed its fsid change. Such
684 * disk can belong to an fs which has its FSID changed or to one which doesn't.
685 * Handle both cases here.
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200686 */
687static struct btrfs_fs_devices *find_fsid_inprogress(
688 struct btrfs_super_block *disk_super)
689{
690 struct btrfs_fs_devices *fs_devices;
691
692 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
693 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
694 BTRFS_FSID_SIZE) != 0 &&
695 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
696 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
697 return fs_devices;
698 }
699 }
700
Su Yuec0d81c72020-01-10 14:11:32 +0200701 return find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200702}
703
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200704
705static struct btrfs_fs_devices *find_fsid_changed(
706 struct btrfs_super_block *disk_super)
707{
708 struct btrfs_fs_devices *fs_devices;
709
710 /*
711 * Handles the case where scanned device is part of an fs that had
712 * multiple successful changes of FSID but curently device didn't
Nikolay Borisov05840712020-01-10 14:11:34 +0200713 * observe it. Meaning our fsid will be different than theirs. We need
714 * to handle two subcases :
715 * 1 - The fs still continues to have different METADATA/FSID uuids.
716 * 2 - The fs is switched back to its original FSID (METADATA/FSID
717 * are equal).
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200718 */
719 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov05840712020-01-10 14:11:34 +0200720 /* Changed UUIDs */
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200721 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
722 BTRFS_FSID_SIZE) != 0 &&
723 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
724 BTRFS_FSID_SIZE) == 0 &&
725 memcmp(fs_devices->fsid, disk_super->fsid,
Nikolay Borisov05840712020-01-10 14:11:34 +0200726 BTRFS_FSID_SIZE) != 0)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200727 return fs_devices;
Nikolay Borisov05840712020-01-10 14:11:34 +0200728
729 /* Unchanged UUIDs */
730 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
731 BTRFS_FSID_SIZE) == 0 &&
732 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
733 BTRFS_FSID_SIZE) == 0)
734 return fs_devices;
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200735 }
736
737 return NULL;
738}
Nikolay Borisov13620892020-01-10 14:11:35 +0200739
740static struct btrfs_fs_devices *find_fsid_reverted_metadata(
741 struct btrfs_super_block *disk_super)
742{
743 struct btrfs_fs_devices *fs_devices;
744
745 /*
746 * Handle the case where the scanned device is part of an fs whose last
747 * metadata UUID change reverted it to the original FSID. At the same
748 * time * fs_devices was first created by another constitutent device
749 * which didn't fully observe the operation. This results in an
750 * btrfs_fs_devices created with metadata/fsid different AND
751 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
752 * fs_devices equal to the FSID of the disk.
753 */
754 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
755 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
756 BTRFS_FSID_SIZE) != 0 &&
757 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
758 BTRFS_FSID_SIZE) == 0 &&
759 fs_devices->fsid_change)
760 return fs_devices;
761 }
762
763 return NULL;
764}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200765/*
David Sterba60999ca2014-03-26 18:26:36 +0100766 * Add new device to list of registered devices
767 *
768 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800769 * device pointer which was just added or updated when successful
770 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100771 */
Anand Jaine124ece2018-01-18 22:02:35 +0800772static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800773 struct btrfs_super_block *disk_super,
774 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400775{
776 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200777 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400778 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800780 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200781 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
782 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200783 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
784 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400785
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200786 if (fsid_change_in_progress) {
Su Yuec0d81c72020-01-10 14:11:32 +0200787 if (!has_metadata_uuid)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200788 fs_devices = find_fsid_inprogress(disk_super);
Su Yuec0d81c72020-01-10 14:11:32 +0200789 else
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200790 fs_devices = find_fsid_changed(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200791 } else if (has_metadata_uuid) {
Su Yuec6730a02020-01-10 14:11:33 +0200792 fs_devices = find_fsid_with_metadata_uuid(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200793 } else {
Nikolay Borisov13620892020-01-10 14:11:35 +0200794 fs_devices = find_fsid_reverted_metadata(disk_super);
795 if (!fs_devices)
796 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200797 }
798
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200799
Chris Mason8a4b83c2008-03-24 15:02:07 -0400800 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200801 if (has_metadata_uuid)
802 fs_devices = alloc_fs_devices(disk_super->fsid,
803 disk_super->metadata_uuid);
804 else
805 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
806
Ilya Dryomov2208a372013-08-12 14:33:03 +0300807 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800808 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300809
Al Viro92900e52019-01-27 04:58:00 +0000810 fs_devices->fsid_change = fsid_change_in_progress;
811
Anand Jain9c6d1732018-05-29 14:10:20 +0800812 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800813 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300814
Chris Mason8a4b83c2008-03-24 15:02:07 -0400815 device = NULL;
816 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800817 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800818 device = btrfs_find_device(fs_devices, devid,
819 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200820
821 /*
822 * If this disk has been pulled into an fs devices created by
823 * a device which had the CHANGING_FSID_V2 flag then replace the
824 * metadata_uuid/fsid values of the fs_devices.
825 */
Nikolay Borisov13620892020-01-10 14:11:35 +0200826 if (fs_devices->fsid_change &&
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200827 found_transid > fs_devices->latest_generation) {
828 memcpy(fs_devices->fsid, disk_super->fsid,
829 BTRFS_FSID_SIZE);
Nikolay Borisov13620892020-01-10 14:11:35 +0200830
831 if (has_metadata_uuid)
832 memcpy(fs_devices->metadata_uuid,
833 disk_super->metadata_uuid,
834 BTRFS_FSID_SIZE);
835 else
836 memcpy(fs_devices->metadata_uuid,
837 disk_super->fsid, BTRFS_FSID_SIZE);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200838
839 fs_devices->fsid_change = false;
840 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400841 }
Miao Xie443f24f2014-07-24 11:37:15 +0800842
Chris Mason8a4b83c2008-03-24 15:02:07 -0400843 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800844 if (fs_devices->opened) {
845 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800846 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800847 }
Yan Zheng2b820322008-11-17 21:11:30 -0500848
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300849 device = btrfs_alloc_device(NULL, &devid,
850 disk_super->dev_item.uuid);
851 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800852 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400853 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800854 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400855 }
Josef Bacik606686e2012-06-04 14:03:51 -0400856
857 name = rcu_string_strdup(path, GFP_NOFS);
858 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100859 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800860 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800861 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400862 }
Josef Bacik606686e2012-06-04 14:03:51 -0400863 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200864
Xiao Guangrong1f781602011-04-20 10:09:16 +0000865 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100866 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400867
Yan Zheng2b820322008-11-17 21:11:30 -0500868 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800869 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800870
871 if (disk_super->label[0])
Anand Jainaa6c0df2019-10-02 18:30:48 +0800872 pr_info(
873 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
874 disk_super->label, devid, found_transid, path,
875 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800876 else
Anand Jainaa6c0df2019-10-02 18:30:48 +0800877 pr_info(
878 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
879 disk_super->fsid, devid, found_transid, path,
880 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800881
Josef Bacik606686e2012-06-04 14:03:51 -0400882 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800883 /*
884 * When FS is already mounted.
885 * 1. If you are here and if the device->name is NULL that
886 * means this device was missing at time of FS mount.
887 * 2. If you are here and if the device->name is different
888 * from 'path' that means either
889 * a. The same device disappeared and reappeared with
890 * different name. or
891 * b. The missing-disk-which-was-replaced, has
892 * reappeared now.
893 *
894 * We must allow 1 and 2a above. But 2b would be a spurious
895 * and unintentional.
896 *
897 * Further in case of 1 and 2a above, the disk at 'path'
898 * would have missed some transaction when it was away and
899 * in case of 2a the stale bdev has to be updated as well.
900 * 2b must not be allowed at all time.
901 */
902
903 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700904 * For now, we do allow update to btrfs_fs_device through the
905 * btrfs dev scan cli after FS has been mounted. We're still
906 * tracking a problem where systems fail mount by subvolume id
907 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800908 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700909 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800910 /*
911 * That is if the FS is _not_ mounted and if you
912 * are here, that means there is more than one
913 * disk with same uuid and devid.We keep the one
914 * with larger generation number or the last-in if
915 * generation are equal.
916 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800917 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800918 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800919 }
Anand Jainb96de002014-07-03 18:22:05 +0800920
Anand Jaina9261d42018-10-15 10:45:17 +0800921 /*
922 * We are going to replace the device path for a given devid,
923 * make sure it's the same device if the device is mounted
924 */
925 if (device->bdev) {
926 struct block_device *path_bdev;
927
928 path_bdev = lookup_bdev(path);
929 if (IS_ERR(path_bdev)) {
930 mutex_unlock(&fs_devices->device_list_mutex);
931 return ERR_CAST(path_bdev);
932 }
933
934 if (device->bdev != path_bdev) {
935 bdput(path_bdev);
936 mutex_unlock(&fs_devices->device_list_mutex);
937 btrfs_warn_in_rcu(device->fs_info,
938 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
939 disk_super->fsid, devid,
940 rcu_str_deref(device->name), path);
941 return ERR_PTR(-EEXIST);
942 }
943 bdput(path_bdev);
944 btrfs_info_in_rcu(device->fs_info,
945 "device fsid %pU devid %llu moved old:%s new:%s",
946 disk_super->fsid, devid,
947 rcu_str_deref(device->name), path);
948 }
949
Josef Bacik606686e2012-06-04 14:03:51 -0400950 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800951 if (!name) {
952 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800953 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800954 }
Josef Bacik606686e2012-06-04 14:03:51 -0400955 rcu_string_free(device->name);
956 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800957 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500958 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800959 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500960 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400961 }
962
Anand Jain77bdae42014-07-03 18:22:06 +0800963 /*
964 * Unmount does not free the btrfs_device struct but would zero
965 * generation along with most of the other members. So just update
966 * it back. We need it to pick the disk with largest generation
967 * (as above).
968 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200969 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +0800970 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200971 fs_devices->latest_generation = max_t(u64, found_transid,
972 fs_devices->latest_generation);
973 }
Anand Jain77bdae42014-07-03 18:22:06 +0800974
Anand Jainf2788d22018-01-18 22:02:34 +0800975 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
976
Anand Jain9c6d1732018-05-29 14:10:20 +0800977 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800978 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400979}
980
Yan Zhenge4404d62008-12-12 10:03:26 -0500981static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
982{
983 struct btrfs_fs_devices *fs_devices;
984 struct btrfs_device *device;
985 struct btrfs_device *orig_dev;
Anand Jaind2979aa2019-08-27 15:40:45 +0800986 int ret = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -0500987
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200988 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300989 if (IS_ERR(fs_devices))
990 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500991
Miao Xieadbbb862014-09-03 21:35:42 +0800992 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400993 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500994
995 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400996 struct rcu_string *name;
997
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300998 device = btrfs_alloc_device(NULL, &orig_dev->devid,
999 orig_dev->uuid);
Anand Jaind2979aa2019-08-27 15:40:45 +08001000 if (IS_ERR(device)) {
1001 ret = PTR_ERR(device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001002 goto error;
Anand Jaind2979aa2019-08-27 15:40:45 +08001003 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001004
Josef Bacik606686e2012-06-04 14:03:51 -04001005 /*
1006 * This is ok to do without rcu read locked because we hold the
1007 * uuid mutex so nothing we touch in here is going to disappear.
1008 */
Anand Jaine755f782014-06-30 17:12:47 +08001009 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001010 name = rcu_string_strdup(orig_dev->name->str,
1011 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001012 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001013 btrfs_free_device(device);
Anand Jaind2979aa2019-08-27 15:40:45 +08001014 ret = -ENOMEM;
Anand Jaine755f782014-06-30 17:12:47 +08001015 goto error;
1016 }
1017 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001018 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001019
Yan Zhenge4404d62008-12-12 10:03:26 -05001020 list_add(&device->dev_list, &fs_devices->devices);
1021 device->fs_devices = fs_devices;
1022 fs_devices->num_devices++;
1023 }
Miao Xieadbbb862014-09-03 21:35:42 +08001024 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001025 return fs_devices;
1026error:
Miao Xieadbbb862014-09-03 21:35:42 +08001027 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001028 free_fs_devices(fs_devices);
Anand Jaind2979aa2019-08-27 15:40:45 +08001029 return ERR_PTR(ret);
Yan Zhenge4404d62008-12-12 10:03:26 -05001030}
1031
Anand Jain9b99b112018-02-27 12:41:59 +08001032/*
1033 * After we have read the system tree and know devids belonging to
1034 * this filesystem, remove the device which does not belong there.
1035 */
1036void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001037{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001038 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001039 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001040
Chris Masondfe25022008-05-13 13:46:40 -04001041 mutex_lock(&uuid_mutex);
1042again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001043 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001044 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001045 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1046 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001047 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1048 &device->dev_state) &&
1049 (!latest_dev ||
1050 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001051 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001052 }
Yan Zheng2b820322008-11-17 21:11:30 -05001053 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001054 }
Yan Zheng2b820322008-11-17 21:11:30 -05001055
Stefan Behrens8dabb742012-11-06 13:15:27 +01001056 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1057 /*
1058 * In the first step, keep the device which has
1059 * the correct fsid and the devid that is used
1060 * for the dev_replace procedure.
1061 * In the second step, the dev_replace state is
1062 * read from the device tree and it is known
1063 * whether the procedure is really active or
1064 * not, which means whether this device is
1065 * used or whether it should be removed.
1066 */
Anand Jain401e29c2017-12-04 12:54:55 +08001067 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1068 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001069 continue;
1070 }
1071 }
Yan Zheng2b820322008-11-17 21:11:30 -05001072 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001073 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001074 device->bdev = NULL;
1075 fs_devices->open_devices--;
1076 }
Anand Jainebbede42017-12-04 12:54:52 +08001077 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001078 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001079 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001080 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1081 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001082 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001083 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001084 list_del_init(&device->dev_list);
1085 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001086 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001087 }
Yan Zheng2b820322008-11-17 21:11:30 -05001088
1089 if (fs_devices->seed) {
1090 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001091 goto again;
1092 }
1093
Miao Xie443f24f2014-07-24 11:37:15 +08001094 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001095
Chris Masondfe25022008-05-13 13:46:40 -04001096 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001097}
Chris Masona0af4692008-05-13 16:03:06 -04001098
Anand Jain14238812016-07-22 06:04:53 +08001099static void btrfs_close_bdev(struct btrfs_device *device)
1100{
David Sterba08ffcae2017-06-19 16:55:35 +02001101 if (!device->bdev)
1102 return;
1103
Anand Jainebbede42017-12-04 12:54:52 +08001104 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001105 sync_blockdev(device->bdev);
1106 invalidate_bdev(device->bdev);
1107 }
1108
David Sterba08ffcae2017-06-19 16:55:35 +02001109 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001110}
1111
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001112static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001113{
1114 struct btrfs_fs_devices *fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001115
Anand Jainebbede42017-12-04 12:54:52 +08001116 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001117 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1118 list_del_init(&device->dev_alloc_list);
1119 fs_devices->rw_devices--;
1120 }
1121
Anand Jaine6e674b2017-12-04 12:54:54 +08001122 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001123 fs_devices->missing_devices--;
1124
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001125 btrfs_close_bdev(device);
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001126 if (device->bdev) {
Johannes Thumshirn3fff3972019-11-26 09:40:05 +01001127 fs_devices->open_devices--;
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001128 device->bdev = NULL;
Anand Jainf448341a2016-06-14 18:55:25 +08001129 }
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001130 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jainf448341a2016-06-14 18:55:25 +08001131
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001132 device->fs_info = NULL;
1133 atomic_set(&device->dev_stats_ccnt, 0);
1134 extent_io_tree_release(&device->alloc_state);
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001135
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001136 /* Verify the device is back in a pristine state */
1137 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1138 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1139 ASSERT(list_empty(&device->dev_alloc_list));
1140 ASSERT(list_empty(&device->post_commit_list));
1141 ASSERT(atomic_read(&device->reada_in_flight) == 0);
Anand Jainf448341a2016-06-14 18:55:25 +08001142}
1143
Anand Jain0226e0e2018-04-12 10:29:27 +08001144static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001145{
Sasha Levin2037a092015-05-12 19:31:37 -04001146 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001147
Yan Zheng2b820322008-11-17 21:11:30 -05001148 if (--fs_devices->opened > 0)
1149 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001150
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001151 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001152 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001153 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001154 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001155 mutex_unlock(&fs_devices->device_list_mutex);
1156
Yan Zhenge4404d62008-12-12 10:03:26 -05001157 WARN_ON(fs_devices->open_devices);
1158 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001159 fs_devices->opened = 0;
Johannes Thumshirn0395d842019-11-13 11:27:27 +01001160 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05001161
Chris Mason8a4b83c2008-03-24 15:02:07 -04001162 return 0;
1163}
1164
Yan Zheng2b820322008-11-17 21:11:30 -05001165int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1166{
Yan Zhenge4404d62008-12-12 10:03:26 -05001167 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001168 int ret;
1169
1170 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001171 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001172 if (!fs_devices->opened) {
1173 seed_devices = fs_devices->seed;
1174 fs_devices->seed = NULL;
1175 }
Yan Zheng2b820322008-11-17 21:11:30 -05001176 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001177
1178 while (seed_devices) {
1179 fs_devices = seed_devices;
1180 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001181 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001182 free_fs_devices(fs_devices);
1183 }
Yan Zheng2b820322008-11-17 21:11:30 -05001184 return ret;
1185}
1186
Anand Jain897fb572018-04-12 10:29:28 +08001187static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001188 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001189{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001190 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001191 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001192 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001193
Tejun Heod4d77622010-11-13 11:55:18 +01001194 flags |= FMODE_EXCL;
1195
Anand Jainf117e292018-04-12 10:29:26 +08001196 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001197 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001198 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001199 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001200
Anand Jain9f050db2017-11-09 23:45:25 +08001201 if (!latest_dev ||
1202 device->generation > latest_dev->generation)
1203 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001204 }
Chris Masona0af4692008-05-13 16:03:06 -04001205 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001206 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001207 goto out;
1208 }
Yan Zheng2b820322008-11-17 21:11:30 -05001209 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001210 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001211 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001212out:
Yan Zheng2b820322008-11-17 21:11:30 -05001213 return ret;
1214}
1215
Anand Jainf8e10cd2018-01-22 14:49:36 -08001216static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1217{
1218 struct btrfs_device *dev1, *dev2;
1219
1220 dev1 = list_entry(a, struct btrfs_device, dev_list);
1221 dev2 = list_entry(b, struct btrfs_device, dev_list);
1222
1223 if (dev1->devid < dev2->devid)
1224 return -1;
1225 else if (dev1->devid > dev2->devid)
1226 return 1;
1227 return 0;
1228}
1229
Yan Zheng2b820322008-11-17 21:11:30 -05001230int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001231 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001232{
1233 int ret;
1234
David Sterbaf5194e32018-06-19 17:09:47 +02001235 lockdep_assert_held(&uuid_mutex);
1236
Anand Jain542c5902018-04-12 10:29:34 +08001237 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001238 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001239 fs_devices->opened++;
1240 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001241 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001242 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001243 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001244 }
Anand Jain542c5902018-04-12 10:29:34 +08001245 mutex_unlock(&fs_devices->device_list_mutex);
1246
Chris Mason8a4b83c2008-03-24 15:02:07 -04001247 return ret;
1248}
1249
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001250static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001251{
1252 kunmap(page);
1253 put_page(page);
1254}
1255
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001256static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1257 struct page **page,
1258 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001259{
1260 void *p;
1261 pgoff_t index;
1262
1263 /* make sure our super fits in the device */
1264 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1265 return 1;
1266
1267 /* make sure our super fits in the page */
1268 if (sizeof(**disk_super) > PAGE_SIZE)
1269 return 1;
1270
1271 /* make sure our super doesn't straddle pages on disk */
1272 index = bytenr >> PAGE_SHIFT;
1273 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1274 return 1;
1275
1276 /* pull in the page with our super */
1277 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1278 index, GFP_KERNEL);
1279
1280 if (IS_ERR_OR_NULL(*page))
1281 return 1;
1282
1283 p = kmap(*page);
1284
1285 /* align our pointer to the offset of the super block */
Johannes Thumshirn70730172018-12-05 15:23:03 +01001286 *disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001287
1288 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1289 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1290 btrfs_release_disk_super(*page);
1291 return 1;
1292 }
1293
1294 if ((*disk_super)->label[0] &&
1295 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1296 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1297
1298 return 0;
1299}
1300
Anand Jain228a73a2019-01-04 13:31:54 +08001301int btrfs_forget_devices(const char *path)
1302{
1303 int ret;
1304
1305 mutex_lock(&uuid_mutex);
1306 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1307 mutex_unlock(&uuid_mutex);
1308
1309 return ret;
1310}
1311
David Sterba6f60cbd2013-02-15 11:31:02 -07001312/*
1313 * Look for a btrfs signature on a device. This may be called out of the mount path
1314 * and we are not allowed to call set_blocksize during the scan. The superblock
1315 * is read via pagecache
1316 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001317struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1318 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001319{
1320 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001321 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001322 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001323 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001324 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001325 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001326
David Sterba899f9302018-06-19 16:37:36 +02001327 lockdep_assert_held(&uuid_mutex);
1328
David Sterba6f60cbd2013-02-15 11:31:02 -07001329 /*
1330 * we would like to check all the supers, but that would make
1331 * a btrfs mount succeed after a mkfs from a different FS.
1332 * So, we need to add a special mount option to scan for
1333 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1334 */
1335 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001336 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001337
1338 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001339 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001340 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001341
Anand Jain05a5c552017-12-15 15:40:16 +08001342 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001343 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001344 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001345 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001346
Anand Jain4306a972018-05-29 12:28:37 +08001347 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001348 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001349 if (new_device_added)
1350 btrfs_free_stale_devices(path, device);
1351 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001352
Anand Jain6cf86a002016-02-13 10:01:29 +08001353 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001354
1355error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001356 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001357
Gu Jinxiang36350e92018-07-12 14:23:16 +08001358 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001359}
Chris Mason0b86a832008-03-24 15:01:56 -04001360
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001361/*
1362 * Try to find a chunk that intersects [start, start + len] range and when one
1363 * such is found, record the end of it in *start
1364 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001365static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1366 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001367{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001368 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001369
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001370 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001371
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001372 if (!find_first_extent_bit(&device->alloc_state, *start,
1373 &physical_start, &physical_end,
1374 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001375
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001376 if (in_range(physical_start, *start, len) ||
1377 in_range(*start, physical_start,
1378 physical_end - physical_start)) {
1379 *start = physical_end + 1;
1380 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001381 }
1382 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001383 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001384}
1385
1386
Chris Mason0b86a832008-03-24 15:01:56 -04001387/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001388 * find_free_dev_extent_start - find free space in the specified device
1389 * @device: the device which we search the free space in
1390 * @num_bytes: the size of the free space that we need
1391 * @search_start: the position from which to begin the search
1392 * @start: store the start of the free space.
1393 * @len: the size of the free space. that we find, or the size
1394 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001395 *
Chris Mason0b86a832008-03-24 15:01:56 -04001396 * this uses a pretty simple search, the expectation is that it is
1397 * called very infrequently and that a given device has a small number
1398 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001399 *
1400 * @start is used to store the start of the free space if we find. But if we
1401 * don't find suitable free space, it will be used to store the start position
1402 * of the max free space.
1403 *
1404 * @len is used to store the size of the free space that we find.
1405 * But if we don't find suitable free space, it is used to store the size of
1406 * the max free space.
Qu Wenruo135da972019-07-19 14:51:42 +08001407 *
1408 * NOTE: This function will search *commit* root of device tree, and does extra
1409 * check to ensure dev extents are not double allocated.
1410 * This makes the function safe to allocate dev extents but may not report
1411 * correct usable device space, as device extent freed in current transaction
1412 * is not reported as avaiable.
Chris Mason0b86a832008-03-24 15:01:56 -04001413 */
Qu Wenruo9e3246a2019-07-19 14:51:41 +08001414static int find_free_dev_extent_start(struct btrfs_device *device,
1415 u64 num_bytes, u64 search_start, u64 *start,
1416 u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001418 struct btrfs_fs_info *fs_info = device->fs_info;
1419 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001420 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001421 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001422 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001423 u64 hole_size;
1424 u64 max_hole_start;
1425 u64 max_hole_size;
1426 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001427 u64 search_end = device->total_bytes;
1428 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001429 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001430 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001431
1432 /*
1433 * We don't want to overwrite the superblock on the drive nor any area
1434 * used by the boot loader (grub for example), so we make sure to start
1435 * at an offset of at least 1MB.
1436 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001437 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001438
Josef Bacik6df9a952013-06-27 13:22:46 -04001439 path = btrfs_alloc_path();
1440 if (!path)
1441 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001442
Miao Xie7bfc8372011-01-05 10:07:26 +00001443 max_hole_start = search_start;
1444 max_hole_size = 0;
1445
Zhao Leif2ab7612015-02-16 18:52:17 +08001446again:
Anand Jain401e29c2017-12-04 12:54:55 +08001447 if (search_start >= search_end ||
1448 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001449 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001450 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001451 }
1452
David Sterbae4058b52015-11-27 16:31:35 +01001453 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001454 path->search_commit_root = 1;
1455 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001456
Chris Mason0b86a832008-03-24 15:01:56 -04001457 key.objectid = device->devid;
1458 key.offset = search_start;
1459 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001460
Li Zefan125ccb02011-12-08 15:07:24 +08001461 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001462 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001463 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001464 if (ret > 0) {
1465 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1466 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001467 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001468 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001469
Chris Mason0b86a832008-03-24 15:01:56 -04001470 while (1) {
1471 l = path->nodes[0];
1472 slot = path->slots[0];
1473 if (slot >= btrfs_header_nritems(l)) {
1474 ret = btrfs_next_leaf(root, path);
1475 if (ret == 0)
1476 continue;
1477 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001478 goto out;
1479
1480 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001481 }
1482 btrfs_item_key_to_cpu(l, &key, slot);
1483
1484 if (key.objectid < device->devid)
1485 goto next;
1486
1487 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001488 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001489
David Sterba962a2982014-06-04 18:41:45 +02001490 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001491 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001492
Miao Xie7bfc8372011-01-05 10:07:26 +00001493 if (key.offset > search_start) {
1494 hole_size = key.offset - search_start;
1495
Josef Bacik6df9a952013-06-27 13:22:46 -04001496 /*
1497 * Have to check before we set max_hole_start, otherwise
1498 * we could end up sending back this offset anyway.
1499 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001500 if (contains_pending_extent(device, &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001501 hole_size)) {
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001502 if (key.offset >= search_start)
Forrest Liu1b984502015-02-09 17:30:47 +08001503 hole_size = key.offset - search_start;
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001504 else
Forrest Liu1b984502015-02-09 17:30:47 +08001505 hole_size = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001506 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001507
Miao Xie7bfc8372011-01-05 10:07:26 +00001508 if (hole_size > max_hole_size) {
1509 max_hole_start = search_start;
1510 max_hole_size = hole_size;
1511 }
1512
1513 /*
1514 * If this free space is greater than which we need,
1515 * it must be the max free space that we have found
1516 * until now, so max_hole_start must point to the start
1517 * of this free space and the length of this free space
1518 * is stored in max_hole_size. Thus, we return
1519 * max_hole_start and max_hole_size and go back to the
1520 * caller.
1521 */
1522 if (hole_size >= num_bytes) {
1523 ret = 0;
1524 goto out;
1525 }
1526 }
1527
Chris Mason0b86a832008-03-24 15:01:56 -04001528 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001529 extent_end = key.offset + btrfs_dev_extent_length(l,
1530 dev_extent);
1531 if (extent_end > search_start)
1532 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001533next:
1534 path->slots[0]++;
1535 cond_resched();
1536 }
Chris Mason0b86a832008-03-24 15:01:56 -04001537
liubo38c01b92011-08-02 02:39:03 +00001538 /*
1539 * At this point, search_start should be the end of
1540 * allocated dev extents, and when shrinking the device,
1541 * search_end may be smaller than search_start.
1542 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001543 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001544 hole_size = search_end - search_start;
1545
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001546 if (contains_pending_extent(device, &search_start, hole_size)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001547 btrfs_release_path(path);
1548 goto again;
1549 }
Chris Mason0b86a832008-03-24 15:01:56 -04001550
Zhao Leif2ab7612015-02-16 18:52:17 +08001551 if (hole_size > max_hole_size) {
1552 max_hole_start = search_start;
1553 max_hole_size = hole_size;
1554 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001555 }
1556
Miao Xie7bfc8372011-01-05 10:07:26 +00001557 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001558 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001559 ret = -ENOSPC;
1560 else
1561 ret = 0;
1562
1563out:
Yan Zheng2b820322008-11-17 21:11:30 -05001564 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001565 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001566 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001567 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001568 return ret;
1569}
1570
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001571int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001572 u64 *start, u64 *len)
1573{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001574 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001575 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001576}
1577
Christoph Hellwigb2950862008-12-02 09:54:17 -05001578static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001579 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001580 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001582 struct btrfs_fs_info *fs_info = device->fs_info;
1583 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001584 int ret;
1585 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001586 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001587 struct btrfs_key found_key;
1588 struct extent_buffer *leaf = NULL;
1589 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001590
1591 path = btrfs_alloc_path();
1592 if (!path)
1593 return -ENOMEM;
1594
1595 key.objectid = device->devid;
1596 key.offset = start;
1597 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001598again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001599 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001600 if (ret > 0) {
1601 ret = btrfs_previous_item(root, path, key.objectid,
1602 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001603 if (ret)
1604 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001605 leaf = path->nodes[0];
1606 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1607 extent = btrfs_item_ptr(leaf, path->slots[0],
1608 struct btrfs_dev_extent);
1609 BUG_ON(found_key.offset > start || found_key.offset +
1610 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001611 key = found_key;
1612 btrfs_release_path(path);
1613 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001614 } else if (ret == 0) {
1615 leaf = path->nodes[0];
1616 extent = btrfs_item_ptr(leaf, path->slots[0],
1617 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001618 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001619 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001620 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001621 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001622
Miao Xie2196d6e2014-09-03 21:35:41 +08001623 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1624
Chris Mason8f18cf12008-04-25 16:53:30 -04001625 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001626 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001627 btrfs_handle_fs_error(fs_info, ret,
1628 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001629 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001630 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001631 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001632out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001633 btrfs_free_path(path);
1634 return ret;
1635}
1636
Eric Sandeen48a3b632013-04-25 20:41:01 +00001637static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1638 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001639 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001640{
1641 int ret;
1642 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001643 struct btrfs_fs_info *fs_info = device->fs_info;
1644 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001645 struct btrfs_dev_extent *extent;
1646 struct extent_buffer *leaf;
1647 struct btrfs_key key;
1648
Anand Jaine12c9622017-12-04 12:54:53 +08001649 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001650 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001651 path = btrfs_alloc_path();
1652 if (!path)
1653 return -ENOMEM;
1654
Chris Mason0b86a832008-03-24 15:01:56 -04001655 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001656 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001657 key.type = BTRFS_DEV_EXTENT_KEY;
1658 ret = btrfs_insert_empty_item(trans, root, path, &key,
1659 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001660 if (ret)
1661 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001662
1663 leaf = path->nodes[0];
1664 extent = btrfs_item_ptr(leaf, path->slots[0],
1665 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001666 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1667 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001668 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1669 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001670 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1671
Chris Mason0b86a832008-03-24 15:01:56 -04001672 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1673 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001674out:
Chris Mason0b86a832008-03-24 15:01:56 -04001675 btrfs_free_path(path);
1676 return ret;
1677}
1678
Josef Bacik6df9a952013-06-27 13:22:46 -04001679static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001680{
Josef Bacik6df9a952013-06-27 13:22:46 -04001681 struct extent_map_tree *em_tree;
1682 struct extent_map *em;
1683 struct rb_node *n;
1684 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001685
David Sterbac8bf1b62019-05-17 11:43:17 +02001686 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001687 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001688 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001689 if (n) {
1690 em = rb_entry(n, struct extent_map, rb_node);
1691 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001692 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001693 read_unlock(&em_tree->lock);
1694
Chris Mason0b86a832008-03-24 15:01:56 -04001695 return ret;
1696}
1697
Ilya Dryomov53f10652013-08-12 14:33:01 +03001698static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1699 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001700{
1701 int ret;
1702 struct btrfs_key key;
1703 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001704 struct btrfs_path *path;
1705
Yan Zheng2b820322008-11-17 21:11:30 -05001706 path = btrfs_alloc_path();
1707 if (!path)
1708 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001709
1710 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1711 key.type = BTRFS_DEV_ITEM_KEY;
1712 key.offset = (u64)-1;
1713
Ilya Dryomov53f10652013-08-12 14:33:01 +03001714 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001715 if (ret < 0)
1716 goto error;
1717
Anand Jaina06dee4d2019-08-27 15:40:44 +08001718 if (ret == 0) {
1719 /* Corruption */
1720 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1721 ret = -EUCLEAN;
1722 goto error;
1723 }
Chris Mason0b86a832008-03-24 15:01:56 -04001724
Ilya Dryomov53f10652013-08-12 14:33:01 +03001725 ret = btrfs_previous_item(fs_info->chunk_root, path,
1726 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001727 BTRFS_DEV_ITEM_KEY);
1728 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001729 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001730 } else {
1731 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1732 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001733 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001734 }
1735 ret = 0;
1736error:
Yan Zheng2b820322008-11-17 21:11:30 -05001737 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001738 return ret;
1739}
1740
1741/*
1742 * the device information is stored in the chunk root
1743 * the btrfs_device struct should be fully filled in
1744 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001745static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001746 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001747{
1748 int ret;
1749 struct btrfs_path *path;
1750 struct btrfs_dev_item *dev_item;
1751 struct extent_buffer *leaf;
1752 struct btrfs_key key;
1753 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001754
Chris Mason0b86a832008-03-24 15:01:56 -04001755 path = btrfs_alloc_path();
1756 if (!path)
1757 return -ENOMEM;
1758
Chris Mason0b86a832008-03-24 15:01:56 -04001759 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1760 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001761 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001762
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001763 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1764 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001765 if (ret)
1766 goto out;
1767
1768 leaf = path->nodes[0];
1769 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1770
1771 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001772 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001773 btrfs_set_device_type(leaf, dev_item, device->type);
1774 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1775 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1776 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001777 btrfs_set_device_total_bytes(leaf, dev_item,
1778 btrfs_device_get_disk_total_bytes(device));
1779 btrfs_set_device_bytes_used(leaf, dev_item,
1780 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001781 btrfs_set_device_group(leaf, dev_item, 0);
1782 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1783 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001784 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001785
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001786 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001787 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001788 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001789 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1790 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001791 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001792
Yan Zheng2b820322008-11-17 21:11:30 -05001793 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001794out:
1795 btrfs_free_path(path);
1796 return ret;
1797}
Chris Mason8f18cf12008-04-25 16:53:30 -04001798
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001799/*
1800 * Function to update ctime/mtime for a given device path.
1801 * Mainly used for ctime/mtime based probe like libblkid.
1802 */
David Sterbada353f62017-02-14 17:55:53 +01001803static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001804{
1805 struct file *filp;
1806
1807 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001808 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001809 return;
1810 file_update_time(filp);
1811 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001812}
1813
David Sterbaf331a952019-03-20 16:31:53 +01001814static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001815{
David Sterbaf331a952019-03-20 16:31:53 +01001816 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001817 int ret;
1818 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001819 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001820 struct btrfs_trans_handle *trans;
1821
Chris Masona061fc82008-05-07 11:43:44 -04001822 path = btrfs_alloc_path();
1823 if (!path)
1824 return -ENOMEM;
1825
Yan, Zhenga22285a2010-05-16 10:48:46 -04001826 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001827 if (IS_ERR(trans)) {
1828 btrfs_free_path(path);
1829 return PTR_ERR(trans);
1830 }
Chris Masona061fc82008-05-07 11:43:44 -04001831 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1832 key.type = BTRFS_DEV_ITEM_KEY;
1833 key.offset = device->devid;
1834
1835 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001836 if (ret) {
1837 if (ret > 0)
1838 ret = -ENOENT;
1839 btrfs_abort_transaction(trans, ret);
1840 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001841 goto out;
1842 }
1843
1844 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001845 if (ret) {
1846 btrfs_abort_transaction(trans, ret);
1847 btrfs_end_transaction(trans);
1848 }
1849
Chris Masona061fc82008-05-07 11:43:44 -04001850out:
1851 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001852 if (!ret)
1853 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001854 return ret;
1855}
1856
David Sterba3cc31a02016-02-15 16:00:26 +01001857/*
1858 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1859 * filesystem. It's up to the caller to adjust that number regarding eg. device
1860 * replace.
1861 */
1862static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1863 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001864{
Chris Masona061fc82008-05-07 11:43:44 -04001865 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001866 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001867 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001868
Miao Xiede98ced2013-01-29 10:13:12 +00001869 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001870 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001871
Anand Jainbd45ffb2016-02-13 10:01:34 +08001872 all_avail = fs_info->avail_data_alloc_bits |
1873 fs_info->avail_system_alloc_bits |
1874 fs_info->avail_metadata_alloc_bits;
1875 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001876
David Sterba418775a2016-02-15 16:28:14 +01001877 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001878 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001879 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001880
David Sterba418775a2016-02-15 16:28:14 +01001881 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001882 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001883
David Sterba418775a2016-02-15 16:28:14 +01001884 if (ret)
1885 return ret;
1886 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001887 }
1888
1889 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001890}
1891
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001892static struct btrfs_device * btrfs_find_next_active_device(
1893 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001894{
1895 struct btrfs_device *next_device;
1896
1897 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1898 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001899 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1900 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001901 return next_device;
1902 }
1903
1904 return NULL;
1905}
1906
1907/*
1908 * Helper function to check if the given device is part of s_bdev / latest_bdev
1909 * and replace it with the provided or the next active device, in the context
1910 * where this function called, there should be always be another device (or
1911 * this_dev) which is active.
1912 */
David Sterbab105e922019-10-01 19:57:35 +02001913void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001914 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08001915{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001916 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08001917 struct btrfs_device *next_device;
1918
1919 if (this_dev)
1920 next_device = this_dev;
1921 else
1922 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1923 device);
1924 ASSERT(next_device);
1925
1926 if (fs_info->sb->s_bdev &&
1927 (fs_info->sb->s_bdev == device->bdev))
1928 fs_info->sb->s_bdev = next_device->bdev;
1929
1930 if (fs_info->fs_devices->latest_bdev == device->bdev)
1931 fs_info->fs_devices->latest_bdev = next_device->bdev;
1932}
1933
Anand Jain1da73962018-08-10 13:53:21 +08001934/*
1935 * Return btrfs_fs_devices::num_devices excluding the device that's being
1936 * currently replaced.
1937 */
1938static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1939{
1940 u64 num_devices = fs_info->fs_devices->num_devices;
1941
David Sterbacb5583d2018-09-07 16:11:23 +02001942 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001943 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1944 ASSERT(num_devices > 1);
1945 num_devices--;
1946 }
David Sterbacb5583d2018-09-07 16:11:23 +02001947 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001948
1949 return num_devices;
1950}
1951
David Sterbada353f62017-02-14 17:55:53 +01001952int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1953 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001954{
1955 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001956 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001957 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001958 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001959 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001960
1961 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001962
Anand Jain1da73962018-08-10 13:53:21 +08001963 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04001964
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001965 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001966 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001967 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001968
Nikolay Borisova27a94c2018-09-03 12:46:14 +03001969 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
1970
1971 if (IS_ERR(device)) {
1972 if (PTR_ERR(device) == -ENOENT &&
1973 strcmp(device_path, "missing") == 0)
1974 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1975 else
1976 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04001977 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03001978 }
Yan Zheng2b820322008-11-17 21:11:30 -05001979
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001980 if (btrfs_pinned_by_swapfile(fs_info, device)) {
1981 btrfs_warn_in_rcu(fs_info,
1982 "cannot remove device %s (devid %llu) due to active swapfile",
1983 rcu_str_deref(device->name), device->devid);
1984 ret = -ETXTBSY;
1985 goto out;
1986 }
1987
Anand Jain401e29c2017-12-04 12:54:55 +08001988 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001989 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001990 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001991 }
1992
Anand Jainebbede42017-12-04 12:54:52 +08001993 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1994 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001995 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001996 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001997 }
1998
Anand Jainebbede42017-12-04 12:54:52 +08001999 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002000 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002001 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002002 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002003 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002004 }
Chris Masona061fc82008-05-07 11:43:44 -04002005
Carey Underwoodd7901552013-03-04 16:37:06 -06002006 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002007 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002008 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002009 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002010 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002011
Stefan Behrens63a212a2012-11-05 18:29:28 +01002012 /*
2013 * TODO: the superblock still includes this device in its num_devices
2014 * counter although write_all_supers() is not locked out. This
2015 * could give a filesystem state which requires a degraded mount.
2016 */
David Sterbaf331a952019-03-20 16:31:53 +01002017 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002018 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002019 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002020
Anand Jaine12c9622017-12-04 12:54:53 +08002021 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002022 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002023
2024 /*
2025 * the device list mutex makes sure that we don't change
2026 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002027 * the device supers. Whoever is writing all supers, should
2028 * lock the device list mutex before getting the number of
2029 * devices in the super block (super_copy). Conversely,
2030 * whoever updates the number of devices in the super block
2031 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002032 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002033
Anand Jain41a52a02018-04-12 10:29:31 +08002034 /*
2035 * In normal cases the cur_devices == fs_devices. But in case
2036 * of deleting a seed device, the cur_devices should point to
2037 * its own fs_devices listed under the fs_devices->seed.
2038 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002039 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002040 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002041 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002042
Anand Jain41a52a02018-04-12 10:29:31 +08002043 cur_devices->num_devices--;
2044 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002045 /* Update total_devices of the parent fs_devices if it's seed */
2046 if (cur_devices != fs_devices)
2047 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002048
Anand Jaine6e674b2017-12-04 12:54:54 +08002049 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002050 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002051
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002052 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002053
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002054 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002055 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002056 /* remove sysfs entry */
Anand Jainf3cd2c52020-02-12 17:28:13 +08002057 btrfs_sysfs_remove_devices_dir(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002058 }
Anand Jain99994cd2014-06-03 11:36:00 +08002059
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002060 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2061 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002062 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002063
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002064 /*
2065 * at this point, the device is zero sized and detached from
2066 * the devices list. All that's left is to zero out the old
2067 * supers and free the device.
2068 */
Anand Jainebbede42017-12-04 12:54:52 +08002069 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002070 btrfs_scratch_superblocks(device->bdev, device->name->str);
2071
2072 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002073 synchronize_rcu();
2074 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002075
Xiao Guangrong1f781602011-04-20 10:09:16 +00002076 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002077 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002078 if (fs_devices->seed == cur_devices) {
2079 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002080 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002081 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002082 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002083 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002084 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002085 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002086 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002087 }
2088
Chris Masona061fc82008-05-07 11:43:44 -04002089out:
2090 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002091 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002092
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002093error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002094 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002095 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002096 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002097 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002098 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002099 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002100 }
Anand Jain24fc5722016-02-13 10:01:36 +08002101 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002102}
2103
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002104void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002105{
Anand Jaind51908c2014-08-13 14:24:19 +08002106 struct btrfs_fs_devices *fs_devices;
2107
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002108 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002109
Anand Jain25e8e912014-08-20 10:56:56 +08002110 /*
2111 * in case of fs with no seed, srcdev->fs_devices will point
2112 * to fs_devices of fs_info. However when the dev being replaced is
2113 * a seed dev it will point to the seed's local fs_devices. In short
2114 * srcdev will have its correct fs_devices in both the cases.
2115 */
2116 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002117
Stefan Behrense93c89c2012-11-05 17:33:06 +01002118 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002119 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002120 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002121 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002122 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002123
Anand Jainebbede42017-12-04 12:54:52 +08002124 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002125 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002126
Miao Xie82372bc2014-09-03 21:35:44 +08002127 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002128 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002129}
2130
David Sterba65237ee2019-03-20 16:34:54 +01002131void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002132{
David Sterba65237ee2019-03-20 16:34:54 +01002133 struct btrfs_fs_info *fs_info = srcdev->fs_info;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002134 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002135
Anand Jainebbede42017-12-04 12:54:52 +08002136 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002137 /* zero out the old super if it is writable */
2138 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2139 }
Anand Jain14238812016-07-22 06:04:53 +08002140
2141 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002142 synchronize_rcu();
2143 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002144
Anand Jain94d5f0c2014-08-13 14:24:22 +08002145 /* if this is no devs we rather delete the fs_devices */
2146 if (!fs_devices->num_devices) {
2147 struct btrfs_fs_devices *tmp_fs_devices;
2148
Anand Jain6dd38f82017-10-17 06:53:50 +08002149 /*
2150 * On a mounted FS, num_devices can't be zero unless it's a
2151 * seed. In case of a seed device being replaced, the replace
2152 * target added to the sprout FS, so there will be no more
2153 * device left under the seed FS.
2154 */
2155 ASSERT(fs_devices->seeding);
2156
Anand Jain94d5f0c2014-08-13 14:24:22 +08002157 tmp_fs_devices = fs_info->fs_devices;
2158 while (tmp_fs_devices) {
2159 if (tmp_fs_devices->seed == fs_devices) {
2160 tmp_fs_devices->seed = fs_devices->seed;
2161 break;
2162 }
2163 tmp_fs_devices = tmp_fs_devices->seed;
2164 }
2165 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002166 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002167 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002168 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002169}
2170
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002171void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002172{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002173 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002174
Anand Jaind9a071f2018-04-12 10:29:38 +08002175 mutex_lock(&fs_devices->device_list_mutex);
2176
Anand Jainf3cd2c52020-02-12 17:28:13 +08002177 btrfs_sysfs_remove_devices_dir(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002178
Anand Jain779bf3fe2016-04-18 16:51:23 +08002179 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002180 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002181
Anand Jaind9a071f2018-04-12 10:29:38 +08002182 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002183
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002184 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002185
Stefan Behrense93c89c2012-11-05 17:33:06 +01002186 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002187
Anand Jaind9a071f2018-04-12 10:29:38 +08002188 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002189
2190 /*
2191 * The update_dev_time() with in btrfs_scratch_superblocks()
2192 * may lead to a call to btrfs_show_devname() which will try
2193 * to hold device_list_mutex. And here this device
2194 * is already out of device list, so we don't have to hold
2195 * the device_list_mutex lock.
2196 */
2197 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002198
2199 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002200 synchronize_rcu();
2201 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002202}
2203
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002204static struct btrfs_device *btrfs_find_device_by_path(
2205 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002206{
2207 int ret = 0;
2208 struct btrfs_super_block *disk_super;
2209 u64 devid;
2210 u8 *dev_uuid;
2211 struct block_device *bdev;
2212 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002213 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002214
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002215 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002216 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002217 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002218 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002219 disk_super = (struct btrfs_super_block *)bh->b_data;
2220 devid = btrfs_stack_device_id(&disk_super->dev_item);
2221 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002222 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002223 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002224 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002225 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002226 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002227 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002228
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002229 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002230 if (!device)
2231 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002232 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002233 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002234}
2235
Yan Zheng2b820322008-11-17 21:11:30 -05002236/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002237 * Lookup a device given by device id, or the path if the id is 0.
2238 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002239struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002240 struct btrfs_fs_info *fs_info, u64 devid,
2241 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002242{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002243 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002244
David Sterba5c5c0df2016-02-15 16:39:55 +01002245 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002246 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002247 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002248 if (!device)
2249 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002250 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002251 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002252
2253 if (!device_path || !device_path[0])
2254 return ERR_PTR(-EINVAL);
2255
2256 if (strcmp(device_path, "missing") == 0) {
2257 /* Find first missing device */
2258 list_for_each_entry(device, &fs_info->fs_devices->devices,
2259 dev_list) {
2260 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2261 &device->dev_state) && !device->bdev)
2262 return device;
2263 }
2264 return ERR_PTR(-ENOENT);
2265 }
2266
2267 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002268}
2269
Yan Zheng2b820322008-11-17 21:11:30 -05002270/*
2271 * does all the dirty work required for changing file system's UUID.
2272 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002273static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002274{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002275 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002276 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002277 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002278 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002279 struct btrfs_device *device;
2280 u64 super_flags;
2281
David Sterbaa32bf9a2018-03-16 02:21:22 +01002282 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002283 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002284 return -EINVAL;
2285
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002286 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002287 if (IS_ERR(seed_devices))
2288 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002289
Yan Zhenge4404d62008-12-12 10:03:26 -05002290 old_devices = clone_fs_devices(fs_devices);
2291 if (IS_ERR(old_devices)) {
2292 kfree(seed_devices);
2293 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002294 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002295
Anand Jainc4babc52018-04-12 10:29:25 +08002296 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002297
Yan Zhenge4404d62008-12-12 10:03:26 -05002298 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2299 seed_devices->opened = 1;
2300 INIT_LIST_HEAD(&seed_devices->devices);
2301 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002302 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002303
Anand Jain321a4bf2018-07-16 22:58:09 +08002304 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002305 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2306 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002307 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002308 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002309
David Sterba34441362016-10-04 19:34:27 +02002310 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002311 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002312 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002313
Johannes Thumshirn0395d842019-11-13 11:27:27 +01002314 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002315 fs_devices->num_devices = 0;
2316 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002317 fs_devices->missing_devices = 0;
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002318 fs_devices->rotating = false;
Yan Zhenge4404d62008-12-12 10:03:26 -05002319 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002320
2321 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002322 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002323 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002324 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002325
Yan Zheng2b820322008-11-17 21:11:30 -05002326 super_flags = btrfs_super_flags(disk_super) &
2327 ~BTRFS_SUPER_FLAG_SEEDING;
2328 btrfs_set_super_flags(disk_super, super_flags);
2329
2330 return 0;
2331}
2332
2333/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002334 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002335 */
David Sterba5c4666292019-03-20 16:36:39 +01002336static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002337{
David Sterba5c4666292019-03-20 16:36:39 +01002338 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002339 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002340 struct btrfs_path *path;
2341 struct extent_buffer *leaf;
2342 struct btrfs_dev_item *dev_item;
2343 struct btrfs_device *device;
2344 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002345 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002346 u8 dev_uuid[BTRFS_UUID_SIZE];
2347 u64 devid;
2348 int ret;
2349
2350 path = btrfs_alloc_path();
2351 if (!path)
2352 return -ENOMEM;
2353
Yan Zheng2b820322008-11-17 21:11:30 -05002354 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2355 key.offset = 0;
2356 key.type = BTRFS_DEV_ITEM_KEY;
2357
2358 while (1) {
2359 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2360 if (ret < 0)
2361 goto error;
2362
2363 leaf = path->nodes[0];
2364next_slot:
2365 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2366 ret = btrfs_next_leaf(root, path);
2367 if (ret > 0)
2368 break;
2369 if (ret < 0)
2370 goto error;
2371 leaf = path->nodes[0];
2372 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002373 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002374 continue;
2375 }
2376
2377 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2378 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2379 key.type != BTRFS_DEV_ITEM_KEY)
2380 break;
2381
2382 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2383 struct btrfs_dev_item);
2384 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002385 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002386 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002387 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002388 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002389 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002390 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002391 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002392
2393 if (device->fs_devices->seeding) {
2394 btrfs_set_device_generation(leaf, dev_item,
2395 device->generation);
2396 btrfs_mark_buffer_dirty(leaf);
2397 }
2398
2399 path->slots[0]++;
2400 goto next_slot;
2401 }
2402 ret = 0;
2403error:
2404 btrfs_free_path(path);
2405 return ret;
2406}
2407
David Sterbada353f62017-02-14 17:55:53 +01002408int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002409{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002410 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002411 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002412 struct btrfs_trans_handle *trans;
2413 struct btrfs_device *device;
2414 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002415 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002416 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002417 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002418 u64 orig_super_total_bytes;
2419 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002420 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002421 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002422 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002423
Anand Jain5da54bc2018-07-03 13:14:50 +08002424 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002425 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002426
Li Zefana5d163332011-12-07 20:08:40 -05002427 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002428 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002429 if (IS_ERR(bdev))
2430 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002431
Anand Jain5da54bc2018-07-03 13:14:50 +08002432 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002433 seeding_dev = 1;
2434 down_write(&sb->s_umount);
2435 mutex_lock(&uuid_mutex);
2436 }
2437
Chris Mason8c8bee12008-09-29 11:19:10 -04002438 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002439
Anand Jain5da54bc2018-07-03 13:14:50 +08002440 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002441 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002442 if (device->bdev == bdev) {
2443 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002444 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002445 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002446 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002447 }
2448 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002449 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002451 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002452 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002453 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002454 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002455 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002456 }
2457
David Sterba78f2c9e2016-02-11 14:25:38 +01002458 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002459 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002460 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002461 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002462 }
Josef Bacik606686e2012-06-04 14:03:51 -04002463 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002464
Yan, Zhenga22285a2010-05-16 10:48:46 -04002465 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002466 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002467 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002468 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002469 }
2470
Josef Bacikd5e20032011-08-04 14:52:27 +00002471 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002472 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002473 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002474 device->io_width = fs_info->sectorsize;
2475 device->io_align = fs_info->sectorsize;
2476 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002477 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2478 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002479 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002480 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002481 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002482 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002483 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002484 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002485 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002486 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002487 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002488
Yan Zheng2b820322008-11-17 21:11:30 -05002489 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002490 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002491 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002492 if (ret) {
2493 btrfs_abort_transaction(trans, ret);
2494 goto error_trans;
2495 }
Yan Zheng2b820322008-11-17 21:11:30 -05002496 }
2497
Anand Jain5da54bc2018-07-03 13:14:50 +08002498 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002499
Anand Jain5da54bc2018-07-03 13:14:50 +08002500 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002501 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002502 list_add_rcu(&device->dev_list, &fs_devices->devices);
2503 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2504 fs_devices->num_devices++;
2505 fs_devices->open_devices++;
2506 fs_devices->rw_devices++;
2507 fs_devices->total_devices++;
2508 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002509
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002510 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002511
Anand Jaine884f4f2017-04-04 18:40:19 +08002512 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002513 fs_devices->rotating = true;
Chris Masonc2898112009-06-10 09:51:32 -04002514
Naohiro Aota39379fa2018-07-27 09:04:55 +09002515 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002516 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002517 round_down(orig_super_total_bytes + device->total_bytes,
2518 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002519
Naohiro Aota39379fa2018-07-27 09:04:55 +09002520 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2521 btrfs_set_super_num_devices(fs_info->super_copy,
2522 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002523
2524 /* add sysfs device entry */
Anand Jainf3cd2c52020-02-12 17:28:13 +08002525 btrfs_sysfs_add_devices_dir(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002526
Miao Xie2196d6e2014-09-03 21:35:41 +08002527 /*
2528 * we've got more storage, clear any full flags on the space
2529 * infos
2530 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002531 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002532
David Sterba34441362016-10-04 19:34:27 +02002533 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002534 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002535
Yan Zheng2b820322008-11-17 21:11:30 -05002536 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002537 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002538 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002539 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002540 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002541 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002542 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002543 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002544 }
2545
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002546 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002547 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002548 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002549 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002550 }
2551
2552 if (seeding_dev) {
David Sterba5c4666292019-03-20 16:36:39 +01002553 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002554 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002555 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002556 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002557 }
Anand Jainb2373f22014-06-03 11:36:03 +08002558
David Sterbaf93c3992019-08-01 18:50:16 +02002559 btrfs_sysfs_update_sprout_fsid(fs_devices,
2560 fs_info->fs_devices->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05002561 }
2562
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002563 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002564
2565 if (seeding_dev) {
2566 mutex_unlock(&uuid_mutex);
2567 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002568 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002569
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002570 if (ret) /* transaction commit */
2571 return ret;
2572
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002573 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002574 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002575 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002576 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002577 trans = btrfs_attach_transaction(root);
2578 if (IS_ERR(trans)) {
2579 if (PTR_ERR(trans) == -ENOENT)
2580 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002581 ret = PTR_ERR(trans);
2582 trans = NULL;
2583 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002584 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002585 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002586 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002587
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002588 /* Update ctime/mtime for libblkid */
2589 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002590 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002591
Anand Jaind31c32f2017-09-28 14:51:10 +08002592error_sysfs:
Anand Jainf3cd2c52020-02-12 17:28:13 +08002593 btrfs_sysfs_remove_devices_dir(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002594 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2595 mutex_lock(&fs_info->chunk_mutex);
2596 list_del_rcu(&device->dev_list);
2597 list_del(&device->dev_alloc_list);
2598 fs_info->fs_devices->num_devices--;
2599 fs_info->fs_devices->open_devices--;
2600 fs_info->fs_devices->rw_devices--;
2601 fs_info->fs_devices->total_devices--;
2602 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2603 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2604 btrfs_set_super_total_bytes(fs_info->super_copy,
2605 orig_super_total_bytes);
2606 btrfs_set_super_num_devices(fs_info->super_copy,
2607 orig_super_num_devices);
2608 mutex_unlock(&fs_info->chunk_mutex);
2609 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002610error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002611 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002612 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002613 if (trans)
2614 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002615error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002616 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002617error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002618 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002619 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002620 mutex_unlock(&uuid_mutex);
2621 up_write(&sb->s_umount);
2622 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002623 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002624}
2625
Chris Masond3977122009-01-05 21:25:51 -05002626static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2627 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002628{
2629 int ret;
2630 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002631 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002632 struct btrfs_dev_item *dev_item;
2633 struct extent_buffer *leaf;
2634 struct btrfs_key key;
2635
Chris Mason0b86a832008-03-24 15:01:56 -04002636 path = btrfs_alloc_path();
2637 if (!path)
2638 return -ENOMEM;
2639
2640 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2641 key.type = BTRFS_DEV_ITEM_KEY;
2642 key.offset = device->devid;
2643
2644 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2645 if (ret < 0)
2646 goto out;
2647
2648 if (ret > 0) {
2649 ret = -ENOENT;
2650 goto out;
2651 }
2652
2653 leaf = path->nodes[0];
2654 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2655
2656 btrfs_set_device_id(leaf, dev_item, device->devid);
2657 btrfs_set_device_type(leaf, dev_item, device->type);
2658 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2659 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2660 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002661 btrfs_set_device_total_bytes(leaf, dev_item,
2662 btrfs_device_get_disk_total_bytes(device));
2663 btrfs_set_device_bytes_used(leaf, dev_item,
2664 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002665 btrfs_mark_buffer_dirty(leaf);
2666
2667out:
2668 btrfs_free_path(path);
2669 return ret;
2670}
2671
Miao Xie2196d6e2014-09-03 21:35:41 +08002672int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002673 struct btrfs_device *device, u64 new_size)
2674{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 struct btrfs_fs_info *fs_info = device->fs_info;
2676 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002677 u64 old_total;
2678 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002679
Anand Jainebbede42017-12-04 12:54:52 +08002680 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002681 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002682
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002683 new_size = round_down(new_size, fs_info->sectorsize);
2684
David Sterba34441362016-10-04 19:34:27 +02002685 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002686 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002687 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002688
Stefan Behrens63a212a2012-11-05 18:29:28 +01002689 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002690 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002691 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002692 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002693 }
Yan Zheng2b820322008-11-17 21:11:30 -05002694
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002695 btrfs_set_super_total_bytes(super_copy,
2696 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002697 device->fs_devices->total_rw_bytes += diff;
2698
Miao Xie7cc8e582014-09-03 21:35:38 +08002699 btrfs_device_set_total_bytes(device, new_size);
2700 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002701 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002702 if (list_empty(&device->post_commit_list))
2703 list_add_tail(&device->post_commit_list,
2704 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002705 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002706
Chris Mason8f18cf12008-04-25 16:53:30 -04002707 return btrfs_update_device(trans, device);
2708}
2709
Nikolay Borisovf4208792018-07-20 19:37:52 +03002710static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002711{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002712 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002713 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002714 int ret;
2715 struct btrfs_path *path;
2716 struct btrfs_key key;
2717
Chris Mason8f18cf12008-04-25 16:53:30 -04002718 path = btrfs_alloc_path();
2719 if (!path)
2720 return -ENOMEM;
2721
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002722 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002723 key.offset = chunk_offset;
2724 key.type = BTRFS_CHUNK_ITEM_KEY;
2725
2726 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002727 if (ret < 0)
2728 goto out;
2729 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002730 btrfs_handle_fs_error(fs_info, -ENOENT,
2731 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002732 ret = -ENOENT;
2733 goto out;
2734 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002735
2736 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002737 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002738 btrfs_handle_fs_error(fs_info, ret,
2739 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002740out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002741 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002742 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002743}
2744
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002745static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002746{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002747 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002748 struct btrfs_disk_key *disk_key;
2749 struct btrfs_chunk *chunk;
2750 u8 *ptr;
2751 int ret = 0;
2752 u32 num_stripes;
2753 u32 array_size;
2754 u32 len = 0;
2755 u32 cur;
2756 struct btrfs_key key;
2757
David Sterba34441362016-10-04 19:34:27 +02002758 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002759 array_size = btrfs_super_sys_array_size(super_copy);
2760
2761 ptr = super_copy->sys_chunk_array;
2762 cur = 0;
2763
2764 while (cur < array_size) {
2765 disk_key = (struct btrfs_disk_key *)ptr;
2766 btrfs_disk_key_to_cpu(&key, disk_key);
2767
2768 len = sizeof(*disk_key);
2769
2770 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2771 chunk = (struct btrfs_chunk *)(ptr + len);
2772 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2773 len += btrfs_chunk_item_size(num_stripes);
2774 } else {
2775 ret = -EIO;
2776 break;
2777 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002778 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002779 key.offset == chunk_offset) {
2780 memmove(ptr, ptr + len, array_size - (cur + len));
2781 array_size -= len;
2782 btrfs_set_super_sys_array_size(super_copy, array_size);
2783 } else {
2784 ptr += len;
2785 cur += len;
2786 }
2787 }
David Sterba34441362016-10-04 19:34:27 +02002788 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002789 return ret;
2790}
2791
Omar Sandoval60ca8422018-05-16 16:34:31 -07002792/*
2793 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2794 * @logical: Logical block offset in bytes.
2795 * @length: Length of extent in bytes.
2796 *
2797 * Return: Chunk mapping or ERR_PTR.
2798 */
2799struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2800 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002801{
2802 struct extent_map_tree *em_tree;
2803 struct extent_map *em;
2804
David Sterbac8bf1b62019-05-17 11:43:17 +02002805 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002806 read_lock(&em_tree->lock);
2807 em = lookup_extent_mapping(em_tree, logical, length);
2808 read_unlock(&em_tree->lock);
2809
2810 if (!em) {
2811 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2812 logical, length);
2813 return ERR_PTR(-EINVAL);
2814 }
2815
2816 if (em->start > logical || em->start + em->len < logical) {
2817 btrfs_crit(fs_info,
2818 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2819 logical, length, em->start, em->start + em->len);
2820 free_extent_map(em);
2821 return ERR_PTR(-EINVAL);
2822 }
2823
2824 /* callers are responsible for dropping em's ref. */
2825 return em;
2826}
2827
Nikolay Borisov97aff912018-07-20 19:37:53 +03002828int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002829{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002830 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002831 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002832 struct map_lookup *map;
2833 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002834 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002835 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002836
Omar Sandoval60ca8422018-05-16 16:34:31 -07002837 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002838 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002839 /*
2840 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002841 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002842 * do anything we still error out.
2843 */
2844 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002845 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002846 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002847 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002848 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002849 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002850 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002851
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002852 /*
2853 * Take the device list mutex to prevent races with the final phase of
2854 * a device replace operation that replaces the device object associated
2855 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2856 */
2857 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002858 for (i = 0; i < map->num_stripes; i++) {
2859 struct btrfs_device *device = map->stripes[i].dev;
2860 ret = btrfs_free_dev_extent(trans, device,
2861 map->stripes[i].physical,
2862 &dev_extent_len);
2863 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002864 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002865 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002866 goto out;
2867 }
2868
2869 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002870 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002871 btrfs_device_set_bytes_used(device,
2872 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002873 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002875 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002876 }
2877
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03002878 ret = btrfs_update_device(trans, device);
2879 if (ret) {
2880 mutex_unlock(&fs_devices->device_list_mutex);
2881 btrfs_abort_transaction(trans, ret);
2882 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002883 }
2884 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002885 mutex_unlock(&fs_devices->device_list_mutex);
2886
Nikolay Borisovf4208792018-07-20 19:37:52 +03002887 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002889 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002890 goto out;
2891 }
2892
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002893 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894
2895 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002896 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002897 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002898 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002899 goto out;
2900 }
2901 }
2902
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03002903 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002904 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002905 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002906 goto out;
2907 }
2908
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909out:
2910 /* once for us */
2911 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002912 return ret;
2913}
2914
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002915static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002916{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002917 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002918 struct btrfs_trans_handle *trans;
Dennis Zhoub0643e52019-12-13 16:22:14 -08002919 struct btrfs_block_group *block_group;
Chris Mason8f18cf12008-04-25 16:53:30 -04002920 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002921
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002922 /*
2923 * Prevent races with automatic removal of unused block groups.
2924 * After we relocate and before we remove the chunk with offset
2925 * chunk_offset, automatic removal of the block group can kick in,
2926 * resulting in a failure when calling btrfs_remove_chunk() below.
2927 *
2928 * Make sure to acquire this mutex before doing a tree search (dev
2929 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2930 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2931 * we release the path used to search the chunk/dev tree and before
2932 * the current task acquires this mutex and calls us.
2933 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002934 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002935
Chris Mason8f18cf12008-04-25 16:53:30 -04002936 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002937 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002939 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002940 if (ret)
2941 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002942
Dennis Zhoub0643e52019-12-13 16:22:14 -08002943 block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
2944 if (!block_group)
2945 return -ENOENT;
2946 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
2947 btrfs_put_block_group(block_group);
2948
Chris Mason19c4d2f2016-10-10 13:43:31 -07002949 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2950 chunk_offset);
2951 if (IS_ERR(trans)) {
2952 ret = PTR_ERR(trans);
2953 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2954 return ret;
2955 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002956
Chris Mason19c4d2f2016-10-10 13:43:31 -07002957 /*
2958 * step two, delete the device extents and the
2959 * chunk tree entries
2960 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03002961 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002962 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002963 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002964}
2965
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002966static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002967{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002968 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002969 struct btrfs_path *path;
2970 struct extent_buffer *leaf;
2971 struct btrfs_chunk *chunk;
2972 struct btrfs_key key;
2973 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002974 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002975 bool retried = false;
2976 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002977 int ret;
2978
2979 path = btrfs_alloc_path();
2980 if (!path)
2981 return -ENOMEM;
2982
Josef Bacikba1bf482009-09-11 16:11:19 -04002983again:
Yan Zheng2b820322008-11-17 21:11:30 -05002984 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2985 key.offset = (u64)-1;
2986 key.type = BTRFS_CHUNK_ITEM_KEY;
2987
2988 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002989 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002990 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002991 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002992 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002993 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002994 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002995 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002996
2997 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2998 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002999 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003000 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003001 if (ret < 0)
3002 goto error;
3003 if (ret > 0)
3004 break;
3005
3006 leaf = path->nodes[0];
3007 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3008
3009 chunk = btrfs_item_ptr(leaf, path->slots[0],
3010 struct btrfs_chunk);
3011 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003012 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003013
3014 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003015 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003016 if (ret == -ENOSPC)
3017 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303018 else
3019 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003020 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003021 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003022
3023 if (found_key.offset == 0)
3024 break;
3025 key.offset = found_key.offset - 1;
3026 }
3027 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003028 if (failed && !retried) {
3029 failed = 0;
3030 retried = true;
3031 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303032 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003033 ret = -ENOSPC;
3034 }
Yan Zheng2b820322008-11-17 21:11:30 -05003035error:
3036 btrfs_free_path(path);
3037 return ret;
3038}
3039
Liu Boa6f93c72017-11-15 16:28:11 -07003040/*
3041 * return 1 : allocate a data chunk successfully,
3042 * return <0: errors during allocating a data chunk,
3043 * return 0 : no need to allocate a data chunk.
3044 */
3045static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3046 u64 chunk_offset)
3047{
David Sterba32da53862019-10-29 19:20:18 +01003048 struct btrfs_block_group *cache;
Liu Boa6f93c72017-11-15 16:28:11 -07003049 u64 bytes_used;
3050 u64 chunk_type;
3051
3052 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3053 ASSERT(cache);
3054 chunk_type = cache->flags;
3055 btrfs_put_block_group(cache);
3056
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003057 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3058 return 0;
Liu Boa6f93c72017-11-15 16:28:11 -07003059
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003060 spin_lock(&fs_info->data_sinfo->lock);
3061 bytes_used = fs_info->data_sinfo->bytes_used;
3062 spin_unlock(&fs_info->data_sinfo->lock);
Liu Boa6f93c72017-11-15 16:28:11 -07003063
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003064 if (!bytes_used) {
3065 struct btrfs_trans_handle *trans;
3066 int ret;
Liu Boa6f93c72017-11-15 16:28:11 -07003067
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003068 trans = btrfs_join_transaction(fs_info->tree_root);
3069 if (IS_ERR(trans))
3070 return PTR_ERR(trans);
3071
3072 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3073 btrfs_end_transaction(trans);
3074 if (ret < 0)
3075 return ret;
3076 return 1;
Liu Boa6f93c72017-11-15 16:28:11 -07003077 }
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003078
Liu Boa6f93c72017-11-15 16:28:11 -07003079 return 0;
3080}
3081
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003082static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003083 struct btrfs_balance_control *bctl)
3084{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003085 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003086 struct btrfs_trans_handle *trans;
3087 struct btrfs_balance_item *item;
3088 struct btrfs_disk_balance_args disk_bargs;
3089 struct btrfs_path *path;
3090 struct extent_buffer *leaf;
3091 struct btrfs_key key;
3092 int ret, err;
3093
3094 path = btrfs_alloc_path();
3095 if (!path)
3096 return -ENOMEM;
3097
3098 trans = btrfs_start_transaction(root, 0);
3099 if (IS_ERR(trans)) {
3100 btrfs_free_path(path);
3101 return PTR_ERR(trans);
3102 }
3103
3104 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003105 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003106 key.offset = 0;
3107
3108 ret = btrfs_insert_empty_item(trans, root, path, &key,
3109 sizeof(*item));
3110 if (ret)
3111 goto out;
3112
3113 leaf = path->nodes[0];
3114 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3115
David Sterbab159fa22016-11-08 18:09:03 +01003116 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003117
3118 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3119 btrfs_set_balance_data(leaf, item, &disk_bargs);
3120 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3121 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3122 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3123 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3124
3125 btrfs_set_balance_flags(leaf, item, bctl->flags);
3126
3127 btrfs_mark_buffer_dirty(leaf);
3128out:
3129 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003130 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003131 if (err && !ret)
3132 ret = err;
3133 return ret;
3134}
3135
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003136static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003137{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003138 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003139 struct btrfs_trans_handle *trans;
3140 struct btrfs_path *path;
3141 struct btrfs_key key;
3142 int ret, err;
3143
3144 path = btrfs_alloc_path();
3145 if (!path)
3146 return -ENOMEM;
3147
3148 trans = btrfs_start_transaction(root, 0);
3149 if (IS_ERR(trans)) {
3150 btrfs_free_path(path);
3151 return PTR_ERR(trans);
3152 }
3153
3154 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003155 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003156 key.offset = 0;
3157
3158 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3159 if (ret < 0)
3160 goto out;
3161 if (ret > 0) {
3162 ret = -ENOENT;
3163 goto out;
3164 }
3165
3166 ret = btrfs_del_item(trans, root, path);
3167out:
3168 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003169 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003170 if (err && !ret)
3171 ret = err;
3172 return ret;
3173}
3174
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003175/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003176 * This is a heuristic used to reduce the number of chunks balanced on
3177 * resume after balance was interrupted.
3178 */
3179static void update_balance_args(struct btrfs_balance_control *bctl)
3180{
3181 /*
3182 * Turn on soft mode for chunk types that were being converted.
3183 */
3184 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3185 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3186 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3187 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3188 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3189 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3190
3191 /*
3192 * Turn on usage filter if is not already used. The idea is
3193 * that chunks that we have already balanced should be
3194 * reasonably full. Don't do it for chunks that are being
3195 * converted - that will keep us from relocating unconverted
3196 * (albeit full) chunks.
3197 */
3198 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003199 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003200 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3201 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3202 bctl->data.usage = 90;
3203 }
3204 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003205 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003206 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3207 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3208 bctl->sys.usage = 90;
3209 }
3210 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003211 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003212 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3213 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3214 bctl->meta.usage = 90;
3215 }
3216}
3217
3218/*
David Sterba149196a2018-03-20 20:23:09 +01003219 * Clear the balance status in fs_info and delete the balance item from disk.
3220 */
3221static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003222{
3223 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003224 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003225
3226 BUG_ON(!fs_info->balance_ctl);
3227
3228 spin_lock(&fs_info->balance_lock);
3229 fs_info->balance_ctl = NULL;
3230 spin_unlock(&fs_info->balance_lock);
3231
3232 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003233 ret = del_balance_item(fs_info);
3234 if (ret)
3235 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003236}
3237
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003238/*
3239 * Balance filters. Return 1 if chunk should be filtered out
3240 * (should not be balanced).
3241 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003242static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003243 struct btrfs_balance_args *bargs)
3244{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003245 chunk_type = chunk_to_extended(chunk_type) &
3246 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003247
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003248 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003249 return 0;
3250
3251 return 1;
3252}
3253
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003254static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003255 struct btrfs_balance_args *bargs)
3256{
David Sterba32da53862019-10-29 19:20:18 +01003257 struct btrfs_block_group *cache;
David Sterbabc309462015-10-20 18:22:13 +02003258 u64 chunk_used;
3259 u64 user_thresh_min;
3260 u64 user_thresh_max;
3261 int ret = 1;
3262
3263 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003264 chunk_used = cache->used;
David Sterbabc309462015-10-20 18:22:13 +02003265
3266 if (bargs->usage_min == 0)
3267 user_thresh_min = 0;
3268 else
David Sterbab3470b52019-10-23 18:48:22 +02003269 user_thresh_min = div_factor_fine(cache->length,
3270 bargs->usage_min);
David Sterbabc309462015-10-20 18:22:13 +02003271
3272 if (bargs->usage_max == 0)
3273 user_thresh_max = 1;
3274 else if (bargs->usage_max > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003275 user_thresh_max = cache->length;
David Sterbabc309462015-10-20 18:22:13 +02003276 else
David Sterbab3470b52019-10-23 18:48:22 +02003277 user_thresh_max = div_factor_fine(cache->length,
3278 bargs->usage_max);
David Sterbabc309462015-10-20 18:22:13 +02003279
3280 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3281 ret = 0;
3282
3283 btrfs_put_block_group(cache);
3284 return ret;
3285}
3286
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003287static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003288 u64 chunk_offset, struct btrfs_balance_args *bargs)
3289{
David Sterba32da53862019-10-29 19:20:18 +01003290 struct btrfs_block_group *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003291 u64 chunk_used, user_thresh;
3292 int ret = 1;
3293
3294 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003295 chunk_used = cache->used;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003296
David Sterbabc309462015-10-20 18:22:13 +02003297 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003298 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003299 else if (bargs->usage > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003300 user_thresh = cache->length;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003301 else
David Sterbab3470b52019-10-23 18:48:22 +02003302 user_thresh = div_factor_fine(cache->length, bargs->usage);
Ilya Dryomova105bb82013-01-21 15:15:56 +02003303
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003304 if (chunk_used < user_thresh)
3305 ret = 0;
3306
3307 btrfs_put_block_group(cache);
3308 return ret;
3309}
3310
Ilya Dryomov409d4042012-01-16 22:04:47 +02003311static int chunk_devid_filter(struct extent_buffer *leaf,
3312 struct btrfs_chunk *chunk,
3313 struct btrfs_balance_args *bargs)
3314{
3315 struct btrfs_stripe *stripe;
3316 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3317 int i;
3318
3319 for (i = 0; i < num_stripes; i++) {
3320 stripe = btrfs_stripe_nr(chunk, i);
3321 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3322 return 0;
3323 }
3324
3325 return 1;
3326}
3327
David Sterba946c9252019-05-17 11:43:34 +02003328static u64 calc_data_stripes(u64 type, int num_stripes)
3329{
3330 const int index = btrfs_bg_flags_to_raid_index(type);
3331 const int ncopies = btrfs_raid_array[index].ncopies;
3332 const int nparity = btrfs_raid_array[index].nparity;
3333
3334 if (nparity)
3335 return num_stripes - nparity;
3336 else
3337 return num_stripes / ncopies;
3338}
3339
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003340/* [pstart, pend) */
3341static int chunk_drange_filter(struct extent_buffer *leaf,
3342 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003343 struct btrfs_balance_args *bargs)
3344{
3345 struct btrfs_stripe *stripe;
3346 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3347 u64 stripe_offset;
3348 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003349 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003350 int factor;
3351 int i;
3352
3353 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3354 return 0;
3355
David Sterba946c9252019-05-17 11:43:34 +02003356 type = btrfs_chunk_type(leaf, chunk);
3357 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003358
3359 for (i = 0; i < num_stripes; i++) {
3360 stripe = btrfs_stripe_nr(chunk, i);
3361 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3362 continue;
3363
3364 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3365 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003366 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003367
3368 if (stripe_offset < bargs->pend &&
3369 stripe_offset + stripe_length > bargs->pstart)
3370 return 0;
3371 }
3372
3373 return 1;
3374}
3375
Ilya Dryomovea671762012-01-16 22:04:48 +02003376/* [vstart, vend) */
3377static int chunk_vrange_filter(struct extent_buffer *leaf,
3378 struct btrfs_chunk *chunk,
3379 u64 chunk_offset,
3380 struct btrfs_balance_args *bargs)
3381{
3382 if (chunk_offset < bargs->vend &&
3383 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3384 /* at least part of the chunk is inside this vrange */
3385 return 0;
3386
3387 return 1;
3388}
3389
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003390static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3391 struct btrfs_chunk *chunk,
3392 struct btrfs_balance_args *bargs)
3393{
3394 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3395
3396 if (bargs->stripes_min <= num_stripes
3397 && num_stripes <= bargs->stripes_max)
3398 return 0;
3399
3400 return 1;
3401}
3402
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003403static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003404 struct btrfs_balance_args *bargs)
3405{
3406 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3407 return 0;
3408
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003409 chunk_type = chunk_to_extended(chunk_type) &
3410 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003411
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003412 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003413 return 1;
3414
3415 return 0;
3416}
3417
David Sterba6ec08962019-03-20 16:38:52 +01003418static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003419 struct btrfs_chunk *chunk, u64 chunk_offset)
3420{
David Sterba6ec08962019-03-20 16:38:52 +01003421 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003422 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003423 struct btrfs_balance_args *bargs = NULL;
3424 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3425
3426 /* type filter */
3427 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3428 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3429 return 0;
3430 }
3431
3432 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3433 bargs = &bctl->data;
3434 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3435 bargs = &bctl->sys;
3436 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3437 bargs = &bctl->meta;
3438
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003439 /* profiles filter */
3440 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3441 chunk_profiles_filter(chunk_type, bargs)) {
3442 return 0;
3443 }
3444
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003445 /* usage filter */
3446 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003447 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003448 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003449 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003450 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003451 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003452 }
3453
Ilya Dryomov409d4042012-01-16 22:04:47 +02003454 /* devid filter */
3455 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3456 chunk_devid_filter(leaf, chunk, bargs)) {
3457 return 0;
3458 }
3459
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003460 /* drange filter, makes sense only with devid filter */
3461 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003462 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003463 return 0;
3464 }
3465
Ilya Dryomovea671762012-01-16 22:04:48 +02003466 /* vrange filter */
3467 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3468 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3469 return 0;
3470 }
3471
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003472 /* stripes filter */
3473 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3474 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3475 return 0;
3476 }
3477
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003478 /* soft profile changing mode */
3479 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3480 chunk_soft_convert_filter(chunk_type, bargs)) {
3481 return 0;
3482 }
3483
David Sterba7d824b62014-05-07 17:37:51 +02003484 /*
3485 * limited by count, must be the last filter
3486 */
3487 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3488 if (bargs->limit == 0)
3489 return 0;
3490 else
3491 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003492 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3493 /*
3494 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003495 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003496 * about the count of all chunks that satisfy the filters.
3497 */
3498 if (bargs->limit_max == 0)
3499 return 0;
3500 else
3501 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003502 }
3503
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003504 return 1;
3505}
3506
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003507static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003508{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003509 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003510 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003511 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003512 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003513 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003514 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003515 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003516 struct extent_buffer *leaf;
3517 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003518 int ret;
3519 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003520 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003521 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003522 u64 limit_data = bctl->data.limit;
3523 u64 limit_meta = bctl->meta.limit;
3524 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003525 u32 count_data = 0;
3526 u32 count_meta = 0;
3527 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003528 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003529
Chris Masonec44a352008-04-28 15:29:52 -04003530 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003531 if (!path) {
3532 ret = -ENOMEM;
3533 goto error;
3534 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003535
3536 /* zero out stat counters */
3537 spin_lock(&fs_info->balance_lock);
3538 memset(&bctl->stat, 0, sizeof(bctl->stat));
3539 spin_unlock(&fs_info->balance_lock);
3540again:
David Sterba7d824b62014-05-07 17:37:51 +02003541 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003542 /*
3543 * The single value limit and min/max limits use the same bytes
3544 * in the
3545 */
David Sterba7d824b62014-05-07 17:37:51 +02003546 bctl->data.limit = limit_data;
3547 bctl->meta.limit = limit_meta;
3548 bctl->sys.limit = limit_sys;
3549 }
Chris Masonec44a352008-04-28 15:29:52 -04003550 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3551 key.offset = (u64)-1;
3552 key.type = BTRFS_CHUNK_ITEM_KEY;
3553
Chris Masond3977122009-01-05 21:25:51 -05003554 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003555 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003556 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003557 ret = -ECANCELED;
3558 goto error;
3559 }
3560
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003561 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003562 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003563 if (ret < 0) {
3564 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003565 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003566 }
Chris Masonec44a352008-04-28 15:29:52 -04003567
3568 /*
3569 * this shouldn't happen, it means the last relocate
3570 * failed
3571 */
3572 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003573 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003574
3575 ret = btrfs_previous_item(chunk_root, path, 0,
3576 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003577 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003578 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003579 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003580 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003581 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003582
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003583 leaf = path->nodes[0];
3584 slot = path->slots[0];
3585 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3586
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003587 if (found_key.objectid != key.objectid) {
3588 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003589 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003590 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003591
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003592 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003593 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003594
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003595 if (!counting) {
3596 spin_lock(&fs_info->balance_lock);
3597 bctl->stat.considered++;
3598 spin_unlock(&fs_info->balance_lock);
3599 }
3600
David Sterba6ec08962019-03-20 16:38:52 +01003601 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003602
David Sterbab3b4aa72011-04-21 01:20:15 +02003603 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003604 if (!ret) {
3605 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003606 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003607 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003608
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003609 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003610 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003611 spin_lock(&fs_info->balance_lock);
3612 bctl->stat.expected++;
3613 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003614
3615 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3616 count_data++;
3617 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3618 count_sys++;
3619 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3620 count_meta++;
3621
3622 goto loop;
3623 }
3624
3625 /*
3626 * Apply limit_min filter, no need to check if the LIMITS
3627 * filter is used, limit_min is 0 by default
3628 */
3629 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3630 count_data < bctl->data.limit_min)
3631 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3632 count_meta < bctl->meta.limit_min)
3633 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3634 count_sys < bctl->sys.limit_min)) {
3635 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003636 goto loop;
3637 }
3638
Liu Boa6f93c72017-11-15 16:28:11 -07003639 if (!chunk_reserved) {
3640 /*
3641 * We may be relocating the only data chunk we have,
3642 * which could potentially end up with losing data's
3643 * raid profile, so lets allocate an empty one in
3644 * advance.
3645 */
3646 ret = btrfs_may_alloc_data_chunk(fs_info,
3647 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003648 if (ret < 0) {
3649 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3650 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003651 } else if (ret == 1) {
3652 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003653 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003654 }
3655
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003656 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003657 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003658 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003659 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003660 } else if (ret == -ETXTBSY) {
3661 btrfs_info(fs_info,
3662 "skipping relocation of block group %llu due to active swapfile",
3663 found_key.offset);
3664 ret = 0;
3665 } else if (ret) {
3666 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003667 } else {
3668 spin_lock(&fs_info->balance_lock);
3669 bctl->stat.completed++;
3670 spin_unlock(&fs_info->balance_lock);
3671 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003672loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003673 if (found_key.offset == 0)
3674 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003675 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003676 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003677
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003678 if (counting) {
3679 btrfs_release_path(path);
3680 counting = false;
3681 goto again;
3682 }
Chris Masonec44a352008-04-28 15:29:52 -04003683error:
3684 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003685 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003686 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003687 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003688 if (!ret)
3689 ret = -ENOSPC;
3690 }
3691
Chris Masonec44a352008-04-28 15:29:52 -04003692 return ret;
3693}
3694
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003695/**
3696 * alloc_profile_is_valid - see if a given profile is valid and reduced
3697 * @flags: profile to validate
3698 * @extended: if true @flags is treated as an extended profile
3699 */
3700static int alloc_profile_is_valid(u64 flags, int extended)
3701{
3702 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3703 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3704
3705 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3706
3707 /* 1) check that all other bits are zeroed */
3708 if (flags & ~mask)
3709 return 0;
3710
3711 /* 2) see if profile is reduced */
3712 if (flags == 0)
3713 return !extended; /* "0" is valid for usual profiles */
3714
David Sterbac1499162019-10-01 19:44:42 +02003715 return has_single_bit_set(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003716}
3717
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003718static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3719{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003720 /* cancel requested || normal exit path */
3721 return atomic_read(&fs_info->balance_cancel_req) ||
3722 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3723 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003724}
3725
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003726/* Non-zero return value signifies invalidity */
3727static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3728 u64 allowed)
3729{
3730 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3731 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3732 (bctl_arg->target & ~allowed)));
3733}
3734
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003735/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003736 * Fill @buf with textual description of balance filter flags @bargs, up to
3737 * @size_buf including the terminating null. The output may be trimmed if it
3738 * does not fit into the provided buffer.
3739 */
3740static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3741 u32 size_buf)
3742{
3743 int ret;
3744 u32 size_bp = size_buf;
3745 char *bp = buf;
3746 u64 flags = bargs->flags;
3747 char tmp_buf[128] = {'\0'};
3748
3749 if (!flags)
3750 return;
3751
3752#define CHECK_APPEND_NOARG(a) \
3753 do { \
3754 ret = snprintf(bp, size_bp, (a)); \
3755 if (ret < 0 || ret >= size_bp) \
3756 goto out_overflow; \
3757 size_bp -= ret; \
3758 bp += ret; \
3759 } while (0)
3760
3761#define CHECK_APPEND_1ARG(a, v1) \
3762 do { \
3763 ret = snprintf(bp, size_bp, (a), (v1)); \
3764 if (ret < 0 || ret >= size_bp) \
3765 goto out_overflow; \
3766 size_bp -= ret; \
3767 bp += ret; \
3768 } while (0)
3769
3770#define CHECK_APPEND_2ARG(a, v1, v2) \
3771 do { \
3772 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3773 if (ret < 0 || ret >= size_bp) \
3774 goto out_overflow; \
3775 size_bp -= ret; \
3776 bp += ret; \
3777 } while (0)
3778
David Sterba158da512019-05-17 11:43:41 +02003779 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3780 CHECK_APPEND_1ARG("convert=%s,",
3781 btrfs_bg_type_to_raid_name(bargs->target));
Anand Jain56fc37d2018-11-20 16:12:56 +08003782
3783 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3784 CHECK_APPEND_NOARG("soft,");
3785
3786 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3787 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3788 sizeof(tmp_buf));
3789 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3790 }
3791
3792 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3793 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3794
3795 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3796 CHECK_APPEND_2ARG("usage=%u..%u,",
3797 bargs->usage_min, bargs->usage_max);
3798
3799 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3800 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3801
3802 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3803 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3804 bargs->pstart, bargs->pend);
3805
3806 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3807 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3808 bargs->vstart, bargs->vend);
3809
3810 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3811 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3812
3813 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3814 CHECK_APPEND_2ARG("limit=%u..%u,",
3815 bargs->limit_min, bargs->limit_max);
3816
3817 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3818 CHECK_APPEND_2ARG("stripes=%u..%u,",
3819 bargs->stripes_min, bargs->stripes_max);
3820
3821#undef CHECK_APPEND_2ARG
3822#undef CHECK_APPEND_1ARG
3823#undef CHECK_APPEND_NOARG
3824
3825out_overflow:
3826
3827 if (size_bp < size_buf)
3828 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3829 else
3830 buf[0] = '\0';
3831}
3832
3833static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3834{
3835 u32 size_buf = 1024;
3836 char tmp_buf[192] = {'\0'};
3837 char *buf;
3838 char *bp;
3839 u32 size_bp = size_buf;
3840 int ret;
3841 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3842
3843 buf = kzalloc(size_buf, GFP_KERNEL);
3844 if (!buf)
3845 return;
3846
3847 bp = buf;
3848
3849#define CHECK_APPEND_1ARG(a, v1) \
3850 do { \
3851 ret = snprintf(bp, size_bp, (a), (v1)); \
3852 if (ret < 0 || ret >= size_bp) \
3853 goto out_overflow; \
3854 size_bp -= ret; \
3855 bp += ret; \
3856 } while (0)
3857
3858 if (bctl->flags & BTRFS_BALANCE_FORCE)
3859 CHECK_APPEND_1ARG("%s", "-f ");
3860
3861 if (bctl->flags & BTRFS_BALANCE_DATA) {
3862 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3863 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3864 }
3865
3866 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3867 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3868 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3869 }
3870
3871 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3872 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3873 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3874 }
3875
3876#undef CHECK_APPEND_1ARG
3877
3878out_overflow:
3879
3880 if (size_bp < size_buf)
3881 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3882 btrfs_info(fs_info, "balance: %s %s",
3883 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3884 "resume" : "start", buf);
3885
3886 kfree(buf);
3887}
3888
3889/*
David Sterbadccdb072018-03-21 00:20:05 +01003890 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003891 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003892int btrfs_balance(struct btrfs_fs_info *fs_info,
3893 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003894 struct btrfs_ioctl_balance_args *bargs)
3895{
Adam Borowski14506122017-03-07 23:34:44 +01003896 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003897 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003898 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003899 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003900 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003901 unsigned seq;
Anand Jaine62869b2019-09-25 14:29:28 +08003902 bool reducing_redundancy;
David Sterba081db892019-05-17 11:43:27 +02003903 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003904
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003905 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003906 atomic_read(&fs_info->balance_pause_req) ||
3907 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003908 ret = -EINVAL;
3909 goto out;
3910 }
3911
Ilya Dryomove4837f82012-03-27 17:09:17 +03003912 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3913 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3914 mixed = 1;
3915
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003916 /*
3917 * In case of mixed groups both data and meta should be picked,
3918 * and identical options should be given for both of them.
3919 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003920 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3921 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003922 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3923 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3924 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003925 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003926 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003927 ret = -EINVAL;
3928 goto out;
3929 }
3930 }
3931
Josef Bacikb35cf1f2020-01-10 11:11:24 -05003932 /*
3933 * rw_devices will not change at the moment, device add/delete/replace
3934 * are excluded by EXCL_OP
3935 */
3936 num_devices = fs_info->fs_devices->rw_devices;
Qu Wenruofab27352019-09-25 10:13:27 +08003937
3938 /*
3939 * SINGLE profile on-disk has no profile bit, but in-memory we have a
3940 * special bit for it, to make it easier to distinguish. Thus we need
3941 * to set it manually, or balance would refuse the profile.
3942 */
3943 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
David Sterba081db892019-05-17 11:43:27 +02003944 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
3945 if (num_devices >= btrfs_raid_array[i].devs_min)
3946 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08003947
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003948 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003949 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003950 "balance: invalid convert data profile %s",
David Sterba158da512019-05-17 11:43:41 +02003951 btrfs_bg_type_to_raid_name(bctl->data.target));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003952 ret = -EINVAL;
3953 goto out;
3954 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003955 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003956 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003957 "balance: invalid convert metadata profile %s",
David Sterba158da512019-05-17 11:43:41 +02003958 btrfs_bg_type_to_raid_name(bctl->meta.target));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003959 ret = -EINVAL;
3960 goto out;
3961 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003962 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003963 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003964 "balance: invalid convert system profile %s",
David Sterba158da512019-05-17 11:43:41 +02003965 btrfs_bg_type_to_raid_name(bctl->sys.target));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003966 ret = -EINVAL;
3967 goto out;
3968 }
3969
David Sterba6079e122019-05-17 11:43:29 +02003970 /*
3971 * Allow to reduce metadata or system integrity only if force set for
3972 * profiles with redundancy (copies, parity)
3973 */
3974 allowed = 0;
3975 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
3976 if (btrfs_raid_array[i].ncopies >= 2 ||
3977 btrfs_raid_array[i].tolerated_failures >= 1)
3978 allowed |= btrfs_raid_array[i].bg_flag;
3979 }
Miao Xiede98ced2013-01-29 10:13:12 +00003980 do {
3981 seq = read_seqbegin(&fs_info->profiles_lock);
3982
3983 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3984 (fs_info->avail_system_alloc_bits & allowed) &&
3985 !(bctl->sys.target & allowed)) ||
3986 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3987 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00003988 !(bctl->meta.target & allowed)))
Anand Jaine62869b2019-09-25 14:29:28 +08003989 reducing_redundancy = true;
Filipe Manana5a8067c2018-11-19 09:48:12 +00003990 else
Anand Jaine62869b2019-09-25 14:29:28 +08003991 reducing_redundancy = false;
Filipe Manana5a8067c2018-11-19 09:48:12 +00003992
3993 /* if we're not converting, the target field is uninitialized */
3994 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3995 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3996 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3997 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00003998 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003999
Anand Jaine62869b2019-09-25 14:29:28 +08004000 if (reducing_redundancy) {
Filipe Manana5a8067c2018-11-19 09:48:12 +00004001 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4002 btrfs_info(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004003 "balance: force reducing metadata redundancy");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004004 } else {
4005 btrfs_err(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004006 "balance: reduces metadata redundancy, use --force if you want this");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004007 ret = -EINVAL;
4008 goto out;
4009 }
4010 }
4011
Adam Borowski14506122017-03-07 23:34:44 +01004012 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4013 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00004014 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004015 "balance: metadata profile %s has lower redundancy than data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004016 btrfs_bg_type_to_raid_name(meta_target),
4017 btrfs_bg_type_to_raid_name(data_target));
Sam Tygieree592d02016-01-06 08:46:12 +00004018 }
4019
Filipe Manana9e967492019-04-22 16:44:09 +01004020 if (fs_info->send_in_progress) {
4021 btrfs_warn_rl(fs_info,
4022"cannot run balance while send operations are in progress (%d in progress)",
4023 fs_info->send_in_progress);
4024 ret = -EAGAIN;
4025 goto out;
4026 }
4027
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004028 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004029 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004030 goto out;
4031
Ilya Dryomov59641012012-01-16 22:04:48 +02004032 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4033 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004034 BUG_ON(fs_info->balance_ctl);
4035 spin_lock(&fs_info->balance_lock);
4036 fs_info->balance_ctl = bctl;
4037 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004038 } else {
4039 BUG_ON(ret != -EEXIST);
4040 spin_lock(&fs_info->balance_lock);
4041 update_balance_args(bctl);
4042 spin_unlock(&fs_info->balance_lock);
4043 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004044
David Sterba3009a622018-03-21 01:31:04 +01004045 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4046 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004047 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004048 mutex_unlock(&fs_info->balance_mutex);
4049
4050 ret = __btrfs_balance(fs_info);
4051
4052 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004053 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4054 btrfs_info(fs_info, "balance: paused");
4055 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4056 btrfs_info(fs_info, "balance: canceled");
4057 else
4058 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4059
David Sterba3009a622018-03-21 01:31:04 +01004060 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004061
4062 if (bargs) {
4063 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004064 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004065 }
4066
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004067 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4068 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004069 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004070 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004071 }
4072
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004073 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004074
4075 return ret;
4076out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004077 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004078 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004079 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004080 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004081 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4082
Ilya Dryomov59641012012-01-16 22:04:48 +02004083 return ret;
4084}
4085
4086static int balance_kthread(void *data)
4087{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004088 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004089 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004090
Ilya Dryomov59641012012-01-16 22:04:48 +02004091 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004092 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004093 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004094 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004095
Ilya Dryomov59641012012-01-16 22:04:48 +02004096 return ret;
4097}
4098
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004099int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4100{
4101 struct task_struct *tsk;
4102
David Sterba1354e1a2018-03-21 02:29:13 +01004103 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004104 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004105 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004106 return 0;
4107 }
David Sterba1354e1a2018-03-21 02:29:13 +01004108 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004109
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004110 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004111 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004112 return 0;
4113 }
4114
Anand Jain02ee6542018-05-17 15:16:51 +08004115 /*
4116 * A ro->rw remount sequence should continue with the paused balance
4117 * regardless of who pauses it, system or the user as of now, so set
4118 * the resume flag.
4119 */
4120 spin_lock(&fs_info->balance_lock);
4121 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4122 spin_unlock(&fs_info->balance_lock);
4123
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004124 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304125 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004126}
4127
Ilya Dryomov68310a52012-06-22 12:24:12 -06004128int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004129{
Ilya Dryomov59641012012-01-16 22:04:48 +02004130 struct btrfs_balance_control *bctl;
4131 struct btrfs_balance_item *item;
4132 struct btrfs_disk_balance_args disk_bargs;
4133 struct btrfs_path *path;
4134 struct extent_buffer *leaf;
4135 struct btrfs_key key;
4136 int ret;
4137
4138 path = btrfs_alloc_path();
4139 if (!path)
4140 return -ENOMEM;
4141
Ilya Dryomov68310a52012-06-22 12:24:12 -06004142 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004143 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004144 key.offset = 0;
4145
4146 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4147 if (ret < 0)
4148 goto out;
4149 if (ret > 0) { /* ret = -ENOENT; */
4150 ret = 0;
4151 goto out;
4152 }
4153
Ilya Dryomov59641012012-01-16 22:04:48 +02004154 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4155 if (!bctl) {
4156 ret = -ENOMEM;
4157 goto out;
4158 }
4159
Ilya Dryomov59641012012-01-16 22:04:48 +02004160 leaf = path->nodes[0];
4161 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4162
Ilya Dryomov68310a52012-06-22 12:24:12 -06004163 bctl->flags = btrfs_balance_flags(leaf, item);
4164 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004165
4166 btrfs_balance_data(leaf, item, &disk_bargs);
4167 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4168 btrfs_balance_meta(leaf, item, &disk_bargs);
4169 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4170 btrfs_balance_sys(leaf, item, &disk_bargs);
4171 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4172
David Sterbaeee95e32018-03-20 20:07:58 +01004173 /*
4174 * This should never happen, as the paused balance state is recovered
4175 * during mount without any chance of other exclusive ops to collide.
4176 *
4177 * This gives the exclusive op status to balance and keeps in paused
4178 * state until user intervention (cancel or umount). If the ownership
4179 * cannot be assigned, show a message but do not fail. The balance
4180 * is in a paused state and must have fs_info::balance_ctl properly
4181 * set up.
4182 */
4183 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4184 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004185 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004186
Ilya Dryomov68310a52012-06-22 12:24:12 -06004187 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004188 BUG_ON(fs_info->balance_ctl);
4189 spin_lock(&fs_info->balance_lock);
4190 fs_info->balance_ctl = bctl;
4191 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004192 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004193out:
4194 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004195 return ret;
4196}
4197
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004198int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4199{
4200 int ret = 0;
4201
4202 mutex_lock(&fs_info->balance_mutex);
4203 if (!fs_info->balance_ctl) {
4204 mutex_unlock(&fs_info->balance_mutex);
4205 return -ENOTCONN;
4206 }
4207
David Sterba3009a622018-03-21 01:31:04 +01004208 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004209 atomic_inc(&fs_info->balance_pause_req);
4210 mutex_unlock(&fs_info->balance_mutex);
4211
4212 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004213 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004214
4215 mutex_lock(&fs_info->balance_mutex);
4216 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004217 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004218 atomic_dec(&fs_info->balance_pause_req);
4219 } else {
4220 ret = -ENOTCONN;
4221 }
4222
4223 mutex_unlock(&fs_info->balance_mutex);
4224 return ret;
4225}
4226
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004227int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4228{
4229 mutex_lock(&fs_info->balance_mutex);
4230 if (!fs_info->balance_ctl) {
4231 mutex_unlock(&fs_info->balance_mutex);
4232 return -ENOTCONN;
4233 }
4234
David Sterbacf7d20f2018-03-21 01:45:32 +01004235 /*
4236 * A paused balance with the item stored on disk can be resumed at
4237 * mount time if the mount is read-write. Otherwise it's still paused
4238 * and we must not allow cancelling as it deletes the item.
4239 */
4240 if (sb_rdonly(fs_info->sb)) {
4241 mutex_unlock(&fs_info->balance_mutex);
4242 return -EROFS;
4243 }
4244
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004245 atomic_inc(&fs_info->balance_cancel_req);
4246 /*
4247 * if we are running just wait and return, balance item is
4248 * deleted in btrfs_balance in this case
4249 */
David Sterba3009a622018-03-21 01:31:04 +01004250 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004251 mutex_unlock(&fs_info->balance_mutex);
4252 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004253 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004254 mutex_lock(&fs_info->balance_mutex);
4255 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004256 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004257 /*
4258 * Lock released to allow other waiters to continue, we'll
4259 * reexamine the status again.
4260 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004261 mutex_lock(&fs_info->balance_mutex);
4262
David Sterbaa17c95d2018-03-20 17:28:05 +01004263 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004264 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004265 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004266 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004267 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004268 }
4269
David Sterba3009a622018-03-21 01:31:04 +01004270 BUG_ON(fs_info->balance_ctl ||
4271 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004272 atomic_dec(&fs_info->balance_cancel_req);
4273 mutex_unlock(&fs_info->balance_mutex);
4274 return 0;
4275}
4276
Stefan Behrens803b2f52013-08-15 17:11:21 +02004277static int btrfs_uuid_scan_kthread(void *data)
4278{
4279 struct btrfs_fs_info *fs_info = data;
4280 struct btrfs_root *root = fs_info->tree_root;
4281 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004282 struct btrfs_path *path = NULL;
4283 int ret = 0;
4284 struct extent_buffer *eb;
4285 int slot;
4286 struct btrfs_root_item root_item;
4287 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004288 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004289
4290 path = btrfs_alloc_path();
4291 if (!path) {
4292 ret = -ENOMEM;
4293 goto out;
4294 }
4295
4296 key.objectid = 0;
4297 key.type = BTRFS_ROOT_ITEM_KEY;
4298 key.offset = 0;
4299
Stefan Behrens803b2f52013-08-15 17:11:21 +02004300 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004301 ret = btrfs_search_forward(root, &key, path,
4302 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004303 if (ret) {
4304 if (ret > 0)
4305 ret = 0;
4306 break;
4307 }
4308
4309 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4310 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4311 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4312 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4313 goto skip;
4314
4315 eb = path->nodes[0];
4316 slot = path->slots[0];
4317 item_size = btrfs_item_size_nr(eb, slot);
4318 if (item_size < sizeof(root_item))
4319 goto skip;
4320
Stefan Behrens803b2f52013-08-15 17:11:21 +02004321 read_extent_buffer(eb, &root_item,
4322 btrfs_item_ptr_offset(eb, slot),
4323 (int)sizeof(root_item));
4324 if (btrfs_root_refs(&root_item) == 0)
4325 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004326
4327 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4328 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4329 if (trans)
4330 goto update_tree;
4331
4332 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004333 /*
4334 * 1 - subvol uuid item
4335 * 1 - received_subvol uuid item
4336 */
4337 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4338 if (IS_ERR(trans)) {
4339 ret = PTR_ERR(trans);
4340 break;
4341 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004342 continue;
4343 } else {
4344 goto skip;
4345 }
4346update_tree:
4347 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004348 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004349 BTRFS_UUID_KEY_SUBVOL,
4350 key.objectid);
4351 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004352 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004353 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004354 break;
4355 }
4356 }
4357
4358 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004359 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004360 root_item.received_uuid,
4361 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4362 key.objectid);
4363 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004364 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004365 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004366 break;
4367 }
4368 }
4369
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004370skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004371 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004372 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004373 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004374 if (ret)
4375 break;
4376 }
4377
Stefan Behrens803b2f52013-08-15 17:11:21 +02004378 btrfs_release_path(path);
4379 if (key.offset < (u64)-1) {
4380 key.offset++;
4381 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4382 key.offset = 0;
4383 key.type = BTRFS_ROOT_ITEM_KEY;
4384 } else if (key.objectid < (u64)-1) {
4385 key.offset = 0;
4386 key.type = BTRFS_ROOT_ITEM_KEY;
4387 key.objectid++;
4388 } else {
4389 break;
4390 }
4391 cond_resched();
4392 }
4393
4394out:
4395 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004396 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004397 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004398 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004399 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004400 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004401 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004402 up(&fs_info->uuid_tree_rescan_sem);
4403 return 0;
4404}
4405
Stefan Behrens70f80172013-08-15 17:11:23 +02004406/*
4407 * Callback for btrfs_uuid_tree_iterate().
4408 * returns:
4409 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004410 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004411 * > 0 if the check failed, which means the caller shall remove the entry.
4412 */
4413static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4414 u8 *uuid, u8 type, u64 subid)
4415{
4416 struct btrfs_key key;
4417 int ret = 0;
4418 struct btrfs_root *subvol_root;
4419
4420 if (type != BTRFS_UUID_KEY_SUBVOL &&
4421 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4422 goto out;
4423
4424 key.objectid = subid;
4425 key.type = BTRFS_ROOT_ITEM_KEY;
4426 key.offset = (u64)-1;
Josef Bacik3619c942020-01-24 09:32:24 -05004427 subvol_root = btrfs_get_fs_root(fs_info, &key, true);
Stefan Behrens70f80172013-08-15 17:11:23 +02004428 if (IS_ERR(subvol_root)) {
4429 ret = PTR_ERR(subvol_root);
4430 if (ret == -ENOENT)
4431 ret = 1;
4432 goto out;
4433 }
4434
4435 switch (type) {
4436 case BTRFS_UUID_KEY_SUBVOL:
4437 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4438 ret = 1;
4439 break;
4440 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4441 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4442 BTRFS_UUID_SIZE))
4443 ret = 1;
4444 break;
4445 }
Josef Bacik00246522020-01-24 09:33:01 -05004446 btrfs_put_root(subvol_root);
Stefan Behrens70f80172013-08-15 17:11:23 +02004447out:
4448 return ret;
4449}
4450
4451static int btrfs_uuid_rescan_kthread(void *data)
4452{
4453 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4454 int ret;
4455
4456 /*
4457 * 1st step is to iterate through the existing UUID tree and
4458 * to delete all entries that contain outdated data.
4459 * 2nd step is to add all missing entries to the UUID tree.
4460 */
4461 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4462 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004463 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004464 up(&fs_info->uuid_tree_rescan_sem);
4465 return ret;
4466 }
4467 return btrfs_uuid_scan_kthread(data);
4468}
4469
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004470int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4471{
4472 struct btrfs_trans_handle *trans;
4473 struct btrfs_root *tree_root = fs_info->tree_root;
4474 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004475 struct task_struct *task;
4476 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004477
4478 /*
4479 * 1 - root node
4480 * 1 - root item
4481 */
4482 trans = btrfs_start_transaction(tree_root, 2);
4483 if (IS_ERR(trans))
4484 return PTR_ERR(trans);
4485
David Sterba9b7a2442019-03-20 13:20:49 +01004486 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004487 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004488 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004489 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004490 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004491 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004492 }
4493
4494 fs_info->uuid_root = uuid_root;
4495
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004496 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004497 if (ret)
4498 return ret;
4499
4500 down(&fs_info->uuid_tree_rescan_sem);
4501 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4502 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004503 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004504 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004505 up(&fs_info->uuid_tree_rescan_sem);
4506 return PTR_ERR(task);
4507 }
4508
4509 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004510}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004511
Stefan Behrens70f80172013-08-15 17:11:23 +02004512int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4513{
4514 struct task_struct *task;
4515
4516 down(&fs_info->uuid_tree_rescan_sem);
4517 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4518 if (IS_ERR(task)) {
4519 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004520 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004521 up(&fs_info->uuid_tree_rescan_sem);
4522 return PTR_ERR(task);
4523 }
4524
4525 return 0;
4526}
4527
Chris Mason8f18cf12008-04-25 16:53:30 -04004528/*
4529 * shrinking a device means finding all of the device extents past
4530 * the new size, and then following the back refs to the chunks.
4531 * The chunk relocation code actually frees the device extent
4532 */
4533int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4534{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004535 struct btrfs_fs_info *fs_info = device->fs_info;
4536 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004537 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004538 struct btrfs_dev_extent *dev_extent = NULL;
4539 struct btrfs_path *path;
4540 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004541 u64 chunk_offset;
4542 int ret;
4543 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004544 int failed = 0;
4545 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004546 struct extent_buffer *l;
4547 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004548 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004549 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004550 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004551 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004552 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004553
4554 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004555 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004556 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004557
Anand Jain401e29c2017-12-04 12:54:55 +08004558 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004559 return -EINVAL;
4560
Chris Mason8f18cf12008-04-25 16:53:30 -04004561 path = btrfs_alloc_path();
4562 if (!path)
4563 return -ENOMEM;
4564
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004565 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004566
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004567 trans = btrfs_start_transaction(root, 0);
4568 if (IS_ERR(trans)) {
4569 btrfs_free_path(path);
4570 return PTR_ERR(trans);
4571 }
4572
David Sterba34441362016-10-04 19:34:27 +02004573 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004574
Miao Xie7cc8e582014-09-03 21:35:38 +08004575 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004576 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004577 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004578 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004579 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004580
4581 /*
4582 * Once the device's size has been set to the new size, ensure all
4583 * in-memory chunks are synced to disk so that the loop below sees them
4584 * and relocates them accordingly.
4585 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004586 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004587 mutex_unlock(&fs_info->chunk_mutex);
4588 ret = btrfs_commit_transaction(trans);
4589 if (ret)
4590 goto done;
4591 } else {
4592 mutex_unlock(&fs_info->chunk_mutex);
4593 btrfs_end_transaction(trans);
4594 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004595
Josef Bacikba1bf482009-09-11 16:11:19 -04004596again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004597 key.objectid = device->devid;
4598 key.offset = (u64)-1;
4599 key.type = BTRFS_DEV_EXTENT_KEY;
4600
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004601 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004602 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004604 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004605 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004606 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004607 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004608
4609 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004610 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004611 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004612 if (ret < 0)
4613 goto done;
4614 if (ret) {
4615 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004616 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004617 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004618 }
4619
4620 l = path->nodes[0];
4621 slot = path->slots[0];
4622 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4623
Josef Bacikba1bf482009-09-11 16:11:19 -04004624 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004625 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004626 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004627 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004628 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004629
4630 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4631 length = btrfs_dev_extent_length(l, dev_extent);
4632
Josef Bacikba1bf482009-09-11 16:11:19 -04004633 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004635 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004636 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004637 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004638
Chris Mason8f18cf12008-04-25 16:53:30 -04004639 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004640 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004641
Liu Boa6f93c72017-11-15 16:28:11 -07004642 /*
4643 * We may be relocating the only data chunk we have,
4644 * which could potentially end up with losing data's
4645 * raid profile, so lets allocate an empty one in
4646 * advance.
4647 */
4648 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4649 if (ret < 0) {
4650 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4651 goto done;
4652 }
4653
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004654 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4655 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004656 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004657 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004658 } else if (ret) {
4659 if (ret == -ETXTBSY) {
4660 btrfs_warn(fs_info,
4661 "could not shrink block group %llu due to active swapfile",
4662 chunk_offset);
4663 }
4664 goto done;
4665 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004666 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004667
4668 if (failed && !retried) {
4669 failed = 0;
4670 retried = true;
4671 goto again;
4672 } else if (failed && retried) {
4673 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004674 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004675 }
4676
Chris Balld6397ba2009-04-27 07:29:03 -04004677 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004678 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004679 if (IS_ERR(trans)) {
4680 ret = PTR_ERR(trans);
4681 goto done;
4682 }
4683
David Sterba34441362016-10-04 19:34:27 +02004684 mutex_lock(&fs_info->chunk_mutex);
Miao Xie7cc8e582014-09-03 21:35:38 +08004685 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004686 if (list_empty(&device->post_commit_list))
4687 list_add_tail(&device->post_commit_list,
4688 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004689
Chris Balld6397ba2009-04-27 07:29:03 -04004690 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004691 btrfs_set_super_total_bytes(super_copy,
4692 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004693 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004694
4695 /* Now btrfs_update_device() will change the on-disk size. */
4696 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004697 if (ret < 0) {
4698 btrfs_abort_transaction(trans, ret);
4699 btrfs_end_transaction(trans);
4700 } else {
4701 ret = btrfs_commit_transaction(trans);
4702 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004703done:
4704 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004705 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004706 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004707 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004708 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004709 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004710 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004711 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004712 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004713 return ret;
4714}
4715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004716static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004717 struct btrfs_key *key,
4718 struct btrfs_chunk *chunk, int item_size)
4719{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004720 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004721 struct btrfs_disk_key disk_key;
4722 u32 array_size;
4723 u8 *ptr;
4724
David Sterba34441362016-10-04 19:34:27 +02004725 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004726 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004727 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004728 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004729 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004730 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004731 }
Chris Mason0b86a832008-03-24 15:01:56 -04004732
4733 ptr = super_copy->sys_chunk_array + array_size;
4734 btrfs_cpu_key_to_disk(&disk_key, key);
4735 memcpy(ptr, &disk_key, sizeof(disk_key));
4736 ptr += sizeof(disk_key);
4737 memcpy(ptr, chunk, item_size);
4738 item_size += sizeof(disk_key);
4739 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004740 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004741
Chris Mason0b86a832008-03-24 15:01:56 -04004742 return 0;
4743}
4744
Miao Xieb2117a32011-01-05 10:07:28 +00004745/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004746 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004747 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004748static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004749{
Arne Jansen73c5de02011-04-12 12:07:57 +02004750 const struct btrfs_device_info *di_a = a;
4751 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004752
Arne Jansen73c5de02011-04-12 12:07:57 +02004753 if (di_a->max_avail > di_b->max_avail)
4754 return -1;
4755 if (di_a->max_avail < di_b->max_avail)
4756 return 1;
4757 if (di_a->total_avail > di_b->total_avail)
4758 return -1;
4759 if (di_a->total_avail < di_b->total_avail)
4760 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004761 return 0;
4762}
4763
David Woodhouse53b381b2013-01-29 18:40:14 -05004764static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4765{
Zhao Leiffe2d202015-01-20 15:11:44 +08004766 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004767 return;
4768
Miao Xieceda0862013-04-11 10:30:16 +00004769 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004770}
4771
David Sterbacfbb8252018-07-10 18:15:05 +02004772static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4773{
4774 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4775 return;
4776
4777 btrfs_set_fs_incompat(info, RAID1C34);
4778}
4779
Miao Xieb2117a32011-01-05 10:07:28 +00004780static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004781 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004782{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004783 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004784 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004785 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004786 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004787 struct extent_map_tree *em_tree;
4788 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004789 struct btrfs_device_info *devices_info = NULL;
4790 u64 total_avail;
4791 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004792 int data_stripes; /* number of stripes that count for
4793 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004794 int sub_stripes; /* sub_stripes info for map */
4795 int dev_stripes; /* stripes per dev */
4796 int devs_max; /* max devs to use */
4797 int devs_min; /* min devs needed */
4798 int devs_increment; /* ndevs has to be a multiple of this */
4799 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004800 int nparity; /* number of stripes worth of bytes to
4801 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00004802 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004803 u64 max_stripe_size;
4804 u64 max_chunk_size;
4805 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004806 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004807 int ndevs;
4808 int i;
4809 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004810 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004811
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004812 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004813
Qu Wenruo4117f202018-01-22 13:50:54 +08004814 if (list_empty(&fs_devices->alloc_list)) {
4815 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4816 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004817 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004818 }
Miao Xieb2117a32011-01-05 10:07:28 +00004819
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004820 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004821
Liu Bo31e50222012-11-21 14:18:10 +00004822 sub_stripes = btrfs_raid_array[index].sub_stripes;
4823 dev_stripes = btrfs_raid_array[index].dev_stripes;
4824 devs_max = btrfs_raid_array[index].devs_max;
David Sterbae3ecdb32019-05-17 11:43:38 +02004825 if (!devs_max)
4826 devs_max = BTRFS_MAX_DEVS(info);
Liu Bo31e50222012-11-21 14:18:10 +00004827 devs_min = btrfs_raid_array[index].devs_min;
4828 devs_increment = btrfs_raid_array[index].devs_increment;
4829 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004830 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02004831
4832 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004833 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08004834 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Arne Jansen73c5de02011-04-12 12:07:57 +02004835 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004836 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004837 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4838 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004839 else
Byongho Leeee221842015-12-15 01:42:10 +09004840 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004841 max_chunk_size = max_stripe_size;
4842 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004843 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004844 max_chunk_size = 2 * max_stripe_size;
Qu Wenruoc17add72019-08-28 10:33:12 +08004845 devs_max = min_t(int, devs_max, BTRFS_MAX_DEVS_SYS_CHUNK);
Arne Jansen73c5de02011-04-12 12:07:57 +02004846 } else {
David Sterba351fd352014-05-15 16:48:20 +02004847 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004848 type);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004849 BUG();
Arne Jansen73c5de02011-04-12 12:07:57 +02004850 }
4851
Andrea Gelmini52042d82018-11-28 12:05:13 +01004852 /* We don't want a chunk larger than 10% of writable space */
Arne Jansen73c5de02011-04-12 12:07:57 +02004853 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4854 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004855
David Sterba31e818f2015-02-20 18:00:26 +01004856 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004857 GFP_NOFS);
4858 if (!devices_info)
4859 return -ENOMEM;
4860
Arne Jansen73c5de02011-04-12 12:07:57 +02004861 /*
4862 * in the first pass through the devices list, we gather information
4863 * about the available holes on each device.
4864 */
4865 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004866 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004867 u64 max_avail;
4868 u64 dev_offset;
4869
Anand Jainebbede42017-12-04 12:54:52 +08004870 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004871 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004872 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004873 continue;
4874 }
4875
Anand Jaine12c9622017-12-04 12:54:53 +08004876 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4877 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004878 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004879 continue;
4880
4881 if (device->total_bytes > device->bytes_used)
4882 total_avail = device->total_bytes - device->bytes_used;
4883 else
4884 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004885
4886 /* If there is no space on this device, skip it. */
4887 if (total_avail == 0)
4888 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004889
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02004890 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004891 max_stripe_size * dev_stripes,
4892 &dev_offset, &max_avail);
4893 if (ret && ret != -ENOSPC)
4894 goto error;
4895
4896 if (ret == 0)
4897 max_avail = max_stripe_size * dev_stripes;
4898
Qu Wenruo4117f202018-01-22 13:50:54 +08004899 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4900 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4901 btrfs_debug(info,
4902 "%s: devid %llu has no free space, have=%llu want=%u",
4903 __func__, device->devid, max_avail,
4904 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004905 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004906 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004907
Eric Sandeen063d0062013-01-31 00:55:01 +00004908 if (ndevs == fs_devices->rw_devices) {
4909 WARN(1, "%s: found more than %llu devices\n",
4910 __func__, fs_devices->rw_devices);
4911 break;
4912 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004913 devices_info[ndevs].dev_offset = dev_offset;
4914 devices_info[ndevs].max_avail = max_avail;
4915 devices_info[ndevs].total_avail = total_avail;
4916 devices_info[ndevs].dev = device;
4917 ++ndevs;
4918 }
4919
4920 /*
4921 * now sort the devices by hole size / available space
4922 */
4923 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4924 btrfs_cmp_device_info, NULL);
4925
David Sterba47e6f742018-03-02 22:56:53 +01004926 /*
4927 * Round down to number of usable stripes, devs_increment can be any
4928 * number so we can't use round_down()
4929 */
4930 ndevs -= ndevs % devs_increment;
Arne Jansen73c5de02011-04-12 12:07:57 +02004931
Qu Wenruoba89b802018-01-31 13:56:15 +08004932 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004933 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004934 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4935 btrfs_debug(info,
4936 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004937 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004938 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004939 goto error;
4940 }
4941
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004942 ndevs = min(ndevs, devs_max);
4943
Arne Jansen73c5de02011-04-12 12:07:57 +02004944 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004945 * The primary goal is to maximize the number of stripes, so use as
4946 * many devices as possible, even if the stripes are not maximum sized.
4947 *
4948 * The DUP profile stores more than one stripe per device, the
4949 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004950 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004951 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004952 num_stripes = ndevs * dev_stripes;
4953
David Woodhouse53b381b2013-01-29 18:40:14 -05004954 /*
4955 * this will have to be fixed for RAID1 and RAID10 over
4956 * more drives
4957 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004958 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05004959
4960 /*
4961 * Use the number of data stripes to figure out how big this chunk
4962 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02004963 * and compare that answer with the max chunk size. If it's higher,
4964 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05004965 */
4966 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004967 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02004968 * Reduce stripe_size, round it up to a 16MB boundary again and
4969 * then use it, unless it ends up being even bigger than the
4970 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05004971 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02004972 stripe_size = min(round_up(div_u64(max_chunk_size,
4973 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004974 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004975 }
4976
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004977 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004978 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004979
Miao Xieb2117a32011-01-05 10:07:28 +00004980 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4981 if (!map) {
4982 ret = -ENOMEM;
4983 goto error;
4984 }
4985 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004986
Arne Jansen73c5de02011-04-12 12:07:57 +02004987 for (i = 0; i < ndevs; ++i) {
4988 for (j = 0; j < dev_stripes; ++j) {
4989 int s = i * dev_stripes + j;
4990 map->stripes[s].dev = devices_info[i].dev;
4991 map->stripes[s].physical = devices_info[i].dev_offset +
4992 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004993 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004994 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004995 map->stripe_len = BTRFS_STRIPE_LEN;
4996 map->io_align = BTRFS_STRIPE_LEN;
4997 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004998 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004999 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005000
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005001 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005002
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005003 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005004
David Sterba172ddd62011-04-21 00:48:27 +02005005 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005006 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005007 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005008 ret = -ENOMEM;
5009 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005010 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005011 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005012 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005013 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005014 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005015 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005016 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005017 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005018
David Sterbac8bf1b62019-05-17 11:43:17 +02005019 em_tree = &info->mapping_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005020 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005021 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005022 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005023 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005024 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005025 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005026 }
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005027 write_unlock(&em_tree->lock);
5028
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005029 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005030 if (ret)
5031 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005032
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02005033 for (i = 0; i < map->num_stripes; i++) {
5034 struct btrfs_device *dev = map->stripes[i].dev;
5035
5036 btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size);
5037 if (list_empty(&dev->post_commit_list))
5038 list_add_tail(&dev->post_commit_list,
5039 &trans->transaction->dev_update_list);
5040 }
Miao Xie43530c42014-09-03 21:35:36 +08005041
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005042 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005043
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005044 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 check_raid56_incompat_flag(info, type);
David Sterbacfbb8252018-07-10 18:15:05 +02005046 check_raid1c34_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005047
Miao Xieb2117a32011-01-05 10:07:28 +00005048 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005049 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005050
Josef Bacik6df9a952013-06-27 13:22:46 -04005051error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005052 write_lock(&em_tree->lock);
5053 remove_extent_mapping(em_tree, em);
5054 write_unlock(&em_tree->lock);
5055
5056 /* One for our allocation */
5057 free_extent_map(em);
5058 /* One for the tree reference */
5059 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005060error:
Miao Xieb2117a32011-01-05 10:07:28 +00005061 kfree(devices_info);
5062 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005063}
5064
Josef Bacik6df9a952013-06-27 13:22:46 -04005065int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005066 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005067{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005068 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005069 struct btrfs_root *extent_root = fs_info->extent_root;
5070 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005071 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005072 struct btrfs_device *device;
5073 struct btrfs_chunk *chunk;
5074 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005075 struct extent_map *em;
5076 struct map_lookup *map;
5077 size_t item_size;
5078 u64 dev_offset;
5079 u64 stripe_size;
5080 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005081 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005082
Omar Sandoval60ca8422018-05-16 16:34:31 -07005083 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005084 if (IS_ERR(em))
5085 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005086
Jeff Mahoney95617d62015-06-03 10:55:48 -04005087 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005088 item_size = btrfs_chunk_item_size(map->num_stripes);
5089 stripe_size = em->orig_block_len;
5090
5091 chunk = kzalloc(item_size, GFP_NOFS);
5092 if (!chunk) {
5093 ret = -ENOMEM;
5094 goto out;
5095 }
5096
Filipe Manana50460e32015-11-20 10:42:47 +00005097 /*
5098 * Take the device list mutex to prevent races with the final phase of
5099 * a device replace operation that replaces the device object associated
5100 * with the map's stripes, because the device object's id can change
5101 * at any time during that final phase of the device replace operation
5102 * (dev-replace.c:btrfs_dev_replace_finishing()).
5103 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005104 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005105 for (i = 0; i < map->num_stripes; i++) {
5106 device = map->stripes[i].dev;
5107 dev_offset = map->stripes[i].physical;
5108
Yan Zheng2b820322008-11-17 21:11:30 -05005109 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005110 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005111 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005112 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5113 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005114 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005115 break;
5116 }
5117 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005118 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005119 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005120 }
5121
Yan Zheng2b820322008-11-17 21:11:30 -05005122 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005123 for (i = 0; i < map->num_stripes; i++) {
5124 device = map->stripes[i].dev;
5125 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005126
5127 btrfs_set_stack_stripe_devid(stripe, device->devid);
5128 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5129 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5130 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005131 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005132 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005133
5134 btrfs_set_stack_chunk_length(chunk, chunk_size);
5135 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5136 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5137 btrfs_set_stack_chunk_type(chunk, map->type);
5138 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5139 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5140 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005141 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005142 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5143
5144 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5145 key.type = BTRFS_CHUNK_ITEM_KEY;
5146 key.offset = chunk_offset;
5147
5148 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005149 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5150 /*
5151 * TODO: Cleanup of inserted chunk root in case of
5152 * failure.
5153 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005154 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005155 }
liubo1abe9b82011-03-24 11:18:59 +00005156
Josef Bacik6df9a952013-06-27 13:22:46 -04005157out:
Yan Zheng2b820322008-11-17 21:11:30 -05005158 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005159 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005160 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005161}
5162
5163/*
Andrea Gelmini52042d82018-11-28 12:05:13 +01005164 * Chunk allocation falls into two parts. The first part does work
5165 * that makes the new allocated chunk usable, but does not do any operation
5166 * that modifies the chunk tree. The second part does the work that
5167 * requires modifying the chunk tree. This division is important for the
Yan Zheng2b820322008-11-17 21:11:30 -05005168 * bootstrap process of adding storage to a seed btrfs.
5169 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005170int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005171{
5172 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005173
Nikolay Borisovc216b202018-06-20 15:49:06 +03005174 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5175 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005176 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005177}
5178
David Sterba6f8e0fc2019-03-20 16:29:13 +01005179static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005180{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005181 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005182 u64 chunk_offset;
5183 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005184 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005185 int ret;
5186
Josef Bacik6df9a952013-06-27 13:22:46 -04005187 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005188 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005189 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005190 if (ret)
5191 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005192
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005193 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005194 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005195 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005196 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005197}
5198
Miao Xied20983b2014-07-03 18:22:13 +08005199static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5200{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005201 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005202
David Sterbafc9a2ac2019-05-17 11:43:22 +02005203 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005204}
5205
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005206int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005207{
5208 struct extent_map *em;
5209 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005210 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005211 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005212 int i;
5213
Omar Sandoval60ca8422018-05-16 16:34:31 -07005214 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005215 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005216 return 1;
5217
Jeff Mahoney95617d62015-06-03 10:55:48 -04005218 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005219 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005220 if (test_bit(BTRFS_DEV_STATE_MISSING,
5221 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005222 miss_ndevs++;
5223 continue;
5224 }
Anand Jainebbede42017-12-04 12:54:52 +08005225 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5226 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005227 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005228 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005229 }
5230 }
Miao Xied20983b2014-07-03 18:22:13 +08005231
5232 /*
5233 * If the number of missing devices is larger than max errors,
5234 * we can not write the data into that chunk successfully, so
5235 * set it readonly.
5236 */
5237 if (miss_ndevs > btrfs_chunk_max_errors(map))
5238 readonly = 1;
5239end:
Yan Zheng2b820322008-11-17 21:11:30 -05005240 free_extent_map(em);
5241 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005242}
5243
David Sterbac8bf1b62019-05-17 11:43:17 +02005244void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005245{
5246 struct extent_map *em;
5247
Chris Masond3977122009-01-05 21:25:51 -05005248 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005249 write_lock(&tree->lock);
5250 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005251 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005252 remove_extent_mapping(tree, em);
5253 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005254 if (!em)
5255 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005256 /* once for us */
5257 free_extent_map(em);
5258 /* once for the tree */
5259 free_extent_map(em);
5260 }
5261}
5262
Stefan Behrens5d964052012-11-05 14:59:07 +01005263int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005264{
5265 struct extent_map *em;
5266 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005267 int ret;
5268
Omar Sandoval60ca8422018-05-16 16:34:31 -07005269 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005270 if (IS_ERR(em))
5271 /*
5272 * We could return errors for these cases, but that could get
5273 * ugly and we'd probably do the same thing which is just not do
5274 * anything else and exit, so return 1 so the callers don't try
5275 * to use other copies.
5276 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005277 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005278
Jeff Mahoney95617d62015-06-03 10:55:48 -04005279 map = em->map_lookup;
David Sterbac7369b32019-05-31 15:39:31 +02005280 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
Chris Masonf1885912008-04-09 16:28:12 -04005281 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005282 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5283 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005284 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5285 ret = 2;
5286 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005287 /*
5288 * There could be two corrupted data stripes, we need
5289 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005290 *
Liu Bo8810f752018-01-02 13:36:41 -07005291 * Fail a stripe at a time on every retry except the
5292 * stripe under reconstruction.
5293 */
5294 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005295 else
5296 ret = 1;
5297 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005298
David Sterbacb5583d2018-09-07 16:11:23 +02005299 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005300 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5301 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005302 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005303 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005304
Chris Masonf1885912008-04-09 16:28:12 -04005305 return ret;
5306}
5307
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005308unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005309 u64 logical)
5310{
5311 struct extent_map *em;
5312 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005313 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005314
Omar Sandoval60ca8422018-05-16 16:34:31 -07005315 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005316
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005317 if (!WARN_ON(IS_ERR(em))) {
5318 map = em->map_lookup;
5319 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5320 len = map->stripe_len * nr_data_stripes(map);
5321 free_extent_map(em);
5322 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005323 return len;
5324}
5325
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005326int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005327{
5328 struct extent_map *em;
5329 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005330 int ret = 0;
5331
Omar Sandoval60ca8422018-05-16 16:34:31 -07005332 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005333
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005334 if(!WARN_ON(IS_ERR(em))) {
5335 map = em->map_lookup;
5336 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5337 ret = 1;
5338 free_extent_map(em);
5339 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005340 return ret;
5341}
5342
Stefan Behrens30d98612012-11-06 14:52:18 +01005343static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005344 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005345 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005346{
5347 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005348 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005349 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005350 int tolerance;
5351 struct btrfs_device *srcdev;
5352
Anand Jain99f92a72018-03-14 16:29:12 +08005353 ASSERT((map->type &
David Sterbac7369b32019-05-31 15:39:31 +02005354 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
Anand Jain99f92a72018-03-14 16:29:12 +08005355
5356 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5357 num_stripes = map->sub_stripes;
5358 else
5359 num_stripes = map->num_stripes;
5360
Anand Jain8ba0ae72018-03-14 16:29:13 +08005361 preferred_mirror = first + current->pid % num_stripes;
5362
Stefan Behrens30d98612012-11-06 14:52:18 +01005363 if (dev_replace_is_ongoing &&
5364 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5365 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5366 srcdev = fs_info->dev_replace.srcdev;
5367 else
5368 srcdev = NULL;
5369
5370 /*
5371 * try to avoid the drive that is the source drive for a
5372 * dev-replace procedure, only choose it if no other non-missing
5373 * mirror is available
5374 */
5375 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005376 if (map->stripes[preferred_mirror].dev->bdev &&
5377 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5378 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005379 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005380 if (map->stripes[i].dev->bdev &&
5381 (tolerance || map->stripes[i].dev != srcdev))
5382 return i;
5383 }
Chris Masondfe25022008-05-13 13:46:40 -04005384 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005385
Chris Masondfe25022008-05-13 13:46:40 -04005386 /* we couldn't find one that doesn't fail. Just return something
5387 * and the io error handling code will clean up eventually
5388 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005389 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005390}
5391
David Woodhouse53b381b2013-01-29 18:40:14 -05005392static inline int parity_smaller(u64 a, u64 b)
5393{
5394 return a > b;
5395}
5396
5397/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005398static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005399{
5400 struct btrfs_bio_stripe s;
5401 int i;
5402 u64 l;
5403 int again = 1;
5404
5405 while (again) {
5406 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005407 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005408 if (parity_smaller(bbio->raid_map[i],
5409 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005410 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005411 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005412 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005413 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005414 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005415 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005416
David Woodhouse53b381b2013-01-29 18:40:14 -05005417 again = 1;
5418 }
5419 }
5420 }
5421}
5422
Zhao Lei6e9606d2015-01-20 15:11:34 +08005423static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5424{
5425 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005426 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005427 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005428 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005429 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005430 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005431 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005432 /*
5433 * plus the raid_map, which includes both the tgt dev
5434 * and the stripes
5435 */
5436 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005437 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005438
5439 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005440 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005441
5442 return bbio;
5443}
5444
5445void btrfs_get_bbio(struct btrfs_bio *bbio)
5446{
Elena Reshetova140475a2017-03-03 10:55:10 +02005447 WARN_ON(!refcount_read(&bbio->refs));
5448 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005449}
5450
5451void btrfs_put_bbio(struct btrfs_bio *bbio)
5452{
5453 if (!bbio)
5454 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005455 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005456 kfree(bbio);
5457}
5458
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005459/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5460/*
5461 * Please note that, discard won't be sent to target device of device
5462 * replace.
5463 */
5464static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005465 u64 logical, u64 *length_ret,
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005466 struct btrfs_bio **bbio_ret)
5467{
5468 struct extent_map *em;
5469 struct map_lookup *map;
5470 struct btrfs_bio *bbio;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005471 u64 length = *length_ret;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005472 u64 offset;
5473 u64 stripe_nr;
5474 u64 stripe_nr_end;
5475 u64 stripe_end_offset;
5476 u64 stripe_cnt;
5477 u64 stripe_len;
5478 u64 stripe_offset;
5479 u64 num_stripes;
5480 u32 stripe_index;
5481 u32 factor = 0;
5482 u32 sub_stripes = 0;
5483 u64 stripes_per_dev = 0;
5484 u32 remaining_stripes = 0;
5485 u32 last_stripe = 0;
5486 int ret = 0;
5487 int i;
5488
5489 /* discard always return a bbio */
5490 ASSERT(bbio_ret);
5491
Omar Sandoval60ca8422018-05-16 16:34:31 -07005492 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005493 if (IS_ERR(em))
5494 return PTR_ERR(em);
5495
5496 map = em->map_lookup;
5497 /* we don't discard raid56 yet */
5498 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5499 ret = -EOPNOTSUPP;
5500 goto out;
5501 }
5502
5503 offset = logical - em->start;
Qu Wenruo2d974612019-10-23 21:57:26 +08005504 length = min_t(u64, em->start + em->len - logical, length);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005505 *length_ret = length;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005506
5507 stripe_len = map->stripe_len;
5508 /*
5509 * stripe_nr counts the total number of stripes we have to stride
5510 * to get to this block
5511 */
5512 stripe_nr = div64_u64(offset, stripe_len);
5513
5514 /* stripe_offset is the offset of this block in its stripe */
5515 stripe_offset = offset - stripe_nr * stripe_len;
5516
5517 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005518 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005519 stripe_cnt = stripe_nr_end - stripe_nr;
5520 stripe_end_offset = stripe_nr_end * map->stripe_len -
5521 (offset + length);
5522 /*
5523 * after this, stripe_nr is the number of stripes on this
5524 * device we have to walk to find the data, and stripe_index is
5525 * the number of our device in the stripe array
5526 */
5527 num_stripes = 1;
5528 stripe_index = 0;
5529 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5530 BTRFS_BLOCK_GROUP_RAID10)) {
5531 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5532 sub_stripes = 1;
5533 else
5534 sub_stripes = map->sub_stripes;
5535
5536 factor = map->num_stripes / sub_stripes;
5537 num_stripes = min_t(u64, map->num_stripes,
5538 sub_stripes * stripe_cnt);
5539 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5540 stripe_index *= sub_stripes;
5541 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5542 &remaining_stripes);
5543 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5544 last_stripe *= sub_stripes;
David Sterbac7369b32019-05-31 15:39:31 +02005545 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005546 BTRFS_BLOCK_GROUP_DUP)) {
5547 num_stripes = map->num_stripes;
5548 } else {
5549 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5550 &stripe_index);
5551 }
5552
5553 bbio = alloc_btrfs_bio(num_stripes, 0);
5554 if (!bbio) {
5555 ret = -ENOMEM;
5556 goto out;
5557 }
5558
5559 for (i = 0; i < num_stripes; i++) {
5560 bbio->stripes[i].physical =
5561 map->stripes[stripe_index].physical +
5562 stripe_offset + stripe_nr * map->stripe_len;
5563 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5564
5565 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5566 BTRFS_BLOCK_GROUP_RAID10)) {
5567 bbio->stripes[i].length = stripes_per_dev *
5568 map->stripe_len;
5569
5570 if (i / sub_stripes < remaining_stripes)
5571 bbio->stripes[i].length +=
5572 map->stripe_len;
5573
5574 /*
5575 * Special for the first stripe and
5576 * the last stripe:
5577 *
5578 * |-------|...|-------|
5579 * |----------|
5580 * off end_off
5581 */
5582 if (i < sub_stripes)
5583 bbio->stripes[i].length -=
5584 stripe_offset;
5585
5586 if (stripe_index >= last_stripe &&
5587 stripe_index <= (last_stripe +
5588 sub_stripes - 1))
5589 bbio->stripes[i].length -=
5590 stripe_end_offset;
5591
5592 if (i == sub_stripes - 1)
5593 stripe_offset = 0;
5594 } else {
5595 bbio->stripes[i].length = length;
5596 }
5597
5598 stripe_index++;
5599 if (stripe_index == map->num_stripes) {
5600 stripe_index = 0;
5601 stripe_nr++;
5602 }
5603 }
5604
5605 *bbio_ret = bbio;
5606 bbio->map_type = map->type;
5607 bbio->num_stripes = num_stripes;
5608out:
5609 free_extent_map(em);
5610 return ret;
5611}
5612
Liu Bo5ab56092017-03-14 13:33:57 -07005613/*
5614 * In dev-replace case, for repair case (that's the only case where the mirror
5615 * is selected explicitly when calling btrfs_map_block), blocks left of the
5616 * left cursor can also be read from the target drive.
5617 *
5618 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5619 * array of stripes.
5620 * For READ, it also needs to be supported using the same mirror number.
5621 *
5622 * If the requested block is not left of the left cursor, EIO is returned. This
5623 * can happen because btrfs_num_copies() returns one more in the dev-replace
5624 * case.
5625 */
5626static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5627 u64 logical, u64 length,
5628 u64 srcdev_devid, int *mirror_num,
5629 u64 *physical)
5630{
5631 struct btrfs_bio *bbio = NULL;
5632 int num_stripes;
5633 int index_srcdev = 0;
5634 int found = 0;
5635 u64 physical_of_found = 0;
5636 int i;
5637 int ret = 0;
5638
5639 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5640 logical, &length, &bbio, 0, 0);
5641 if (ret) {
5642 ASSERT(bbio == NULL);
5643 return ret;
5644 }
5645
5646 num_stripes = bbio->num_stripes;
5647 if (*mirror_num > num_stripes) {
5648 /*
5649 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5650 * that means that the requested area is not left of the left
5651 * cursor
5652 */
5653 btrfs_put_bbio(bbio);
5654 return -EIO;
5655 }
5656
5657 /*
5658 * process the rest of the function using the mirror_num of the source
5659 * drive. Therefore look it up first. At the end, patch the device
5660 * pointer to the one of the target drive.
5661 */
5662 for (i = 0; i < num_stripes; i++) {
5663 if (bbio->stripes[i].dev->devid != srcdev_devid)
5664 continue;
5665
5666 /*
5667 * In case of DUP, in order to keep it simple, only add the
5668 * mirror with the lowest physical address
5669 */
5670 if (found &&
5671 physical_of_found <= bbio->stripes[i].physical)
5672 continue;
5673
5674 index_srcdev = i;
5675 found = 1;
5676 physical_of_found = bbio->stripes[i].physical;
5677 }
5678
5679 btrfs_put_bbio(bbio);
5680
5681 ASSERT(found);
5682 if (!found)
5683 return -EIO;
5684
5685 *mirror_num = index_srcdev + 1;
5686 *physical = physical_of_found;
5687 return ret;
5688}
5689
Liu Bo73c0f222017-03-14 13:33:58 -07005690static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5691 struct btrfs_bio **bbio_ret,
5692 struct btrfs_dev_replace *dev_replace,
5693 int *num_stripes_ret, int *max_errors_ret)
5694{
5695 struct btrfs_bio *bbio = *bbio_ret;
5696 u64 srcdev_devid = dev_replace->srcdev->devid;
5697 int tgtdev_indexes = 0;
5698 int num_stripes = *num_stripes_ret;
5699 int max_errors = *max_errors_ret;
5700 int i;
5701
5702 if (op == BTRFS_MAP_WRITE) {
5703 int index_where_to_add;
5704
5705 /*
5706 * duplicate the write operations while the dev replace
5707 * procedure is running. Since the copying of the old disk to
5708 * the new disk takes place at run time while the filesystem is
5709 * mounted writable, the regular write operations to the old
5710 * disk have to be duplicated to go to the new disk as well.
5711 *
5712 * Note that device->missing is handled by the caller, and that
5713 * the write to the old disk is already set up in the stripes
5714 * array.
5715 */
5716 index_where_to_add = num_stripes;
5717 for (i = 0; i < num_stripes; i++) {
5718 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5719 /* write to new disk, too */
5720 struct btrfs_bio_stripe *new =
5721 bbio->stripes + index_where_to_add;
5722 struct btrfs_bio_stripe *old =
5723 bbio->stripes + i;
5724
5725 new->physical = old->physical;
5726 new->length = old->length;
5727 new->dev = dev_replace->tgtdev;
5728 bbio->tgtdev_map[i] = index_where_to_add;
5729 index_where_to_add++;
5730 max_errors++;
5731 tgtdev_indexes++;
5732 }
5733 }
5734 num_stripes = index_where_to_add;
5735 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5736 int index_srcdev = 0;
5737 int found = 0;
5738 u64 physical_of_found = 0;
5739
5740 /*
5741 * During the dev-replace procedure, the target drive can also
5742 * be used to read data in case it is needed to repair a corrupt
5743 * block elsewhere. This is possible if the requested area is
5744 * left of the left cursor. In this area, the target drive is a
5745 * full copy of the source drive.
5746 */
5747 for (i = 0; i < num_stripes; i++) {
5748 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5749 /*
5750 * In case of DUP, in order to keep it simple,
5751 * only add the mirror with the lowest physical
5752 * address
5753 */
5754 if (found &&
5755 physical_of_found <=
5756 bbio->stripes[i].physical)
5757 continue;
5758 index_srcdev = i;
5759 found = 1;
5760 physical_of_found = bbio->stripes[i].physical;
5761 }
5762 }
5763 if (found) {
5764 struct btrfs_bio_stripe *tgtdev_stripe =
5765 bbio->stripes + num_stripes;
5766
5767 tgtdev_stripe->physical = physical_of_found;
5768 tgtdev_stripe->length =
5769 bbio->stripes[index_srcdev].length;
5770 tgtdev_stripe->dev = dev_replace->tgtdev;
5771 bbio->tgtdev_map[index_srcdev] = num_stripes;
5772
5773 tgtdev_indexes++;
5774 num_stripes++;
5775 }
5776 }
5777
5778 *num_stripes_ret = num_stripes;
5779 *max_errors_ret = max_errors;
5780 bbio->num_tgtdevs = tgtdev_indexes;
5781 *bbio_ret = bbio;
5782}
5783
Liu Bo2b19a1f2017-03-14 13:34:00 -07005784static bool need_full_stripe(enum btrfs_map_op op)
5785{
5786 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5787}
5788
Nikolay Borisov5f141122019-06-03 12:05:03 +03005789/*
5790 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5791 * tuple. This information is used to calculate how big a
5792 * particular bio can get before it straddles a stripe.
5793 *
5794 * @fs_info - the filesystem
5795 * @logical - address that we want to figure out the geometry of
5796 * @len - the length of IO we are going to perform, starting at @logical
5797 * @op - type of operation - write or read
5798 * @io_geom - pointer used to return values
5799 *
5800 * Returns < 0 in case a chunk for the given logical address cannot be found,
5801 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5802 */
5803int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005804 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
Nikolay Borisov5f141122019-06-03 12:05:03 +03005805{
5806 struct extent_map *em;
5807 struct map_lookup *map;
5808 u64 offset;
5809 u64 stripe_offset;
5810 u64 stripe_nr;
5811 u64 stripe_len;
5812 u64 raid56_full_stripe_start = (u64)-1;
5813 int data_stripes;
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005814 int ret = 0;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005815
5816 ASSERT(op != BTRFS_MAP_DISCARD);
5817
5818 em = btrfs_get_chunk_map(fs_info, logical, len);
5819 if (IS_ERR(em))
5820 return PTR_ERR(em);
5821
5822 map = em->map_lookup;
5823 /* Offset of this logical address in the chunk */
5824 offset = logical - em->start;
5825 /* Len of a stripe in a chunk */
5826 stripe_len = map->stripe_len;
5827 /* Stripe wher this block falls in */
5828 stripe_nr = div64_u64(offset, stripe_len);
5829 /* Offset of stripe in the chunk */
5830 stripe_offset = stripe_nr * stripe_len;
5831 if (offset < stripe_offset) {
5832 btrfs_crit(fs_info,
5833"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5834 stripe_offset, offset, em->start, logical, stripe_len);
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005835 ret = -EINVAL;
5836 goto out;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005837 }
5838
5839 /* stripe_offset is the offset of this block in its stripe */
5840 stripe_offset = offset - stripe_offset;
5841 data_stripes = nr_data_stripes(map);
5842
5843 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5844 u64 max_len = stripe_len - stripe_offset;
5845
5846 /*
5847 * In case of raid56, we need to know the stripe aligned start
5848 */
5849 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5850 unsigned long full_stripe_len = stripe_len * data_stripes;
5851 raid56_full_stripe_start = offset;
5852
5853 /*
5854 * Allow a write of a full stripe, but make sure we
5855 * don't allow straddling of stripes
5856 */
5857 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5858 full_stripe_len);
5859 raid56_full_stripe_start *= full_stripe_len;
5860
5861 /*
5862 * For writes to RAID[56], allow a full stripeset across
5863 * all disks. For other RAID types and for RAID[56]
5864 * reads, just allow a single stripe (on a single disk).
5865 */
5866 if (op == BTRFS_MAP_WRITE) {
5867 max_len = stripe_len * data_stripes -
5868 (offset - raid56_full_stripe_start);
5869 }
5870 }
5871 len = min_t(u64, em->len - offset, max_len);
5872 } else {
5873 len = em->len - offset;
5874 }
5875
5876 io_geom->len = len;
5877 io_geom->offset = offset;
5878 io_geom->stripe_len = stripe_len;
5879 io_geom->stripe_nr = stripe_nr;
5880 io_geom->stripe_offset = stripe_offset;
5881 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5882
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005883out:
5884 /* once for us */
5885 free_extent_map(em);
5886 return ret;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005887}
5888
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005889static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5890 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005891 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005892 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005893 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005894{
5895 struct extent_map *em;
5896 struct map_lookup *map;
Chris Mason593060d2008-03-25 16:50:33 -04005897 u64 stripe_offset;
5898 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005899 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005900 u32 stripe_index;
David Sterbacff82672019-05-17 11:43:45 +02005901 int data_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04005902 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005903 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005904 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005905 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005906 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005907 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005908 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5909 int dev_replace_is_ongoing = 0;
5910 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005911 int patch_the_first_stripe_for_dev_replace = 0;
5912 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005913 u64 raid56_full_stripe_start = (u64)-1;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005914 struct btrfs_io_geometry geom;
5915
5916 ASSERT(bbio_ret);
Chris Mason0b86a832008-03-24 15:01:56 -04005917
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005918 if (op == BTRFS_MAP_DISCARD)
5919 return __btrfs_map_block_for_discard(fs_info, logical,
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005920 length, bbio_ret);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005921
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005922 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
5923 if (ret < 0)
5924 return ret;
5925
Omar Sandoval60ca8422018-05-16 16:34:31 -07005926 em = btrfs_get_chunk_map(fs_info, logical, *length);
Dan Carpenterf1136982019-08-09 17:07:39 +03005927 ASSERT(!IS_ERR(em));
Jeff Mahoney95617d62015-06-03 10:55:48 -04005928 map = em->map_lookup;
Chris Mason593060d2008-03-25 16:50:33 -04005929
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005930 *length = geom.len;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005931 stripe_len = geom.stripe_len;
5932 stripe_nr = geom.stripe_nr;
5933 stripe_offset = geom.stripe_offset;
5934 raid56_full_stripe_start = geom.raid56_stripe_offset;
David Sterbacff82672019-05-17 11:43:45 +02005935 data_stripes = nr_data_stripes(map);
Chris Mason593060d2008-03-25 16:50:33 -04005936
David Sterbacb5583d2018-09-07 16:11:23 +02005937 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01005938 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02005939 /*
5940 * Hold the semaphore for read during the whole operation, write is
5941 * requested at commit time but must wait.
5942 */
Stefan Behrens472262f2012-11-06 14:43:46 +01005943 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02005944 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01005945
Stefan Behrensad6d6202012-11-06 15:06:47 +01005946 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005947 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005948 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5949 dev_replace->srcdev->devid,
5950 &mirror_num,
5951 &physical_to_patch_in_first_stripe);
5952 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005953 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005954 else
5955 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005956 } else if (mirror_num > map->num_stripes) {
5957 mirror_num = 0;
5958 }
5959
Chris Masonf2d8d742008-04-21 10:03:05 -04005960 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005961 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005962 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005963 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5964 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005965 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005966 mirror_num = 1;
David Sterbac7369b32019-05-31 15:39:31 +02005967 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005968 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005969 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005970 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005971 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005972 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005973 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005974 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005975 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005976 }
Chris Mason2fff7342008-04-29 14:12:09 -04005977
Chris Mason611f0e02008-04-03 16:29:03 -04005978 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005979 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005980 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005981 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005982 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005983 } else {
5984 mirror_num = 1;
5985 }
Chris Mason2fff7342008-04-29 14:12:09 -04005986
Chris Mason321aecc2008-04-16 10:49:51 -04005987 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005988 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005989
David Sterba47c57132015-02-20 18:43:47 +01005990 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005991 stripe_index *= map->sub_stripes;
5992
Anand Jainde483732017-10-12 16:43:00 +08005993 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005994 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005995 else if (mirror_num)
5996 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005997 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005998 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005999 stripe_index = find_live_mirror(fs_info, map,
6000 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006001 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006002 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006003 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006004
Zhao Leiffe2d202015-01-20 15:11:44 +08006005 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006006 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006007 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006008 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbacff82672019-05-17 11:43:45 +02006009 stripe_len * data_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05006010
6011 /* RAID[56] write or recovery. Return all stripes */
6012 num_stripes = map->num_stripes;
6013 max_errors = nr_parity_stripes(map);
6014
David Woodhouse53b381b2013-01-29 18:40:14 -05006015 *length = map->stripe_len;
6016 stripe_index = 0;
6017 stripe_offset = 0;
6018 } else {
6019 /*
6020 * Mirror #0 or #1 means the original data block.
6021 * Mirror #2 is RAID5 parity block.
6022 * Mirror #3 is RAID6 Q block.
6023 */
David Sterba47c57132015-02-20 18:43:47 +01006024 stripe_nr = div_u64_rem(stripe_nr,
David Sterbacff82672019-05-17 11:43:45 +02006025 data_stripes, &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006026 if (mirror_num > 1)
David Sterbacff82672019-05-17 11:43:45 +02006027 stripe_index = data_stripes + mirror_num - 2;
David Woodhouse53b381b2013-01-29 18:40:14 -05006028
6029 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006030 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6031 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006032 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006033 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006034 }
Chris Mason8790d502008-04-03 16:29:03 -04006035 } else {
6036 /*
David Sterba47c57132015-02-20 18:43:47 +01006037 * after this, stripe_nr is the number of stripes on this
6038 * device we have to walk to find the data, and stripe_index is
6039 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006040 */
David Sterba47c57132015-02-20 18:43:47 +01006041 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6042 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006043 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006044 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006045 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006046 btrfs_crit(fs_info,
6047 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006048 stripe_index, map->num_stripes);
6049 ret = -EINVAL;
6050 goto out;
6051 }
Chris Mason593060d2008-03-25 16:50:33 -04006052
Stefan Behrens472262f2012-11-06 14:43:46 +01006053 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006054 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006055 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006056 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006057 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006058 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006059 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006060 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006061
Zhao Lei6e9606d2015-01-20 15:11:34 +08006062 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006063 if (!bbio) {
6064 ret = -ENOMEM;
6065 goto out;
6066 }
Liu Bo6fad8232017-03-14 13:33:59 -07006067 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006068 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006069
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006070 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006071 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6072 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006073 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006074 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006075
6076 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6077 sizeof(struct btrfs_bio_stripe) *
6078 num_alloc_stripes +
6079 sizeof(int) * tgtdev_indexes);
6080
6081 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006082 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006083
6084 /* Fill in the logical address of each stripe */
David Sterbacff82672019-05-17 11:43:45 +02006085 tmp = stripe_nr * data_stripes;
6086 for (i = 0; i < data_stripes; i++)
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006087 bbio->raid_map[(i+rot) % num_stripes] =
6088 em->start + (tmp + i) * map->stripe_len;
6089
6090 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6091 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6092 bbio->raid_map[(i+rot+1) % num_stripes] =
6093 RAID6_Q_STRIPE;
6094 }
6095
Li Zefanec9ef7a2011-12-01 14:06:42 +08006096
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006097 for (i = 0; i < num_stripes; i++) {
6098 bbio->stripes[i].physical =
6099 map->stripes[stripe_index].physical +
6100 stripe_offset +
6101 stripe_nr * map->stripe_len;
6102 bbio->stripes[i].dev =
6103 map->stripes[stripe_index].dev;
6104 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006105 }
Li Zefande11cc12011-12-01 12:55:47 +08006106
Liu Bo2b19a1f2017-03-14 13:34:00 -07006107 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006108 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006109
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006110 if (bbio->raid_map)
6111 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006112
Liu Bo73c0f222017-03-14 13:33:58 -07006113 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006114 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006115 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6116 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006117 }
6118
Li Zefande11cc12011-12-01 12:55:47 +08006119 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006120 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006121 bbio->num_stripes = num_stripes;
6122 bbio->max_errors = max_errors;
6123 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006124
6125 /*
6126 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6127 * mirror_num == num_stripes + 1 && dev_replace target drive is
6128 * available as a mirror
6129 */
6130 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6131 WARN_ON(num_stripes > 1);
6132 bbio->stripes[0].dev = dev_replace->tgtdev;
6133 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6134 bbio->mirror_num = map->num_stripes + 1;
6135 }
Chris Masoncea9e442008-04-09 16:28:12 -04006136out:
Liu Bo73beece2015-07-17 16:49:19 +08006137 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006138 lockdep_assert_held(&dev_replace->rwsem);
6139 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006140 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006141 }
Chris Mason0b86a832008-03-24 15:01:56 -04006142 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006143 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006144}
6145
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006146int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006147 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006148 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006149{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006150 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006151 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006152}
6153
Miao Xieaf8e2d12014-10-23 14:42:50 +08006154/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006155int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006156 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006157 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006158{
David Sterba825ad4c2017-03-28 14:45:22 +02006159 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006160}
6161
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006162static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006163{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006164 bio->bi_private = bbio->private;
6165 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006166 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006167
Zhao Lei6e9606d2015-01-20 15:11:34 +08006168 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006169}
6170
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006171static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006172{
Chris Mason9be33952013-05-17 18:30:14 -04006173 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006174 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006175
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006176 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006177 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006178 if (bio->bi_status == BLK_STS_IOERR ||
6179 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006180 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006181 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006182 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006183
6184 BUG_ON(stripe_index >= bbio->num_stripes);
6185 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006186 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006187 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006188 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006189 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterba0cc068e2019-03-07 15:40:50 +01006190 else if (!(bio->bi_opf & REQ_RAHEAD))
Anand Jain1cb34c82017-10-21 01:45:33 +08006191 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006192 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006193 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006194 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006195 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006196 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006197 }
6198 }
Chris Mason8790d502008-04-03 16:29:03 -04006199
Jan Schmidta1d3c472011-08-04 17:15:33 +02006200 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006201 is_orig_bio = 1;
6202
Miao Xiec404e0d2014-01-30 16:46:55 +08006203 btrfs_bio_counter_dec(bbio->fs_info);
6204
Jan Schmidta1d3c472011-08-04 17:15:33 +02006205 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006206 if (!is_orig_bio) {
6207 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006208 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006209 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006210
Chris Mason9be33952013-05-17 18:30:14 -04006211 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006212 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006213 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006214 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006215 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006216 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006217 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006218 /*
6219 * this bio is actually up to date, we didn't
6220 * go over the max number of errors
6221 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006222 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006223 }
Miao Xiec55f1392014-06-19 10:42:54 +08006224
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006225 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006226 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006227 bio_put(bio);
6228 }
Chris Mason8790d502008-04-03 16:29:03 -04006229}
6230
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006231static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
Chris Mason08635ba2019-07-10 12:28:14 -07006232 u64 physical, int dev_nr)
Josef Bacikde1ee922012-10-19 16:50:56 -04006233{
6234 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006235 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006236
6237 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006238 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006239 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006240 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006241 btrfs_debug_in_rcu(fs_info,
6242 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6243 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6244 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6245 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006246 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006247
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006248 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006249
Chris Mason08635ba2019-07-10 12:28:14 -07006250 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006251}
6252
Josef Bacikde1ee922012-10-19 16:50:56 -04006253static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6254{
6255 atomic_inc(&bbio->error);
6256 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006257 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006258 WARN_ON(bio != bbio->orig_bio);
6259
Chris Mason9be33952013-05-17 18:30:14 -04006260 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006261 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006262 if (atomic_read(&bbio->error) > bbio->max_errors)
6263 bio->bi_status = BLK_STS_IOERR;
6264 else
6265 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006266 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006267 }
6268}
6269
Omar Sandoval58efbc92017-08-22 23:45:59 -07006270blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Chris Mason08635ba2019-07-10 12:28:14 -07006271 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04006272{
Chris Mason0b86a832008-03-24 15:01:56 -04006273 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006274 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006275 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006276 u64 length = 0;
6277 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006278 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006279 int dev_nr;
6280 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006281 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006282
Kent Overstreet4f024f32013-10-11 15:44:27 -07006283 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006284 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006285
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006286 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006287 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006288 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006289 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006290 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006291 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006292 }
Chris Masoncea9e442008-04-09 16:28:12 -04006293
Jan Schmidta1d3c472011-08-04 17:15:33 +02006294 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006295 bbio->orig_bio = first_bio;
6296 bbio->private = first_bio->bi_private;
6297 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006298 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006299 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6300
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006301 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006302 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006303 /* In this case, map_length has been set to the length of
6304 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006305 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006306 ret = raid56_parity_write(fs_info, bio, bbio,
6307 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006308 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006309 ret = raid56_parity_recover(fs_info, bio, bbio,
6310 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006311 }
Miao Xie42452152014-11-25 16:39:28 +08006312
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006313 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006314 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006315 }
6316
Chris Masoncea9e442008-04-09 16:28:12 -04006317 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006318 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006319 "mapping failed logical %llu bio len %llu len %llu",
6320 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006321 BUG();
6322 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006323
Zhao Lei08da7572015-02-12 15:42:16 +08006324 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006325 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006326 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6327 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006328 (bio_op(first_bio) == REQ_OP_WRITE &&
6329 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006330 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006331 continue;
6332 }
6333
David Sterba3aa8e072017-06-02 17:38:30 +02006334 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006335 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006336 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006337 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006338
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006339 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
Chris Mason08635ba2019-07-10 12:28:14 -07006340 dev_nr);
Chris Mason239b14b2008-03-24 15:02:07 -04006341 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006342 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006343 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006344}
6345
Anand Jain09ba3bc2019-01-19 14:48:55 +08006346/*
6347 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6348 * return NULL.
6349 *
6350 * If devid and uuid are both specified, the match must be exact, otherwise
6351 * only devid is used.
6352 *
6353 * If @seed is true, traverse through the seed devices.
6354 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006355struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006356 u64 devid, u8 *uuid, u8 *fsid,
6357 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006358{
Yan Zheng2b820322008-11-17 21:11:30 -05006359 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006360
Anand Jaine4319cd2019-01-17 23:32:31 +08006361 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006362 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006363 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08006364 list_for_each_entry(device, &fs_devices->devices,
6365 dev_list) {
6366 if (device->devid == devid &&
6367 (!uuid || memcmp(device->uuid, uuid,
6368 BTRFS_UUID_SIZE) == 0))
6369 return device;
6370 }
Yan Zheng2b820322008-11-17 21:11:30 -05006371 }
Anand Jain09ba3bc2019-01-19 14:48:55 +08006372 if (seed)
6373 fs_devices = fs_devices->seed;
6374 else
6375 return NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05006376 }
6377 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006378}
6379
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006380static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006381 u64 devid, u8 *dev_uuid)
6382{
6383 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006384
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006385 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6386 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006387 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006388
6389 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006390 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006391 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006392
Anand Jaine6e674b2017-12-04 12:54:54 +08006393 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006394 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006395
Chris Masondfe25022008-05-13 13:46:40 -04006396 return device;
6397}
6398
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006399/**
6400 * btrfs_alloc_device - allocate struct btrfs_device
6401 * @fs_info: used only for generating a new devid, can be NULL if
6402 * devid is provided (i.e. @devid != NULL).
6403 * @devid: a pointer to devid for this device. If NULL a new devid
6404 * is generated.
6405 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6406 * is generated.
6407 *
6408 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006409 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006410 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006411 */
6412struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6413 const u64 *devid,
6414 const u8 *uuid)
6415{
6416 struct btrfs_device *dev;
6417 u64 tmp;
6418
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306419 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006420 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006421
6422 dev = __alloc_device();
6423 if (IS_ERR(dev))
6424 return dev;
6425
6426 if (devid)
6427 tmp = *devid;
6428 else {
6429 int ret;
6430
6431 ret = find_next_devid(fs_info, &tmp);
6432 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006433 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006434 return ERR_PTR(ret);
6435 }
6436 }
6437 dev->devid = tmp;
6438
6439 if (uuid)
6440 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6441 else
6442 generate_random_uuid(dev->uuid);
6443
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006444 return dev;
6445}
6446
Anand Jain5a2b8e62017-10-09 11:07:45 +08006447static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006448 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006449{
Anand Jain2b902df2017-10-09 11:07:46 +08006450 if (error)
6451 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6452 devid, uuid);
6453 else
6454 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6455 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006456}
6457
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006458static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6459{
6460 int index = btrfs_bg_flags_to_raid_index(type);
6461 int ncopies = btrfs_raid_array[index].ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006462 const int nparity = btrfs_raid_array[index].nparity;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006463 int data_stripes;
6464
David Sterbae4f6c6b2019-05-14 01:59:54 +02006465 if (nparity)
6466 data_stripes = num_stripes - nparity;
6467 else
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006468 data_stripes = num_stripes / ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006469
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006470 return div_u64(chunk_len, data_stripes);
6471}
6472
David Sterba9690ac02019-03-20 16:43:07 +01006473static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006474 struct btrfs_chunk *chunk)
6475{
David Sterba9690ac02019-03-20 16:43:07 +01006476 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006477 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006478 struct map_lookup *map;
6479 struct extent_map *em;
6480 u64 logical;
6481 u64 length;
6482 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006483 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006484 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006485 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006486 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006487
Chris Masone17cade2008-04-15 15:41:47 -04006488 logical = key->offset;
6489 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006490 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006491
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006492 /*
6493 * Only need to verify chunk item if we're reading from sys chunk array,
6494 * as chunk item in tree block is already verified by tree-checker.
6495 */
6496 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006497 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006498 if (ret)
6499 return ret;
6500 }
Chris Masona061fc82008-05-07 11:43:44 -04006501
David Sterbac8bf1b62019-05-17 11:43:17 +02006502 read_lock(&map_tree->lock);
6503 em = lookup_extent_mapping(map_tree, logical, 1);
6504 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006505
6506 /* already mapped? */
6507 if (em && em->start <= logical && em->start + em->len > logical) {
6508 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006509 return 0;
6510 } else if (em) {
6511 free_extent_map(em);
6512 }
Chris Mason0b86a832008-03-24 15:01:56 -04006513
David Sterba172ddd62011-04-21 00:48:27 +02006514 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006515 if (!em)
6516 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006517 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006518 if (!map) {
6519 free_extent_map(em);
6520 return -ENOMEM;
6521 }
6522
Wang Shilong298a8f92014-06-19 10:42:52 +08006523 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006524 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006525 em->start = logical;
6526 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006527 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006528 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006529 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006530
Chris Mason593060d2008-03-25 16:50:33 -04006531 map->num_stripes = num_stripes;
6532 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6533 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006534 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6535 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006536 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006537 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006538 em->orig_block_len = calc_stripe_length(map->type, em->len,
6539 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006540 for (i = 0; i < num_stripes; i++) {
6541 map->stripes[i].physical =
6542 btrfs_stripe_offset_nr(leaf, chunk, i);
6543 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006544 read_extent_buffer(leaf, uuid, (unsigned long)
6545 btrfs_stripe_dev_uuid_nr(chunk, i),
6546 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006547 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006548 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006549 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006550 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006551 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006552 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006553 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006554 }
Chris Masondfe25022008-05-13 13:46:40 -04006555 if (!map->stripes[i].dev) {
6556 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006557 add_missing_dev(fs_info->fs_devices, devid,
6558 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006559 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006560 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006561 btrfs_err(fs_info,
6562 "failed to init missing dev %llu: %ld",
6563 devid, PTR_ERR(map->stripes[i].dev));
6564 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006565 }
Anand Jain2b902df2017-10-09 11:07:46 +08006566 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006567 }
Anand Jaine12c9622017-12-04 12:54:53 +08006568 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6569 &(map->stripes[i].dev->dev_state));
6570
Chris Mason0b86a832008-03-24 15:01:56 -04006571 }
6572
David Sterbac8bf1b62019-05-17 11:43:17 +02006573 write_lock(&map_tree->lock);
6574 ret = add_extent_mapping(map_tree, em, 0);
6575 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006576 if (ret < 0) {
6577 btrfs_err(fs_info,
6578 "failed to add chunk map, start=%llu len=%llu: %d",
6579 em->start, em->len, ret);
6580 }
Chris Mason0b86a832008-03-24 15:01:56 -04006581 free_extent_map(em);
6582
Qu Wenruo64f64f42018-08-01 10:37:20 +08006583 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006584}
6585
Jeff Mahoney143bede2012-03-01 14:56:26 +01006586static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006587 struct btrfs_dev_item *dev_item,
6588 struct btrfs_device *device)
6589{
6590 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006591
6592 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006593 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6594 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006595 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006596 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006597 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006598 device->type = btrfs_device_type(leaf, dev_item);
6599 device->io_align = btrfs_device_io_align(leaf, dev_item);
6600 device->io_width = btrfs_device_io_width(leaf, dev_item);
6601 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006602 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006603 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006604
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006605 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006606 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006607}
6608
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006609static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006610 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006611{
6612 struct btrfs_fs_devices *fs_devices;
6613 int ret;
6614
David Sterbaa32bf9a2018-03-16 02:21:22 +01006615 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006616 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006617
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006618 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006619 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006620 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006621 return fs_devices;
6622
Yan Zheng2b820322008-11-17 21:11:30 -05006623 fs_devices = fs_devices->seed;
6624 }
6625
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006626 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006627 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006628 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006629 return ERR_PTR(-ENOENT);
6630
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006631 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006632 if (IS_ERR(fs_devices))
6633 return fs_devices;
6634
Johannes Thumshirn0395d842019-11-13 11:27:27 +01006635 fs_devices->seeding = true;
Miao Xie5f375832014-09-03 21:35:46 +08006636 fs_devices->opened = 1;
6637 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006638 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006639
6640 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006641 if (IS_ERR(fs_devices))
6642 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006643
Anand Jain897fb572018-04-12 10:29:28 +08006644 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006645 if (ret) {
6646 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006647 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006648 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006649 }
Yan Zheng2b820322008-11-17 21:11:30 -05006650
6651 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006652 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006653 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006654 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006655 goto out;
6656 }
6657
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006658 fs_devices->seed = fs_info->fs_devices->seed;
6659 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006660out:
Miao Xie5f375832014-09-03 21:35:46 +08006661 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006662}
6663
David Sterba17850752019-03-20 16:45:15 +01006664static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006665 struct btrfs_dev_item *dev_item)
6666{
David Sterba17850752019-03-20 16:45:15 +01006667 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006668 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006669 struct btrfs_device *device;
6670 u64 devid;
6671 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006672 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006673 u8 dev_uuid[BTRFS_UUID_SIZE];
6674
Chris Mason0b86a832008-03-24 15:01:56 -04006675 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006676 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006677 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006678 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006679 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006680
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006681 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006682 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006683 if (IS_ERR(fs_devices))
6684 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006685 }
6686
Anand Jaine4319cd2019-01-17 23:32:31 +08006687 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006688 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006689 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006690 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006691 btrfs_report_missing_device(fs_info, devid,
6692 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006693 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006694 }
Yan Zheng2b820322008-11-17 21:11:30 -05006695
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006696 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006697 if (IS_ERR(device)) {
6698 btrfs_err(fs_info,
6699 "failed to add missing dev %llu: %ld",
6700 devid, PTR_ERR(device));
6701 return PTR_ERR(device);
6702 }
Anand Jain2b902df2017-10-09 11:07:46 +08006703 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006704 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006705 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006706 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6707 btrfs_report_missing_device(fs_info,
6708 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006709 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006710 }
6711 btrfs_report_missing_device(fs_info, devid,
6712 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006713 }
Miao Xie5f375832014-09-03 21:35:46 +08006714
Anand Jaine6e674b2017-12-04 12:54:54 +08006715 if (!device->bdev &&
6716 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006717 /*
6718 * this happens when a device that was properly setup
6719 * in the device info lists suddenly goes bad.
6720 * device->bdev is NULL, and so we have to set
6721 * device->missing to one here
6722 */
Miao Xie5f375832014-09-03 21:35:46 +08006723 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006724 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006725 }
Miao Xie5f375832014-09-03 21:35:46 +08006726
6727 /* Move the device to its own fs_devices */
6728 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006729 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6730 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006731
6732 list_move(&device->dev_list, &fs_devices->devices);
6733 device->fs_devices->num_devices--;
6734 fs_devices->num_devices++;
6735
6736 device->fs_devices->missing_devices--;
6737 fs_devices->missing_devices++;
6738
6739 device->fs_devices = fs_devices;
6740 }
Yan Zheng2b820322008-11-17 21:11:30 -05006741 }
6742
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006743 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006744 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006745 if (device->generation !=
6746 btrfs_device_generation(leaf, dev_item))
6747 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006748 }
Chris Mason0b86a832008-03-24 15:01:56 -04006749
6750 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006751 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006752 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006753 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006754 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006755 atomic64_add(device->total_bytes - device->bytes_used,
6756 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006757 }
Chris Mason0b86a832008-03-24 15:01:56 -04006758 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006759 return ret;
6760}
6761
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006762int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006763{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006764 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006765 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006766 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006767 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006768 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006769 u8 *array_ptr;
6770 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006771 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006772 u32 num_stripes;
6773 u32 array_size;
6774 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006775 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006776 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006777 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006778
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006779 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006780 /*
6781 * This will create extent buffer of nodesize, superblock size is
6782 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6783 * overallocate but we can keep it as-is, only the first page is used.
6784 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006785 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006786 if (IS_ERR(sb))
6787 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006788 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006789 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006790 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006791 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006792 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006793 * pages up-to-date when the page is larger: extent does not cover the
6794 * whole page and consequently check_page_uptodate does not find all
6795 * the page's extents up-to-date (the hole beyond sb),
6796 * write_extent_buffer then triggers a WARN_ON.
6797 *
6798 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6799 * but sb spans only this function. Add an explicit SetPageUptodate call
6800 * to silence the warning eg. on PowerPC 64.
6801 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006802 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006803 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006804
Chris Masona061fc82008-05-07 11:43:44 -04006805 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006806 array_size = btrfs_super_sys_array_size(super_copy);
6807
David Sterba1ffb22c2014-10-31 19:02:42 +01006808 array_ptr = super_copy->sys_chunk_array;
6809 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6810 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006811
David Sterba1ffb22c2014-10-31 19:02:42 +01006812 while (cur_offset < array_size) {
6813 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006814 len = sizeof(*disk_key);
6815 if (cur_offset + len > array_size)
6816 goto out_short_read;
6817
Chris Mason0b86a832008-03-24 15:01:56 -04006818 btrfs_disk_key_to_cpu(&key, disk_key);
6819
David Sterba1ffb22c2014-10-31 19:02:42 +01006820 array_ptr += len;
6821 sb_array_offset += len;
6822 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006823
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006824 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006825 btrfs_err(fs_info,
6826 "unexpected item type %u in sys_array at offset %u",
6827 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006828 ret = -EIO;
6829 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006830 }
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006831
6832 chunk = (struct btrfs_chunk *)sb_array_offset;
6833 /*
6834 * At least one btrfs_chunk with one stripe must be present,
6835 * exact stripe count check comes afterwards
6836 */
6837 len = btrfs_chunk_item_size(1);
6838 if (cur_offset + len > array_size)
6839 goto out_short_read;
6840
6841 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6842 if (!num_stripes) {
6843 btrfs_err(fs_info,
6844 "invalid number of stripes %u in sys_array at offset %u",
6845 num_stripes, cur_offset);
6846 ret = -EIO;
6847 break;
6848 }
6849
6850 type = btrfs_chunk_type(sb, chunk);
6851 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
6852 btrfs_err(fs_info,
6853 "invalid chunk type %llu in sys_array at offset %u",
6854 type, cur_offset);
6855 ret = -EIO;
6856 break;
6857 }
6858
6859 len = btrfs_chunk_item_size(num_stripes);
6860 if (cur_offset + len > array_size)
6861 goto out_short_read;
6862
6863 ret = read_one_chunk(&key, sb, chunk);
6864 if (ret)
6865 break;
6866
David Sterba1ffb22c2014-10-31 19:02:42 +01006867 array_ptr += len;
6868 sb_array_offset += len;
6869 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006870 }
Liu Bod8651772016-06-03 17:41:42 -07006871 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006872 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006873 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006874
6875out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006876 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006877 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006878 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006879 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006880 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006881}
6882
Qu Wenruo21634a12017-03-09 09:34:36 +08006883/*
6884 * Check if all chunks in the fs are OK for read-write degraded mount
6885 *
Anand Jain6528b992017-12-18 17:08:59 +08006886 * If the @failing_dev is specified, it's accounted as missing.
6887 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006888 * Return true if all chunks meet the minimal RW mount requirements.
6889 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6890 */
Anand Jain6528b992017-12-18 17:08:59 +08006891bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6892 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006893{
David Sterbac8bf1b62019-05-17 11:43:17 +02006894 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08006895 struct extent_map *em;
6896 u64 next_start = 0;
6897 bool ret = true;
6898
David Sterbac8bf1b62019-05-17 11:43:17 +02006899 read_lock(&map_tree->lock);
6900 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
6901 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08006902 /* No chunk at all? Return false anyway */
6903 if (!em) {
6904 ret = false;
6905 goto out;
6906 }
6907 while (em) {
6908 struct map_lookup *map;
6909 int missing = 0;
6910 int max_tolerated;
6911 int i;
6912
6913 map = em->map_lookup;
6914 max_tolerated =
6915 btrfs_get_num_tolerated_disk_barrier_failures(
6916 map->type);
6917 for (i = 0; i < map->num_stripes; i++) {
6918 struct btrfs_device *dev = map->stripes[i].dev;
6919
Anand Jaine6e674b2017-12-04 12:54:54 +08006920 if (!dev || !dev->bdev ||
6921 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006922 dev->last_flush_error)
6923 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006924 else if (failing_dev && failing_dev == dev)
6925 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006926 }
6927 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006928 if (!failing_dev)
6929 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01006930 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08006931 em->start, missing, max_tolerated);
6932 free_extent_map(em);
6933 ret = false;
6934 goto out;
6935 }
6936 next_start = extent_map_end(em);
6937 free_extent_map(em);
6938
David Sterbac8bf1b62019-05-17 11:43:17 +02006939 read_lock(&map_tree->lock);
6940 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08006941 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02006942 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08006943 }
6944out:
6945 return ret;
6946}
6947
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006948int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006949{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006950 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006951 struct btrfs_path *path;
6952 struct extent_buffer *leaf;
6953 struct btrfs_key key;
6954 struct btrfs_key found_key;
6955 int ret;
6956 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006957 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006958
Chris Mason0b86a832008-03-24 15:01:56 -04006959 path = btrfs_alloc_path();
6960 if (!path)
6961 return -ENOMEM;
6962
Anand Jain3dd0f7a2018-04-12 10:29:32 +08006963 /*
6964 * uuid_mutex is needed only if we are mounting a sprout FS
6965 * otherwise we don't need it.
6966 */
Li Zefanb367e472011-12-07 11:38:24 +08006967 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006968 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006969
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006970 /*
6971 * Read all device items, and then all the chunk items. All
6972 * device items are found before any chunk item (their object id
6973 * is smaller than the lowest possible object id for a chunk
6974 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006975 */
6976 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6977 key.offset = 0;
6978 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006979 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006980 if (ret < 0)
6981 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006982 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006983 leaf = path->nodes[0];
6984 slot = path->slots[0];
6985 if (slot >= btrfs_header_nritems(leaf)) {
6986 ret = btrfs_next_leaf(root, path);
6987 if (ret == 0)
6988 continue;
6989 if (ret < 0)
6990 goto error;
6991 break;
6992 }
6993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006994 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6995 struct btrfs_dev_item *dev_item;
6996 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006997 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01006998 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006999 if (ret)
7000 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007001 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007002 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7003 struct btrfs_chunk *chunk;
7004 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba9690ac02019-03-20 16:43:07 +01007005 ret = read_one_chunk(&found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007006 if (ret)
7007 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007008 }
7009 path->slots[0]++;
7010 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007011
7012 /*
7013 * After loading chunk tree, we've got all device information,
7014 * do another round of validation checks.
7015 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007016 if (total_dev != fs_info->fs_devices->total_devices) {
7017 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007018 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007019 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007020 total_dev);
7021 ret = -EINVAL;
7022 goto error;
7023 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007024 if (btrfs_super_total_bytes(fs_info->super_copy) <
7025 fs_info->fs_devices->total_rw_bytes) {
7026 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007027 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007028 btrfs_super_total_bytes(fs_info->super_copy),
7029 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007030 ret = -EINVAL;
7031 goto error;
7032 }
Chris Mason0b86a832008-03-24 15:01:56 -04007033 ret = 0;
7034error:
David Sterba34441362016-10-04 19:34:27 +02007035 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007036 mutex_unlock(&uuid_mutex);
7037
Yan Zheng2b820322008-11-17 21:11:30 -05007038 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007039 return ret;
7040}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007041
Miao Xiecb517ea2013-05-15 07:48:19 +00007042void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7043{
7044 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7045 struct btrfs_device *device;
7046
Liu Bo29cc83f2014-05-11 23:14:59 +08007047 while (fs_devices) {
7048 mutex_lock(&fs_devices->device_list_mutex);
7049 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007050 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007051 mutex_unlock(&fs_devices->device_list_mutex);
7052
7053 fs_devices = fs_devices->seed;
7054 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007055}
7056
David Sterba1dc990d2019-08-21 20:05:32 +02007057static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7058 const struct btrfs_dev_stats_item *ptr,
7059 int index)
7060{
7061 u64 val;
7062
7063 read_extent_buffer(eb, &val,
7064 offsetof(struct btrfs_dev_stats_item, values) +
7065 ((unsigned long)ptr) + (index * sizeof(u64)),
7066 sizeof(val));
7067 return val;
7068}
7069
7070static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7071 struct btrfs_dev_stats_item *ptr,
7072 int index, u64 val)
7073{
7074 write_extent_buffer(eb, &val,
7075 offsetof(struct btrfs_dev_stats_item, values) +
7076 ((unsigned long)ptr) + (index * sizeof(u64)),
7077 sizeof(val));
7078}
7079
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007080int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7081{
7082 struct btrfs_key key;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007083 struct btrfs_root *dev_root = fs_info->dev_root;
7084 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7085 struct extent_buffer *eb;
7086 int slot;
7087 int ret = 0;
7088 struct btrfs_device *device;
7089 struct btrfs_path *path = NULL;
7090 int i;
7091
7092 path = btrfs_alloc_path();
Anand Jain3b80a9842019-08-21 17:26:32 +08007093 if (!path)
7094 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007095
7096 mutex_lock(&fs_devices->device_list_mutex);
7097 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7098 int item_size;
7099 struct btrfs_dev_stats_item *ptr;
7100
David Sterba242e2952016-01-25 17:51:31 +01007101 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7102 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007103 key.offset = device->devid;
7104 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7105 if (ret) {
Anand Jainae4b9b42019-08-07 16:21:20 +08007106 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7107 btrfs_dev_stat_set(device, i, 0);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007108 device->dev_stats_valid = 1;
7109 btrfs_release_path(path);
7110 continue;
7111 }
7112 slot = path->slots[0];
7113 eb = path->nodes[0];
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007114 item_size = btrfs_item_size_nr(eb, slot);
7115
7116 ptr = btrfs_item_ptr(eb, slot,
7117 struct btrfs_dev_stats_item);
7118
7119 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7120 if (item_size >= (1 + i) * sizeof(__le64))
7121 btrfs_dev_stat_set(device, i,
7122 btrfs_dev_stats_value(eb, ptr, i));
7123 else
Anand Jain4e411a72019-08-07 16:21:19 +08007124 btrfs_dev_stat_set(device, i, 0);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007125 }
7126
7127 device->dev_stats_valid = 1;
7128 btrfs_dev_stat_print_on_load(device);
7129 btrfs_release_path(path);
7130 }
7131 mutex_unlock(&fs_devices->device_list_mutex);
7132
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007133 btrfs_free_path(path);
7134 return ret < 0 ? ret : 0;
7135}
7136
7137static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007138 struct btrfs_device *device)
7139{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007140 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007141 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007142 struct btrfs_path *path;
7143 struct btrfs_key key;
7144 struct extent_buffer *eb;
7145 struct btrfs_dev_stats_item *ptr;
7146 int ret;
7147 int i;
7148
David Sterba242e2952016-01-25 17:51:31 +01007149 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7150 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007151 key.offset = device->devid;
7152
7153 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007154 if (!path)
7155 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007156 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7157 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007158 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007159 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007160 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007161 goto out;
7162 }
7163
7164 if (ret == 0 &&
7165 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7166 /* need to delete old one and insert a new one */
7167 ret = btrfs_del_item(trans, dev_root, path);
7168 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007169 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007170 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007171 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007172 goto out;
7173 }
7174 ret = 1;
7175 }
7176
7177 if (ret == 1) {
7178 /* need to insert a new item */
7179 btrfs_release_path(path);
7180 ret = btrfs_insert_empty_item(trans, dev_root, path,
7181 &key, sizeof(*ptr));
7182 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007183 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007184 "insert dev_stats item for device %s failed %d",
7185 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007186 goto out;
7187 }
7188 }
7189
7190 eb = path->nodes[0];
7191 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7192 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7193 btrfs_set_dev_stats_value(eb, ptr, i,
7194 btrfs_dev_stat_read(device, i));
7195 btrfs_mark_buffer_dirty(eb);
7196
7197out:
7198 btrfs_free_path(path);
7199 return ret;
7200}
7201
7202/*
7203 * called from commit_transaction. Writes all changed device stats to disk.
7204 */
David Sterba196c9d82019-03-20 16:50:38 +01007205int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007206{
David Sterba196c9d82019-03-20 16:50:38 +01007207 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007208 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7209 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007210 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007211 int ret = 0;
7212
7213 mutex_lock(&fs_devices->device_list_mutex);
7214 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007215 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7216 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007217 continue;
7218
Nikolay Borisov9deae962017-10-24 13:47:37 +03007219
7220 /*
7221 * There is a LOAD-LOAD control dependency between the value of
7222 * dev_stats_ccnt and updating the on-disk values which requires
7223 * reading the in-memory counters. Such control dependencies
7224 * require explicit read memory barriers.
7225 *
7226 * This memory barriers pairs with smp_mb__before_atomic in
7227 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7228 * barrier implied by atomic_xchg in
7229 * btrfs_dev_stats_read_and_reset
7230 */
7231 smp_rmb();
7232
Nikolay Borisov5495f192018-07-20 19:37:49 +03007233 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007234 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007235 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007236 }
7237 mutex_unlock(&fs_devices->device_list_mutex);
7238
7239 return ret;
7240}
7241
Stefan Behrens442a4f62012-05-25 16:06:08 +02007242void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7243{
7244 btrfs_dev_stat_inc(dev, index);
7245 btrfs_dev_stat_print_on_error(dev);
7246}
7247
Eric Sandeen48a3b632013-04-25 20:41:01 +00007248static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007249{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007250 if (!dev->dev_stats_valid)
7251 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007252 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007253 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007254 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007255 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7256 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7257 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007258 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7259 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007260}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007261
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007262static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7263{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007264 int i;
7265
7266 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7267 if (btrfs_dev_stat_read(dev, i) != 0)
7268 break;
7269 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7270 return; /* all values == 0, suppress message */
7271
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007272 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007273 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007274 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007275 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7276 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7277 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7278 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7279 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7280}
7281
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007282int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007283 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007284{
7285 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007286 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007287 int i;
7288
7289 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007290 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7291 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007292 mutex_unlock(&fs_devices->device_list_mutex);
7293
7294 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007295 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007296 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007297 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007298 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007299 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007300 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007301 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7302 if (stats->nr_items > i)
7303 stats->values[i] =
7304 btrfs_dev_stat_read_and_reset(dev, i);
7305 else
Anand Jain4e411a72019-08-07 16:21:19 +08007306 btrfs_dev_stat_set(dev, i, 0);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007307 }
Anand Jaina69976b2020-01-10 12:26:34 +08007308 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7309 current->comm, task_pid_nr(current));
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007310 } else {
7311 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7312 if (stats->nr_items > i)
7313 stats->values[i] = btrfs_dev_stat_read(dev, i);
7314 }
7315 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7316 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7317 return 0;
7318}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007319
David Sterbada353f62017-02-14 17:55:53 +01007320void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007321{
7322 struct buffer_head *bh;
7323 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007324 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007325
Anand Jain12b1c262015-08-14 18:32:59 +08007326 if (!bdev)
7327 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007328
Anand Jain12b1c262015-08-14 18:32:59 +08007329 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7330 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007331
Anand Jain12b1c262015-08-14 18:32:59 +08007332 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7333 continue;
7334
7335 disk_super = (struct btrfs_super_block *)bh->b_data;
7336
7337 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7338 set_buffer_dirty(bh);
7339 sync_dirty_buffer(bh);
7340 brelse(bh);
7341 }
7342
7343 /* Notify udev that device has changed */
7344 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7345
7346 /* Update ctime/mtime for device path for libblkid */
7347 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007348}
Miao Xie935e5cc2014-09-03 21:35:33 +08007349
7350/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007351 * Update the size and bytes used for each device where it changed. This is
7352 * delayed since we would otherwise get errors while writing out the
7353 * superblocks.
7354 *
7355 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007356 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007357void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007358{
Miao Xie935e5cc2014-09-03 21:35:33 +08007359 struct btrfs_device *curr, *next;
7360
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007361 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7362
7363 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007364 return;
7365
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007366 /*
7367 * We don't need the device_list_mutex here. This list is owned by the
7368 * transaction and the transaction must complete before the device is
7369 * released.
7370 */
7371 mutex_lock(&trans->fs_info->chunk_mutex);
7372 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7373 post_commit_list) {
7374 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007375 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007376 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007377 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007378 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007379}
Anand Jain5a13f432015-03-10 06:38:31 +08007380
7381void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7382{
7383 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7384 while (fs_devices) {
7385 fs_devices->fs_info = fs_info;
7386 fs_devices = fs_devices->seed;
7387 }
7388}
7389
7390void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7391{
7392 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7393 while (fs_devices) {
7394 fs_devices->fs_info = NULL;
7395 fs_devices = fs_devices->seed;
7396 }
7397}
David Sterba46df06b2018-07-13 20:46:30 +02007398
7399/*
7400 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7401 */
7402int btrfs_bg_type_to_factor(u64 flags)
7403{
David Sterba44b28ad2019-05-17 11:43:31 +02007404 const int index = btrfs_bg_flags_to_raid_index(flags);
7405
7406 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007407}
Qu Wenruocf90d882018-08-01 10:37:19 +08007408
7409
Qu Wenruocf90d882018-08-01 10:37:19 +08007410
7411static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7412 u64 chunk_offset, u64 devid,
7413 u64 physical_offset, u64 physical_len)
7414{
David Sterbac8bf1b62019-05-17 11:43:17 +02007415 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007416 struct extent_map *em;
7417 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007418 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007419 u64 stripe_len;
7420 bool found = false;
7421 int ret = 0;
7422 int i;
7423
7424 read_lock(&em_tree->lock);
7425 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7426 read_unlock(&em_tree->lock);
7427
7428 if (!em) {
7429 btrfs_err(fs_info,
7430"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7431 physical_offset, devid);
7432 ret = -EUCLEAN;
7433 goto out;
7434 }
7435
7436 map = em->map_lookup;
7437 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7438 if (physical_len != stripe_len) {
7439 btrfs_err(fs_info,
7440"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7441 physical_offset, devid, em->start, physical_len,
7442 stripe_len);
7443 ret = -EUCLEAN;
7444 goto out;
7445 }
7446
7447 for (i = 0; i < map->num_stripes; i++) {
7448 if (map->stripes[i].dev->devid == devid &&
7449 map->stripes[i].physical == physical_offset) {
7450 found = true;
7451 if (map->verified_stripes >= map->num_stripes) {
7452 btrfs_err(fs_info,
7453 "too many dev extents for chunk %llu found",
7454 em->start);
7455 ret = -EUCLEAN;
7456 goto out;
7457 }
7458 map->verified_stripes++;
7459 break;
7460 }
7461 }
7462 if (!found) {
7463 btrfs_err(fs_info,
7464 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7465 physical_offset, devid);
7466 ret = -EUCLEAN;
7467 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007468
7469 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007470 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007471 if (!dev) {
7472 btrfs_err(fs_info, "failed to find devid %llu", devid);
7473 ret = -EUCLEAN;
7474 goto out;
7475 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007476
7477 /* It's possible this device is a dummy for seed device */
7478 if (dev->disk_total_bytes == 0) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08007479 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7480 NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007481 if (!dev) {
7482 btrfs_err(fs_info, "failed to find seed devid %llu",
7483 devid);
7484 ret = -EUCLEAN;
7485 goto out;
7486 }
7487 }
7488
Qu Wenruo05a37c42018-10-05 17:45:55 +08007489 if (physical_offset + physical_len > dev->disk_total_bytes) {
7490 btrfs_err(fs_info,
7491"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7492 devid, physical_offset, physical_len,
7493 dev->disk_total_bytes);
7494 ret = -EUCLEAN;
7495 goto out;
7496 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007497out:
7498 free_extent_map(em);
7499 return ret;
7500}
7501
7502static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7503{
David Sterbac8bf1b62019-05-17 11:43:17 +02007504 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007505 struct extent_map *em;
7506 struct rb_node *node;
7507 int ret = 0;
7508
7509 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007510 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007511 em = rb_entry(node, struct extent_map, rb_node);
7512 if (em->map_lookup->num_stripes !=
7513 em->map_lookup->verified_stripes) {
7514 btrfs_err(fs_info,
7515 "chunk %llu has missing dev extent, have %d expect %d",
7516 em->start, em->map_lookup->verified_stripes,
7517 em->map_lookup->num_stripes);
7518 ret = -EUCLEAN;
7519 goto out;
7520 }
7521 }
7522out:
7523 read_unlock(&em_tree->lock);
7524 return ret;
7525}
7526
7527/*
7528 * Ensure that all dev extents are mapped to correct chunk, otherwise
7529 * later chunk allocation/free would cause unexpected behavior.
7530 *
7531 * NOTE: This will iterate through the whole device tree, which should be of
7532 * the same size level as the chunk tree. This slightly increases mount time.
7533 */
7534int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7535{
7536 struct btrfs_path *path;
7537 struct btrfs_root *root = fs_info->dev_root;
7538 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007539 u64 prev_devid = 0;
7540 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007541 int ret = 0;
7542
7543 key.objectid = 1;
7544 key.type = BTRFS_DEV_EXTENT_KEY;
7545 key.offset = 0;
7546
7547 path = btrfs_alloc_path();
7548 if (!path)
7549 return -ENOMEM;
7550
7551 path->reada = READA_FORWARD;
7552 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7553 if (ret < 0)
7554 goto out;
7555
7556 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7557 ret = btrfs_next_item(root, path);
7558 if (ret < 0)
7559 goto out;
7560 /* No dev extents at all? Not good */
7561 if (ret > 0) {
7562 ret = -EUCLEAN;
7563 goto out;
7564 }
7565 }
7566 while (1) {
7567 struct extent_buffer *leaf = path->nodes[0];
7568 struct btrfs_dev_extent *dext;
7569 int slot = path->slots[0];
7570 u64 chunk_offset;
7571 u64 physical_offset;
7572 u64 physical_len;
7573 u64 devid;
7574
7575 btrfs_item_key_to_cpu(leaf, &key, slot);
7576 if (key.type != BTRFS_DEV_EXTENT_KEY)
7577 break;
7578 devid = key.objectid;
7579 physical_offset = key.offset;
7580
7581 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7582 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7583 physical_len = btrfs_dev_extent_length(leaf, dext);
7584
Qu Wenruo5eb19382018-10-05 17:45:54 +08007585 /* Check if this dev extent overlaps with the previous one */
7586 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7587 btrfs_err(fs_info,
7588"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7589 devid, physical_offset, prev_dev_ext_end);
7590 ret = -EUCLEAN;
7591 goto out;
7592 }
7593
Qu Wenruocf90d882018-08-01 10:37:19 +08007594 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7595 physical_offset, physical_len);
7596 if (ret < 0)
7597 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007598 prev_devid = devid;
7599 prev_dev_ext_end = physical_offset + physical_len;
7600
Qu Wenruocf90d882018-08-01 10:37:19 +08007601 ret = btrfs_next_item(root, path);
7602 if (ret < 0)
7603 goto out;
7604 if (ret > 0) {
7605 ret = 0;
7606 break;
7607 }
7608 }
7609
7610 /* Ensure all chunks have corresponding dev extents */
7611 ret = verify_chunk_dev_extent_mapping(fs_info);
7612out:
7613 btrfs_free_path(path);
7614 return ret;
7615}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007616
7617/*
7618 * Check whether the given block group or device is pinned by any inode being
7619 * used as a swapfile.
7620 */
7621bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7622{
7623 struct btrfs_swapfile_pin *sp;
7624 struct rb_node *node;
7625
7626 spin_lock(&fs_info->swapfile_pins_lock);
7627 node = fs_info->swapfile_pins.rb_node;
7628 while (node) {
7629 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7630 if (ptr < sp->ptr)
7631 node = node->rb_left;
7632 else if (ptr > sp->ptr)
7633 node = node->rb_right;
7634 else
7635 break;
7636 }
7637 spin_unlock(&fs_info->swapfile_pins_lock);
7638 return node != NULL;
7639}