blob: 5449e3899e672f3e88125428b1751eab76ceb031 [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 *
294 * Exclusive operations, BTRFS_FS_EXCL_OP
295 * ======================================
296 *
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 *
321 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
322 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
323 * 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.
325 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
326 * 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);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100434 btrfs_device_data_ordered_init(dev);
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 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200599 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800600 }
601 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800602
Anand Jain7bcb8162018-05-29 17:23:20 +0800603 if (fs_devices->num_devices == 0) {
604 btrfs_sysfs_remove_fsid(fs_devices);
605 list_del(&fs_devices->fs_list);
606 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800607 }
608 }
Anand Jain70bc7082019-01-04 13:31:53 +0800609
610 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800611}
612
Josef Bacik18c850f2020-07-17 15:12:27 -0400613/*
614 * This is only used on mount, and we are protected from competing things
615 * messing with our fs_devices by the uuid_mutex, thus we do not need the
616 * fs_devices->device_list_mutex here.
617 */
Anand Jain0fb08bc2017-11-09 23:45:24 +0800618static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
619 struct btrfs_device *device, fmode_t flags,
620 void *holder)
621{
622 struct request_queue *q;
623 struct block_device *bdev;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800624 struct btrfs_super_block *disk_super;
625 u64 devid;
626 int ret;
627
628 if (device->bdev)
629 return -EINVAL;
630 if (!device->name)
631 return -EINVAL;
632
633 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900634 &bdev, &disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800635 if (ret)
636 return ret;
637
Anand Jain0fb08bc2017-11-09 23:45:24 +0800638 devid = btrfs_stack_device_id(&disk_super->dev_item);
639 if (devid != device->devid)
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900640 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800641
642 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900643 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800644
645 device->generation = btrfs_super_generation(disk_super);
646
647 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200648 if (btrfs_super_incompat_flags(disk_super) &
649 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
650 pr_err(
651 "BTRFS: Invalid seeding and uuid-changed device detected\n");
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900652 goto error_free_page;
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200653 }
654
Anand Jainebbede42017-12-04 12:54:52 +0800655 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Johannes Thumshirn0395d842019-11-13 11:27:27 +0100656 fs_devices->seeding = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800657 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800658 if (bdev_read_only(bdev))
659 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
660 else
661 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800662 }
663
664 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800665 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +0100666 fs_devices->rotating = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800667
668 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800669 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800670 device->mode = flags;
671
672 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800673 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
674 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800675 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800676 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800677 }
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900678 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800679
680 return 0;
681
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900682error_free_page:
683 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800684 blkdev_put(bdev, flags);
685
686 return -EINVAL;
687}
688
David Sterba60999ca2014-03-26 18:26:36 +0100689/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200690 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
Su Yuec0d81c72020-01-10 14:11:32 +0200691 * being created with a disk that has already completed its fsid change. Such
692 * disk can belong to an fs which has its FSID changed or to one which doesn't.
693 * Handle both cases here.
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200694 */
695static struct btrfs_fs_devices *find_fsid_inprogress(
696 struct btrfs_super_block *disk_super)
697{
698 struct btrfs_fs_devices *fs_devices;
699
700 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
701 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
702 BTRFS_FSID_SIZE) != 0 &&
703 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
704 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
705 return fs_devices;
706 }
707 }
708
Su Yuec0d81c72020-01-10 14:11:32 +0200709 return find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200710}
711
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200712
713static struct btrfs_fs_devices *find_fsid_changed(
714 struct btrfs_super_block *disk_super)
715{
716 struct btrfs_fs_devices *fs_devices;
717
718 /*
719 * Handles the case where scanned device is part of an fs that had
720 * multiple successful changes of FSID but curently device didn't
Nikolay Borisov05840712020-01-10 14:11:34 +0200721 * observe it. Meaning our fsid will be different than theirs. We need
722 * to handle two subcases :
723 * 1 - The fs still continues to have different METADATA/FSID uuids.
724 * 2 - The fs is switched back to its original FSID (METADATA/FSID
725 * are equal).
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200726 */
727 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov05840712020-01-10 14:11:34 +0200728 /* Changed UUIDs */
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200729 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
730 BTRFS_FSID_SIZE) != 0 &&
731 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
732 BTRFS_FSID_SIZE) == 0 &&
733 memcmp(fs_devices->fsid, disk_super->fsid,
Nikolay Borisov05840712020-01-10 14:11:34 +0200734 BTRFS_FSID_SIZE) != 0)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200735 return fs_devices;
Nikolay Borisov05840712020-01-10 14:11:34 +0200736
737 /* Unchanged UUIDs */
738 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
739 BTRFS_FSID_SIZE) == 0 &&
740 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
741 BTRFS_FSID_SIZE) == 0)
742 return fs_devices;
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200743 }
744
745 return NULL;
746}
Nikolay Borisov13620892020-01-10 14:11:35 +0200747
748static struct btrfs_fs_devices *find_fsid_reverted_metadata(
749 struct btrfs_super_block *disk_super)
750{
751 struct btrfs_fs_devices *fs_devices;
752
753 /*
754 * Handle the case where the scanned device is part of an fs whose last
755 * metadata UUID change reverted it to the original FSID. At the same
756 * time * fs_devices was first created by another constitutent device
757 * which didn't fully observe the operation. This results in an
758 * btrfs_fs_devices created with metadata/fsid different AND
759 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
760 * fs_devices equal to the FSID of the disk.
761 */
762 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
763 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
764 BTRFS_FSID_SIZE) != 0 &&
765 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
766 BTRFS_FSID_SIZE) == 0 &&
767 fs_devices->fsid_change)
768 return fs_devices;
769 }
770
771 return NULL;
772}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200773/*
David Sterba60999ca2014-03-26 18:26:36 +0100774 * Add new device to list of registered devices
775 *
776 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800777 * device pointer which was just added or updated when successful
778 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100779 */
Anand Jaine124ece2018-01-18 22:02:35 +0800780static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800781 struct btrfs_super_block *disk_super,
782 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400783{
784 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200785 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400786 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400787 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800788 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200789 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
790 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200791 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
792 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400793
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200794 if (fsid_change_in_progress) {
Su Yuec0d81c72020-01-10 14:11:32 +0200795 if (!has_metadata_uuid)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200796 fs_devices = find_fsid_inprogress(disk_super);
Su Yuec0d81c72020-01-10 14:11:32 +0200797 else
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200798 fs_devices = find_fsid_changed(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200799 } else if (has_metadata_uuid) {
Su Yuec6730a02020-01-10 14:11:33 +0200800 fs_devices = find_fsid_with_metadata_uuid(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200801 } else {
Nikolay Borisov13620892020-01-10 14:11:35 +0200802 fs_devices = find_fsid_reverted_metadata(disk_super);
803 if (!fs_devices)
804 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200805 }
806
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200807
Chris Mason8a4b83c2008-03-24 15:02:07 -0400808 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200809 if (has_metadata_uuid)
810 fs_devices = alloc_fs_devices(disk_super->fsid,
811 disk_super->metadata_uuid);
812 else
813 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
814
Ilya Dryomov2208a372013-08-12 14:33:03 +0300815 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800816 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300817
Al Viro92900e52019-01-27 04:58:00 +0000818 fs_devices->fsid_change = fsid_change_in_progress;
819
Anand Jain9c6d1732018-05-29 14:10:20 +0800820 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800821 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300822
Chris Mason8a4b83c2008-03-24 15:02:07 -0400823 device = NULL;
824 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800825 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800826 device = btrfs_find_device(fs_devices, devid,
827 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200828
829 /*
830 * If this disk has been pulled into an fs devices created by
831 * a device which had the CHANGING_FSID_V2 flag then replace the
832 * metadata_uuid/fsid values of the fs_devices.
833 */
Nikolay Borisov13620892020-01-10 14:11:35 +0200834 if (fs_devices->fsid_change &&
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200835 found_transid > fs_devices->latest_generation) {
836 memcpy(fs_devices->fsid, disk_super->fsid,
837 BTRFS_FSID_SIZE);
Nikolay Borisov13620892020-01-10 14:11:35 +0200838
839 if (has_metadata_uuid)
840 memcpy(fs_devices->metadata_uuid,
841 disk_super->metadata_uuid,
842 BTRFS_FSID_SIZE);
843 else
844 memcpy(fs_devices->metadata_uuid,
845 disk_super->fsid, BTRFS_FSID_SIZE);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200846
847 fs_devices->fsid_change = false;
848 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849 }
Miao Xie443f24f2014-07-24 11:37:15 +0800850
Chris Mason8a4b83c2008-03-24 15:02:07 -0400851 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800852 if (fs_devices->opened) {
853 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800854 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800855 }
Yan Zheng2b820322008-11-17 21:11:30 -0500856
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300857 device = btrfs_alloc_device(NULL, &devid,
858 disk_super->dev_item.uuid);
859 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800860 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400861 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800862 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400863 }
Josef Bacik606686e2012-06-04 14:03:51 -0400864
865 name = rcu_string_strdup(path, GFP_NOFS);
866 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100867 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800868 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800869 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400870 }
Josef Bacik606686e2012-06-04 14:03:51 -0400871 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200872
Xiao Guangrong1f781602011-04-20 10:09:16 +0000873 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100874 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400875
Yan Zheng2b820322008-11-17 21:11:30 -0500876 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800877 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800878
879 if (disk_super->label[0])
Anand Jainaa6c0df2019-10-02 18:30:48 +0800880 pr_info(
881 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
882 disk_super->label, devid, found_transid, path,
883 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800884 else
Anand Jainaa6c0df2019-10-02 18:30:48 +0800885 pr_info(
886 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
887 disk_super->fsid, devid, found_transid, path,
888 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800889
Josef Bacik606686e2012-06-04 14:03:51 -0400890 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800891 /*
892 * When FS is already mounted.
893 * 1. If you are here and if the device->name is NULL that
894 * means this device was missing at time of FS mount.
895 * 2. If you are here and if the device->name is different
896 * from 'path' that means either
897 * a. The same device disappeared and reappeared with
898 * different name. or
899 * b. The missing-disk-which-was-replaced, has
900 * reappeared now.
901 *
902 * We must allow 1 and 2a above. But 2b would be a spurious
903 * and unintentional.
904 *
905 * Further in case of 1 and 2a above, the disk at 'path'
906 * would have missed some transaction when it was away and
907 * in case of 2a the stale bdev has to be updated as well.
908 * 2b must not be allowed at all time.
909 */
910
911 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700912 * For now, we do allow update to btrfs_fs_device through the
913 * btrfs dev scan cli after FS has been mounted. We're still
914 * tracking a problem where systems fail mount by subvolume id
915 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800916 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700917 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800918 /*
919 * That is if the FS is _not_ mounted and if you
920 * are here, that means there is more than one
921 * disk with same uuid and devid.We keep the one
922 * with larger generation number or the last-in if
923 * generation are equal.
924 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800925 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800926 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800927 }
Anand Jainb96de002014-07-03 18:22:05 +0800928
Anand Jaina9261d42018-10-15 10:45:17 +0800929 /*
930 * We are going to replace the device path for a given devid,
931 * make sure it's the same device if the device is mounted
932 */
933 if (device->bdev) {
934 struct block_device *path_bdev;
935
936 path_bdev = lookup_bdev(path);
937 if (IS_ERR(path_bdev)) {
938 mutex_unlock(&fs_devices->device_list_mutex);
939 return ERR_CAST(path_bdev);
940 }
941
942 if (device->bdev != path_bdev) {
943 bdput(path_bdev);
944 mutex_unlock(&fs_devices->device_list_mutex);
945 btrfs_warn_in_rcu(device->fs_info,
946 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
947 disk_super->fsid, devid,
948 rcu_str_deref(device->name), path);
949 return ERR_PTR(-EEXIST);
950 }
951 bdput(path_bdev);
952 btrfs_info_in_rcu(device->fs_info,
953 "device fsid %pU devid %llu moved old:%s new:%s",
954 disk_super->fsid, devid,
955 rcu_str_deref(device->name), path);
956 }
957
Josef Bacik606686e2012-06-04 14:03:51 -0400958 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800959 if (!name) {
960 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800961 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800962 }
Josef Bacik606686e2012-06-04 14:03:51 -0400963 rcu_string_free(device->name);
964 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800965 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500966 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800967 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500968 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400969 }
970
Anand Jain77bdae42014-07-03 18:22:06 +0800971 /*
972 * Unmount does not free the btrfs_device struct but would zero
973 * generation along with most of the other members. So just update
974 * it back. We need it to pick the disk with largest generation
975 * (as above).
976 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200977 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +0800978 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200979 fs_devices->latest_generation = max_t(u64, found_transid,
980 fs_devices->latest_generation);
981 }
Anand Jain77bdae42014-07-03 18:22:06 +0800982
Anand Jainf2788d22018-01-18 22:02:34 +0800983 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
984
Anand Jain9c6d1732018-05-29 14:10:20 +0800985 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800986 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400987}
988
Yan Zhenge4404d62008-12-12 10:03:26 -0500989static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
990{
991 struct btrfs_fs_devices *fs_devices;
992 struct btrfs_device *device;
993 struct btrfs_device *orig_dev;
Anand Jaind2979aa2019-08-27 15:40:45 +0800994 int ret = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -0500995
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200996 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300997 if (IS_ERR(fs_devices))
998 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500999
Miao Xieadbbb862014-09-03 21:35:42 +08001000 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001001 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001002
1003 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001004 struct rcu_string *name;
1005
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001006 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1007 orig_dev->uuid);
Anand Jaind2979aa2019-08-27 15:40:45 +08001008 if (IS_ERR(device)) {
1009 ret = PTR_ERR(device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001010 goto error;
Anand Jaind2979aa2019-08-27 15:40:45 +08001011 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001012
Josef Bacik606686e2012-06-04 14:03:51 -04001013 /*
1014 * This is ok to do without rcu read locked because we hold the
1015 * uuid mutex so nothing we touch in here is going to disappear.
1016 */
Anand Jaine755f782014-06-30 17:12:47 +08001017 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001018 name = rcu_string_strdup(orig_dev->name->str,
1019 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001020 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001021 btrfs_free_device(device);
Anand Jaind2979aa2019-08-27 15:40:45 +08001022 ret = -ENOMEM;
Anand Jaine755f782014-06-30 17:12:47 +08001023 goto error;
1024 }
1025 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001026 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001027
Yan Zhenge4404d62008-12-12 10:03:26 -05001028 list_add(&device->dev_list, &fs_devices->devices);
1029 device->fs_devices = fs_devices;
1030 fs_devices->num_devices++;
1031 }
Miao Xieadbbb862014-09-03 21:35:42 +08001032 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001033 return fs_devices;
1034error:
Miao Xieadbbb862014-09-03 21:35:42 +08001035 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001036 free_fs_devices(fs_devices);
Anand Jaind2979aa2019-08-27 15:40:45 +08001037 return ERR_PTR(ret);
Yan Zhenge4404d62008-12-12 10:03:26 -05001038}
1039
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001040static void __btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices,
1041 int step, struct btrfs_device **latest_dev)
Chris Masondfe25022008-05-13 13:46:40 -04001042{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001043 struct btrfs_device *device, *next;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001044
Xiao Guangrong46224702011-04-20 10:08:47 +00001045 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001046 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001047 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001048 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001049 &device->dev_state) &&
Anand Jain998a0672020-05-05 02:58:25 +08001050 !test_bit(BTRFS_DEV_STATE_MISSING,
1051 &device->dev_state) &&
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001052 (!*latest_dev ||
1053 device->generation > (*latest_dev)->generation)) {
1054 *latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001055 }
Yan Zheng2b820322008-11-17 21:11:30 -05001056 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001057 }
Yan Zheng2b820322008-11-17 21:11:30 -05001058
Stefan Behrens8dabb742012-11-06 13:15:27 +01001059 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1060 /*
1061 * In the first step, keep the device which has
1062 * the correct fsid and the devid that is used
1063 * for the dev_replace procedure.
1064 * In the second step, the dev_replace state is
1065 * read from the device tree and it is known
1066 * whether the procedure is really active or
1067 * not, which means whether this device is
1068 * used or whether it should be removed.
1069 */
Anand Jain401e29c2017-12-04 12:54:55 +08001070 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1071 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001072 continue;
1073 }
1074 }
Yan Zheng2b820322008-11-17 21:11:30 -05001075 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001076 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001077 device->bdev = NULL;
1078 fs_devices->open_devices--;
1079 }
Anand Jainebbede42017-12-04 12:54:52 +08001080 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001081 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001082 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001083 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1084 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001085 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001086 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001087 list_del_init(&device->dev_list);
1088 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001089 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001090 }
Yan Zheng2b820322008-11-17 21:11:30 -05001091
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001092}
1093
1094/*
1095 * After we have read the system tree and know devids belonging to this
1096 * filesystem, remove the device which does not belong there.
1097 */
1098void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
1099{
1100 struct btrfs_device *latest_dev = NULL;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001101 struct btrfs_fs_devices *seed_dev;
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001102
1103 mutex_lock(&uuid_mutex);
Nikolay Borisov3712ccb2020-07-16 10:17:04 +03001104 __btrfs_free_extra_devids(fs_devices, step, &latest_dev);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001105
1106 list_for_each_entry(seed_dev, &fs_devices->seed_list, seed_list)
1107 __btrfs_free_extra_devids(seed_dev, step, &latest_dev);
Yan Zheng2b820322008-11-17 21:11:30 -05001108
Miao Xie443f24f2014-07-24 11:37:15 +08001109 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001110
Chris Masondfe25022008-05-13 13:46:40 -04001111 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001112}
Chris Masona0af4692008-05-13 16:03:06 -04001113
Anand Jain14238812016-07-22 06:04:53 +08001114static void btrfs_close_bdev(struct btrfs_device *device)
1115{
David Sterba08ffcae2017-06-19 16:55:35 +02001116 if (!device->bdev)
1117 return;
1118
Anand Jainebbede42017-12-04 12:54:52 +08001119 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001120 sync_blockdev(device->bdev);
1121 invalidate_bdev(device->bdev);
1122 }
1123
David Sterba08ffcae2017-06-19 16:55:35 +02001124 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001125}
1126
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001127static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001128{
1129 struct btrfs_fs_devices *fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001130
Anand Jainebbede42017-12-04 12:54:52 +08001131 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001132 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1133 list_del_init(&device->dev_alloc_list);
1134 fs_devices->rw_devices--;
1135 }
1136
Anand Jaine6e674b2017-12-04 12:54:54 +08001137 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001138 fs_devices->missing_devices--;
1139
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001140 btrfs_close_bdev(device);
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001141 if (device->bdev) {
Johannes Thumshirn3fff3972019-11-26 09:40:05 +01001142 fs_devices->open_devices--;
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001143 device->bdev = NULL;
Anand Jainf448341a2016-06-14 18:55:25 +08001144 }
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001145 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jainf448341a2016-06-14 18:55:25 +08001146
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001147 device->fs_info = NULL;
1148 atomic_set(&device->dev_stats_ccnt, 0);
1149 extent_io_tree_release(&device->alloc_state);
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001150
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001151 /* Verify the device is back in a pristine state */
1152 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1153 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1154 ASSERT(list_empty(&device->dev_alloc_list));
1155 ASSERT(list_empty(&device->post_commit_list));
1156 ASSERT(atomic_read(&device->reada_in_flight) == 0);
Anand Jainf448341a2016-06-14 18:55:25 +08001157}
1158
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001159static void close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001160{
Sasha Levin2037a092015-05-12 19:31:37 -04001161 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001162
Josef Bacik425c6ed2020-08-10 11:42:28 -04001163 lockdep_assert_held(&uuid_mutex);
1164
Yan Zheng2b820322008-11-17 21:11:30 -05001165 if (--fs_devices->opened > 0)
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001166 return;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001167
Josef Bacik425c6ed2020-08-10 11:42:28 -04001168 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list)
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001169 btrfs_close_one_device(device);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001170
Yan Zhenge4404d62008-12-12 10:03:26 -05001171 WARN_ON(fs_devices->open_devices);
1172 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001173 fs_devices->opened = 0;
Johannes Thumshirn0395d842019-11-13 11:27:27 +01001174 fs_devices->seeding = false;
Nikolay Borisovc4989c2f2020-07-15 13:48:49 +03001175 fs_devices->fs_info = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001176}
1177
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001178void btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001179{
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001180 LIST_HEAD(list);
1181 struct btrfs_fs_devices *tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05001182
1183 mutex_lock(&uuid_mutex);
Nikolay Borisov54eed6a2020-07-15 13:48:48 +03001184 close_fs_devices(fs_devices);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001185 if (!fs_devices->opened)
1186 list_splice_init(&fs_devices->seed_list, &list);
Yan Zhenge4404d62008-12-12 10:03:26 -05001187
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001188 list_for_each_entry_safe(fs_devices, tmp, &list, seed_list) {
Anand Jain0226e0e2018-04-12 10:29:27 +08001189 close_fs_devices(fs_devices);
Nikolay Borisov944d3f92020-07-16 10:25:33 +03001190 list_del(&fs_devices->seed_list);
Yan Zhenge4404d62008-12-12 10:03:26 -05001191 free_fs_devices(fs_devices);
1192 }
Josef Bacik425c6ed2020-08-10 11:42:28 -04001193 mutex_unlock(&uuid_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001194}
1195
Anand Jain897fb572018-04-12 10:29:28 +08001196static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001197 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001198{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001199 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001200 struct btrfs_device *latest_dev = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001201
Tejun Heod4d77622010-11-13 11:55:18 +01001202 flags |= FMODE_EXCL;
1203
Anand Jainf117e292018-04-12 10:29:26 +08001204 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001205 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001206 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001207 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001208
Anand Jain9f050db2017-11-09 23:45:25 +08001209 if (!latest_dev ||
1210 device->generation > latest_dev->generation)
1211 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001212 }
Anand Jain1ed802c2020-04-28 23:22:25 +08001213 if (fs_devices->open_devices == 0)
1214 return -EINVAL;
1215
Yan Zheng2b820322008-11-17 21:11:30 -05001216 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001217 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001218 fs_devices->total_rw_bytes = 0;
Naohiro Aotac4a816c2020-02-25 12:56:08 +09001219 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
Anand Jain1ed802c2020-04-28 23:22:25 +08001220
1221 return 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001222}
1223
Anand Jainf8e10cd2018-01-22 14:49:36 -08001224static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1225{
1226 struct btrfs_device *dev1, *dev2;
1227
1228 dev1 = list_entry(a, struct btrfs_device, dev_list);
1229 dev2 = list_entry(b, struct btrfs_device, dev_list);
1230
1231 if (dev1->devid < dev2->devid)
1232 return -1;
1233 else if (dev1->devid > dev2->devid)
1234 return 1;
1235 return 0;
1236}
1237
Yan Zheng2b820322008-11-17 21:11:30 -05001238int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001239 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001240{
1241 int ret;
1242
David Sterbaf5194e32018-06-19 17:09:47 +02001243 lockdep_assert_held(&uuid_mutex);
Josef Bacik18c850f2020-07-17 15:12:27 -04001244 /*
1245 * The device_list_mutex cannot be taken here in case opening the
1246 * underlying device takes further locks like bd_mutex.
1247 *
1248 * We also don't need the lock here as this is called during mount and
1249 * exclusion is provided by uuid_mutex
1250 */
David Sterbaf5194e32018-06-19 17:09:47 +02001251
Yan Zheng2b820322008-11-17 21:11:30 -05001252 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001253 fs_devices->opened++;
1254 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001255 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001256 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001257 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001258 }
Anand Jain542c5902018-04-12 10:29:34 +08001259
Chris Mason8a4b83c2008-03-24 15:02:07 -04001260 return ret;
1261}
1262
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001263void btrfs_release_disk_super(struct btrfs_super_block *super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001264{
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001265 struct page *page = virt_to_page(super);
1266
Anand Jain6cf86a002016-02-13 10:01:29 +08001267 put_page(page);
1268}
1269
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001270static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
1271 u64 bytenr)
Anand Jain6cf86a002016-02-13 10:01:29 +08001272{
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001273 struct btrfs_super_block *disk_super;
1274 struct page *page;
Anand Jain6cf86a002016-02-13 10:01:29 +08001275 void *p;
1276 pgoff_t index;
1277
1278 /* make sure our super fits in the device */
1279 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001280 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001281
1282 /* make sure our super fits in the page */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001283 if (sizeof(*disk_super) > PAGE_SIZE)
1284 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001285
1286 /* make sure our super doesn't straddle pages on disk */
1287 index = bytenr >> PAGE_SHIFT;
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001288 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
1289 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001290
1291 /* pull in the page with our super */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001292 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001293
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001294 if (IS_ERR(page))
1295 return ERR_CAST(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001296
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001297 p = page_address(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001298
1299 /* align our pointer to the offset of the super block */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001300 disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001301
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001302 if (btrfs_super_bytenr(disk_super) != bytenr ||
1303 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001304 btrfs_release_disk_super(p);
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001305 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001306 }
1307
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001308 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1])
1309 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0;
Anand Jain6cf86a002016-02-13 10:01:29 +08001310
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001311 return disk_super;
Anand Jain6cf86a002016-02-13 10:01:29 +08001312}
1313
Anand Jain228a73a2019-01-04 13:31:54 +08001314int btrfs_forget_devices(const char *path)
1315{
1316 int ret;
1317
1318 mutex_lock(&uuid_mutex);
1319 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1320 mutex_unlock(&uuid_mutex);
1321
1322 return ret;
1323}
1324
David Sterba6f60cbd2013-02-15 11:31:02 -07001325/*
1326 * Look for a btrfs signature on a device. This may be called out of the mount path
1327 * and we are not allowed to call set_blocksize during the scan. The superblock
1328 * is read via pagecache
1329 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001330struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1331 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001332{
1333 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001334 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001335 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001336 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001337 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001338
David Sterba899f9302018-06-19 16:37:36 +02001339 lockdep_assert_held(&uuid_mutex);
1340
David Sterba6f60cbd2013-02-15 11:31:02 -07001341 /*
1342 * we would like to check all the supers, but that would make
1343 * a btrfs mount succeed after a mkfs from a different FS.
1344 * So, we need to add a special mount option to scan for
1345 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1346 */
1347 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001348 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001349
1350 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001351 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001352 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001353
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001354 disk_super = btrfs_read_disk_super(bdev, bytenr);
1355 if (IS_ERR(disk_super)) {
1356 device = ERR_CAST(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001357 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001358 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001359
Anand Jain4306a972018-05-29 12:28:37 +08001360 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001361 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001362 if (new_device_added)
1363 btrfs_free_stale_devices(path, device);
1364 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001365
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001366 btrfs_release_disk_super(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001367
1368error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001369 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001370
Gu Jinxiang36350e92018-07-12 14:23:16 +08001371 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001372}
Chris Mason0b86a832008-03-24 15:01:56 -04001373
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001374/*
1375 * Try to find a chunk that intersects [start, start + len] range and when one
1376 * such is found, record the end of it in *start
1377 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001378static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1379 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001380{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001381 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001382
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001383 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001384
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001385 if (!find_first_extent_bit(&device->alloc_state, *start,
1386 &physical_start, &physical_end,
1387 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001388
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001389 if (in_range(physical_start, *start, len) ||
1390 in_range(*start, physical_start,
1391 physical_end - physical_start)) {
1392 *start = physical_end + 1;
1393 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001394 }
1395 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001396 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001397}
1398
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001399static u64 dev_extent_search_start(struct btrfs_device *device, u64 start)
1400{
1401 switch (device->fs_devices->chunk_alloc_policy) {
1402 case BTRFS_CHUNK_ALLOC_REGULAR:
1403 /*
1404 * We don't want to overwrite the superblock on the drive nor
1405 * any area used by the boot loader (grub for example), so we
1406 * make sure to start at an offset of at least 1MB.
1407 */
1408 return max_t(u64, start, SZ_1M);
1409 default:
1410 BUG();
1411 }
1412}
1413
1414/**
1415 * dev_extent_hole_check - check if specified hole is suitable for allocation
1416 * @device: the device which we have the hole
1417 * @hole_start: starting position of the hole
1418 * @hole_size: the size of the hole
1419 * @num_bytes: the size of the free space that we need
1420 *
1421 * This function may modify @hole_start and @hole_end to reflect the suitable
1422 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1423 */
1424static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
1425 u64 *hole_size, u64 num_bytes)
1426{
1427 bool changed = false;
1428 u64 hole_end = *hole_start + *hole_size;
1429
1430 /*
1431 * Check before we set max_hole_start, otherwise we could end up
1432 * sending back this offset anyway.
1433 */
1434 if (contains_pending_extent(device, hole_start, *hole_size)) {
1435 if (hole_end >= *hole_start)
1436 *hole_size = hole_end - *hole_start;
1437 else
1438 *hole_size = 0;
1439 changed = true;
1440 }
1441
1442 switch (device->fs_devices->chunk_alloc_policy) {
1443 case BTRFS_CHUNK_ALLOC_REGULAR:
1444 /* No extra check */
1445 break;
1446 default:
1447 BUG();
1448 }
1449
1450 return changed;
1451}
Josef Bacik6df9a952013-06-27 13:22:46 -04001452
Chris Mason0b86a832008-03-24 15:01:56 -04001453/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001454 * find_free_dev_extent_start - find free space in the specified device
1455 * @device: the device which we search the free space in
1456 * @num_bytes: the size of the free space that we need
1457 * @search_start: the position from which to begin the search
1458 * @start: store the start of the free space.
1459 * @len: the size of the free space. that we find, or the size
1460 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001461 *
Chris Mason0b86a832008-03-24 15:01:56 -04001462 * this uses a pretty simple search, the expectation is that it is
1463 * called very infrequently and that a given device has a small number
1464 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001465 *
1466 * @start is used to store the start of the free space if we find. But if we
1467 * don't find suitable free space, it will be used to store the start position
1468 * of the max free space.
1469 *
1470 * @len is used to store the size of the free space that we find.
1471 * But if we don't find suitable free space, it is used to store the size of
1472 * the max free space.
Qu Wenruo135da972019-07-19 14:51:42 +08001473 *
1474 * NOTE: This function will search *commit* root of device tree, and does extra
1475 * check to ensure dev extents are not double allocated.
1476 * This makes the function safe to allocate dev extents but may not report
1477 * correct usable device space, as device extent freed in current transaction
1478 * is not reported as avaiable.
Chris Mason0b86a832008-03-24 15:01:56 -04001479 */
Qu Wenruo9e3246a2019-07-19 14:51:41 +08001480static int find_free_dev_extent_start(struct btrfs_device *device,
1481 u64 num_bytes, u64 search_start, u64 *start,
1482 u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001484 struct btrfs_fs_info *fs_info = device->fs_info;
1485 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001486 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001487 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001488 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001489 u64 hole_size;
1490 u64 max_hole_start;
1491 u64 max_hole_size;
1492 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001493 u64 search_end = device->total_bytes;
1494 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001495 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001496 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001497
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001498 search_start = dev_extent_search_start(device, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001499
Josef Bacik6df9a952013-06-27 13:22:46 -04001500 path = btrfs_alloc_path();
1501 if (!path)
1502 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001503
Miao Xie7bfc8372011-01-05 10:07:26 +00001504 max_hole_start = search_start;
1505 max_hole_size = 0;
1506
Zhao Leif2ab7612015-02-16 18:52:17 +08001507again:
Anand Jain401e29c2017-12-04 12:54:55 +08001508 if (search_start >= search_end ||
1509 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001510 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001511 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001512 }
1513
David Sterbae4058b52015-11-27 16:31:35 +01001514 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001515 path->search_commit_root = 1;
1516 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001517
Chris Mason0b86a832008-03-24 15:01:56 -04001518 key.objectid = device->devid;
1519 key.offset = search_start;
1520 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001521
Li Zefan125ccb02011-12-08 15:07:24 +08001522 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001523 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001524 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001525 if (ret > 0) {
1526 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1527 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001528 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001529 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001530
Chris Mason0b86a832008-03-24 15:01:56 -04001531 while (1) {
1532 l = path->nodes[0];
1533 slot = path->slots[0];
1534 if (slot >= btrfs_header_nritems(l)) {
1535 ret = btrfs_next_leaf(root, path);
1536 if (ret == 0)
1537 continue;
1538 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001539 goto out;
1540
1541 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001542 }
1543 btrfs_item_key_to_cpu(l, &key, slot);
1544
1545 if (key.objectid < device->devid)
1546 goto next;
1547
1548 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001549 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001550
David Sterba962a2982014-06-04 18:41:45 +02001551 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001552 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001553
Miao Xie7bfc8372011-01-05 10:07:26 +00001554 if (key.offset > search_start) {
1555 hole_size = key.offset - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001556 dev_extent_hole_check(device, &search_start, &hole_size,
1557 num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04001558
Miao Xie7bfc8372011-01-05 10:07:26 +00001559 if (hole_size > max_hole_size) {
1560 max_hole_start = search_start;
1561 max_hole_size = hole_size;
1562 }
1563
1564 /*
1565 * If this free space is greater than which we need,
1566 * it must be the max free space that we have found
1567 * until now, so max_hole_start must point to the start
1568 * of this free space and the length of this free space
1569 * is stored in max_hole_size. Thus, we return
1570 * max_hole_start and max_hole_size and go back to the
1571 * caller.
1572 */
1573 if (hole_size >= num_bytes) {
1574 ret = 0;
1575 goto out;
1576 }
1577 }
1578
Chris Mason0b86a832008-03-24 15:01:56 -04001579 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001580 extent_end = key.offset + btrfs_dev_extent_length(l,
1581 dev_extent);
1582 if (extent_end > search_start)
1583 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001584next:
1585 path->slots[0]++;
1586 cond_resched();
1587 }
Chris Mason0b86a832008-03-24 15:01:56 -04001588
liubo38c01b92011-08-02 02:39:03 +00001589 /*
1590 * At this point, search_start should be the end of
1591 * allocated dev extents, and when shrinking the device,
1592 * search_end may be smaller than search_start.
1593 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001594 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001595 hole_size = search_end - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001596 if (dev_extent_hole_check(device, &search_start, &hole_size,
1597 num_bytes)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001598 btrfs_release_path(path);
1599 goto again;
1600 }
Chris Mason0b86a832008-03-24 15:01:56 -04001601
Zhao Leif2ab7612015-02-16 18:52:17 +08001602 if (hole_size > max_hole_size) {
1603 max_hole_start = search_start;
1604 max_hole_size = hole_size;
1605 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001606 }
1607
Miao Xie7bfc8372011-01-05 10:07:26 +00001608 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001609 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001610 ret = -ENOSPC;
1611 else
1612 ret = 0;
1613
1614out:
Yan Zheng2b820322008-11-17 21:11:30 -05001615 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001616 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001617 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001618 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001619 return ret;
1620}
1621
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001622int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001623 u64 *start, u64 *len)
1624{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001625 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001626 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001627}
1628
Christoph Hellwigb2950862008-12-02 09:54:17 -05001629static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001630 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001631 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001632{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001633 struct btrfs_fs_info *fs_info = device->fs_info;
1634 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001635 int ret;
1636 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001637 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001638 struct btrfs_key found_key;
1639 struct extent_buffer *leaf = NULL;
1640 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001641
1642 path = btrfs_alloc_path();
1643 if (!path)
1644 return -ENOMEM;
1645
1646 key.objectid = device->devid;
1647 key.offset = start;
1648 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001649again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001650 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001651 if (ret > 0) {
1652 ret = btrfs_previous_item(root, path, key.objectid,
1653 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001654 if (ret)
1655 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001656 leaf = path->nodes[0];
1657 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1658 extent = btrfs_item_ptr(leaf, path->slots[0],
1659 struct btrfs_dev_extent);
1660 BUG_ON(found_key.offset > start || found_key.offset +
1661 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001662 key = found_key;
1663 btrfs_release_path(path);
1664 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001665 } else if (ret == 0) {
1666 leaf = path->nodes[0];
1667 extent = btrfs_item_ptr(leaf, path->slots[0],
1668 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001669 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001670 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001671 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001672 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001673
Miao Xie2196d6e2014-09-03 21:35:41 +08001674 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1675
Chris Mason8f18cf12008-04-25 16:53:30 -04001676 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001677 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001678 btrfs_handle_fs_error(fs_info, ret,
1679 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001680 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001681 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001682 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001683out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001684 btrfs_free_path(path);
1685 return ret;
1686}
1687
Eric Sandeen48a3b632013-04-25 20:41:01 +00001688static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1689 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001690 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001691{
1692 int ret;
1693 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001694 struct btrfs_fs_info *fs_info = device->fs_info;
1695 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001696 struct btrfs_dev_extent *extent;
1697 struct extent_buffer *leaf;
1698 struct btrfs_key key;
1699
Anand Jaine12c9622017-12-04 12:54:53 +08001700 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001701 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001702 path = btrfs_alloc_path();
1703 if (!path)
1704 return -ENOMEM;
1705
Chris Mason0b86a832008-03-24 15:01:56 -04001706 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001707 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001708 key.type = BTRFS_DEV_EXTENT_KEY;
1709 ret = btrfs_insert_empty_item(trans, root, path, &key,
1710 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001711 if (ret)
1712 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001713
1714 leaf = path->nodes[0];
1715 extent = btrfs_item_ptr(leaf, path->slots[0],
1716 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001717 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1718 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001719 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1720 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001721 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1722
Chris Mason0b86a832008-03-24 15:01:56 -04001723 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1724 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001725out:
Chris Mason0b86a832008-03-24 15:01:56 -04001726 btrfs_free_path(path);
1727 return ret;
1728}
1729
Josef Bacik6df9a952013-06-27 13:22:46 -04001730static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001731{
Josef Bacik6df9a952013-06-27 13:22:46 -04001732 struct extent_map_tree *em_tree;
1733 struct extent_map *em;
1734 struct rb_node *n;
1735 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001736
David Sterbac8bf1b62019-05-17 11:43:17 +02001737 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001738 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001739 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001740 if (n) {
1741 em = rb_entry(n, struct extent_map, rb_node);
1742 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001743 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001744 read_unlock(&em_tree->lock);
1745
Chris Mason0b86a832008-03-24 15:01:56 -04001746 return ret;
1747}
1748
Ilya Dryomov53f10652013-08-12 14:33:01 +03001749static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1750 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001751{
1752 int ret;
1753 struct btrfs_key key;
1754 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001755 struct btrfs_path *path;
1756
Yan Zheng2b820322008-11-17 21:11:30 -05001757 path = btrfs_alloc_path();
1758 if (!path)
1759 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001760
1761 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1762 key.type = BTRFS_DEV_ITEM_KEY;
1763 key.offset = (u64)-1;
1764
Ilya Dryomov53f10652013-08-12 14:33:01 +03001765 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001766 if (ret < 0)
1767 goto error;
1768
Anand Jaina06dee4d2019-08-27 15:40:44 +08001769 if (ret == 0) {
1770 /* Corruption */
1771 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1772 ret = -EUCLEAN;
1773 goto error;
1774 }
Chris Mason0b86a832008-03-24 15:01:56 -04001775
Ilya Dryomov53f10652013-08-12 14:33:01 +03001776 ret = btrfs_previous_item(fs_info->chunk_root, path,
1777 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001778 BTRFS_DEV_ITEM_KEY);
1779 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001780 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001781 } else {
1782 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1783 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001784 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001785 }
1786 ret = 0;
1787error:
Yan Zheng2b820322008-11-17 21:11:30 -05001788 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001789 return ret;
1790}
1791
1792/*
1793 * the device information is stored in the chunk root
1794 * the btrfs_device struct should be fully filled in
1795 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001796static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001797 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001798{
1799 int ret;
1800 struct btrfs_path *path;
1801 struct btrfs_dev_item *dev_item;
1802 struct extent_buffer *leaf;
1803 struct btrfs_key key;
1804 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001805
Chris Mason0b86a832008-03-24 15:01:56 -04001806 path = btrfs_alloc_path();
1807 if (!path)
1808 return -ENOMEM;
1809
Chris Mason0b86a832008-03-24 15:01:56 -04001810 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1811 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001812 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001813
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001814 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1815 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001816 if (ret)
1817 goto out;
1818
1819 leaf = path->nodes[0];
1820 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1821
1822 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001823 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001824 btrfs_set_device_type(leaf, dev_item, device->type);
1825 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1826 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1827 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001828 btrfs_set_device_total_bytes(leaf, dev_item,
1829 btrfs_device_get_disk_total_bytes(device));
1830 btrfs_set_device_bytes_used(leaf, dev_item,
1831 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001832 btrfs_set_device_group(leaf, dev_item, 0);
1833 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1834 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001835 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001836
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001837 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001838 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001839 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001840 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1841 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001842 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001843
Yan Zheng2b820322008-11-17 21:11:30 -05001844 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001845out:
1846 btrfs_free_path(path);
1847 return ret;
1848}
Chris Mason8f18cf12008-04-25 16:53:30 -04001849
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001850/*
1851 * Function to update ctime/mtime for a given device path.
1852 * Mainly used for ctime/mtime based probe like libblkid.
1853 */
David Sterbada353f62017-02-14 17:55:53 +01001854static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001855{
1856 struct file *filp;
1857
1858 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001859 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001860 return;
1861 file_update_time(filp);
1862 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001863}
1864
David Sterbaf331a952019-03-20 16:31:53 +01001865static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001866{
David Sterbaf331a952019-03-20 16:31:53 +01001867 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001868 int ret;
1869 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001870 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001871 struct btrfs_trans_handle *trans;
1872
Chris Masona061fc82008-05-07 11:43:44 -04001873 path = btrfs_alloc_path();
1874 if (!path)
1875 return -ENOMEM;
1876
Yan, Zhenga22285a2010-05-16 10:48:46 -04001877 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001878 if (IS_ERR(trans)) {
1879 btrfs_free_path(path);
1880 return PTR_ERR(trans);
1881 }
Chris Masona061fc82008-05-07 11:43:44 -04001882 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1883 key.type = BTRFS_DEV_ITEM_KEY;
1884 key.offset = device->devid;
1885
1886 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001887 if (ret) {
1888 if (ret > 0)
1889 ret = -ENOENT;
1890 btrfs_abort_transaction(trans, ret);
1891 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001892 goto out;
1893 }
1894
1895 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001896 if (ret) {
1897 btrfs_abort_transaction(trans, ret);
1898 btrfs_end_transaction(trans);
1899 }
1900
Chris Masona061fc82008-05-07 11:43:44 -04001901out:
1902 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001903 if (!ret)
1904 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001905 return ret;
1906}
1907
David Sterba3cc31a02016-02-15 16:00:26 +01001908/*
1909 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1910 * filesystem. It's up to the caller to adjust that number regarding eg. device
1911 * replace.
1912 */
1913static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1914 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001915{
Chris Masona061fc82008-05-07 11:43:44 -04001916 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001917 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001918 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001919
Miao Xiede98ced2013-01-29 10:13:12 +00001920 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001921 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001922
Anand Jainbd45ffb2016-02-13 10:01:34 +08001923 all_avail = fs_info->avail_data_alloc_bits |
1924 fs_info->avail_system_alloc_bits |
1925 fs_info->avail_metadata_alloc_bits;
1926 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001927
David Sterba418775a2016-02-15 16:28:14 +01001928 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001929 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001930 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001931
David Sterba418775a2016-02-15 16:28:14 +01001932 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001933 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001934
David Sterba418775a2016-02-15 16:28:14 +01001935 if (ret)
1936 return ret;
1937 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001938 }
1939
1940 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001941}
1942
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001943static struct btrfs_device * btrfs_find_next_active_device(
1944 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001945{
1946 struct btrfs_device *next_device;
1947
1948 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1949 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001950 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1951 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001952 return next_device;
1953 }
1954
1955 return NULL;
1956}
1957
1958/*
1959 * Helper function to check if the given device is part of s_bdev / latest_bdev
1960 * and replace it with the provided or the next active device, in the context
1961 * where this function called, there should be always be another device (or
1962 * this_dev) which is active.
1963 */
David Sterbab105e922019-10-01 19:57:35 +02001964void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001965 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08001966{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001967 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08001968 struct btrfs_device *next_device;
1969
1970 if (this_dev)
1971 next_device = this_dev;
1972 else
1973 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1974 device);
1975 ASSERT(next_device);
1976
1977 if (fs_info->sb->s_bdev &&
1978 (fs_info->sb->s_bdev == device->bdev))
1979 fs_info->sb->s_bdev = next_device->bdev;
1980
1981 if (fs_info->fs_devices->latest_bdev == device->bdev)
1982 fs_info->fs_devices->latest_bdev = next_device->bdev;
1983}
1984
Anand Jain1da73962018-08-10 13:53:21 +08001985/*
1986 * Return btrfs_fs_devices::num_devices excluding the device that's being
1987 * currently replaced.
1988 */
1989static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1990{
1991 u64 num_devices = fs_info->fs_devices->num_devices;
1992
David Sterbacb5583d2018-09-07 16:11:23 +02001993 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001994 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1995 ASSERT(num_devices > 1);
1996 num_devices--;
1997 }
David Sterbacb5583d2018-09-07 16:11:23 +02001998 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001999
2000 return num_devices;
2001}
2002
Josef Bacik313b0852020-08-20 11:18:26 -04002003void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
2004 struct block_device *bdev,
2005 const char *device_path)
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002006{
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002007 struct btrfs_super_block *disk_super;
2008 int copy_num;
2009
2010 if (!bdev)
2011 return;
2012
2013 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002014 struct page *page;
2015 int ret;
2016
2017 disk_super = btrfs_read_dev_one_super(bdev, copy_num);
2018 if (IS_ERR(disk_super))
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002019 continue;
2020
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002021 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002022
2023 page = virt_to_page(disk_super);
2024 set_page_dirty(page);
2025 lock_page(page);
2026 /* write_on_page() unlocks the page */
2027 ret = write_one_page(page);
2028 if (ret)
2029 btrfs_warn(fs_info,
2030 "error clearing superblock number %d (%d)",
2031 copy_num, ret);
2032 btrfs_release_disk_super(disk_super);
2033
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002034 }
2035
2036 /* Notify udev that device has changed */
2037 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
2038
2039 /* Update ctime/mtime for device path for libblkid */
2040 update_dev_time(device_path);
2041}
2042
David Sterbada353f62017-02-14 17:55:53 +01002043int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2044 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002045{
2046 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002047 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002048 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002049 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002050 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002051
2052 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002053
Anand Jain1da73962018-08-10 13:53:21 +08002054 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002055
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002056 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002057 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002058 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002059
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002060 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2061
2062 if (IS_ERR(device)) {
2063 if (PTR_ERR(device) == -ENOENT &&
2064 strcmp(device_path, "missing") == 0)
2065 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2066 else
2067 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002068 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002069 }
Yan Zheng2b820322008-11-17 21:11:30 -05002070
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002071 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2072 btrfs_warn_in_rcu(fs_info,
2073 "cannot remove device %s (devid %llu) due to active swapfile",
2074 rcu_str_deref(device->name), device->devid);
2075 ret = -ETXTBSY;
2076 goto out;
2077 }
2078
Anand Jain401e29c2017-12-04 12:54:55 +08002079 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002080 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002081 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002082 }
2083
Anand Jainebbede42017-12-04 12:54:52 +08002084 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2085 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002086 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002087 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002088 }
2089
Anand Jainebbede42017-12-04 12:54:52 +08002090 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002091 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002092 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002093 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002094 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002095 }
Chris Masona061fc82008-05-07 11:43:44 -04002096
Carey Underwoodd7901552013-03-04 16:37:06 -06002097 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002098 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002099 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002100 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002101 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002102
Stefan Behrens63a212a2012-11-05 18:29:28 +01002103 /*
2104 * TODO: the superblock still includes this device in its num_devices
2105 * counter although write_all_supers() is not locked out. This
2106 * could give a filesystem state which requires a degraded mount.
2107 */
David Sterbaf331a952019-03-20 16:31:53 +01002108 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002109 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002110 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002111
Anand Jaine12c9622017-12-04 12:54:53 +08002112 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002113 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002114
2115 /*
2116 * the device list mutex makes sure that we don't change
2117 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002118 * the device supers. Whoever is writing all supers, should
2119 * lock the device list mutex before getting the number of
2120 * devices in the super block (super_copy). Conversely,
2121 * whoever updates the number of devices in the super block
2122 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002123 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002124
Anand Jain41a52a02018-04-12 10:29:31 +08002125 /*
2126 * In normal cases the cur_devices == fs_devices. But in case
2127 * of deleting a seed device, the cur_devices should point to
2128 * its own fs_devices listed under the fs_devices->seed.
2129 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002130 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002131 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002132 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002133
Anand Jain41a52a02018-04-12 10:29:31 +08002134 cur_devices->num_devices--;
2135 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002136 /* Update total_devices of the parent fs_devices if it's seed */
2137 if (cur_devices != fs_devices)
2138 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002139
Anand Jaine6e674b2017-12-04 12:54:54 +08002140 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002141 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002142
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002143 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002144
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002145 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002146 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002147 /* remove sysfs entry */
Anand Jainf3cd2c52020-02-12 17:28:13 +08002148 btrfs_sysfs_remove_devices_dir(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002149 }
Anand Jain99994cd2014-06-03 11:36:00 +08002150
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002151 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2152 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002153 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002154
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002155 /*
2156 * at this point, the device is zero sized and detached from
2157 * the devices list. All that's left is to zero out the old
2158 * supers and free the device.
2159 */
Anand Jainebbede42017-12-04 12:54:52 +08002160 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002161 btrfs_scratch_superblocks(fs_info, device->bdev,
2162 device->name->str);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002163
2164 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002165 synchronize_rcu();
2166 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002167
Xiao Guangrong1f781602011-04-20 10:09:16 +00002168 if (cur_devices->open_devices == 0) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002169 list_del_init(&cur_devices->seed_list);
Anand Jain0226e0e2018-04-12 10:29:27 +08002170 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002171 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002172 }
2173
Chris Masona061fc82008-05-07 11:43:44 -04002174out:
2175 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002176 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002177
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002178error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002179 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002180 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002181 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002182 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002183 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002184 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002185 }
Anand Jain24fc5722016-02-13 10:01:36 +08002186 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002187}
2188
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002189void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002190{
Anand Jaind51908c2014-08-13 14:24:19 +08002191 struct btrfs_fs_devices *fs_devices;
2192
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002193 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002194
Anand Jain25e8e912014-08-20 10:56:56 +08002195 /*
2196 * in case of fs with no seed, srcdev->fs_devices will point
2197 * to fs_devices of fs_info. However when the dev being replaced is
2198 * a seed dev it will point to the seed's local fs_devices. In short
2199 * srcdev will have its correct fs_devices in both the cases.
2200 */
2201 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002202
Stefan Behrense93c89c2012-11-05 17:33:06 +01002203 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002204 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002205 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002206 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002207 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002208
Anand Jainebbede42017-12-04 12:54:52 +08002209 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002210 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002211
Miao Xie82372bc2014-09-03 21:35:44 +08002212 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002213 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002214}
2215
David Sterba65237ee2019-03-20 16:34:54 +01002216void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002217{
2218 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002219
Josef Bacika466c852020-08-20 11:18:27 -04002220 mutex_lock(&uuid_mutex);
2221
Anand Jain14238812016-07-22 06:04:53 +08002222 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002223 synchronize_rcu();
2224 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002225
Anand Jain94d5f0c2014-08-13 14:24:22 +08002226 /* if this is no devs we rather delete the fs_devices */
2227 if (!fs_devices->num_devices) {
Anand Jain6dd38f82017-10-17 06:53:50 +08002228 /*
2229 * On a mounted FS, num_devices can't be zero unless it's a
2230 * seed. In case of a seed device being replaced, the replace
2231 * target added to the sprout FS, so there will be no more
2232 * device left under the seed FS.
2233 */
2234 ASSERT(fs_devices->seeding);
2235
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002236 list_del_init(&fs_devices->seed_list);
Anand Jain0226e0e2018-04-12 10:29:27 +08002237 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002238 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002239 }
Josef Bacika466c852020-08-20 11:18:27 -04002240 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002241}
2242
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002243void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002244{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002245 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002246
Anand Jaind9a071f2018-04-12 10:29:38 +08002247 mutex_lock(&fs_devices->device_list_mutex);
2248
Anand Jainf3cd2c52020-02-12 17:28:13 +08002249 btrfs_sysfs_remove_devices_dir(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002250
Anand Jain779bf3fe2016-04-18 16:51:23 +08002251 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002252 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002253
Anand Jaind9a071f2018-04-12 10:29:38 +08002254 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002255
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002256 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002257
Stefan Behrense93c89c2012-11-05 17:33:06 +01002258 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002259
Anand Jaind9a071f2018-04-12 10:29:38 +08002260 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002261
2262 /*
2263 * The update_dev_time() with in btrfs_scratch_superblocks()
2264 * may lead to a call to btrfs_show_devname() which will try
2265 * to hold device_list_mutex. And here this device
2266 * is already out of device list, so we don't have to hold
2267 * the device_list_mutex lock.
2268 */
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002269 btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev,
2270 tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002271
2272 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002273 synchronize_rcu();
2274 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002275}
2276
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002277static struct btrfs_device *btrfs_find_device_by_path(
2278 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002279{
2280 int ret = 0;
2281 struct btrfs_super_block *disk_super;
2282 u64 devid;
2283 u8 *dev_uuid;
2284 struct block_device *bdev;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002285 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002286
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002287 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002288 fs_info->bdev_holder, 0, &bdev, &disk_super);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002289 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002290 return ERR_PTR(ret);
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002291
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002292 devid = btrfs_stack_device_id(&disk_super->dev_item);
2293 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002294 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002295 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002296 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002297 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002298 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002299 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002300
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002301 btrfs_release_disk_super(disk_super);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002302 if (!device)
2303 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002304 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002305 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002306}
2307
Yan Zheng2b820322008-11-17 21:11:30 -05002308/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002309 * Lookup a device given by device id, or the path if the id is 0.
2310 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002311struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002312 struct btrfs_fs_info *fs_info, u64 devid,
2313 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002314{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002315 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002316
David Sterba5c5c0df2016-02-15 16:39:55 +01002317 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002318 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002319 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002320 if (!device)
2321 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002322 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002323 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002324
2325 if (!device_path || !device_path[0])
2326 return ERR_PTR(-EINVAL);
2327
2328 if (strcmp(device_path, "missing") == 0) {
2329 /* Find first missing device */
2330 list_for_each_entry(device, &fs_info->fs_devices->devices,
2331 dev_list) {
2332 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2333 &device->dev_state) && !device->bdev)
2334 return device;
2335 }
2336 return ERR_PTR(-ENOENT);
2337 }
2338
2339 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002340}
2341
Yan Zheng2b820322008-11-17 21:11:30 -05002342/*
2343 * does all the dirty work required for changing file system's UUID.
2344 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002345static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002346{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002347 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002348 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002349 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002350 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002351 struct btrfs_device *device;
2352 u64 super_flags;
2353
David Sterbaa32bf9a2018-03-16 02:21:22 +01002354 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002355 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002356 return -EINVAL;
2357
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03002358 /*
2359 * Private copy of the seed devices, anchored at
2360 * fs_info->fs_devices->seed_list
2361 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002362 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002363 if (IS_ERR(seed_devices))
2364 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002365
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03002366 /*
2367 * It's necessary to retain a copy of the original seed fs_devices in
2368 * fs_uuids so that filesystems which have been seeded can successfully
2369 * reference the seed device from open_seed_devices. This also supports
2370 * multiple fs seed.
2371 */
Yan Zhenge4404d62008-12-12 10:03:26 -05002372 old_devices = clone_fs_devices(fs_devices);
2373 if (IS_ERR(old_devices)) {
2374 kfree(seed_devices);
2375 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002376 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002377
Anand Jainc4babc52018-04-12 10:29:25 +08002378 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002379
Yan Zhenge4404d62008-12-12 10:03:26 -05002380 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2381 seed_devices->opened = 1;
2382 INIT_LIST_HEAD(&seed_devices->devices);
2383 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002384 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002385
Anand Jain321a4bf2018-07-16 22:58:09 +08002386 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002387 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2388 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002389 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002390 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002391
Johannes Thumshirn0395d842019-11-13 11:27:27 +01002392 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002393 fs_devices->num_devices = 0;
2394 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002395 fs_devices->missing_devices = 0;
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002396 fs_devices->rotating = false;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03002397 list_add(&seed_devices->seed_list, &fs_devices->seed_list);
Yan Zheng2b820322008-11-17 21:11:30 -05002398
2399 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002400 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002401 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002402 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002403
Yan Zheng2b820322008-11-17 21:11:30 -05002404 super_flags = btrfs_super_flags(disk_super) &
2405 ~BTRFS_SUPER_FLAG_SEEDING;
2406 btrfs_set_super_flags(disk_super, super_flags);
2407
2408 return 0;
2409}
2410
2411/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002412 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002413 */
David Sterba5c4666292019-03-20 16:36:39 +01002414static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002415{
David Sterba5c4666292019-03-20 16:36:39 +01002416 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002417 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002418 struct btrfs_path *path;
2419 struct extent_buffer *leaf;
2420 struct btrfs_dev_item *dev_item;
2421 struct btrfs_device *device;
2422 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002423 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002424 u8 dev_uuid[BTRFS_UUID_SIZE];
2425 u64 devid;
2426 int ret;
2427
2428 path = btrfs_alloc_path();
2429 if (!path)
2430 return -ENOMEM;
2431
Yan Zheng2b820322008-11-17 21:11:30 -05002432 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2433 key.offset = 0;
2434 key.type = BTRFS_DEV_ITEM_KEY;
2435
2436 while (1) {
2437 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2438 if (ret < 0)
2439 goto error;
2440
2441 leaf = path->nodes[0];
2442next_slot:
2443 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2444 ret = btrfs_next_leaf(root, path);
2445 if (ret > 0)
2446 break;
2447 if (ret < 0)
2448 goto error;
2449 leaf = path->nodes[0];
2450 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002451 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002452 continue;
2453 }
2454
2455 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2456 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2457 key.type != BTRFS_DEV_ITEM_KEY)
2458 break;
2459
2460 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2461 struct btrfs_dev_item);
2462 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002463 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002464 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002465 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002466 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002467 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002468 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002469 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002470
2471 if (device->fs_devices->seeding) {
2472 btrfs_set_device_generation(leaf, dev_item,
2473 device->generation);
2474 btrfs_mark_buffer_dirty(leaf);
2475 }
2476
2477 path->slots[0]++;
2478 goto next_slot;
2479 }
2480 ret = 0;
2481error:
2482 btrfs_free_path(path);
2483 return ret;
2484}
2485
David Sterbada353f62017-02-14 17:55:53 +01002486int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002487{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002488 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002489 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002490 struct btrfs_trans_handle *trans;
2491 struct btrfs_device *device;
2492 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002494 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002495 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002496 u64 orig_super_total_bytes;
2497 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002498 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002499 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002500 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002501
Anand Jain5da54bc2018-07-03 13:14:50 +08002502 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002503 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002504
Li Zefana5d163332011-12-07 20:08:40 -05002505 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002507 if (IS_ERR(bdev))
2508 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002509
Anand Jain5da54bc2018-07-03 13:14:50 +08002510 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002511 seeding_dev = 1;
2512 down_write(&sb->s_umount);
2513 mutex_lock(&uuid_mutex);
2514 }
2515
Chris Mason8c8bee12008-09-29 11:19:10 -04002516 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002517
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002518 rcu_read_lock();
2519 list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002520 if (device->bdev == bdev) {
2521 ret = -EEXIST;
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002522 rcu_read_unlock();
Yan Zheng2b820322008-11-17 21:11:30 -05002523 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002524 }
2525 }
Nikolay Borisovf4cfa9b2020-07-22 11:09:22 +03002526 rcu_read_unlock();
Chris Mason788f20e2008-04-28 15:29:42 -04002527
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002528 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002529 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002530 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002531 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002532 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002533 }
2534
David Sterba78f2c9e2016-02-11 14:25:38 +01002535 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002536 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002537 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002538 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002539 }
Josef Bacik606686e2012-06-04 14:03:51 -04002540 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002541
Yan, Zhenga22285a2010-05-16 10:48:46 -04002542 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002543 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002544 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002545 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002546 }
2547
Josef Bacikd5e20032011-08-04 14:52:27 +00002548 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002549 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002550 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002551 device->io_width = fs_info->sectorsize;
2552 device->io_align = fs_info->sectorsize;
2553 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002554 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2555 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002556 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002557 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002558 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002559 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002560 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002561 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002562 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002563 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002564 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002565
Yan Zheng2b820322008-11-17 21:11:30 -05002566 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002567 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002568 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002569 if (ret) {
2570 btrfs_abort_transaction(trans, ret);
2571 goto error_trans;
2572 }
Yan Zheng2b820322008-11-17 21:11:30 -05002573 }
2574
Anand Jain5da54bc2018-07-03 13:14:50 +08002575 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002576
Anand Jain5da54bc2018-07-03 13:14:50 +08002577 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002578 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002579 list_add_rcu(&device->dev_list, &fs_devices->devices);
2580 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2581 fs_devices->num_devices++;
2582 fs_devices->open_devices++;
2583 fs_devices->rw_devices++;
2584 fs_devices->total_devices++;
2585 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002586
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002587 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002588
Anand Jaine884f4f2017-04-04 18:40:19 +08002589 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002590 fs_devices->rotating = true;
Chris Masonc2898112009-06-10 09:51:32 -04002591
Naohiro Aota39379fa2018-07-27 09:04:55 +09002592 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002593 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002594 round_down(orig_super_total_bytes + device->total_bytes,
2595 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002596
Naohiro Aota39379fa2018-07-27 09:04:55 +09002597 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2598 btrfs_set_super_num_devices(fs_info->super_copy,
2599 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002600
2601 /* add sysfs device entry */
Anand Jainf3cd2c52020-02-12 17:28:13 +08002602 btrfs_sysfs_add_devices_dir(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002603
Miao Xie2196d6e2014-09-03 21:35:41 +08002604 /*
2605 * we've got more storage, clear any full flags on the space
2606 * infos
2607 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002608 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002609
David Sterba34441362016-10-04 19:34:27 +02002610 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002611 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002612
Yan Zheng2b820322008-11-17 21:11:30 -05002613 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002614 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002615 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002616 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002617 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002618 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002619 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002620 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002621 }
2622
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002623 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002624 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002625 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002626 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002627 }
2628
2629 if (seeding_dev) {
David Sterba5c4666292019-03-20 16:36:39 +01002630 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002631 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002632 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002633 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002634 }
Anand Jainb2373f22014-06-03 11:36:03 +08002635
Nikolay Borisov8e560082020-08-12 16:18:51 +03002636 /*
2637 * fs_devices now represents the newly sprouted filesystem and
2638 * its fsid has been changed by btrfs_prepare_sprout
2639 */
2640 btrfs_sysfs_update_sprout_fsid(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002641 }
2642
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002643 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002644
2645 if (seeding_dev) {
2646 mutex_unlock(&uuid_mutex);
2647 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002648 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002649
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002650 if (ret) /* transaction commit */
2651 return ret;
2652
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002653 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002655 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002656 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002657 trans = btrfs_attach_transaction(root);
2658 if (IS_ERR(trans)) {
2659 if (PTR_ERR(trans) == -ENOENT)
2660 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002661 ret = PTR_ERR(trans);
2662 trans = NULL;
2663 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002664 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002665 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002666 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002667
Anand Jain7f551d92020-05-05 02:58:26 +08002668 /*
2669 * Now that we have written a new super block to this device, check all
2670 * other fs_devices list if device_path alienates any other scanned
2671 * device.
2672 * We can ignore the return value as it typically returns -EINVAL and
2673 * only succeeds if the device was an alien.
2674 */
2675 btrfs_forget_devices(device_path);
2676
2677 /* Update ctime/mtime for blkid or udev */
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002678 update_dev_time(device_path);
Anand Jain7f551d92020-05-05 02:58:26 +08002679
Chris Mason788f20e2008-04-28 15:29:42 -04002680 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002681
Anand Jaind31c32f2017-09-28 14:51:10 +08002682error_sysfs:
Anand Jainf3cd2c52020-02-12 17:28:13 +08002683 btrfs_sysfs_remove_devices_dir(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002684 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2685 mutex_lock(&fs_info->chunk_mutex);
2686 list_del_rcu(&device->dev_list);
2687 list_del(&device->dev_alloc_list);
2688 fs_info->fs_devices->num_devices--;
2689 fs_info->fs_devices->open_devices--;
2690 fs_info->fs_devices->rw_devices--;
2691 fs_info->fs_devices->total_devices--;
2692 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2693 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2694 btrfs_set_super_total_bytes(fs_info->super_copy,
2695 orig_super_total_bytes);
2696 btrfs_set_super_num_devices(fs_info->super_copy,
2697 orig_super_num_devices);
2698 mutex_unlock(&fs_info->chunk_mutex);
2699 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002700error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002701 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002702 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002703 if (trans)
2704 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002705error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002706 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002707error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002708 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002709 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002710 mutex_unlock(&uuid_mutex);
2711 up_write(&sb->s_umount);
2712 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002713 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002714}
2715
Chris Masond3977122009-01-05 21:25:51 -05002716static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2717 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002718{
2719 int ret;
2720 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002721 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002722 struct btrfs_dev_item *dev_item;
2723 struct extent_buffer *leaf;
2724 struct btrfs_key key;
2725
Chris Mason0b86a832008-03-24 15:01:56 -04002726 path = btrfs_alloc_path();
2727 if (!path)
2728 return -ENOMEM;
2729
2730 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2731 key.type = BTRFS_DEV_ITEM_KEY;
2732 key.offset = device->devid;
2733
2734 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2735 if (ret < 0)
2736 goto out;
2737
2738 if (ret > 0) {
2739 ret = -ENOENT;
2740 goto out;
2741 }
2742
2743 leaf = path->nodes[0];
2744 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2745
2746 btrfs_set_device_id(leaf, dev_item, device->devid);
2747 btrfs_set_device_type(leaf, dev_item, device->type);
2748 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2749 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2750 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002751 btrfs_set_device_total_bytes(leaf, dev_item,
2752 btrfs_device_get_disk_total_bytes(device));
2753 btrfs_set_device_bytes_used(leaf, dev_item,
2754 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002755 btrfs_mark_buffer_dirty(leaf);
2756
2757out:
2758 btrfs_free_path(path);
2759 return ret;
2760}
2761
Miao Xie2196d6e2014-09-03 21:35:41 +08002762int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002763 struct btrfs_device *device, u64 new_size)
2764{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002765 struct btrfs_fs_info *fs_info = device->fs_info;
2766 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002767 u64 old_total;
2768 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002769
Anand Jainebbede42017-12-04 12:54:52 +08002770 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002771 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002772
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002773 new_size = round_down(new_size, fs_info->sectorsize);
2774
David Sterba34441362016-10-04 19:34:27 +02002775 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002776 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002777 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002778
Stefan Behrens63a212a2012-11-05 18:29:28 +01002779 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002780 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002781 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002782 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002783 }
Yan Zheng2b820322008-11-17 21:11:30 -05002784
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002785 btrfs_set_super_total_bytes(super_copy,
2786 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002787 device->fs_devices->total_rw_bytes += diff;
2788
Miao Xie7cc8e582014-09-03 21:35:38 +08002789 btrfs_device_set_total_bytes(device, new_size);
2790 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002791 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002792 if (list_empty(&device->post_commit_list))
2793 list_add_tail(&device->post_commit_list,
2794 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002795 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002796
Chris Mason8f18cf12008-04-25 16:53:30 -04002797 return btrfs_update_device(trans, device);
2798}
2799
Nikolay Borisovf4208792018-07-20 19:37:52 +03002800static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002801{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002802 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002803 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002804 int ret;
2805 struct btrfs_path *path;
2806 struct btrfs_key key;
2807
Chris Mason8f18cf12008-04-25 16:53:30 -04002808 path = btrfs_alloc_path();
2809 if (!path)
2810 return -ENOMEM;
2811
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002812 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002813 key.offset = chunk_offset;
2814 key.type = BTRFS_CHUNK_ITEM_KEY;
2815
2816 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002817 if (ret < 0)
2818 goto out;
2819 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002820 btrfs_handle_fs_error(fs_info, -ENOENT,
2821 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002822 ret = -ENOENT;
2823 goto out;
2824 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002825
2826 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002827 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002828 btrfs_handle_fs_error(fs_info, ret,
2829 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002830out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002831 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002832 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002833}
2834
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002835static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002836{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002837 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002838 struct btrfs_disk_key *disk_key;
2839 struct btrfs_chunk *chunk;
2840 u8 *ptr;
2841 int ret = 0;
2842 u32 num_stripes;
2843 u32 array_size;
2844 u32 len = 0;
2845 u32 cur;
2846 struct btrfs_key key;
2847
David Sterba34441362016-10-04 19:34:27 +02002848 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002849 array_size = btrfs_super_sys_array_size(super_copy);
2850
2851 ptr = super_copy->sys_chunk_array;
2852 cur = 0;
2853
2854 while (cur < array_size) {
2855 disk_key = (struct btrfs_disk_key *)ptr;
2856 btrfs_disk_key_to_cpu(&key, disk_key);
2857
2858 len = sizeof(*disk_key);
2859
2860 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2861 chunk = (struct btrfs_chunk *)(ptr + len);
2862 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2863 len += btrfs_chunk_item_size(num_stripes);
2864 } else {
2865 ret = -EIO;
2866 break;
2867 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002868 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002869 key.offset == chunk_offset) {
2870 memmove(ptr, ptr + len, array_size - (cur + len));
2871 array_size -= len;
2872 btrfs_set_super_sys_array_size(super_copy, array_size);
2873 } else {
2874 ptr += len;
2875 cur += len;
2876 }
2877 }
David Sterba34441362016-10-04 19:34:27 +02002878 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002879 return ret;
2880}
2881
Omar Sandoval60ca8422018-05-16 16:34:31 -07002882/*
2883 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2884 * @logical: Logical block offset in bytes.
2885 * @length: Length of extent in bytes.
2886 *
2887 * Return: Chunk mapping or ERR_PTR.
2888 */
2889struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2890 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002891{
2892 struct extent_map_tree *em_tree;
2893 struct extent_map *em;
2894
David Sterbac8bf1b62019-05-17 11:43:17 +02002895 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002896 read_lock(&em_tree->lock);
2897 em = lookup_extent_mapping(em_tree, logical, length);
2898 read_unlock(&em_tree->lock);
2899
2900 if (!em) {
2901 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2902 logical, length);
2903 return ERR_PTR(-EINVAL);
2904 }
2905
2906 if (em->start > logical || em->start + em->len < logical) {
2907 btrfs_crit(fs_info,
2908 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2909 logical, length, em->start, em->start + em->len);
2910 free_extent_map(em);
2911 return ERR_PTR(-EINVAL);
2912 }
2913
2914 /* callers are responsible for dropping em's ref. */
2915 return em;
2916}
2917
Nikolay Borisov97aff912018-07-20 19:37:53 +03002918int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002919{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002920 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002921 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002922 struct map_lookup *map;
2923 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002924 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002925 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002926
Omar Sandoval60ca8422018-05-16 16:34:31 -07002927 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002928 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002929 /*
2930 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002931 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002932 * do anything we still error out.
2933 */
2934 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002935 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002936 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002937 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002938 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002939 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002940 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002941
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002942 /*
2943 * Take the device list mutex to prevent races with the final phase of
2944 * a device replace operation that replaces the device object associated
2945 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2946 */
2947 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002948 for (i = 0; i < map->num_stripes; i++) {
2949 struct btrfs_device *device = map->stripes[i].dev;
2950 ret = btrfs_free_dev_extent(trans, device,
2951 map->stripes[i].physical,
2952 &dev_extent_len);
2953 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002954 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002955 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002956 goto out;
2957 }
2958
2959 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002960 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002961 btrfs_device_set_bytes_used(device,
2962 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002963 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002964 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002965 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002966 }
2967
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03002968 ret = btrfs_update_device(trans, device);
2969 if (ret) {
2970 mutex_unlock(&fs_devices->device_list_mutex);
2971 btrfs_abort_transaction(trans, ret);
2972 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002973 }
2974 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002975 mutex_unlock(&fs_devices->device_list_mutex);
2976
Nikolay Borisovf4208792018-07-20 19:37:52 +03002977 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002978 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002979 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002980 goto out;
2981 }
2982
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002983 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002984
2985 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002986 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002987 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002988 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002989 goto out;
2990 }
2991 }
2992
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03002993 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002994 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002995 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002996 goto out;
2997 }
2998
Josef Bacik47ab2a62014-09-18 11:20:02 -04002999out:
3000 /* once for us */
3001 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003002 return ret;
3003}
3004
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003005static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003006{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003007 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003008 struct btrfs_trans_handle *trans;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003009 struct btrfs_block_group *block_group;
Chris Mason8f18cf12008-04-25 16:53:30 -04003010 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003011
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003012 /*
3013 * Prevent races with automatic removal of unused block groups.
3014 * After we relocate and before we remove the chunk with offset
3015 * chunk_offset, automatic removal of the block group can kick in,
3016 * resulting in a failure when calling btrfs_remove_chunk() below.
3017 *
3018 * Make sure to acquire this mutex before doing a tree search (dev
3019 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3020 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3021 * we release the path used to search the chunk/dev tree and before
3022 * the current task acquires this mutex and calls us.
3023 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003024 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003025
Chris Mason8f18cf12008-04-25 16:53:30 -04003026 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003027 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003028 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003029 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003030 if (ret)
3031 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003032
Dennis Zhoub0643e52019-12-13 16:22:14 -08003033 block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
3034 if (!block_group)
3035 return -ENOENT;
3036 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
3037 btrfs_put_block_group(block_group);
3038
Chris Mason19c4d2f2016-10-10 13:43:31 -07003039 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3040 chunk_offset);
3041 if (IS_ERR(trans)) {
3042 ret = PTR_ERR(trans);
3043 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3044 return ret;
3045 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003046
Chris Mason19c4d2f2016-10-10 13:43:31 -07003047 /*
3048 * step two, delete the device extents and the
3049 * chunk tree entries
3050 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003051 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003052 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003053 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003054}
3055
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003056static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003057{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003058 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003059 struct btrfs_path *path;
3060 struct extent_buffer *leaf;
3061 struct btrfs_chunk *chunk;
3062 struct btrfs_key key;
3063 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003064 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003065 bool retried = false;
3066 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003067 int ret;
3068
3069 path = btrfs_alloc_path();
3070 if (!path)
3071 return -ENOMEM;
3072
Josef Bacikba1bf482009-09-11 16:11:19 -04003073again:
Yan Zheng2b820322008-11-17 21:11:30 -05003074 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3075 key.offset = (u64)-1;
3076 key.type = BTRFS_CHUNK_ITEM_KEY;
3077
3078 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003079 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003080 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003081 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003082 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003083 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003084 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003085 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003086
3087 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3088 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003089 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003090 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003091 if (ret < 0)
3092 goto error;
3093 if (ret > 0)
3094 break;
3095
3096 leaf = path->nodes[0];
3097 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3098
3099 chunk = btrfs_item_ptr(leaf, path->slots[0],
3100 struct btrfs_chunk);
3101 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003102 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003103
3104 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003105 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003106 if (ret == -ENOSPC)
3107 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303108 else
3109 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003110 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003111 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003112
3113 if (found_key.offset == 0)
3114 break;
3115 key.offset = found_key.offset - 1;
3116 }
3117 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003118 if (failed && !retried) {
3119 failed = 0;
3120 retried = true;
3121 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303122 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003123 ret = -ENOSPC;
3124 }
Yan Zheng2b820322008-11-17 21:11:30 -05003125error:
3126 btrfs_free_path(path);
3127 return ret;
3128}
3129
Liu Boa6f93c72017-11-15 16:28:11 -07003130/*
3131 * return 1 : allocate a data chunk successfully,
3132 * return <0: errors during allocating a data chunk,
3133 * return 0 : no need to allocate a data chunk.
3134 */
3135static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3136 u64 chunk_offset)
3137{
David Sterba32da53862019-10-29 19:20:18 +01003138 struct btrfs_block_group *cache;
Liu Boa6f93c72017-11-15 16:28:11 -07003139 u64 bytes_used;
3140 u64 chunk_type;
3141
3142 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3143 ASSERT(cache);
3144 chunk_type = cache->flags;
3145 btrfs_put_block_group(cache);
3146
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003147 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3148 return 0;
Liu Boa6f93c72017-11-15 16:28:11 -07003149
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003150 spin_lock(&fs_info->data_sinfo->lock);
3151 bytes_used = fs_info->data_sinfo->bytes_used;
3152 spin_unlock(&fs_info->data_sinfo->lock);
Liu Boa6f93c72017-11-15 16:28:11 -07003153
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003154 if (!bytes_used) {
3155 struct btrfs_trans_handle *trans;
3156 int ret;
Liu Boa6f93c72017-11-15 16:28:11 -07003157
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003158 trans = btrfs_join_transaction(fs_info->tree_root);
3159 if (IS_ERR(trans))
3160 return PTR_ERR(trans);
3161
3162 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3163 btrfs_end_transaction(trans);
3164 if (ret < 0)
3165 return ret;
3166 return 1;
Liu Boa6f93c72017-11-15 16:28:11 -07003167 }
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003168
Liu Boa6f93c72017-11-15 16:28:11 -07003169 return 0;
3170}
3171
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003172static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003173 struct btrfs_balance_control *bctl)
3174{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003175 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003176 struct btrfs_trans_handle *trans;
3177 struct btrfs_balance_item *item;
3178 struct btrfs_disk_balance_args disk_bargs;
3179 struct btrfs_path *path;
3180 struct extent_buffer *leaf;
3181 struct btrfs_key key;
3182 int ret, err;
3183
3184 path = btrfs_alloc_path();
3185 if (!path)
3186 return -ENOMEM;
3187
3188 trans = btrfs_start_transaction(root, 0);
3189 if (IS_ERR(trans)) {
3190 btrfs_free_path(path);
3191 return PTR_ERR(trans);
3192 }
3193
3194 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003195 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003196 key.offset = 0;
3197
3198 ret = btrfs_insert_empty_item(trans, root, path, &key,
3199 sizeof(*item));
3200 if (ret)
3201 goto out;
3202
3203 leaf = path->nodes[0];
3204 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3205
David Sterbab159fa22016-11-08 18:09:03 +01003206 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003207
3208 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3209 btrfs_set_balance_data(leaf, item, &disk_bargs);
3210 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3211 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3212 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3213 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3214
3215 btrfs_set_balance_flags(leaf, item, bctl->flags);
3216
3217 btrfs_mark_buffer_dirty(leaf);
3218out:
3219 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003220 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003221 if (err && !ret)
3222 ret = err;
3223 return ret;
3224}
3225
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003226static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003227{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003228 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003229 struct btrfs_trans_handle *trans;
3230 struct btrfs_path *path;
3231 struct btrfs_key key;
3232 int ret, err;
3233
3234 path = btrfs_alloc_path();
3235 if (!path)
3236 return -ENOMEM;
3237
David Sterba3502a8c2020-06-25 12:35:28 +02003238 trans = btrfs_start_transaction_fallback_global_rsv(root, 0);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003239 if (IS_ERR(trans)) {
3240 btrfs_free_path(path);
3241 return PTR_ERR(trans);
3242 }
3243
3244 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003245 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003246 key.offset = 0;
3247
3248 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3249 if (ret < 0)
3250 goto out;
3251 if (ret > 0) {
3252 ret = -ENOENT;
3253 goto out;
3254 }
3255
3256 ret = btrfs_del_item(trans, root, path);
3257out:
3258 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003259 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003260 if (err && !ret)
3261 ret = err;
3262 return ret;
3263}
3264
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003265/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003266 * This is a heuristic used to reduce the number of chunks balanced on
3267 * resume after balance was interrupted.
3268 */
3269static void update_balance_args(struct btrfs_balance_control *bctl)
3270{
3271 /*
3272 * Turn on soft mode for chunk types that were being converted.
3273 */
3274 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3275 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3276 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3277 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3278 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3279 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3280
3281 /*
3282 * Turn on usage filter if is not already used. The idea is
3283 * that chunks that we have already balanced should be
3284 * reasonably full. Don't do it for chunks that are being
3285 * converted - that will keep us from relocating unconverted
3286 * (albeit full) chunks.
3287 */
3288 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003289 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003290 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3291 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3292 bctl->data.usage = 90;
3293 }
3294 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003295 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003296 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3297 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3298 bctl->sys.usage = 90;
3299 }
3300 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003301 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003302 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3303 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3304 bctl->meta.usage = 90;
3305 }
3306}
3307
3308/*
David Sterba149196a2018-03-20 20:23:09 +01003309 * Clear the balance status in fs_info and delete the balance item from disk.
3310 */
3311static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003312{
3313 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003314 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003315
3316 BUG_ON(!fs_info->balance_ctl);
3317
3318 spin_lock(&fs_info->balance_lock);
3319 fs_info->balance_ctl = NULL;
3320 spin_unlock(&fs_info->balance_lock);
3321
3322 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003323 ret = del_balance_item(fs_info);
3324 if (ret)
3325 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003326}
3327
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003328/*
3329 * Balance filters. Return 1 if chunk should be filtered out
3330 * (should not be balanced).
3331 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003332static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003333 struct btrfs_balance_args *bargs)
3334{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003335 chunk_type = chunk_to_extended(chunk_type) &
3336 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003337
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003338 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003339 return 0;
3340
3341 return 1;
3342}
3343
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003344static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003345 struct btrfs_balance_args *bargs)
3346{
David Sterba32da53862019-10-29 19:20:18 +01003347 struct btrfs_block_group *cache;
David Sterbabc309462015-10-20 18:22:13 +02003348 u64 chunk_used;
3349 u64 user_thresh_min;
3350 u64 user_thresh_max;
3351 int ret = 1;
3352
3353 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003354 chunk_used = cache->used;
David Sterbabc309462015-10-20 18:22:13 +02003355
3356 if (bargs->usage_min == 0)
3357 user_thresh_min = 0;
3358 else
David Sterbab3470b52019-10-23 18:48:22 +02003359 user_thresh_min = div_factor_fine(cache->length,
3360 bargs->usage_min);
David Sterbabc309462015-10-20 18:22:13 +02003361
3362 if (bargs->usage_max == 0)
3363 user_thresh_max = 1;
3364 else if (bargs->usage_max > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003365 user_thresh_max = cache->length;
David Sterbabc309462015-10-20 18:22:13 +02003366 else
David Sterbab3470b52019-10-23 18:48:22 +02003367 user_thresh_max = div_factor_fine(cache->length,
3368 bargs->usage_max);
David Sterbabc309462015-10-20 18:22:13 +02003369
3370 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3371 ret = 0;
3372
3373 btrfs_put_block_group(cache);
3374 return ret;
3375}
3376
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003377static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003378 u64 chunk_offset, struct btrfs_balance_args *bargs)
3379{
David Sterba32da53862019-10-29 19:20:18 +01003380 struct btrfs_block_group *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003381 u64 chunk_used, user_thresh;
3382 int ret = 1;
3383
3384 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003385 chunk_used = cache->used;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003386
David Sterbabc309462015-10-20 18:22:13 +02003387 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003388 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003389 else if (bargs->usage > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003390 user_thresh = cache->length;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003391 else
David Sterbab3470b52019-10-23 18:48:22 +02003392 user_thresh = div_factor_fine(cache->length, bargs->usage);
Ilya Dryomova105bb82013-01-21 15:15:56 +02003393
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003394 if (chunk_used < user_thresh)
3395 ret = 0;
3396
3397 btrfs_put_block_group(cache);
3398 return ret;
3399}
3400
Ilya Dryomov409d4042012-01-16 22:04:47 +02003401static int chunk_devid_filter(struct extent_buffer *leaf,
3402 struct btrfs_chunk *chunk,
3403 struct btrfs_balance_args *bargs)
3404{
3405 struct btrfs_stripe *stripe;
3406 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3407 int i;
3408
3409 for (i = 0; i < num_stripes; i++) {
3410 stripe = btrfs_stripe_nr(chunk, i);
3411 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3412 return 0;
3413 }
3414
3415 return 1;
3416}
3417
David Sterba946c9252019-05-17 11:43:34 +02003418static u64 calc_data_stripes(u64 type, int num_stripes)
3419{
3420 const int index = btrfs_bg_flags_to_raid_index(type);
3421 const int ncopies = btrfs_raid_array[index].ncopies;
3422 const int nparity = btrfs_raid_array[index].nparity;
3423
3424 if (nparity)
3425 return num_stripes - nparity;
3426 else
3427 return num_stripes / ncopies;
3428}
3429
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003430/* [pstart, pend) */
3431static int chunk_drange_filter(struct extent_buffer *leaf,
3432 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003433 struct btrfs_balance_args *bargs)
3434{
3435 struct btrfs_stripe *stripe;
3436 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3437 u64 stripe_offset;
3438 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003439 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003440 int factor;
3441 int i;
3442
3443 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3444 return 0;
3445
David Sterba946c9252019-05-17 11:43:34 +02003446 type = btrfs_chunk_type(leaf, chunk);
3447 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003448
3449 for (i = 0; i < num_stripes; i++) {
3450 stripe = btrfs_stripe_nr(chunk, i);
3451 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3452 continue;
3453
3454 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3455 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003456 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003457
3458 if (stripe_offset < bargs->pend &&
3459 stripe_offset + stripe_length > bargs->pstart)
3460 return 0;
3461 }
3462
3463 return 1;
3464}
3465
Ilya Dryomovea671762012-01-16 22:04:48 +02003466/* [vstart, vend) */
3467static int chunk_vrange_filter(struct extent_buffer *leaf,
3468 struct btrfs_chunk *chunk,
3469 u64 chunk_offset,
3470 struct btrfs_balance_args *bargs)
3471{
3472 if (chunk_offset < bargs->vend &&
3473 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3474 /* at least part of the chunk is inside this vrange */
3475 return 0;
3476
3477 return 1;
3478}
3479
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003480static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3481 struct btrfs_chunk *chunk,
3482 struct btrfs_balance_args *bargs)
3483{
3484 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3485
3486 if (bargs->stripes_min <= num_stripes
3487 && num_stripes <= bargs->stripes_max)
3488 return 0;
3489
3490 return 1;
3491}
3492
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003493static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003494 struct btrfs_balance_args *bargs)
3495{
3496 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3497 return 0;
3498
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003499 chunk_type = chunk_to_extended(chunk_type) &
3500 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003501
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003502 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003503 return 1;
3504
3505 return 0;
3506}
3507
David Sterba6ec08962019-03-20 16:38:52 +01003508static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003509 struct btrfs_chunk *chunk, u64 chunk_offset)
3510{
David Sterba6ec08962019-03-20 16:38:52 +01003511 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003512 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003513 struct btrfs_balance_args *bargs = NULL;
3514 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3515
3516 /* type filter */
3517 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3518 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3519 return 0;
3520 }
3521
3522 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3523 bargs = &bctl->data;
3524 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3525 bargs = &bctl->sys;
3526 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3527 bargs = &bctl->meta;
3528
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003529 /* profiles filter */
3530 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3531 chunk_profiles_filter(chunk_type, bargs)) {
3532 return 0;
3533 }
3534
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003535 /* usage filter */
3536 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003537 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003538 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003539 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003540 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003541 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003542 }
3543
Ilya Dryomov409d4042012-01-16 22:04:47 +02003544 /* devid filter */
3545 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3546 chunk_devid_filter(leaf, chunk, bargs)) {
3547 return 0;
3548 }
3549
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003550 /* drange filter, makes sense only with devid filter */
3551 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003552 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003553 return 0;
3554 }
3555
Ilya Dryomovea671762012-01-16 22:04:48 +02003556 /* vrange filter */
3557 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3558 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3559 return 0;
3560 }
3561
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003562 /* stripes filter */
3563 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3564 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3565 return 0;
3566 }
3567
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003568 /* soft profile changing mode */
3569 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3570 chunk_soft_convert_filter(chunk_type, bargs)) {
3571 return 0;
3572 }
3573
David Sterba7d824b62014-05-07 17:37:51 +02003574 /*
3575 * limited by count, must be the last filter
3576 */
3577 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3578 if (bargs->limit == 0)
3579 return 0;
3580 else
3581 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003582 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3583 /*
3584 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003585 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003586 * about the count of all chunks that satisfy the filters.
3587 */
3588 if (bargs->limit_max == 0)
3589 return 0;
3590 else
3591 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003592 }
3593
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003594 return 1;
3595}
3596
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003597static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003598{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003599 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003600 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003601 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003602 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003603 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003604 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003605 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003606 struct extent_buffer *leaf;
3607 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003608 int ret;
3609 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003610 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003611 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003612 u64 limit_data = bctl->data.limit;
3613 u64 limit_meta = bctl->meta.limit;
3614 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003615 u32 count_data = 0;
3616 u32 count_meta = 0;
3617 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003618 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003619
Chris Masonec44a352008-04-28 15:29:52 -04003620 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003621 if (!path) {
3622 ret = -ENOMEM;
3623 goto error;
3624 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003625
3626 /* zero out stat counters */
3627 spin_lock(&fs_info->balance_lock);
3628 memset(&bctl->stat, 0, sizeof(bctl->stat));
3629 spin_unlock(&fs_info->balance_lock);
3630again:
David Sterba7d824b62014-05-07 17:37:51 +02003631 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003632 /*
3633 * The single value limit and min/max limits use the same bytes
3634 * in the
3635 */
David Sterba7d824b62014-05-07 17:37:51 +02003636 bctl->data.limit = limit_data;
3637 bctl->meta.limit = limit_meta;
3638 bctl->sys.limit = limit_sys;
3639 }
Chris Masonec44a352008-04-28 15:29:52 -04003640 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3641 key.offset = (u64)-1;
3642 key.type = BTRFS_CHUNK_ITEM_KEY;
3643
Chris Masond3977122009-01-05 21:25:51 -05003644 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003645 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003646 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003647 ret = -ECANCELED;
3648 goto error;
3649 }
3650
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003651 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003652 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003653 if (ret < 0) {
3654 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003655 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003656 }
Chris Masonec44a352008-04-28 15:29:52 -04003657
3658 /*
3659 * this shouldn't happen, it means the last relocate
3660 * failed
3661 */
3662 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003663 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003664
3665 ret = btrfs_previous_item(chunk_root, path, 0,
3666 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003667 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003668 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003669 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003670 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003671 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003672
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003673 leaf = path->nodes[0];
3674 slot = path->slots[0];
3675 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3676
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003677 if (found_key.objectid != key.objectid) {
3678 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003679 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003680 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003681
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003682 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003683 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003684
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003685 if (!counting) {
3686 spin_lock(&fs_info->balance_lock);
3687 bctl->stat.considered++;
3688 spin_unlock(&fs_info->balance_lock);
3689 }
3690
David Sterba6ec08962019-03-20 16:38:52 +01003691 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003692
David Sterbab3b4aa72011-04-21 01:20:15 +02003693 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003694 if (!ret) {
3695 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003696 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003697 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003698
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003699 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003700 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003701 spin_lock(&fs_info->balance_lock);
3702 bctl->stat.expected++;
3703 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003704
3705 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3706 count_data++;
3707 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3708 count_sys++;
3709 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3710 count_meta++;
3711
3712 goto loop;
3713 }
3714
3715 /*
3716 * Apply limit_min filter, no need to check if the LIMITS
3717 * filter is used, limit_min is 0 by default
3718 */
3719 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3720 count_data < bctl->data.limit_min)
3721 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3722 count_meta < bctl->meta.limit_min)
3723 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3724 count_sys < bctl->sys.limit_min)) {
3725 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003726 goto loop;
3727 }
3728
Liu Boa6f93c72017-11-15 16:28:11 -07003729 if (!chunk_reserved) {
3730 /*
3731 * We may be relocating the only data chunk we have,
3732 * which could potentially end up with losing data's
3733 * raid profile, so lets allocate an empty one in
3734 * advance.
3735 */
3736 ret = btrfs_may_alloc_data_chunk(fs_info,
3737 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003738 if (ret < 0) {
3739 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3740 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003741 } else if (ret == 1) {
3742 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003743 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003744 }
3745
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003746 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003747 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003748 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003749 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003750 } else if (ret == -ETXTBSY) {
3751 btrfs_info(fs_info,
3752 "skipping relocation of block group %llu due to active swapfile",
3753 found_key.offset);
3754 ret = 0;
3755 } else if (ret) {
3756 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003757 } else {
3758 spin_lock(&fs_info->balance_lock);
3759 bctl->stat.completed++;
3760 spin_unlock(&fs_info->balance_lock);
3761 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003762loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003763 if (found_key.offset == 0)
3764 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003765 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003766 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003767
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003768 if (counting) {
3769 btrfs_release_path(path);
3770 counting = false;
3771 goto again;
3772 }
Chris Masonec44a352008-04-28 15:29:52 -04003773error:
3774 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003775 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003776 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003777 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003778 if (!ret)
3779 ret = -ENOSPC;
3780 }
3781
Chris Masonec44a352008-04-28 15:29:52 -04003782 return ret;
3783}
3784
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003785/**
3786 * alloc_profile_is_valid - see if a given profile is valid and reduced
3787 * @flags: profile to validate
3788 * @extended: if true @flags is treated as an extended profile
3789 */
3790static int alloc_profile_is_valid(u64 flags, int extended)
3791{
3792 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3793 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3794
3795 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3796
3797 /* 1) check that all other bits are zeroed */
3798 if (flags & ~mask)
3799 return 0;
3800
3801 /* 2) see if profile is reduced */
3802 if (flags == 0)
3803 return !extended; /* "0" is valid for usual profiles */
3804
David Sterbac1499162019-10-01 19:44:42 +02003805 return has_single_bit_set(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003806}
3807
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003808static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3809{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003810 /* cancel requested || normal exit path */
3811 return atomic_read(&fs_info->balance_cancel_req) ||
3812 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3813 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003814}
3815
David Sterba5ba366c2020-02-27 21:00:52 +01003816/*
3817 * Validate target profile against allowed profiles and return true if it's OK.
3818 * Otherwise print the error message and return false.
3819 */
3820static inline int validate_convert_profile(struct btrfs_fs_info *fs_info,
3821 const struct btrfs_balance_args *bargs,
3822 u64 allowed, const char *type)
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003823{
David Sterba5ba366c2020-02-27 21:00:52 +01003824 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3825 return true;
3826
3827 /* Profile is valid and does not have bits outside of the allowed set */
3828 if (alloc_profile_is_valid(bargs->target, 1) &&
3829 (bargs->target & ~allowed) == 0)
3830 return true;
3831
3832 btrfs_err(fs_info, "balance: invalid convert %s profile %s",
3833 type, btrfs_bg_type_to_raid_name(bargs->target));
3834 return false;
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003835}
3836
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003837/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003838 * Fill @buf with textual description of balance filter flags @bargs, up to
3839 * @size_buf including the terminating null. The output may be trimmed if it
3840 * does not fit into the provided buffer.
3841 */
3842static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3843 u32 size_buf)
3844{
3845 int ret;
3846 u32 size_bp = size_buf;
3847 char *bp = buf;
3848 u64 flags = bargs->flags;
3849 char tmp_buf[128] = {'\0'};
3850
3851 if (!flags)
3852 return;
3853
3854#define CHECK_APPEND_NOARG(a) \
3855 do { \
3856 ret = snprintf(bp, size_bp, (a)); \
3857 if (ret < 0 || ret >= size_bp) \
3858 goto out_overflow; \
3859 size_bp -= ret; \
3860 bp += ret; \
3861 } while (0)
3862
3863#define CHECK_APPEND_1ARG(a, v1) \
3864 do { \
3865 ret = snprintf(bp, size_bp, (a), (v1)); \
3866 if (ret < 0 || ret >= size_bp) \
3867 goto out_overflow; \
3868 size_bp -= ret; \
3869 bp += ret; \
3870 } while (0)
3871
3872#define CHECK_APPEND_2ARG(a, v1, v2) \
3873 do { \
3874 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3875 if (ret < 0 || ret >= size_bp) \
3876 goto out_overflow; \
3877 size_bp -= ret; \
3878 bp += ret; \
3879 } while (0)
3880
David Sterba158da512019-05-17 11:43:41 +02003881 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3882 CHECK_APPEND_1ARG("convert=%s,",
3883 btrfs_bg_type_to_raid_name(bargs->target));
Anand Jain56fc37d2018-11-20 16:12:56 +08003884
3885 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3886 CHECK_APPEND_NOARG("soft,");
3887
3888 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3889 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3890 sizeof(tmp_buf));
3891 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3892 }
3893
3894 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3895 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3896
3897 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3898 CHECK_APPEND_2ARG("usage=%u..%u,",
3899 bargs->usage_min, bargs->usage_max);
3900
3901 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3902 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3903
3904 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3905 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3906 bargs->pstart, bargs->pend);
3907
3908 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3909 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3910 bargs->vstart, bargs->vend);
3911
3912 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3913 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3914
3915 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3916 CHECK_APPEND_2ARG("limit=%u..%u,",
3917 bargs->limit_min, bargs->limit_max);
3918
3919 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3920 CHECK_APPEND_2ARG("stripes=%u..%u,",
3921 bargs->stripes_min, bargs->stripes_max);
3922
3923#undef CHECK_APPEND_2ARG
3924#undef CHECK_APPEND_1ARG
3925#undef CHECK_APPEND_NOARG
3926
3927out_overflow:
3928
3929 if (size_bp < size_buf)
3930 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3931 else
3932 buf[0] = '\0';
3933}
3934
3935static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3936{
3937 u32 size_buf = 1024;
3938 char tmp_buf[192] = {'\0'};
3939 char *buf;
3940 char *bp;
3941 u32 size_bp = size_buf;
3942 int ret;
3943 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3944
3945 buf = kzalloc(size_buf, GFP_KERNEL);
3946 if (!buf)
3947 return;
3948
3949 bp = buf;
3950
3951#define CHECK_APPEND_1ARG(a, v1) \
3952 do { \
3953 ret = snprintf(bp, size_bp, (a), (v1)); \
3954 if (ret < 0 || ret >= size_bp) \
3955 goto out_overflow; \
3956 size_bp -= ret; \
3957 bp += ret; \
3958 } while (0)
3959
3960 if (bctl->flags & BTRFS_BALANCE_FORCE)
3961 CHECK_APPEND_1ARG("%s", "-f ");
3962
3963 if (bctl->flags & BTRFS_BALANCE_DATA) {
3964 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3965 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3966 }
3967
3968 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3969 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3970 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3971 }
3972
3973 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3974 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3975 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3976 }
3977
3978#undef CHECK_APPEND_1ARG
3979
3980out_overflow:
3981
3982 if (size_bp < size_buf)
3983 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3984 btrfs_info(fs_info, "balance: %s %s",
3985 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3986 "resume" : "start", buf);
3987
3988 kfree(buf);
3989}
3990
3991/*
David Sterbadccdb072018-03-21 00:20:05 +01003992 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003993 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003994int btrfs_balance(struct btrfs_fs_info *fs_info,
3995 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996 struct btrfs_ioctl_balance_args *bargs)
3997{
Adam Borowski14506122017-03-07 23:34:44 +01003998 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003999 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004000 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004001 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004002 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004003 unsigned seq;
Anand Jaine62869b2019-09-25 14:29:28 +08004004 bool reducing_redundancy;
David Sterba081db892019-05-17 11:43:27 +02004005 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004006
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004007 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004008 atomic_read(&fs_info->balance_pause_req) ||
Qu Wenruo726a3422020-02-17 14:16:52 +08004009 btrfs_should_cancel_balance(fs_info)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004010 ret = -EINVAL;
4011 goto out;
4012 }
4013
Ilya Dryomove4837f82012-03-27 17:09:17 +03004014 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4015 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4016 mixed = 1;
4017
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004018 /*
4019 * In case of mixed groups both data and meta should be picked,
4020 * and identical options should be given for both of them.
4021 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004022 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4023 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004024 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4025 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4026 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004027 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004028 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004029 ret = -EINVAL;
4030 goto out;
4031 }
4032 }
4033
Josef Bacikb35cf1f2020-01-10 11:11:24 -05004034 /*
4035 * rw_devices will not change at the moment, device add/delete/replace
4036 * are excluded by EXCL_OP
4037 */
4038 num_devices = fs_info->fs_devices->rw_devices;
Qu Wenruofab27352019-09-25 10:13:27 +08004039
4040 /*
4041 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4042 * special bit for it, to make it easier to distinguish. Thus we need
4043 * to set it manually, or balance would refuse the profile.
4044 */
4045 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
David Sterba081db892019-05-17 11:43:27 +02004046 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4047 if (num_devices >= btrfs_raid_array[i].devs_min)
4048 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08004049
David Sterba5ba366c2020-02-27 21:00:52 +01004050 if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") ||
4051 !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") ||
4052 !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004053 ret = -EINVAL;
4054 goto out;
4055 }
4056
David Sterba6079e122019-05-17 11:43:29 +02004057 /*
4058 * Allow to reduce metadata or system integrity only if force set for
4059 * profiles with redundancy (copies, parity)
4060 */
4061 allowed = 0;
4062 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4063 if (btrfs_raid_array[i].ncopies >= 2 ||
4064 btrfs_raid_array[i].tolerated_failures >= 1)
4065 allowed |= btrfs_raid_array[i].bg_flag;
4066 }
Miao Xiede98ced2013-01-29 10:13:12 +00004067 do {
4068 seq = read_seqbegin(&fs_info->profiles_lock);
4069
4070 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4071 (fs_info->avail_system_alloc_bits & allowed) &&
4072 !(bctl->sys.target & allowed)) ||
4073 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4074 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004075 !(bctl->meta.target & allowed)))
Anand Jaine62869b2019-09-25 14:29:28 +08004076 reducing_redundancy = true;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004077 else
Anand Jaine62869b2019-09-25 14:29:28 +08004078 reducing_redundancy = false;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004079
4080 /* if we're not converting, the target field is uninitialized */
4081 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4082 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4083 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4084 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004085 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004086
Anand Jaine62869b2019-09-25 14:29:28 +08004087 if (reducing_redundancy) {
Filipe Manana5a8067c2018-11-19 09:48:12 +00004088 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4089 btrfs_info(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004090 "balance: force reducing metadata redundancy");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004091 } else {
4092 btrfs_err(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004093 "balance: reduces metadata redundancy, use --force if you want this");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004094 ret = -EINVAL;
4095 goto out;
4096 }
4097 }
4098
Adam Borowski14506122017-03-07 23:34:44 +01004099 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4100 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00004101 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004102 "balance: metadata profile %s has lower redundancy than data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004103 btrfs_bg_type_to_raid_name(meta_target),
4104 btrfs_bg_type_to_raid_name(data_target));
Sam Tygieree592d02016-01-06 08:46:12 +00004105 }
4106
Filipe Manana9e967492019-04-22 16:44:09 +01004107 if (fs_info->send_in_progress) {
4108 btrfs_warn_rl(fs_info,
4109"cannot run balance while send operations are in progress (%d in progress)",
4110 fs_info->send_in_progress);
4111 ret = -EAGAIN;
4112 goto out;
4113 }
4114
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004115 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004116 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004117 goto out;
4118
Ilya Dryomov59641012012-01-16 22:04:48 +02004119 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4120 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004121 BUG_ON(fs_info->balance_ctl);
4122 spin_lock(&fs_info->balance_lock);
4123 fs_info->balance_ctl = bctl;
4124 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004125 } else {
4126 BUG_ON(ret != -EEXIST);
4127 spin_lock(&fs_info->balance_lock);
4128 update_balance_args(bctl);
4129 spin_unlock(&fs_info->balance_lock);
4130 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004131
David Sterba3009a622018-03-21 01:31:04 +01004132 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4133 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004134 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004135 mutex_unlock(&fs_info->balance_mutex);
4136
4137 ret = __btrfs_balance(fs_info);
4138
4139 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004140 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4141 btrfs_info(fs_info, "balance: paused");
Qu Wenruo44d354a2020-07-13 09:03:21 +08004142 /*
4143 * Balance can be canceled by:
4144 *
4145 * - Regular cancel request
4146 * Then ret == -ECANCELED and balance_cancel_req > 0
4147 *
4148 * - Fatal signal to "btrfs" process
4149 * Either the signal caught by wait_reserve_ticket() and callers
4150 * got -EINTR, or caught by btrfs_should_cancel_balance() and
4151 * got -ECANCELED.
4152 * Either way, in this case balance_cancel_req = 0, and
4153 * ret == -EINTR or ret == -ECANCELED.
4154 *
4155 * So here we only check the return value to catch canceled balance.
4156 */
4157 else if (ret == -ECANCELED || ret == -EINTR)
Anand Jain7333bd02018-11-20 16:12:57 +08004158 btrfs_info(fs_info, "balance: canceled");
4159 else
4160 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4161
David Sterba3009a622018-03-21 01:31:04 +01004162 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004163
4164 if (bargs) {
4165 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004166 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004167 }
4168
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004169 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4170 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004171 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004172 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004173 }
4174
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004175 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004176
4177 return ret;
4178out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004179 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004180 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004181 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004182 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004183 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4184
Ilya Dryomov59641012012-01-16 22:04:48 +02004185 return ret;
4186}
4187
4188static int balance_kthread(void *data)
4189{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004190 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004191 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004192
Ilya Dryomov59641012012-01-16 22:04:48 +02004193 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004194 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004195 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004196 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004197
Ilya Dryomov59641012012-01-16 22:04:48 +02004198 return ret;
4199}
4200
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004201int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4202{
4203 struct task_struct *tsk;
4204
David Sterba1354e1a2018-03-21 02:29:13 +01004205 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004206 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004207 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004208 return 0;
4209 }
David Sterba1354e1a2018-03-21 02:29:13 +01004210 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004211
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004212 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004213 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004214 return 0;
4215 }
4216
Anand Jain02ee6542018-05-17 15:16:51 +08004217 /*
4218 * A ro->rw remount sequence should continue with the paused balance
4219 * regardless of who pauses it, system or the user as of now, so set
4220 * the resume flag.
4221 */
4222 spin_lock(&fs_info->balance_lock);
4223 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4224 spin_unlock(&fs_info->balance_lock);
4225
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004226 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304227 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004228}
4229
Ilya Dryomov68310a52012-06-22 12:24:12 -06004230int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004231{
Ilya Dryomov59641012012-01-16 22:04:48 +02004232 struct btrfs_balance_control *bctl;
4233 struct btrfs_balance_item *item;
4234 struct btrfs_disk_balance_args disk_bargs;
4235 struct btrfs_path *path;
4236 struct extent_buffer *leaf;
4237 struct btrfs_key key;
4238 int ret;
4239
4240 path = btrfs_alloc_path();
4241 if (!path)
4242 return -ENOMEM;
4243
Ilya Dryomov68310a52012-06-22 12:24:12 -06004244 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004245 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004246 key.offset = 0;
4247
4248 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4249 if (ret < 0)
4250 goto out;
4251 if (ret > 0) { /* ret = -ENOENT; */
4252 ret = 0;
4253 goto out;
4254 }
4255
Ilya Dryomov59641012012-01-16 22:04:48 +02004256 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4257 if (!bctl) {
4258 ret = -ENOMEM;
4259 goto out;
4260 }
4261
Ilya Dryomov59641012012-01-16 22:04:48 +02004262 leaf = path->nodes[0];
4263 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4264
Ilya Dryomov68310a52012-06-22 12:24:12 -06004265 bctl->flags = btrfs_balance_flags(leaf, item);
4266 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004267
4268 btrfs_balance_data(leaf, item, &disk_bargs);
4269 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4270 btrfs_balance_meta(leaf, item, &disk_bargs);
4271 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4272 btrfs_balance_sys(leaf, item, &disk_bargs);
4273 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4274
David Sterbaeee95e32018-03-20 20:07:58 +01004275 /*
4276 * This should never happen, as the paused balance state is recovered
4277 * during mount without any chance of other exclusive ops to collide.
4278 *
4279 * This gives the exclusive op status to balance and keeps in paused
4280 * state until user intervention (cancel or umount). If the ownership
4281 * cannot be assigned, show a message but do not fail. The balance
4282 * is in a paused state and must have fs_info::balance_ctl properly
4283 * set up.
4284 */
4285 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4286 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004287 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004288
Ilya Dryomov68310a52012-06-22 12:24:12 -06004289 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004290 BUG_ON(fs_info->balance_ctl);
4291 spin_lock(&fs_info->balance_lock);
4292 fs_info->balance_ctl = bctl;
4293 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004294 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004295out:
4296 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004297 return ret;
4298}
4299
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004300int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4301{
4302 int ret = 0;
4303
4304 mutex_lock(&fs_info->balance_mutex);
4305 if (!fs_info->balance_ctl) {
4306 mutex_unlock(&fs_info->balance_mutex);
4307 return -ENOTCONN;
4308 }
4309
David Sterba3009a622018-03-21 01:31:04 +01004310 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004311 atomic_inc(&fs_info->balance_pause_req);
4312 mutex_unlock(&fs_info->balance_mutex);
4313
4314 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004315 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004316
4317 mutex_lock(&fs_info->balance_mutex);
4318 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004319 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004320 atomic_dec(&fs_info->balance_pause_req);
4321 } else {
4322 ret = -ENOTCONN;
4323 }
4324
4325 mutex_unlock(&fs_info->balance_mutex);
4326 return ret;
4327}
4328
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004329int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4330{
4331 mutex_lock(&fs_info->balance_mutex);
4332 if (!fs_info->balance_ctl) {
4333 mutex_unlock(&fs_info->balance_mutex);
4334 return -ENOTCONN;
4335 }
4336
David Sterbacf7d20f2018-03-21 01:45:32 +01004337 /*
4338 * A paused balance with the item stored on disk can be resumed at
4339 * mount time if the mount is read-write. Otherwise it's still paused
4340 * and we must not allow cancelling as it deletes the item.
4341 */
4342 if (sb_rdonly(fs_info->sb)) {
4343 mutex_unlock(&fs_info->balance_mutex);
4344 return -EROFS;
4345 }
4346
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004347 atomic_inc(&fs_info->balance_cancel_req);
4348 /*
4349 * if we are running just wait and return, balance item is
4350 * deleted in btrfs_balance in this case
4351 */
David Sterba3009a622018-03-21 01:31:04 +01004352 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004353 mutex_unlock(&fs_info->balance_mutex);
4354 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004355 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004356 mutex_lock(&fs_info->balance_mutex);
4357 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004358 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004359 /*
4360 * Lock released to allow other waiters to continue, we'll
4361 * reexamine the status again.
4362 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004363 mutex_lock(&fs_info->balance_mutex);
4364
David Sterbaa17c95d2018-03-20 17:28:05 +01004365 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004366 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004367 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004368 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004369 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004370 }
4371
David Sterba3009a622018-03-21 01:31:04 +01004372 BUG_ON(fs_info->balance_ctl ||
4373 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004374 atomic_dec(&fs_info->balance_cancel_req);
4375 mutex_unlock(&fs_info->balance_mutex);
4376 return 0;
4377}
4378
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02004379int btrfs_uuid_scan_kthread(void *data)
Stefan Behrens803b2f52013-08-15 17:11:21 +02004380{
4381 struct btrfs_fs_info *fs_info = data;
4382 struct btrfs_root *root = fs_info->tree_root;
4383 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004384 struct btrfs_path *path = NULL;
4385 int ret = 0;
4386 struct extent_buffer *eb;
4387 int slot;
4388 struct btrfs_root_item root_item;
4389 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004390 struct btrfs_trans_handle *trans = NULL;
Josef Bacikc94bec22020-02-14 15:05:01 -05004391 bool closing = false;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004392
4393 path = btrfs_alloc_path();
4394 if (!path) {
4395 ret = -ENOMEM;
4396 goto out;
4397 }
4398
4399 key.objectid = 0;
4400 key.type = BTRFS_ROOT_ITEM_KEY;
4401 key.offset = 0;
4402
Stefan Behrens803b2f52013-08-15 17:11:21 +02004403 while (1) {
Josef Bacikc94bec22020-02-14 15:05:01 -05004404 if (btrfs_fs_closing(fs_info)) {
4405 closing = true;
4406 break;
4407 }
Anand Jain7c829b72018-03-07 17:29:18 +08004408 ret = btrfs_search_forward(root, &key, path,
4409 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004410 if (ret) {
4411 if (ret > 0)
4412 ret = 0;
4413 break;
4414 }
4415
4416 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4417 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4418 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4419 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4420 goto skip;
4421
4422 eb = path->nodes[0];
4423 slot = path->slots[0];
4424 item_size = btrfs_item_size_nr(eb, slot);
4425 if (item_size < sizeof(root_item))
4426 goto skip;
4427
Stefan Behrens803b2f52013-08-15 17:11:21 +02004428 read_extent_buffer(eb, &root_item,
4429 btrfs_item_ptr_offset(eb, slot),
4430 (int)sizeof(root_item));
4431 if (btrfs_root_refs(&root_item) == 0)
4432 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004433
4434 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4435 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4436 if (trans)
4437 goto update_tree;
4438
4439 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004440 /*
4441 * 1 - subvol uuid item
4442 * 1 - received_subvol uuid item
4443 */
4444 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4445 if (IS_ERR(trans)) {
4446 ret = PTR_ERR(trans);
4447 break;
4448 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004449 continue;
4450 } else {
4451 goto skip;
4452 }
4453update_tree:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004454 btrfs_release_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004455 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004456 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004457 BTRFS_UUID_KEY_SUBVOL,
4458 key.objectid);
4459 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004460 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004461 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004462 break;
4463 }
4464 }
4465
4466 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004467 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004468 root_item.received_uuid,
4469 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4470 key.objectid);
4471 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004472 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004473 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004474 break;
4475 }
4476 }
4477
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004478skip:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004479 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004480 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004481 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004482 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004483 if (ret)
4484 break;
4485 }
4486
Stefan Behrens803b2f52013-08-15 17:11:21 +02004487 if (key.offset < (u64)-1) {
4488 key.offset++;
4489 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4490 key.offset = 0;
4491 key.type = BTRFS_ROOT_ITEM_KEY;
4492 } else if (key.objectid < (u64)-1) {
4493 key.offset = 0;
4494 key.type = BTRFS_ROOT_ITEM_KEY;
4495 key.objectid++;
4496 } else {
4497 break;
4498 }
4499 cond_resched();
4500 }
4501
4502out:
4503 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004504 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004505 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004506 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004507 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Josef Bacikc94bec22020-02-14 15:05:01 -05004508 else if (!closing)
Josef Bacikafcdd122016-09-02 15:40:02 -04004509 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004510 up(&fs_info->uuid_tree_rescan_sem);
4511 return 0;
4512}
4513
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004514int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4515{
4516 struct btrfs_trans_handle *trans;
4517 struct btrfs_root *tree_root = fs_info->tree_root;
4518 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004519 struct task_struct *task;
4520 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004521
4522 /*
4523 * 1 - root node
4524 * 1 - root item
4525 */
4526 trans = btrfs_start_transaction(tree_root, 2);
4527 if (IS_ERR(trans))
4528 return PTR_ERR(trans);
4529
David Sterba9b7a2442019-03-20 13:20:49 +01004530 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004531 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004532 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004533 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004534 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004535 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004536 }
4537
4538 fs_info->uuid_root = uuid_root;
4539
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004540 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004541 if (ret)
4542 return ret;
4543
4544 down(&fs_info->uuid_tree_rescan_sem);
4545 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4546 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004547 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004548 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004549 up(&fs_info->uuid_tree_rescan_sem);
4550 return PTR_ERR(task);
4551 }
4552
4553 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004554}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004555
Chris Mason8f18cf12008-04-25 16:53:30 -04004556/*
4557 * shrinking a device means finding all of the device extents past
4558 * the new size, and then following the back refs to the chunks.
4559 * The chunk relocation code actually frees the device extent
4560 */
4561int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4562{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004563 struct btrfs_fs_info *fs_info = device->fs_info;
4564 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004565 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004566 struct btrfs_dev_extent *dev_extent = NULL;
4567 struct btrfs_path *path;
4568 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004569 u64 chunk_offset;
4570 int ret;
4571 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004572 int failed = 0;
4573 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004574 struct extent_buffer *l;
4575 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004576 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004577 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004578 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004579 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004580 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004581
4582 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004583 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004584 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004585
Anand Jain401e29c2017-12-04 12:54:55 +08004586 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004587 return -EINVAL;
4588
Chris Mason8f18cf12008-04-25 16:53:30 -04004589 path = btrfs_alloc_path();
4590 if (!path)
4591 return -ENOMEM;
4592
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004593 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004594
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004595 trans = btrfs_start_transaction(root, 0);
4596 if (IS_ERR(trans)) {
4597 btrfs_free_path(path);
4598 return PTR_ERR(trans);
4599 }
4600
David Sterba34441362016-10-04 19:34:27 +02004601 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004602
Miao Xie7cc8e582014-09-03 21:35:38 +08004603 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004604 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004605 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004606 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004607 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004608
4609 /*
4610 * Once the device's size has been set to the new size, ensure all
4611 * in-memory chunks are synced to disk so that the loop below sees them
4612 * and relocates them accordingly.
4613 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004614 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004615 mutex_unlock(&fs_info->chunk_mutex);
4616 ret = btrfs_commit_transaction(trans);
4617 if (ret)
4618 goto done;
4619 } else {
4620 mutex_unlock(&fs_info->chunk_mutex);
4621 btrfs_end_transaction(trans);
4622 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004623
Josef Bacikba1bf482009-09-11 16:11:19 -04004624again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004625 key.objectid = device->devid;
4626 key.offset = (u64)-1;
4627 key.type = BTRFS_DEV_EXTENT_KEY;
4628
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004629 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004630 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004631 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004632 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004633 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004634 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004635 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004636
4637 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004638 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004639 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004640 if (ret < 0)
4641 goto done;
4642 if (ret) {
4643 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004644 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004645 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004646 }
4647
4648 l = path->nodes[0];
4649 slot = path->slots[0];
4650 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4651
Josef Bacikba1bf482009-09-11 16:11:19 -04004652 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004653 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004654 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004655 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004656 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004657
4658 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4659 length = btrfs_dev_extent_length(l, dev_extent);
4660
Josef Bacikba1bf482009-09-11 16:11:19 -04004661 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004662 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004663 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004664 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004665 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004666
Chris Mason8f18cf12008-04-25 16:53:30 -04004667 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004668 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004669
Liu Boa6f93c72017-11-15 16:28:11 -07004670 /*
4671 * We may be relocating the only data chunk we have,
4672 * which could potentially end up with losing data's
4673 * raid profile, so lets allocate an empty one in
4674 * advance.
4675 */
4676 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4677 if (ret < 0) {
4678 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4679 goto done;
4680 }
4681
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004682 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4683 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004684 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004685 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004686 } else if (ret) {
4687 if (ret == -ETXTBSY) {
4688 btrfs_warn(fs_info,
4689 "could not shrink block group %llu due to active swapfile",
4690 chunk_offset);
4691 }
4692 goto done;
4693 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004694 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004695
4696 if (failed && !retried) {
4697 failed = 0;
4698 retried = true;
4699 goto again;
4700 } else if (failed && retried) {
4701 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004702 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004703 }
4704
Chris Balld6397ba2009-04-27 07:29:03 -04004705 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004706 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004707 if (IS_ERR(trans)) {
4708 ret = PTR_ERR(trans);
4709 goto done;
4710 }
4711
David Sterba34441362016-10-04 19:34:27 +02004712 mutex_lock(&fs_info->chunk_mutex);
Qu Wenruoc57dd1f2020-07-31 19:29:11 +08004713 /* Clear all state bits beyond the shrunk device size */
4714 clear_extent_bits(&device->alloc_state, new_size, (u64)-1,
4715 CHUNK_STATE_MASK);
4716
Miao Xie7cc8e582014-09-03 21:35:38 +08004717 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004718 if (list_empty(&device->post_commit_list))
4719 list_add_tail(&device->post_commit_list,
4720 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004721
Chris Balld6397ba2009-04-27 07:29:03 -04004722 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004723 btrfs_set_super_total_bytes(super_copy,
4724 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004725 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004726
4727 /* Now btrfs_update_device() will change the on-disk size. */
4728 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004729 if (ret < 0) {
4730 btrfs_abort_transaction(trans, ret);
4731 btrfs_end_transaction(trans);
4732 } else {
4733 ret = btrfs_commit_transaction(trans);
4734 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004735done:
4736 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004737 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004738 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004739 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004740 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004741 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004742 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004743 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004744 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004745 return ret;
4746}
4747
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004748static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004749 struct btrfs_key *key,
4750 struct btrfs_chunk *chunk, int item_size)
4751{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004752 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004753 struct btrfs_disk_key disk_key;
4754 u32 array_size;
4755 u8 *ptr;
4756
David Sterba34441362016-10-04 19:34:27 +02004757 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004758 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004759 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004760 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004761 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004762 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004763 }
Chris Mason0b86a832008-03-24 15:01:56 -04004764
4765 ptr = super_copy->sys_chunk_array + array_size;
4766 btrfs_cpu_key_to_disk(&disk_key, key);
4767 memcpy(ptr, &disk_key, sizeof(disk_key));
4768 ptr += sizeof(disk_key);
4769 memcpy(ptr, chunk, item_size);
4770 item_size += sizeof(disk_key);
4771 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004772 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004773
Chris Mason0b86a832008-03-24 15:01:56 -04004774 return 0;
4775}
4776
Miao Xieb2117a32011-01-05 10:07:28 +00004777/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004778 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004779 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004780static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004781{
Arne Jansen73c5de02011-04-12 12:07:57 +02004782 const struct btrfs_device_info *di_a = a;
4783 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004784
Arne Jansen73c5de02011-04-12 12:07:57 +02004785 if (di_a->max_avail > di_b->max_avail)
4786 return -1;
4787 if (di_a->max_avail < di_b->max_avail)
4788 return 1;
4789 if (di_a->total_avail > di_b->total_avail)
4790 return -1;
4791 if (di_a->total_avail < di_b->total_avail)
4792 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004793 return 0;
4794}
4795
David Woodhouse53b381b2013-01-29 18:40:14 -05004796static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4797{
Zhao Leiffe2d202015-01-20 15:11:44 +08004798 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004799 return;
4800
Miao Xieceda0862013-04-11 10:30:16 +00004801 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004802}
4803
David Sterbacfbb8252018-07-10 18:15:05 +02004804static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4805{
4806 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4807 return;
4808
4809 btrfs_set_fs_incompat(info, RAID1C34);
4810}
4811
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004812/*
4813 * Structure used internally for __btrfs_alloc_chunk() function.
4814 * Wraps needed parameters.
4815 */
4816struct alloc_chunk_ctl {
4817 u64 start;
4818 u64 type;
4819 /* Total number of stripes to allocate */
4820 int num_stripes;
4821 /* sub_stripes info for map */
4822 int sub_stripes;
4823 /* Stripes per device */
4824 int dev_stripes;
4825 /* Maximum number of devices to use */
4826 int devs_max;
4827 /* Minimum number of devices to use */
4828 int devs_min;
4829 /* ndevs has to be a multiple of this */
4830 int devs_increment;
4831 /* Number of copies */
4832 int ncopies;
4833 /* Number of stripes worth of bytes to store parity information */
4834 int nparity;
4835 u64 max_stripe_size;
4836 u64 max_chunk_size;
Naohiro Aota6aafb302020-02-25 12:56:15 +09004837 u64 dev_extent_min;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004838 u64 stripe_size;
4839 u64 chunk_size;
4840 int ndevs;
4841};
4842
Naohiro Aota27c314d2020-02-25 12:56:11 +09004843static void init_alloc_chunk_ctl_policy_regular(
4844 struct btrfs_fs_devices *fs_devices,
4845 struct alloc_chunk_ctl *ctl)
4846{
4847 u64 type = ctl->type;
4848
4849 if (type & BTRFS_BLOCK_GROUP_DATA) {
4850 ctl->max_stripe_size = SZ_1G;
4851 ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4852 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4853 /* For larger filesystems, use larger metadata chunks */
4854 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4855 ctl->max_stripe_size = SZ_1G;
4856 else
4857 ctl->max_stripe_size = SZ_256M;
4858 ctl->max_chunk_size = ctl->max_stripe_size;
4859 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4860 ctl->max_stripe_size = SZ_32M;
4861 ctl->max_chunk_size = 2 * ctl->max_stripe_size;
4862 ctl->devs_max = min_t(int, ctl->devs_max,
4863 BTRFS_MAX_DEVS_SYS_CHUNK);
4864 } else {
4865 BUG();
4866 }
4867
4868 /* We don't want a chunk larger than 10% of writable space */
4869 ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4870 ctl->max_chunk_size);
Naohiro Aota6aafb302020-02-25 12:56:15 +09004871 ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
Naohiro Aota27c314d2020-02-25 12:56:11 +09004872}
4873
4874static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices,
4875 struct alloc_chunk_ctl *ctl)
4876{
4877 int index = btrfs_bg_flags_to_raid_index(ctl->type);
4878
4879 ctl->sub_stripes = btrfs_raid_array[index].sub_stripes;
4880 ctl->dev_stripes = btrfs_raid_array[index].dev_stripes;
4881 ctl->devs_max = btrfs_raid_array[index].devs_max;
4882 if (!ctl->devs_max)
4883 ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info);
4884 ctl->devs_min = btrfs_raid_array[index].devs_min;
4885 ctl->devs_increment = btrfs_raid_array[index].devs_increment;
4886 ctl->ncopies = btrfs_raid_array[index].ncopies;
4887 ctl->nparity = btrfs_raid_array[index].nparity;
4888 ctl->ndevs = 0;
4889
4890 switch (fs_devices->chunk_alloc_policy) {
4891 case BTRFS_CHUNK_ALLOC_REGULAR:
4892 init_alloc_chunk_ctl_policy_regular(fs_devices, ctl);
4893 break;
4894 default:
4895 BUG();
4896 }
4897}
4898
Naohiro Aota560156c2020-02-25 12:56:12 +09004899static int gather_device_info(struct btrfs_fs_devices *fs_devices,
4900 struct alloc_chunk_ctl *ctl,
4901 struct btrfs_device_info *devices_info)
4902{
4903 struct btrfs_fs_info *info = fs_devices->fs_info;
4904 struct btrfs_device *device;
4905 u64 total_avail;
4906 u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes;
Naohiro Aota560156c2020-02-25 12:56:12 +09004907 int ret;
4908 int ndevs = 0;
4909 u64 max_avail;
4910 u64 dev_offset;
4911
4912 /*
4913 * in the first pass through the devices list, we gather information
4914 * about the available holes on each device.
4915 */
4916 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
4917 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4918 WARN(1, KERN_ERR
4919 "BTRFS: read-only device in alloc_list\n");
4920 continue;
4921 }
4922
4923 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4924 &device->dev_state) ||
4925 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4926 continue;
4927
4928 if (device->total_bytes > device->bytes_used)
4929 total_avail = device->total_bytes - device->bytes_used;
4930 else
4931 total_avail = 0;
4932
4933 /* If there is no space on this device, skip it. */
Naohiro Aota6aafb302020-02-25 12:56:15 +09004934 if (total_avail < ctl->dev_extent_min)
Naohiro Aota560156c2020-02-25 12:56:12 +09004935 continue;
4936
4937 ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
4938 &max_avail);
4939 if (ret && ret != -ENOSPC)
4940 return ret;
4941
4942 if (ret == 0)
4943 max_avail = dev_extent_want;
4944
Naohiro Aota6aafb302020-02-25 12:56:15 +09004945 if (max_avail < ctl->dev_extent_min) {
Naohiro Aota560156c2020-02-25 12:56:12 +09004946 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4947 btrfs_debug(info,
4948 "%s: devid %llu has no free space, have=%llu want=%llu",
4949 __func__, device->devid, max_avail,
Naohiro Aota6aafb302020-02-25 12:56:15 +09004950 ctl->dev_extent_min);
Naohiro Aota560156c2020-02-25 12:56:12 +09004951 continue;
4952 }
4953
4954 if (ndevs == fs_devices->rw_devices) {
4955 WARN(1, "%s: found more than %llu devices\n",
4956 __func__, fs_devices->rw_devices);
4957 break;
4958 }
4959 devices_info[ndevs].dev_offset = dev_offset;
4960 devices_info[ndevs].max_avail = max_avail;
4961 devices_info[ndevs].total_avail = total_avail;
4962 devices_info[ndevs].dev = device;
4963 ++ndevs;
4964 }
4965 ctl->ndevs = ndevs;
4966
4967 /*
4968 * now sort the devices by hole size / available space
4969 */
4970 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4971 btrfs_cmp_device_info, NULL);
4972
4973 return 0;
4974}
4975
Naohiro Aota5badf512020-02-25 12:56:13 +09004976static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
4977 struct btrfs_device_info *devices_info)
4978{
4979 /* Number of stripes that count for block group size */
4980 int data_stripes;
4981
4982 /*
4983 * The primary goal is to maximize the number of stripes, so use as
4984 * many devices as possible, even if the stripes are not maximum sized.
4985 *
4986 * The DUP profile stores more than one stripe per device, the
4987 * max_avail is the total size so we have to adjust.
4988 */
4989 ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail,
4990 ctl->dev_stripes);
4991 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
4992
4993 /* This will have to be fixed for RAID1 and RAID10 over more drives */
4994 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
4995
4996 /*
4997 * Use the number of data stripes to figure out how big this chunk is
4998 * really going to be in terms of logical address space, and compare
4999 * that answer with the max chunk size. If it's higher, we try to
5000 * reduce stripe_size.
5001 */
5002 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
5003 /*
5004 * Reduce stripe_size, round it up to a 16MB boundary again and
5005 * then use it, unless it ends up being even bigger than the
5006 * previous value we had already.
5007 */
5008 ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
5009 data_stripes), SZ_16M),
5010 ctl->stripe_size);
5011 }
5012
5013 /* Align to BTRFS_STRIPE_LEN */
5014 ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN);
5015 ctl->chunk_size = ctl->stripe_size * data_stripes;
5016
5017 return 0;
5018}
5019
5020static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
5021 struct alloc_chunk_ctl *ctl,
5022 struct btrfs_device_info *devices_info)
5023{
5024 struct btrfs_fs_info *info = fs_devices->fs_info;
5025
5026 /*
5027 * Round down to number of usable stripes, devs_increment can be any
5028 * number so we can't use round_down() that requires power of 2, while
5029 * rounddown is safe.
5030 */
5031 ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment);
5032
5033 if (ctl->ndevs < ctl->devs_min) {
5034 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5035 btrfs_debug(info,
5036 "%s: not enough devices with free space: have=%d minimum required=%d",
5037 __func__, ctl->ndevs, ctl->devs_min);
5038 }
5039 return -ENOSPC;
5040 }
5041
5042 ctl->ndevs = min(ctl->ndevs, ctl->devs_max);
5043
5044 switch (fs_devices->chunk_alloc_policy) {
5045 case BTRFS_CHUNK_ALLOC_REGULAR:
5046 return decide_stripe_size_regular(ctl, devices_info);
5047 default:
5048 BUG();
5049 }
5050}
5051
Naohiro Aotadce580c2020-02-25 12:56:14 +09005052static int create_chunk(struct btrfs_trans_handle *trans,
5053 struct alloc_chunk_ctl *ctl,
5054 struct btrfs_device_info *devices_info)
5055{
5056 struct btrfs_fs_info *info = trans->fs_info;
5057 struct map_lookup *map = NULL;
5058 struct extent_map_tree *em_tree;
5059 struct extent_map *em;
5060 u64 start = ctl->start;
5061 u64 type = ctl->type;
5062 int ret;
5063 int i;
5064 int j;
5065
5066 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);
5067 if (!map)
5068 return -ENOMEM;
5069 map->num_stripes = ctl->num_stripes;
5070
5071 for (i = 0; i < ctl->ndevs; ++i) {
5072 for (j = 0; j < ctl->dev_stripes; ++j) {
5073 int s = i * ctl->dev_stripes + j;
5074 map->stripes[s].dev = devices_info[i].dev;
5075 map->stripes[s].physical = devices_info[i].dev_offset +
5076 j * ctl->stripe_size;
5077 }
5078 }
5079 map->stripe_len = BTRFS_STRIPE_LEN;
5080 map->io_align = BTRFS_STRIPE_LEN;
5081 map->io_width = BTRFS_STRIPE_LEN;
5082 map->type = type;
5083 map->sub_stripes = ctl->sub_stripes;
5084
5085 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
5086
5087 em = alloc_extent_map();
5088 if (!em) {
5089 kfree(map);
5090 return -ENOMEM;
5091 }
5092 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5093 em->map_lookup = map;
5094 em->start = start;
5095 em->len = ctl->chunk_size;
5096 em->block_start = 0;
5097 em->block_len = em->len;
5098 em->orig_block_len = ctl->stripe_size;
5099
5100 em_tree = &info->mapping_tree;
5101 write_lock(&em_tree->lock);
5102 ret = add_extent_mapping(em_tree, em, 0);
5103 if (ret) {
5104 write_unlock(&em_tree->lock);
5105 free_extent_map(em);
5106 return ret;
5107 }
5108 write_unlock(&em_tree->lock);
5109
5110 ret = btrfs_make_block_group(trans, 0, type, start, ctl->chunk_size);
5111 if (ret)
5112 goto error_del_extent;
5113
5114 for (i = 0; i < map->num_stripes; i++) {
5115 struct btrfs_device *dev = map->stripes[i].dev;
5116
5117 btrfs_device_set_bytes_used(dev,
5118 dev->bytes_used + ctl->stripe_size);
5119 if (list_empty(&dev->post_commit_list))
5120 list_add_tail(&dev->post_commit_list,
5121 &trans->transaction->dev_update_list);
5122 }
5123
5124 atomic64_sub(ctl->stripe_size * map->num_stripes,
5125 &info->free_chunk_space);
5126
5127 free_extent_map(em);
5128 check_raid56_incompat_flag(info, type);
5129 check_raid1c34_incompat_flag(info, type);
5130
5131 return 0;
5132
5133error_del_extent:
5134 write_lock(&em_tree->lock);
5135 remove_extent_mapping(em_tree, em);
5136 write_unlock(&em_tree->lock);
5137
5138 /* One for our allocation */
5139 free_extent_map(em);
5140 /* One for the tree reference */
5141 free_extent_map(em);
5142
5143 return ret;
5144}
5145
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005146int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00005147{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005148 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00005149 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Arne Jansen73c5de02011-04-12 12:07:57 +02005150 struct btrfs_device_info *devices_info = NULL;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09005151 struct alloc_chunk_ctl ctl;
Miao Xieb2117a32011-01-05 10:07:28 +00005152 int ret;
Miao Xieb2117a32011-01-05 10:07:28 +00005153
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005154 lockdep_assert_held(&info->chunk_mutex);
5155
Naohiro Aotab25c19f2020-02-25 12:56:07 +09005156 if (!alloc_profile_is_valid(type, 0)) {
5157 ASSERT(0);
5158 return -EINVAL;
5159 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005160
Qu Wenruo4117f202018-01-22 13:50:54 +08005161 if (list_empty(&fs_devices->alloc_list)) {
5162 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5163 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005164 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005165 }
Miao Xieb2117a32011-01-05 10:07:28 +00005166
Naohiro Aota27c314d2020-02-25 12:56:11 +09005167 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
5168 btrfs_err(info, "invalid chunk type 0x%llx requested", type);
5169 ASSERT(0);
5170 return -EINVAL;
Arne Jansen73c5de02011-04-12 12:07:57 +02005171 }
5172
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005173 ctl.start = find_next_chunk(info);
Naohiro Aota27c314d2020-02-25 12:56:11 +09005174 ctl.type = type;
5175 init_alloc_chunk_ctl(fs_devices, &ctl);
Miao Xieb2117a32011-01-05 10:07:28 +00005176
David Sterba31e818f2015-02-20 18:00:26 +01005177 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005178 GFP_NOFS);
5179 if (!devices_info)
5180 return -ENOMEM;
5181
Naohiro Aota560156c2020-02-25 12:56:12 +09005182 ret = gather_device_info(fs_devices, &ctl, devices_info);
5183 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005184 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005185
Naohiro Aota5badf512020-02-25 12:56:13 +09005186 ret = decide_stripe_size(fs_devices, &ctl, devices_info);
5187 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005188 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005189
Naohiro Aotadce580c2020-02-25 12:56:14 +09005190 ret = create_chunk(trans, &ctl, devices_info);
Chris Mason9b3f68b2008-04-18 10:29:51 -04005191
Naohiro Aotadce580c2020-02-25 12:56:14 +09005192out:
Miao Xieb2117a32011-01-05 10:07:28 +00005193 kfree(devices_info);
5194 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005195}
5196
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005197/*
5198 * Chunk allocation falls into two parts. The first part does work
5199 * that makes the new allocated chunk usable, but does not do any operation
5200 * that modifies the chunk tree. The second part does the work that
5201 * requires modifying the chunk tree. This division is important for the
5202 * bootstrap process of adding storage to a seed btrfs.
5203 */
Josef Bacik6df9a952013-06-27 13:22:46 -04005204int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005205 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005206{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005207 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005208 struct btrfs_root *extent_root = fs_info->extent_root;
5209 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005210 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005211 struct btrfs_device *device;
5212 struct btrfs_chunk *chunk;
5213 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005214 struct extent_map *em;
5215 struct map_lookup *map;
5216 size_t item_size;
5217 u64 dev_offset;
5218 u64 stripe_size;
5219 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005220 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005221
Omar Sandoval60ca8422018-05-16 16:34:31 -07005222 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005223 if (IS_ERR(em))
5224 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005225
Jeff Mahoney95617d62015-06-03 10:55:48 -04005226 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005227 item_size = btrfs_chunk_item_size(map->num_stripes);
5228 stripe_size = em->orig_block_len;
5229
5230 chunk = kzalloc(item_size, GFP_NOFS);
5231 if (!chunk) {
5232 ret = -ENOMEM;
5233 goto out;
5234 }
5235
Filipe Manana50460e32015-11-20 10:42:47 +00005236 /*
5237 * Take the device list mutex to prevent races with the final phase of
5238 * a device replace operation that replaces the device object associated
5239 * with the map's stripes, because the device object's id can change
5240 * at any time during that final phase of the device replace operation
5241 * (dev-replace.c:btrfs_dev_replace_finishing()).
5242 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005243 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005244 for (i = 0; i < map->num_stripes; i++) {
5245 device = map->stripes[i].dev;
5246 dev_offset = map->stripes[i].physical;
5247
Yan Zheng2b820322008-11-17 21:11:30 -05005248 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005249 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005250 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005251 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5252 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005253 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005254 break;
5255 }
5256 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005257 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005258 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005259 }
5260
Yan Zheng2b820322008-11-17 21:11:30 -05005261 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005262 for (i = 0; i < map->num_stripes; i++) {
5263 device = map->stripes[i].dev;
5264 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005265
5266 btrfs_set_stack_stripe_devid(stripe, device->devid);
5267 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5268 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5269 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005270 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005271 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005272
5273 btrfs_set_stack_chunk_length(chunk, chunk_size);
5274 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5275 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5276 btrfs_set_stack_chunk_type(chunk, map->type);
5277 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5278 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5279 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005281 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5282
5283 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5284 key.type = BTRFS_CHUNK_ITEM_KEY;
5285 key.offset = chunk_offset;
5286
5287 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005288 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5289 /*
5290 * TODO: Cleanup of inserted chunk root in case of
5291 * failure.
5292 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005293 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005294 }
liubo1abe9b82011-03-24 11:18:59 +00005295
Josef Bacik6df9a952013-06-27 13:22:46 -04005296out:
Yan Zheng2b820322008-11-17 21:11:30 -05005297 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005298 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005299 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005300}
5301
David Sterba6f8e0fc2019-03-20 16:29:13 +01005302static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005303{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005304 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005305 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005306 int ret;
5307
Jeff Mahoney1b868262017-05-17 11:38:35 -04005308 alloc_profile = btrfs_metadata_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 if (ret)
5311 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005312
Jeff Mahoney1b868262017-05-17 11:38:35 -04005313 alloc_profile = btrfs_system_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005314 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005315 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005316}
5317
Miao Xied20983b2014-07-03 18:22:13 +08005318static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5319{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005320 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005321
David Sterbafc9a2ac2019-05-17 11:43:22 +02005322 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005323}
5324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005325int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005326{
5327 struct extent_map *em;
5328 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005329 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005330 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005331 int i;
5332
Omar Sandoval60ca8422018-05-16 16:34:31 -07005333 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005334 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005335 return 1;
5336
Jeff Mahoney95617d62015-06-03 10:55:48 -04005337 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005338 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005339 if (test_bit(BTRFS_DEV_STATE_MISSING,
5340 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005341 miss_ndevs++;
5342 continue;
5343 }
Anand Jainebbede42017-12-04 12:54:52 +08005344 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5345 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005346 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005347 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005348 }
5349 }
Miao Xied20983b2014-07-03 18:22:13 +08005350
5351 /*
5352 * If the number of missing devices is larger than max errors,
5353 * we can not write the data into that chunk successfully, so
5354 * set it readonly.
5355 */
5356 if (miss_ndevs > btrfs_chunk_max_errors(map))
5357 readonly = 1;
5358end:
Yan Zheng2b820322008-11-17 21:11:30 -05005359 free_extent_map(em);
5360 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005361}
5362
David Sterbac8bf1b62019-05-17 11:43:17 +02005363void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005364{
5365 struct extent_map *em;
5366
Chris Masond3977122009-01-05 21:25:51 -05005367 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005368 write_lock(&tree->lock);
5369 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005370 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005371 remove_extent_mapping(tree, em);
5372 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005373 if (!em)
5374 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005375 /* once for us */
5376 free_extent_map(em);
5377 /* once for the tree */
5378 free_extent_map(em);
5379 }
5380}
5381
Stefan Behrens5d964052012-11-05 14:59:07 +01005382int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005383{
5384 struct extent_map *em;
5385 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005386 int ret;
5387
Omar Sandoval60ca8422018-05-16 16:34:31 -07005388 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005389 if (IS_ERR(em))
5390 /*
5391 * We could return errors for these cases, but that could get
5392 * ugly and we'd probably do the same thing which is just not do
5393 * anything else and exit, so return 1 so the callers don't try
5394 * to use other copies.
5395 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005396 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005397
Jeff Mahoney95617d62015-06-03 10:55:48 -04005398 map = em->map_lookup;
David Sterbac7369b32019-05-31 15:39:31 +02005399 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
Chris Masonf1885912008-04-09 16:28:12 -04005400 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005401 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5402 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005403 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5404 ret = 2;
5405 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005406 /*
5407 * There could be two corrupted data stripes, we need
5408 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005409 *
Liu Bo8810f752018-01-02 13:36:41 -07005410 * Fail a stripe at a time on every retry except the
5411 * stripe under reconstruction.
5412 */
5413 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005414 else
5415 ret = 1;
5416 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005417
David Sterbacb5583d2018-09-07 16:11:23 +02005418 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005419 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5420 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005421 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005422 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005423
Chris Masonf1885912008-04-09 16:28:12 -04005424 return ret;
5425}
5426
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005427unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005428 u64 logical)
5429{
5430 struct extent_map *em;
5431 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005432 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005433
Omar Sandoval60ca8422018-05-16 16:34:31 -07005434 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005435
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005436 if (!WARN_ON(IS_ERR(em))) {
5437 map = em->map_lookup;
5438 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5439 len = map->stripe_len * nr_data_stripes(map);
5440 free_extent_map(em);
5441 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005442 return len;
5443}
5444
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005445int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005446{
5447 struct extent_map *em;
5448 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005449 int ret = 0;
5450
Omar Sandoval60ca8422018-05-16 16:34:31 -07005451 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005452
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005453 if(!WARN_ON(IS_ERR(em))) {
5454 map = em->map_lookup;
5455 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5456 ret = 1;
5457 free_extent_map(em);
5458 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005459 return ret;
5460}
5461
Stefan Behrens30d98612012-11-06 14:52:18 +01005462static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005463 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005464 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005465{
5466 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005467 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005468 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005469 int tolerance;
5470 struct btrfs_device *srcdev;
5471
Anand Jain99f92a72018-03-14 16:29:12 +08005472 ASSERT((map->type &
David Sterbac7369b32019-05-31 15:39:31 +02005473 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
Anand Jain99f92a72018-03-14 16:29:12 +08005474
5475 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5476 num_stripes = map->sub_stripes;
5477 else
5478 num_stripes = map->num_stripes;
5479
Anand Jain8ba0ae72018-03-14 16:29:13 +08005480 preferred_mirror = first + current->pid % num_stripes;
5481
Stefan Behrens30d98612012-11-06 14:52:18 +01005482 if (dev_replace_is_ongoing &&
5483 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5484 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5485 srcdev = fs_info->dev_replace.srcdev;
5486 else
5487 srcdev = NULL;
5488
5489 /*
5490 * try to avoid the drive that is the source drive for a
5491 * dev-replace procedure, only choose it if no other non-missing
5492 * mirror is available
5493 */
5494 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005495 if (map->stripes[preferred_mirror].dev->bdev &&
5496 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5497 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005498 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005499 if (map->stripes[i].dev->bdev &&
5500 (tolerance || map->stripes[i].dev != srcdev))
5501 return i;
5502 }
Chris Masondfe25022008-05-13 13:46:40 -04005503 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005504
Chris Masondfe25022008-05-13 13:46:40 -04005505 /* we couldn't find one that doesn't fail. Just return something
5506 * and the io error handling code will clean up eventually
5507 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005508 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005509}
5510
David Woodhouse53b381b2013-01-29 18:40:14 -05005511/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005512static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005513{
David Woodhouse53b381b2013-01-29 18:40:14 -05005514 int i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005515 int again = 1;
5516
5517 while (again) {
5518 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005519 for (i = 0; i < num_stripes - 1; i++) {
David Sterbaeeb6f172019-11-28 15:31:17 +01005520 /* Swap if parity is on a smaller index */
5521 if (bbio->raid_map[i] > bbio->raid_map[i + 1]) {
5522 swap(bbio->stripes[i], bbio->stripes[i + 1]);
5523 swap(bbio->raid_map[i], bbio->raid_map[i + 1]);
David Woodhouse53b381b2013-01-29 18:40:14 -05005524 again = 1;
5525 }
5526 }
5527 }
5528}
5529
Zhao Lei6e9606d2015-01-20 15:11:34 +08005530static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5531{
5532 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005533 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005534 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005535 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005536 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005537 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005538 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005539 /*
5540 * plus the raid_map, which includes both the tgt dev
5541 * and the stripes
5542 */
5543 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005544 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005545
5546 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005547 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005548
Nikolay Borisov608769a2020-07-02 16:46:41 +03005549 bbio->tgtdev_map = (int *)(bbio->stripes + total_stripes);
5550 bbio->raid_map = (u64 *)(bbio->tgtdev_map + real_stripes);
5551
Zhao Lei6e9606d2015-01-20 15:11:34 +08005552 return bbio;
5553}
5554
5555void btrfs_get_bbio(struct btrfs_bio *bbio)
5556{
Elena Reshetova140475a2017-03-03 10:55:10 +02005557 WARN_ON(!refcount_read(&bbio->refs));
5558 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005559}
5560
5561void btrfs_put_bbio(struct btrfs_bio *bbio)
5562{
5563 if (!bbio)
5564 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005565 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005566 kfree(bbio);
5567}
5568
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005569/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5570/*
5571 * Please note that, discard won't be sent to target device of device
5572 * replace.
5573 */
5574static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005575 u64 logical, u64 *length_ret,
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005576 struct btrfs_bio **bbio_ret)
5577{
5578 struct extent_map *em;
5579 struct map_lookup *map;
5580 struct btrfs_bio *bbio;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005581 u64 length = *length_ret;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005582 u64 offset;
5583 u64 stripe_nr;
5584 u64 stripe_nr_end;
5585 u64 stripe_end_offset;
5586 u64 stripe_cnt;
5587 u64 stripe_len;
5588 u64 stripe_offset;
5589 u64 num_stripes;
5590 u32 stripe_index;
5591 u32 factor = 0;
5592 u32 sub_stripes = 0;
5593 u64 stripes_per_dev = 0;
5594 u32 remaining_stripes = 0;
5595 u32 last_stripe = 0;
5596 int ret = 0;
5597 int i;
5598
5599 /* discard always return a bbio */
5600 ASSERT(bbio_ret);
5601
Omar Sandoval60ca8422018-05-16 16:34:31 -07005602 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005603 if (IS_ERR(em))
5604 return PTR_ERR(em);
5605
5606 map = em->map_lookup;
5607 /* we don't discard raid56 yet */
5608 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5609 ret = -EOPNOTSUPP;
5610 goto out;
5611 }
5612
5613 offset = logical - em->start;
Qu Wenruo2d974612019-10-23 21:57:26 +08005614 length = min_t(u64, em->start + em->len - logical, length);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005615 *length_ret = length;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005616
5617 stripe_len = map->stripe_len;
5618 /*
5619 * stripe_nr counts the total number of stripes we have to stride
5620 * to get to this block
5621 */
5622 stripe_nr = div64_u64(offset, stripe_len);
5623
5624 /* stripe_offset is the offset of this block in its stripe */
5625 stripe_offset = offset - stripe_nr * stripe_len;
5626
5627 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005628 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005629 stripe_cnt = stripe_nr_end - stripe_nr;
5630 stripe_end_offset = stripe_nr_end * map->stripe_len -
5631 (offset + length);
5632 /*
5633 * after this, stripe_nr is the number of stripes on this
5634 * device we have to walk to find the data, and stripe_index is
5635 * the number of our device in the stripe array
5636 */
5637 num_stripes = 1;
5638 stripe_index = 0;
5639 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5640 BTRFS_BLOCK_GROUP_RAID10)) {
5641 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5642 sub_stripes = 1;
5643 else
5644 sub_stripes = map->sub_stripes;
5645
5646 factor = map->num_stripes / sub_stripes;
5647 num_stripes = min_t(u64, map->num_stripes,
5648 sub_stripes * stripe_cnt);
5649 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5650 stripe_index *= sub_stripes;
5651 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5652 &remaining_stripes);
5653 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5654 last_stripe *= sub_stripes;
David Sterbac7369b32019-05-31 15:39:31 +02005655 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005656 BTRFS_BLOCK_GROUP_DUP)) {
5657 num_stripes = map->num_stripes;
5658 } else {
5659 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5660 &stripe_index);
5661 }
5662
5663 bbio = alloc_btrfs_bio(num_stripes, 0);
5664 if (!bbio) {
5665 ret = -ENOMEM;
5666 goto out;
5667 }
5668
5669 for (i = 0; i < num_stripes; i++) {
5670 bbio->stripes[i].physical =
5671 map->stripes[stripe_index].physical +
5672 stripe_offset + stripe_nr * map->stripe_len;
5673 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5674
5675 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5676 BTRFS_BLOCK_GROUP_RAID10)) {
5677 bbio->stripes[i].length = stripes_per_dev *
5678 map->stripe_len;
5679
5680 if (i / sub_stripes < remaining_stripes)
5681 bbio->stripes[i].length +=
5682 map->stripe_len;
5683
5684 /*
5685 * Special for the first stripe and
5686 * the last stripe:
5687 *
5688 * |-------|...|-------|
5689 * |----------|
5690 * off end_off
5691 */
5692 if (i < sub_stripes)
5693 bbio->stripes[i].length -=
5694 stripe_offset;
5695
5696 if (stripe_index >= last_stripe &&
5697 stripe_index <= (last_stripe +
5698 sub_stripes - 1))
5699 bbio->stripes[i].length -=
5700 stripe_end_offset;
5701
5702 if (i == sub_stripes - 1)
5703 stripe_offset = 0;
5704 } else {
5705 bbio->stripes[i].length = length;
5706 }
5707
5708 stripe_index++;
5709 if (stripe_index == map->num_stripes) {
5710 stripe_index = 0;
5711 stripe_nr++;
5712 }
5713 }
5714
5715 *bbio_ret = bbio;
5716 bbio->map_type = map->type;
5717 bbio->num_stripes = num_stripes;
5718out:
5719 free_extent_map(em);
5720 return ret;
5721}
5722
Liu Bo5ab56092017-03-14 13:33:57 -07005723/*
5724 * In dev-replace case, for repair case (that's the only case where the mirror
5725 * is selected explicitly when calling btrfs_map_block), blocks left of the
5726 * left cursor can also be read from the target drive.
5727 *
5728 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5729 * array of stripes.
5730 * For READ, it also needs to be supported using the same mirror number.
5731 *
5732 * If the requested block is not left of the left cursor, EIO is returned. This
5733 * can happen because btrfs_num_copies() returns one more in the dev-replace
5734 * case.
5735 */
5736static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5737 u64 logical, u64 length,
5738 u64 srcdev_devid, int *mirror_num,
5739 u64 *physical)
5740{
5741 struct btrfs_bio *bbio = NULL;
5742 int num_stripes;
5743 int index_srcdev = 0;
5744 int found = 0;
5745 u64 physical_of_found = 0;
5746 int i;
5747 int ret = 0;
5748
5749 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5750 logical, &length, &bbio, 0, 0);
5751 if (ret) {
5752 ASSERT(bbio == NULL);
5753 return ret;
5754 }
5755
5756 num_stripes = bbio->num_stripes;
5757 if (*mirror_num > num_stripes) {
5758 /*
5759 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5760 * that means that the requested area is not left of the left
5761 * cursor
5762 */
5763 btrfs_put_bbio(bbio);
5764 return -EIO;
5765 }
5766
5767 /*
5768 * process the rest of the function using the mirror_num of the source
5769 * drive. Therefore look it up first. At the end, patch the device
5770 * pointer to the one of the target drive.
5771 */
5772 for (i = 0; i < num_stripes; i++) {
5773 if (bbio->stripes[i].dev->devid != srcdev_devid)
5774 continue;
5775
5776 /*
5777 * In case of DUP, in order to keep it simple, only add the
5778 * mirror with the lowest physical address
5779 */
5780 if (found &&
5781 physical_of_found <= bbio->stripes[i].physical)
5782 continue;
5783
5784 index_srcdev = i;
5785 found = 1;
5786 physical_of_found = bbio->stripes[i].physical;
5787 }
5788
5789 btrfs_put_bbio(bbio);
5790
5791 ASSERT(found);
5792 if (!found)
5793 return -EIO;
5794
5795 *mirror_num = index_srcdev + 1;
5796 *physical = physical_of_found;
5797 return ret;
5798}
5799
Liu Bo73c0f222017-03-14 13:33:58 -07005800static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5801 struct btrfs_bio **bbio_ret,
5802 struct btrfs_dev_replace *dev_replace,
5803 int *num_stripes_ret, int *max_errors_ret)
5804{
5805 struct btrfs_bio *bbio = *bbio_ret;
5806 u64 srcdev_devid = dev_replace->srcdev->devid;
5807 int tgtdev_indexes = 0;
5808 int num_stripes = *num_stripes_ret;
5809 int max_errors = *max_errors_ret;
5810 int i;
5811
5812 if (op == BTRFS_MAP_WRITE) {
5813 int index_where_to_add;
5814
5815 /*
5816 * duplicate the write operations while the dev replace
5817 * procedure is running. Since the copying of the old disk to
5818 * the new disk takes place at run time while the filesystem is
5819 * mounted writable, the regular write operations to the old
5820 * disk have to be duplicated to go to the new disk as well.
5821 *
5822 * Note that device->missing is handled by the caller, and that
5823 * the write to the old disk is already set up in the stripes
5824 * array.
5825 */
5826 index_where_to_add = num_stripes;
5827 for (i = 0; i < num_stripes; i++) {
5828 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5829 /* write to new disk, too */
5830 struct btrfs_bio_stripe *new =
5831 bbio->stripes + index_where_to_add;
5832 struct btrfs_bio_stripe *old =
5833 bbio->stripes + i;
5834
5835 new->physical = old->physical;
5836 new->length = old->length;
5837 new->dev = dev_replace->tgtdev;
5838 bbio->tgtdev_map[i] = index_where_to_add;
5839 index_where_to_add++;
5840 max_errors++;
5841 tgtdev_indexes++;
5842 }
5843 }
5844 num_stripes = index_where_to_add;
5845 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5846 int index_srcdev = 0;
5847 int found = 0;
5848 u64 physical_of_found = 0;
5849
5850 /*
5851 * During the dev-replace procedure, the target drive can also
5852 * be used to read data in case it is needed to repair a corrupt
5853 * block elsewhere. This is possible if the requested area is
5854 * left of the left cursor. In this area, the target drive is a
5855 * full copy of the source drive.
5856 */
5857 for (i = 0; i < num_stripes; i++) {
5858 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5859 /*
5860 * In case of DUP, in order to keep it simple,
5861 * only add the mirror with the lowest physical
5862 * address
5863 */
5864 if (found &&
5865 physical_of_found <=
5866 bbio->stripes[i].physical)
5867 continue;
5868 index_srcdev = i;
5869 found = 1;
5870 physical_of_found = bbio->stripes[i].physical;
5871 }
5872 }
5873 if (found) {
5874 struct btrfs_bio_stripe *tgtdev_stripe =
5875 bbio->stripes + num_stripes;
5876
5877 tgtdev_stripe->physical = physical_of_found;
5878 tgtdev_stripe->length =
5879 bbio->stripes[index_srcdev].length;
5880 tgtdev_stripe->dev = dev_replace->tgtdev;
5881 bbio->tgtdev_map[index_srcdev] = num_stripes;
5882
5883 tgtdev_indexes++;
5884 num_stripes++;
5885 }
5886 }
5887
5888 *num_stripes_ret = num_stripes;
5889 *max_errors_ret = max_errors;
5890 bbio->num_tgtdevs = tgtdev_indexes;
5891 *bbio_ret = bbio;
5892}
5893
Liu Bo2b19a1f2017-03-14 13:34:00 -07005894static bool need_full_stripe(enum btrfs_map_op op)
5895{
5896 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5897}
5898
Nikolay Borisov5f141122019-06-03 12:05:03 +03005899/*
5900 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5901 * tuple. This information is used to calculate how big a
5902 * particular bio can get before it straddles a stripe.
5903 *
5904 * @fs_info - the filesystem
5905 * @logical - address that we want to figure out the geometry of
5906 * @len - the length of IO we are going to perform, starting at @logical
5907 * @op - type of operation - write or read
5908 * @io_geom - pointer used to return values
5909 *
5910 * Returns < 0 in case a chunk for the given logical address cannot be found,
5911 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5912 */
5913int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005914 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
Nikolay Borisov5f141122019-06-03 12:05:03 +03005915{
5916 struct extent_map *em;
5917 struct map_lookup *map;
5918 u64 offset;
5919 u64 stripe_offset;
5920 u64 stripe_nr;
5921 u64 stripe_len;
5922 u64 raid56_full_stripe_start = (u64)-1;
5923 int data_stripes;
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005924 int ret = 0;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005925
5926 ASSERT(op != BTRFS_MAP_DISCARD);
5927
5928 em = btrfs_get_chunk_map(fs_info, logical, len);
5929 if (IS_ERR(em))
5930 return PTR_ERR(em);
5931
5932 map = em->map_lookup;
5933 /* Offset of this logical address in the chunk */
5934 offset = logical - em->start;
5935 /* Len of a stripe in a chunk */
5936 stripe_len = map->stripe_len;
5937 /* Stripe wher this block falls in */
5938 stripe_nr = div64_u64(offset, stripe_len);
5939 /* Offset of stripe in the chunk */
5940 stripe_offset = stripe_nr * stripe_len;
5941 if (offset < stripe_offset) {
5942 btrfs_crit(fs_info,
5943"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5944 stripe_offset, offset, em->start, logical, stripe_len);
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005945 ret = -EINVAL;
5946 goto out;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005947 }
5948
5949 /* stripe_offset is the offset of this block in its stripe */
5950 stripe_offset = offset - stripe_offset;
5951 data_stripes = nr_data_stripes(map);
5952
5953 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5954 u64 max_len = stripe_len - stripe_offset;
5955
5956 /*
5957 * In case of raid56, we need to know the stripe aligned start
5958 */
5959 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5960 unsigned long full_stripe_len = stripe_len * data_stripes;
5961 raid56_full_stripe_start = offset;
5962
5963 /*
5964 * Allow a write of a full stripe, but make sure we
5965 * don't allow straddling of stripes
5966 */
5967 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5968 full_stripe_len);
5969 raid56_full_stripe_start *= full_stripe_len;
5970
5971 /*
5972 * For writes to RAID[56], allow a full stripeset across
5973 * all disks. For other RAID types and for RAID[56]
5974 * reads, just allow a single stripe (on a single disk).
5975 */
5976 if (op == BTRFS_MAP_WRITE) {
5977 max_len = stripe_len * data_stripes -
5978 (offset - raid56_full_stripe_start);
5979 }
5980 }
5981 len = min_t(u64, em->len - offset, max_len);
5982 } else {
5983 len = em->len - offset;
5984 }
5985
5986 io_geom->len = len;
5987 io_geom->offset = offset;
5988 io_geom->stripe_len = stripe_len;
5989 io_geom->stripe_nr = stripe_nr;
5990 io_geom->stripe_offset = stripe_offset;
5991 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
5992
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005993out:
5994 /* once for us */
5995 free_extent_map(em);
5996 return ret;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005997}
5998
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005999static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
6000 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006001 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006002 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006003 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04006004{
6005 struct extent_map *em;
6006 struct map_lookup *map;
Chris Mason593060d2008-03-25 16:50:33 -04006007 u64 stripe_offset;
6008 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006009 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006010 u32 stripe_index;
David Sterbacff82672019-05-17 11:43:45 +02006011 int data_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04006012 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006013 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006014 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006015 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006016 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006017 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006018 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6019 int dev_replace_is_ongoing = 0;
6020 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006021 int patch_the_first_stripe_for_dev_replace = 0;
6022 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006023 u64 raid56_full_stripe_start = (u64)-1;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006024 struct btrfs_io_geometry geom;
6025
6026 ASSERT(bbio_ret);
David Sterba75fb2e92018-08-03 00:36:29 +02006027 ASSERT(op != BTRFS_MAP_DISCARD);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006028
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006029 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
6030 if (ret < 0)
6031 return ret;
6032
Omar Sandoval60ca8422018-05-16 16:34:31 -07006033 em = btrfs_get_chunk_map(fs_info, logical, *length);
Dan Carpenterf1136982019-08-09 17:07:39 +03006034 ASSERT(!IS_ERR(em));
Jeff Mahoney95617d62015-06-03 10:55:48 -04006035 map = em->map_lookup;
Chris Mason593060d2008-03-25 16:50:33 -04006036
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006037 *length = geom.len;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006038 stripe_len = geom.stripe_len;
6039 stripe_nr = geom.stripe_nr;
6040 stripe_offset = geom.stripe_offset;
6041 raid56_full_stripe_start = geom.raid56_stripe_offset;
David Sterbacff82672019-05-17 11:43:45 +02006042 data_stripes = nr_data_stripes(map);
Chris Mason593060d2008-03-25 16:50:33 -04006043
David Sterbacb5583d2018-09-07 16:11:23 +02006044 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006045 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006046 /*
6047 * Hold the semaphore for read during the whole operation, write is
6048 * requested at commit time but must wait.
6049 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006050 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006051 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006052
Stefan Behrensad6d6202012-11-06 15:06:47 +01006053 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006054 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006055 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6056 dev_replace->srcdev->devid,
6057 &mirror_num,
6058 &physical_to_patch_in_first_stripe);
6059 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006060 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006061 else
6062 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006063 } else if (mirror_num > map->num_stripes) {
6064 mirror_num = 0;
6065 }
6066
Chris Masonf2d8d742008-04-21 10:03:05 -04006067 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006068 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006069 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006070 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6071 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006072 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006073 mirror_num = 1;
David Sterbac7369b32019-05-31 15:39:31 +02006074 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006075 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006076 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006077 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006078 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006079 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006080 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006081 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006082 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006083 }
Chris Mason2fff7342008-04-29 14:12:09 -04006084
Chris Mason611f0e02008-04-03 16:29:03 -04006085 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006086 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006087 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006088 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006089 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006090 } else {
6091 mirror_num = 1;
6092 }
Chris Mason2fff7342008-04-29 14:12:09 -04006093
Chris Mason321aecc2008-04-16 10:49:51 -04006094 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006095 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006096
David Sterba47c57132015-02-20 18:43:47 +01006097 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006098 stripe_index *= map->sub_stripes;
6099
Anand Jainde483732017-10-12 16:43:00 +08006100 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006101 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006102 else if (mirror_num)
6103 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006104 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006105 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006106 stripe_index = find_live_mirror(fs_info, map,
6107 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006108 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006109 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006110 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006111
Zhao Leiffe2d202015-01-20 15:11:44 +08006112 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006113 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006114 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006115 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbacff82672019-05-17 11:43:45 +02006116 stripe_len * data_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05006117
6118 /* RAID[56] write or recovery. Return all stripes */
6119 num_stripes = map->num_stripes;
6120 max_errors = nr_parity_stripes(map);
6121
David Woodhouse53b381b2013-01-29 18:40:14 -05006122 *length = map->stripe_len;
6123 stripe_index = 0;
6124 stripe_offset = 0;
6125 } else {
6126 /*
6127 * Mirror #0 or #1 means the original data block.
6128 * Mirror #2 is RAID5 parity block.
6129 * Mirror #3 is RAID6 Q block.
6130 */
David Sterba47c57132015-02-20 18:43:47 +01006131 stripe_nr = div_u64_rem(stripe_nr,
David Sterbacff82672019-05-17 11:43:45 +02006132 data_stripes, &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006133 if (mirror_num > 1)
David Sterbacff82672019-05-17 11:43:45 +02006134 stripe_index = data_stripes + mirror_num - 2;
David Woodhouse53b381b2013-01-29 18:40:14 -05006135
6136 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006137 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6138 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006139 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006140 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006141 }
Chris Mason8790d502008-04-03 16:29:03 -04006142 } else {
6143 /*
David Sterba47c57132015-02-20 18:43:47 +01006144 * after this, stripe_nr is the number of stripes on this
6145 * device we have to walk to find the data, and stripe_index is
6146 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006147 */
David Sterba47c57132015-02-20 18:43:47 +01006148 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6149 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006150 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006151 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006152 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006153 btrfs_crit(fs_info,
6154 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006155 stripe_index, map->num_stripes);
6156 ret = -EINVAL;
6157 goto out;
6158 }
Chris Mason593060d2008-03-25 16:50:33 -04006159
Stefan Behrens472262f2012-11-06 14:43:46 +01006160 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006161 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006162 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006163 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006164 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006165 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006166 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006167 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006168
Zhao Lei6e9606d2015-01-20 15:11:34 +08006169 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006170 if (!bbio) {
6171 ret = -ENOMEM;
6172 goto out;
6173 }
Nikolay Borisov608769a2020-07-02 16:46:41 +03006174
6175 for (i = 0; i < num_stripes; i++) {
6176 bbio->stripes[i].physical = map->stripes[stripe_index].physical +
6177 stripe_offset + stripe_nr * map->stripe_len;
6178 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
6179 stripe_index++;
6180 }
Li Zefande11cc12011-12-01 12:55:47 +08006181
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006182 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006183 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6184 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006185 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006186 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006187
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006188 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006189 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006190
6191 /* Fill in the logical address of each stripe */
David Sterbacff82672019-05-17 11:43:45 +02006192 tmp = stripe_nr * data_stripes;
6193 for (i = 0; i < data_stripes; i++)
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006194 bbio->raid_map[(i+rot) % num_stripes] =
6195 em->start + (tmp + i) * map->stripe_len;
6196
6197 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6198 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6199 bbio->raid_map[(i+rot+1) % num_stripes] =
6200 RAID6_Q_STRIPE;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006201
Nikolay Borisov608769a2020-07-02 16:46:41 +03006202 sort_parity_stripes(bbio, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006203 }
Li Zefande11cc12011-12-01 12:55:47 +08006204
Liu Bo2b19a1f2017-03-14 13:34:00 -07006205 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006206 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006207
Liu Bo73c0f222017-03-14 13:33:58 -07006208 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006209 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006210 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6211 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006212 }
6213
Li Zefande11cc12011-12-01 12:55:47 +08006214 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006215 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006216 bbio->num_stripes = num_stripes;
6217 bbio->max_errors = max_errors;
6218 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006219
6220 /*
6221 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6222 * mirror_num == num_stripes + 1 && dev_replace target drive is
6223 * available as a mirror
6224 */
6225 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6226 WARN_ON(num_stripes > 1);
6227 bbio->stripes[0].dev = dev_replace->tgtdev;
6228 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6229 bbio->mirror_num = map->num_stripes + 1;
6230 }
Chris Masoncea9e442008-04-09 16:28:12 -04006231out:
Liu Bo73beece2015-07-17 16:49:19 +08006232 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006233 lockdep_assert_held(&dev_replace->rwsem);
6234 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006235 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006236 }
Chris Mason0b86a832008-03-24 15:01:56 -04006237 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006238 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006239}
6240
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006241int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006242 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006243 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006244{
David Sterba75fb2e92018-08-03 00:36:29 +02006245 if (op == BTRFS_MAP_DISCARD)
6246 return __btrfs_map_block_for_discard(fs_info, logical,
6247 length, bbio_ret);
6248
Mike Christieb3d3fa52016-06-05 14:31:53 -05006249 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006250 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006251}
6252
Miao Xieaf8e2d12014-10-23 14:42:50 +08006253/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006254int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006255 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006256 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006257{
David Sterba825ad4c2017-03-28 14:45:22 +02006258 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006259}
6260
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006261static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006262{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006263 bio->bi_private = bbio->private;
6264 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006265 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006266
Zhao Lei6e9606d2015-01-20 15:11:34 +08006267 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006268}
6269
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006270static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006271{
Chris Mason9be33952013-05-17 18:30:14 -04006272 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006273 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006274
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006275 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006276 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006277 if (bio->bi_status == BLK_STS_IOERR ||
6278 bio->bi_status == BLK_STS_TARGET) {
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006279 struct btrfs_device *dev = btrfs_io_bio(bio)->device;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006280
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006281 ASSERT(dev->bdev);
6282 if (bio_op(bio) == REQ_OP_WRITE)
6283 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006284 BTRFS_DEV_STAT_WRITE_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006285 else if (!(bio->bi_opf & REQ_RAHEAD))
6286 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006287 BTRFS_DEV_STAT_READ_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006288 if (bio->bi_opf & REQ_PREFLUSH)
6289 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006290 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens442a4f62012-05-25 16:06:08 +02006291 }
6292 }
Chris Mason8790d502008-04-03 16:29:03 -04006293
Jan Schmidta1d3c472011-08-04 17:15:33 +02006294 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006295 is_orig_bio = 1;
6296
Miao Xiec404e0d2014-01-30 16:46:55 +08006297 btrfs_bio_counter_dec(bbio->fs_info);
6298
Jan Schmidta1d3c472011-08-04 17:15:33 +02006299 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006300 if (!is_orig_bio) {
6301 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006302 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006303 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006304
Chris Mason9be33952013-05-17 18:30:14 -04006305 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006306 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006307 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006308 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006309 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006310 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006311 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006312 /*
6313 * this bio is actually up to date, we didn't
6314 * go over the max number of errors
6315 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006316 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006317 }
Miao Xiec55f1392014-06-19 10:42:54 +08006318
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006319 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006320 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006321 bio_put(bio);
6322 }
Chris Mason8790d502008-04-03 16:29:03 -04006323}
6324
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006325static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006326 u64 physical, struct btrfs_device *dev)
Josef Bacikde1ee922012-10-19 16:50:56 -04006327{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006328 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006329
6330 bio->bi_private = bbio;
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006331 btrfs_io_bio(bio)->device = dev;
Josef Bacikde1ee922012-10-19 16:50:56 -04006332 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006333 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006334 btrfs_debug_in_rcu(fs_info,
6335 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6336 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
David Sterba1db45a32019-11-28 15:31:46 +01006337 (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name),
6338 dev->devid, bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006339 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006341 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006342
Chris Mason08635ba2019-07-10 12:28:14 -07006343 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006344}
6345
Josef Bacikde1ee922012-10-19 16:50:56 -04006346static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6347{
6348 atomic_inc(&bbio->error);
6349 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006350 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006351 WARN_ON(bio != bbio->orig_bio);
6352
Chris Mason9be33952013-05-17 18:30:14 -04006353 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006354 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006355 if (atomic_read(&bbio->error) > bbio->max_errors)
6356 bio->bi_status = BLK_STS_IOERR;
6357 else
6358 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006359 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006360 }
6361}
6362
Omar Sandoval58efbc92017-08-22 23:45:59 -07006363blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Chris Mason08635ba2019-07-10 12:28:14 -07006364 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04006365{
Chris Mason0b86a832008-03-24 15:01:56 -04006366 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006367 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006368 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006369 u64 length = 0;
6370 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006371 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006372 int dev_nr;
6373 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006374 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006375
Kent Overstreet4f024f32013-10-11 15:44:27 -07006376 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006377 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006378
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006379 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006380 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006381 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006382 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006383 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006384 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006385 }
Chris Masoncea9e442008-04-09 16:28:12 -04006386
Jan Schmidta1d3c472011-08-04 17:15:33 +02006387 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006388 bbio->orig_bio = first_bio;
6389 bbio->private = first_bio->bi_private;
6390 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006391 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006392 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6393
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006394 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006395 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006396 /* In this case, map_length has been set to the length of
6397 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006398 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006399 ret = raid56_parity_write(fs_info, bio, bbio,
6400 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006401 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006402 ret = raid56_parity_recover(fs_info, bio, bbio,
6403 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006404 }
Miao Xie42452152014-11-25 16:39:28 +08006405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006406 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006407 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006408 }
6409
Chris Masoncea9e442008-04-09 16:28:12 -04006410 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006411 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006412 "mapping failed logical %llu bio len %llu len %llu",
6413 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006414 BUG();
6415 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006416
Zhao Lei08da7572015-02-12 15:42:16 +08006417 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006418 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006419 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6420 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006421 (bio_op(first_bio) == REQ_OP_WRITE &&
6422 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006423 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006424 continue;
6425 }
6426
David Sterba3aa8e072017-06-02 17:38:30 +02006427 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006428 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006429 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006430 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006431
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006432 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, dev);
Chris Mason239b14b2008-03-24 15:02:07 -04006433 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006434 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006435 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006436}
6437
Anand Jain09ba3bc2019-01-19 14:48:55 +08006438/*
6439 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6440 * return NULL.
6441 *
6442 * If devid and uuid are both specified, the match must be exact, otherwise
6443 * only devid is used.
6444 *
6445 * If @seed is true, traverse through the seed devices.
6446 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006447struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006448 u64 devid, u8 *uuid, u8 *fsid,
6449 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006450{
Yan Zheng2b820322008-11-17 21:11:30 -05006451 struct btrfs_device *device;
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006452 struct btrfs_fs_devices *seed_devs;
Chris Mason0b86a832008-03-24 15:01:56 -04006453
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006454 if (!fsid || !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6455 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6456 if (device->devid == devid &&
6457 (!uuid || memcmp(device->uuid, uuid,
6458 BTRFS_UUID_SIZE) == 0))
6459 return device;
6460 }
6461 }
6462
6463 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
Yan Zheng2b820322008-11-17 21:11:30 -05006464 if (!fsid ||
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006465 !memcmp(seed_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
6466 list_for_each_entry(device, &seed_devs->devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006467 dev_list) {
6468 if (device->devid == devid &&
6469 (!uuid || memcmp(device->uuid, uuid,
6470 BTRFS_UUID_SIZE) == 0))
6471 return device;
6472 }
Yan Zheng2b820322008-11-17 21:11:30 -05006473 }
Yan Zheng2b820322008-11-17 21:11:30 -05006474 }
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006475
Yan Zheng2b820322008-11-17 21:11:30 -05006476 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006477}
6478
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006479static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006480 u64 devid, u8 *dev_uuid)
6481{
6482 struct btrfs_device *device;
Josef Bacikfccc0002020-08-31 10:52:42 -04006483 unsigned int nofs_flag;
Chris Masondfe25022008-05-13 13:46:40 -04006484
Josef Bacikfccc0002020-08-31 10:52:42 -04006485 /*
6486 * We call this under the chunk_mutex, so we want to use NOFS for this
6487 * allocation, however we don't want to change btrfs_alloc_device() to
6488 * always do NOFS because we use it in a lot of other GFP_KERNEL safe
6489 * places.
6490 */
6491 nofs_flag = memalloc_nofs_save();
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006492 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
Josef Bacikfccc0002020-08-31 10:52:42 -04006493 memalloc_nofs_restore(nofs_flag);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006494 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006495 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006496
6497 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006498 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006499 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006500
Anand Jaine6e674b2017-12-04 12:54:54 +08006501 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006502 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006503
Chris Masondfe25022008-05-13 13:46:40 -04006504 return device;
6505}
6506
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006507/**
6508 * btrfs_alloc_device - allocate struct btrfs_device
6509 * @fs_info: used only for generating a new devid, can be NULL if
6510 * devid is provided (i.e. @devid != NULL).
6511 * @devid: a pointer to devid for this device. If NULL a new devid
6512 * is generated.
6513 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6514 * is generated.
6515 *
6516 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006517 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006518 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006519 */
6520struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6521 const u64 *devid,
6522 const u8 *uuid)
6523{
6524 struct btrfs_device *dev;
6525 u64 tmp;
6526
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306527 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006528 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006529
Qu Wenruo154f7cb2020-08-20 15:42:46 +08006530 dev = __alloc_device(fs_info);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006531 if (IS_ERR(dev))
6532 return dev;
6533
6534 if (devid)
6535 tmp = *devid;
6536 else {
6537 int ret;
6538
6539 ret = find_next_devid(fs_info, &tmp);
6540 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006541 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006542 return ERR_PTR(ret);
6543 }
6544 }
6545 dev->devid = tmp;
6546
6547 if (uuid)
6548 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6549 else
6550 generate_random_uuid(dev->uuid);
6551
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006552 return dev;
6553}
6554
Anand Jain5a2b8e62017-10-09 11:07:45 +08006555static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006556 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006557{
Anand Jain2b902df2017-10-09 11:07:46 +08006558 if (error)
6559 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6560 devid, uuid);
6561 else
6562 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6563 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006564}
6565
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006566static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6567{
6568 int index = btrfs_bg_flags_to_raid_index(type);
6569 int ncopies = btrfs_raid_array[index].ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006570 const int nparity = btrfs_raid_array[index].nparity;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006571 int data_stripes;
6572
David Sterbae4f6c6b2019-05-14 01:59:54 +02006573 if (nparity)
6574 data_stripes = num_stripes - nparity;
6575 else
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006576 data_stripes = num_stripes / ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006577
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006578 return div_u64(chunk_len, data_stripes);
6579}
6580
David Sterba9690ac02019-03-20 16:43:07 +01006581static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006582 struct btrfs_chunk *chunk)
6583{
David Sterba9690ac02019-03-20 16:43:07 +01006584 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006585 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006586 struct map_lookup *map;
6587 struct extent_map *em;
6588 u64 logical;
6589 u64 length;
6590 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006591 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006592 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006593 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006594 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006595
Chris Masone17cade2008-04-15 15:41:47 -04006596 logical = key->offset;
6597 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006598 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006599
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006600 /*
6601 * Only need to verify chunk item if we're reading from sys chunk array,
6602 * as chunk item in tree block is already verified by tree-checker.
6603 */
6604 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006605 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006606 if (ret)
6607 return ret;
6608 }
Chris Masona061fc82008-05-07 11:43:44 -04006609
David Sterbac8bf1b62019-05-17 11:43:17 +02006610 read_lock(&map_tree->lock);
6611 em = lookup_extent_mapping(map_tree, logical, 1);
6612 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006613
6614 /* already mapped? */
6615 if (em && em->start <= logical && em->start + em->len > logical) {
6616 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006617 return 0;
6618 } else if (em) {
6619 free_extent_map(em);
6620 }
Chris Mason0b86a832008-03-24 15:01:56 -04006621
David Sterba172ddd62011-04-21 00:48:27 +02006622 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006623 if (!em)
6624 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006625 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006626 if (!map) {
6627 free_extent_map(em);
6628 return -ENOMEM;
6629 }
6630
Wang Shilong298a8f92014-06-19 10:42:52 +08006631 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006632 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006633 em->start = logical;
6634 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006635 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006636 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006637 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006638
Chris Mason593060d2008-03-25 16:50:33 -04006639 map->num_stripes = num_stripes;
6640 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6641 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006642 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6643 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006644 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006645 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006646 em->orig_block_len = calc_stripe_length(map->type, em->len,
6647 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006648 for (i = 0; i < num_stripes; i++) {
6649 map->stripes[i].physical =
6650 btrfs_stripe_offset_nr(leaf, chunk, i);
6651 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006652 read_extent_buffer(leaf, uuid, (unsigned long)
6653 btrfs_stripe_dev_uuid_nr(chunk, i),
6654 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006655 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006656 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006657 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006658 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006659 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006660 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006661 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006662 }
Chris Masondfe25022008-05-13 13:46:40 -04006663 if (!map->stripes[i].dev) {
6664 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006665 add_missing_dev(fs_info->fs_devices, devid,
6666 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006667 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006668 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006669 btrfs_err(fs_info,
6670 "failed to init missing dev %llu: %ld",
6671 devid, PTR_ERR(map->stripes[i].dev));
6672 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006673 }
Anand Jain2b902df2017-10-09 11:07:46 +08006674 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006675 }
Anand Jaine12c9622017-12-04 12:54:53 +08006676 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6677 &(map->stripes[i].dev->dev_state));
6678
Chris Mason0b86a832008-03-24 15:01:56 -04006679 }
6680
David Sterbac8bf1b62019-05-17 11:43:17 +02006681 write_lock(&map_tree->lock);
6682 ret = add_extent_mapping(map_tree, em, 0);
6683 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006684 if (ret < 0) {
6685 btrfs_err(fs_info,
6686 "failed to add chunk map, start=%llu len=%llu: %d",
6687 em->start, em->len, ret);
6688 }
Chris Mason0b86a832008-03-24 15:01:56 -04006689 free_extent_map(em);
6690
Qu Wenruo64f64f42018-08-01 10:37:20 +08006691 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006692}
6693
Jeff Mahoney143bede2012-03-01 14:56:26 +01006694static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006695 struct btrfs_dev_item *dev_item,
6696 struct btrfs_device *device)
6697{
6698 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006699
6700 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006701 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6702 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006703 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006704 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006705 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006706 device->type = btrfs_device_type(leaf, dev_item);
6707 device->io_align = btrfs_device_io_align(leaf, dev_item);
6708 device->io_width = btrfs_device_io_width(leaf, dev_item);
6709 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006710 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006711 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006712
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006713 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006714 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006715}
6716
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006717static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006718 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006719{
6720 struct btrfs_fs_devices *fs_devices;
6721 int ret;
6722
David Sterbaa32bf9a2018-03-16 02:21:22 +01006723 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006724 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006725
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03006726 /* This will match only for multi-device seed fs */
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006727 list_for_each_entry(fs_devices, &fs_info->fs_devices->seed_list, seed_list)
Anand Jain44880fd2017-07-29 17:50:09 +08006728 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006729 return fs_devices;
6730
Yan Zheng2b820322008-11-17 21:11:30 -05006731
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006732 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006733 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006734 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006735 return ERR_PTR(-ENOENT);
6736
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006737 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006738 if (IS_ERR(fs_devices))
6739 return fs_devices;
6740
Johannes Thumshirn0395d842019-11-13 11:27:27 +01006741 fs_devices->seeding = true;
Miao Xie5f375832014-09-03 21:35:46 +08006742 fs_devices->opened = 1;
6743 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006744 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006745
Nikolay Borisov427c8fd2020-08-12 17:04:36 +03006746 /*
6747 * Upon first call for a seed fs fsid, just create a private copy of the
6748 * respective fs_devices and anchor it at fs_info->fs_devices->seed_list
6749 */
Yan Zhenge4404d62008-12-12 10:03:26 -05006750 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006751 if (IS_ERR(fs_devices))
6752 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006753
Anand Jain897fb572018-04-12 10:29:28 +08006754 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006755 if (ret) {
6756 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006757 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006758 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006759 }
Yan Zheng2b820322008-11-17 21:11:30 -05006760
6761 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006762 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006763 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006764 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006765 goto out;
6766 }
6767
Nikolay Borisov944d3f92020-07-16 10:25:33 +03006768 list_add(&fs_devices->seed_list, &fs_info->fs_devices->seed_list);
Yan Zheng2b820322008-11-17 21:11:30 -05006769out:
Miao Xie5f375832014-09-03 21:35:46 +08006770 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006771}
6772
David Sterba17850752019-03-20 16:45:15 +01006773static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006774 struct btrfs_dev_item *dev_item)
6775{
David Sterba17850752019-03-20 16:45:15 +01006776 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006777 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006778 struct btrfs_device *device;
6779 u64 devid;
6780 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006781 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006782 u8 dev_uuid[BTRFS_UUID_SIZE];
6783
Chris Mason0b86a832008-03-24 15:01:56 -04006784 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006785 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006786 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006787 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006788 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006789
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006790 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006791 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006792 if (IS_ERR(fs_devices))
6793 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006794 }
6795
Anand Jaine4319cd2019-01-17 23:32:31 +08006796 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006797 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006798 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006799 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006800 btrfs_report_missing_device(fs_info, devid,
6801 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006802 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006803 }
Yan Zheng2b820322008-11-17 21:11:30 -05006804
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006805 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006806 if (IS_ERR(device)) {
6807 btrfs_err(fs_info,
6808 "failed to add missing dev %llu: %ld",
6809 devid, PTR_ERR(device));
6810 return PTR_ERR(device);
6811 }
Anand Jain2b902df2017-10-09 11:07:46 +08006812 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006813 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006814 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006815 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6816 btrfs_report_missing_device(fs_info,
6817 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006818 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006819 }
6820 btrfs_report_missing_device(fs_info, devid,
6821 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006822 }
Miao Xie5f375832014-09-03 21:35:46 +08006823
Anand Jaine6e674b2017-12-04 12:54:54 +08006824 if (!device->bdev &&
6825 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006826 /*
6827 * this happens when a device that was properly setup
6828 * in the device info lists suddenly goes bad.
6829 * device->bdev is NULL, and so we have to set
6830 * device->missing to one here
6831 */
Miao Xie5f375832014-09-03 21:35:46 +08006832 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006833 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006834 }
Miao Xie5f375832014-09-03 21:35:46 +08006835
6836 /* Move the device to its own fs_devices */
6837 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006838 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6839 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006840
6841 list_move(&device->dev_list, &fs_devices->devices);
6842 device->fs_devices->num_devices--;
6843 fs_devices->num_devices++;
6844
6845 device->fs_devices->missing_devices--;
6846 fs_devices->missing_devices++;
6847
6848 device->fs_devices = fs_devices;
6849 }
Yan Zheng2b820322008-11-17 21:11:30 -05006850 }
6851
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006852 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006853 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006854 if (device->generation !=
6855 btrfs_device_generation(leaf, dev_item))
6856 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006857 }
Chris Mason0b86a832008-03-24 15:01:56 -04006858
6859 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006860 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006861 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006862 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006863 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006864 atomic64_add(device->total_bytes - device->bytes_used,
6865 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006866 }
Chris Mason0b86a832008-03-24 15:01:56 -04006867 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006868 return ret;
6869}
6870
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006871int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006872{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006873 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006874 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006875 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006876 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006877 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006878 u8 *array_ptr;
6879 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006880 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006881 u32 num_stripes;
6882 u32 array_size;
6883 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006884 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006885 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006886 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006887
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006888 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006889 /*
6890 * This will create extent buffer of nodesize, superblock size is
6891 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6892 * overallocate but we can keep it as-is, only the first page is used.
6893 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006894 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006895 if (IS_ERR(sb))
6896 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006897 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006898 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006899 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006900 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006901 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006902 * pages up-to-date when the page is larger: extent does not cover the
6903 * whole page and consequently check_page_uptodate does not find all
6904 * the page's extents up-to-date (the hole beyond sb),
6905 * write_extent_buffer then triggers a WARN_ON.
6906 *
6907 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6908 * but sb spans only this function. Add an explicit SetPageUptodate call
6909 * to silence the warning eg. on PowerPC 64.
6910 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006911 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006912 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006913
Chris Masona061fc82008-05-07 11:43:44 -04006914 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006915 array_size = btrfs_super_sys_array_size(super_copy);
6916
David Sterba1ffb22c2014-10-31 19:02:42 +01006917 array_ptr = super_copy->sys_chunk_array;
6918 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6919 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006920
David Sterba1ffb22c2014-10-31 19:02:42 +01006921 while (cur_offset < array_size) {
6922 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006923 len = sizeof(*disk_key);
6924 if (cur_offset + len > array_size)
6925 goto out_short_read;
6926
Chris Mason0b86a832008-03-24 15:01:56 -04006927 btrfs_disk_key_to_cpu(&key, disk_key);
6928
David Sterba1ffb22c2014-10-31 19:02:42 +01006929 array_ptr += len;
6930 sb_array_offset += len;
6931 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006932
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006933 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006934 btrfs_err(fs_info,
6935 "unexpected item type %u in sys_array at offset %u",
6936 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006937 ret = -EIO;
6938 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006939 }
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006940
6941 chunk = (struct btrfs_chunk *)sb_array_offset;
6942 /*
6943 * At least one btrfs_chunk with one stripe must be present,
6944 * exact stripe count check comes afterwards
6945 */
6946 len = btrfs_chunk_item_size(1);
6947 if (cur_offset + len > array_size)
6948 goto out_short_read;
6949
6950 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6951 if (!num_stripes) {
6952 btrfs_err(fs_info,
6953 "invalid number of stripes %u in sys_array at offset %u",
6954 num_stripes, cur_offset);
6955 ret = -EIO;
6956 break;
6957 }
6958
6959 type = btrfs_chunk_type(sb, chunk);
6960 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
6961 btrfs_err(fs_info,
6962 "invalid chunk type %llu in sys_array at offset %u",
6963 type, cur_offset);
6964 ret = -EIO;
6965 break;
6966 }
6967
6968 len = btrfs_chunk_item_size(num_stripes);
6969 if (cur_offset + len > array_size)
6970 goto out_short_read;
6971
6972 ret = read_one_chunk(&key, sb, chunk);
6973 if (ret)
6974 break;
6975
David Sterba1ffb22c2014-10-31 19:02:42 +01006976 array_ptr += len;
6977 sb_array_offset += len;
6978 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006979 }
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);
Chris Mason84eed902008-04-25 09:04:37 -04006982 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006983
6984out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006985 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006986 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006987 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006988 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006989 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006990}
6991
Qu Wenruo21634a12017-03-09 09:34:36 +08006992/*
6993 * Check if all chunks in the fs are OK for read-write degraded mount
6994 *
Anand Jain6528b992017-12-18 17:08:59 +08006995 * If the @failing_dev is specified, it's accounted as missing.
6996 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006997 * Return true if all chunks meet the minimal RW mount requirements.
6998 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6999 */
Anand Jain6528b992017-12-18 17:08:59 +08007000bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7001 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007002{
David Sterbac8bf1b62019-05-17 11:43:17 +02007003 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08007004 struct extent_map *em;
7005 u64 next_start = 0;
7006 bool ret = true;
7007
David Sterbac8bf1b62019-05-17 11:43:17 +02007008 read_lock(&map_tree->lock);
7009 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7010 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007011 /* No chunk at all? Return false anyway */
7012 if (!em) {
7013 ret = false;
7014 goto out;
7015 }
7016 while (em) {
7017 struct map_lookup *map;
7018 int missing = 0;
7019 int max_tolerated;
7020 int i;
7021
7022 map = em->map_lookup;
7023 max_tolerated =
7024 btrfs_get_num_tolerated_disk_barrier_failures(
7025 map->type);
7026 for (i = 0; i < map->num_stripes; i++) {
7027 struct btrfs_device *dev = map->stripes[i].dev;
7028
Anand Jaine6e674b2017-12-04 12:54:54 +08007029 if (!dev || !dev->bdev ||
7030 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007031 dev->last_flush_error)
7032 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007033 else if (failing_dev && failing_dev == dev)
7034 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007035 }
7036 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007037 if (!failing_dev)
7038 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007039 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007040 em->start, missing, max_tolerated);
7041 free_extent_map(em);
7042 ret = false;
7043 goto out;
7044 }
7045 next_start = extent_map_end(em);
7046 free_extent_map(em);
7047
David Sterbac8bf1b62019-05-17 11:43:17 +02007048 read_lock(&map_tree->lock);
7049 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08007050 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02007051 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007052 }
7053out:
7054 return ret;
7055}
7056
David Sterbad85327b12020-07-08 22:55:14 +02007057static void readahead_tree_node_children(struct extent_buffer *node)
7058{
7059 int i;
7060 const int nr_items = btrfs_header_nritems(node);
7061
7062 for (i = 0; i < nr_items; i++) {
7063 u64 start;
7064
7065 start = btrfs_node_blockptr(node, i);
7066 readahead_tree_block(node->fs_info, start);
7067 }
7068}
7069
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007070int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007071{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007072 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007073 struct btrfs_path *path;
7074 struct extent_buffer *leaf;
7075 struct btrfs_key key;
7076 struct btrfs_key found_key;
7077 int ret;
7078 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007079 u64 total_dev = 0;
David Sterbad85327b12020-07-08 22:55:14 +02007080 u64 last_ra_node = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007081
Chris Mason0b86a832008-03-24 15:01:56 -04007082 path = btrfs_alloc_path();
7083 if (!path)
7084 return -ENOMEM;
7085
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007086 /*
7087 * uuid_mutex is needed only if we are mounting a sprout FS
7088 * otherwise we don't need it.
7089 */
Li Zefanb367e472011-12-07 11:38:24 +08007090 mutex_lock(&uuid_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007091
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007092 /*
Boris Burkov48cfa612020-07-16 13:29:46 -07007093 * It is possible for mount and umount to race in such a way that
7094 * we execute this code path, but open_fs_devices failed to clear
7095 * total_rw_bytes. We certainly want it cleared before reading the
7096 * device items, so clear it here.
7097 */
7098 fs_info->fs_devices->total_rw_bytes = 0;
7099
7100 /*
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007101 * Read all device items, and then all the chunk items. All
7102 * device items are found before any chunk item (their object id
7103 * is smaller than the lowest possible object id for a chunk
7104 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007105 */
7106 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7107 key.offset = 0;
7108 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007109 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007110 if (ret < 0)
7111 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007112 while (1) {
David Sterbad85327b12020-07-08 22:55:14 +02007113 struct extent_buffer *node;
7114
Chris Mason0b86a832008-03-24 15:01:56 -04007115 leaf = path->nodes[0];
7116 slot = path->slots[0];
7117 if (slot >= btrfs_header_nritems(leaf)) {
7118 ret = btrfs_next_leaf(root, path);
7119 if (ret == 0)
7120 continue;
7121 if (ret < 0)
7122 goto error;
7123 break;
7124 }
David Sterbad85327b12020-07-08 22:55:14 +02007125 /*
7126 * The nodes on level 1 are not locked but we don't need to do
7127 * that during mount time as nothing else can access the tree
7128 */
7129 node = path->nodes[1];
7130 if (node) {
7131 if (last_ra_node != node->start) {
7132 readahead_tree_node_children(node);
7133 last_ra_node = node->start;
7134 }
7135 }
Chris Mason0b86a832008-03-24 15:01:56 -04007136 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007137 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7138 struct btrfs_dev_item *dev_item;
7139 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007140 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01007141 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007142 if (ret)
7143 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007144 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007145 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7146 struct btrfs_chunk *chunk;
7147 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007148 mutex_lock(&fs_info->chunk_mutex);
David Sterba9690ac02019-03-20 16:43:07 +01007149 ret = read_one_chunk(&found_key, leaf, chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007150 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05007151 if (ret)
7152 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007153 }
7154 path->slots[0]++;
7155 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007156
7157 /*
7158 * After loading chunk tree, we've got all device information,
7159 * do another round of validation checks.
7160 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007161 if (total_dev != fs_info->fs_devices->total_devices) {
7162 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007163 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007164 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007165 total_dev);
7166 ret = -EINVAL;
7167 goto error;
7168 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007169 if (btrfs_super_total_bytes(fs_info->super_copy) <
7170 fs_info->fs_devices->total_rw_bytes) {
7171 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007172 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007173 btrfs_super_total_bytes(fs_info->super_copy),
7174 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007175 ret = -EINVAL;
7176 goto error;
7177 }
Chris Mason0b86a832008-03-24 15:01:56 -04007178 ret = 0;
7179error:
Li Zefanb367e472011-12-07 11:38:24 +08007180 mutex_unlock(&uuid_mutex);
7181
Yan Zheng2b820322008-11-17 21:11:30 -05007182 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007183 return ret;
7184}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007185
Miao Xiecb517ea2013-05-15 07:48:19 +00007186void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7187{
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007188 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
Miao Xiecb517ea2013-05-15 07:48:19 +00007189 struct btrfs_device *device;
7190
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007191 fs_devices->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007192
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007193 mutex_lock(&fs_devices->device_list_mutex);
7194 list_for_each_entry(device, &fs_devices->devices, dev_list)
7195 device->fs_info = fs_info;
7196 mutex_unlock(&fs_devices->device_list_mutex);
7197
7198 list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list) {
7199 mutex_lock(&seed_devs->device_list_mutex);
7200 list_for_each_entry(device, &seed_devs->devices, dev_list)
7201 device->fs_info = fs_info;
7202 mutex_unlock(&seed_devs->device_list_mutex);
7203
7204 seed_devs->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007205 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007206}
7207
David Sterba1dc990d2019-08-21 20:05:32 +02007208static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7209 const struct btrfs_dev_stats_item *ptr,
7210 int index)
7211{
7212 u64 val;
7213
7214 read_extent_buffer(eb, &val,
7215 offsetof(struct btrfs_dev_stats_item, values) +
7216 ((unsigned long)ptr) + (index * sizeof(u64)),
7217 sizeof(val));
7218 return val;
7219}
7220
7221static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7222 struct btrfs_dev_stats_item *ptr,
7223 int index, u64 val)
7224{
7225 write_extent_buffer(eb, &val,
7226 offsetof(struct btrfs_dev_stats_item, values) +
7227 ((unsigned long)ptr) + (index * sizeof(u64)),
7228 sizeof(val));
7229}
7230
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007231int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7232{
7233 struct btrfs_key key;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007234 struct btrfs_root *dev_root = fs_info->dev_root;
7235 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7236 struct extent_buffer *eb;
7237 int slot;
7238 int ret = 0;
7239 struct btrfs_device *device;
7240 struct btrfs_path *path = NULL;
7241 int i;
7242
7243 path = btrfs_alloc_path();
Anand Jain3b80a9842019-08-21 17:26:32 +08007244 if (!path)
7245 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007246
7247 mutex_lock(&fs_devices->device_list_mutex);
7248 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7249 int item_size;
7250 struct btrfs_dev_stats_item *ptr;
7251
David Sterba242e2952016-01-25 17:51:31 +01007252 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7253 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007254 key.offset = device->devid;
7255 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7256 if (ret) {
Anand Jainae4b9b42019-08-07 16:21:20 +08007257 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7258 btrfs_dev_stat_set(device, i, 0);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007259 device->dev_stats_valid = 1;
7260 btrfs_release_path(path);
7261 continue;
7262 }
7263 slot = path->slots[0];
7264 eb = path->nodes[0];
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007265 item_size = btrfs_item_size_nr(eb, slot);
7266
7267 ptr = btrfs_item_ptr(eb, slot,
7268 struct btrfs_dev_stats_item);
7269
7270 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7271 if (item_size >= (1 + i) * sizeof(__le64))
7272 btrfs_dev_stat_set(device, i,
7273 btrfs_dev_stats_value(eb, ptr, i));
7274 else
Anand Jain4e411a72019-08-07 16:21:19 +08007275 btrfs_dev_stat_set(device, i, 0);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007276 }
7277
7278 device->dev_stats_valid = 1;
7279 btrfs_dev_stat_print_on_load(device);
7280 btrfs_release_path(path);
7281 }
7282 mutex_unlock(&fs_devices->device_list_mutex);
7283
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007284 btrfs_free_path(path);
7285 return ret < 0 ? ret : 0;
7286}
7287
7288static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007289 struct btrfs_device *device)
7290{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007291 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007292 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007293 struct btrfs_path *path;
7294 struct btrfs_key key;
7295 struct extent_buffer *eb;
7296 struct btrfs_dev_stats_item *ptr;
7297 int ret;
7298 int i;
7299
David Sterba242e2952016-01-25 17:51:31 +01007300 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7301 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007302 key.offset = device->devid;
7303
7304 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007305 if (!path)
7306 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007307 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7308 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007309 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007310 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007311 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007312 goto out;
7313 }
7314
7315 if (ret == 0 &&
7316 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7317 /* need to delete old one and insert a new one */
7318 ret = btrfs_del_item(trans, dev_root, path);
7319 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007320 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007321 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007322 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007323 goto out;
7324 }
7325 ret = 1;
7326 }
7327
7328 if (ret == 1) {
7329 /* need to insert a new item */
7330 btrfs_release_path(path);
7331 ret = btrfs_insert_empty_item(trans, dev_root, path,
7332 &key, sizeof(*ptr));
7333 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007334 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007335 "insert dev_stats item for device %s failed %d",
7336 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007337 goto out;
7338 }
7339 }
7340
7341 eb = path->nodes[0];
7342 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7343 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7344 btrfs_set_dev_stats_value(eb, ptr, i,
7345 btrfs_dev_stat_read(device, i));
7346 btrfs_mark_buffer_dirty(eb);
7347
7348out:
7349 btrfs_free_path(path);
7350 return ret;
7351}
7352
7353/*
7354 * called from commit_transaction. Writes all changed device stats to disk.
7355 */
David Sterba196c9d82019-03-20 16:50:38 +01007356int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007357{
David Sterba196c9d82019-03-20 16:50:38 +01007358 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007359 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7360 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007361 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007362 int ret = 0;
7363
7364 mutex_lock(&fs_devices->device_list_mutex);
7365 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007366 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7367 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007368 continue;
7369
Nikolay Borisov9deae962017-10-24 13:47:37 +03007370
7371 /*
7372 * There is a LOAD-LOAD control dependency between the value of
7373 * dev_stats_ccnt and updating the on-disk values which requires
7374 * reading the in-memory counters. Such control dependencies
7375 * require explicit read memory barriers.
7376 *
7377 * This memory barriers pairs with smp_mb__before_atomic in
7378 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7379 * barrier implied by atomic_xchg in
7380 * btrfs_dev_stats_read_and_reset
7381 */
7382 smp_rmb();
7383
Nikolay Borisov5495f192018-07-20 19:37:49 +03007384 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007385 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007386 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007387 }
7388 mutex_unlock(&fs_devices->device_list_mutex);
7389
7390 return ret;
7391}
7392
Stefan Behrens442a4f62012-05-25 16:06:08 +02007393void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7394{
7395 btrfs_dev_stat_inc(dev, index);
7396 btrfs_dev_stat_print_on_error(dev);
7397}
7398
Eric Sandeen48a3b632013-04-25 20:41:01 +00007399static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007400{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007401 if (!dev->dev_stats_valid)
7402 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007403 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007404 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007405 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007406 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7407 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7408 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007409 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7410 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007411}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007412
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007413static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7414{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007415 int i;
7416
7417 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7418 if (btrfs_dev_stat_read(dev, i) != 0)
7419 break;
7420 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7421 return; /* all values == 0, suppress message */
7422
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007423 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007424 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007425 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007426 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7427 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7428 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7429 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7430 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7431}
7432
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007433int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007434 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007435{
7436 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007437 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007438 int i;
7439
7440 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007441 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7442 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007443 mutex_unlock(&fs_devices->device_list_mutex);
7444
7445 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007446 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007447 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007448 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007449 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007450 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007451 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007452 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7453 if (stats->nr_items > i)
7454 stats->values[i] =
7455 btrfs_dev_stat_read_and_reset(dev, i);
7456 else
Anand Jain4e411a72019-08-07 16:21:19 +08007457 btrfs_dev_stat_set(dev, i, 0);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007458 }
Anand Jaina69976b2020-01-10 12:26:34 +08007459 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7460 current->comm, task_pid_nr(current));
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007461 } else {
7462 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7463 if (stats->nr_items > i)
7464 stats->values[i] = btrfs_dev_stat_read(dev, i);
7465 }
7466 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7467 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7468 return 0;
7469}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007470
Miao Xie935e5cc2014-09-03 21:35:33 +08007471/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007472 * Update the size and bytes used for each device where it changed. This is
7473 * delayed since we would otherwise get errors while writing out the
7474 * superblocks.
7475 *
7476 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007477 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007478void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007479{
Miao Xie935e5cc2014-09-03 21:35:33 +08007480 struct btrfs_device *curr, *next;
7481
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007482 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7483
7484 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007485 return;
7486
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007487 /*
7488 * We don't need the device_list_mutex here. This list is owned by the
7489 * transaction and the transaction must complete before the device is
7490 * released.
7491 */
7492 mutex_lock(&trans->fs_info->chunk_mutex);
7493 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7494 post_commit_list) {
7495 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007496 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007497 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007498 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007499 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007500}
Anand Jain5a13f432015-03-10 06:38:31 +08007501
David Sterba46df06b2018-07-13 20:46:30 +02007502/*
7503 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7504 */
7505int btrfs_bg_type_to_factor(u64 flags)
7506{
David Sterba44b28ad2019-05-17 11:43:31 +02007507 const int index = btrfs_bg_flags_to_raid_index(flags);
7508
7509 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007510}
Qu Wenruocf90d882018-08-01 10:37:19 +08007511
7512
Qu Wenruocf90d882018-08-01 10:37:19 +08007513
7514static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7515 u64 chunk_offset, u64 devid,
7516 u64 physical_offset, u64 physical_len)
7517{
David Sterbac8bf1b62019-05-17 11:43:17 +02007518 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007519 struct extent_map *em;
7520 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007521 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007522 u64 stripe_len;
7523 bool found = false;
7524 int ret = 0;
7525 int i;
7526
7527 read_lock(&em_tree->lock);
7528 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7529 read_unlock(&em_tree->lock);
7530
7531 if (!em) {
7532 btrfs_err(fs_info,
7533"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7534 physical_offset, devid);
7535 ret = -EUCLEAN;
7536 goto out;
7537 }
7538
7539 map = em->map_lookup;
7540 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7541 if (physical_len != stripe_len) {
7542 btrfs_err(fs_info,
7543"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7544 physical_offset, devid, em->start, physical_len,
7545 stripe_len);
7546 ret = -EUCLEAN;
7547 goto out;
7548 }
7549
7550 for (i = 0; i < map->num_stripes; i++) {
7551 if (map->stripes[i].dev->devid == devid &&
7552 map->stripes[i].physical == physical_offset) {
7553 found = true;
7554 if (map->verified_stripes >= map->num_stripes) {
7555 btrfs_err(fs_info,
7556 "too many dev extents for chunk %llu found",
7557 em->start);
7558 ret = -EUCLEAN;
7559 goto out;
7560 }
7561 map->verified_stripes++;
7562 break;
7563 }
7564 }
7565 if (!found) {
7566 btrfs_err(fs_info,
7567 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7568 physical_offset, devid);
7569 ret = -EUCLEAN;
7570 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007571
7572 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007573 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007574 if (!dev) {
7575 btrfs_err(fs_info, "failed to find devid %llu", devid);
7576 ret = -EUCLEAN;
7577 goto out;
7578 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007579
7580 /* It's possible this device is a dummy for seed device */
7581 if (dev->disk_total_bytes == 0) {
Nikolay Borisov944d3f92020-07-16 10:25:33 +03007582 struct btrfs_fs_devices *devs;
7583
7584 devs = list_first_entry(&fs_info->fs_devices->seed_list,
7585 struct btrfs_fs_devices, seed_list);
7586 dev = btrfs_find_device(devs, devid, NULL, NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007587 if (!dev) {
7588 btrfs_err(fs_info, "failed to find seed devid %llu",
7589 devid);
7590 ret = -EUCLEAN;
7591 goto out;
7592 }
7593 }
7594
Qu Wenruo05a37c42018-10-05 17:45:55 +08007595 if (physical_offset + physical_len > dev->disk_total_bytes) {
7596 btrfs_err(fs_info,
7597"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7598 devid, physical_offset, physical_len,
7599 dev->disk_total_bytes);
7600 ret = -EUCLEAN;
7601 goto out;
7602 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007603out:
7604 free_extent_map(em);
7605 return ret;
7606}
7607
7608static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7609{
David Sterbac8bf1b62019-05-17 11:43:17 +02007610 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007611 struct extent_map *em;
7612 struct rb_node *node;
7613 int ret = 0;
7614
7615 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007616 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007617 em = rb_entry(node, struct extent_map, rb_node);
7618 if (em->map_lookup->num_stripes !=
7619 em->map_lookup->verified_stripes) {
7620 btrfs_err(fs_info,
7621 "chunk %llu has missing dev extent, have %d expect %d",
7622 em->start, em->map_lookup->verified_stripes,
7623 em->map_lookup->num_stripes);
7624 ret = -EUCLEAN;
7625 goto out;
7626 }
7627 }
7628out:
7629 read_unlock(&em_tree->lock);
7630 return ret;
7631}
7632
7633/*
7634 * Ensure that all dev extents are mapped to correct chunk, otherwise
7635 * later chunk allocation/free would cause unexpected behavior.
7636 *
7637 * NOTE: This will iterate through the whole device tree, which should be of
7638 * the same size level as the chunk tree. This slightly increases mount time.
7639 */
7640int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7641{
7642 struct btrfs_path *path;
7643 struct btrfs_root *root = fs_info->dev_root;
7644 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007645 u64 prev_devid = 0;
7646 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007647 int ret = 0;
7648
7649 key.objectid = 1;
7650 key.type = BTRFS_DEV_EXTENT_KEY;
7651 key.offset = 0;
7652
7653 path = btrfs_alloc_path();
7654 if (!path)
7655 return -ENOMEM;
7656
7657 path->reada = READA_FORWARD;
7658 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7659 if (ret < 0)
7660 goto out;
7661
7662 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7663 ret = btrfs_next_item(root, path);
7664 if (ret < 0)
7665 goto out;
7666 /* No dev extents at all? Not good */
7667 if (ret > 0) {
7668 ret = -EUCLEAN;
7669 goto out;
7670 }
7671 }
7672 while (1) {
7673 struct extent_buffer *leaf = path->nodes[0];
7674 struct btrfs_dev_extent *dext;
7675 int slot = path->slots[0];
7676 u64 chunk_offset;
7677 u64 physical_offset;
7678 u64 physical_len;
7679 u64 devid;
7680
7681 btrfs_item_key_to_cpu(leaf, &key, slot);
7682 if (key.type != BTRFS_DEV_EXTENT_KEY)
7683 break;
7684 devid = key.objectid;
7685 physical_offset = key.offset;
7686
7687 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7688 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7689 physical_len = btrfs_dev_extent_length(leaf, dext);
7690
Qu Wenruo5eb19382018-10-05 17:45:54 +08007691 /* Check if this dev extent overlaps with the previous one */
7692 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7693 btrfs_err(fs_info,
7694"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7695 devid, physical_offset, prev_dev_ext_end);
7696 ret = -EUCLEAN;
7697 goto out;
7698 }
7699
Qu Wenruocf90d882018-08-01 10:37:19 +08007700 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7701 physical_offset, physical_len);
7702 if (ret < 0)
7703 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007704 prev_devid = devid;
7705 prev_dev_ext_end = physical_offset + physical_len;
7706
Qu Wenruocf90d882018-08-01 10:37:19 +08007707 ret = btrfs_next_item(root, path);
7708 if (ret < 0)
7709 goto out;
7710 if (ret > 0) {
7711 ret = 0;
7712 break;
7713 }
7714 }
7715
7716 /* Ensure all chunks have corresponding dev extents */
7717 ret = verify_chunk_dev_extent_mapping(fs_info);
7718out:
7719 btrfs_free_path(path);
7720 return ret;
7721}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007722
7723/*
7724 * Check whether the given block group or device is pinned by any inode being
7725 * used as a swapfile.
7726 */
7727bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7728{
7729 struct btrfs_swapfile_pin *sp;
7730 struct rb_node *node;
7731
7732 spin_lock(&fs_info->swapfile_pins_lock);
7733 node = fs_info->swapfile_pins.rb_node;
7734 while (node) {
7735 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7736 if (ptr < sp->ptr)
7737 node = node->rb_left;
7738 else if (ptr > sp->ptr)
7739 node = node->rb_right;
7740 else
7741 break;
7742 }
7743 spin_unlock(&fs_info->swapfile_pins_lock);
7744 return node != NULL;
7745}