blob: fbc4b58228f78432edb6f2bd030f874b684dd0e6 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason0b86a832008-03-24 15:01:56 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason0b86a832008-03-24 15:01:56 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Chris Mason0b86a832008-03-24 15:01:56 -04006#include <linux/sched.h>
Josef Bacikfccc0002020-08-31 10:52:42 -04007#include <linux/sched/mm.h>
Chris Mason0b86a832008-03-24 15:01:56 -04008#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040010#include <linux/blkdev.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020011#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020012#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050013#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020014#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070015#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080016#include <linux/list_sort.h>
David Sterba784352f2019-08-21 18:54:28 +020017#include "misc.h"
Chris Mason0b86a832008-03-24 15:01:56 -040018#include "ctree.h"
19#include "extent_map.h"
20#include "disk-io.h"
21#include "transaction.h"
22#include "print-tree.h"
23#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050024#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040025#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010026#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040027#include "rcu-string.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010028#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080029#include "sysfs.h"
Qu Wenruo82fc28f2019-03-20 13:16:42 +080030#include "tree-checker.h"
Josef Bacik8719aaa2019-06-18 16:09:16 -040031#include "space-info.h"
Josef Bacikaac00232019-06-20 15:37:44 -040032#include "block-group.h"
Dennis Zhoub0643e52019-12-13 16:22:14 -080033#include "discard.h"
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 *
Josef Bacik18c850f2020-07-17 15:12:27 -0400249 * does not protect: manipulation of the fs_devices::devices list in general
250 * but in mount context it could be used to exclude list modifications by eg.
251 * scan ioctl
David Sterba9c6b1c42017-06-16 22:30:00 +0200252 *
253 * btrfs_device::name - renames (write side), read is RCU
254 *
255 * fs_devices::device_list_mutex (per-fs, with RCU)
256 * ------------------------------------------------
257 * protects updates to fs_devices::devices, ie. adding and deleting
258 *
259 * simple list traversal with read-only actions can be done with RCU protection
260 *
261 * may be used to exclude some operations from running concurrently without any
262 * modifications to the list (see write_all_supers)
263 *
Josef Bacik18c850f2020-07-17 15:12:27 -0400264 * Is not required at mount and close times, because our device list is
265 * protected by the uuid_mutex at that point.
266 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200267 * balance_mutex
268 * -------------
269 * protects balance structures (status, state) and context accessed from
270 * several places (internally, ioctl)
271 *
272 * chunk_mutex
273 * -----------
274 * protects chunks, adding or removing during allocation, trim or when a new
Nikolay Borisov0b6f5d42019-05-09 18:11:11 +0300275 * device is added/removed. Additionally it also protects post_commit_list of
276 * individual devices, since they can be added to the transaction's
277 * post_commit_list only with chunk_mutex held.
David Sterba9c6b1c42017-06-16 22:30:00 +0200278 *
279 * cleaner_mutex
280 * -------------
281 * a big lock that is held by the cleaner thread and prevents running subvolume
282 * cleaning together with relocation or delayed iputs
283 *
284 *
285 * Lock nesting
286 * ============
287 *
288 * uuid_mutex
Anand Jainae3e7152020-05-14 01:42:45 +0800289 * device_list_mutex
290 * chunk_mutex
291 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800292 *
293 *
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500294 * Exclusive operations
295 * ====================
Anand Jain89595e82018-04-18 14:59:25 +0800296 *
297 * Maintains the exclusivity of the following operations that apply to the
298 * whole filesystem and cannot run in parallel.
299 *
300 * - Balance (*)
301 * - Device add
302 * - Device remove
303 * - Device replace (*)
304 * - Resize
305 *
306 * The device operations (as above) can be in one of the following states:
307 *
308 * - Running state
309 * - Paused state
310 * - Completed state
311 *
312 * Only device operations marked with (*) can go into the Paused state for the
313 * following reasons:
314 *
315 * - ioctl (only Balance can be Paused through ioctl)
316 * - filesystem remounted as read-only
317 * - filesystem unmounted and mounted as read-only
318 * - system power-cycle and filesystem mounted as read-only
319 * - filesystem or device errors leading to forced read-only
320 *
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500321 * The status of exclusive operation is set and cleared atomically.
322 * During the course of Paused state, fs_info::exclusive_operation remains set.
Anand Jain89595e82018-04-18 14:59:25 +0800323 * A device operation in Paused or Running state can be canceled or resumed
324 * either by ioctl (Balance only) or when remounted as read-write.
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -0500325 * The exclusive status is cleared when the device operation is canceled or
Anand Jain89595e82018-04-18 14:59:25 +0800326 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200327 */
328
Miao Xie67a2c452014-09-03 21:35:43 +0800329DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400330static LIST_HEAD(fs_uuids);
David Sterba4143cb82019-10-01 19:57:37 +0200331struct list_head * __attribute_const__ btrfs_get_fs_uuids(void)
Anand Jainc73eccf2015-03-10 06:38:30 +0800332{
333 return &fs_uuids;
334}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400335
David Sterba2dfeca92017-06-14 02:48:07 +0200336/*
337 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200338 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
339 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200340 *
341 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
342 * The returned struct is not linked onto any lists and can be destroyed with
343 * kfree() right away.
344 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200345static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
346 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300347{
348 struct btrfs_fs_devices *fs_devs;
349
David Sterba78f2c9e2016-02-11 14:25:38 +0100350 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300351 if (!fs_devs)
352 return ERR_PTR(-ENOMEM);
353
354 mutex_init(&fs_devs->device_list_mutex);
355
356 INIT_LIST_HEAD(&fs_devs->devices);
357 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800358 INIT_LIST_HEAD(&fs_devs->fs_list);
Nikolay Borisov944d3f92020-07-16 10:25:33 +0300359 INIT_LIST_HEAD(&fs_devs->seed_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300360 if (fsid)
361 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300362
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200363 if (metadata_fsid)
364 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
365 else if (fsid)
366 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
367
Ilya Dryomov2208a372013-08-12 14:33:03 +0300368 return fs_devs;
369}
370
David Sterbaa425f9d2018-03-20 15:47:33 +0100371void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100372{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200373 WARN_ON(!list_empty(&device->post_commit_list));
David Sterba48dae9c2017-10-30 18:10:25 +0100374 rcu_string_free(device->name);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200375 extent_io_tree_release(&device->alloc_state);
David Sterba48dae9c2017-10-30 18:10:25 +0100376 bio_put(device->flush_bio);
377 kfree(device);
378}
379
Yan Zhenge4404d62008-12-12 10:03:26 -0500380static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
381{
382 struct btrfs_device *device;
383 WARN_ON(fs_devices->opened);
384 while (!list_empty(&fs_devices->devices)) {
385 device = list_entry(fs_devices->devices.next,
386 struct btrfs_device, dev_list);
387 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100388 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500389 }
390 kfree(fs_devices);
391}
392
David Sterbaffc5a372018-02-19 17:24:15 +0100393void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400394{
395 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400396
Yan Zheng2b820322008-11-17 21:11:30 -0500397 while (!list_empty(&fs_uuids)) {
398 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800399 struct btrfs_fs_devices, fs_list);
400 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500401 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400403}
404
David Sterba48dae9c2017-10-30 18:10:25 +0100405/*
406 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
407 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100408 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100409 */
Qu Wenruo154f7cb2020-08-20 15:42:46 +0800410static struct btrfs_device *__alloc_device(struct btrfs_fs_info *fs_info)
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300411{
412 struct btrfs_device *dev;
413
David Sterba78f2c9e2016-02-11 14:25:38 +0100414 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300415 if (!dev)
416 return ERR_PTR(-ENOMEM);
417
David Sterbae0ae9992017-06-06 17:06:06 +0200418 /*
419 * Preallocate a bio that's always going to be used for flushing device
420 * barriers and matches the device lifespan
421 */
422 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
423 if (!dev->flush_bio) {
424 kfree(dev);
425 return ERR_PTR(-ENOMEM);
426 }
David Sterbae0ae9992017-06-06 17:06:06 +0200427
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300428 INIT_LIST_HEAD(&dev->dev_list);
429 INIT_LIST_HEAD(&dev->dev_alloc_list);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200430 INIT_LIST_HEAD(&dev->post_commit_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300431
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300432 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800433 atomic_set(&dev->dev_stats_ccnt, 0);
Davidlohr Buesod5c82382020-10-21 13:17:24 -0700434 btrfs_device_data_ordered_init(dev, fs_info);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700435 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800436 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Qu Wenruo154f7cb2020-08-20 15:42:46 +0800437 extent_io_tree_init(fs_info, &dev->alloc_state,
438 IO_TREE_DEVICE_ALLOC_STATE, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300439
440 return dev;
441}
442
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200443static noinline struct btrfs_fs_devices *find_fsid(
444 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400445{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400446 struct btrfs_fs_devices *fs_devices;
447
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200448 ASSERT(fsid);
449
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200450 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800451 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200452 if (metadata_fsid) {
453 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
454 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
455 BTRFS_FSID_SIZE) == 0)
456 return fs_devices;
457 } else {
458 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
459 return fs_devices;
460 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400461 }
462 return NULL;
463}
464
Su Yuec6730a02020-01-10 14:11:33 +0200465static struct btrfs_fs_devices *find_fsid_with_metadata_uuid(
466 struct btrfs_super_block *disk_super)
467{
468
469 struct btrfs_fs_devices *fs_devices;
470
471 /*
472 * Handle scanned device having completed its fsid change but
473 * belonging to a fs_devices that was created by first scanning
474 * a device which didn't have its fsid/metadata_uuid changed
475 * at all and the CHANGING_FSID_V2 flag set.
476 */
477 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
478 if (fs_devices->fsid_change &&
479 memcmp(disk_super->metadata_uuid, fs_devices->fsid,
480 BTRFS_FSID_SIZE) == 0 &&
481 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
482 BTRFS_FSID_SIZE) == 0) {
483 return fs_devices;
484 }
485 }
486 /*
487 * Handle scanned device having completed its fsid change but
488 * belonging to a fs_devices that was created by a device that
489 * has an outdated pair of fsid/metadata_uuid and
490 * CHANGING_FSID_V2 flag set.
491 */
492 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
493 if (fs_devices->fsid_change &&
494 memcmp(fs_devices->metadata_uuid,
495 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
496 memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid,
497 BTRFS_FSID_SIZE) == 0) {
498 return fs_devices;
499 }
500 }
501
502 return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
503}
504
505
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100506static int
507btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
508 int flush, struct block_device **bdev,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900509 struct btrfs_super_block **disk_super)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100510{
511 int ret;
512
513 *bdev = blkdev_get_by_path(device_path, flags, holder);
514
515 if (IS_ERR(*bdev)) {
516 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100517 goto error;
518 }
519
520 if (flush)
521 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200522 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100523 if (ret) {
524 blkdev_put(*bdev, flags);
525 goto error;
526 }
527 invalidate_bdev(*bdev);
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900528 *disk_super = btrfs_read_dev_super(*bdev);
529 if (IS_ERR(*disk_super)) {
530 ret = PTR_ERR(*disk_super);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100531 blkdev_put(*bdev, flags);
532 goto error;
533 }
534
535 return 0;
536
537error:
538 *bdev = NULL;
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100539 return ret;
540}
541
Anand Jain70bc7082019-01-04 13:31:53 +0800542static bool device_path_matched(const char *path, struct btrfs_device *device)
543{
544 int found;
545
546 rcu_read_lock();
547 found = strcmp(rcu_str_deref(device->name), path);
548 rcu_read_unlock();
549
550 return found == 0;
551}
552
Anand Jaind8367db2018-01-18 22:00:37 +0800553/*
554 * Search and remove all stale (devices which are not mounted) devices.
555 * When both inputs are NULL, it will search and release all stale devices.
556 * path: Optional. When provided will it release all unmounted devices
557 * matching this path only.
558 * skip_dev: Optional. Will skip this device when searching for the stale
559 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800560 * Return: 0 for success or if @path is NULL.
561 * -EBUSY if @path is a mounted device.
562 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800563 */
Anand Jain70bc7082019-01-04 13:31:53 +0800564static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800565 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800566{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800567 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
568 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800569 int ret = 0;
570
571 if (path)
572 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800573
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800574 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800575
Anand Jain70bc7082019-01-04 13:31:53 +0800576 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800577 list_for_each_entry_safe(device, tmp_device,
578 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800579 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800580 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800581 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800582 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800583 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800584 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800585 if (fs_devices->opened) {
586 /* for an already deleted device return 0 */
587 if (path && ret != 0)
588 ret = -EBUSY;
589 break;
590 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800591
Anand Jain4fde46f2015-06-17 21:10:48 +0800592 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800593 fs_devices->num_devices--;
594 list_del(&device->dev_list);
595 btrfs_free_device(device);
596
Anand Jain70bc7082019-01-04 13:31:53 +0800597 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800598 }
599 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800600
Anand Jain7bcb8162018-05-29 17:23:20 +0800601 if (fs_devices->num_devices == 0) {
602 btrfs_sysfs_remove_fsid(fs_devices);
603 list_del(&fs_devices->fs_list);
604 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800605 }
606 }
Anand Jain70bc7082019-01-04 13:31:53 +0800607
608 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800609}
610
Josef Bacik18c850f2020-07-17 15:12:27 -0400611/*
612 * This is only used on mount, and we are protected from competing things
613 * messing with our fs_devices by the uuid_mutex, thus we do not need the
614 * fs_devices->device_list_mutex here.
615 */
Anand Jain0fb08bc2017-11-09 23:45:24 +0800616static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
617 struct btrfs_device *device, fmode_t flags,
618 void *holder)
619{
620 struct request_queue *q;
621 struct block_device *bdev;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800622 struct btrfs_super_block *disk_super;
623 u64 devid;
624 int ret;
625
626 if (device->bdev)
627 return -EINVAL;
628 if (!device->name)
629 return -EINVAL;
630
631 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900632 &bdev, &disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800633 if (ret)
634 return ret;
635
Anand Jain0fb08bc2017-11-09 23:45:24 +0800636 devid = btrfs_stack_device_id(&disk_super->dev_item);
637 if (devid != device->devid)
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900638 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800639
640 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900641 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800642
643 device->generation = btrfs_super_generation(disk_super);
644
645 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200646 if (btrfs_super_incompat_flags(disk_super) &
647 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
648 pr_err(
649 "BTRFS: Invalid seeding and uuid-changed device detected\n");
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900650 goto error_free_page;
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200651 }
652
Anand Jainebbede42017-12-04 12:54:52 +0800653 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Johannes Thumshirn0395d842019-11-13 11:27:27 +0100654 fs_devices->seeding = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800655 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800656 if (bdev_read_only(bdev))
657 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
658 else
659 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800660 }
661
662 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800663 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +0100664 fs_devices->rotating = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800665
666 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800667 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800668 device->mode = flags;
669
670 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800671 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
672 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800673 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800674 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800675 }
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900676 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800677
678 return 0;
679
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900680error_free_page:
681 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800682 blkdev_put(bdev, flags);
683
684 return -EINVAL;
685}
686
David Sterba60999ca2014-03-26 18:26:36 +0100687/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200688 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
Su Yuec0d81c72020-01-10 14:11:32 +0200689 * being created with a disk that has already completed its fsid change. Such
690 * disk can belong to an fs which has its FSID changed or to one which doesn't.
691 * Handle both cases here.
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200692 */
693static struct btrfs_fs_devices *find_fsid_inprogress(
694 struct btrfs_super_block *disk_super)
695{
696 struct btrfs_fs_devices *fs_devices;
697
698 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
699 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
700 BTRFS_FSID_SIZE) != 0 &&
701 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
702 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
703 return fs_devices;
704 }
705 }
706
Su Yuec0d81c72020-01-10 14:11:32 +0200707 return find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200708}
709
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200710
711static struct btrfs_fs_devices *find_fsid_changed(
712 struct btrfs_super_block *disk_super)
713{
714 struct btrfs_fs_devices *fs_devices;
715
716 /*
717 * Handles the case where scanned device is part of an fs that had
718 * multiple successful changes of FSID but curently device didn't
Nikolay Borisov05840712020-01-10 14:11:34 +0200719 * observe it. Meaning our fsid will be different than theirs. We need
720 * to handle two subcases :
721 * 1 - The fs still continues to have different METADATA/FSID uuids.
722 * 2 - The fs is switched back to its original FSID (METADATA/FSID
723 * are equal).
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200724 */
725 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov05840712020-01-10 14:11:34 +0200726 /* Changed UUIDs */
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200727 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
728 BTRFS_FSID_SIZE) != 0 &&
729 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
730 BTRFS_FSID_SIZE) == 0 &&
731 memcmp(fs_devices->fsid, disk_super->fsid,
Nikolay Borisov05840712020-01-10 14:11:34 +0200732 BTRFS_FSID_SIZE) != 0)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200733 return fs_devices;
Nikolay Borisov05840712020-01-10 14:11:34 +0200734
735 /* Unchanged UUIDs */
736 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
737 BTRFS_FSID_SIZE) == 0 &&
738 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
739 BTRFS_FSID_SIZE) == 0)
740 return fs_devices;
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200741 }
742
743 return NULL;
744}
Nikolay Borisov13620892020-01-10 14:11:35 +0200745
746static struct btrfs_fs_devices *find_fsid_reverted_metadata(
747 struct btrfs_super_block *disk_super)
748{
749 struct btrfs_fs_devices *fs_devices;
750
751 /*
752 * Handle the case where the scanned device is part of an fs whose last
753 * metadata UUID change reverted it to the original FSID. At the same
754 * time * fs_devices was first created by another constitutent device
755 * which didn't fully observe the operation. This results in an
756 * btrfs_fs_devices created with metadata/fsid different AND
757 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
758 * fs_devices equal to the FSID of the disk.
759 */
760 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
761 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
762 BTRFS_FSID_SIZE) != 0 &&
763 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
764 BTRFS_FSID_SIZE) == 0 &&
765 fs_devices->fsid_change)
766 return fs_devices;
767 }
768
769 return NULL;
770}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200771/*
David Sterba60999ca2014-03-26 18:26:36 +0100772 * Add new device to list of registered devices
773 *
774 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800775 * device pointer which was just added or updated when successful
776 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100777 */
Anand Jaine124ece2018-01-18 22:02:35 +0800778static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800779 struct btrfs_super_block *disk_super,
780 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400781{
782 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200783 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400784 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400785 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800786 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200787 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
788 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200789 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
790 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400791
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200792 if (fsid_change_in_progress) {
Su Yuec0d81c72020-01-10 14:11:32 +0200793 if (!has_metadata_uuid)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200794 fs_devices = find_fsid_inprogress(disk_super);
Su Yuec0d81c72020-01-10 14:11:32 +0200795 else
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200796 fs_devices = find_fsid_changed(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200797 } else if (has_metadata_uuid) {
Su Yuec6730a02020-01-10 14:11:33 +0200798 fs_devices = find_fsid_with_metadata_uuid(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200799 } else {
Nikolay Borisov13620892020-01-10 14:11:35 +0200800 fs_devices = find_fsid_reverted_metadata(disk_super);
801 if (!fs_devices)
802 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200803 }
804
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200805
Chris Mason8a4b83c2008-03-24 15:02:07 -0400806 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200807 if (has_metadata_uuid)
808 fs_devices = alloc_fs_devices(disk_super->fsid,
809 disk_super->metadata_uuid);
810 else
811 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
812
Ilya Dryomov2208a372013-08-12 14:33:03 +0300813 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800814 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300815
Al Viro92900e52019-01-27 04:58:00 +0000816 fs_devices->fsid_change = fsid_change_in_progress;
817
Anand Jain9c6d1732018-05-29 14:10:20 +0800818 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800819 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300820
Chris Mason8a4b83c2008-03-24 15:02:07 -0400821 device = NULL;
822 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800823 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800824 device = btrfs_find_device(fs_devices, devid,
825 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200826
827 /*
828 * If this disk has been pulled into an fs devices created by
829 * a device which had the CHANGING_FSID_V2 flag then replace the
830 * metadata_uuid/fsid values of the fs_devices.
831 */
Nikolay Borisov13620892020-01-10 14:11:35 +0200832 if (fs_devices->fsid_change &&
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200833 found_transid > fs_devices->latest_generation) {
834 memcpy(fs_devices->fsid, disk_super->fsid,
835 BTRFS_FSID_SIZE);
Nikolay Borisov13620892020-01-10 14:11:35 +0200836
837 if (has_metadata_uuid)
838 memcpy(fs_devices->metadata_uuid,
839 disk_super->metadata_uuid,
840 BTRFS_FSID_SIZE);
841 else
842 memcpy(fs_devices->metadata_uuid,
843 disk_super->fsid, BTRFS_FSID_SIZE);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200844
845 fs_devices->fsid_change = false;
846 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400847 }
Miao Xie443f24f2014-07-24 11:37:15 +0800848
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800850 if (fs_devices->opened) {
851 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800852 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800853 }
Yan Zheng2b820322008-11-17 21:11:30 -0500854
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300855 device = btrfs_alloc_device(NULL, &devid,
856 disk_super->dev_item.uuid);
857 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800858 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400859 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800860 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400861 }
Josef Bacik606686e2012-06-04 14:03:51 -0400862
863 name = rcu_string_strdup(path, GFP_NOFS);
864 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100865 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800866 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800867 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400868 }
Josef Bacik606686e2012-06-04 14:03:51 -0400869 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200870
Xiao Guangrong1f781602011-04-20 10:09:16 +0000871 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100872 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400873
Yan Zheng2b820322008-11-17 21:11:30 -0500874 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800875 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800876
877 if (disk_super->label[0])
Anand Jainaa6c0df2019-10-02 18:30:48 +0800878 pr_info(
879 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
880 disk_super->label, devid, found_transid, path,
881 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800882 else
Anand Jainaa6c0df2019-10-02 18:30:48 +0800883 pr_info(
884 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
885 disk_super->fsid, devid, found_transid, path,
886 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800887
Josef Bacik606686e2012-06-04 14:03:51 -0400888 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800889 /*
890 * When FS is already mounted.
891 * 1. If you are here and if the device->name is NULL that
892 * means this device was missing at time of FS mount.
893 * 2. If you are here and if the device->name is different
894 * from 'path' that means either
895 * a. The same device disappeared and reappeared with
896 * different name. or
897 * b. The missing-disk-which-was-replaced, has
898 * reappeared now.
899 *
900 * We must allow 1 and 2a above. But 2b would be a spurious
901 * and unintentional.
902 *
903 * Further in case of 1 and 2a above, the disk at 'path'
904 * would have missed some transaction when it was away and
905 * in case of 2a the stale bdev has to be updated as well.
906 * 2b must not be allowed at all time.
907 */
908
909 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700910 * For now, we do allow update to btrfs_fs_device through the
911 * btrfs dev scan cli after FS has been mounted. We're still
912 * tracking a problem where systems fail mount by subvolume id
913 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800914 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700915 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800916 /*
917 * That is if the FS is _not_ mounted and if you
918 * are here, that means there is more than one
919 * disk with same uuid and devid.We keep the one
920 * with larger generation number or the last-in if
921 * generation are equal.
922 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800923 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800924 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800925 }
Anand Jainb96de002014-07-03 18:22:05 +0800926
Anand Jaina9261d42018-10-15 10:45:17 +0800927 /*
928 * We are going to replace the device path for a given devid,
929 * make sure it's the same device if the device is mounted
930 */
931 if (device->bdev) {
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100932 int error;
933 dev_t path_dev;
Anand Jaina9261d42018-10-15 10:45:17 +0800934
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100935 error = lookup_bdev(path, &path_dev);
936 if (error) {
Anand Jaina9261d42018-10-15 10:45:17 +0800937 mutex_unlock(&fs_devices->device_list_mutex);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100938 return ERR_PTR(error);
Anand Jaina9261d42018-10-15 10:45:17 +0800939 }
940
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100941 if (device->bdev->bd_dev != path_dev) {
Anand Jaina9261d42018-10-15 10:45:17 +0800942 mutex_unlock(&fs_devices->device_list_mutex);
943 btrfs_warn_in_rcu(device->fs_info,
Anand Jain79dae172020-09-03 21:30:12 +0800944 "duplicate device %s devid %llu generation %llu scanned by %s (%d)",
945 path, devid, found_transid,
946 current->comm,
947 task_pid_nr(current));
Anand Jaina9261d42018-10-15 10:45:17 +0800948 return ERR_PTR(-EEXIST);
949 }
Anand Jaina9261d42018-10-15 10:45:17 +0800950 btrfs_info_in_rcu(device->fs_info,
Anand Jain79dae172020-09-03 21:30:12 +0800951 "devid %llu device path %s changed to %s scanned by %s (%d)",
952 devid, rcu_str_deref(device->name),
953 path, current->comm,
954 task_pid_nr(current));
Anand Jaina9261d42018-10-15 10:45:17 +0800955 }
956
Josef Bacik606686e2012-06-04 14:03:51 -0400957 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800958 if (!name) {
959 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800960 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800961 }
Josef Bacik606686e2012-06-04 14:03:51 -0400962 rcu_string_free(device->name);
963 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800964 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500965 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800966 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500967 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400968 }
969
Anand Jain77bdae42014-07-03 18:22:06 +0800970 /*
971 * Unmount does not free the btrfs_device struct but would zero
972 * generation along with most of the other members. So just update
973 * it back. We need it to pick the disk with largest generation
974 * (as above).
975 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200976 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +0800977 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200978 fs_devices->latest_generation = max_t(u64, found_transid,
979 fs_devices->latest_generation);
980 }
Anand Jain77bdae42014-07-03 18:22:06 +0800981
Anand Jainf2788d22018-01-18 22:02:34 +0800982 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
983
Anand Jain9c6d1732018-05-29 14:10:20 +0800984 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800985 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400986}
987
Yan Zhenge4404d62008-12-12 10:03:26 -0500988static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
989{
990 struct btrfs_fs_devices *fs_devices;
991 struct btrfs_device *device;
992 struct btrfs_device *orig_dev;
Anand Jaind2979aa2019-08-27 15:40:45 +0800993 int ret = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -0500994
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200995 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300996 if (IS_ERR(fs_devices))
997 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500998
Miao Xieadbbb862014-09-03 21:35:42 +0800999 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001000 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001001
1002 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001003 struct rcu_string *name;
1004
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001005 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1006 orig_dev->uuid);
Anand Jaind2979aa2019-08-27 15:40:45 +08001007 if (IS_ERR(device)) {
1008 ret = PTR_ERR(device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001009 goto error;
Anand Jaind2979aa2019-08-27 15:40:45 +08001010 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001011
Josef Bacik606686e2012-06-04 14:03:51 -04001012 /*
1013 * This is ok to do without rcu read locked because we hold the
1014 * uuid mutex so nothing we touch in here is going to disappear.
1015 */
Anand Jaine755f782014-06-30 17:12:47 +08001016 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001017 name = rcu_string_strdup(orig_dev->name->str,
1018 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001019 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001020 btrfs_free_device(device);
Anand Jaind2979aa2019-08-27 15:40:45 +08001021 ret = -ENOMEM;
Anand Jaine755f782014-06-30 17:12:47 +08001022 goto error;
1023 }
1024 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001025 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001026
Yan Zhenge4404d62008-12-12 10:03:26 -05001027 list_add(&device->dev_list, &fs_devices->devices);
1028 device->fs_devices = fs_devices;
1029 fs_devices->num_devices++;
1030 }
Miao Xieadbbb862014-09-03 21:35:42 +08001031 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001032 return fs_devices;
1033error:
Miao Xieadbbb862014-09-03 21:35:42 +08001034 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001035 free_fs_devices(fs_devices);
Anand Jaind2979aa2019-08-27 15:40:45 +08001036 return ERR_PTR(ret);
Yan Zhenge4404d62008-12-12 10:03:26 -05001037}
1038
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001039static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,
1040 int step, struct btrfs_device **latest_dev)
Chris Masondfe25022008-05-13 13:46:40 -04001041{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001042 struct btrfs_device *device, *next;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001043
Xiao Guangrong46224702011-04-20 10:08:47 +00001044 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001045 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001046 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001047 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001048 &device->dev_state) &&
Anand Jain998a0672020-05-05 02:58:25 +08001049 !test_bit(BTRFS_DEV_STATE_MISSING,
1050 &device->dev_state) &&
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001051 (!*latest_dev ||
1052 device->generation > (*latest_dev)->generation)) {
1053 *latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001054 }
Yan Zheng2b820322008-11-17 21:11:30 -05001055 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001056 }
Yan Zheng2b820322008-11-17 21:11:30 -05001057
Anand Jaincf89af12020-10-30 06:53:56 +08001058 /*
1059 * We have already validated the presence of BTRFS_DEV_REPLACE_DEVID,
1060 * in btrfs_init_dev_replace() so just continue.
1061 */
1062 if (device->devid == BTRFS_DEV_REPLACE_DEVID)
1063 continue;
1064
Yan Zheng2b820322008-11-17 21:11:30 -05001065 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001066 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001067 device->bdev = NULL;
1068 fs_devices->open_devices--;
1069 }
Anand Jainebbede42017-12-04 12:54:52 +08001070 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001071 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001072 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05001073 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001074 list_del_init(&device->dev_list);
1075 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001076 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001077 }
Yan Zheng2b820322008-11-17 21:11:30 -05001078
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001079}
1080
1081/*
1082 * After we have read the system tree and know devids belonging to this
1083 * filesystem, remove the device which does not belong there.
1084 */
1085void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
1086{
1087 struct btrfs_device *latest_dev = NULL;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001088 struct btrfs_fs_devices *seed_dev;
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001089
1090 mutex_lock(&uuid_mutex);
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001091 __btrfs_free_extra_devids(fs_devices, step, &latest_dev);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001092
1093 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list)
1094 __btrfs_free_extra_devids(seed_dev, step, &latest_dev);
Yan Zheng2b820322008-11-17 21:11:30 -05001095
Miao Xie443f24f2014-07-24 11:37:15 +08001096 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001097
Chris Masondfe25022008-05-13 13:46:40 -04001098 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001099}
Chris Masona0af4692008-05-13 16:03:06 -04001100
Anand Jain14238812016-07-22 06:04:53 +08001101static void btrfs_close_bdev(struct btrfs_device *device)
1102{
David Sterba08ffcae2017-06-19 16:55:35 +02001103 if (!device->bdev)
1104 return;
1105
Anand Jainebbede42017-12-04 12:54:52 +08001106 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001107 sync_blockdev(device->bdev);
1108 invalidate_bdev(device->bdev);
1109 }
1110
David Sterba08ffcae2017-06-19 16:55:35 +02001111 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001112}
1113
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001114static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001115{
1116 struct btrfs_fs_devices *fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001117
Anand Jainebbede42017-12-04 12:54:52 +08001118 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001119 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1120 list_del_init(&device->dev_alloc_list);
1121 fs_devices->rw_devices--;
1122 }
1123
Anand Jaine6e674b2017-12-04 12:54:54 +08001124 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001125 fs_devices->missing_devices--;
1126
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001127 btrfs_close_bdev(device);
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001128 if (device->bdev) {
Johannes Thumshirn3fff3972019-11-26 09:40:05 +01001129 fs_devices->open_devices--;
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001130 device->bdev = NULL;
Anand Jainf448341a2016-06-14 18:55:25 +08001131 }
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001132 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jainf448341a2016-06-14 18:55:25 +08001133
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001134 device->fs_info = NULL;
1135 atomic_set(&device->dev_stats_ccnt, 0);
1136 extent_io_tree_release(&device->alloc_state);
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001137
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001138 /* Verify the device is back in a pristine state */
1139 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1140 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1141 ASSERT(list_empty(&device->dev_alloc_list));
1142 ASSERT(list_empty(&device->post_commit_list));
1143 ASSERT(atomic_read(&device->reada_in_flight) == 0);
Anand Jainf448341a2016-06-14 18:55:25 +08001144}
1145
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001146static void close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001147{
Sasha Levin2037a092015-05-12 19:31:37 -04001148 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001149
Josef Bacik425c6ed2020-08-10 11:42:28 -04001150 lockdep_assert_held(&uuid_mutex);
1151
Yan Zheng2b820322008-11-17 21:11:30 -05001152 if (--fs_devices->opened > 0)
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001153 return;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001154
Josef Bacik425c6ed2020-08-10 11:42:28 -04001155 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list)
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001156 btrfs_close_one_device(device);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001157
Yan Zhenge4404d62008-12-12 10:03:26 -05001158 WARN_ON(fs_devices->open_devices);
1159 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001160 fs_devices->opened = 0;
Johannes Thumshirn0395d842019-11-13 11:27:27 +01001161 fs_devices->seeding = false;
Nikolay Borisovc4989c2f2020-07-15 13:48:49 +03001162 fs_devices->fs_info = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001163}
1164
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001165void btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001166{
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001167 LIST_HEAD(list);
1168 struct btrfs_fs_devices *tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05001169
1170 mutex_lock(&uuid_mutex);
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001171 close_fs_devices(fs_devices);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001172 if (!fs_devices->opened)
1173 list_splice_init(&fs_devices->seed_list, &list);
Yan Zhenge4404d62008-12-12 10:03:26 -05001174
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001175 list_for_each_entry_safe(fs_devices, tmp, &list, seed_list) {
Anand Jain0226e0e2018-04-12 10:29:27 +08001176 close_fs_devices(fs_devices);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001177 list_del(&fs_devices->seed_list);
Yan Zhenge4404d62008-12-12 10:03:26 -05001178 free_fs_devices(fs_devices);
1179 }
Josef Bacik425c6ed2020-08-10 11:42:28 -04001180 mutex_unlock(&uuid_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001181}
1182
Anand Jain897fb572018-04-12 10:29:28 +08001183static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001184 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001185{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001186 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001187 struct btrfs_device *latest_dev = NULL;
Anand Jain96c2e062020-09-30 21:09:52 +08001188 struct btrfs_device *tmp_device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001189
Tejun Heod4d77622010-11-13 11:55:18 +01001190 flags |= FMODE_EXCL;
1191
Anand Jain96c2e062020-09-30 21:09:52 +08001192 list_for_each_entry_safe(device, tmp_device, &fs_devices->devices,
1193 dev_list) {
1194 int ret;
Chris Masona0af4692008-05-13 16:03:06 -04001195
Anand Jain96c2e062020-09-30 21:09:52 +08001196 ret = btrfs_open_one_device(fs_devices, device, flags, holder);
1197 if (ret == 0 &&
1198 (!latest_dev || device->generation > latest_dev->generation)) {
Anand Jain9f050db2017-11-09 23:45:25 +08001199 latest_dev = device;
Anand Jain96c2e062020-09-30 21:09:52 +08001200 } else if (ret == -ENODATA) {
1201 fs_devices->num_devices--;
1202 list_del(&device->dev_list);
1203 btrfs_free_device(device);
1204 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001205 }
Anand Jain1ed802c2020-04-28 23:22:25 +08001206 if (fs_devices->open_devices == 0)
1207 return -EINVAL;
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;
Naohiro Aotac4a816c2020-02-25 12:56:08 +09001212 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
Anand Jain1ed802c2020-04-28 23:22:25 +08001213
1214 return 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001215}
1216
Anand Jainf8e10cd2018-01-22 14:49:36 -08001217static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1218{
1219 struct btrfs_device *dev1, *dev2;
1220
1221 dev1 = list_entry(a, struct btrfs_device, dev_list);
1222 dev2 = list_entry(b, struct btrfs_device, dev_list);
1223
1224 if (dev1->devid < dev2->devid)
1225 return -1;
1226 else if (dev1->devid > dev2->devid)
1227 return 1;
1228 return 0;
1229}
1230
Yan Zheng2b820322008-11-17 21:11:30 -05001231int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001232 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001233{
1234 int ret;
1235
David Sterbaf5194e32018-06-19 17:09:47 +02001236 lockdep_assert_held(&uuid_mutex);
Josef Bacik18c850f2020-07-17 15:12:27 -04001237 /*
1238 * The device_list_mutex cannot be taken here in case opening the
1239 * underlying device takes further locks like bd_mutex.
1240 *
1241 * We also don't need the lock here as this is called during mount and
1242 * exclusion is provided by uuid_mutex
1243 */
David Sterbaf5194e32018-06-19 17:09:47 +02001244
Yan Zheng2b820322008-11-17 21:11:30 -05001245 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001246 fs_devices->opened++;
1247 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001248 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001249 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001250 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001251 }
Anand Jain542c5902018-04-12 10:29:34 +08001252
Chris Mason8a4b83c2008-03-24 15:02:07 -04001253 return ret;
1254}
1255
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001256void btrfs_release_disk_super(struct btrfs_super_block *super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001257{
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001258 struct page *page = virt_to_page(super);
1259
Anand Jain6cf86a002016-02-13 10:01:29 +08001260 put_page(page);
1261}
1262
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001263static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
1264 u64 bytenr)
Anand Jain6cf86a002016-02-13 10:01:29 +08001265{
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001266 struct btrfs_super_block *disk_super;
1267 struct page *page;
Anand Jain6cf86a002016-02-13 10:01:29 +08001268 void *p;
1269 pgoff_t index;
1270
1271 /* make sure our super fits in the device */
1272 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001273 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001274
1275 /* make sure our super fits in the page */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001276 if (sizeof(*disk_super) > PAGE_SIZE)
1277 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001278
1279 /* make sure our super doesn't straddle pages on disk */
1280 index = bytenr >> PAGE_SHIFT;
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001281 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
1282 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001283
1284 /* pull in the page with our super */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001285 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001286
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001287 if (IS_ERR(page))
1288 return ERR_CAST(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001289
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001290 p = page_address(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001291
1292 /* align our pointer to the offset of the super block */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001293 disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001294
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001295 if (btrfs_super_bytenr(disk_super) != bytenr ||
1296 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001297 btrfs_release_disk_super(p);
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001298 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001299 }
1300
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001301 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1])
1302 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0;
Anand Jain6cf86a002016-02-13 10:01:29 +08001303
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001304 return disk_super;
Anand Jain6cf86a002016-02-13 10:01:29 +08001305}
1306
Anand Jain228a73a2019-01-04 13:31:54 +08001307int btrfs_forget_devices(const char *path)
1308{
1309 int ret;
1310
1311 mutex_lock(&uuid_mutex);
1312 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1313 mutex_unlock(&uuid_mutex);
1314
1315 return ret;
1316}
1317
David Sterba6f60cbd2013-02-15 11:31:02 -07001318/*
1319 * Look for a btrfs signature on a device. This may be called out of the mount path
1320 * and we are not allowed to call set_blocksize during the scan. The superblock
1321 * is read via pagecache
1322 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001323struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1324 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001325{
1326 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001327 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001328 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001329 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001330 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001331
David Sterba899f9302018-06-19 16:37:36 +02001332 lockdep_assert_held(&uuid_mutex);
1333
David Sterba6f60cbd2013-02-15 11:31:02 -07001334 /*
1335 * we would like to check all the supers, but that would make
1336 * a btrfs mount succeed after a mkfs from a different FS.
1337 * So, we need to add a special mount option to scan for
1338 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1339 */
1340 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001341 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001342
1343 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001344 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001345 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001346
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001347 disk_super = btrfs_read_disk_super(bdev, bytenr);
1348 if (IS_ERR(disk_super)) {
1349 device = ERR_CAST(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001350 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001351 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001352
Anand Jain4306a972018-05-29 12:28:37 +08001353 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001354 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001355 if (new_device_added)
1356 btrfs_free_stale_devices(path, device);
1357 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001358
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001359 btrfs_release_disk_super(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001360
1361error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001362 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001363
Gu Jinxiang36350e92018-07-12 14:23:16 +08001364 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001365}
Chris Mason0b86a832008-03-24 15:01:56 -04001366
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001367/*
1368 * Try to find a chunk that intersects [start, start + len] range and when one
1369 * such is found, record the end of it in *start
1370 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001371static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1372 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001373{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001374 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001375
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001376 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001377
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001378 if (!find_first_extent_bit(&device->alloc_state, *start,
1379 &physical_start, &physical_end,
1380 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001381
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001382 if (in_range(physical_start, *start, len) ||
1383 in_range(*start, physical_start,
1384 physical_end - physical_start)) {
1385 *start = physical_end + 1;
1386 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001387 }
1388 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001389 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001390}
1391
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001392static u64 dev_extent_search_start(struct btrfs_device *device, u64 start)
1393{
1394 switch (device->fs_devices->chunk_alloc_policy) {
1395 case BTRFS_CHUNK_ALLOC_REGULAR:
1396 /*
1397 * We don't want to overwrite the superblock on the drive nor
1398 * any area used by the boot loader (grub for example), so we
1399 * make sure to start at an offset of at least 1MB.
1400 */
1401 return max_t(u64, start, SZ_1M);
1402 default:
1403 BUG();
1404 }
1405}
1406
1407/**
1408 * dev_extent_hole_check - check if specified hole is suitable for allocation
1409 * @device: the device which we have the hole
1410 * @hole_start: starting position of the hole
1411 * @hole_size: the size of the hole
1412 * @num_bytes: the size of the free space that we need
1413 *
1414 * This function may modify @hole_start and @hole_end to reflect the suitable
1415 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1416 */
1417static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
1418 u64 *hole_size, u64 num_bytes)
1419{
1420 bool changed = false;
1421 u64 hole_end = *hole_start + *hole_size;
1422
1423 /*
1424 * Check before we set max_hole_start, otherwise we could end up
1425 * sending back this offset anyway.
1426 */
1427 if (contains_pending_extent(device, hole_start, *hole_size)) {
1428 if (hole_end >= *hole_start)
1429 *hole_size = hole_end - *hole_start;
1430 else
1431 *hole_size = 0;
1432 changed = true;
1433 }
1434
1435 switch (device->fs_devices->chunk_alloc_policy) {
1436 case BTRFS_CHUNK_ALLOC_REGULAR:
1437 /* No extra check */
1438 break;
1439 default:
1440 BUG();
1441 }
1442
1443 return changed;
1444}
Josef Bacik6df9a952013-06-27 13:22:46 -04001445
Chris Mason0b86a832008-03-24 15:01:56 -04001446/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001447 * find_free_dev_extent_start - find free space in the specified device
1448 * @device: the device which we search the free space in
1449 * @num_bytes: the size of the free space that we need
1450 * @search_start: the position from which to begin the search
1451 * @start: store the start of the free space.
1452 * @len: the size of the free space. that we find, or the size
1453 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001454 *
Chris Mason0b86a832008-03-24 15:01:56 -04001455 * this uses a pretty simple search, the expectation is that it is
1456 * called very infrequently and that a given device has a small number
1457 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001458 *
1459 * @start is used to store the start of the free space if we find. But if we
1460 * don't find suitable free space, it will be used to store the start position
1461 * of the max free space.
1462 *
1463 * @len is used to store the size of the free space that we find.
1464 * But if we don't find suitable free space, it is used to store the size of
1465 * the max free space.
Qu Wenruo135da972019-07-19 14:51:42 +08001466 *
1467 * NOTE: This function will search *commit* root of device tree, and does extra
1468 * check to ensure dev extents are not double allocated.
1469 * This makes the function safe to allocate dev extents but may not report
1470 * correct usable device space, as device extent freed in current transaction
1471 * is not reported as avaiable.
Chris Mason0b86a832008-03-24 15:01:56 -04001472 */
Qu Wenruo9e3246a2019-07-19 14:51:41 +08001473static int find_free_dev_extent_start(struct btrfs_device *device,
1474 u64 num_bytes, u64 search_start, u64 *start,
1475 u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001476{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001477 struct btrfs_fs_info *fs_info = device->fs_info;
1478 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001479 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001480 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001481 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001482 u64 hole_size;
1483 u64 max_hole_start;
1484 u64 max_hole_size;
1485 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001486 u64 search_end = device->total_bytes;
1487 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001488 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001489 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001490
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001491 search_start = dev_extent_search_start(device, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001492
Josef Bacik6df9a952013-06-27 13:22:46 -04001493 path = btrfs_alloc_path();
1494 if (!path)
1495 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001496
Miao Xie7bfc8372011-01-05 10:07:26 +00001497 max_hole_start = search_start;
1498 max_hole_size = 0;
1499
Zhao Leif2ab7612015-02-16 18:52:17 +08001500again:
Anand Jain401e29c2017-12-04 12:54:55 +08001501 if (search_start >= search_end ||
1502 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001503 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001504 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001505 }
1506
David Sterbae4058b52015-11-27 16:31:35 +01001507 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001508 path->search_commit_root = 1;
1509 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001510
Chris Mason0b86a832008-03-24 15:01:56 -04001511 key.objectid = device->devid;
1512 key.offset = search_start;
1513 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001514
Li Zefan125ccb02011-12-08 15:07:24 +08001515 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001516 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001517 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001518 if (ret > 0) {
1519 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1520 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001521 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001522 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001523
Chris Mason0b86a832008-03-24 15:01:56 -04001524 while (1) {
1525 l = path->nodes[0];
1526 slot = path->slots[0];
1527 if (slot >= btrfs_header_nritems(l)) {
1528 ret = btrfs_next_leaf(root, path);
1529 if (ret == 0)
1530 continue;
1531 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001532 goto out;
1533
1534 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001535 }
1536 btrfs_item_key_to_cpu(l, &key, slot);
1537
1538 if (key.objectid < device->devid)
1539 goto next;
1540
1541 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001542 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001543
David Sterba962a2982014-06-04 18:41:45 +02001544 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001545 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001546
Miao Xie7bfc8372011-01-05 10:07:26 +00001547 if (key.offset > search_start) {
1548 hole_size = key.offset - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001549 dev_extent_hole_check(device, &search_start, &hole_size,
1550 num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04001551
Miao Xie7bfc8372011-01-05 10:07:26 +00001552 if (hole_size > max_hole_size) {
1553 max_hole_start = search_start;
1554 max_hole_size = hole_size;
1555 }
1556
1557 /*
1558 * If this free space is greater than which we need,
1559 * it must be the max free space that we have found
1560 * until now, so max_hole_start must point to the start
1561 * of this free space and the length of this free space
1562 * is stored in max_hole_size. Thus, we return
1563 * max_hole_start and max_hole_size and go back to the
1564 * caller.
1565 */
1566 if (hole_size >= num_bytes) {
1567 ret = 0;
1568 goto out;
1569 }
1570 }
1571
Chris Mason0b86a832008-03-24 15:01:56 -04001572 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001573 extent_end = key.offset + btrfs_dev_extent_length(l,
1574 dev_extent);
1575 if (extent_end > search_start)
1576 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001577next:
1578 path->slots[0]++;
1579 cond_resched();
1580 }
Chris Mason0b86a832008-03-24 15:01:56 -04001581
liubo38c01b92011-08-02 02:39:03 +00001582 /*
1583 * At this point, search_start should be the end of
1584 * allocated dev extents, and when shrinking the device,
1585 * search_end may be smaller than search_start.
1586 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001587 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001588 hole_size = search_end - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001589 if (dev_extent_hole_check(device, &search_start, &hole_size,
1590 num_bytes)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001591 btrfs_release_path(path);
1592 goto again;
1593 }
Chris Mason0b86a832008-03-24 15:01:56 -04001594
Zhao Leif2ab7612015-02-16 18:52:17 +08001595 if (hole_size > max_hole_size) {
1596 max_hole_start = search_start;
1597 max_hole_size = hole_size;
1598 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001599 }
1600
Miao Xie7bfc8372011-01-05 10:07:26 +00001601 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001602 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001603 ret = -ENOSPC;
1604 else
1605 ret = 0;
1606
1607out:
Yan Zheng2b820322008-11-17 21:11:30 -05001608 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001609 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001610 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001611 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001612 return ret;
1613}
1614
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001615int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001616 u64 *start, u64 *len)
1617{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001618 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001619 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001620}
1621
Christoph Hellwigb2950862008-12-02 09:54:17 -05001622static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001623 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001624 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001625{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001626 struct btrfs_fs_info *fs_info = device->fs_info;
1627 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001628 int ret;
1629 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001630 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001631 struct btrfs_key found_key;
1632 struct extent_buffer *leaf = NULL;
1633 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001634
1635 path = btrfs_alloc_path();
1636 if (!path)
1637 return -ENOMEM;
1638
1639 key.objectid = device->devid;
1640 key.offset = start;
1641 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001642again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001643 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001644 if (ret > 0) {
1645 ret = btrfs_previous_item(root, path, key.objectid,
1646 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001647 if (ret)
1648 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001649 leaf = path->nodes[0];
1650 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1651 extent = btrfs_item_ptr(leaf, path->slots[0],
1652 struct btrfs_dev_extent);
1653 BUG_ON(found_key.offset > start || found_key.offset +
1654 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001655 key = found_key;
1656 btrfs_release_path(path);
1657 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001658 } else if (ret == 0) {
1659 leaf = path->nodes[0];
1660 extent = btrfs_item_ptr(leaf, path->slots[0],
1661 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001662 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001663 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001664 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001665 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001666
Miao Xie2196d6e2014-09-03 21:35:41 +08001667 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1668
Chris Mason8f18cf12008-04-25 16:53:30 -04001669 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001670 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001671 btrfs_handle_fs_error(fs_info, ret,
1672 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001673 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001674 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001675 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001676out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001677 btrfs_free_path(path);
1678 return ret;
1679}
1680
Eric Sandeen48a3b632013-04-25 20:41:01 +00001681static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1682 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001683 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001684{
1685 int ret;
1686 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001687 struct btrfs_fs_info *fs_info = device->fs_info;
1688 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001689 struct btrfs_dev_extent *extent;
1690 struct extent_buffer *leaf;
1691 struct btrfs_key key;
1692
Anand Jaine12c9622017-12-04 12:54:53 +08001693 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001694 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001695 path = btrfs_alloc_path();
1696 if (!path)
1697 return -ENOMEM;
1698
Chris Mason0b86a832008-03-24 15:01:56 -04001699 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001700 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001701 key.type = BTRFS_DEV_EXTENT_KEY;
1702 ret = btrfs_insert_empty_item(trans, root, path, &key,
1703 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001704 if (ret)
1705 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001706
1707 leaf = path->nodes[0];
1708 extent = btrfs_item_ptr(leaf, path->slots[0],
1709 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001710 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1711 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001712 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1713 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001714 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1715
Chris Mason0b86a832008-03-24 15:01:56 -04001716 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1717 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001718out:
Chris Mason0b86a832008-03-24 15:01:56 -04001719 btrfs_free_path(path);
1720 return ret;
1721}
1722
Josef Bacik6df9a952013-06-27 13:22:46 -04001723static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001724{
Josef Bacik6df9a952013-06-27 13:22:46 -04001725 struct extent_map_tree *em_tree;
1726 struct extent_map *em;
1727 struct rb_node *n;
1728 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001729
David Sterbac8bf1b62019-05-17 11:43:17 +02001730 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001731 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001732 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001733 if (n) {
1734 em = rb_entry(n, struct extent_map, rb_node);
1735 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001736 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001737 read_unlock(&em_tree->lock);
1738
Chris Mason0b86a832008-03-24 15:01:56 -04001739 return ret;
1740}
1741
Ilya Dryomov53f10652013-08-12 14:33:01 +03001742static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1743 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001744{
1745 int ret;
1746 struct btrfs_key key;
1747 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001748 struct btrfs_path *path;
1749
Yan Zheng2b820322008-11-17 21:11:30 -05001750 path = btrfs_alloc_path();
1751 if (!path)
1752 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001753
1754 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1755 key.type = BTRFS_DEV_ITEM_KEY;
1756 key.offset = (u64)-1;
1757
Ilya Dryomov53f10652013-08-12 14:33:01 +03001758 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001759 if (ret < 0)
1760 goto error;
1761
Anand Jaina06dee4d2019-08-27 15:40:44 +08001762 if (ret == 0) {
1763 /* Corruption */
1764 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1765 ret = -EUCLEAN;
1766 goto error;
1767 }
Chris Mason0b86a832008-03-24 15:01:56 -04001768
Ilya Dryomov53f10652013-08-12 14:33:01 +03001769 ret = btrfs_previous_item(fs_info->chunk_root, path,
1770 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001771 BTRFS_DEV_ITEM_KEY);
1772 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001773 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001774 } else {
1775 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1776 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001777 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001778 }
1779 ret = 0;
1780error:
Yan Zheng2b820322008-11-17 21:11:30 -05001781 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001782 return ret;
1783}
1784
1785/*
1786 * the device information is stored in the chunk root
1787 * the btrfs_device struct should be fully filled in
1788 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001789static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001790 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001791{
1792 int ret;
1793 struct btrfs_path *path;
1794 struct btrfs_dev_item *dev_item;
1795 struct extent_buffer *leaf;
1796 struct btrfs_key key;
1797 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001798
Chris Mason0b86a832008-03-24 15:01:56 -04001799 path = btrfs_alloc_path();
1800 if (!path)
1801 return -ENOMEM;
1802
Chris Mason0b86a832008-03-24 15:01:56 -04001803 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1804 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001805 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001806
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001807 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1808 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001809 if (ret)
1810 goto out;
1811
1812 leaf = path->nodes[0];
1813 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1814
1815 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001816 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001817 btrfs_set_device_type(leaf, dev_item, device->type);
1818 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1819 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1820 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001821 btrfs_set_device_total_bytes(leaf, dev_item,
1822 btrfs_device_get_disk_total_bytes(device));
1823 btrfs_set_device_bytes_used(leaf, dev_item,
1824 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001825 btrfs_set_device_group(leaf, dev_item, 0);
1826 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1827 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001828 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001829
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001830 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001831 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001832 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001833 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1834 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001835 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001836
Yan Zheng2b820322008-11-17 21:11:30 -05001837 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001838out:
1839 btrfs_free_path(path);
1840 return ret;
1841}
Chris Mason8f18cf12008-04-25 16:53:30 -04001842
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001843/*
1844 * Function to update ctime/mtime for a given device path.
1845 * Mainly used for ctime/mtime based probe like libblkid.
1846 */
David Sterbada353f62017-02-14 17:55:53 +01001847static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001848{
1849 struct file *filp;
1850
1851 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001852 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001853 return;
1854 file_update_time(filp);
1855 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001856}
1857
David Sterbaf331a952019-03-20 16:31:53 +01001858static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001859{
David Sterbaf331a952019-03-20 16:31:53 +01001860 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001861 int ret;
1862 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001863 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001864 struct btrfs_trans_handle *trans;
1865
Chris Masona061fc82008-05-07 11:43:44 -04001866 path = btrfs_alloc_path();
1867 if (!path)
1868 return -ENOMEM;
1869
Yan, Zhenga22285a2010-05-16 10:48:46 -04001870 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001871 if (IS_ERR(trans)) {
1872 btrfs_free_path(path);
1873 return PTR_ERR(trans);
1874 }
Chris Masona061fc82008-05-07 11:43:44 -04001875 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1876 key.type = BTRFS_DEV_ITEM_KEY;
1877 key.offset = device->devid;
1878
1879 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001880 if (ret) {
1881 if (ret > 0)
1882 ret = -ENOENT;
1883 btrfs_abort_transaction(trans, ret);
1884 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001885 goto out;
1886 }
1887
1888 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001889 if (ret) {
1890 btrfs_abort_transaction(trans, ret);
1891 btrfs_end_transaction(trans);
1892 }
1893
Chris Masona061fc82008-05-07 11:43:44 -04001894out:
1895 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001896 if (!ret)
1897 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001898 return ret;
1899}
1900
David Sterba3cc31a02016-02-15 16:00:26 +01001901/*
1902 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1903 * filesystem. It's up to the caller to adjust that number regarding eg. device
1904 * replace.
1905 */
1906static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1907 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001908{
Chris Masona061fc82008-05-07 11:43:44 -04001909 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001910 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001911 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001912
Miao Xiede98ced2013-01-29 10:13:12 +00001913 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001914 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001915
Anand Jainbd45ffb2016-02-13 10:01:34 +08001916 all_avail = fs_info->avail_data_alloc_bits |
1917 fs_info->avail_system_alloc_bits |
1918 fs_info->avail_metadata_alloc_bits;
1919 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001920
David Sterba418775a2016-02-15 16:28:14 +01001921 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001922 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001923 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001924
David Sterba418775a2016-02-15 16:28:14 +01001925 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001926 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001927
David Sterba418775a2016-02-15 16:28:14 +01001928 if (ret)
1929 return ret;
1930 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001931 }
1932
1933 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001934}
1935
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001936static struct btrfs_device * btrfs_find_next_active_device(
1937 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001938{
1939 struct btrfs_device *next_device;
1940
1941 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1942 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001943 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1944 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001945 return next_device;
1946 }
1947
1948 return NULL;
1949}
1950
1951/*
1952 * Helper function to check if the given device is part of s_bdev / latest_bdev
1953 * and replace it with the provided or the next active device, in the context
1954 * where this function called, there should be always be another device (or
1955 * this_dev) which is active.
1956 */
David Sterbab105e922019-10-01 19:57:35 +02001957void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
Anand Jaine493e8f2020-09-05 01:34:34 +08001958 struct btrfs_device *next_device)
Anand Jain88acff62016-05-03 17:44:43 +08001959{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001960 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08001961
Anand Jaine493e8f2020-09-05 01:34:34 +08001962 if (!next_device)
Anand Jain88acff62016-05-03 17:44:43 +08001963 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
Anand Jaine493e8f2020-09-05 01:34:34 +08001964 device);
Anand Jain88acff62016-05-03 17:44:43 +08001965 ASSERT(next_device);
1966
1967 if (fs_info->sb->s_bdev &&
1968 (fs_info->sb->s_bdev == device->bdev))
1969 fs_info->sb->s_bdev = next_device->bdev;
1970
1971 if (fs_info->fs_devices->latest_bdev == device->bdev)
1972 fs_info->fs_devices->latest_bdev = next_device->bdev;
1973}
1974
Anand Jain1da73962018-08-10 13:53:21 +08001975/*
1976 * Return btrfs_fs_devices::num_devices excluding the device that's being
1977 * currently replaced.
1978 */
1979static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1980{
1981 u64 num_devices = fs_info->fs_devices->num_devices;
1982
David Sterbacb5583d2018-09-07 16:11:23 +02001983 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001984 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1985 ASSERT(num_devices > 1);
1986 num_devices--;
1987 }
David Sterbacb5583d2018-09-07 16:11:23 +02001988 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001989
1990 return num_devices;
1991}
1992
Josef Bacik313b0852020-08-20 11:18:26 -04001993void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
1994 struct block_device *bdev,
1995 const char *device_path)
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09001996{
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09001997 struct btrfs_super_block *disk_super;
1998 int copy_num;
1999
2000 if (!bdev)
2001 return;
2002
2003 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002004 struct page *page;
2005 int ret;
2006
2007 disk_super = btrfs_read_dev_one_super(bdev, copy_num);
2008 if (IS_ERR(disk_super))
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002009 continue;
2010
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002011 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002012
2013 page = virt_to_page(disk_super);
2014 set_page_dirty(page);
2015 lock_page(page);
2016 /* write_on_page() unlocks the page */
2017 ret = write_one_page(page);
2018 if (ret)
2019 btrfs_warn(fs_info,
2020 "error clearing superblock number %d (%d)",
2021 copy_num, ret);
2022 btrfs_release_disk_super(disk_super);
2023
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002024 }
2025
2026 /* Notify udev that device has changed */
2027 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
2028
2029 /* Update ctime/mtime for device path for libblkid */
2030 update_dev_time(device_path);
2031}
2032
David Sterbada353f62017-02-14 17:55:53 +01002033int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
Anand Jain53f8a742020-09-05 01:34:27 +08002034 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002035{
2036 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002037 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002038 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002039 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002040 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002041
2042 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002043
Anand Jain1da73962018-08-10 13:53:21 +08002044 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002045
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002046 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002047 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002048 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002049
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002050 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2051
2052 if (IS_ERR(device)) {
2053 if (PTR_ERR(device) == -ENOENT &&
2054 strcmp(device_path, "missing") == 0)
2055 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2056 else
2057 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002058 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002059 }
Yan Zheng2b820322008-11-17 21:11:30 -05002060
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002061 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2062 btrfs_warn_in_rcu(fs_info,
2063 "cannot remove device %s (devid %llu) due to active swapfile",
2064 rcu_str_deref(device->name), device->devid);
2065 ret = -ETXTBSY;
2066 goto out;
2067 }
2068
Anand Jain401e29c2017-12-04 12:54:55 +08002069 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002070 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002071 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002072 }
2073
Anand Jainebbede42017-12-04 12:54:52 +08002074 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2075 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002076 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002077 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002078 }
2079
Anand Jainebbede42017-12-04 12:54:52 +08002080 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002081 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002082 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002083 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002084 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002085 }
Chris Masona061fc82008-05-07 11:43:44 -04002086
Carey Underwoodd7901552013-03-04 16:37:06 -06002087 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002088 ret = btrfs_shrink_device(device, 0);
Filipe Manana66d204a2020-10-12 11:55:24 +01002089 if (!ret)
2090 btrfs_reada_remove_dev(device);
Carey Underwoodd7901552013-03-04 16:37:06 -06002091 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002092 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002093 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002094
Stefan Behrens63a212a2012-11-05 18:29:28 +01002095 /*
2096 * TODO: the superblock still includes this device in its num_devices
2097 * counter although write_all_supers() is not locked out. This
2098 * could give a filesystem state which requires a degraded mount.
2099 */
David Sterbaf331a952019-03-20 16:31:53 +01002100 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002101 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002102 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002103
Anand Jaine12c9622017-12-04 12:54:53 +08002104 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002105 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002106
2107 /*
2108 * the device list mutex makes sure that we don't change
2109 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002110 * the device supers. Whoever is writing all supers, should
2111 * lock the device list mutex before getting the number of
2112 * devices in the super block (super_copy). Conversely,
2113 * whoever updates the number of devices in the super block
2114 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002115 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002116
Anand Jain41a52a02018-04-12 10:29:31 +08002117 /*
2118 * In normal cases the cur_devices == fs_devices. But in case
2119 * of deleting a seed device, the cur_devices should point to
2120 * its own fs_devices listed under the fs_devices->seed.
2121 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002122 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002123 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002124 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002125
Anand Jain41a52a02018-04-12 10:29:31 +08002126 cur_devices->num_devices--;
2127 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002128 /* Update total_devices of the parent fs_devices if it's seed */
2129 if (cur_devices != fs_devices)
2130 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002131
Anand Jaine6e674b2017-12-04 12:54:54 +08002132 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002133 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002134
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002135 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002136
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002137 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002138 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002139 /* remove sysfs entry */
Anand Jain53f8a742020-09-05 01:34:27 +08002140 btrfs_sysfs_remove_device(device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002141 }
Anand Jain99994cd2014-06-03 11:36:00 +08002142
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002143 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2144 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002145 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002146
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002147 /*
2148 * at this point, the device is zero sized and detached from
2149 * the devices list. All that's left is to zero out the old
2150 * supers and free the device.
2151 */
Anand Jainebbede42017-12-04 12:54:52 +08002152 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002153 btrfs_scratch_superblocks(fs_info, device->bdev,
2154 device->name->str);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002155
2156 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002157 synchronize_rcu();
2158 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002159
Xiao Guangrong1f781602011-04-20 10:09:16 +00002160 if (cur_devices->open_devices == 0) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002161 list_del_init(&cur_devices->seed_list);
Anand Jain0226e0e2018-04-12 10:29:27 +08002162 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002163 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002164 }
2165
Chris Masona061fc82008-05-07 11:43:44 -04002166out:
2167 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002168 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002169
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002170error_undo:
Filipe Manana66d204a2020-10-12 11:55:24 +01002171 btrfs_reada_undo_remove_dev(device);
Anand Jainebbede42017-12-04 12:54:52 +08002172 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002173 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002174 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002175 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002176 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002177 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002178 }
Anand Jain24fc5722016-02-13 10:01:36 +08002179 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002180}
2181
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002182void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002183{
Anand Jaind51908c2014-08-13 14:24:19 +08002184 struct btrfs_fs_devices *fs_devices;
2185
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002186 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002187
Anand Jain25e8e912014-08-20 10:56:56 +08002188 /*
2189 * in case of fs with no seed, srcdev->fs_devices will point
2190 * to fs_devices of fs_info. However when the dev being replaced is
2191 * a seed dev it will point to the seed's local fs_devices. In short
2192 * srcdev will have its correct fs_devices in both the cases.
2193 */
2194 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002195
Stefan Behrense93c89c2012-11-05 17:33:06 +01002196 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002197 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002198 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002199 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002200 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002201
Anand Jainebbede42017-12-04 12:54:52 +08002202 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002203 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002204
Miao Xie82372bc2014-09-03 21:35:44 +08002205 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002206 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002207}
2208
David Sterba65237ee2019-03-20 16:34:54 +01002209void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002210{
2211 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002212
Josef Bacika466c852020-08-20 11:18:27 -04002213 mutex_lock(&uuid_mutex);
2214
Anand Jain14238812016-07-22 06:04:53 +08002215 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002216 synchronize_rcu();
2217 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002218
Anand Jain94d5f0c2014-08-13 14:24:22 +08002219 /* if this is no devs we rather delete the fs_devices */
2220 if (!fs_devices->num_devices) {
Anand Jain6dd38f82017-10-17 06:53:50 +08002221 /*
2222 * On a mounted FS, num_devices can't be zero unless it's a
2223 * seed. In case of a seed device being replaced, the replace
2224 * target added to the sprout FS, so there will be no more
2225 * device left under the seed FS.
2226 */
2227 ASSERT(fs_devices->seeding);
2228
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002229 list_del_init(&fs_devices->seed_list);
Anand Jain0226e0e2018-04-12 10:29:27 +08002230 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002231 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002232 }
Josef Bacika466c852020-08-20 11:18:27 -04002233 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002234}
2235
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002236void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002237{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002238 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002239
Anand Jaind9a071f2018-04-12 10:29:38 +08002240 mutex_lock(&fs_devices->device_list_mutex);
2241
Anand Jain53f8a742020-09-05 01:34:27 +08002242 btrfs_sysfs_remove_device(tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002243
Anand Jain779bf3fe2016-04-18 16:51:23 +08002244 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002245 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002246
Anand Jaind9a071f2018-04-12 10:29:38 +08002247 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002248
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002249 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002250
Stefan Behrense93c89c2012-11-05 17:33:06 +01002251 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002252
Anand Jaind9a071f2018-04-12 10:29:38 +08002253 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002254
2255 /*
2256 * The update_dev_time() with in btrfs_scratch_superblocks()
2257 * may lead to a call to btrfs_show_devname() which will try
2258 * to hold device_list_mutex. And here this device
2259 * is already out of device list, so we don't have to hold
2260 * the device_list_mutex lock.
2261 */
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002262 btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev,
2263 tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002264
2265 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002266 synchronize_rcu();
2267 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002268}
2269
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002270static struct btrfs_device *btrfs_find_device_by_path(
2271 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002272{
2273 int ret = 0;
2274 struct btrfs_super_block *disk_super;
2275 u64 devid;
2276 u8 *dev_uuid;
2277 struct block_device *bdev;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002278 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002279
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002280 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002281 fs_info->bdev_holder, 0, &bdev, &disk_super);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002282 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002283 return ERR_PTR(ret);
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002284
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002285 devid = btrfs_stack_device_id(&disk_super->dev_item);
2286 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002287 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002288 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002289 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002290 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002291 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002292 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002293
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002294 btrfs_release_disk_super(disk_super);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002295 if (!device)
2296 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002297 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002298 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002299}
2300
Yan Zheng2b820322008-11-17 21:11:30 -05002301/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002302 * Lookup a device given by device id, or the path if the id is 0.
2303 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002304struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002305 struct btrfs_fs_info *fs_info, u64 devid,
2306 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002307{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002308 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002309
David Sterba5c5c0df2016-02-15 16:39:55 +01002310 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002311 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002312 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002313 if (!device)
2314 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002315 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002316 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002317
2318 if (!device_path || !device_path[0])
2319 return ERR_PTR(-EINVAL);
2320
2321 if (strcmp(device_path, "missing") == 0) {
2322 /* Find first missing device */
2323 list_for_each_entry(device, &fs_info->fs_devices->devices,
2324 dev_list) {
2325 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2326 &device->dev_state) && !device->bdev)
2327 return device;
2328 }
2329 return ERR_PTR(-ENOENT);
2330 }
2331
2332 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002333}
2334
Yan Zheng2b820322008-11-17 21:11:30 -05002335/*
2336 * does all the dirty work required for changing file system's UUID.
2337 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002338static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002340 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002341 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002342 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002343 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002344 struct btrfs_device *device;
2345 u64 super_flags;
2346
David Sterbaa32bf9a2018-03-16 02:21:22 +01002347 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002348 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002349 return -EINVAL;
2350
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03002351 /*
2352 * Private copy of the seed devices, anchored at
2353 * fs_info->fs_devices->seed_list
2354 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002355 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002356 if (IS_ERR(seed_devices))
2357 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002358
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03002359 /*
2360 * It's necessary to retain a copy of the original seed fs_devices in
2361 * fs_uuids so that filesystems which have been seeded can successfully
2362 * reference the seed device from open_seed_devices. This also supports
2363 * multiple fs seed.
2364 */
Yan Zhenge4404d62008-12-12 10:03:26 -05002365 old_devices = clone_fs_devices(fs_devices);
2366 if (IS_ERR(old_devices)) {
2367 kfree(seed_devices);
2368 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002369 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002370
Anand Jainc4babc52018-04-12 10:29:25 +08002371 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002372
Yan Zhenge4404d62008-12-12 10:03:26 -05002373 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2374 seed_devices->opened = 1;
2375 INIT_LIST_HEAD(&seed_devices->devices);
2376 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002377 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002378
Anand Jain321a4bf2018-07-16 22:58:09 +08002379 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002380 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2381 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002382 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002383 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002384
Johannes Thumshirn0395d842019-11-13 11:27:27 +01002385 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002386 fs_devices->num_devices = 0;
2387 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002388 fs_devices->missing_devices = 0;
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002389 fs_devices->rotating = false;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002390 list_add(&seed_devices->seed_list, &fs_devices->seed_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002391
2392 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002393 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002394 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002395 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002396
Yan Zheng2b820322008-11-17 21:11:30 -05002397 super_flags = btrfs_super_flags(disk_super) &
2398 ~BTRFS_SUPER_FLAG_SEEDING;
2399 btrfs_set_super_flags(disk_super, super_flags);
2400
2401 return 0;
2402}
2403
2404/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002405 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002406 */
David Sterba5c4666292019-03-20 16:36:39 +01002407static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002408{
David Sterba5c4666292019-03-20 16:36:39 +01002409 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002410 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002411 struct btrfs_path *path;
2412 struct extent_buffer *leaf;
2413 struct btrfs_dev_item *dev_item;
2414 struct btrfs_device *device;
2415 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002416 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002417 u8 dev_uuid[BTRFS_UUID_SIZE];
2418 u64 devid;
2419 int ret;
2420
2421 path = btrfs_alloc_path();
2422 if (!path)
2423 return -ENOMEM;
2424
Yan Zheng2b820322008-11-17 21:11:30 -05002425 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2426 key.offset = 0;
2427 key.type = BTRFS_DEV_ITEM_KEY;
2428
2429 while (1) {
2430 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2431 if (ret < 0)
2432 goto error;
2433
2434 leaf = path->nodes[0];
2435next_slot:
2436 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2437 ret = btrfs_next_leaf(root, path);
2438 if (ret > 0)
2439 break;
2440 if (ret < 0)
2441 goto error;
2442 leaf = path->nodes[0];
2443 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002444 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002445 continue;
2446 }
2447
2448 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2449 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2450 key.type != BTRFS_DEV_ITEM_KEY)
2451 break;
2452
2453 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2454 struct btrfs_dev_item);
2455 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002456 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002457 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002458 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002459 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002460 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002461 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002462 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002463
2464 if (device->fs_devices->seeding) {
2465 btrfs_set_device_generation(leaf, dev_item,
2466 device->generation);
2467 btrfs_mark_buffer_dirty(leaf);
2468 }
2469
2470 path->slots[0]++;
2471 goto next_slot;
2472 }
2473 ret = 0;
2474error:
2475 btrfs_free_path(path);
2476 return ret;
2477}
2478
David Sterbada353f62017-02-14 17:55:53 +01002479int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002480{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002481 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002482 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002483 struct btrfs_trans_handle *trans;
2484 struct btrfs_device *device;
2485 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002487 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002488 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002489 u64 orig_super_total_bytes;
2490 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002491 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002492 int ret = 0;
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002493 bool locked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002494
Anand Jain5da54bc2018-07-03 13:14:50 +08002495 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002496 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002497
Li Zefana5d163332011-12-07 20:08:40 -05002498 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002500 if (IS_ERR(bdev))
2501 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002502
Anand Jain5da54bc2018-07-03 13:14:50 +08002503 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002504 seeding_dev = 1;
2505 down_write(&sb->s_umount);
2506 mutex_lock(&uuid_mutex);
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002507 locked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002508 }
2509
Nikolay Borisovb9ba0172020-07-22 11:09:25 +03002510 sync_blockdev(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002511
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002512 rcu_read_lock();
2513 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002514 if (device->bdev == bdev) {
2515 ret = -EEXIST;
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002516 rcu_read_unlock();
Yan Zheng2b820322008-11-17 21:11:30 -05002517 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002518 }
2519 }
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002520 rcu_read_unlock();
Chris Mason788f20e2008-04-28 15:29:42 -04002521
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002522 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002523 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002524 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002525 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002526 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002527 }
2528
David Sterba78f2c9e2016-02-11 14:25:38 +01002529 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002530 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002531 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002532 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002533 }
Josef Bacik606686e2012-06-04 14:03:51 -04002534 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002535
Yan, Zhenga22285a2010-05-16 10:48:46 -04002536 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002537 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002538 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002539 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002540 }
2541
Josef Bacikd5e20032011-08-04 14:52:27 +00002542 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002543 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002544 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002545 device->io_width = fs_info->sectorsize;
2546 device->io_align = fs_info->sectorsize;
2547 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002548 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2549 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002550 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002551 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002552 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002553 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002554 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002555 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002556 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002557 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002558 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002559
Yan Zheng2b820322008-11-17 21:11:30 -05002560 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002561 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002562 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002563 if (ret) {
2564 btrfs_abort_transaction(trans, ret);
2565 goto error_trans;
2566 }
Yan Zheng2b820322008-11-17 21:11:30 -05002567 }
2568
Anand Jain5da54bc2018-07-03 13:14:50 +08002569 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002570
Anand Jain5da54bc2018-07-03 13:14:50 +08002571 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002572 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002573 list_add_rcu(&device->dev_list, &fs_devices->devices);
2574 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2575 fs_devices->num_devices++;
2576 fs_devices->open_devices++;
2577 fs_devices->rw_devices++;
2578 fs_devices->total_devices++;
2579 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002580
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002581 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002582
Anand Jaine884f4f2017-04-04 18:40:19 +08002583 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002584 fs_devices->rotating = true;
Chris Masonc2898112009-06-10 09:51:32 -04002585
Naohiro Aota39379fa2018-07-27 09:04:55 +09002586 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002587 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002588 round_down(orig_super_total_bytes + device->total_bytes,
2589 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002590
Naohiro Aota39379fa2018-07-27 09:04:55 +09002591 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2592 btrfs_set_super_num_devices(fs_info->super_copy,
2593 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002594
Miao Xie2196d6e2014-09-03 21:35:41 +08002595 /*
2596 * we've got more storage, clear any full flags on the space
2597 * infos
2598 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002599 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002600
David Sterba34441362016-10-04 19:34:27 +02002601 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacikca108452020-09-01 08:09:01 -04002602
2603 /* Add sysfs device entry */
Anand Jaincd36da22020-09-05 01:34:26 +08002604 btrfs_sysfs_add_device(device);
Josef Bacikca108452020-09-01 08:09:01 -04002605
Anand Jain5da54bc2018-07-03 13:14:50 +08002606 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002607
Yan Zheng2b820322008-11-17 21:11:30 -05002608 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002609 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002610 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002611 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002612 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002613 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002614 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002615 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002616 }
2617
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002618 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002619 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002620 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002621 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002622 }
2623
2624 if (seeding_dev) {
David Sterba5c4666292019-03-20 16:36:39 +01002625 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002626 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002627 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002628 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002629 }
Anand Jainb2373f22014-06-03 11:36:03 +08002630
Nikolay Borisov8e560082020-08-12 16:18:51 +03002631 /*
2632 * fs_devices now represents the newly sprouted filesystem and
2633 * its fsid has been changed by btrfs_prepare_sprout
2634 */
2635 btrfs_sysfs_update_sprout_fsid(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002636 }
2637
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002638 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002639
2640 if (seeding_dev) {
2641 mutex_unlock(&uuid_mutex);
2642 up_write(&sb->s_umount);
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002643 locked = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002644
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002645 if (ret) /* transaction commit */
2646 return ret;
2647
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002648 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002649 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002651 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002652 trans = btrfs_attach_transaction(root);
2653 if (IS_ERR(trans)) {
2654 if (PTR_ERR(trans) == -ENOENT)
2655 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002656 ret = PTR_ERR(trans);
2657 trans = NULL;
2658 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002659 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002660 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002661 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002662
Anand Jain7f551d92020-05-05 02:58:26 +08002663 /*
2664 * Now that we have written a new super block to this device, check all
2665 * other fs_devices list if device_path alienates any other scanned
2666 * device.
2667 * We can ignore the return value as it typically returns -EINVAL and
2668 * only succeeds if the device was an alien.
2669 */
2670 btrfs_forget_devices(device_path);
2671
2672 /* Update ctime/mtime for blkid or udev */
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002673 update_dev_time(device_path);
Anand Jain7f551d92020-05-05 02:58:26 +08002674
Chris Mason788f20e2008-04-28 15:29:42 -04002675 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676
Anand Jaind31c32f2017-09-28 14:51:10 +08002677error_sysfs:
Anand Jain53f8a742020-09-05 01:34:27 +08002678 btrfs_sysfs_remove_device(device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002679 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2680 mutex_lock(&fs_info->chunk_mutex);
2681 list_del_rcu(&device->dev_list);
2682 list_del(&device->dev_alloc_list);
2683 fs_info->fs_devices->num_devices--;
2684 fs_info->fs_devices->open_devices--;
2685 fs_info->fs_devices->rw_devices--;
2686 fs_info->fs_devices->total_devices--;
2687 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2688 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2689 btrfs_set_super_total_bytes(fs_info->super_copy,
2690 orig_super_total_bytes);
2691 btrfs_set_super_num_devices(fs_info->super_copy,
2692 orig_super_num_devices);
2693 mutex_unlock(&fs_info->chunk_mutex);
2694 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002695error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002696 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002697 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002698 if (trans)
2699 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002700error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002701 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002702error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002703 blkdev_put(bdev, FMODE_EXCL);
Nikolay Borisov44cab9b2020-07-22 11:09:23 +03002704 if (locked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002705 mutex_unlock(&uuid_mutex);
2706 up_write(&sb->s_umount);
2707 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002708 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002709}
2710
Chris Masond3977122009-01-05 21:25:51 -05002711static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2712 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002713{
2714 int ret;
2715 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002716 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002717 struct btrfs_dev_item *dev_item;
2718 struct extent_buffer *leaf;
2719 struct btrfs_key key;
2720
Chris Mason0b86a832008-03-24 15:01:56 -04002721 path = btrfs_alloc_path();
2722 if (!path)
2723 return -ENOMEM;
2724
2725 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2726 key.type = BTRFS_DEV_ITEM_KEY;
2727 key.offset = device->devid;
2728
2729 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2730 if (ret < 0)
2731 goto out;
2732
2733 if (ret > 0) {
2734 ret = -ENOENT;
2735 goto out;
2736 }
2737
2738 leaf = path->nodes[0];
2739 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2740
2741 btrfs_set_device_id(leaf, dev_item, device->devid);
2742 btrfs_set_device_type(leaf, dev_item, device->type);
2743 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2744 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2745 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002746 btrfs_set_device_total_bytes(leaf, dev_item,
2747 btrfs_device_get_disk_total_bytes(device));
2748 btrfs_set_device_bytes_used(leaf, dev_item,
2749 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002750 btrfs_mark_buffer_dirty(leaf);
2751
2752out:
2753 btrfs_free_path(path);
2754 return ret;
2755}
2756
Miao Xie2196d6e2014-09-03 21:35:41 +08002757int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002758 struct btrfs_device *device, u64 new_size)
2759{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002760 struct btrfs_fs_info *fs_info = device->fs_info;
2761 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002762 u64 old_total;
2763 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002764
Anand Jainebbede42017-12-04 12:54:52 +08002765 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002766 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002767
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002768 new_size = round_down(new_size, fs_info->sectorsize);
2769
David Sterba34441362016-10-04 19:34:27 +02002770 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002771 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002772 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002773
Stefan Behrens63a212a2012-11-05 18:29:28 +01002774 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002775 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002776 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002777 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002778 }
Yan Zheng2b820322008-11-17 21:11:30 -05002779
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002780 btrfs_set_super_total_bytes(super_copy,
2781 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002782 device->fs_devices->total_rw_bytes += diff;
2783
Miao Xie7cc8e582014-09-03 21:35:38 +08002784 btrfs_device_set_total_bytes(device, new_size);
2785 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002786 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002787 if (list_empty(&device->post_commit_list))
2788 list_add_tail(&device->post_commit_list,
2789 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002790 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002791
Chris Mason8f18cf12008-04-25 16:53:30 -04002792 return btrfs_update_device(trans, device);
2793}
2794
Nikolay Borisovf4208792018-07-20 19:37:52 +03002795static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002796{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002797 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002798 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002799 int ret;
2800 struct btrfs_path *path;
2801 struct btrfs_key key;
2802
Chris Mason8f18cf12008-04-25 16:53:30 -04002803 path = btrfs_alloc_path();
2804 if (!path)
2805 return -ENOMEM;
2806
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002807 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002808 key.offset = chunk_offset;
2809 key.type = BTRFS_CHUNK_ITEM_KEY;
2810
2811 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002812 if (ret < 0)
2813 goto out;
2814 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002815 btrfs_handle_fs_error(fs_info, -ENOENT,
2816 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002817 ret = -ENOENT;
2818 goto out;
2819 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002820
2821 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002822 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002823 btrfs_handle_fs_error(fs_info, ret,
2824 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002825out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002826 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002827 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002828}
2829
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002830static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002831{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002832 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002833 struct btrfs_disk_key *disk_key;
2834 struct btrfs_chunk *chunk;
2835 u8 *ptr;
2836 int ret = 0;
2837 u32 num_stripes;
2838 u32 array_size;
2839 u32 len = 0;
2840 u32 cur;
2841 struct btrfs_key key;
2842
David Sterba34441362016-10-04 19:34:27 +02002843 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002844 array_size = btrfs_super_sys_array_size(super_copy);
2845
2846 ptr = super_copy->sys_chunk_array;
2847 cur = 0;
2848
2849 while (cur < array_size) {
2850 disk_key = (struct btrfs_disk_key *)ptr;
2851 btrfs_disk_key_to_cpu(&key, disk_key);
2852
2853 len = sizeof(*disk_key);
2854
2855 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2856 chunk = (struct btrfs_chunk *)(ptr + len);
2857 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2858 len += btrfs_chunk_item_size(num_stripes);
2859 } else {
2860 ret = -EIO;
2861 break;
2862 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002863 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002864 key.offset == chunk_offset) {
2865 memmove(ptr, ptr + len, array_size - (cur + len));
2866 array_size -= len;
2867 btrfs_set_super_sys_array_size(super_copy, array_size);
2868 } else {
2869 ptr += len;
2870 cur += len;
2871 }
2872 }
David Sterba34441362016-10-04 19:34:27 +02002873 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002874 return ret;
2875}
2876
Omar Sandoval60ca8422018-05-16 16:34:31 -07002877/*
2878 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2879 * @logical: Logical block offset in bytes.
2880 * @length: Length of extent in bytes.
2881 *
2882 * Return: Chunk mapping or ERR_PTR.
2883 */
2884struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2885 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002886{
2887 struct extent_map_tree *em_tree;
2888 struct extent_map *em;
2889
David Sterbac8bf1b62019-05-17 11:43:17 +02002890 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002891 read_lock(&em_tree->lock);
2892 em = lookup_extent_mapping(em_tree, logical, length);
2893 read_unlock(&em_tree->lock);
2894
2895 if (!em) {
2896 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2897 logical, length);
2898 return ERR_PTR(-EINVAL);
2899 }
2900
2901 if (em->start > logical || em->start + em->len < logical) {
2902 btrfs_crit(fs_info,
2903 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2904 logical, length, em->start, em->start + em->len);
2905 free_extent_map(em);
2906 return ERR_PTR(-EINVAL);
2907 }
2908
2909 /* callers are responsible for dropping em's ref. */
2910 return em;
2911}
2912
Nikolay Borisov97aff912018-07-20 19:37:53 +03002913int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002914{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002915 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002916 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002917 struct map_lookup *map;
2918 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002919 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002920 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002921
Omar Sandoval60ca8422018-05-16 16:34:31 -07002922 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002923 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002924 /*
2925 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002926 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002927 * do anything we still error out.
2928 */
2929 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002930 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002931 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002932 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002933 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002934 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002935 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002936
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002937 /*
2938 * Take the device list mutex to prevent races with the final phase of
2939 * a device replace operation that replaces the device object associated
2940 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2941 */
2942 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002943 for (i = 0; i < map->num_stripes; i++) {
2944 struct btrfs_device *device = map->stripes[i].dev;
2945 ret = btrfs_free_dev_extent(trans, device,
2946 map->stripes[i].physical,
2947 &dev_extent_len);
2948 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002949 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002950 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002951 goto out;
2952 }
2953
2954 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002955 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002956 btrfs_device_set_bytes_used(device,
2957 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002958 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002959 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002960 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002961 }
2962
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03002963 ret = btrfs_update_device(trans, device);
2964 if (ret) {
2965 mutex_unlock(&fs_devices->device_list_mutex);
2966 btrfs_abort_transaction(trans, ret);
2967 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002968 }
2969 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002970 mutex_unlock(&fs_devices->device_list_mutex);
2971
Nikolay Borisovf4208792018-07-20 19:37:52 +03002972 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002973 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002974 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002975 goto out;
2976 }
2977
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002978 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002979
2980 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002981 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002982 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002983 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002984 goto out;
2985 }
2986 }
2987
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03002988 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002989 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002990 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002991 goto out;
2992 }
2993
Josef Bacik47ab2a62014-09-18 11:20:02 -04002994out:
2995 /* once for us */
2996 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002997 return ret;
2998}
2999
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003000static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003001{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003002 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003003 struct btrfs_trans_handle *trans;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003004 struct btrfs_block_group *block_group;
Chris Mason8f18cf12008-04-25 16:53:30 -04003005 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003006
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003007 /*
3008 * Prevent races with automatic removal of unused block groups.
3009 * After we relocate and before we remove the chunk with offset
3010 * chunk_offset, automatic removal of the block group can kick in,
3011 * resulting in a failure when calling btrfs_remove_chunk() below.
3012 *
3013 * Make sure to acquire this mutex before doing a tree search (dev
3014 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3015 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3016 * we release the path used to search the chunk/dev tree and before
3017 * the current task acquires this mutex and calls us.
3018 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003019 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003020
Chris Mason8f18cf12008-04-25 16:53:30 -04003021 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003022 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003023 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003024 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003025 if (ret)
3026 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003027
Dennis Zhoub0643e52019-12-13 16:22:14 -08003028 block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
3029 if (!block_group)
3030 return -ENOENT;
3031 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
3032 btrfs_put_block_group(block_group);
3033
Chris Mason19c4d2f2016-10-10 13:43:31 -07003034 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3035 chunk_offset);
3036 if (IS_ERR(trans)) {
3037 ret = PTR_ERR(trans);
3038 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3039 return ret;
3040 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003041
Chris Mason19c4d2f2016-10-10 13:43:31 -07003042 /*
3043 * step two, delete the device extents and the
3044 * chunk tree entries
3045 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003046 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003047 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003048 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003049}
3050
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003051static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003052{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003053 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003054 struct btrfs_path *path;
3055 struct extent_buffer *leaf;
3056 struct btrfs_chunk *chunk;
3057 struct btrfs_key key;
3058 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003059 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003060 bool retried = false;
3061 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003062 int ret;
3063
3064 path = btrfs_alloc_path();
3065 if (!path)
3066 return -ENOMEM;
3067
Josef Bacikba1bf482009-09-11 16:11:19 -04003068again:
Yan Zheng2b820322008-11-17 21:11:30 -05003069 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3070 key.offset = (u64)-1;
3071 key.type = BTRFS_CHUNK_ITEM_KEY;
3072
3073 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003074 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003075 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003076 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003077 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003078 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003079 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003080 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003081
3082 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3083 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003084 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003085 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003086 if (ret < 0)
3087 goto error;
3088 if (ret > 0)
3089 break;
3090
3091 leaf = path->nodes[0];
3092 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3093
3094 chunk = btrfs_item_ptr(leaf, path->slots[0],
3095 struct btrfs_chunk);
3096 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003097 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003098
3099 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003100 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003101 if (ret == -ENOSPC)
3102 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303103 else
3104 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003105 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003106 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003107
3108 if (found_key.offset == 0)
3109 break;
3110 key.offset = found_key.offset - 1;
3111 }
3112 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003113 if (failed && !retried) {
3114 failed = 0;
3115 retried = true;
3116 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303117 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003118 ret = -ENOSPC;
3119 }
Yan Zheng2b820322008-11-17 21:11:30 -05003120error:
3121 btrfs_free_path(path);
3122 return ret;
3123}
3124
Liu Boa6f93c72017-11-15 16:28:11 -07003125/*
3126 * return 1 : allocate a data chunk successfully,
3127 * return <0: errors during allocating a data chunk,
3128 * return 0 : no need to allocate a data chunk.
3129 */
3130static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3131 u64 chunk_offset)
3132{
David Sterba32da53862019-10-29 19:20:18 +01003133 struct btrfs_block_group *cache;
Liu Boa6f93c72017-11-15 16:28:11 -07003134 u64 bytes_used;
3135 u64 chunk_type;
3136
3137 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3138 ASSERT(cache);
3139 chunk_type = cache->flags;
3140 btrfs_put_block_group(cache);
3141
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003142 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3143 return 0;
Liu Boa6f93c72017-11-15 16:28:11 -07003144
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003145 spin_lock(&fs_info->data_sinfo->lock);
3146 bytes_used = fs_info->data_sinfo->bytes_used;
3147 spin_unlock(&fs_info->data_sinfo->lock);
Liu Boa6f93c72017-11-15 16:28:11 -07003148
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003149 if (!bytes_used) {
3150 struct btrfs_trans_handle *trans;
3151 int ret;
Liu Boa6f93c72017-11-15 16:28:11 -07003152
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003153 trans = btrfs_join_transaction(fs_info->tree_root);
3154 if (IS_ERR(trans))
3155 return PTR_ERR(trans);
3156
3157 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3158 btrfs_end_transaction(trans);
3159 if (ret < 0)
3160 return ret;
3161 return 1;
Liu Boa6f93c72017-11-15 16:28:11 -07003162 }
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003163
Liu Boa6f93c72017-11-15 16:28:11 -07003164 return 0;
3165}
3166
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003167static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003168 struct btrfs_balance_control *bctl)
3169{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003170 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003171 struct btrfs_trans_handle *trans;
3172 struct btrfs_balance_item *item;
3173 struct btrfs_disk_balance_args disk_bargs;
3174 struct btrfs_path *path;
3175 struct extent_buffer *leaf;
3176 struct btrfs_key key;
3177 int ret, err;
3178
3179 path = btrfs_alloc_path();
3180 if (!path)
3181 return -ENOMEM;
3182
3183 trans = btrfs_start_transaction(root, 0);
3184 if (IS_ERR(trans)) {
3185 btrfs_free_path(path);
3186 return PTR_ERR(trans);
3187 }
3188
3189 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003190 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003191 key.offset = 0;
3192
3193 ret = btrfs_insert_empty_item(trans, root, path, &key,
3194 sizeof(*item));
3195 if (ret)
3196 goto out;
3197
3198 leaf = path->nodes[0];
3199 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3200
David Sterbab159fa22016-11-08 18:09:03 +01003201 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003202
3203 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3204 btrfs_set_balance_data(leaf, item, &disk_bargs);
3205 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3206 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3207 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3208 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3209
3210 btrfs_set_balance_flags(leaf, item, bctl->flags);
3211
3212 btrfs_mark_buffer_dirty(leaf);
3213out:
3214 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003215 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003216 if (err && !ret)
3217 ret = err;
3218 return ret;
3219}
3220
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003221static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003222{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003223 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003224 struct btrfs_trans_handle *trans;
3225 struct btrfs_path *path;
3226 struct btrfs_key key;
3227 int ret, err;
3228
3229 path = btrfs_alloc_path();
3230 if (!path)
3231 return -ENOMEM;
3232
David Sterba3502a8c2020-06-25 12:35:28 +02003233 trans = btrfs_start_transaction_fallback_global_rsv(root, 0);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003234 if (IS_ERR(trans)) {
3235 btrfs_free_path(path);
3236 return PTR_ERR(trans);
3237 }
3238
3239 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003240 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003241 key.offset = 0;
3242
3243 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3244 if (ret < 0)
3245 goto out;
3246 if (ret > 0) {
3247 ret = -ENOENT;
3248 goto out;
3249 }
3250
3251 ret = btrfs_del_item(trans, root, path);
3252out:
3253 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003254 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003255 if (err && !ret)
3256 ret = err;
3257 return ret;
3258}
3259
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003260/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003261 * This is a heuristic used to reduce the number of chunks balanced on
3262 * resume after balance was interrupted.
3263 */
3264static void update_balance_args(struct btrfs_balance_control *bctl)
3265{
3266 /*
3267 * Turn on soft mode for chunk types that were being converted.
3268 */
3269 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3270 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3271 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3272 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3273 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3274 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3275
3276 /*
3277 * Turn on usage filter if is not already used. The idea is
3278 * that chunks that we have already balanced should be
3279 * reasonably full. Don't do it for chunks that are being
3280 * converted - that will keep us from relocating unconverted
3281 * (albeit full) chunks.
3282 */
3283 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003284 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003285 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3286 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3287 bctl->data.usage = 90;
3288 }
3289 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003290 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003291 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3292 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3293 bctl->sys.usage = 90;
3294 }
3295 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003296 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003297 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3298 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3299 bctl->meta.usage = 90;
3300 }
3301}
3302
3303/*
David Sterba149196a2018-03-20 20:23:09 +01003304 * Clear the balance status in fs_info and delete the balance item from disk.
3305 */
3306static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003307{
3308 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003309 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003310
3311 BUG_ON(!fs_info->balance_ctl);
3312
3313 spin_lock(&fs_info->balance_lock);
3314 fs_info->balance_ctl = NULL;
3315 spin_unlock(&fs_info->balance_lock);
3316
3317 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003318 ret = del_balance_item(fs_info);
3319 if (ret)
3320 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003321}
3322
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003323/*
3324 * Balance filters. Return 1 if chunk should be filtered out
3325 * (should not be balanced).
3326 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003327static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003328 struct btrfs_balance_args *bargs)
3329{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003330 chunk_type = chunk_to_extended(chunk_type) &
3331 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003332
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003333 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003334 return 0;
3335
3336 return 1;
3337}
3338
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003339static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003340 struct btrfs_balance_args *bargs)
3341{
David Sterba32da53862019-10-29 19:20:18 +01003342 struct btrfs_block_group *cache;
David Sterbabc309462015-10-20 18:22:13 +02003343 u64 chunk_used;
3344 u64 user_thresh_min;
3345 u64 user_thresh_max;
3346 int ret = 1;
3347
3348 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003349 chunk_used = cache->used;
David Sterbabc309462015-10-20 18:22:13 +02003350
3351 if (bargs->usage_min == 0)
3352 user_thresh_min = 0;
3353 else
David Sterbab3470b52019-10-23 18:48:22 +02003354 user_thresh_min = div_factor_fine(cache->length,
3355 bargs->usage_min);
David Sterbabc309462015-10-20 18:22:13 +02003356
3357 if (bargs->usage_max == 0)
3358 user_thresh_max = 1;
3359 else if (bargs->usage_max > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003360 user_thresh_max = cache->length;
David Sterbabc309462015-10-20 18:22:13 +02003361 else
David Sterbab3470b52019-10-23 18:48:22 +02003362 user_thresh_max = div_factor_fine(cache->length,
3363 bargs->usage_max);
David Sterbabc309462015-10-20 18:22:13 +02003364
3365 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3366 ret = 0;
3367
3368 btrfs_put_block_group(cache);
3369 return ret;
3370}
3371
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003372static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003373 u64 chunk_offset, struct btrfs_balance_args *bargs)
3374{
David Sterba32da53862019-10-29 19:20:18 +01003375 struct btrfs_block_group *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003376 u64 chunk_used, user_thresh;
3377 int ret = 1;
3378
3379 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003380 chunk_used = cache->used;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003381
David Sterbabc309462015-10-20 18:22:13 +02003382 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003383 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003384 else if (bargs->usage > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003385 user_thresh = cache->length;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003386 else
David Sterbab3470b52019-10-23 18:48:22 +02003387 user_thresh = div_factor_fine(cache->length, bargs->usage);
Ilya Dryomova105bb82013-01-21 15:15:56 +02003388
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003389 if (chunk_used < user_thresh)
3390 ret = 0;
3391
3392 btrfs_put_block_group(cache);
3393 return ret;
3394}
3395
Ilya Dryomov409d4042012-01-16 22:04:47 +02003396static int chunk_devid_filter(struct extent_buffer *leaf,
3397 struct btrfs_chunk *chunk,
3398 struct btrfs_balance_args *bargs)
3399{
3400 struct btrfs_stripe *stripe;
3401 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3402 int i;
3403
3404 for (i = 0; i < num_stripes; i++) {
3405 stripe = btrfs_stripe_nr(chunk, i);
3406 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3407 return 0;
3408 }
3409
3410 return 1;
3411}
3412
David Sterba946c9252019-05-17 11:43:34 +02003413static u64 calc_data_stripes(u64 type, int num_stripes)
3414{
3415 const int index = btrfs_bg_flags_to_raid_index(type);
3416 const int ncopies = btrfs_raid_array[index].ncopies;
3417 const int nparity = btrfs_raid_array[index].nparity;
3418
3419 if (nparity)
3420 return num_stripes - nparity;
3421 else
3422 return num_stripes / ncopies;
3423}
3424
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003425/* [pstart, pend) */
3426static int chunk_drange_filter(struct extent_buffer *leaf,
3427 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003428 struct btrfs_balance_args *bargs)
3429{
3430 struct btrfs_stripe *stripe;
3431 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3432 u64 stripe_offset;
3433 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003434 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003435 int factor;
3436 int i;
3437
3438 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3439 return 0;
3440
David Sterba946c9252019-05-17 11:43:34 +02003441 type = btrfs_chunk_type(leaf, chunk);
3442 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003443
3444 for (i = 0; i < num_stripes; i++) {
3445 stripe = btrfs_stripe_nr(chunk, i);
3446 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3447 continue;
3448
3449 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3450 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003451 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003452
3453 if (stripe_offset < bargs->pend &&
3454 stripe_offset + stripe_length > bargs->pstart)
3455 return 0;
3456 }
3457
3458 return 1;
3459}
3460
Ilya Dryomovea671762012-01-16 22:04:48 +02003461/* [vstart, vend) */
3462static int chunk_vrange_filter(struct extent_buffer *leaf,
3463 struct btrfs_chunk *chunk,
3464 u64 chunk_offset,
3465 struct btrfs_balance_args *bargs)
3466{
3467 if (chunk_offset < bargs->vend &&
3468 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3469 /* at least part of the chunk is inside this vrange */
3470 return 0;
3471
3472 return 1;
3473}
3474
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003475static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3476 struct btrfs_chunk *chunk,
3477 struct btrfs_balance_args *bargs)
3478{
3479 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3480
3481 if (bargs->stripes_min <= num_stripes
3482 && num_stripes <= bargs->stripes_max)
3483 return 0;
3484
3485 return 1;
3486}
3487
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003488static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003489 struct btrfs_balance_args *bargs)
3490{
3491 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3492 return 0;
3493
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003494 chunk_type = chunk_to_extended(chunk_type) &
3495 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003496
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003497 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003498 return 1;
3499
3500 return 0;
3501}
3502
David Sterba6ec08962019-03-20 16:38:52 +01003503static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003504 struct btrfs_chunk *chunk, u64 chunk_offset)
3505{
David Sterba6ec08962019-03-20 16:38:52 +01003506 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003508 struct btrfs_balance_args *bargs = NULL;
3509 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3510
3511 /* type filter */
3512 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3513 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3514 return 0;
3515 }
3516
3517 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3518 bargs = &bctl->data;
3519 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3520 bargs = &bctl->sys;
3521 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3522 bargs = &bctl->meta;
3523
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003524 /* profiles filter */
3525 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3526 chunk_profiles_filter(chunk_type, bargs)) {
3527 return 0;
3528 }
3529
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003530 /* usage filter */
3531 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003532 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003533 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003534 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003536 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003537 }
3538
Ilya Dryomov409d4042012-01-16 22:04:47 +02003539 /* devid filter */
3540 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3541 chunk_devid_filter(leaf, chunk, bargs)) {
3542 return 0;
3543 }
3544
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003545 /* drange filter, makes sense only with devid filter */
3546 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003547 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003548 return 0;
3549 }
3550
Ilya Dryomovea671762012-01-16 22:04:48 +02003551 /* vrange filter */
3552 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3553 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3554 return 0;
3555 }
3556
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003557 /* stripes filter */
3558 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3559 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3560 return 0;
3561 }
3562
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003563 /* soft profile changing mode */
3564 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3565 chunk_soft_convert_filter(chunk_type, bargs)) {
3566 return 0;
3567 }
3568
David Sterba7d824b62014-05-07 17:37:51 +02003569 /*
3570 * limited by count, must be the last filter
3571 */
3572 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3573 if (bargs->limit == 0)
3574 return 0;
3575 else
3576 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003577 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3578 /*
3579 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003580 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003581 * about the count of all chunks that satisfy the filters.
3582 */
3583 if (bargs->limit_max == 0)
3584 return 0;
3585 else
3586 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003587 }
3588
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003589 return 1;
3590}
3591
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003592static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003593{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003594 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003595 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003596 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003597 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003598 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003599 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003600 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003601 struct extent_buffer *leaf;
3602 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003603 int ret;
3604 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003605 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003606 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003607 u64 limit_data = bctl->data.limit;
3608 u64 limit_meta = bctl->meta.limit;
3609 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003610 u32 count_data = 0;
3611 u32 count_meta = 0;
3612 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003613 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003614
Chris Masonec44a352008-04-28 15:29:52 -04003615 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003616 if (!path) {
3617 ret = -ENOMEM;
3618 goto error;
3619 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003620
3621 /* zero out stat counters */
3622 spin_lock(&fs_info->balance_lock);
3623 memset(&bctl->stat, 0, sizeof(bctl->stat));
3624 spin_unlock(&fs_info->balance_lock);
3625again:
David Sterba7d824b62014-05-07 17:37:51 +02003626 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003627 /*
3628 * The single value limit and min/max limits use the same bytes
3629 * in the
3630 */
David Sterba7d824b62014-05-07 17:37:51 +02003631 bctl->data.limit = limit_data;
3632 bctl->meta.limit = limit_meta;
3633 bctl->sys.limit = limit_sys;
3634 }
Chris Masonec44a352008-04-28 15:29:52 -04003635 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3636 key.offset = (u64)-1;
3637 key.type = BTRFS_CHUNK_ITEM_KEY;
3638
Chris Masond3977122009-01-05 21:25:51 -05003639 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003640 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003641 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003642 ret = -ECANCELED;
3643 goto error;
3644 }
3645
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003646 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003647 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003648 if (ret < 0) {
3649 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003650 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003651 }
Chris Masonec44a352008-04-28 15:29:52 -04003652
3653 /*
3654 * this shouldn't happen, it means the last relocate
3655 * failed
3656 */
3657 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003658 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003659
3660 ret = btrfs_previous_item(chunk_root, path, 0,
3661 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003662 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003663 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003664 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003665 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003666 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003667
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003668 leaf = path->nodes[0];
3669 slot = path->slots[0];
3670 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3671
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003672 if (found_key.objectid != key.objectid) {
3673 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003674 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003675 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003676
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003677 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003678 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003679
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003680 if (!counting) {
3681 spin_lock(&fs_info->balance_lock);
3682 bctl->stat.considered++;
3683 spin_unlock(&fs_info->balance_lock);
3684 }
3685
David Sterba6ec08962019-03-20 16:38:52 +01003686 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003687
David Sterbab3b4aa72011-04-21 01:20:15 +02003688 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003689 if (!ret) {
3690 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003691 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003692 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003693
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003694 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003695 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003696 spin_lock(&fs_info->balance_lock);
3697 bctl->stat.expected++;
3698 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003699
3700 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3701 count_data++;
3702 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3703 count_sys++;
3704 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3705 count_meta++;
3706
3707 goto loop;
3708 }
3709
3710 /*
3711 * Apply limit_min filter, no need to check if the LIMITS
3712 * filter is used, limit_min is 0 by default
3713 */
3714 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3715 count_data < bctl->data.limit_min)
3716 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3717 count_meta < bctl->meta.limit_min)
3718 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3719 count_sys < bctl->sys.limit_min)) {
3720 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003721 goto loop;
3722 }
3723
Liu Boa6f93c72017-11-15 16:28:11 -07003724 if (!chunk_reserved) {
3725 /*
3726 * We may be relocating the only data chunk we have,
3727 * which could potentially end up with losing data's
3728 * raid profile, so lets allocate an empty one in
3729 * advance.
3730 */
3731 ret = btrfs_may_alloc_data_chunk(fs_info,
3732 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003733 if (ret < 0) {
3734 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3735 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003736 } else if (ret == 1) {
3737 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003738 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003739 }
3740
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003741 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003742 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003743 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003744 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003745 } else if (ret == -ETXTBSY) {
3746 btrfs_info(fs_info,
3747 "skipping relocation of block group %llu due to active swapfile",
3748 found_key.offset);
3749 ret = 0;
3750 } else if (ret) {
3751 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003752 } else {
3753 spin_lock(&fs_info->balance_lock);
3754 bctl->stat.completed++;
3755 spin_unlock(&fs_info->balance_lock);
3756 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003757loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003758 if (found_key.offset == 0)
3759 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003760 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003761 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003762
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003763 if (counting) {
3764 btrfs_release_path(path);
3765 counting = false;
3766 goto again;
3767 }
Chris Masonec44a352008-04-28 15:29:52 -04003768error:
3769 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003770 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003771 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003772 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003773 if (!ret)
3774 ret = -ENOSPC;
3775 }
3776
Chris Masonec44a352008-04-28 15:29:52 -04003777 return ret;
3778}
3779
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003780/**
3781 * alloc_profile_is_valid - see if a given profile is valid and reduced
3782 * @flags: profile to validate
3783 * @extended: if true @flags is treated as an extended profile
3784 */
3785static int alloc_profile_is_valid(u64 flags, int extended)
3786{
3787 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3788 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3789
3790 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3791
3792 /* 1) check that all other bits are zeroed */
3793 if (flags & ~mask)
3794 return 0;
3795
3796 /* 2) see if profile is reduced */
3797 if (flags == 0)
3798 return !extended; /* "0" is valid for usual profiles */
3799
David Sterbac1499162019-10-01 19:44:42 +02003800 return has_single_bit_set(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003801}
3802
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003803static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3804{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003805 /* cancel requested || normal exit path */
3806 return atomic_read(&fs_info->balance_cancel_req) ||
3807 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3808 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003809}
3810
David Sterba5ba366c2020-02-27 21:00:52 +01003811/*
3812 * Validate target profile against allowed profiles and return true if it's OK.
3813 * Otherwise print the error message and return false.
3814 */
3815static inline int validate_convert_profile(struct btrfs_fs_info *fs_info,
3816 const struct btrfs_balance_args *bargs,
3817 u64 allowed, const char *type)
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003818{
David Sterba5ba366c2020-02-27 21:00:52 +01003819 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3820 return true;
3821
3822 /* Profile is valid and does not have bits outside of the allowed set */
3823 if (alloc_profile_is_valid(bargs->target, 1) &&
3824 (bargs->target & ~allowed) == 0)
3825 return true;
3826
3827 btrfs_err(fs_info, "balance: invalid convert %s profile %s",
3828 type, btrfs_bg_type_to_raid_name(bargs->target));
3829 return false;
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003830}
3831
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003832/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003833 * Fill @buf with textual description of balance filter flags @bargs, up to
3834 * @size_buf including the terminating null. The output may be trimmed if it
3835 * does not fit into the provided buffer.
3836 */
3837static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3838 u32 size_buf)
3839{
3840 int ret;
3841 u32 size_bp = size_buf;
3842 char *bp = buf;
3843 u64 flags = bargs->flags;
3844 char tmp_buf[128] = {'\0'};
3845
3846 if (!flags)
3847 return;
3848
3849#define CHECK_APPEND_NOARG(a) \
3850 do { \
3851 ret = snprintf(bp, size_bp, (a)); \
3852 if (ret < 0 || ret >= size_bp) \
3853 goto out_overflow; \
3854 size_bp -= ret; \
3855 bp += ret; \
3856 } while (0)
3857
3858#define CHECK_APPEND_1ARG(a, v1) \
3859 do { \
3860 ret = snprintf(bp, size_bp, (a), (v1)); \
3861 if (ret < 0 || ret >= size_bp) \
3862 goto out_overflow; \
3863 size_bp -= ret; \
3864 bp += ret; \
3865 } while (0)
3866
3867#define CHECK_APPEND_2ARG(a, v1, v2) \
3868 do { \
3869 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3870 if (ret < 0 || ret >= size_bp) \
3871 goto out_overflow; \
3872 size_bp -= ret; \
3873 bp += ret; \
3874 } while (0)
3875
David Sterba158da512019-05-17 11:43:41 +02003876 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3877 CHECK_APPEND_1ARG("convert=%s,",
3878 btrfs_bg_type_to_raid_name(bargs->target));
Anand Jain56fc37d2018-11-20 16:12:56 +08003879
3880 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3881 CHECK_APPEND_NOARG("soft,");
3882
3883 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3884 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3885 sizeof(tmp_buf));
3886 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3887 }
3888
3889 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3890 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3891
3892 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3893 CHECK_APPEND_2ARG("usage=%u..%u,",
3894 bargs->usage_min, bargs->usage_max);
3895
3896 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3897 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3898
3899 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3900 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3901 bargs->pstart, bargs->pend);
3902
3903 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3904 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3905 bargs->vstart, bargs->vend);
3906
3907 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3908 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3909
3910 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3911 CHECK_APPEND_2ARG("limit=%u..%u,",
3912 bargs->limit_min, bargs->limit_max);
3913
3914 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3915 CHECK_APPEND_2ARG("stripes=%u..%u,",
3916 bargs->stripes_min, bargs->stripes_max);
3917
3918#undef CHECK_APPEND_2ARG
3919#undef CHECK_APPEND_1ARG
3920#undef CHECK_APPEND_NOARG
3921
3922out_overflow:
3923
3924 if (size_bp < size_buf)
3925 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3926 else
3927 buf[0] = '\0';
3928}
3929
3930static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3931{
3932 u32 size_buf = 1024;
3933 char tmp_buf[192] = {'\0'};
3934 char *buf;
3935 char *bp;
3936 u32 size_bp = size_buf;
3937 int ret;
3938 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3939
3940 buf = kzalloc(size_buf, GFP_KERNEL);
3941 if (!buf)
3942 return;
3943
3944 bp = buf;
3945
3946#define CHECK_APPEND_1ARG(a, v1) \
3947 do { \
3948 ret = snprintf(bp, size_bp, (a), (v1)); \
3949 if (ret < 0 || ret >= size_bp) \
3950 goto out_overflow; \
3951 size_bp -= ret; \
3952 bp += ret; \
3953 } while (0)
3954
3955 if (bctl->flags & BTRFS_BALANCE_FORCE)
3956 CHECK_APPEND_1ARG("%s", "-f ");
3957
3958 if (bctl->flags & BTRFS_BALANCE_DATA) {
3959 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3960 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3961 }
3962
3963 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3964 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3965 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3966 }
3967
3968 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3969 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3970 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3971 }
3972
3973#undef CHECK_APPEND_1ARG
3974
3975out_overflow:
3976
3977 if (size_bp < size_buf)
3978 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3979 btrfs_info(fs_info, "balance: %s %s",
3980 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3981 "resume" : "start", buf);
3982
3983 kfree(buf);
3984}
3985
3986/*
David Sterbadccdb072018-03-21 00:20:05 +01003987 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003989int btrfs_balance(struct btrfs_fs_info *fs_info,
3990 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003991 struct btrfs_ioctl_balance_args *bargs)
3992{
Adam Borowski14506122017-03-07 23:34:44 +01003993 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003994 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003995 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003997 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003998 unsigned seq;
Anand Jaine62869b2019-09-25 14:29:28 +08003999 bool reducing_redundancy;
David Sterba081db892019-05-17 11:43:27 +02004000 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004001
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004002 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004003 atomic_read(&fs_info->balance_pause_req) ||
Qu Wenruo726a3422020-02-17 14:16:52 +08004004 btrfs_should_cancel_balance(fs_info)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004005 ret = -EINVAL;
4006 goto out;
4007 }
4008
Ilya Dryomove4837f82012-03-27 17:09:17 +03004009 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4010 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4011 mixed = 1;
4012
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004013 /*
4014 * In case of mixed groups both data and meta should be picked,
4015 * and identical options should be given for both of them.
4016 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004017 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4018 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004019 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4020 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4021 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004022 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004023 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004024 ret = -EINVAL;
4025 goto out;
4026 }
4027 }
4028
Josef Bacikb35cf1f2020-01-10 11:11:24 -05004029 /*
4030 * rw_devices will not change at the moment, device add/delete/replace
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004031 * are exclusive
Josef Bacikb35cf1f2020-01-10 11:11:24 -05004032 */
4033 num_devices = fs_info->fs_devices->rw_devices;
Qu Wenruofab27352019-09-25 10:13:27 +08004034
4035 /*
4036 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4037 * special bit for it, to make it easier to distinguish. Thus we need
4038 * to set it manually, or balance would refuse the profile.
4039 */
4040 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
David Sterba081db892019-05-17 11:43:27 +02004041 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4042 if (num_devices >= btrfs_raid_array[i].devs_min)
4043 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08004044
David Sterba5ba366c2020-02-27 21:00:52 +01004045 if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") ||
4046 !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") ||
4047 !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004048 ret = -EINVAL;
4049 goto out;
4050 }
4051
David Sterba6079e122019-05-17 11:43:29 +02004052 /*
4053 * Allow to reduce metadata or system integrity only if force set for
4054 * profiles with redundancy (copies, parity)
4055 */
4056 allowed = 0;
4057 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4058 if (btrfs_raid_array[i].ncopies >= 2 ||
4059 btrfs_raid_array[i].tolerated_failures >= 1)
4060 allowed |= btrfs_raid_array[i].bg_flag;
4061 }
Miao Xiede98ced2013-01-29 10:13:12 +00004062 do {
4063 seq = read_seqbegin(&fs_info->profiles_lock);
4064
4065 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4066 (fs_info->avail_system_alloc_bits & allowed) &&
4067 !(bctl->sys.target & allowed)) ||
4068 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4069 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004070 !(bctl->meta.target & allowed)))
Anand Jaine62869b2019-09-25 14:29:28 +08004071 reducing_redundancy = true;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004072 else
Anand Jaine62869b2019-09-25 14:29:28 +08004073 reducing_redundancy = false;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004074
4075 /* if we're not converting, the target field is uninitialized */
4076 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4077 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4078 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4079 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004080 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004081
Anand Jaine62869b2019-09-25 14:29:28 +08004082 if (reducing_redundancy) {
Filipe Manana5a8067c2018-11-19 09:48:12 +00004083 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4084 btrfs_info(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004085 "balance: force reducing metadata redundancy");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004086 } else {
4087 btrfs_err(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004088 "balance: reduces metadata redundancy, use --force if you want this");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004089 ret = -EINVAL;
4090 goto out;
4091 }
4092 }
4093
Adam Borowski14506122017-03-07 23:34:44 +01004094 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4095 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00004096 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004097 "balance: metadata profile %s has lower redundancy than data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004098 btrfs_bg_type_to_raid_name(meta_target),
4099 btrfs_bg_type_to_raid_name(data_target));
Sam Tygieree592d02016-01-06 08:46:12 +00004100 }
4101
Filipe Manana9e967492019-04-22 16:44:09 +01004102 if (fs_info->send_in_progress) {
4103 btrfs_warn_rl(fs_info,
4104"cannot run balance while send operations are in progress (%d in progress)",
4105 fs_info->send_in_progress);
4106 ret = -EAGAIN;
4107 goto out;
4108 }
4109
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004110 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004111 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004112 goto out;
4113
Ilya Dryomov59641012012-01-16 22:04:48 +02004114 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4115 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004116 BUG_ON(fs_info->balance_ctl);
4117 spin_lock(&fs_info->balance_lock);
4118 fs_info->balance_ctl = bctl;
4119 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004120 } else {
4121 BUG_ON(ret != -EEXIST);
4122 spin_lock(&fs_info->balance_lock);
4123 update_balance_args(bctl);
4124 spin_unlock(&fs_info->balance_lock);
4125 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004126
David Sterba3009a622018-03-21 01:31:04 +01004127 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4128 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004129 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004130 mutex_unlock(&fs_info->balance_mutex);
4131
4132 ret = __btrfs_balance(fs_info);
4133
4134 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004135 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4136 btrfs_info(fs_info, "balance: paused");
Qu Wenruo44d354a2020-07-13 09:03:21 +08004137 /*
4138 * Balance can be canceled by:
4139 *
4140 * - Regular cancel request
4141 * Then ret == -ECANCELED and balance_cancel_req > 0
4142 *
4143 * - Fatal signal to "btrfs" process
4144 * Either the signal caught by wait_reserve_ticket() and callers
4145 * got -EINTR, or caught by btrfs_should_cancel_balance() and
4146 * got -ECANCELED.
4147 * Either way, in this case balance_cancel_req = 0, and
4148 * ret == -EINTR or ret == -ECANCELED.
4149 *
4150 * So here we only check the return value to catch canceled balance.
4151 */
4152 else if (ret == -ECANCELED || ret == -EINTR)
Anand Jain7333bd02018-11-20 16:12:57 +08004153 btrfs_info(fs_info, "balance: canceled");
4154 else
4155 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4156
David Sterba3009a622018-03-21 01:31:04 +01004157 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004158
4159 if (bargs) {
4160 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004161 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004162 }
4163
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004164 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4165 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004166 reset_balance_state(fs_info);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004167 btrfs_exclop_finish(fs_info);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004168 }
4169
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004170 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004171
4172 return ret;
4173out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004174 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004175 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004176 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004177 kfree(bctl);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004178 btrfs_exclop_finish(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004179
Ilya Dryomov59641012012-01-16 22:04:48 +02004180 return ret;
4181}
4182
4183static int balance_kthread(void *data)
4184{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004185 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004186 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004187
Ilya Dryomov59641012012-01-16 22:04:48 +02004188 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004189 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004190 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004191 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004192
Ilya Dryomov59641012012-01-16 22:04:48 +02004193 return ret;
4194}
4195
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004196int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4197{
4198 struct task_struct *tsk;
4199
David Sterba1354e1a2018-03-21 02:29:13 +01004200 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004201 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004202 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004203 return 0;
4204 }
David Sterba1354e1a2018-03-21 02:29:13 +01004205 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004206
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004207 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004208 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004209 return 0;
4210 }
4211
Anand Jain02ee6542018-05-17 15:16:51 +08004212 /*
4213 * A ro->rw remount sequence should continue with the paused balance
4214 * regardless of who pauses it, system or the user as of now, so set
4215 * the resume flag.
4216 */
4217 spin_lock(&fs_info->balance_lock);
4218 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4219 spin_unlock(&fs_info->balance_lock);
4220
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004221 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304222 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004223}
4224
Ilya Dryomov68310a52012-06-22 12:24:12 -06004225int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004226{
Ilya Dryomov59641012012-01-16 22:04:48 +02004227 struct btrfs_balance_control *bctl;
4228 struct btrfs_balance_item *item;
4229 struct btrfs_disk_balance_args disk_bargs;
4230 struct btrfs_path *path;
4231 struct extent_buffer *leaf;
4232 struct btrfs_key key;
4233 int ret;
4234
4235 path = btrfs_alloc_path();
4236 if (!path)
4237 return -ENOMEM;
4238
Ilya Dryomov68310a52012-06-22 12:24:12 -06004239 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004240 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004241 key.offset = 0;
4242
4243 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4244 if (ret < 0)
4245 goto out;
4246 if (ret > 0) { /* ret = -ENOENT; */
4247 ret = 0;
4248 goto out;
4249 }
4250
Ilya Dryomov59641012012-01-16 22:04:48 +02004251 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4252 if (!bctl) {
4253 ret = -ENOMEM;
4254 goto out;
4255 }
4256
Ilya Dryomov59641012012-01-16 22:04:48 +02004257 leaf = path->nodes[0];
4258 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4259
Ilya Dryomov68310a52012-06-22 12:24:12 -06004260 bctl->flags = btrfs_balance_flags(leaf, item);
4261 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004262
4263 btrfs_balance_data(leaf, item, &disk_bargs);
4264 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4265 btrfs_balance_meta(leaf, item, &disk_bargs);
4266 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4267 btrfs_balance_sys(leaf, item, &disk_bargs);
4268 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4269
David Sterbaeee95e32018-03-20 20:07:58 +01004270 /*
4271 * This should never happen, as the paused balance state is recovered
4272 * during mount without any chance of other exclusive ops to collide.
4273 *
4274 * This gives the exclusive op status to balance and keeps in paused
4275 * state until user intervention (cancel or umount). If the ownership
4276 * cannot be assigned, show a message but do not fail. The balance
4277 * is in a paused state and must have fs_info::balance_ctl properly
4278 * set up.
4279 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004280 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
David Sterbaeee95e32018-03-20 20:07:58 +01004281 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004282 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004283
Ilya Dryomov68310a52012-06-22 12:24:12 -06004284 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004285 BUG_ON(fs_info->balance_ctl);
4286 spin_lock(&fs_info->balance_lock);
4287 fs_info->balance_ctl = bctl;
4288 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004289 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004290out:
4291 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004292 return ret;
4293}
4294
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004295int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4296{
4297 int ret = 0;
4298
4299 mutex_lock(&fs_info->balance_mutex);
4300 if (!fs_info->balance_ctl) {
4301 mutex_unlock(&fs_info->balance_mutex);
4302 return -ENOTCONN;
4303 }
4304
David Sterba3009a622018-03-21 01:31:04 +01004305 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004306 atomic_inc(&fs_info->balance_pause_req);
4307 mutex_unlock(&fs_info->balance_mutex);
4308
4309 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004310 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004311
4312 mutex_lock(&fs_info->balance_mutex);
4313 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004314 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004315 atomic_dec(&fs_info->balance_pause_req);
4316 } else {
4317 ret = -ENOTCONN;
4318 }
4319
4320 mutex_unlock(&fs_info->balance_mutex);
4321 return ret;
4322}
4323
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004324int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4325{
4326 mutex_lock(&fs_info->balance_mutex);
4327 if (!fs_info->balance_ctl) {
4328 mutex_unlock(&fs_info->balance_mutex);
4329 return -ENOTCONN;
4330 }
4331
David Sterbacf7d20f2018-03-21 01:45:32 +01004332 /*
4333 * A paused balance with the item stored on disk can be resumed at
4334 * mount time if the mount is read-write. Otherwise it's still paused
4335 * and we must not allow cancelling as it deletes the item.
4336 */
4337 if (sb_rdonly(fs_info->sb)) {
4338 mutex_unlock(&fs_info->balance_mutex);
4339 return -EROFS;
4340 }
4341
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004342 atomic_inc(&fs_info->balance_cancel_req);
4343 /*
4344 * if we are running just wait and return, balance item is
4345 * deleted in btrfs_balance in this case
4346 */
David Sterba3009a622018-03-21 01:31:04 +01004347 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004348 mutex_unlock(&fs_info->balance_mutex);
4349 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004350 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004351 mutex_lock(&fs_info->balance_mutex);
4352 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004353 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004354 /*
4355 * Lock released to allow other waiters to continue, we'll
4356 * reexamine the status again.
4357 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004358 mutex_lock(&fs_info->balance_mutex);
4359
David Sterbaa17c95d2018-03-20 17:28:05 +01004360 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004361 reset_balance_state(fs_info);
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -05004362 btrfs_exclop_finish(fs_info);
Anand Jain6dac13f2018-05-16 10:51:26 +08004363 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004364 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004365 }
4366
David Sterba3009a622018-03-21 01:31:04 +01004367 BUG_ON(fs_info->balance_ctl ||
4368 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004369 atomic_dec(&fs_info->balance_cancel_req);
4370 mutex_unlock(&fs_info->balance_mutex);
4371 return 0;
4372}
4373
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02004374int btrfs_uuid_scan_kthread(void *data)
Stefan Behrens803b2f52013-08-15 17:11:21 +02004375{
4376 struct btrfs_fs_info *fs_info = data;
4377 struct btrfs_root *root = fs_info->tree_root;
4378 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004379 struct btrfs_path *path = NULL;
4380 int ret = 0;
4381 struct extent_buffer *eb;
4382 int slot;
4383 struct btrfs_root_item root_item;
4384 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004385 struct btrfs_trans_handle *trans = NULL;
Josef Bacikc94bec22020-02-14 15:05:01 -05004386 bool closing = false;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004387
4388 path = btrfs_alloc_path();
4389 if (!path) {
4390 ret = -ENOMEM;
4391 goto out;
4392 }
4393
4394 key.objectid = 0;
4395 key.type = BTRFS_ROOT_ITEM_KEY;
4396 key.offset = 0;
4397
Stefan Behrens803b2f52013-08-15 17:11:21 +02004398 while (1) {
Josef Bacikc94bec22020-02-14 15:05:01 -05004399 if (btrfs_fs_closing(fs_info)) {
4400 closing = true;
4401 break;
4402 }
Anand Jain7c829b72018-03-07 17:29:18 +08004403 ret = btrfs_search_forward(root, &key, path,
4404 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004405 if (ret) {
4406 if (ret > 0)
4407 ret = 0;
4408 break;
4409 }
4410
4411 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4412 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4413 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4414 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4415 goto skip;
4416
4417 eb = path->nodes[0];
4418 slot = path->slots[0];
4419 item_size = btrfs_item_size_nr(eb, slot);
4420 if (item_size < sizeof(root_item))
4421 goto skip;
4422
Stefan Behrens803b2f52013-08-15 17:11:21 +02004423 read_extent_buffer(eb, &root_item,
4424 btrfs_item_ptr_offset(eb, slot),
4425 (int)sizeof(root_item));
4426 if (btrfs_root_refs(&root_item) == 0)
4427 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004428
4429 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4430 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4431 if (trans)
4432 goto update_tree;
4433
4434 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004435 /*
4436 * 1 - subvol uuid item
4437 * 1 - received_subvol uuid item
4438 */
4439 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4440 if (IS_ERR(trans)) {
4441 ret = PTR_ERR(trans);
4442 break;
4443 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004444 continue;
4445 } else {
4446 goto skip;
4447 }
4448update_tree:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004449 btrfs_release_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004450 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004451 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004452 BTRFS_UUID_KEY_SUBVOL,
4453 key.objectid);
4454 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004455 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004456 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004457 break;
4458 }
4459 }
4460
4461 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004462 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004463 root_item.received_uuid,
4464 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4465 key.objectid);
4466 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004467 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004468 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004469 break;
4470 }
4471 }
4472
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004473skip:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004474 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004475 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004476 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004477 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004478 if (ret)
4479 break;
4480 }
4481
Stefan Behrens803b2f52013-08-15 17:11:21 +02004482 if (key.offset < (u64)-1) {
4483 key.offset++;
4484 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4485 key.offset = 0;
4486 key.type = BTRFS_ROOT_ITEM_KEY;
4487 } else if (key.objectid < (u64)-1) {
4488 key.offset = 0;
4489 key.type = BTRFS_ROOT_ITEM_KEY;
4490 key.objectid++;
4491 } else {
4492 break;
4493 }
4494 cond_resched();
4495 }
4496
4497out:
4498 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004499 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004500 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004501 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004502 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Josef Bacikc94bec22020-02-14 15:05:01 -05004503 else if (!closing)
Josef Bacikafcdd122016-09-02 15:40:02 -04004504 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004505 up(&fs_info->uuid_tree_rescan_sem);
4506 return 0;
4507}
4508
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004509int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4510{
4511 struct btrfs_trans_handle *trans;
4512 struct btrfs_root *tree_root = fs_info->tree_root;
4513 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004514 struct task_struct *task;
4515 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004516
4517 /*
4518 * 1 - root node
4519 * 1 - root item
4520 */
4521 trans = btrfs_start_transaction(tree_root, 2);
4522 if (IS_ERR(trans))
4523 return PTR_ERR(trans);
4524
David Sterba9b7a2442019-03-20 13:20:49 +01004525 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004526 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004527 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004528 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004529 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004530 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004531 }
4532
4533 fs_info->uuid_root = uuid_root;
4534
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004535 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004536 if (ret)
4537 return ret;
4538
4539 down(&fs_info->uuid_tree_rescan_sem);
4540 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4541 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004542 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004543 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004544 up(&fs_info->uuid_tree_rescan_sem);
4545 return PTR_ERR(task);
4546 }
4547
4548 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004549}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004550
Chris Mason8f18cf12008-04-25 16:53:30 -04004551/*
4552 * shrinking a device means finding all of the device extents past
4553 * the new size, and then following the back refs to the chunks.
4554 * The chunk relocation code actually frees the device extent
4555 */
4556int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4557{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004558 struct btrfs_fs_info *fs_info = device->fs_info;
4559 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004560 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004561 struct btrfs_dev_extent *dev_extent = NULL;
4562 struct btrfs_path *path;
4563 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004564 u64 chunk_offset;
4565 int ret;
4566 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004567 int failed = 0;
4568 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004569 struct extent_buffer *l;
4570 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004571 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004572 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004573 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004574 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004575 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004576
4577 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004578 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004579 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004580
Anand Jain401e29c2017-12-04 12:54:55 +08004581 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004582 return -EINVAL;
4583
Chris Mason8f18cf12008-04-25 16:53:30 -04004584 path = btrfs_alloc_path();
4585 if (!path)
4586 return -ENOMEM;
4587
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004588 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004589
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004590 trans = btrfs_start_transaction(root, 0);
4591 if (IS_ERR(trans)) {
4592 btrfs_free_path(path);
4593 return PTR_ERR(trans);
4594 }
4595
David Sterba34441362016-10-04 19:34:27 +02004596 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004597
Miao Xie7cc8e582014-09-03 21:35:38 +08004598 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004599 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004600 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004601 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004602 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004603
4604 /*
4605 * Once the device's size has been set to the new size, ensure all
4606 * in-memory chunks are synced to disk so that the loop below sees them
4607 * and relocates them accordingly.
4608 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004609 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004610 mutex_unlock(&fs_info->chunk_mutex);
4611 ret = btrfs_commit_transaction(trans);
4612 if (ret)
4613 goto done;
4614 } else {
4615 mutex_unlock(&fs_info->chunk_mutex);
4616 btrfs_end_transaction(trans);
4617 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004618
Josef Bacikba1bf482009-09-11 16:11:19 -04004619again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004620 key.objectid = device->devid;
4621 key.offset = (u64)-1;
4622 key.type = BTRFS_DEV_EXTENT_KEY;
4623
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004624 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004625 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004626 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004627 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004628 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004629 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004630 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004631
4632 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004633 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004635 if (ret < 0)
4636 goto done;
4637 if (ret) {
4638 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004639 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004640 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004641 }
4642
4643 l = path->nodes[0];
4644 slot = path->slots[0];
4645 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4646
Josef Bacikba1bf482009-09-11 16:11:19 -04004647 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004649 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004650 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004651 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004652
4653 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4654 length = btrfs_dev_extent_length(l, dev_extent);
4655
Josef Bacikba1bf482009-09-11 16:11:19 -04004656 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004657 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004658 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004659 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004660 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004661
Chris Mason8f18cf12008-04-25 16:53:30 -04004662 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004663 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004664
Liu Boa6f93c72017-11-15 16:28:11 -07004665 /*
4666 * We may be relocating the only data chunk we have,
4667 * which could potentially end up with losing data's
4668 * raid profile, so lets allocate an empty one in
4669 * advance.
4670 */
4671 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4672 if (ret < 0) {
4673 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4674 goto done;
4675 }
4676
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004677 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4678 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004679 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004680 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004681 } else if (ret) {
4682 if (ret == -ETXTBSY) {
4683 btrfs_warn(fs_info,
4684 "could not shrink block group %llu due to active swapfile",
4685 chunk_offset);
4686 }
4687 goto done;
4688 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004689 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004690
4691 if (failed && !retried) {
4692 failed = 0;
4693 retried = true;
4694 goto again;
4695 } else if (failed && retried) {
4696 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004697 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004698 }
4699
Chris Balld6397ba2009-04-27 07:29:03 -04004700 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004701 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004702 if (IS_ERR(trans)) {
4703 ret = PTR_ERR(trans);
4704 goto done;
4705 }
4706
David Sterba34441362016-10-04 19:34:27 +02004707 mutex_lock(&fs_info->chunk_mutex);
Qu Wenruoc57dd1f2020-07-31 19:29:11 +08004708 /* Clear all state bits beyond the shrunk device size */
4709 clear_extent_bits(&device->alloc_state, new_size, (u64)-1,
4710 CHUNK_STATE_MASK);
4711
Miao Xie7cc8e582014-09-03 21:35:38 +08004712 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004713 if (list_empty(&device->post_commit_list))
4714 list_add_tail(&device->post_commit_list,
4715 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004716
Chris Balld6397ba2009-04-27 07:29:03 -04004717 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004718 btrfs_set_super_total_bytes(super_copy,
4719 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004720 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004721
4722 /* Now btrfs_update_device() will change the on-disk size. */
4723 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004724 if (ret < 0) {
4725 btrfs_abort_transaction(trans, ret);
4726 btrfs_end_transaction(trans);
4727 } else {
4728 ret = btrfs_commit_transaction(trans);
4729 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004730done:
4731 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004732 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004733 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004734 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004735 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004736 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004737 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004738 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004739 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004740 return ret;
4741}
4742
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004743static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004744 struct btrfs_key *key,
4745 struct btrfs_chunk *chunk, int item_size)
4746{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004747 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004748 struct btrfs_disk_key disk_key;
4749 u32 array_size;
4750 u8 *ptr;
4751
David Sterba34441362016-10-04 19:34:27 +02004752 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004753 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004754 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004755 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004756 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004757 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004758 }
Chris Mason0b86a832008-03-24 15:01:56 -04004759
4760 ptr = super_copy->sys_chunk_array + array_size;
4761 btrfs_cpu_key_to_disk(&disk_key, key);
4762 memcpy(ptr, &disk_key, sizeof(disk_key));
4763 ptr += sizeof(disk_key);
4764 memcpy(ptr, chunk, item_size);
4765 item_size += sizeof(disk_key);
4766 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004767 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004768
Chris Mason0b86a832008-03-24 15:01:56 -04004769 return 0;
4770}
4771
Miao Xieb2117a32011-01-05 10:07:28 +00004772/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004773 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004774 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004775static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004776{
Arne Jansen73c5de02011-04-12 12:07:57 +02004777 const struct btrfs_device_info *di_a = a;
4778 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004779
Arne Jansen73c5de02011-04-12 12:07:57 +02004780 if (di_a->max_avail > di_b->max_avail)
4781 return -1;
4782 if (di_a->max_avail < di_b->max_avail)
4783 return 1;
4784 if (di_a->total_avail > di_b->total_avail)
4785 return -1;
4786 if (di_a->total_avail < di_b->total_avail)
4787 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004788 return 0;
4789}
4790
David Woodhouse53b381b2013-01-29 18:40:14 -05004791static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4792{
Zhao Leiffe2d202015-01-20 15:11:44 +08004793 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004794 return;
4795
Miao Xieceda0862013-04-11 10:30:16 +00004796 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004797}
4798
David Sterbacfbb8252018-07-10 18:15:05 +02004799static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4800{
4801 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4802 return;
4803
4804 btrfs_set_fs_incompat(info, RAID1C34);
4805}
4806
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004807/*
4808 * Structure used internally for __btrfs_alloc_chunk() function.
4809 * Wraps needed parameters.
4810 */
4811struct alloc_chunk_ctl {
4812 u64 start;
4813 u64 type;
4814 /* Total number of stripes to allocate */
4815 int num_stripes;
4816 /* sub_stripes info for map */
4817 int sub_stripes;
4818 /* Stripes per device */
4819 int dev_stripes;
4820 /* Maximum number of devices to use */
4821 int devs_max;
4822 /* Minimum number of devices to use */
4823 int devs_min;
4824 /* ndevs has to be a multiple of this */
4825 int devs_increment;
4826 /* Number of copies */
4827 int ncopies;
4828 /* Number of stripes worth of bytes to store parity information */
4829 int nparity;
4830 u64 max_stripe_size;
4831 u64 max_chunk_size;
Naohiro Aota6aafb302020-02-25 12:56:15 +09004832 u64 dev_extent_min;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004833 u64 stripe_size;
4834 u64 chunk_size;
4835 int ndevs;
4836};
4837
Naohiro Aota27c314d2020-02-25 12:56:11 +09004838static void init_alloc_chunk_ctl_policy_regular(
4839 struct btrfs_fs_devices *fs_devices,
4840 struct alloc_chunk_ctl *ctl)
4841{
4842 u64 type = ctl->type;
4843
4844 if (type & BTRFS_BLOCK_GROUP_DATA) {
4845 ctl->max_stripe_size = SZ_1G;
4846 ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4847 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4848 /* For larger filesystems, use larger metadata chunks */
4849 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4850 ctl->max_stripe_size = SZ_1G;
4851 else
4852 ctl->max_stripe_size = SZ_256M;
4853 ctl->max_chunk_size = ctl->max_stripe_size;
4854 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4855 ctl->max_stripe_size = SZ_32M;
4856 ctl->max_chunk_size = 2 * ctl->max_stripe_size;
4857 ctl->devs_max = min_t(int, ctl->devs_max,
4858 BTRFS_MAX_DEVS_SYS_CHUNK);
4859 } else {
4860 BUG();
4861 }
4862
4863 /* We don't want a chunk larger than 10% of writable space */
4864 ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4865 ctl->max_chunk_size);
Naohiro Aota6aafb302020-02-25 12:56:15 +09004866 ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
Naohiro Aota27c314d2020-02-25 12:56:11 +09004867}
4868
4869static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices,
4870 struct alloc_chunk_ctl *ctl)
4871{
4872 int index = btrfs_bg_flags_to_raid_index(ctl->type);
4873
4874 ctl->sub_stripes = btrfs_raid_array[index].sub_stripes;
4875 ctl->dev_stripes = btrfs_raid_array[index].dev_stripes;
4876 ctl->devs_max = btrfs_raid_array[index].devs_max;
4877 if (!ctl->devs_max)
4878 ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info);
4879 ctl->devs_min = btrfs_raid_array[index].devs_min;
4880 ctl->devs_increment = btrfs_raid_array[index].devs_increment;
4881 ctl->ncopies = btrfs_raid_array[index].ncopies;
4882 ctl->nparity = btrfs_raid_array[index].nparity;
4883 ctl->ndevs = 0;
4884
4885 switch (fs_devices->chunk_alloc_policy) {
4886 case BTRFS_CHUNK_ALLOC_REGULAR:
4887 init_alloc_chunk_ctl_policy_regular(fs_devices, ctl);
4888 break;
4889 default:
4890 BUG();
4891 }
4892}
4893
Naohiro Aota560156c2020-02-25 12:56:12 +09004894static int gather_device_info(struct btrfs_fs_devices *fs_devices,
4895 struct alloc_chunk_ctl *ctl,
4896 struct btrfs_device_info *devices_info)
4897{
4898 struct btrfs_fs_info *info = fs_devices->fs_info;
4899 struct btrfs_device *device;
4900 u64 total_avail;
4901 u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes;
Naohiro Aota560156c2020-02-25 12:56:12 +09004902 int ret;
4903 int ndevs = 0;
4904 u64 max_avail;
4905 u64 dev_offset;
4906
4907 /*
4908 * in the first pass through the devices list, we gather information
4909 * about the available holes on each device.
4910 */
4911 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
4912 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4913 WARN(1, KERN_ERR
4914 "BTRFS: read-only device in alloc_list\n");
4915 continue;
4916 }
4917
4918 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4919 &device->dev_state) ||
4920 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4921 continue;
4922
4923 if (device->total_bytes > device->bytes_used)
4924 total_avail = device->total_bytes - device->bytes_used;
4925 else
4926 total_avail = 0;
4927
4928 /* If there is no space on this device, skip it. */
Naohiro Aota6aafb302020-02-25 12:56:15 +09004929 if (total_avail < ctl->dev_extent_min)
Naohiro Aota560156c2020-02-25 12:56:12 +09004930 continue;
4931
4932 ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
4933 &max_avail);
4934 if (ret && ret != -ENOSPC)
4935 return ret;
4936
4937 if (ret == 0)
4938 max_avail = dev_extent_want;
4939
Naohiro Aota6aafb302020-02-25 12:56:15 +09004940 if (max_avail < ctl->dev_extent_min) {
Naohiro Aota560156c2020-02-25 12:56:12 +09004941 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4942 btrfs_debug(info,
4943 "%s: devid %llu has no free space, have=%llu want=%llu",
4944 __func__, device->devid, max_avail,
Naohiro Aota6aafb302020-02-25 12:56:15 +09004945 ctl->dev_extent_min);
Naohiro Aota560156c2020-02-25 12:56:12 +09004946 continue;
4947 }
4948
4949 if (ndevs == fs_devices->rw_devices) {
4950 WARN(1, "%s: found more than %llu devices\n",
4951 __func__, fs_devices->rw_devices);
4952 break;
4953 }
4954 devices_info[ndevs].dev_offset = dev_offset;
4955 devices_info[ndevs].max_avail = max_avail;
4956 devices_info[ndevs].total_avail = total_avail;
4957 devices_info[ndevs].dev = device;
4958 ++ndevs;
4959 }
4960 ctl->ndevs = ndevs;
4961
4962 /*
4963 * now sort the devices by hole size / available space
4964 */
4965 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4966 btrfs_cmp_device_info, NULL);
4967
4968 return 0;
4969}
4970
Naohiro Aota5badf512020-02-25 12:56:13 +09004971static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
4972 struct btrfs_device_info *devices_info)
4973{
4974 /* Number of stripes that count for block group size */
4975 int data_stripes;
4976
4977 /*
4978 * The primary goal is to maximize the number of stripes, so use as
4979 * many devices as possible, even if the stripes are not maximum sized.
4980 *
4981 * The DUP profile stores more than one stripe per device, the
4982 * max_avail is the total size so we have to adjust.
4983 */
4984 ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail,
4985 ctl->dev_stripes);
4986 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
4987
4988 /* This will have to be fixed for RAID1 and RAID10 over more drives */
4989 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
4990
4991 /*
4992 * Use the number of data stripes to figure out how big this chunk is
4993 * really going to be in terms of logical address space, and compare
4994 * that answer with the max chunk size. If it's higher, we try to
4995 * reduce stripe_size.
4996 */
4997 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
4998 /*
4999 * Reduce stripe_size, round it up to a 16MB boundary again and
5000 * then use it, unless it ends up being even bigger than the
5001 * previous value we had already.
5002 */
5003 ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
5004 data_stripes), SZ_16M),
5005 ctl->stripe_size);
5006 }
5007
5008 /* Align to BTRFS_STRIPE_LEN */
5009 ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN);
5010 ctl->chunk_size = ctl->stripe_size * data_stripes;
5011
5012 return 0;
5013}
5014
5015static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
5016 struct alloc_chunk_ctl *ctl,
5017 struct btrfs_device_info *devices_info)
5018{
5019 struct btrfs_fs_info *info = fs_devices->fs_info;
5020
5021 /*
5022 * Round down to number of usable stripes, devs_increment can be any
5023 * number so we can't use round_down() that requires power of 2, while
5024 * rounddown is safe.
5025 */
5026 ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment);
5027
5028 if (ctl->ndevs < ctl->devs_min) {
5029 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5030 btrfs_debug(info,
5031 "%s: not enough devices with free space: have=%d minimum required=%d",
5032 __func__, ctl->ndevs, ctl->devs_min);
5033 }
5034 return -ENOSPC;
5035 }
5036
5037 ctl->ndevs = min(ctl->ndevs, ctl->devs_max);
5038
5039 switch (fs_devices->chunk_alloc_policy) {
5040 case BTRFS_CHUNK_ALLOC_REGULAR:
5041 return decide_stripe_size_regular(ctl, devices_info);
5042 default:
5043 BUG();
5044 }
5045}
5046
Naohiro Aotadce580c2020-02-25 12:56:14 +09005047static int create_chunk(struct btrfs_trans_handle *trans,
5048 struct alloc_chunk_ctl *ctl,
5049 struct btrfs_device_info *devices_info)
5050{
5051 struct btrfs_fs_info *info = trans->fs_info;
5052 struct map_lookup *map = NULL;
5053 struct extent_map_tree *em_tree;
5054 struct extent_map *em;
5055 u64 start = ctl->start;
5056 u64 type = ctl->type;
5057 int ret;
5058 int i;
5059 int j;
5060
5061 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);
5062 if (!map)
5063 return -ENOMEM;
5064 map->num_stripes = ctl->num_stripes;
5065
5066 for (i = 0; i < ctl->ndevs; ++i) {
5067 for (j = 0; j < ctl->dev_stripes; ++j) {
5068 int s = i * ctl->dev_stripes + j;
5069 map->stripes[s].dev = devices_info[i].dev;
5070 map->stripes[s].physical = devices_info[i].dev_offset +
5071 j * ctl->stripe_size;
5072 }
5073 }
5074 map->stripe_len = BTRFS_STRIPE_LEN;
5075 map->io_align = BTRFS_STRIPE_LEN;
5076 map->io_width = BTRFS_STRIPE_LEN;
5077 map->type = type;
5078 map->sub_stripes = ctl->sub_stripes;
5079
5080 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
5081
5082 em = alloc_extent_map();
5083 if (!em) {
5084 kfree(map);
5085 return -ENOMEM;
5086 }
5087 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5088 em->map_lookup = map;
5089 em->start = start;
5090 em->len = ctl->chunk_size;
5091 em->block_start = 0;
5092 em->block_len = em->len;
5093 em->orig_block_len = ctl->stripe_size;
5094
5095 em_tree = &info->mapping_tree;
5096 write_lock(&em_tree->lock);
5097 ret = add_extent_mapping(em_tree, em, 0);
5098 if (ret) {
5099 write_unlock(&em_tree->lock);
5100 free_extent_map(em);
5101 return ret;
5102 }
5103 write_unlock(&em_tree->lock);
5104
5105 ret = btrfs_make_block_group(trans, 0, type, start, ctl->chunk_size);
5106 if (ret)
5107 goto error_del_extent;
5108
5109 for (i = 0; i < map->num_stripes; i++) {
5110 struct btrfs_device *dev = map->stripes[i].dev;
5111
5112 btrfs_device_set_bytes_used(dev,
5113 dev->bytes_used + ctl->stripe_size);
5114 if (list_empty(&dev->post_commit_list))
5115 list_add_tail(&dev->post_commit_list,
5116 &trans->transaction->dev_update_list);
5117 }
5118
5119 atomic64_sub(ctl->stripe_size * map->num_stripes,
5120 &info->free_chunk_space);
5121
5122 free_extent_map(em);
5123 check_raid56_incompat_flag(info, type);
5124 check_raid1c34_incompat_flag(info, type);
5125
5126 return 0;
5127
5128error_del_extent:
5129 write_lock(&em_tree->lock);
5130 remove_extent_mapping(em_tree, em);
5131 write_unlock(&em_tree->lock);
5132
5133 /* One for our allocation */
5134 free_extent_map(em);
5135 /* One for the tree reference */
5136 free_extent_map(em);
5137
5138 return ret;
5139}
5140
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005141int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00005142{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005143 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00005144 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Arne Jansen73c5de02011-04-12 12:07:57 +02005145 struct btrfs_device_info *devices_info = NULL;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09005146 struct alloc_chunk_ctl ctl;
Miao Xieb2117a32011-01-05 10:07:28 +00005147 int ret;
Miao Xieb2117a32011-01-05 10:07:28 +00005148
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005149 lockdep_assert_held(&info->chunk_mutex);
5150
Naohiro Aotab25c19f2020-02-25 12:56:07 +09005151 if (!alloc_profile_is_valid(type, 0)) {
5152 ASSERT(0);
5153 return -EINVAL;
5154 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005155
Qu Wenruo4117f202018-01-22 13:50:54 +08005156 if (list_empty(&fs_devices->alloc_list)) {
5157 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5158 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005159 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005160 }
Miao Xieb2117a32011-01-05 10:07:28 +00005161
Naohiro Aota27c314d2020-02-25 12:56:11 +09005162 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
5163 btrfs_err(info, "invalid chunk type 0x%llx requested", type);
5164 ASSERT(0);
5165 return -EINVAL;
Arne Jansen73c5de02011-04-12 12:07:57 +02005166 }
5167
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005168 ctl.start = find_next_chunk(info);
Naohiro Aota27c314d2020-02-25 12:56:11 +09005169 ctl.type = type;
5170 init_alloc_chunk_ctl(fs_devices, &ctl);
Miao Xieb2117a32011-01-05 10:07:28 +00005171
David Sterba31e818f2015-02-20 18:00:26 +01005172 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005173 GFP_NOFS);
5174 if (!devices_info)
5175 return -ENOMEM;
5176
Naohiro Aota560156c2020-02-25 12:56:12 +09005177 ret = gather_device_info(fs_devices, &ctl, devices_info);
5178 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005179 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005180
Naohiro Aota5badf512020-02-25 12:56:13 +09005181 ret = decide_stripe_size(fs_devices, &ctl, devices_info);
5182 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005183 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005184
Naohiro Aotadce580c2020-02-25 12:56:14 +09005185 ret = create_chunk(trans, &ctl, devices_info);
Chris Mason9b3f68b2008-04-18 10:29:51 -04005186
Naohiro Aotadce580c2020-02-25 12:56:14 +09005187out:
Miao Xieb2117a32011-01-05 10:07:28 +00005188 kfree(devices_info);
5189 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005190}
5191
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005192/*
5193 * Chunk allocation falls into two parts. The first part does work
5194 * that makes the new allocated chunk usable, but does not do any operation
5195 * that modifies the chunk tree. The second part does the work that
5196 * requires modifying the chunk tree. This division is important for the
5197 * bootstrap process of adding storage to a seed btrfs.
5198 */
Josef Bacik6df9a952013-06-27 13:22:46 -04005199int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005200 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005201{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005202 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005203 struct btrfs_root *extent_root = fs_info->extent_root;
5204 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005205 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005206 struct btrfs_device *device;
5207 struct btrfs_chunk *chunk;
5208 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005209 struct extent_map *em;
5210 struct map_lookup *map;
5211 size_t item_size;
5212 u64 dev_offset;
5213 u64 stripe_size;
5214 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005215 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005216
Omar Sandoval60ca8422018-05-16 16:34:31 -07005217 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005218 if (IS_ERR(em))
5219 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005220
Jeff Mahoney95617d62015-06-03 10:55:48 -04005221 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005222 item_size = btrfs_chunk_item_size(map->num_stripes);
5223 stripe_size = em->orig_block_len;
5224
5225 chunk = kzalloc(item_size, GFP_NOFS);
5226 if (!chunk) {
5227 ret = -ENOMEM;
5228 goto out;
5229 }
5230
Filipe Manana50460e32015-11-20 10:42:47 +00005231 /*
5232 * Take the device list mutex to prevent races with the final phase of
5233 * a device replace operation that replaces the device object associated
5234 * with the map's stripes, because the device object's id can change
5235 * at any time during that final phase of the device replace operation
5236 * (dev-replace.c:btrfs_dev_replace_finishing()).
5237 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005238 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005239 for (i = 0; i < map->num_stripes; i++) {
5240 device = map->stripes[i].dev;
5241 dev_offset = map->stripes[i].physical;
5242
Yan Zheng2b820322008-11-17 21:11:30 -05005243 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005244 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005245 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005246 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5247 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005248 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005249 break;
5250 }
5251 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005252 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005253 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005254 }
5255
Yan Zheng2b820322008-11-17 21:11:30 -05005256 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005257 for (i = 0; i < map->num_stripes; i++) {
5258 device = map->stripes[i].dev;
5259 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005260
5261 btrfs_set_stack_stripe_devid(stripe, device->devid);
5262 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5263 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5264 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005265 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005266 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005267
5268 btrfs_set_stack_chunk_length(chunk, chunk_size);
5269 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5270 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5271 btrfs_set_stack_chunk_type(chunk, map->type);
5272 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5273 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5274 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005275 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005276 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5277
5278 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5279 key.type = BTRFS_CHUNK_ITEM_KEY;
5280 key.offset = chunk_offset;
5281
5282 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005283 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5284 /*
5285 * TODO: Cleanup of inserted chunk root in case of
5286 * failure.
5287 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005288 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005289 }
liubo1abe9b82011-03-24 11:18:59 +00005290
Josef Bacik6df9a952013-06-27 13:22:46 -04005291out:
Yan Zheng2b820322008-11-17 21:11:30 -05005292 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005293 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005294 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005295}
5296
David Sterba6f8e0fc2019-03-20 16:29:13 +01005297static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005298{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005299 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005300 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005301 int ret;
5302
Jeff Mahoney1b868262017-05-17 11:38:35 -04005303 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005304 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005305 if (ret)
5306 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005307
Jeff Mahoney1b868262017-05-17 11:38:35 -04005308 alloc_profile = btrfs_system_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005309 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005310 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005311}
5312
Miao Xied20983b2014-07-03 18:22:13 +08005313static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5314{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005315 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005316
David Sterbafc9a2ac2019-05-17 11:43:22 +02005317 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005318}
5319
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005320int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005321{
5322 struct extent_map *em;
5323 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005324 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005325 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005326 int i;
5327
Omar Sandoval60ca8422018-05-16 16:34:31 -07005328 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005329 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005330 return 1;
5331
Jeff Mahoney95617d62015-06-03 10:55:48 -04005332 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005333 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005334 if (test_bit(BTRFS_DEV_STATE_MISSING,
5335 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005336 miss_ndevs++;
5337 continue;
5338 }
Anand Jainebbede42017-12-04 12:54:52 +08005339 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5340 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005341 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005342 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005343 }
5344 }
Miao Xied20983b2014-07-03 18:22:13 +08005345
5346 /*
5347 * If the number of missing devices is larger than max errors,
5348 * we can not write the data into that chunk successfully, so
5349 * set it readonly.
5350 */
5351 if (miss_ndevs > btrfs_chunk_max_errors(map))
5352 readonly = 1;
5353end:
Yan Zheng2b820322008-11-17 21:11:30 -05005354 free_extent_map(em);
5355 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005356}
5357
David Sterbac8bf1b62019-05-17 11:43:17 +02005358void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005359{
5360 struct extent_map *em;
5361
Chris Masond3977122009-01-05 21:25:51 -05005362 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005363 write_lock(&tree->lock);
5364 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005365 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005366 remove_extent_mapping(tree, em);
5367 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005368 if (!em)
5369 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005370 /* once for us */
5371 free_extent_map(em);
5372 /* once for the tree */
5373 free_extent_map(em);
5374 }
5375}
5376
Stefan Behrens5d964052012-11-05 14:59:07 +01005377int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005378{
5379 struct extent_map *em;
5380 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005381 int ret;
5382
Omar Sandoval60ca8422018-05-16 16:34:31 -07005383 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005384 if (IS_ERR(em))
5385 /*
5386 * We could return errors for these cases, but that could get
5387 * ugly and we'd probably do the same thing which is just not do
5388 * anything else and exit, so return 1 so the callers don't try
5389 * to use other copies.
5390 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005391 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005392
Jeff Mahoney95617d62015-06-03 10:55:48 -04005393 map = em->map_lookup;
David Sterbac7369b32019-05-31 15:39:31 +02005394 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
Chris Masonf1885912008-04-09 16:28:12 -04005395 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005396 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5397 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005398 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5399 ret = 2;
5400 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005401 /*
5402 * There could be two corrupted data stripes, we need
5403 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005404 *
Liu Bo8810f752018-01-02 13:36:41 -07005405 * Fail a stripe at a time on every retry except the
5406 * stripe under reconstruction.
5407 */
5408 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005409 else
5410 ret = 1;
5411 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005412
David Sterbacb5583d2018-09-07 16:11:23 +02005413 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005414 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5415 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005416 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005417 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005418
Chris Masonf1885912008-04-09 16:28:12 -04005419 return ret;
5420}
5421
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005422unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005423 u64 logical)
5424{
5425 struct extent_map *em;
5426 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005427 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005428
Omar Sandoval60ca8422018-05-16 16:34:31 -07005429 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005430
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005431 if (!WARN_ON(IS_ERR(em))) {
5432 map = em->map_lookup;
5433 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5434 len = map->stripe_len * nr_data_stripes(map);
5435 free_extent_map(em);
5436 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005437 return len;
5438}
5439
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005440int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005441{
5442 struct extent_map *em;
5443 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005444 int ret = 0;
5445
Omar Sandoval60ca8422018-05-16 16:34:31 -07005446 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005447
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005448 if(!WARN_ON(IS_ERR(em))) {
5449 map = em->map_lookup;
5450 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5451 ret = 1;
5452 free_extent_map(em);
5453 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005454 return ret;
5455}
5456
Stefan Behrens30d98612012-11-06 14:52:18 +01005457static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005458 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005459 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005460{
5461 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005462 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005463 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005464 int tolerance;
5465 struct btrfs_device *srcdev;
5466
Anand Jain99f92a72018-03-14 16:29:12 +08005467 ASSERT((map->type &
David Sterbac7369b32019-05-31 15:39:31 +02005468 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
Anand Jain99f92a72018-03-14 16:29:12 +08005469
5470 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5471 num_stripes = map->sub_stripes;
5472 else
5473 num_stripes = map->num_stripes;
5474
Anand Jain8ba0ae72018-03-14 16:29:13 +08005475 preferred_mirror = first + current->pid % num_stripes;
5476
Stefan Behrens30d98612012-11-06 14:52:18 +01005477 if (dev_replace_is_ongoing &&
5478 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5479 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5480 srcdev = fs_info->dev_replace.srcdev;
5481 else
5482 srcdev = NULL;
5483
5484 /*
5485 * try to avoid the drive that is the source drive for a
5486 * dev-replace procedure, only choose it if no other non-missing
5487 * mirror is available
5488 */
5489 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005490 if (map->stripes[preferred_mirror].dev->bdev &&
5491 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5492 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005493 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005494 if (map->stripes[i].dev->bdev &&
5495 (tolerance || map->stripes[i].dev != srcdev))
5496 return i;
5497 }
Chris Masondfe25022008-05-13 13:46:40 -04005498 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005499
Chris Masondfe25022008-05-13 13:46:40 -04005500 /* we couldn't find one that doesn't fail. Just return something
5501 * and the io error handling code will clean up eventually
5502 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005503 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005504}
5505
David Woodhouse53b381b2013-01-29 18:40:14 -05005506/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005507static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005508{
David Woodhouse53b381b2013-01-29 18:40:14 -05005509 int i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005510 int again = 1;
5511
5512 while (again) {
5513 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005514 for (i = 0; i < num_stripes - 1; i++) {
David Sterbaeeb6f172019-11-28 15:31:17 +01005515 /* Swap if parity is on a smaller index */
5516 if (bbio->raid_map[i] > bbio->raid_map[i + 1]) {
5517 swap(bbio->stripes[i], bbio->stripes[i + 1]);
5518 swap(bbio->raid_map[i], bbio->raid_map[i + 1]);
David Woodhouse53b381b2013-01-29 18:40:14 -05005519 again = 1;
5520 }
5521 }
5522 }
5523}
5524
Zhao Lei6e9606d2015-01-20 15:11:34 +08005525static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5526{
5527 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005528 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005529 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005530 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005531 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005532 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005533 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005534 /*
5535 * plus the raid_map, which includes both the tgt dev
5536 * and the stripes
5537 */
5538 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005539 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005540
5541 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005542 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005543
Nikolay Borisov608769a2020-07-02 16:46:41 +03005544 bbio->tgtdev_map = (int *)(bbio->stripes + total_stripes);
5545 bbio->raid_map = (u64 *)(bbio->tgtdev_map + real_stripes);
5546
Zhao Lei6e9606d2015-01-20 15:11:34 +08005547 return bbio;
5548}
5549
5550void btrfs_get_bbio(struct btrfs_bio *bbio)
5551{
Elena Reshetova140475a2017-03-03 10:55:10 +02005552 WARN_ON(!refcount_read(&bbio->refs));
5553 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005554}
5555
5556void btrfs_put_bbio(struct btrfs_bio *bbio)
5557{
5558 if (!bbio)
5559 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005560 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005561 kfree(bbio);
5562}
5563
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005564/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5565/*
5566 * Please note that, discard won't be sent to target device of device
5567 * replace.
5568 */
5569static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005570 u64 logical, u64 *length_ret,
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005571 struct btrfs_bio **bbio_ret)
5572{
5573 struct extent_map *em;
5574 struct map_lookup *map;
5575 struct btrfs_bio *bbio;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005576 u64 length = *length_ret;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005577 u64 offset;
5578 u64 stripe_nr;
5579 u64 stripe_nr_end;
5580 u64 stripe_end_offset;
5581 u64 stripe_cnt;
5582 u64 stripe_len;
5583 u64 stripe_offset;
5584 u64 num_stripes;
5585 u32 stripe_index;
5586 u32 factor = 0;
5587 u32 sub_stripes = 0;
5588 u64 stripes_per_dev = 0;
5589 u32 remaining_stripes = 0;
5590 u32 last_stripe = 0;
5591 int ret = 0;
5592 int i;
5593
5594 /* discard always return a bbio */
5595 ASSERT(bbio_ret);
5596
Omar Sandoval60ca8422018-05-16 16:34:31 -07005597 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005598 if (IS_ERR(em))
5599 return PTR_ERR(em);
5600
5601 map = em->map_lookup;
5602 /* we don't discard raid56 yet */
5603 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5604 ret = -EOPNOTSUPP;
5605 goto out;
5606 }
5607
5608 offset = logical - em->start;
Qu Wenruo2d974612019-10-23 21:57:26 +08005609 length = min_t(u64, em->start + em->len - logical, length);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005610 *length_ret = length;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005611
5612 stripe_len = map->stripe_len;
5613 /*
5614 * stripe_nr counts the total number of stripes we have to stride
5615 * to get to this block
5616 */
5617 stripe_nr = div64_u64(offset, stripe_len);
5618
5619 /* stripe_offset is the offset of this block in its stripe */
5620 stripe_offset = offset - stripe_nr * stripe_len;
5621
5622 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005623 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005624 stripe_cnt = stripe_nr_end - stripe_nr;
5625 stripe_end_offset = stripe_nr_end * map->stripe_len -
5626 (offset + length);
5627 /*
5628 * after this, stripe_nr is the number of stripes on this
5629 * device we have to walk to find the data, and stripe_index is
5630 * the number of our device in the stripe array
5631 */
5632 num_stripes = 1;
5633 stripe_index = 0;
5634 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5635 BTRFS_BLOCK_GROUP_RAID10)) {
5636 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5637 sub_stripes = 1;
5638 else
5639 sub_stripes = map->sub_stripes;
5640
5641 factor = map->num_stripes / sub_stripes;
5642 num_stripes = min_t(u64, map->num_stripes,
5643 sub_stripes * stripe_cnt);
5644 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5645 stripe_index *= sub_stripes;
5646 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5647 &remaining_stripes);
5648 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5649 last_stripe *= sub_stripes;
David Sterbac7369b32019-05-31 15:39:31 +02005650 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005651 BTRFS_BLOCK_GROUP_DUP)) {
5652 num_stripes = map->num_stripes;
5653 } else {
5654 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5655 &stripe_index);
5656 }
5657
5658 bbio = alloc_btrfs_bio(num_stripes, 0);
5659 if (!bbio) {
5660 ret = -ENOMEM;
5661 goto out;
5662 }
5663
5664 for (i = 0; i < num_stripes; i++) {
5665 bbio->stripes[i].physical =
5666 map->stripes[stripe_index].physical +
5667 stripe_offset + stripe_nr * map->stripe_len;
5668 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5669
5670 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5671 BTRFS_BLOCK_GROUP_RAID10)) {
5672 bbio->stripes[i].length = stripes_per_dev *
5673 map->stripe_len;
5674
5675 if (i / sub_stripes < remaining_stripes)
5676 bbio->stripes[i].length +=
5677 map->stripe_len;
5678
5679 /*
5680 * Special for the first stripe and
5681 * the last stripe:
5682 *
5683 * |-------|...|-------|
5684 * |----------|
5685 * off end_off
5686 */
5687 if (i < sub_stripes)
5688 bbio->stripes[i].length -=
5689 stripe_offset;
5690
5691 if (stripe_index >= last_stripe &&
5692 stripe_index <= (last_stripe +
5693 sub_stripes - 1))
5694 bbio->stripes[i].length -=
5695 stripe_end_offset;
5696
5697 if (i == sub_stripes - 1)
5698 stripe_offset = 0;
5699 } else {
5700 bbio->stripes[i].length = length;
5701 }
5702
5703 stripe_index++;
5704 if (stripe_index == map->num_stripes) {
5705 stripe_index = 0;
5706 stripe_nr++;
5707 }
5708 }
5709
5710 *bbio_ret = bbio;
5711 bbio->map_type = map->type;
5712 bbio->num_stripes = num_stripes;
5713out:
5714 free_extent_map(em);
5715 return ret;
5716}
5717
Liu Bo5ab56092017-03-14 13:33:57 -07005718/*
5719 * In dev-replace case, for repair case (that's the only case where the mirror
5720 * is selected explicitly when calling btrfs_map_block), blocks left of the
5721 * left cursor can also be read from the target drive.
5722 *
5723 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5724 * array of stripes.
5725 * For READ, it also needs to be supported using the same mirror number.
5726 *
5727 * If the requested block is not left of the left cursor, EIO is returned. This
5728 * can happen because btrfs_num_copies() returns one more in the dev-replace
5729 * case.
5730 */
5731static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5732 u64 logical, u64 length,
5733 u64 srcdev_devid, int *mirror_num,
5734 u64 *physical)
5735{
5736 struct btrfs_bio *bbio = NULL;
5737 int num_stripes;
5738 int index_srcdev = 0;
5739 int found = 0;
5740 u64 physical_of_found = 0;
5741 int i;
5742 int ret = 0;
5743
5744 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5745 logical, &length, &bbio, 0, 0);
5746 if (ret) {
5747 ASSERT(bbio == NULL);
5748 return ret;
5749 }
5750
5751 num_stripes = bbio->num_stripes;
5752 if (*mirror_num > num_stripes) {
5753 /*
5754 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5755 * that means that the requested area is not left of the left
5756 * cursor
5757 */
5758 btrfs_put_bbio(bbio);
5759 return -EIO;
5760 }
5761
5762 /*
5763 * process the rest of the function using the mirror_num of the source
5764 * drive. Therefore look it up first. At the end, patch the device
5765 * pointer to the one of the target drive.
5766 */
5767 for (i = 0; i < num_stripes; i++) {
5768 if (bbio->stripes[i].dev->devid != srcdev_devid)
5769 continue;
5770
5771 /*
5772 * In case of DUP, in order to keep it simple, only add the
5773 * mirror with the lowest physical address
5774 */
5775 if (found &&
5776 physical_of_found <= bbio->stripes[i].physical)
5777 continue;
5778
5779 index_srcdev = i;
5780 found = 1;
5781 physical_of_found = bbio->stripes[i].physical;
5782 }
5783
5784 btrfs_put_bbio(bbio);
5785
5786 ASSERT(found);
5787 if (!found)
5788 return -EIO;
5789
5790 *mirror_num = index_srcdev + 1;
5791 *physical = physical_of_found;
5792 return ret;
5793}
5794
Liu Bo73c0f222017-03-14 13:33:58 -07005795static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5796 struct btrfs_bio **bbio_ret,
5797 struct btrfs_dev_replace *dev_replace,
5798 int *num_stripes_ret, int *max_errors_ret)
5799{
5800 struct btrfs_bio *bbio = *bbio_ret;
5801 u64 srcdev_devid = dev_replace->srcdev->devid;
5802 int tgtdev_indexes = 0;
5803 int num_stripes = *num_stripes_ret;
5804 int max_errors = *max_errors_ret;
5805 int i;
5806
5807 if (op == BTRFS_MAP_WRITE) {
5808 int index_where_to_add;
5809
5810 /*
5811 * duplicate the write operations while the dev replace
5812 * procedure is running. Since the copying of the old disk to
5813 * the new disk takes place at run time while the filesystem is
5814 * mounted writable, the regular write operations to the old
5815 * disk have to be duplicated to go to the new disk as well.
5816 *
5817 * Note that device->missing is handled by the caller, and that
5818 * the write to the old disk is already set up in the stripes
5819 * array.
5820 */
5821 index_where_to_add = num_stripes;
5822 for (i = 0; i < num_stripes; i++) {
5823 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5824 /* write to new disk, too */
5825 struct btrfs_bio_stripe *new =
5826 bbio->stripes + index_where_to_add;
5827 struct btrfs_bio_stripe *old =
5828 bbio->stripes + i;
5829
5830 new->physical = old->physical;
5831 new->length = old->length;
5832 new->dev = dev_replace->tgtdev;
5833 bbio->tgtdev_map[i] = index_where_to_add;
5834 index_where_to_add++;
5835 max_errors++;
5836 tgtdev_indexes++;
5837 }
5838 }
5839 num_stripes = index_where_to_add;
5840 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5841 int index_srcdev = 0;
5842 int found = 0;
5843 u64 physical_of_found = 0;
5844
5845 /*
5846 * During the dev-replace procedure, the target drive can also
5847 * be used to read data in case it is needed to repair a corrupt
5848 * block elsewhere. This is possible if the requested area is
5849 * left of the left cursor. In this area, the target drive is a
5850 * full copy of the source drive.
5851 */
5852 for (i = 0; i < num_stripes; i++) {
5853 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5854 /*
5855 * In case of DUP, in order to keep it simple,
5856 * only add the mirror with the lowest physical
5857 * address
5858 */
5859 if (found &&
5860 physical_of_found <=
5861 bbio->stripes[i].physical)
5862 continue;
5863 index_srcdev = i;
5864 found = 1;
5865 physical_of_found = bbio->stripes[i].physical;
5866 }
5867 }
5868 if (found) {
5869 struct btrfs_bio_stripe *tgtdev_stripe =
5870 bbio->stripes + num_stripes;
5871
5872 tgtdev_stripe->physical = physical_of_found;
5873 tgtdev_stripe->length =
5874 bbio->stripes[index_srcdev].length;
5875 tgtdev_stripe->dev = dev_replace->tgtdev;
5876 bbio->tgtdev_map[index_srcdev] = num_stripes;
5877
5878 tgtdev_indexes++;
5879 num_stripes++;
5880 }
5881 }
5882
5883 *num_stripes_ret = num_stripes;
5884 *max_errors_ret = max_errors;
5885 bbio->num_tgtdevs = tgtdev_indexes;
5886 *bbio_ret = bbio;
5887}
5888
Liu Bo2b19a1f2017-03-14 13:34:00 -07005889static bool need_full_stripe(enum btrfs_map_op op)
5890{
5891 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5892}
5893
Nikolay Borisov5f141122019-06-03 12:05:03 +03005894/*
5895 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5896 * tuple. This information is used to calculate how big a
5897 * particular bio can get before it straddles a stripe.
5898 *
5899 * @fs_info - the filesystem
5900 * @logical - address that we want to figure out the geometry of
5901 * @len - the length of IO we are going to perform, starting at @logical
5902 * @op - type of operation - write or read
5903 * @io_geom - pointer used to return values
5904 *
5905 * Returns < 0 in case a chunk for the given logical address cannot be found,
5906 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5907 */
5908int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005909 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
Nikolay Borisov5f141122019-06-03 12:05:03 +03005910{
5911 struct extent_map *em;
5912 struct map_lookup *map;
5913 u64 offset;
5914 u64 stripe_offset;
5915 u64 stripe_nr;
5916 u64 stripe_len;
5917 u64 raid56_full_stripe_start = (u64)-1;
5918 int data_stripes;
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005919 int ret = 0;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005920
5921 ASSERT(op != BTRFS_MAP_DISCARD);
5922
5923 em = btrfs_get_chunk_map(fs_info, logical, len);
5924 if (IS_ERR(em))
5925 return PTR_ERR(em);
5926
5927 map = em->map_lookup;
5928 /* Offset of this logical address in the chunk */
5929 offset = logical - em->start;
5930 /* Len of a stripe in a chunk */
5931 stripe_len = map->stripe_len;
5932 /* Stripe wher this block falls in */
5933 stripe_nr = div64_u64(offset, stripe_len);
5934 /* Offset of stripe in the chunk */
5935 stripe_offset = stripe_nr * stripe_len;
5936 if (offset < stripe_offset) {
5937 btrfs_crit(fs_info,
5938"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5939 stripe_offset, offset, em->start, logical, stripe_len);
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005940 ret = -EINVAL;
5941 goto out;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005942 }
5943
5944 /* stripe_offset is the offset of this block in its stripe */
5945 stripe_offset = offset - stripe_offset;
5946 data_stripes = nr_data_stripes(map);
5947
5948 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5949 u64 max_len = stripe_len - stripe_offset;
5950
5951 /*
5952 * In case of raid56, we need to know the stripe aligned start
5953 */
5954 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5955 unsigned long full_stripe_len = stripe_len * data_stripes;
5956 raid56_full_stripe_start = offset;
5957
5958 /*
5959 * Allow a write of a full stripe, but make sure we
5960 * don't allow straddling of stripes
5961 */
5962 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5963 full_stripe_len);
5964 raid56_full_stripe_start *= full_stripe_len;
5965
5966 /*
5967 * For writes to RAID[56], allow a full stripeset across
5968 * all disks. For other RAID types and for RAID[56]
5969 * reads, just allow a single stripe (on a single disk).
5970 */
5971 if (op == BTRFS_MAP_WRITE) {
5972 max_len = stripe_len * data_stripes -
5973 (offset - raid56_full_stripe_start);
5974 }
5975 }
5976 len = min_t(u64, em->len - offset, max_len);
5977 } else {
5978 len = em->len - offset;
5979 }
5980
5981 io_geom->len = len;
5982 io_geom->offset = offset;
5983 io_geom->stripe_len = stripe_len;
5984 io_geom->stripe_nr = stripe_nr;
5985 io_geom->stripe_offset = stripe_offset;
5986 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5987
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005988out:
5989 /* once for us */
5990 free_extent_map(em);
5991 return ret;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005992}
5993
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005994static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5995 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005996 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005997 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005998 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005999{
6000 struct extent_map *em;
6001 struct map_lookup *map;
Chris Mason593060d2008-03-25 16:50:33 -04006002 u64 stripe_offset;
6003 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006004 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006005 u32 stripe_index;
David Sterbacff82672019-05-17 11:43:45 +02006006 int data_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04006007 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006008 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006009 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006010 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006011 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006012 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006013 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6014 int dev_replace_is_ongoing = 0;
6015 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006016 int patch_the_first_stripe_for_dev_replace = 0;
6017 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006018 u64 raid56_full_stripe_start = (u64)-1;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006019 struct btrfs_io_geometry geom;
6020
6021 ASSERT(bbio_ret);
David Sterba75fb2e92018-08-03 00:36:29 +02006022 ASSERT(op != BTRFS_MAP_DISCARD);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006023
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006024 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
6025 if (ret < 0)
6026 return ret;
6027
Omar Sandoval60ca8422018-05-16 16:34:31 -07006028 em = btrfs_get_chunk_map(fs_info, logical, *length);
Dan Carpenterf1136982019-08-09 17:07:39 +03006029 ASSERT(!IS_ERR(em));
Jeff Mahoney95617d62015-06-03 10:55:48 -04006030 map = em->map_lookup;
Chris Mason593060d2008-03-25 16:50:33 -04006031
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006032 *length = geom.len;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006033 stripe_len = geom.stripe_len;
6034 stripe_nr = geom.stripe_nr;
6035 stripe_offset = geom.stripe_offset;
6036 raid56_full_stripe_start = geom.raid56_stripe_offset;
David Sterbacff82672019-05-17 11:43:45 +02006037 data_stripes = nr_data_stripes(map);
Chris Mason593060d2008-03-25 16:50:33 -04006038
David Sterbacb5583d2018-09-07 16:11:23 +02006039 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006040 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006041 /*
6042 * Hold the semaphore for read during the whole operation, write is
6043 * requested at commit time but must wait.
6044 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006045 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006046 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006047
Stefan Behrensad6d6202012-11-06 15:06:47 +01006048 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006049 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006050 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6051 dev_replace->srcdev->devid,
6052 &mirror_num,
6053 &physical_to_patch_in_first_stripe);
6054 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006055 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006056 else
6057 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006058 } else if (mirror_num > map->num_stripes) {
6059 mirror_num = 0;
6060 }
6061
Chris Masonf2d8d742008-04-21 10:03:05 -04006062 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006063 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006064 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006065 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6066 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006067 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006068 mirror_num = 1;
David Sterbac7369b32019-05-31 15:39:31 +02006069 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006070 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006071 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006072 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006073 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006074 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006075 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006076 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006077 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006078 }
Chris Mason2fff7342008-04-29 14:12:09 -04006079
Chris Mason611f0e02008-04-03 16:29:03 -04006080 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006081 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006082 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006083 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006084 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006085 } else {
6086 mirror_num = 1;
6087 }
Chris Mason2fff7342008-04-29 14:12:09 -04006088
Chris Mason321aecc2008-04-16 10:49:51 -04006089 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006090 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006091
David Sterba47c57132015-02-20 18:43:47 +01006092 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006093 stripe_index *= map->sub_stripes;
6094
Anand Jainde483732017-10-12 16:43:00 +08006095 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006096 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006097 else if (mirror_num)
6098 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006099 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006100 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006101 stripe_index = find_live_mirror(fs_info, map,
6102 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006103 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006104 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006105 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006106
Zhao Leiffe2d202015-01-20 15:11:44 +08006107 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006108 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006109 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006110 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbacff82672019-05-17 11:43:45 +02006111 stripe_len * data_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05006112
6113 /* RAID[56] write or recovery. Return all stripes */
6114 num_stripes = map->num_stripes;
6115 max_errors = nr_parity_stripes(map);
6116
David Woodhouse53b381b2013-01-29 18:40:14 -05006117 *length = map->stripe_len;
6118 stripe_index = 0;
6119 stripe_offset = 0;
6120 } else {
6121 /*
6122 * Mirror #0 or #1 means the original data block.
6123 * Mirror #2 is RAID5 parity block.
6124 * Mirror #3 is RAID6 Q block.
6125 */
David Sterba47c57132015-02-20 18:43:47 +01006126 stripe_nr = div_u64_rem(stripe_nr,
David Sterbacff82672019-05-17 11:43:45 +02006127 data_stripes, &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006128 if (mirror_num > 1)
David Sterbacff82672019-05-17 11:43:45 +02006129 stripe_index = data_stripes + mirror_num - 2;
David Woodhouse53b381b2013-01-29 18:40:14 -05006130
6131 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006132 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6133 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006134 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006135 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006136 }
Chris Mason8790d502008-04-03 16:29:03 -04006137 } else {
6138 /*
David Sterba47c57132015-02-20 18:43:47 +01006139 * after this, stripe_nr is the number of stripes on this
6140 * device we have to walk to find the data, and stripe_index is
6141 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006142 */
David Sterba47c57132015-02-20 18:43:47 +01006143 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6144 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006145 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006146 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006147 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006148 btrfs_crit(fs_info,
6149 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006150 stripe_index, map->num_stripes);
6151 ret = -EINVAL;
6152 goto out;
6153 }
Chris Mason593060d2008-03-25 16:50:33 -04006154
Stefan Behrens472262f2012-11-06 14:43:46 +01006155 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006156 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006157 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006158 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006159 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006160 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006161 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006162 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006163
Zhao Lei6e9606d2015-01-20 15:11:34 +08006164 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006165 if (!bbio) {
6166 ret = -ENOMEM;
6167 goto out;
6168 }
Nikolay Borisov608769a2020-07-02 16:46:41 +03006169
6170 for (i = 0; i < num_stripes; i++) {
6171 bbio->stripes[i].physical = map->stripes[stripe_index].physical +
6172 stripe_offset + stripe_nr * map->stripe_len;
6173 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
6174 stripe_index++;
6175 }
Li Zefande11cc12011-12-01 12:55:47 +08006176
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006177 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006178 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6179 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006180 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006181 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006182
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006183 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006184 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006185
6186 /* Fill in the logical address of each stripe */
David Sterbacff82672019-05-17 11:43:45 +02006187 tmp = stripe_nr * data_stripes;
6188 for (i = 0; i < data_stripes; i++)
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006189 bbio->raid_map[(i+rot) % num_stripes] =
6190 em->start + (tmp + i) * map->stripe_len;
6191
6192 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6193 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6194 bbio->raid_map[(i+rot+1) % num_stripes] =
6195 RAID6_Q_STRIPE;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006196
Nikolay Borisov608769a2020-07-02 16:46:41 +03006197 sort_parity_stripes(bbio, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006198 }
Li Zefande11cc12011-12-01 12:55:47 +08006199
Liu Bo2b19a1f2017-03-14 13:34:00 -07006200 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006201 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006202
Liu Bo73c0f222017-03-14 13:33:58 -07006203 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006204 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006205 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6206 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006207 }
6208
Li Zefande11cc12011-12-01 12:55:47 +08006209 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006210 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006211 bbio->num_stripes = num_stripes;
6212 bbio->max_errors = max_errors;
6213 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006214
6215 /*
6216 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6217 * mirror_num == num_stripes + 1 && dev_replace target drive is
6218 * available as a mirror
6219 */
6220 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6221 WARN_ON(num_stripes > 1);
6222 bbio->stripes[0].dev = dev_replace->tgtdev;
6223 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6224 bbio->mirror_num = map->num_stripes + 1;
6225 }
Chris Masoncea9e442008-04-09 16:28:12 -04006226out:
Liu Bo73beece2015-07-17 16:49:19 +08006227 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006228 lockdep_assert_held(&dev_replace->rwsem);
6229 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006230 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006231 }
Chris Mason0b86a832008-03-24 15:01:56 -04006232 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006233 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006234}
6235
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006236int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006237 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006238 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006239{
David Sterba75fb2e92018-08-03 00:36:29 +02006240 if (op == BTRFS_MAP_DISCARD)
6241 return __btrfs_map_block_for_discard(fs_info, logical,
6242 length, bbio_ret);
6243
Mike Christieb3d3fa52016-06-05 14:31:53 -05006244 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006245 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006246}
6247
Miao Xieaf8e2d12014-10-23 14:42:50 +08006248/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006249int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006250 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006251 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006252{
David Sterba825ad4c2017-03-28 14:45:22 +02006253 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006254}
6255
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006256static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006257{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006258 bio->bi_private = bbio->private;
6259 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006260 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006261
Zhao Lei6e9606d2015-01-20 15:11:34 +08006262 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006263}
6264
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006265static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006266{
Chris Mason9be33952013-05-17 18:30:14 -04006267 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006268 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006269
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006270 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006271 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006272 if (bio->bi_status == BLK_STS_IOERR ||
6273 bio->bi_status == BLK_STS_TARGET) {
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006274 struct btrfs_device *dev = btrfs_io_bio(bio)->device;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006275
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006276 ASSERT(dev->bdev);
6277 if (bio_op(bio) == REQ_OP_WRITE)
6278 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006279 BTRFS_DEV_STAT_WRITE_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006280 else if (!(bio->bi_opf & REQ_RAHEAD))
6281 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006282 BTRFS_DEV_STAT_READ_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006283 if (bio->bi_opf & REQ_PREFLUSH)
6284 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006285 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens442a4f62012-05-25 16:06:08 +02006286 }
6287 }
Chris Mason8790d502008-04-03 16:29:03 -04006288
Jan Schmidta1d3c472011-08-04 17:15:33 +02006289 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006290 is_orig_bio = 1;
6291
Miao Xiec404e0d2014-01-30 16:46:55 +08006292 btrfs_bio_counter_dec(bbio->fs_info);
6293
Jan Schmidta1d3c472011-08-04 17:15:33 +02006294 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006295 if (!is_orig_bio) {
6296 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006297 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006298 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006299
Chris Mason9be33952013-05-17 18:30:14 -04006300 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006301 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006302 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006303 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006304 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006305 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006306 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006307 /*
6308 * this bio is actually up to date, we didn't
6309 * go over the max number of errors
6310 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006311 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006312 }
Miao Xiec55f1392014-06-19 10:42:54 +08006313
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006314 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006315 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006316 bio_put(bio);
6317 }
Chris Mason8790d502008-04-03 16:29:03 -04006318}
6319
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006320static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006321 u64 physical, struct btrfs_device *dev)
Josef Bacikde1ee922012-10-19 16:50:56 -04006322{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006323 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006324
6325 bio->bi_private = bbio;
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006326 btrfs_io_bio(bio)->device = dev;
Josef Bacikde1ee922012-10-19 16:50:56 -04006327 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006328 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006329 btrfs_debug_in_rcu(fs_info,
6330 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6331 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
David Sterba1db45a32019-11-28 15:31:46 +01006332 (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name),
6333 dev->devid, bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006334 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006335
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006336 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006337
Chris Mason08635ba2019-07-10 12:28:14 -07006338 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006339}
6340
Josef Bacikde1ee922012-10-19 16:50:56 -04006341static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6342{
6343 atomic_inc(&bbio->error);
6344 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006345 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006346 WARN_ON(bio != bbio->orig_bio);
6347
Chris Mason9be33952013-05-17 18:30:14 -04006348 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006349 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006350 if (atomic_read(&bbio->error) > bbio->max_errors)
6351 bio->bi_status = BLK_STS_IOERR;
6352 else
6353 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006354 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006355 }
6356}
6357
Omar Sandoval58efbc92017-08-22 23:45:59 -07006358blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Chris Mason08635ba2019-07-10 12:28:14 -07006359 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04006360{
Chris Mason0b86a832008-03-24 15:01:56 -04006361 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006362 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006363 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006364 u64 length = 0;
6365 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006366 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006367 int dev_nr;
6368 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006369 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006370
Kent Overstreet4f024f32013-10-11 15:44:27 -07006371 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006372 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006373
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006374 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006375 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006376 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006377 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006378 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006379 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006380 }
Chris Masoncea9e442008-04-09 16:28:12 -04006381
Jan Schmidta1d3c472011-08-04 17:15:33 +02006382 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006383 bbio->orig_bio = first_bio;
6384 bbio->private = first_bio->bi_private;
6385 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006386 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006387 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6388
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006389 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006390 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006391 /* In this case, map_length has been set to the length of
6392 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006393 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006394 ret = raid56_parity_write(fs_info, bio, bbio,
6395 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006396 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006397 ret = raid56_parity_recover(fs_info, bio, bbio,
6398 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006399 }
Miao Xie42452152014-11-25 16:39:28 +08006400
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006401 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006402 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006403 }
6404
Chris Masoncea9e442008-04-09 16:28:12 -04006405 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006406 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006407 "mapping failed logical %llu bio len %llu len %llu",
6408 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006409 BUG();
6410 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006411
Zhao Lei08da7572015-02-12 15:42:16 +08006412 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006413 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006414 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6415 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006416 (bio_op(first_bio) == REQ_OP_WRITE &&
6417 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006418 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006419 continue;
6420 }
6421
David Sterba3aa8e072017-06-02 17:38:30 +02006422 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006423 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006424 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006425 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006426
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006427 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, dev);
Chris Mason239b14b2008-03-24 15:02:07 -04006428 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006429 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006430 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006431}
6432
Anand Jain09ba3bc2019-01-19 14:48:55 +08006433/*
6434 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6435 * return NULL.
6436 *
6437 * If devid and uuid are both specified, the match must be exact, otherwise
6438 * only devid is used.
6439 *
6440 * If @seed is true, traverse through the seed devices.
6441 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006442struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006443 u64 devid, u8 *uuid, u8 *fsid,
6444 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006445{
Yan Zheng2b820322008-11-17 21:11:30 -05006446 struct btrfs_device *device;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006447 struct btrfs_fs_devices *seed_devs;
Chris Mason0b86a832008-03-24 15:01:56 -04006448
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006449 if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6450 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6451 if (device->devid == devid &&
6452 (!uuid || memcmp(device->uuid, uuid,
6453 BTRFS_UUID_SIZE) == 0))
6454 return device;
6455 }
6456 }
6457
6458 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
Yan Zheng2b820322008-11-17 21:11:30 -05006459 if (!fsid ||
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006460 !memcmp(seed_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6461 list_for_each_entry(device, &seed_devs->devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006462 dev_list) {
6463 if (device->devid == devid &&
6464 (!uuid || memcmp(device->uuid, uuid,
6465 BTRFS_UUID_SIZE) == 0))
6466 return device;
6467 }
Yan Zheng2b820322008-11-17 21:11:30 -05006468 }
Yan Zheng2b820322008-11-17 21:11:30 -05006469 }
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006470
Yan Zheng2b820322008-11-17 21:11:30 -05006471 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006472}
6473
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006474static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006475 u64 devid, u8 *dev_uuid)
6476{
6477 struct btrfs_device *device;
Josef Bacikfccc0002020-08-31 10:52:42 -04006478 unsigned int nofs_flag;
Chris Masondfe25022008-05-13 13:46:40 -04006479
Josef Bacikfccc0002020-08-31 10:52:42 -04006480 /*
6481 * We call this under the chunk_mutex, so we want to use NOFS for this
6482 * allocation, however we don't want to change btrfs_alloc_device() to
6483 * always do NOFS because we use it in a lot of other GFP_KERNEL safe
6484 * places.
6485 */
6486 nofs_flag = memalloc_nofs_save();
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006487 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
Josef Bacikfccc0002020-08-31 10:52:42 -04006488 memalloc_nofs_restore(nofs_flag);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006489 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006490 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006491
6492 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006493 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006494 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006495
Anand Jaine6e674b2017-12-04 12:54:54 +08006496 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006497 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006498
Chris Masondfe25022008-05-13 13:46:40 -04006499 return device;
6500}
6501
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006502/**
6503 * btrfs_alloc_device - allocate struct btrfs_device
6504 * @fs_info: used only for generating a new devid, can be NULL if
6505 * devid is provided (i.e. @devid != NULL).
6506 * @devid: a pointer to devid for this device. If NULL a new devid
6507 * is generated.
6508 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6509 * is generated.
6510 *
6511 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006512 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006513 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006514 */
6515struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6516 const u64 *devid,
6517 const u8 *uuid)
6518{
6519 struct btrfs_device *dev;
6520 u64 tmp;
6521
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306522 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006523 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006524
Qu Wenruo154f7cb2020-08-20 15:42:46 +08006525 dev = __alloc_device(fs_info);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006526 if (IS_ERR(dev))
6527 return dev;
6528
6529 if (devid)
6530 tmp = *devid;
6531 else {
6532 int ret;
6533
6534 ret = find_next_devid(fs_info, &tmp);
6535 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006536 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006537 return ERR_PTR(ret);
6538 }
6539 }
6540 dev->devid = tmp;
6541
6542 if (uuid)
6543 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6544 else
6545 generate_random_uuid(dev->uuid);
6546
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006547 return dev;
6548}
6549
Anand Jain5a2b8e62017-10-09 11:07:45 +08006550static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006551 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006552{
Anand Jain2b902df2017-10-09 11:07:46 +08006553 if (error)
6554 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6555 devid, uuid);
6556 else
6557 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6558 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006559}
6560
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006561static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6562{
6563 int index = btrfs_bg_flags_to_raid_index(type);
6564 int ncopies = btrfs_raid_array[index].ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006565 const int nparity = btrfs_raid_array[index].nparity;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006566 int data_stripes;
6567
David Sterbae4f6c6b2019-05-14 01:59:54 +02006568 if (nparity)
6569 data_stripes = num_stripes - nparity;
6570 else
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006571 data_stripes = num_stripes / ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006572
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006573 return div_u64(chunk_len, data_stripes);
6574}
6575
David Sterba9690ac02019-03-20 16:43:07 +01006576static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006577 struct btrfs_chunk *chunk)
6578{
David Sterba9690ac02019-03-20 16:43:07 +01006579 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006580 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006581 struct map_lookup *map;
6582 struct extent_map *em;
6583 u64 logical;
6584 u64 length;
6585 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006586 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006587 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006588 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006589 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006590
Chris Masone17cade2008-04-15 15:41:47 -04006591 logical = key->offset;
6592 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006593 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006594
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006595 /*
6596 * Only need to verify chunk item if we're reading from sys chunk array,
6597 * as chunk item in tree block is already verified by tree-checker.
6598 */
6599 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006600 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006601 if (ret)
6602 return ret;
6603 }
Chris Masona061fc82008-05-07 11:43:44 -04006604
David Sterbac8bf1b62019-05-17 11:43:17 +02006605 read_lock(&map_tree->lock);
6606 em = lookup_extent_mapping(map_tree, logical, 1);
6607 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006608
6609 /* already mapped? */
6610 if (em && em->start <= logical && em->start + em->len > logical) {
6611 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006612 return 0;
6613 } else if (em) {
6614 free_extent_map(em);
6615 }
Chris Mason0b86a832008-03-24 15:01:56 -04006616
David Sterba172ddd62011-04-21 00:48:27 +02006617 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006618 if (!em)
6619 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006620 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006621 if (!map) {
6622 free_extent_map(em);
6623 return -ENOMEM;
6624 }
6625
Wang Shilong298a8f92014-06-19 10:42:52 +08006626 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006627 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006628 em->start = logical;
6629 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006630 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006631 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006632 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006633
Chris Mason593060d2008-03-25 16:50:33 -04006634 map->num_stripes = num_stripes;
6635 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6636 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006637 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6638 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006639 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006640 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006641 em->orig_block_len = calc_stripe_length(map->type, em->len,
6642 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006643 for (i = 0; i < num_stripes; i++) {
6644 map->stripes[i].physical =
6645 btrfs_stripe_offset_nr(leaf, chunk, i);
6646 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006647 read_extent_buffer(leaf, uuid, (unsigned long)
6648 btrfs_stripe_dev_uuid_nr(chunk, i),
6649 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006650 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006651 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006652 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006653 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006654 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006655 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006656 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006657 }
Chris Masondfe25022008-05-13 13:46:40 -04006658 if (!map->stripes[i].dev) {
6659 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006660 add_missing_dev(fs_info->fs_devices, devid,
6661 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006662 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006663 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006664 btrfs_err(fs_info,
6665 "failed to init missing dev %llu: %ld",
6666 devid, PTR_ERR(map->stripes[i].dev));
6667 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006668 }
Anand Jain2b902df2017-10-09 11:07:46 +08006669 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006670 }
Anand Jaine12c9622017-12-04 12:54:53 +08006671 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6672 &(map->stripes[i].dev->dev_state));
6673
Chris Mason0b86a832008-03-24 15:01:56 -04006674 }
6675
David Sterbac8bf1b62019-05-17 11:43:17 +02006676 write_lock(&map_tree->lock);
6677 ret = add_extent_mapping(map_tree, em, 0);
6678 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006679 if (ret < 0) {
6680 btrfs_err(fs_info,
6681 "failed to add chunk map, start=%llu len=%llu: %d",
6682 em->start, em->len, ret);
6683 }
Chris Mason0b86a832008-03-24 15:01:56 -04006684 free_extent_map(em);
6685
Qu Wenruo64f64f42018-08-01 10:37:20 +08006686 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006687}
6688
Jeff Mahoney143bede2012-03-01 14:56:26 +01006689static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006690 struct btrfs_dev_item *dev_item,
6691 struct btrfs_device *device)
6692{
6693 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006694
6695 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006696 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6697 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006698 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006699 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006700 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006701 device->type = btrfs_device_type(leaf, dev_item);
6702 device->io_align = btrfs_device_io_align(leaf, dev_item);
6703 device->io_width = btrfs_device_io_width(leaf, dev_item);
6704 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006705 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006706 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006707
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006708 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006709 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006710}
6711
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006712static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006713 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006714{
6715 struct btrfs_fs_devices *fs_devices;
6716 int ret;
6717
David Sterbaa32bf9a2018-03-16 02:21:22 +01006718 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006719 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006720
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03006721 /* This will match only for multi-device seed fs */
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006722 list_for_each_entry(fs_devices, &fs_info->fs_devices->seed_list, seed_list)
Anand Jain44880fd2017-07-29 17:50:09 +08006723 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006724 return fs_devices;
6725
Yan Zheng2b820322008-11-17 21:11:30 -05006726
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006727 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006728 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006729 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006730 return ERR_PTR(-ENOENT);
6731
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006732 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006733 if (IS_ERR(fs_devices))
6734 return fs_devices;
6735
Johannes Thumshirn0395d842019-11-13 11:27:27 +01006736 fs_devices->seeding = true;
Miao Xie5f375832014-09-03 21:35:46 +08006737 fs_devices->opened = 1;
6738 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006739 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006740
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03006741 /*
6742 * Upon first call for a seed fs fsid, just create a private copy of the
6743 * respective fs_devices and anchor it at fs_info->fs_devices->seed_list
6744 */
Yan Zhenge4404d62008-12-12 10:03:26 -05006745 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006746 if (IS_ERR(fs_devices))
6747 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006748
Anand Jain897fb572018-04-12 10:29:28 +08006749 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006750 if (ret) {
6751 free_fs_devices(fs_devices);
Anand Jainc83b60c2020-09-05 01:34:35 +08006752 return ERR_PTR(ret);
Julia Lawall48d28232012-04-14 11:24:33 +02006753 }
Yan Zheng2b820322008-11-17 21:11:30 -05006754
6755 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006756 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006757 free_fs_devices(fs_devices);
Anand Jainc83b60c2020-09-05 01:34:35 +08006758 return ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006759 }
6760
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006761 list_add(&fs_devices->seed_list, &fs_info->fs_devices->seed_list);
Anand Jainc83b60c2020-09-05 01:34:35 +08006762
Miao Xie5f375832014-09-03 21:35:46 +08006763 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006764}
6765
David Sterba17850752019-03-20 16:45:15 +01006766static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006767 struct btrfs_dev_item *dev_item)
6768{
David Sterba17850752019-03-20 16:45:15 +01006769 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006770 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006771 struct btrfs_device *device;
6772 u64 devid;
6773 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006774 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006775 u8 dev_uuid[BTRFS_UUID_SIZE];
6776
Chris Mason0b86a832008-03-24 15:01:56 -04006777 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006778 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006779 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006780 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006781 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006782
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006783 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006784 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006785 if (IS_ERR(fs_devices))
6786 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006787 }
6788
Anand Jaine4319cd2019-01-17 23:32:31 +08006789 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006790 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006791 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006792 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006793 btrfs_report_missing_device(fs_info, devid,
6794 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006795 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006796 }
Yan Zheng2b820322008-11-17 21:11:30 -05006797
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006798 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006799 if (IS_ERR(device)) {
6800 btrfs_err(fs_info,
6801 "failed to add missing dev %llu: %ld",
6802 devid, PTR_ERR(device));
6803 return PTR_ERR(device);
6804 }
Anand Jain2b902df2017-10-09 11:07:46 +08006805 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006806 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006807 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006808 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6809 btrfs_report_missing_device(fs_info,
6810 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006811 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006812 }
6813 btrfs_report_missing_device(fs_info, devid,
6814 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006815 }
Miao Xie5f375832014-09-03 21:35:46 +08006816
Anand Jaine6e674b2017-12-04 12:54:54 +08006817 if (!device->bdev &&
6818 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006819 /*
6820 * this happens when a device that was properly setup
6821 * in the device info lists suddenly goes bad.
6822 * device->bdev is NULL, and so we have to set
6823 * device->missing to one here
6824 */
Miao Xie5f375832014-09-03 21:35:46 +08006825 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006826 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006827 }
Miao Xie5f375832014-09-03 21:35:46 +08006828
6829 /* Move the device to its own fs_devices */
6830 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006831 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6832 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006833
6834 list_move(&device->dev_list, &fs_devices->devices);
6835 device->fs_devices->num_devices--;
6836 fs_devices->num_devices++;
6837
6838 device->fs_devices->missing_devices--;
6839 fs_devices->missing_devices++;
6840
6841 device->fs_devices = fs_devices;
6842 }
Yan Zheng2b820322008-11-17 21:11:30 -05006843 }
6844
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006845 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006846 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006847 if (device->generation !=
6848 btrfs_device_generation(leaf, dev_item))
6849 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006850 }
Chris Mason0b86a832008-03-24 15:01:56 -04006851
6852 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006853 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006854 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006855 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006856 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006857 atomic64_add(device->total_bytes - device->bytes_used,
6858 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006859 }
Chris Mason0b86a832008-03-24 15:01:56 -04006860 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006861 return ret;
6862}
6863
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006864int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006865{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006866 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006867 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006868 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006869 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006870 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006871 u8 *array_ptr;
6872 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006873 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006874 u32 num_stripes;
6875 u32 array_size;
6876 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006877 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006878 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006879 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006880
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006881 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006882 /*
6883 * This will create extent buffer of nodesize, superblock size is
6884 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6885 * overallocate but we can keep it as-is, only the first page is used.
6886 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006887 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006888 if (IS_ERR(sb))
6889 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006890 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006891 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006892 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006893 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006894 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006895 * pages up-to-date when the page is larger: extent does not cover the
6896 * whole page and consequently check_page_uptodate does not find all
6897 * the page's extents up-to-date (the hole beyond sb),
6898 * write_extent_buffer then triggers a WARN_ON.
6899 *
6900 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6901 * but sb spans only this function. Add an explicit SetPageUptodate call
6902 * to silence the warning eg. on PowerPC 64.
6903 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006904 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006905 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006906
Chris Masona061fc82008-05-07 11:43:44 -04006907 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006908 array_size = btrfs_super_sys_array_size(super_copy);
6909
David Sterba1ffb22c2014-10-31 19:02:42 +01006910 array_ptr = super_copy->sys_chunk_array;
6911 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6912 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006913
David Sterba1ffb22c2014-10-31 19:02:42 +01006914 while (cur_offset < array_size) {
6915 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006916 len = sizeof(*disk_key);
6917 if (cur_offset + len > array_size)
6918 goto out_short_read;
6919
Chris Mason0b86a832008-03-24 15:01:56 -04006920 btrfs_disk_key_to_cpu(&key, disk_key);
6921
David Sterba1ffb22c2014-10-31 19:02:42 +01006922 array_ptr += len;
6923 sb_array_offset += len;
6924 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006925
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006926 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006927 btrfs_err(fs_info,
6928 "unexpected item type %u in sys_array at offset %u",
6929 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006930 ret = -EIO;
6931 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006932 }
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006933
6934 chunk = (struct btrfs_chunk *)sb_array_offset;
6935 /*
6936 * At least one btrfs_chunk with one stripe must be present,
6937 * exact stripe count check comes afterwards
6938 */
6939 len = btrfs_chunk_item_size(1);
6940 if (cur_offset + len > array_size)
6941 goto out_short_read;
6942
6943 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6944 if (!num_stripes) {
6945 btrfs_err(fs_info,
6946 "invalid number of stripes %u in sys_array at offset %u",
6947 num_stripes, cur_offset);
6948 ret = -EIO;
6949 break;
6950 }
6951
6952 type = btrfs_chunk_type(sb, chunk);
6953 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
6954 btrfs_err(fs_info,
6955 "invalid chunk type %llu in sys_array at offset %u",
6956 type, cur_offset);
6957 ret = -EIO;
6958 break;
6959 }
6960
6961 len = btrfs_chunk_item_size(num_stripes);
6962 if (cur_offset + len > array_size)
6963 goto out_short_read;
6964
6965 ret = read_one_chunk(&key, sb, chunk);
6966 if (ret)
6967 break;
6968
David Sterba1ffb22c2014-10-31 19:02:42 +01006969 array_ptr += len;
6970 sb_array_offset += len;
6971 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006972 }
Liu Bod8651772016-06-03 17:41:42 -07006973 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006974 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006975 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006976
6977out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006978 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006979 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006980 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006981 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006982 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006983}
6984
Qu Wenruo21634a12017-03-09 09:34:36 +08006985/*
6986 * Check if all chunks in the fs are OK for read-write degraded mount
6987 *
Anand Jain6528b992017-12-18 17:08:59 +08006988 * If the @failing_dev is specified, it's accounted as missing.
6989 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006990 * Return true if all chunks meet the minimal RW mount requirements.
6991 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6992 */
Anand Jain6528b992017-12-18 17:08:59 +08006993bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6994 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006995{
David Sterbac8bf1b62019-05-17 11:43:17 +02006996 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08006997 struct extent_map *em;
6998 u64 next_start = 0;
6999 bool ret = true;
7000
David Sterbac8bf1b62019-05-17 11:43:17 +02007001 read_lock(&map_tree->lock);
7002 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7003 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007004 /* No chunk at all? Return false anyway */
7005 if (!em) {
7006 ret = false;
7007 goto out;
7008 }
7009 while (em) {
7010 struct map_lookup *map;
7011 int missing = 0;
7012 int max_tolerated;
7013 int i;
7014
7015 map = em->map_lookup;
7016 max_tolerated =
7017 btrfs_get_num_tolerated_disk_barrier_failures(
7018 map->type);
7019 for (i = 0; i < map->num_stripes; i++) {
7020 struct btrfs_device *dev = map->stripes[i].dev;
7021
Anand Jaine6e674b2017-12-04 12:54:54 +08007022 if (!dev || !dev->bdev ||
7023 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007024 dev->last_flush_error)
7025 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007026 else if (failing_dev && failing_dev == dev)
7027 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007028 }
7029 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007030 if (!failing_dev)
7031 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007032 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007033 em->start, missing, max_tolerated);
7034 free_extent_map(em);
7035 ret = false;
7036 goto out;
7037 }
7038 next_start = extent_map_end(em);
7039 free_extent_map(em);
7040
David Sterbac8bf1b62019-05-17 11:43:17 +02007041 read_lock(&map_tree->lock);
7042 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08007043 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02007044 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007045 }
7046out:
7047 return ret;
7048}
7049
David Sterbad85327b12020-07-08 22:55:14 +02007050static void readahead_tree_node_children(struct extent_buffer *node)
7051{
7052 int i;
7053 const int nr_items = btrfs_header_nritems(node);
7054
7055 for (i = 0; i < nr_items; i++) {
7056 u64 start;
7057
7058 start = btrfs_node_blockptr(node, i);
7059 readahead_tree_block(node->fs_info, start);
7060 }
7061}
7062
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007063int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007064{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007065 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007066 struct btrfs_path *path;
7067 struct extent_buffer *leaf;
7068 struct btrfs_key key;
7069 struct btrfs_key found_key;
7070 int ret;
7071 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007072 u64 total_dev = 0;
David Sterbad85327b12020-07-08 22:55:14 +02007073 u64 last_ra_node = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007074
Chris Mason0b86a832008-03-24 15:01:56 -04007075 path = btrfs_alloc_path();
7076 if (!path)
7077 return -ENOMEM;
7078
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007079 /*
7080 * uuid_mutex is needed only if we are mounting a sprout FS
7081 * otherwise we don't need it.
7082 */
Li Zefanb367e472011-12-07 11:38:24 +08007083 mutex_lock(&uuid_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007084
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007085 /*
Boris Burkov48cfa612020-07-16 13:29:46 -07007086 * It is possible for mount and umount to race in such a way that
7087 * we execute this code path, but open_fs_devices failed to clear
7088 * total_rw_bytes. We certainly want it cleared before reading the
7089 * device items, so clear it here.
7090 */
7091 fs_info->fs_devices->total_rw_bytes = 0;
7092
7093 /*
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007094 * Read all device items, and then all the chunk items. All
7095 * device items are found before any chunk item (their object id
7096 * is smaller than the lowest possible object id for a chunk
7097 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007098 */
7099 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7100 key.offset = 0;
7101 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007102 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007103 if (ret < 0)
7104 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007105 while (1) {
David Sterbad85327b12020-07-08 22:55:14 +02007106 struct extent_buffer *node;
7107
Chris Mason0b86a832008-03-24 15:01:56 -04007108 leaf = path->nodes[0];
7109 slot = path->slots[0];
7110 if (slot >= btrfs_header_nritems(leaf)) {
7111 ret = btrfs_next_leaf(root, path);
7112 if (ret == 0)
7113 continue;
7114 if (ret < 0)
7115 goto error;
7116 break;
7117 }
David Sterbad85327b12020-07-08 22:55:14 +02007118 /*
7119 * The nodes on level 1 are not locked but we don't need to do
7120 * that during mount time as nothing else can access the tree
7121 */
7122 node = path->nodes[1];
7123 if (node) {
7124 if (last_ra_node != node->start) {
7125 readahead_tree_node_children(node);
7126 last_ra_node = node->start;
7127 }
7128 }
Chris Mason0b86a832008-03-24 15:01:56 -04007129 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007130 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7131 struct btrfs_dev_item *dev_item;
7132 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007133 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01007134 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007135 if (ret)
7136 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007137 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007138 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7139 struct btrfs_chunk *chunk;
7140 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007141 mutex_lock(&fs_info->chunk_mutex);
David Sterba9690ac02019-03-20 16:43:07 +01007142 ret = read_one_chunk(&found_key, leaf, chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007143 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05007144 if (ret)
7145 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007146 }
7147 path->slots[0]++;
7148 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007149
7150 /*
7151 * After loading chunk tree, we've got all device information,
7152 * do another round of validation checks.
7153 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007154 if (total_dev != fs_info->fs_devices->total_devices) {
7155 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007156 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007157 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007158 total_dev);
7159 ret = -EINVAL;
7160 goto error;
7161 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007162 if (btrfs_super_total_bytes(fs_info->super_copy) <
7163 fs_info->fs_devices->total_rw_bytes) {
7164 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007165 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007166 btrfs_super_total_bytes(fs_info->super_copy),
7167 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007168 ret = -EINVAL;
7169 goto error;
7170 }
Chris Mason0b86a832008-03-24 15:01:56 -04007171 ret = 0;
7172error:
Li Zefanb367e472011-12-07 11:38:24 +08007173 mutex_unlock(&uuid_mutex);
7174
Yan Zheng2b820322008-11-17 21:11:30 -05007175 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007176 return ret;
7177}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007178
Miao Xiecb517ea2013-05-15 07:48:19 +00007179void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7180{
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007181 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
Miao Xiecb517ea2013-05-15 07:48:19 +00007182 struct btrfs_device *device;
7183
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007184 fs_devices->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007185
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007186 mutex_lock(&fs_devices->device_list_mutex);
7187 list_for_each_entry(device, &fs_devices->devices, dev_list)
7188 device->fs_info = fs_info;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007189
7190 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007191 list_for_each_entry(device, &seed_devs->devices, dev_list)
7192 device->fs_info = fs_info;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007193
7194 seed_devs->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007195 }
Anand Jaine17125b2020-09-05 01:34:31 +08007196 mutex_unlock(&fs_devices->device_list_mutex);
Miao Xiecb517ea2013-05-15 07:48:19 +00007197}
7198
David Sterba1dc990d2019-08-21 20:05:32 +02007199static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7200 const struct btrfs_dev_stats_item *ptr,
7201 int index)
7202{
7203 u64 val;
7204
7205 read_extent_buffer(eb, &val,
7206 offsetof(struct btrfs_dev_stats_item, values) +
7207 ((unsigned long)ptr) + (index * sizeof(u64)),
7208 sizeof(val));
7209 return val;
7210}
7211
7212static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7213 struct btrfs_dev_stats_item *ptr,
7214 int index, u64 val)
7215{
7216 write_extent_buffer(eb, &val,
7217 offsetof(struct btrfs_dev_stats_item, values) +
7218 ((unsigned long)ptr) + (index * sizeof(u64)),
7219 sizeof(val));
7220}
7221
Josef Bacik92e26df2020-09-18 16:44:33 -04007222static int btrfs_device_init_dev_stats(struct btrfs_device *device,
7223 struct btrfs_path *path)
Josef Bacik124604e2020-09-18 16:44:32 -04007224{
7225 struct btrfs_dev_stats_item *ptr;
7226 struct extent_buffer *eb;
7227 struct btrfs_key key;
7228 int item_size;
7229 int i, ret, slot;
7230
7231 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7232 key.type = BTRFS_PERSISTENT_ITEM_KEY;
7233 key.offset = device->devid;
7234 ret = btrfs_search_slot(NULL, device->fs_info->dev_root, &key, path, 0, 0);
7235 if (ret) {
7236 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7237 btrfs_dev_stat_set(device, i, 0);
7238 device->dev_stats_valid = 1;
7239 btrfs_release_path(path);
Josef Bacik92e26df2020-09-18 16:44:33 -04007240 return ret < 0 ? ret : 0;
Josef Bacik124604e2020-09-18 16:44:32 -04007241 }
7242 slot = path->slots[0];
7243 eb = path->nodes[0];
7244 item_size = btrfs_item_size_nr(eb, slot);
7245
7246 ptr = btrfs_item_ptr(eb, slot, struct btrfs_dev_stats_item);
7247
7248 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7249 if (item_size >= (1 + i) * sizeof(__le64))
7250 btrfs_dev_stat_set(device, i,
7251 btrfs_dev_stats_value(eb, ptr, i));
7252 else
7253 btrfs_dev_stat_set(device, i, 0);
7254 }
7255
7256 device->dev_stats_valid = 1;
7257 btrfs_dev_stat_print_on_load(device);
7258 btrfs_release_path(path);
Josef Bacik92e26df2020-09-18 16:44:33 -04007259
7260 return 0;
Josef Bacik124604e2020-09-18 16:44:32 -04007261}
7262
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007263int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7264{
Josef Bacik124604e2020-09-18 16:44:32 -04007265 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007266 struct btrfs_device *device;
7267 struct btrfs_path *path = NULL;
Josef Bacik92e26df2020-09-18 16:44:33 -04007268 int ret = 0;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007269
7270 path = btrfs_alloc_path();
Anand Jain3b80a9842019-08-21 17:26:32 +08007271 if (!path)
7272 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007273
7274 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik92e26df2020-09-18 16:44:33 -04007275 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7276 ret = btrfs_device_init_dev_stats(device, path);
7277 if (ret)
7278 goto out;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007279 }
Josef Bacik92e26df2020-09-18 16:44:33 -04007280 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
7281 list_for_each_entry(device, &seed_devs->devices, dev_list) {
7282 ret = btrfs_device_init_dev_stats(device, path);
7283 if (ret)
7284 goto out;
7285 }
7286 }
7287out:
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007288 mutex_unlock(&fs_devices->device_list_mutex);
7289
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007290 btrfs_free_path(path);
Josef Bacik92e26df2020-09-18 16:44:33 -04007291 return ret;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007292}
7293
7294static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007295 struct btrfs_device *device)
7296{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007297 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007298 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007299 struct btrfs_path *path;
7300 struct btrfs_key key;
7301 struct extent_buffer *eb;
7302 struct btrfs_dev_stats_item *ptr;
7303 int ret;
7304 int i;
7305
David Sterba242e2952016-01-25 17:51:31 +01007306 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7307 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007308 key.offset = device->devid;
7309
7310 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007311 if (!path)
7312 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007313 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7314 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007315 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007316 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007317 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007318 goto out;
7319 }
7320
7321 if (ret == 0 &&
7322 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7323 /* need to delete old one and insert a new one */
7324 ret = btrfs_del_item(trans, dev_root, path);
7325 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007326 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007327 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007328 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007329 goto out;
7330 }
7331 ret = 1;
7332 }
7333
7334 if (ret == 1) {
7335 /* need to insert a new item */
7336 btrfs_release_path(path);
7337 ret = btrfs_insert_empty_item(trans, dev_root, path,
7338 &key, sizeof(*ptr));
7339 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007340 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007341 "insert dev_stats item for device %s failed %d",
7342 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007343 goto out;
7344 }
7345 }
7346
7347 eb = path->nodes[0];
7348 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7349 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7350 btrfs_set_dev_stats_value(eb, ptr, i,
7351 btrfs_dev_stat_read(device, i));
7352 btrfs_mark_buffer_dirty(eb);
7353
7354out:
7355 btrfs_free_path(path);
7356 return ret;
7357}
7358
7359/*
7360 * called from commit_transaction. Writes all changed device stats to disk.
7361 */
David Sterba196c9d82019-03-20 16:50:38 +01007362int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007363{
David Sterba196c9d82019-03-20 16:50:38 +01007364 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007365 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7366 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007367 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007368 int ret = 0;
7369
7370 mutex_lock(&fs_devices->device_list_mutex);
7371 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007372 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7373 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007374 continue;
7375
Nikolay Borisov9deae962017-10-24 13:47:37 +03007376
7377 /*
7378 * There is a LOAD-LOAD control dependency between the value of
7379 * dev_stats_ccnt and updating the on-disk values which requires
7380 * reading the in-memory counters. Such control dependencies
7381 * require explicit read memory barriers.
7382 *
7383 * This memory barriers pairs with smp_mb__before_atomic in
7384 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7385 * barrier implied by atomic_xchg in
7386 * btrfs_dev_stats_read_and_reset
7387 */
7388 smp_rmb();
7389
Nikolay Borisov5495f192018-07-20 19:37:49 +03007390 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007391 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007392 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007393 }
7394 mutex_unlock(&fs_devices->device_list_mutex);
7395
7396 return ret;
7397}
7398
Stefan Behrens442a4f62012-05-25 16:06:08 +02007399void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7400{
7401 btrfs_dev_stat_inc(dev, index);
7402 btrfs_dev_stat_print_on_error(dev);
7403}
7404
Eric Sandeen48a3b632013-04-25 20:41:01 +00007405static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007406{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007407 if (!dev->dev_stats_valid)
7408 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007409 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007410 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007411 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007412 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7413 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7414 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007415 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7416 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007417}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007418
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007419static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7420{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007421 int i;
7422
7423 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7424 if (btrfs_dev_stat_read(dev, i) != 0)
7425 break;
7426 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7427 return; /* all values == 0, suppress message */
7428
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007429 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007430 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007431 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007432 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7433 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7434 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7435 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7436 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7437}
7438
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007439int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007440 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007441{
7442 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007443 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007444 int i;
7445
7446 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007447 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7448 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007449 mutex_unlock(&fs_devices->device_list_mutex);
7450
7451 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007452 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007453 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007454 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007455 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007456 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007457 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007458 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7459 if (stats->nr_items > i)
7460 stats->values[i] =
7461 btrfs_dev_stat_read_and_reset(dev, i);
7462 else
Anand Jain4e411a72019-08-07 16:21:19 +08007463 btrfs_dev_stat_set(dev, i, 0);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007464 }
Anand Jaina69976b2020-01-10 12:26:34 +08007465 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7466 current->comm, task_pid_nr(current));
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007467 } else {
7468 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7469 if (stats->nr_items > i)
7470 stats->values[i] = btrfs_dev_stat_read(dev, i);
7471 }
7472 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7473 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7474 return 0;
7475}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007476
Miao Xie935e5cc2014-09-03 21:35:33 +08007477/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007478 * Update the size and bytes used for each device where it changed. This is
7479 * delayed since we would otherwise get errors while writing out the
7480 * superblocks.
7481 *
7482 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007483 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007484void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007485{
Miao Xie935e5cc2014-09-03 21:35:33 +08007486 struct btrfs_device *curr, *next;
7487
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007488 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7489
7490 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007491 return;
7492
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007493 /*
7494 * We don't need the device_list_mutex here. This list is owned by the
7495 * transaction and the transaction must complete before the device is
7496 * released.
7497 */
7498 mutex_lock(&trans->fs_info->chunk_mutex);
7499 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7500 post_commit_list) {
7501 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007502 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007503 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007504 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007505 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007506}
Anand Jain5a13f432015-03-10 06:38:31 +08007507
David Sterba46df06b2018-07-13 20:46:30 +02007508/*
7509 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7510 */
7511int btrfs_bg_type_to_factor(u64 flags)
7512{
David Sterba44b28ad2019-05-17 11:43:31 +02007513 const int index = btrfs_bg_flags_to_raid_index(flags);
7514
7515 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007516}
Qu Wenruocf90d882018-08-01 10:37:19 +08007517
7518
Qu Wenruocf90d882018-08-01 10:37:19 +08007519
7520static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7521 u64 chunk_offset, u64 devid,
7522 u64 physical_offset, u64 physical_len)
7523{
David Sterbac8bf1b62019-05-17 11:43:17 +02007524 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007525 struct extent_map *em;
7526 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007527 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007528 u64 stripe_len;
7529 bool found = false;
7530 int ret = 0;
7531 int i;
7532
7533 read_lock(&em_tree->lock);
7534 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7535 read_unlock(&em_tree->lock);
7536
7537 if (!em) {
7538 btrfs_err(fs_info,
7539"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7540 physical_offset, devid);
7541 ret = -EUCLEAN;
7542 goto out;
7543 }
7544
7545 map = em->map_lookup;
7546 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7547 if (physical_len != stripe_len) {
7548 btrfs_err(fs_info,
7549"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7550 physical_offset, devid, em->start, physical_len,
7551 stripe_len);
7552 ret = -EUCLEAN;
7553 goto out;
7554 }
7555
7556 for (i = 0; i < map->num_stripes; i++) {
7557 if (map->stripes[i].dev->devid == devid &&
7558 map->stripes[i].physical == physical_offset) {
7559 found = true;
7560 if (map->verified_stripes >= map->num_stripes) {
7561 btrfs_err(fs_info,
7562 "too many dev extents for chunk %llu found",
7563 em->start);
7564 ret = -EUCLEAN;
7565 goto out;
7566 }
7567 map->verified_stripes++;
7568 break;
7569 }
7570 }
7571 if (!found) {
7572 btrfs_err(fs_info,
7573 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7574 physical_offset, devid);
7575 ret = -EUCLEAN;
7576 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007577
7578 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007579 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007580 if (!dev) {
7581 btrfs_err(fs_info, "failed to find devid %llu", devid);
7582 ret = -EUCLEAN;
7583 goto out;
7584 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007585
7586 /* It's possible this device is a dummy for seed device */
7587 if (dev->disk_total_bytes == 0) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007588 struct btrfs_fs_devices *devs;
7589
7590 devs = list_first_entry(&fs_info->fs_devices->seed_list,
7591 struct btrfs_fs_devices, seed_list);
7592 dev = btrfs_find_device(devs, devid, NULL, NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007593 if (!dev) {
7594 btrfs_err(fs_info, "failed to find seed devid %llu",
7595 devid);
7596 ret = -EUCLEAN;
7597 goto out;
7598 }
7599 }
7600
Qu Wenruo05a37c42018-10-05 17:45:55 +08007601 if (physical_offset + physical_len > dev->disk_total_bytes) {
7602 btrfs_err(fs_info,
7603"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7604 devid, physical_offset, physical_len,
7605 dev->disk_total_bytes);
7606 ret = -EUCLEAN;
7607 goto out;
7608 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007609out:
7610 free_extent_map(em);
7611 return ret;
7612}
7613
7614static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7615{
David Sterbac8bf1b62019-05-17 11:43:17 +02007616 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007617 struct extent_map *em;
7618 struct rb_node *node;
7619 int ret = 0;
7620
7621 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007622 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007623 em = rb_entry(node, struct extent_map, rb_node);
7624 if (em->map_lookup->num_stripes !=
7625 em->map_lookup->verified_stripes) {
7626 btrfs_err(fs_info,
7627 "chunk %llu has missing dev extent, have %d expect %d",
7628 em->start, em->map_lookup->verified_stripes,
7629 em->map_lookup->num_stripes);
7630 ret = -EUCLEAN;
7631 goto out;
7632 }
7633 }
7634out:
7635 read_unlock(&em_tree->lock);
7636 return ret;
7637}
7638
7639/*
7640 * Ensure that all dev extents are mapped to correct chunk, otherwise
7641 * later chunk allocation/free would cause unexpected behavior.
7642 *
7643 * NOTE: This will iterate through the whole device tree, which should be of
7644 * the same size level as the chunk tree. This slightly increases mount time.
7645 */
7646int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7647{
7648 struct btrfs_path *path;
7649 struct btrfs_root *root = fs_info->dev_root;
7650 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007651 u64 prev_devid = 0;
7652 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007653 int ret = 0;
7654
7655 key.objectid = 1;
7656 key.type = BTRFS_DEV_EXTENT_KEY;
7657 key.offset = 0;
7658
7659 path = btrfs_alloc_path();
7660 if (!path)
7661 return -ENOMEM;
7662
7663 path->reada = READA_FORWARD;
7664 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7665 if (ret < 0)
7666 goto out;
7667
7668 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7669 ret = btrfs_next_item(root, path);
7670 if (ret < 0)
7671 goto out;
7672 /* No dev extents at all? Not good */
7673 if (ret > 0) {
7674 ret = -EUCLEAN;
7675 goto out;
7676 }
7677 }
7678 while (1) {
7679 struct extent_buffer *leaf = path->nodes[0];
7680 struct btrfs_dev_extent *dext;
7681 int slot = path->slots[0];
7682 u64 chunk_offset;
7683 u64 physical_offset;
7684 u64 physical_len;
7685 u64 devid;
7686
7687 btrfs_item_key_to_cpu(leaf, &key, slot);
7688 if (key.type != BTRFS_DEV_EXTENT_KEY)
7689 break;
7690 devid = key.objectid;
7691 physical_offset = key.offset;
7692
7693 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7694 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7695 physical_len = btrfs_dev_extent_length(leaf, dext);
7696
Qu Wenruo5eb19382018-10-05 17:45:54 +08007697 /* Check if this dev extent overlaps with the previous one */
7698 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7699 btrfs_err(fs_info,
7700"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7701 devid, physical_offset, prev_dev_ext_end);
7702 ret = -EUCLEAN;
7703 goto out;
7704 }
7705
Qu Wenruocf90d882018-08-01 10:37:19 +08007706 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7707 physical_offset, physical_len);
7708 if (ret < 0)
7709 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007710 prev_devid = devid;
7711 prev_dev_ext_end = physical_offset + physical_len;
7712
Qu Wenruocf90d882018-08-01 10:37:19 +08007713 ret = btrfs_next_item(root, path);
7714 if (ret < 0)
7715 goto out;
7716 if (ret > 0) {
7717 ret = 0;
7718 break;
7719 }
7720 }
7721
7722 /* Ensure all chunks have corresponding dev extents */
7723 ret = verify_chunk_dev_extent_mapping(fs_info);
7724out:
7725 btrfs_free_path(path);
7726 return ret;
7727}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007728
7729/*
7730 * Check whether the given block group or device is pinned by any inode being
7731 * used as a swapfile.
7732 */
7733bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7734{
7735 struct btrfs_swapfile_pin *sp;
7736 struct rb_node *node;
7737
7738 spin_lock(&fs_info->swapfile_pins_lock);
7739 node = fs_info->swapfile_pins.rb_node;
7740 while (node) {
7741 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7742 if (ptr < sp->ptr)
7743 node = node->rb_left;
7744 else if (ptr > sp->ptr)
7745 node = node->rb_right;
7746 else
7747 break;
7748 }
7749 spin_unlock(&fs_info->swapfile_pins_lock);
7750 return node != NULL;
7751}