blob: 1997a7d67f22faa407162771143e111c091d6d7c [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);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300359 if (fsid)
360 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300361
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200362 if (metadata_fsid)
363 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
364 else if (fsid)
365 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
366
Ilya Dryomov2208a372013-08-12 14:33:03 +0300367 return fs_devs;
368}
369
David Sterbaa425f9d2018-03-20 15:47:33 +0100370void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100371{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200372 WARN_ON(!list_empty(&device->post_commit_list));
David Sterba48dae9c2017-10-30 18:10:25 +0100373 rcu_string_free(device->name);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200374 extent_io_tree_release(&device->alloc_state);
David Sterba48dae9c2017-10-30 18:10:25 +0100375 bio_put(device->flush_bio);
376 kfree(device);
377}
378
Yan Zhenge4404d62008-12-12 10:03:26 -0500379static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
380{
381 struct btrfs_device *device;
382 WARN_ON(fs_devices->opened);
383 while (!list_empty(&fs_devices->devices)) {
384 device = list_entry(fs_devices->devices.next,
385 struct btrfs_device, dev_list);
386 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100387 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500388 }
389 kfree(fs_devices);
390}
391
David Sterbaffc5a372018-02-19 17:24:15 +0100392void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400393{
394 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400395
Yan Zheng2b820322008-11-17 21:11:30 -0500396 while (!list_empty(&fs_uuids)) {
397 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800398 struct btrfs_fs_devices, fs_list);
399 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500400 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400401 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400402}
403
David Sterba48dae9c2017-10-30 18:10:25 +0100404/*
405 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
406 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100407 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100408 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300409static struct btrfs_device *__alloc_device(void)
410{
411 struct btrfs_device *dev;
412
David Sterba78f2c9e2016-02-11 14:25:38 +0100413 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300414 if (!dev)
415 return ERR_PTR(-ENOMEM);
416
David Sterbae0ae9992017-06-06 17:06:06 +0200417 /*
418 * Preallocate a bio that's always going to be used for flushing device
419 * barriers and matches the device lifespan
420 */
421 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
422 if (!dev->flush_bio) {
423 kfree(dev);
424 return ERR_PTR(-ENOMEM);
425 }
David Sterbae0ae9992017-06-06 17:06:06 +0200426
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300427 INIT_LIST_HEAD(&dev->dev_list);
428 INIT_LIST_HEAD(&dev->dev_alloc_list);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200429 INIT_LIST_HEAD(&dev->post_commit_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300430
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300431 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800432 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100433 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700434 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800435 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200436 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300437
438 return dev;
439}
440
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200441static noinline struct btrfs_fs_devices *find_fsid(
442 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400443{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400444 struct btrfs_fs_devices *fs_devices;
445
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200446 ASSERT(fsid);
447
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200448 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800449 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200450 if (metadata_fsid) {
451 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
452 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
453 BTRFS_FSID_SIZE) == 0)
454 return fs_devices;
455 } else {
456 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
457 return fs_devices;
458 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400459 }
460 return NULL;
461}
462
Su Yuec6730a02020-01-10 14:11:33 +0200463static struct btrfs_fs_devices *find_fsid_with_metadata_uuid(
464 struct btrfs_super_block *disk_super)
465{
466
467 struct btrfs_fs_devices *fs_devices;
468
469 /*
470 * Handle scanned device having completed its fsid change but
471 * belonging to a fs_devices that was created by first scanning
472 * a device which didn't have its fsid/metadata_uuid changed
473 * at all and the CHANGING_FSID_V2 flag set.
474 */
475 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
476 if (fs_devices->fsid_change &&
477 memcmp(disk_super->metadata_uuid, fs_devices->fsid,
478 BTRFS_FSID_SIZE) == 0 &&
479 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
480 BTRFS_FSID_SIZE) == 0) {
481 return fs_devices;
482 }
483 }
484 /*
485 * Handle scanned device having completed its fsid change but
486 * belonging to a fs_devices that was created by a device that
487 * has an outdated pair of fsid/metadata_uuid and
488 * CHANGING_FSID_V2 flag set.
489 */
490 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
491 if (fs_devices->fsid_change &&
492 memcmp(fs_devices->metadata_uuid,
493 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
494 memcmp(disk_super->metadata_uuid, fs_devices->metadata_uuid,
495 BTRFS_FSID_SIZE) == 0) {
496 return fs_devices;
497 }
498 }
499
500 return find_fsid(disk_super->fsid, disk_super->metadata_uuid);
501}
502
503
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100504static int
505btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
506 int flush, struct block_device **bdev,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900507 struct btrfs_super_block **disk_super)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100508{
509 int ret;
510
511 *bdev = blkdev_get_by_path(device_path, flags, holder);
512
513 if (IS_ERR(*bdev)) {
514 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100515 goto error;
516 }
517
518 if (flush)
519 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200520 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100521 if (ret) {
522 blkdev_put(*bdev, flags);
523 goto error;
524 }
525 invalidate_bdev(*bdev);
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900526 *disk_super = btrfs_read_dev_super(*bdev);
527 if (IS_ERR(*disk_super)) {
528 ret = PTR_ERR(*disk_super);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100529 blkdev_put(*bdev, flags);
530 goto error;
531 }
532
533 return 0;
534
535error:
536 *bdev = NULL;
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100537 return ret;
538}
539
Anand Jain70bc7082019-01-04 13:31:53 +0800540static bool device_path_matched(const char *path, struct btrfs_device *device)
541{
542 int found;
543
544 rcu_read_lock();
545 found = strcmp(rcu_str_deref(device->name), path);
546 rcu_read_unlock();
547
548 return found == 0;
549}
550
Anand Jaind8367db2018-01-18 22:00:37 +0800551/*
552 * Search and remove all stale (devices which are not mounted) devices.
553 * When both inputs are NULL, it will search and release all stale devices.
554 * path: Optional. When provided will it release all unmounted devices
555 * matching this path only.
556 * skip_dev: Optional. Will skip this device when searching for the stale
557 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800558 * Return: 0 for success or if @path is NULL.
559 * -EBUSY if @path is a mounted device.
560 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800561 */
Anand Jain70bc7082019-01-04 13:31:53 +0800562static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800563 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800564{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800565 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
566 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800567 int ret = 0;
568
569 if (path)
570 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800571
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800572 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800573
Anand Jain70bc7082019-01-04 13:31:53 +0800574 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800575 list_for_each_entry_safe(device, tmp_device,
576 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800577 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800578 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800579 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800580 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800581 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800582 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800583 if (fs_devices->opened) {
584 /* for an already deleted device return 0 */
585 if (path && ret != 0)
586 ret = -EBUSY;
587 break;
588 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800589
Anand Jain4fde46f2015-06-17 21:10:48 +0800590 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800591 fs_devices->num_devices--;
592 list_del(&device->dev_list);
593 btrfs_free_device(device);
594
Anand Jain70bc7082019-01-04 13:31:53 +0800595 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800596 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200597 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800598 }
599 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800600
Anand Jain7bcb8162018-05-29 17:23:20 +0800601 if (fs_devices->num_devices == 0) {
602 btrfs_sysfs_remove_fsid(fs_devices);
603 list_del(&fs_devices->fs_list);
604 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800605 }
606 }
Anand Jain70bc7082019-01-04 13:31:53 +0800607
608 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800609}
610
Josef Bacik18c850f2020-07-17 15:12:27 -0400611/*
612 * This is only used on mount, and we are protected from competing things
613 * messing with our fs_devices by the uuid_mutex, thus we do not need the
614 * fs_devices->device_list_mutex here.
615 */
Anand Jain0fb08bc2017-11-09 23:45:24 +0800616static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
617 struct btrfs_device *device, fmode_t flags,
618 void *holder)
619{
620 struct request_queue *q;
621 struct block_device *bdev;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800622 struct btrfs_super_block *disk_super;
623 u64 devid;
624 int ret;
625
626 if (device->bdev)
627 return -EINVAL;
628 if (!device->name)
629 return -EINVAL;
630
631 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900632 &bdev, &disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800633 if (ret)
634 return ret;
635
Anand Jain0fb08bc2017-11-09 23:45:24 +0800636 devid = btrfs_stack_device_id(&disk_super->dev_item);
637 if (devid != device->devid)
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900638 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800639
640 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900641 goto error_free_page;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800642
643 device->generation = btrfs_super_generation(disk_super);
644
645 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200646 if (btrfs_super_incompat_flags(disk_super) &
647 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
648 pr_err(
649 "BTRFS: Invalid seeding and uuid-changed device detected\n");
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900650 goto error_free_page;
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200651 }
652
Anand Jainebbede42017-12-04 12:54:52 +0800653 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Johannes Thumshirn0395d842019-11-13 11:27:27 +0100654 fs_devices->seeding = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800655 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800656 if (bdev_read_only(bdev))
657 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
658 else
659 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800660 }
661
662 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800663 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +0100664 fs_devices->rotating = true;
Anand Jain0fb08bc2017-11-09 23:45:24 +0800665
666 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800667 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800668 device->mode = flags;
669
670 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800671 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
672 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800673 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800674 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800675 }
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900676 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800677
678 return 0;
679
Johannes Thumshirn8f323802020-02-14 00:24:32 +0900680error_free_page:
681 btrfs_release_disk_super(disk_super);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800682 blkdev_put(bdev, flags);
683
684 return -EINVAL;
685}
686
David Sterba60999ca2014-03-26 18:26:36 +0100687/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200688 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
Su Yuec0d81c72020-01-10 14:11:32 +0200689 * being created with a disk that has already completed its fsid change. Such
690 * disk can belong to an fs which has its FSID changed or to one which doesn't.
691 * Handle both cases here.
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200692 */
693static struct btrfs_fs_devices *find_fsid_inprogress(
694 struct btrfs_super_block *disk_super)
695{
696 struct btrfs_fs_devices *fs_devices;
697
698 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
699 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
700 BTRFS_FSID_SIZE) != 0 &&
701 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
702 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
703 return fs_devices;
704 }
705 }
706
Su Yuec0d81c72020-01-10 14:11:32 +0200707 return find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200708}
709
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200710
711static struct btrfs_fs_devices *find_fsid_changed(
712 struct btrfs_super_block *disk_super)
713{
714 struct btrfs_fs_devices *fs_devices;
715
716 /*
717 * Handles the case where scanned device is part of an fs that had
718 * multiple successful changes of FSID but curently device didn't
Nikolay Borisov05840712020-01-10 14:11:34 +0200719 * observe it. Meaning our fsid will be different than theirs. We need
720 * to handle two subcases :
721 * 1 - The fs still continues to have different METADATA/FSID uuids.
722 * 2 - The fs is switched back to its original FSID (METADATA/FSID
723 * are equal).
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200724 */
725 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov05840712020-01-10 14:11:34 +0200726 /* Changed UUIDs */
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200727 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
728 BTRFS_FSID_SIZE) != 0 &&
729 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
730 BTRFS_FSID_SIZE) == 0 &&
731 memcmp(fs_devices->fsid, disk_super->fsid,
Nikolay Borisov05840712020-01-10 14:11:34 +0200732 BTRFS_FSID_SIZE) != 0)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200733 return fs_devices;
Nikolay Borisov05840712020-01-10 14:11:34 +0200734
735 /* Unchanged UUIDs */
736 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
737 BTRFS_FSID_SIZE) == 0 &&
738 memcmp(fs_devices->fsid, disk_super->metadata_uuid,
739 BTRFS_FSID_SIZE) == 0)
740 return fs_devices;
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200741 }
742
743 return NULL;
744}
Nikolay Borisov13620892020-01-10 14:11:35 +0200745
746static struct btrfs_fs_devices *find_fsid_reverted_metadata(
747 struct btrfs_super_block *disk_super)
748{
749 struct btrfs_fs_devices *fs_devices;
750
751 /*
752 * Handle the case where the scanned device is part of an fs whose last
753 * metadata UUID change reverted it to the original FSID. At the same
754 * time * fs_devices was first created by another constitutent device
755 * which didn't fully observe the operation. This results in an
756 * btrfs_fs_devices created with metadata/fsid different AND
757 * btrfs_fs_devices::fsid_change set AND the metadata_uuid of the
758 * fs_devices equal to the FSID of the disk.
759 */
760 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
761 if (memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
762 BTRFS_FSID_SIZE) != 0 &&
763 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
764 BTRFS_FSID_SIZE) == 0 &&
765 fs_devices->fsid_change)
766 return fs_devices;
767 }
768
769 return NULL;
770}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200771/*
David Sterba60999ca2014-03-26 18:26:36 +0100772 * Add new device to list of registered devices
773 *
774 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800775 * device pointer which was just added or updated when successful
776 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100777 */
Anand Jaine124ece2018-01-18 22:02:35 +0800778static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800779 struct btrfs_super_block *disk_super,
780 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400781{
782 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200783 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400784 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400785 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800786 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200787 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
788 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200789 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
790 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400791
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200792 if (fsid_change_in_progress) {
Su Yuec0d81c72020-01-10 14:11:32 +0200793 if (!has_metadata_uuid)
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200794 fs_devices = find_fsid_inprogress(disk_super);
Su Yuec0d81c72020-01-10 14:11:32 +0200795 else
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200796 fs_devices = find_fsid_changed(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200797 } else if (has_metadata_uuid) {
Su Yuec6730a02020-01-10 14:11:33 +0200798 fs_devices = find_fsid_with_metadata_uuid(disk_super);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200799 } else {
Nikolay Borisov13620892020-01-10 14:11:35 +0200800 fs_devices = find_fsid_reverted_metadata(disk_super);
801 if (!fs_devices)
802 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200803 }
804
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200805
Chris Mason8a4b83c2008-03-24 15:02:07 -0400806 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200807 if (has_metadata_uuid)
808 fs_devices = alloc_fs_devices(disk_super->fsid,
809 disk_super->metadata_uuid);
810 else
811 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
812
Ilya Dryomov2208a372013-08-12 14:33:03 +0300813 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800814 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300815
Al Viro92900e52019-01-27 04:58:00 +0000816 fs_devices->fsid_change = fsid_change_in_progress;
817
Anand Jain9c6d1732018-05-29 14:10:20 +0800818 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800819 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300820
Chris Mason8a4b83c2008-03-24 15:02:07 -0400821 device = NULL;
822 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800823 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800824 device = btrfs_find_device(fs_devices, devid,
825 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200826
827 /*
828 * If this disk has been pulled into an fs devices created by
829 * a device which had the CHANGING_FSID_V2 flag then replace the
830 * metadata_uuid/fsid values of the fs_devices.
831 */
Nikolay Borisov13620892020-01-10 14:11:35 +0200832 if (fs_devices->fsid_change &&
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200833 found_transid > fs_devices->latest_generation) {
834 memcpy(fs_devices->fsid, disk_super->fsid,
835 BTRFS_FSID_SIZE);
Nikolay Borisov13620892020-01-10 14:11:35 +0200836
837 if (has_metadata_uuid)
838 memcpy(fs_devices->metadata_uuid,
839 disk_super->metadata_uuid,
840 BTRFS_FSID_SIZE);
841 else
842 memcpy(fs_devices->metadata_uuid,
843 disk_super->fsid, BTRFS_FSID_SIZE);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200844
845 fs_devices->fsid_change = false;
846 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400847 }
Miao Xie443f24f2014-07-24 11:37:15 +0800848
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800850 if (fs_devices->opened) {
851 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800852 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800853 }
Yan Zheng2b820322008-11-17 21:11:30 -0500854
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300855 device = btrfs_alloc_device(NULL, &devid,
856 disk_super->dev_item.uuid);
857 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800858 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400859 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800860 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400861 }
Josef Bacik606686e2012-06-04 14:03:51 -0400862
863 name = rcu_string_strdup(path, GFP_NOFS);
864 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100865 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800866 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800867 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400868 }
Josef Bacik606686e2012-06-04 14:03:51 -0400869 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200870
Xiao Guangrong1f781602011-04-20 10:09:16 +0000871 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100872 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400873
Yan Zheng2b820322008-11-17 21:11:30 -0500874 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800875 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800876
877 if (disk_super->label[0])
Anand Jainaa6c0df2019-10-02 18:30:48 +0800878 pr_info(
879 "BTRFS: device label %s devid %llu transid %llu %s scanned by %s (%d)\n",
880 disk_super->label, devid, found_transid, path,
881 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800882 else
Anand Jainaa6c0df2019-10-02 18:30:48 +0800883 pr_info(
884 "BTRFS: device fsid %pU devid %llu transid %llu %s scanned by %s (%d)\n",
885 disk_super->fsid, devid, found_transid, path,
886 current->comm, task_pid_nr(current));
Anand Jain327f18c2018-01-18 22:02:33 +0800887
Josef Bacik606686e2012-06-04 14:03:51 -0400888 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800889 /*
890 * When FS is already mounted.
891 * 1. If you are here and if the device->name is NULL that
892 * means this device was missing at time of FS mount.
893 * 2. If you are here and if the device->name is different
894 * from 'path' that means either
895 * a. The same device disappeared and reappeared with
896 * different name. or
897 * b. The missing-disk-which-was-replaced, has
898 * reappeared now.
899 *
900 * We must allow 1 and 2a above. But 2b would be a spurious
901 * and unintentional.
902 *
903 * Further in case of 1 and 2a above, the disk at 'path'
904 * would have missed some transaction when it was away and
905 * in case of 2a the stale bdev has to be updated as well.
906 * 2b must not be allowed at all time.
907 */
908
909 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700910 * For now, we do allow update to btrfs_fs_device through the
911 * btrfs dev scan cli after FS has been mounted. We're still
912 * tracking a problem where systems fail mount by subvolume id
913 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800914 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700915 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800916 /*
917 * That is if the FS is _not_ mounted and if you
918 * are here, that means there is more than one
919 * disk with same uuid and devid.We keep the one
920 * with larger generation number or the last-in if
921 * generation are equal.
922 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800923 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800924 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800925 }
Anand Jainb96de002014-07-03 18:22:05 +0800926
Anand Jaina9261d42018-10-15 10:45:17 +0800927 /*
928 * We are going to replace the device path for a given devid,
929 * make sure it's the same device if the device is mounted
930 */
931 if (device->bdev) {
932 struct block_device *path_bdev;
933
934 path_bdev = lookup_bdev(path);
935 if (IS_ERR(path_bdev)) {
936 mutex_unlock(&fs_devices->device_list_mutex);
937 return ERR_CAST(path_bdev);
938 }
939
940 if (device->bdev != path_bdev) {
941 bdput(path_bdev);
942 mutex_unlock(&fs_devices->device_list_mutex);
943 btrfs_warn_in_rcu(device->fs_info,
944 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
945 disk_super->fsid, devid,
946 rcu_str_deref(device->name), path);
947 return ERR_PTR(-EEXIST);
948 }
949 bdput(path_bdev);
950 btrfs_info_in_rcu(device->fs_info,
951 "device fsid %pU devid %llu moved old:%s new:%s",
952 disk_super->fsid, devid,
953 rcu_str_deref(device->name), path);
954 }
955
Josef Bacik606686e2012-06-04 14:03:51 -0400956 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800957 if (!name) {
958 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800959 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800960 }
Josef Bacik606686e2012-06-04 14:03:51 -0400961 rcu_string_free(device->name);
962 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800963 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500964 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800965 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500966 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400967 }
968
Anand Jain77bdae42014-07-03 18:22:06 +0800969 /*
970 * Unmount does not free the btrfs_device struct but would zero
971 * generation along with most of the other members. So just update
972 * it back. We need it to pick the disk with largest generation
973 * (as above).
974 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200975 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +0800976 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200977 fs_devices->latest_generation = max_t(u64, found_transid,
978 fs_devices->latest_generation);
979 }
Anand Jain77bdae42014-07-03 18:22:06 +0800980
Anand Jainf2788d22018-01-18 22:02:34 +0800981 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
982
Anand Jain9c6d1732018-05-29 14:10:20 +0800983 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800984 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400985}
986
Yan Zhenge4404d62008-12-12 10:03:26 -0500987static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
988{
989 struct btrfs_fs_devices *fs_devices;
990 struct btrfs_device *device;
991 struct btrfs_device *orig_dev;
Anand Jaind2979aa2019-08-27 15:40:45 +0800992 int ret = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -0500993
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200994 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300995 if (IS_ERR(fs_devices))
996 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500997
Miao Xieadbbb862014-09-03 21:35:42 +0800998 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400999 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001000
1001 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001002 struct rcu_string *name;
1003
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001004 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1005 orig_dev->uuid);
Anand Jaind2979aa2019-08-27 15:40:45 +08001006 if (IS_ERR(device)) {
1007 ret = PTR_ERR(device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001008 goto error;
Anand Jaind2979aa2019-08-27 15:40:45 +08001009 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001010
Josef Bacik606686e2012-06-04 14:03:51 -04001011 /*
1012 * This is ok to do without rcu read locked because we hold the
1013 * uuid mutex so nothing we touch in here is going to disappear.
1014 */
Anand Jaine755f782014-06-30 17:12:47 +08001015 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001016 name = rcu_string_strdup(orig_dev->name->str,
1017 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001018 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001019 btrfs_free_device(device);
Anand Jaind2979aa2019-08-27 15:40:45 +08001020 ret = -ENOMEM;
Anand Jaine755f782014-06-30 17:12:47 +08001021 goto error;
1022 }
1023 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001024 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001025
Yan Zhenge4404d62008-12-12 10:03:26 -05001026 list_add(&device->dev_list, &fs_devices->devices);
1027 device->fs_devices = fs_devices;
1028 fs_devices->num_devices++;
1029 }
Miao Xieadbbb862014-09-03 21:35:42 +08001030 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001031 return fs_devices;
1032error:
Miao Xieadbbb862014-09-03 21:35:42 +08001033 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001034 free_fs_devices(fs_devices);
Anand Jaind2979aa2019-08-27 15:40:45 +08001035 return ERR_PTR(ret);
Yan Zhenge4404d62008-12-12 10:03:26 -05001036}
1037
Anand Jain9b99b112018-02-27 12:41:59 +08001038/*
1039 * After we have read the system tree and know devids belonging to
1040 * this filesystem, remove the device which does not belong there.
1041 */
1042void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001043{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001044 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001045 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001046
Chris Masondfe25022008-05-13 13:46:40 -04001047 mutex_lock(&uuid_mutex);
1048again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001049 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001050 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001051 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1052 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001053 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1054 &device->dev_state) &&
Anand Jain998a0672020-05-05 02:58:25 +08001055 !test_bit(BTRFS_DEV_STATE_MISSING,
1056 &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08001057 (!latest_dev ||
1058 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001059 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001060 }
Yan Zheng2b820322008-11-17 21:11:30 -05001061 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001062 }
Yan Zheng2b820322008-11-17 21:11:30 -05001063
Stefan Behrens8dabb742012-11-06 13:15:27 +01001064 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1065 /*
1066 * In the first step, keep the device which has
1067 * the correct fsid and the devid that is used
1068 * for the dev_replace procedure.
1069 * In the second step, the dev_replace state is
1070 * read from the device tree and it is known
1071 * whether the procedure is really active or
1072 * not, which means whether this device is
1073 * used or whether it should be removed.
1074 */
Anand Jain401e29c2017-12-04 12:54:55 +08001075 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1076 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001077 continue;
1078 }
1079 }
Yan Zheng2b820322008-11-17 21:11:30 -05001080 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001081 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001082 device->bdev = NULL;
1083 fs_devices->open_devices--;
1084 }
Anand Jainebbede42017-12-04 12:54:52 +08001085 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001086 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001087 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001088 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1089 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001090 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001091 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001092 list_del_init(&device->dev_list);
1093 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001094 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001095 }
Yan Zheng2b820322008-11-17 21:11:30 -05001096
1097 if (fs_devices->seed) {
1098 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001099 goto again;
1100 }
1101
Miao Xie443f24f2014-07-24 11:37:15 +08001102 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001103
Chris Masondfe25022008-05-13 13:46:40 -04001104 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001105}
Chris Masona0af4692008-05-13 16:03:06 -04001106
Anand Jain14238812016-07-22 06:04:53 +08001107static void btrfs_close_bdev(struct btrfs_device *device)
1108{
David Sterba08ffcae2017-06-19 16:55:35 +02001109 if (!device->bdev)
1110 return;
1111
Anand Jainebbede42017-12-04 12:54:52 +08001112 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001113 sync_blockdev(device->bdev);
1114 invalidate_bdev(device->bdev);
1115 }
1116
David Sterba08ffcae2017-06-19 16:55:35 +02001117 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001118}
1119
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001120static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001121{
1122 struct btrfs_fs_devices *fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001123
Anand Jainebbede42017-12-04 12:54:52 +08001124 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001125 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1126 list_del_init(&device->dev_alloc_list);
1127 fs_devices->rw_devices--;
1128 }
1129
Anand Jaine6e674b2017-12-04 12:54:54 +08001130 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001131 fs_devices->missing_devices--;
1132
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001133 btrfs_close_bdev(device);
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001134 if (device->bdev) {
Johannes Thumshirn3fff3972019-11-26 09:40:05 +01001135 fs_devices->open_devices--;
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001136 device->bdev = NULL;
Anand Jainf448341a2016-06-14 18:55:25 +08001137 }
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001138 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jainf448341a2016-06-14 18:55:25 +08001139
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001140 device->fs_info = NULL;
1141 atomic_set(&device->dev_stats_ccnt, 0);
1142 extent_io_tree_release(&device->alloc_state);
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001143
Johannes Thumshirn321f69f2019-12-04 14:36:39 +01001144 /* Verify the device is back in a pristine state */
1145 ASSERT(!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state));
1146 ASSERT(!test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
1147 ASSERT(list_empty(&device->dev_alloc_list));
1148 ASSERT(list_empty(&device->post_commit_list));
1149 ASSERT(atomic_read(&device->reada_in_flight) == 0);
Anand Jainf448341a2016-06-14 18:55:25 +08001150}
1151
Anand Jain0226e0e2018-04-12 10:29:27 +08001152static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001153{
Sasha Levin2037a092015-05-12 19:31:37 -04001154 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001155
Yan Zheng2b820322008-11-17 21:11:30 -05001156 if (--fs_devices->opened > 0)
1157 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001158
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001159 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001160 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001161 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001162 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001163 mutex_unlock(&fs_devices->device_list_mutex);
1164
Yan Zhenge4404d62008-12-12 10:03:26 -05001165 WARN_ON(fs_devices->open_devices);
1166 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001167 fs_devices->opened = 0;
Johannes Thumshirn0395d842019-11-13 11:27:27 +01001168 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05001169
Chris Mason8a4b83c2008-03-24 15:02:07 -04001170 return 0;
1171}
1172
Yan Zheng2b820322008-11-17 21:11:30 -05001173int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1174{
Yan Zhenge4404d62008-12-12 10:03:26 -05001175 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001176 int ret;
1177
1178 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001179 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001180 if (!fs_devices->opened) {
1181 seed_devices = fs_devices->seed;
1182 fs_devices->seed = NULL;
1183 }
Yan Zheng2b820322008-11-17 21:11:30 -05001184 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001185
1186 while (seed_devices) {
1187 fs_devices = seed_devices;
1188 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001189 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001190 free_fs_devices(fs_devices);
1191 }
Yan Zheng2b820322008-11-17 21:11:30 -05001192 return ret;
1193}
1194
Anand Jain897fb572018-04-12 10:29:28 +08001195static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001196 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001197{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001198 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001199 struct btrfs_device *latest_dev = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001200
Tejun Heod4d77622010-11-13 11:55:18 +01001201 flags |= FMODE_EXCL;
1202
Anand Jainf117e292018-04-12 10:29:26 +08001203 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001204 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001205 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001206 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001207
Anand Jain9f050db2017-11-09 23:45:25 +08001208 if (!latest_dev ||
1209 device->generation > latest_dev->generation)
1210 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001211 }
Anand Jain1ed802c2020-04-28 23:22:25 +08001212 if (fs_devices->open_devices == 0)
1213 return -EINVAL;
1214
Yan Zheng2b820322008-11-17 21:11:30 -05001215 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001216 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001217 fs_devices->total_rw_bytes = 0;
Naohiro Aotac4a816c2020-02-25 12:56:08 +09001218 fs_devices->chunk_alloc_policy = BTRFS_CHUNK_ALLOC_REGULAR;
Anand Jain1ed802c2020-04-28 23:22:25 +08001219
1220 return 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001221}
1222
Anand Jainf8e10cd2018-01-22 14:49:36 -08001223static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1224{
1225 struct btrfs_device *dev1, *dev2;
1226
1227 dev1 = list_entry(a, struct btrfs_device, dev_list);
1228 dev2 = list_entry(b, struct btrfs_device, dev_list);
1229
1230 if (dev1->devid < dev2->devid)
1231 return -1;
1232 else if (dev1->devid > dev2->devid)
1233 return 1;
1234 return 0;
1235}
1236
Yan Zheng2b820322008-11-17 21:11:30 -05001237int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001238 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001239{
1240 int ret;
1241
David Sterbaf5194e32018-06-19 17:09:47 +02001242 lockdep_assert_held(&uuid_mutex);
Josef Bacik18c850f2020-07-17 15:12:27 -04001243 /*
1244 * The device_list_mutex cannot be taken here in case opening the
1245 * underlying device takes further locks like bd_mutex.
1246 *
1247 * We also don't need the lock here as this is called during mount and
1248 * exclusion is provided by uuid_mutex
1249 */
David Sterbaf5194e32018-06-19 17:09:47 +02001250
Yan Zheng2b820322008-11-17 21:11:30 -05001251 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001252 fs_devices->opened++;
1253 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001254 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001255 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001256 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001257 }
Anand Jain542c5902018-04-12 10:29:34 +08001258
Chris Mason8a4b83c2008-03-24 15:02:07 -04001259 return ret;
1260}
1261
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001262void btrfs_release_disk_super(struct btrfs_super_block *super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001263{
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001264 struct page *page = virt_to_page(super);
1265
Anand Jain6cf86a002016-02-13 10:01:29 +08001266 put_page(page);
1267}
1268
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001269static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
1270 u64 bytenr)
Anand Jain6cf86a002016-02-13 10:01:29 +08001271{
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001272 struct btrfs_super_block *disk_super;
1273 struct page *page;
Anand Jain6cf86a002016-02-13 10:01:29 +08001274 void *p;
1275 pgoff_t index;
1276
1277 /* make sure our super fits in the device */
1278 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001279 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001280
1281 /* make sure our super fits in the page */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001282 if (sizeof(*disk_super) > PAGE_SIZE)
1283 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001284
1285 /* make sure our super doesn't straddle pages on disk */
1286 index = bytenr >> PAGE_SHIFT;
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001287 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
1288 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001289
1290 /* pull in the page with our super */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001291 page = read_cache_page_gfp(bdev->bd_inode->i_mapping, index, GFP_KERNEL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001292
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001293 if (IS_ERR(page))
1294 return ERR_CAST(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001295
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001296 p = page_address(page);
Anand Jain6cf86a002016-02-13 10:01:29 +08001297
1298 /* align our pointer to the offset of the super block */
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001299 disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001300
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001301 if (btrfs_super_bytenr(disk_super) != bytenr ||
1302 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001303 btrfs_release_disk_super(p);
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001304 return ERR_PTR(-EINVAL);
Anand Jain6cf86a002016-02-13 10:01:29 +08001305 }
1306
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001307 if (disk_super->label[0] && disk_super->label[BTRFS_LABEL_SIZE - 1])
1308 disk_super->label[BTRFS_LABEL_SIZE - 1] = 0;
Anand Jain6cf86a002016-02-13 10:01:29 +08001309
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001310 return disk_super;
Anand Jain6cf86a002016-02-13 10:01:29 +08001311}
1312
Anand Jain228a73a2019-01-04 13:31:54 +08001313int btrfs_forget_devices(const char *path)
1314{
1315 int ret;
1316
1317 mutex_lock(&uuid_mutex);
1318 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1319 mutex_unlock(&uuid_mutex);
1320
1321 return ret;
1322}
1323
David Sterba6f60cbd2013-02-15 11:31:02 -07001324/*
1325 * Look for a btrfs signature on a device. This may be called out of the mount path
1326 * and we are not allowed to call set_blocksize during the scan. The superblock
1327 * is read via pagecache
1328 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001329struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1330 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001331{
1332 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001333 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001334 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001335 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001336 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001337
David Sterba899f9302018-06-19 16:37:36 +02001338 lockdep_assert_held(&uuid_mutex);
1339
David Sterba6f60cbd2013-02-15 11:31:02 -07001340 /*
1341 * we would like to check all the supers, but that would make
1342 * a btrfs mount succeed after a mkfs from a different FS.
1343 * So, we need to add a special mount option to scan for
1344 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1345 */
1346 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001347 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001348
1349 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001350 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001351 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001352
Nikolay Borisovb335eab2020-04-15 15:53:46 +03001353 disk_super = btrfs_read_disk_super(bdev, bytenr);
1354 if (IS_ERR(disk_super)) {
1355 device = ERR_CAST(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001356 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001357 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001358
Anand Jain4306a972018-05-29 12:28:37 +08001359 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001360 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001361 if (new_device_added)
1362 btrfs_free_stale_devices(path, device);
1363 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001364
Johannes Thumshirn8f323802020-02-14 00:24:32 +09001365 btrfs_release_disk_super(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001366
1367error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001368 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001369
Gu Jinxiang36350e92018-07-12 14:23:16 +08001370 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001371}
Chris Mason0b86a832008-03-24 15:01:56 -04001372
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001373/*
1374 * Try to find a chunk that intersects [start, start + len] range and when one
1375 * such is found, record the end of it in *start
1376 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001377static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1378 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001379{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001380 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001381
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001382 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001383
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001384 if (!find_first_extent_bit(&device->alloc_state, *start,
1385 &physical_start, &physical_end,
1386 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001387
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001388 if (in_range(physical_start, *start, len) ||
1389 in_range(*start, physical_start,
1390 physical_end - physical_start)) {
1391 *start = physical_end + 1;
1392 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001393 }
1394 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001395 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001396}
1397
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001398static u64 dev_extent_search_start(struct btrfs_device *device, u64 start)
1399{
1400 switch (device->fs_devices->chunk_alloc_policy) {
1401 case BTRFS_CHUNK_ALLOC_REGULAR:
1402 /*
1403 * We don't want to overwrite the superblock on the drive nor
1404 * any area used by the boot loader (grub for example), so we
1405 * make sure to start at an offset of at least 1MB.
1406 */
1407 return max_t(u64, start, SZ_1M);
1408 default:
1409 BUG();
1410 }
1411}
1412
1413/**
1414 * dev_extent_hole_check - check if specified hole is suitable for allocation
1415 * @device: the device which we have the hole
1416 * @hole_start: starting position of the hole
1417 * @hole_size: the size of the hole
1418 * @num_bytes: the size of the free space that we need
1419 *
1420 * This function may modify @hole_start and @hole_end to reflect the suitable
1421 * position for allocation. Returns 1 if hole position is updated, 0 otherwise.
1422 */
1423static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
1424 u64 *hole_size, u64 num_bytes)
1425{
1426 bool changed = false;
1427 u64 hole_end = *hole_start + *hole_size;
1428
1429 /*
1430 * Check before we set max_hole_start, otherwise we could end up
1431 * sending back this offset anyway.
1432 */
1433 if (contains_pending_extent(device, hole_start, *hole_size)) {
1434 if (hole_end >= *hole_start)
1435 *hole_size = hole_end - *hole_start;
1436 else
1437 *hole_size = 0;
1438 changed = true;
1439 }
1440
1441 switch (device->fs_devices->chunk_alloc_policy) {
1442 case BTRFS_CHUNK_ALLOC_REGULAR:
1443 /* No extra check */
1444 break;
1445 default:
1446 BUG();
1447 }
1448
1449 return changed;
1450}
Josef Bacik6df9a952013-06-27 13:22:46 -04001451
Chris Mason0b86a832008-03-24 15:01:56 -04001452/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001453 * find_free_dev_extent_start - find free space in the specified device
1454 * @device: the device which we search the free space in
1455 * @num_bytes: the size of the free space that we need
1456 * @search_start: the position from which to begin the search
1457 * @start: store the start of the free space.
1458 * @len: the size of the free space. that we find, or the size
1459 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001460 *
Chris Mason0b86a832008-03-24 15:01:56 -04001461 * this uses a pretty simple search, the expectation is that it is
1462 * called very infrequently and that a given device has a small number
1463 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001464 *
1465 * @start is used to store the start of the free space if we find. But if we
1466 * don't find suitable free space, it will be used to store the start position
1467 * of the max free space.
1468 *
1469 * @len is used to store the size of the free space that we find.
1470 * But if we don't find suitable free space, it is used to store the size of
1471 * the max free space.
Qu Wenruo135da972019-07-19 14:51:42 +08001472 *
1473 * NOTE: This function will search *commit* root of device tree, and does extra
1474 * check to ensure dev extents are not double allocated.
1475 * This makes the function safe to allocate dev extents but may not report
1476 * correct usable device space, as device extent freed in current transaction
1477 * is not reported as avaiable.
Chris Mason0b86a832008-03-24 15:01:56 -04001478 */
Qu Wenruo9e3246a2019-07-19 14:51:41 +08001479static int find_free_dev_extent_start(struct btrfs_device *device,
1480 u64 num_bytes, u64 search_start, u64 *start,
1481 u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001482{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001483 struct btrfs_fs_info *fs_info = device->fs_info;
1484 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001485 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001486 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001487 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001488 u64 hole_size;
1489 u64 max_hole_start;
1490 u64 max_hole_size;
1491 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001492 u64 search_end = device->total_bytes;
1493 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001494 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001495 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001496
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001497 search_start = dev_extent_search_start(device, search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001498
Josef Bacik6df9a952013-06-27 13:22:46 -04001499 path = btrfs_alloc_path();
1500 if (!path)
1501 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001502
Miao Xie7bfc8372011-01-05 10:07:26 +00001503 max_hole_start = search_start;
1504 max_hole_size = 0;
1505
Zhao Leif2ab7612015-02-16 18:52:17 +08001506again:
Anand Jain401e29c2017-12-04 12:54:55 +08001507 if (search_start >= search_end ||
1508 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001509 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001510 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001511 }
1512
David Sterbae4058b52015-11-27 16:31:35 +01001513 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001514 path->search_commit_root = 1;
1515 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001516
Chris Mason0b86a832008-03-24 15:01:56 -04001517 key.objectid = device->devid;
1518 key.offset = search_start;
1519 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001520
Li Zefan125ccb02011-12-08 15:07:24 +08001521 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001522 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001523 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001524 if (ret > 0) {
1525 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1526 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001527 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001528 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001529
Chris Mason0b86a832008-03-24 15:01:56 -04001530 while (1) {
1531 l = path->nodes[0];
1532 slot = path->slots[0];
1533 if (slot >= btrfs_header_nritems(l)) {
1534 ret = btrfs_next_leaf(root, path);
1535 if (ret == 0)
1536 continue;
1537 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001538 goto out;
1539
1540 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001541 }
1542 btrfs_item_key_to_cpu(l, &key, slot);
1543
1544 if (key.objectid < device->devid)
1545 goto next;
1546
1547 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001548 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001549
David Sterba962a2982014-06-04 18:41:45 +02001550 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001551 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001552
Miao Xie7bfc8372011-01-05 10:07:26 +00001553 if (key.offset > search_start) {
1554 hole_size = key.offset - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001555 dev_extent_hole_check(device, &search_start, &hole_size,
1556 num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04001557
Miao Xie7bfc8372011-01-05 10:07:26 +00001558 if (hole_size > max_hole_size) {
1559 max_hole_start = search_start;
1560 max_hole_size = hole_size;
1561 }
1562
1563 /*
1564 * If this free space is greater than which we need,
1565 * it must be the max free space that we have found
1566 * until now, so max_hole_start must point to the start
1567 * of this free space and the length of this free space
1568 * is stored in max_hole_size. Thus, we return
1569 * max_hole_start and max_hole_size and go back to the
1570 * caller.
1571 */
1572 if (hole_size >= num_bytes) {
1573 ret = 0;
1574 goto out;
1575 }
1576 }
1577
Chris Mason0b86a832008-03-24 15:01:56 -04001578 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001579 extent_end = key.offset + btrfs_dev_extent_length(l,
1580 dev_extent);
1581 if (extent_end > search_start)
1582 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001583next:
1584 path->slots[0]++;
1585 cond_resched();
1586 }
Chris Mason0b86a832008-03-24 15:01:56 -04001587
liubo38c01b92011-08-02 02:39:03 +00001588 /*
1589 * At this point, search_start should be the end of
1590 * allocated dev extents, and when shrinking the device,
1591 * search_end may be smaller than search_start.
1592 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001593 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001594 hole_size = search_end - search_start;
Naohiro Aota3b4ffa42020-02-25 12:56:09 +09001595 if (dev_extent_hole_check(device, &search_start, &hole_size,
1596 num_bytes)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001597 btrfs_release_path(path);
1598 goto again;
1599 }
Chris Mason0b86a832008-03-24 15:01:56 -04001600
Zhao Leif2ab7612015-02-16 18:52:17 +08001601 if (hole_size > max_hole_size) {
1602 max_hole_start = search_start;
1603 max_hole_size = hole_size;
1604 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001605 }
1606
Miao Xie7bfc8372011-01-05 10:07:26 +00001607 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001608 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001609 ret = -ENOSPC;
1610 else
1611 ret = 0;
1612
1613out:
Yan Zheng2b820322008-11-17 21:11:30 -05001614 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001615 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001616 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001617 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001618 return ret;
1619}
1620
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001621int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001622 u64 *start, u64 *len)
1623{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001624 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001625 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001626}
1627
Christoph Hellwigb2950862008-12-02 09:54:17 -05001628static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001629 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001630 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001631{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001632 struct btrfs_fs_info *fs_info = device->fs_info;
1633 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001634 int ret;
1635 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001636 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001637 struct btrfs_key found_key;
1638 struct extent_buffer *leaf = NULL;
1639 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001640
1641 path = btrfs_alloc_path();
1642 if (!path)
1643 return -ENOMEM;
1644
1645 key.objectid = device->devid;
1646 key.offset = start;
1647 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001648again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001649 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001650 if (ret > 0) {
1651 ret = btrfs_previous_item(root, path, key.objectid,
1652 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001653 if (ret)
1654 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001655 leaf = path->nodes[0];
1656 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1657 extent = btrfs_item_ptr(leaf, path->slots[0],
1658 struct btrfs_dev_extent);
1659 BUG_ON(found_key.offset > start || found_key.offset +
1660 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001661 key = found_key;
1662 btrfs_release_path(path);
1663 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001664 } else if (ret == 0) {
1665 leaf = path->nodes[0];
1666 extent = btrfs_item_ptr(leaf, path->slots[0],
1667 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001668 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001669 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001670 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001671 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001672
Miao Xie2196d6e2014-09-03 21:35:41 +08001673 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1674
Chris Mason8f18cf12008-04-25 16:53:30 -04001675 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001676 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001677 btrfs_handle_fs_error(fs_info, ret,
1678 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001679 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001680 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001681 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001682out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001683 btrfs_free_path(path);
1684 return ret;
1685}
1686
Eric Sandeen48a3b632013-04-25 20:41:01 +00001687static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1688 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001689 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001690{
1691 int ret;
1692 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001693 struct btrfs_fs_info *fs_info = device->fs_info;
1694 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001695 struct btrfs_dev_extent *extent;
1696 struct extent_buffer *leaf;
1697 struct btrfs_key key;
1698
Anand Jaine12c9622017-12-04 12:54:53 +08001699 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001700 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001701 path = btrfs_alloc_path();
1702 if (!path)
1703 return -ENOMEM;
1704
Chris Mason0b86a832008-03-24 15:01:56 -04001705 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001706 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001707 key.type = BTRFS_DEV_EXTENT_KEY;
1708 ret = btrfs_insert_empty_item(trans, root, path, &key,
1709 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001710 if (ret)
1711 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001712
1713 leaf = path->nodes[0];
1714 extent = btrfs_item_ptr(leaf, path->slots[0],
1715 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001716 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1717 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001718 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1719 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001720 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1721
Chris Mason0b86a832008-03-24 15:01:56 -04001722 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1723 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001724out:
Chris Mason0b86a832008-03-24 15:01:56 -04001725 btrfs_free_path(path);
1726 return ret;
1727}
1728
Josef Bacik6df9a952013-06-27 13:22:46 -04001729static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001730{
Josef Bacik6df9a952013-06-27 13:22:46 -04001731 struct extent_map_tree *em_tree;
1732 struct extent_map *em;
1733 struct rb_node *n;
1734 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001735
David Sterbac8bf1b62019-05-17 11:43:17 +02001736 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001737 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001738 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001739 if (n) {
1740 em = rb_entry(n, struct extent_map, rb_node);
1741 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001742 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001743 read_unlock(&em_tree->lock);
1744
Chris Mason0b86a832008-03-24 15:01:56 -04001745 return ret;
1746}
1747
Ilya Dryomov53f10652013-08-12 14:33:01 +03001748static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1749 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001750{
1751 int ret;
1752 struct btrfs_key key;
1753 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001754 struct btrfs_path *path;
1755
Yan Zheng2b820322008-11-17 21:11:30 -05001756 path = btrfs_alloc_path();
1757 if (!path)
1758 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001759
1760 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1761 key.type = BTRFS_DEV_ITEM_KEY;
1762 key.offset = (u64)-1;
1763
Ilya Dryomov53f10652013-08-12 14:33:01 +03001764 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001765 if (ret < 0)
1766 goto error;
1767
Anand Jaina06dee4d2019-08-27 15:40:44 +08001768 if (ret == 0) {
1769 /* Corruption */
1770 btrfs_err(fs_info, "corrupted chunk tree devid -1 matched");
1771 ret = -EUCLEAN;
1772 goto error;
1773 }
Chris Mason0b86a832008-03-24 15:01:56 -04001774
Ilya Dryomov53f10652013-08-12 14:33:01 +03001775 ret = btrfs_previous_item(fs_info->chunk_root, path,
1776 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001777 BTRFS_DEV_ITEM_KEY);
1778 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001779 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001780 } else {
1781 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1782 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001783 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001784 }
1785 ret = 0;
1786error:
Yan Zheng2b820322008-11-17 21:11:30 -05001787 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001788 return ret;
1789}
1790
1791/*
1792 * the device information is stored in the chunk root
1793 * the btrfs_device struct should be fully filled in
1794 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001795static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001796 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001797{
1798 int ret;
1799 struct btrfs_path *path;
1800 struct btrfs_dev_item *dev_item;
1801 struct extent_buffer *leaf;
1802 struct btrfs_key key;
1803 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001804
Chris Mason0b86a832008-03-24 15:01:56 -04001805 path = btrfs_alloc_path();
1806 if (!path)
1807 return -ENOMEM;
1808
Chris Mason0b86a832008-03-24 15:01:56 -04001809 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1810 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001811 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001812
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001813 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1814 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001815 if (ret)
1816 goto out;
1817
1818 leaf = path->nodes[0];
1819 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1820
1821 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001822 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001823 btrfs_set_device_type(leaf, dev_item, device->type);
1824 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1825 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1826 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001827 btrfs_set_device_total_bytes(leaf, dev_item,
1828 btrfs_device_get_disk_total_bytes(device));
1829 btrfs_set_device_bytes_used(leaf, dev_item,
1830 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001831 btrfs_set_device_group(leaf, dev_item, 0);
1832 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1833 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001834 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001835
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001836 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001837 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001838 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001839 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1840 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001841 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001842
Yan Zheng2b820322008-11-17 21:11:30 -05001843 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001844out:
1845 btrfs_free_path(path);
1846 return ret;
1847}
Chris Mason8f18cf12008-04-25 16:53:30 -04001848
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001849/*
1850 * Function to update ctime/mtime for a given device path.
1851 * Mainly used for ctime/mtime based probe like libblkid.
1852 */
David Sterbada353f62017-02-14 17:55:53 +01001853static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001854{
1855 struct file *filp;
1856
1857 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001858 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001859 return;
1860 file_update_time(filp);
1861 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001862}
1863
David Sterbaf331a952019-03-20 16:31:53 +01001864static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001865{
David Sterbaf331a952019-03-20 16:31:53 +01001866 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001867 int ret;
1868 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001869 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001870 struct btrfs_trans_handle *trans;
1871
Chris Masona061fc82008-05-07 11:43:44 -04001872 path = btrfs_alloc_path();
1873 if (!path)
1874 return -ENOMEM;
1875
Yan, Zhenga22285a2010-05-16 10:48:46 -04001876 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001877 if (IS_ERR(trans)) {
1878 btrfs_free_path(path);
1879 return PTR_ERR(trans);
1880 }
Chris Masona061fc82008-05-07 11:43:44 -04001881 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1882 key.type = BTRFS_DEV_ITEM_KEY;
1883 key.offset = device->devid;
1884
1885 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001886 if (ret) {
1887 if (ret > 0)
1888 ret = -ENOENT;
1889 btrfs_abort_transaction(trans, ret);
1890 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001891 goto out;
1892 }
1893
1894 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001895 if (ret) {
1896 btrfs_abort_transaction(trans, ret);
1897 btrfs_end_transaction(trans);
1898 }
1899
Chris Masona061fc82008-05-07 11:43:44 -04001900out:
1901 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001902 if (!ret)
1903 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001904 return ret;
1905}
1906
David Sterba3cc31a02016-02-15 16:00:26 +01001907/*
1908 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1909 * filesystem. It's up to the caller to adjust that number regarding eg. device
1910 * replace.
1911 */
1912static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1913 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001914{
Chris Masona061fc82008-05-07 11:43:44 -04001915 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001916 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001917 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001918
Miao Xiede98ced2013-01-29 10:13:12 +00001919 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001920 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001921
Anand Jainbd45ffb2016-02-13 10:01:34 +08001922 all_avail = fs_info->avail_data_alloc_bits |
1923 fs_info->avail_system_alloc_bits |
1924 fs_info->avail_metadata_alloc_bits;
1925 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001926
David Sterba418775a2016-02-15 16:28:14 +01001927 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001928 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001929 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001930
David Sterba418775a2016-02-15 16:28:14 +01001931 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001932 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001933
David Sterba418775a2016-02-15 16:28:14 +01001934 if (ret)
1935 return ret;
1936 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001937 }
1938
1939 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001940}
1941
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001942static struct btrfs_device * btrfs_find_next_active_device(
1943 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001944{
1945 struct btrfs_device *next_device;
1946
1947 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1948 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001949 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1950 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001951 return next_device;
1952 }
1953
1954 return NULL;
1955}
1956
1957/*
1958 * Helper function to check if the given device is part of s_bdev / latest_bdev
1959 * and replace it with the provided or the next active device, in the context
1960 * where this function called, there should be always be another device (or
1961 * this_dev) which is active.
1962 */
David Sterbab105e922019-10-01 19:57:35 +02001963void __cold btrfs_assign_next_active_device(struct btrfs_device *device,
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001964 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08001965{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001966 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08001967 struct btrfs_device *next_device;
1968
1969 if (this_dev)
1970 next_device = this_dev;
1971 else
1972 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1973 device);
1974 ASSERT(next_device);
1975
1976 if (fs_info->sb->s_bdev &&
1977 (fs_info->sb->s_bdev == device->bdev))
1978 fs_info->sb->s_bdev = next_device->bdev;
1979
1980 if (fs_info->fs_devices->latest_bdev == device->bdev)
1981 fs_info->fs_devices->latest_bdev = next_device->bdev;
1982}
1983
Anand Jain1da73962018-08-10 13:53:21 +08001984/*
1985 * Return btrfs_fs_devices::num_devices excluding the device that's being
1986 * currently replaced.
1987 */
1988static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1989{
1990 u64 num_devices = fs_info->fs_devices->num_devices;
1991
David Sterbacb5583d2018-09-07 16:11:23 +02001992 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001993 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1994 ASSERT(num_devices > 1);
1995 num_devices--;
1996 }
David Sterbacb5583d2018-09-07 16:11:23 +02001997 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08001998
1999 return num_devices;
2000}
2001
Josef Bacik313b0852020-08-20 11:18:26 -04002002void btrfs_scratch_superblocks(struct btrfs_fs_info *fs_info,
2003 struct block_device *bdev,
2004 const char *device_path)
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002005{
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002006 struct btrfs_super_block *disk_super;
2007 int copy_num;
2008
2009 if (!bdev)
2010 return;
2011
2012 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX; copy_num++) {
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002013 struct page *page;
2014 int ret;
2015
2016 disk_super = btrfs_read_dev_one_super(bdev, copy_num);
2017 if (IS_ERR(disk_super))
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002018 continue;
2019
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002020 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002021
2022 page = virt_to_page(disk_super);
2023 set_page_dirty(page);
2024 lock_page(page);
2025 /* write_on_page() unlocks the page */
2026 ret = write_one_page(page);
2027 if (ret)
2028 btrfs_warn(fs_info,
2029 "error clearing superblock number %d (%d)",
2030 copy_num, ret);
2031 btrfs_release_disk_super(disk_super);
2032
Johannes Thumshirn6fbceb92020-02-14 00:24:31 +09002033 }
2034
2035 /* Notify udev that device has changed */
2036 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
2037
2038 /* Update ctime/mtime for device path for libblkid */
2039 update_dev_time(device_path);
2040}
2041
David Sterbada353f62017-02-14 17:55:53 +01002042int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2043 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002044{
2045 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002046 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002047 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002048 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002049 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002050
2051 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002052
Anand Jain1da73962018-08-10 13:53:21 +08002053 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002055 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002056 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002057 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002058
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002059 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2060
2061 if (IS_ERR(device)) {
2062 if (PTR_ERR(device) == -ENOENT &&
2063 strcmp(device_path, "missing") == 0)
2064 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2065 else
2066 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002067 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002068 }
Yan Zheng2b820322008-11-17 21:11:30 -05002069
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002070 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2071 btrfs_warn_in_rcu(fs_info,
2072 "cannot remove device %s (devid %llu) due to active swapfile",
2073 rcu_str_deref(device->name), device->devid);
2074 ret = -ETXTBSY;
2075 goto out;
2076 }
2077
Anand Jain401e29c2017-12-04 12:54:55 +08002078 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002079 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002080 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002081 }
2082
Anand Jainebbede42017-12-04 12:54:52 +08002083 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2084 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002085 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002086 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002087 }
2088
Anand Jainebbede42017-12-04 12:54:52 +08002089 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002090 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002091 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002092 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002093 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002094 }
Chris Masona061fc82008-05-07 11:43:44 -04002095
Carey Underwoodd7901552013-03-04 16:37:06 -06002096 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002097 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002098 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002099 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002100 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002101
Stefan Behrens63a212a2012-11-05 18:29:28 +01002102 /*
2103 * TODO: the superblock still includes this device in its num_devices
2104 * counter although write_all_supers() is not locked out. This
2105 * could give a filesystem state which requires a degraded mount.
2106 */
David Sterbaf331a952019-03-20 16:31:53 +01002107 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002108 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002109 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002110
Anand Jaine12c9622017-12-04 12:54:53 +08002111 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002112 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002113
2114 /*
2115 * the device list mutex makes sure that we don't change
2116 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002117 * the device supers. Whoever is writing all supers, should
2118 * lock the device list mutex before getting the number of
2119 * devices in the super block (super_copy). Conversely,
2120 * whoever updates the number of devices in the super block
2121 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002122 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002123
Anand Jain41a52a02018-04-12 10:29:31 +08002124 /*
2125 * In normal cases the cur_devices == fs_devices. But in case
2126 * of deleting a seed device, the cur_devices should point to
2127 * its own fs_devices listed under the fs_devices->seed.
2128 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002129 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002130 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002131 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002132
Anand Jain41a52a02018-04-12 10:29:31 +08002133 cur_devices->num_devices--;
2134 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002135 /* Update total_devices of the parent fs_devices if it's seed */
2136 if (cur_devices != fs_devices)
2137 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002138
Anand Jaine6e674b2017-12-04 12:54:54 +08002139 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002140 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002141
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002142 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002143
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002144 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002145 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002146 /* remove sysfs entry */
Anand Jainf3cd2c52020-02-12 17:28:13 +08002147 btrfs_sysfs_remove_devices_dir(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002148 }
Anand Jain99994cd2014-06-03 11:36:00 +08002149
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002150 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2151 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002152 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002153
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002154 /*
2155 * at this point, the device is zero sized and detached from
2156 * the devices list. All that's left is to zero out the old
2157 * supers and free the device.
2158 */
Anand Jainebbede42017-12-04 12:54:52 +08002159 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002160 btrfs_scratch_superblocks(fs_info, device->bdev,
2161 device->name->str);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002162
2163 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002164 synchronize_rcu();
2165 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002166
Xiao Guangrong1f781602011-04-20 10:09:16 +00002167 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002168 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002169 if (fs_devices->seed == cur_devices) {
2170 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002171 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002172 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002173 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002174 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002175 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002176 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002177 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002178 }
2179
Chris Masona061fc82008-05-07 11:43:44 -04002180out:
2181 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002182 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002183
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002184error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002185 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002186 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002187 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002188 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002189 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002190 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002191 }
Anand Jain24fc5722016-02-13 10:01:36 +08002192 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002193}
2194
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002195void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002196{
Anand Jaind51908c2014-08-13 14:24:19 +08002197 struct btrfs_fs_devices *fs_devices;
2198
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002199 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002200
Anand Jain25e8e912014-08-20 10:56:56 +08002201 /*
2202 * in case of fs with no seed, srcdev->fs_devices will point
2203 * to fs_devices of fs_info. However when the dev being replaced is
2204 * a seed dev it will point to the seed's local fs_devices. In short
2205 * srcdev will have its correct fs_devices in both the cases.
2206 */
2207 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002208
Stefan Behrense93c89c2012-11-05 17:33:06 +01002209 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002210 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002211 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002212 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002213 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002214
Anand Jainebbede42017-12-04 12:54:52 +08002215 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002216 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002217
Miao Xie82372bc2014-09-03 21:35:44 +08002218 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002219 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002220}
2221
David Sterba65237ee2019-03-20 16:34:54 +01002222void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002223{
David Sterba65237ee2019-03-20 16:34:54 +01002224 struct btrfs_fs_info *fs_info = srcdev->fs_info;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002225 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002226
Josef Bacika466c852020-08-20 11:18:27 -04002227 mutex_lock(&uuid_mutex);
2228
Anand Jain14238812016-07-22 06:04:53 +08002229 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002230 synchronize_rcu();
2231 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002232
Anand Jain94d5f0c2014-08-13 14:24:22 +08002233 /* if this is no devs we rather delete the fs_devices */
2234 if (!fs_devices->num_devices) {
2235 struct btrfs_fs_devices *tmp_fs_devices;
2236
Anand Jain6dd38f82017-10-17 06:53:50 +08002237 /*
2238 * On a mounted FS, num_devices can't be zero unless it's a
2239 * seed. In case of a seed device being replaced, the replace
2240 * target added to the sprout FS, so there will be no more
2241 * device left under the seed FS.
2242 */
2243 ASSERT(fs_devices->seeding);
2244
Anand Jain94d5f0c2014-08-13 14:24:22 +08002245 tmp_fs_devices = fs_info->fs_devices;
2246 while (tmp_fs_devices) {
2247 if (tmp_fs_devices->seed == fs_devices) {
2248 tmp_fs_devices->seed = fs_devices->seed;
2249 break;
2250 }
2251 tmp_fs_devices = tmp_fs_devices->seed;
2252 }
2253 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002254 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002255 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002256 }
Josef Bacika466c852020-08-20 11:18:27 -04002257 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002258}
2259
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002260void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002261{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002262 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002263
Anand Jaind9a071f2018-04-12 10:29:38 +08002264 mutex_lock(&fs_devices->device_list_mutex);
2265
Anand Jainf3cd2c52020-02-12 17:28:13 +08002266 btrfs_sysfs_remove_devices_dir(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002267
Anand Jain779bf3fe2016-04-18 16:51:23 +08002268 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002269 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002270
Anand Jaind9a071f2018-04-12 10:29:38 +08002271 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002272
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002273 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002274
Stefan Behrense93c89c2012-11-05 17:33:06 +01002275 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002276
Anand Jaind9a071f2018-04-12 10:29:38 +08002277 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002278
2279 /*
2280 * The update_dev_time() with in btrfs_scratch_superblocks()
2281 * may lead to a call to btrfs_show_devname() which will try
2282 * to hold device_list_mutex. And here this device
2283 * is already out of device list, so we don't have to hold
2284 * the device_list_mutex lock.
2285 */
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002286 btrfs_scratch_superblocks(tgtdev->fs_info, tgtdev->bdev,
2287 tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002288
2289 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002290 synchronize_rcu();
2291 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002292}
2293
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002294static struct btrfs_device *btrfs_find_device_by_path(
2295 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002296{
2297 int ret = 0;
2298 struct btrfs_super_block *disk_super;
2299 u64 devid;
2300 u8 *dev_uuid;
2301 struct block_device *bdev;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002302 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002303
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002304 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002305 fs_info->bdev_holder, 0, &bdev, &disk_super);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002306 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002307 return ERR_PTR(ret);
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002308
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002309 devid = btrfs_stack_device_id(&disk_super->dev_item);
2310 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002311 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002312 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002313 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002314 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002315 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002316 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002317
Johannes Thumshirn8f323802020-02-14 00:24:32 +09002318 btrfs_release_disk_super(disk_super);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002319 if (!device)
2320 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002321 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002322 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002323}
2324
Yan Zheng2b820322008-11-17 21:11:30 -05002325/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002326 * Lookup a device given by device id, or the path if the id is 0.
2327 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002328struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002329 struct btrfs_fs_info *fs_info, u64 devid,
2330 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002331{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002332 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002333
David Sterba5c5c0df2016-02-15 16:39:55 +01002334 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002335 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002336 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002337 if (!device)
2338 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002339 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002340 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002341
2342 if (!device_path || !device_path[0])
2343 return ERR_PTR(-EINVAL);
2344
2345 if (strcmp(device_path, "missing") == 0) {
2346 /* Find first missing device */
2347 list_for_each_entry(device, &fs_info->fs_devices->devices,
2348 dev_list) {
2349 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2350 &device->dev_state) && !device->bdev)
2351 return device;
2352 }
2353 return ERR_PTR(-ENOENT);
2354 }
2355
2356 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002357}
2358
Yan Zheng2b820322008-11-17 21:11:30 -05002359/*
2360 * does all the dirty work required for changing file system's UUID.
2361 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002362static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002364 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002365 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002366 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002367 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002368 struct btrfs_device *device;
2369 u64 super_flags;
2370
David Sterbaa32bf9a2018-03-16 02:21:22 +01002371 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002372 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002373 return -EINVAL;
2374
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002375 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002376 if (IS_ERR(seed_devices))
2377 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002378
Yan Zhenge4404d62008-12-12 10:03:26 -05002379 old_devices = clone_fs_devices(fs_devices);
2380 if (IS_ERR(old_devices)) {
2381 kfree(seed_devices);
2382 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002383 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002384
Anand Jainc4babc52018-04-12 10:29:25 +08002385 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002386
Yan Zhenge4404d62008-12-12 10:03:26 -05002387 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2388 seed_devices->opened = 1;
2389 INIT_LIST_HEAD(&seed_devices->devices);
2390 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002391 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002392
Anand Jain321a4bf2018-07-16 22:58:09 +08002393 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002394 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2395 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002396 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002397 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002398
David Sterba34441362016-10-04 19:34:27 +02002399 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002400 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002401 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002402
Johannes Thumshirn0395d842019-11-13 11:27:27 +01002403 fs_devices->seeding = false;
Yan Zheng2b820322008-11-17 21:11:30 -05002404 fs_devices->num_devices = 0;
2405 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002406 fs_devices->missing_devices = 0;
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002407 fs_devices->rotating = false;
Yan Zhenge4404d62008-12-12 10:03:26 -05002408 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002409
2410 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002411 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002412 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002413 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002414
Yan Zheng2b820322008-11-17 21:11:30 -05002415 super_flags = btrfs_super_flags(disk_super) &
2416 ~BTRFS_SUPER_FLAG_SEEDING;
2417 btrfs_set_super_flags(disk_super, super_flags);
2418
2419 return 0;
2420}
2421
2422/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002423 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002424 */
David Sterba5c4666292019-03-20 16:36:39 +01002425static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002426{
David Sterba5c4666292019-03-20 16:36:39 +01002427 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002428 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002429 struct btrfs_path *path;
2430 struct extent_buffer *leaf;
2431 struct btrfs_dev_item *dev_item;
2432 struct btrfs_device *device;
2433 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002434 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002435 u8 dev_uuid[BTRFS_UUID_SIZE];
2436 u64 devid;
2437 int ret;
2438
2439 path = btrfs_alloc_path();
2440 if (!path)
2441 return -ENOMEM;
2442
Yan Zheng2b820322008-11-17 21:11:30 -05002443 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2444 key.offset = 0;
2445 key.type = BTRFS_DEV_ITEM_KEY;
2446
2447 while (1) {
2448 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2449 if (ret < 0)
2450 goto error;
2451
2452 leaf = path->nodes[0];
2453next_slot:
2454 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2455 ret = btrfs_next_leaf(root, path);
2456 if (ret > 0)
2457 break;
2458 if (ret < 0)
2459 goto error;
2460 leaf = path->nodes[0];
2461 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002462 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002463 continue;
2464 }
2465
2466 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2467 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2468 key.type != BTRFS_DEV_ITEM_KEY)
2469 break;
2470
2471 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2472 struct btrfs_dev_item);
2473 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002474 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002475 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002476 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002477 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002478 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002479 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002480 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002481
2482 if (device->fs_devices->seeding) {
2483 btrfs_set_device_generation(leaf, dev_item,
2484 device->generation);
2485 btrfs_mark_buffer_dirty(leaf);
2486 }
2487
2488 path->slots[0]++;
2489 goto next_slot;
2490 }
2491 ret = 0;
2492error:
2493 btrfs_free_path(path);
2494 return ret;
2495}
2496
David Sterbada353f62017-02-14 17:55:53 +01002497int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002498{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002499 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002500 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002501 struct btrfs_trans_handle *trans;
2502 struct btrfs_device *device;
2503 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002504 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002505 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002506 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002507 u64 orig_super_total_bytes;
2508 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002509 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002510 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002511 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002512
Anand Jain5da54bc2018-07-03 13:14:50 +08002513 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002514 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002515
Li Zefana5d163332011-12-07 20:08:40 -05002516 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002518 if (IS_ERR(bdev))
2519 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002520
Anand Jain5da54bc2018-07-03 13:14:50 +08002521 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002522 seeding_dev = 1;
2523 down_write(&sb->s_umount);
2524 mutex_lock(&uuid_mutex);
2525 }
2526
Chris Mason8c8bee12008-09-29 11:19:10 -04002527 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002528
Anand Jain5da54bc2018-07-03 13:14:50 +08002529 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002530 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002531 if (device->bdev == bdev) {
2532 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002533 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002534 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002535 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002536 }
2537 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002538 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002540 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002541 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002542 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002543 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002544 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002545 }
2546
David Sterba78f2c9e2016-02-11 14:25:38 +01002547 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002548 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002549 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002550 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002551 }
Josef Bacik606686e2012-06-04 14:03:51 -04002552 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002553
Yan, Zhenga22285a2010-05-16 10:48:46 -04002554 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002555 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002556 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002557 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002558 }
2559
Josef Bacikd5e20032011-08-04 14:52:27 +00002560 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002561 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002562 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002563 device->io_width = fs_info->sectorsize;
2564 device->io_align = fs_info->sectorsize;
2565 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002566 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2567 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002568 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002569 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002570 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002571 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002572 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002573 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002574 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002575 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002576 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002577
Yan Zheng2b820322008-11-17 21:11:30 -05002578 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002579 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002580 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002581 if (ret) {
2582 btrfs_abort_transaction(trans, ret);
2583 goto error_trans;
2584 }
Yan Zheng2b820322008-11-17 21:11:30 -05002585 }
2586
Anand Jain5da54bc2018-07-03 13:14:50 +08002587 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002588
Anand Jain5da54bc2018-07-03 13:14:50 +08002589 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002590 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002591 list_add_rcu(&device->dev_list, &fs_devices->devices);
2592 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2593 fs_devices->num_devices++;
2594 fs_devices->open_devices++;
2595 fs_devices->rw_devices++;
2596 fs_devices->total_devices++;
2597 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002598
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002599 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002600
Anand Jaine884f4f2017-04-04 18:40:19 +08002601 if (!blk_queue_nonrot(q))
Johannes Thumshirn7f0432d2019-11-13 11:27:28 +01002602 fs_devices->rotating = true;
Chris Masonc2898112009-06-10 09:51:32 -04002603
Naohiro Aota39379fa2018-07-27 09:04:55 +09002604 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002605 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002606 round_down(orig_super_total_bytes + device->total_bytes,
2607 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002608
Naohiro Aota39379fa2018-07-27 09:04:55 +09002609 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2610 btrfs_set_super_num_devices(fs_info->super_copy,
2611 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002612
2613 /* add sysfs device entry */
Anand Jainf3cd2c52020-02-12 17:28:13 +08002614 btrfs_sysfs_add_devices_dir(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002615
Miao Xie2196d6e2014-09-03 21:35:41 +08002616 /*
2617 * we've got more storage, clear any full flags on the space
2618 * infos
2619 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002620 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002621
David Sterba34441362016-10-04 19:34:27 +02002622 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002623 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002624
Yan Zheng2b820322008-11-17 21:11:30 -05002625 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002626 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002627 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002628 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002629 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002630 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002631 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002632 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002633 }
2634
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002635 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002636 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002637 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002638 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002639 }
2640
2641 if (seeding_dev) {
David Sterba5c4666292019-03-20 16:36:39 +01002642 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002643 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002644 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002645 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002646 }
Anand Jainb2373f22014-06-03 11:36:03 +08002647
David Sterbaf93c3992019-08-01 18:50:16 +02002648 btrfs_sysfs_update_sprout_fsid(fs_devices,
2649 fs_info->fs_devices->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05002650 }
2651
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002652 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002653
2654 if (seeding_dev) {
2655 mutex_unlock(&uuid_mutex);
2656 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002657 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002658
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002659 if (ret) /* transaction commit */
2660 return ret;
2661
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002662 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002663 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002664 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002665 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002666 trans = btrfs_attach_transaction(root);
2667 if (IS_ERR(trans)) {
2668 if (PTR_ERR(trans) == -ENOENT)
2669 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002670 ret = PTR_ERR(trans);
2671 trans = NULL;
2672 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002673 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002674 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002675 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002676
Anand Jain7f551d92020-05-05 02:58:26 +08002677 /*
2678 * Now that we have written a new super block to this device, check all
2679 * other fs_devices list if device_path alienates any other scanned
2680 * device.
2681 * We can ignore the return value as it typically returns -EINVAL and
2682 * only succeeds if the device was an alien.
2683 */
2684 btrfs_forget_devices(device_path);
2685
2686 /* Update ctime/mtime for blkid or udev */
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002687 update_dev_time(device_path);
Anand Jain7f551d92020-05-05 02:58:26 +08002688
Chris Mason788f20e2008-04-28 15:29:42 -04002689 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002690
Anand Jaind31c32f2017-09-28 14:51:10 +08002691error_sysfs:
Anand Jainf3cd2c52020-02-12 17:28:13 +08002692 btrfs_sysfs_remove_devices_dir(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002693 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2694 mutex_lock(&fs_info->chunk_mutex);
2695 list_del_rcu(&device->dev_list);
2696 list_del(&device->dev_alloc_list);
2697 fs_info->fs_devices->num_devices--;
2698 fs_info->fs_devices->open_devices--;
2699 fs_info->fs_devices->rw_devices--;
2700 fs_info->fs_devices->total_devices--;
2701 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2702 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2703 btrfs_set_super_total_bytes(fs_info->super_copy,
2704 orig_super_total_bytes);
2705 btrfs_set_super_num_devices(fs_info->super_copy,
2706 orig_super_num_devices);
2707 mutex_unlock(&fs_info->chunk_mutex);
2708 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002709error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002710 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002711 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002712 if (trans)
2713 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002714error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002715 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002716error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002717 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002718 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002719 mutex_unlock(&uuid_mutex);
2720 up_write(&sb->s_umount);
2721 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002722 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002723}
2724
Chris Masond3977122009-01-05 21:25:51 -05002725static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2726 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002727{
2728 int ret;
2729 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002730 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002731 struct btrfs_dev_item *dev_item;
2732 struct extent_buffer *leaf;
2733 struct btrfs_key key;
2734
Chris Mason0b86a832008-03-24 15:01:56 -04002735 path = btrfs_alloc_path();
2736 if (!path)
2737 return -ENOMEM;
2738
2739 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2740 key.type = BTRFS_DEV_ITEM_KEY;
2741 key.offset = device->devid;
2742
2743 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2744 if (ret < 0)
2745 goto out;
2746
2747 if (ret > 0) {
2748 ret = -ENOENT;
2749 goto out;
2750 }
2751
2752 leaf = path->nodes[0];
2753 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2754
2755 btrfs_set_device_id(leaf, dev_item, device->devid);
2756 btrfs_set_device_type(leaf, dev_item, device->type);
2757 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2758 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2759 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002760 btrfs_set_device_total_bytes(leaf, dev_item,
2761 btrfs_device_get_disk_total_bytes(device));
2762 btrfs_set_device_bytes_used(leaf, dev_item,
2763 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002764 btrfs_mark_buffer_dirty(leaf);
2765
2766out:
2767 btrfs_free_path(path);
2768 return ret;
2769}
2770
Miao Xie2196d6e2014-09-03 21:35:41 +08002771int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002772 struct btrfs_device *device, u64 new_size)
2773{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002774 struct btrfs_fs_info *fs_info = device->fs_info;
2775 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002776 u64 old_total;
2777 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002778
Anand Jainebbede42017-12-04 12:54:52 +08002779 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002780 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002781
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002782 new_size = round_down(new_size, fs_info->sectorsize);
2783
David Sterba34441362016-10-04 19:34:27 +02002784 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002785 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002786 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002787
Stefan Behrens63a212a2012-11-05 18:29:28 +01002788 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002789 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002790 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002791 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002792 }
Yan Zheng2b820322008-11-17 21:11:30 -05002793
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002794 btrfs_set_super_total_bytes(super_copy,
2795 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002796 device->fs_devices->total_rw_bytes += diff;
2797
Miao Xie7cc8e582014-09-03 21:35:38 +08002798 btrfs_device_set_total_bytes(device, new_size);
2799 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002800 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002801 if (list_empty(&device->post_commit_list))
2802 list_add_tail(&device->post_commit_list,
2803 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002804 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002805
Chris Mason8f18cf12008-04-25 16:53:30 -04002806 return btrfs_update_device(trans, device);
2807}
2808
Nikolay Borisovf4208792018-07-20 19:37:52 +03002809static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002810{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002811 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002812 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002813 int ret;
2814 struct btrfs_path *path;
2815 struct btrfs_key key;
2816
Chris Mason8f18cf12008-04-25 16:53:30 -04002817 path = btrfs_alloc_path();
2818 if (!path)
2819 return -ENOMEM;
2820
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002821 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002822 key.offset = chunk_offset;
2823 key.type = BTRFS_CHUNK_ITEM_KEY;
2824
2825 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002826 if (ret < 0)
2827 goto out;
2828 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002829 btrfs_handle_fs_error(fs_info, -ENOENT,
2830 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002831 ret = -ENOENT;
2832 goto out;
2833 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002834
2835 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002836 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002837 btrfs_handle_fs_error(fs_info, ret,
2838 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002839out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002840 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002841 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002842}
2843
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002844static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002845{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002846 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002847 struct btrfs_disk_key *disk_key;
2848 struct btrfs_chunk *chunk;
2849 u8 *ptr;
2850 int ret = 0;
2851 u32 num_stripes;
2852 u32 array_size;
2853 u32 len = 0;
2854 u32 cur;
2855 struct btrfs_key key;
2856
David Sterba34441362016-10-04 19:34:27 +02002857 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002858 array_size = btrfs_super_sys_array_size(super_copy);
2859
2860 ptr = super_copy->sys_chunk_array;
2861 cur = 0;
2862
2863 while (cur < array_size) {
2864 disk_key = (struct btrfs_disk_key *)ptr;
2865 btrfs_disk_key_to_cpu(&key, disk_key);
2866
2867 len = sizeof(*disk_key);
2868
2869 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2870 chunk = (struct btrfs_chunk *)(ptr + len);
2871 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2872 len += btrfs_chunk_item_size(num_stripes);
2873 } else {
2874 ret = -EIO;
2875 break;
2876 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002877 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002878 key.offset == chunk_offset) {
2879 memmove(ptr, ptr + len, array_size - (cur + len));
2880 array_size -= len;
2881 btrfs_set_super_sys_array_size(super_copy, array_size);
2882 } else {
2883 ptr += len;
2884 cur += len;
2885 }
2886 }
David Sterba34441362016-10-04 19:34:27 +02002887 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002888 return ret;
2889}
2890
Omar Sandoval60ca8422018-05-16 16:34:31 -07002891/*
2892 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2893 * @logical: Logical block offset in bytes.
2894 * @length: Length of extent in bytes.
2895 *
2896 * Return: Chunk mapping or ERR_PTR.
2897 */
2898struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2899 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002900{
2901 struct extent_map_tree *em_tree;
2902 struct extent_map *em;
2903
David Sterbac8bf1b62019-05-17 11:43:17 +02002904 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002905 read_lock(&em_tree->lock);
2906 em = lookup_extent_mapping(em_tree, logical, length);
2907 read_unlock(&em_tree->lock);
2908
2909 if (!em) {
2910 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2911 logical, length);
2912 return ERR_PTR(-EINVAL);
2913 }
2914
2915 if (em->start > logical || em->start + em->len < logical) {
2916 btrfs_crit(fs_info,
2917 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2918 logical, length, em->start, em->start + em->len);
2919 free_extent_map(em);
2920 return ERR_PTR(-EINVAL);
2921 }
2922
2923 /* callers are responsible for dropping em's ref. */
2924 return em;
2925}
2926
Nikolay Borisov97aff912018-07-20 19:37:53 +03002927int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002928{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002929 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002930 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002931 struct map_lookup *map;
2932 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002933 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002935
Omar Sandoval60ca8422018-05-16 16:34:31 -07002936 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002937 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002938 /*
2939 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002940 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002941 * do anything we still error out.
2942 */
2943 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002944 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002945 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002946 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002947 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002948 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002949 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002950
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002951 /*
2952 * Take the device list mutex to prevent races with the final phase of
2953 * a device replace operation that replaces the device object associated
2954 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2955 */
2956 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002957 for (i = 0; i < map->num_stripes; i++) {
2958 struct btrfs_device *device = map->stripes[i].dev;
2959 ret = btrfs_free_dev_extent(trans, device,
2960 map->stripes[i].physical,
2961 &dev_extent_len);
2962 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002963 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002964 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002965 goto out;
2966 }
2967
2968 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002969 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002970 btrfs_device_set_bytes_used(device,
2971 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002972 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002973 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002974 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002975 }
2976
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03002977 ret = btrfs_update_device(trans, device);
2978 if (ret) {
2979 mutex_unlock(&fs_devices->device_list_mutex);
2980 btrfs_abort_transaction(trans, ret);
2981 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002982 }
2983 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002984 mutex_unlock(&fs_devices->device_list_mutex);
2985
Nikolay Borisovf4208792018-07-20 19:37:52 +03002986 ret = btrfs_free_chunk(trans, 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
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002992 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002993
2994 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002995 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002996 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002997 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002998 goto out;
2999 }
3000 }
3001
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003002 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003003 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003004 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003005 goto out;
3006 }
3007
Josef Bacik47ab2a62014-09-18 11:20:02 -04003008out:
3009 /* once for us */
3010 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003011 return ret;
3012}
3013
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003014static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003015{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003016 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003017 struct btrfs_trans_handle *trans;
Dennis Zhoub0643e52019-12-13 16:22:14 -08003018 struct btrfs_block_group *block_group;
Chris Mason8f18cf12008-04-25 16:53:30 -04003019 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003020
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003021 /*
3022 * Prevent races with automatic removal of unused block groups.
3023 * After we relocate and before we remove the chunk with offset
3024 * chunk_offset, automatic removal of the block group can kick in,
3025 * resulting in a failure when calling btrfs_remove_chunk() below.
3026 *
3027 * Make sure to acquire this mutex before doing a tree search (dev
3028 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3029 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3030 * we release the path used to search the chunk/dev tree and before
3031 * the current task acquires this mutex and calls us.
3032 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003033 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003034
Chris Mason8f18cf12008-04-25 16:53:30 -04003035 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003036 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003038 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003039 if (ret)
3040 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003041
Dennis Zhoub0643e52019-12-13 16:22:14 -08003042 block_group = btrfs_lookup_block_group(fs_info, chunk_offset);
3043 if (!block_group)
3044 return -ENOENT;
3045 btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group);
3046 btrfs_put_block_group(block_group);
3047
Chris Mason19c4d2f2016-10-10 13:43:31 -07003048 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3049 chunk_offset);
3050 if (IS_ERR(trans)) {
3051 ret = PTR_ERR(trans);
3052 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3053 return ret;
3054 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003055
Chris Mason19c4d2f2016-10-10 13:43:31 -07003056 /*
3057 * step two, delete the device extents and the
3058 * chunk tree entries
3059 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003060 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003061 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003062 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003063}
3064
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003065static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003066{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003067 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003068 struct btrfs_path *path;
3069 struct extent_buffer *leaf;
3070 struct btrfs_chunk *chunk;
3071 struct btrfs_key key;
3072 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003073 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003074 bool retried = false;
3075 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003076 int ret;
3077
3078 path = btrfs_alloc_path();
3079 if (!path)
3080 return -ENOMEM;
3081
Josef Bacikba1bf482009-09-11 16:11:19 -04003082again:
Yan Zheng2b820322008-11-17 21:11:30 -05003083 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3084 key.offset = (u64)-1;
3085 key.type = BTRFS_CHUNK_ITEM_KEY;
3086
3087 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003088 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003089 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003090 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003091 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003092 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003093 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003094 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003095
3096 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3097 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003098 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003099 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003100 if (ret < 0)
3101 goto error;
3102 if (ret > 0)
3103 break;
3104
3105 leaf = path->nodes[0];
3106 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3107
3108 chunk = btrfs_item_ptr(leaf, path->slots[0],
3109 struct btrfs_chunk);
3110 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003111 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003112
3113 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003114 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003115 if (ret == -ENOSPC)
3116 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303117 else
3118 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003119 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003120 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003121
3122 if (found_key.offset == 0)
3123 break;
3124 key.offset = found_key.offset - 1;
3125 }
3126 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003127 if (failed && !retried) {
3128 failed = 0;
3129 retried = true;
3130 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303131 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003132 ret = -ENOSPC;
3133 }
Yan Zheng2b820322008-11-17 21:11:30 -05003134error:
3135 btrfs_free_path(path);
3136 return ret;
3137}
3138
Liu Boa6f93c72017-11-15 16:28:11 -07003139/*
3140 * return 1 : allocate a data chunk successfully,
3141 * return <0: errors during allocating a data chunk,
3142 * return 0 : no need to allocate a data chunk.
3143 */
3144static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3145 u64 chunk_offset)
3146{
David Sterba32da53862019-10-29 19:20:18 +01003147 struct btrfs_block_group *cache;
Liu Boa6f93c72017-11-15 16:28:11 -07003148 u64 bytes_used;
3149 u64 chunk_type;
3150
3151 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3152 ASSERT(cache);
3153 chunk_type = cache->flags;
3154 btrfs_put_block_group(cache);
3155
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003156 if (!(chunk_type & BTRFS_BLOCK_GROUP_DATA))
3157 return 0;
Liu Boa6f93c72017-11-15 16:28:11 -07003158
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003159 spin_lock(&fs_info->data_sinfo->lock);
3160 bytes_used = fs_info->data_sinfo->bytes_used;
3161 spin_unlock(&fs_info->data_sinfo->lock);
Liu Boa6f93c72017-11-15 16:28:11 -07003162
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003163 if (!bytes_used) {
3164 struct btrfs_trans_handle *trans;
3165 int ret;
Liu Boa6f93c72017-11-15 16:28:11 -07003166
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003167 trans = btrfs_join_transaction(fs_info->tree_root);
3168 if (IS_ERR(trans))
3169 return PTR_ERR(trans);
3170
3171 ret = btrfs_force_chunk_alloc(trans, BTRFS_BLOCK_GROUP_DATA);
3172 btrfs_end_transaction(trans);
3173 if (ret < 0)
3174 return ret;
3175 return 1;
Liu Boa6f93c72017-11-15 16:28:11 -07003176 }
Johannes Thumshirn5ae21692019-10-18 11:58:22 +02003177
Liu Boa6f93c72017-11-15 16:28:11 -07003178 return 0;
3179}
3180
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003181static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003182 struct btrfs_balance_control *bctl)
3183{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003184 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003185 struct btrfs_trans_handle *trans;
3186 struct btrfs_balance_item *item;
3187 struct btrfs_disk_balance_args disk_bargs;
3188 struct btrfs_path *path;
3189 struct extent_buffer *leaf;
3190 struct btrfs_key key;
3191 int ret, err;
3192
3193 path = btrfs_alloc_path();
3194 if (!path)
3195 return -ENOMEM;
3196
3197 trans = btrfs_start_transaction(root, 0);
3198 if (IS_ERR(trans)) {
3199 btrfs_free_path(path);
3200 return PTR_ERR(trans);
3201 }
3202
3203 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003204 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003205 key.offset = 0;
3206
3207 ret = btrfs_insert_empty_item(trans, root, path, &key,
3208 sizeof(*item));
3209 if (ret)
3210 goto out;
3211
3212 leaf = path->nodes[0];
3213 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3214
David Sterbab159fa22016-11-08 18:09:03 +01003215 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003216
3217 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3218 btrfs_set_balance_data(leaf, item, &disk_bargs);
3219 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3220 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3221 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3222 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3223
3224 btrfs_set_balance_flags(leaf, item, bctl->flags);
3225
3226 btrfs_mark_buffer_dirty(leaf);
3227out:
3228 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003229 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003230 if (err && !ret)
3231 ret = err;
3232 return ret;
3233}
3234
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003235static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003236{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003237 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003238 struct btrfs_trans_handle *trans;
3239 struct btrfs_path *path;
3240 struct btrfs_key key;
3241 int ret, err;
3242
3243 path = btrfs_alloc_path();
3244 if (!path)
3245 return -ENOMEM;
3246
David Sterba3502a8c2020-06-25 12:35:28 +02003247 trans = btrfs_start_transaction_fallback_global_rsv(root, 0);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003248 if (IS_ERR(trans)) {
3249 btrfs_free_path(path);
3250 return PTR_ERR(trans);
3251 }
3252
3253 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003254 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003255 key.offset = 0;
3256
3257 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3258 if (ret < 0)
3259 goto out;
3260 if (ret > 0) {
3261 ret = -ENOENT;
3262 goto out;
3263 }
3264
3265 ret = btrfs_del_item(trans, root, path);
3266out:
3267 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003268 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003269 if (err && !ret)
3270 ret = err;
3271 return ret;
3272}
3273
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003274/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003275 * This is a heuristic used to reduce the number of chunks balanced on
3276 * resume after balance was interrupted.
3277 */
3278static void update_balance_args(struct btrfs_balance_control *bctl)
3279{
3280 /*
3281 * Turn on soft mode for chunk types that were being converted.
3282 */
3283 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3284 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3285 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3286 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3287 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3288 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3289
3290 /*
3291 * Turn on usage filter if is not already used. The idea is
3292 * that chunks that we have already balanced should be
3293 * reasonably full. Don't do it for chunks that are being
3294 * converted - that will keep us from relocating unconverted
3295 * (albeit full) chunks.
3296 */
3297 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003298 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003299 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3300 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3301 bctl->data.usage = 90;
3302 }
3303 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003304 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003305 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3306 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3307 bctl->sys.usage = 90;
3308 }
3309 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003310 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003311 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3312 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3313 bctl->meta.usage = 90;
3314 }
3315}
3316
3317/*
David Sterba149196a2018-03-20 20:23:09 +01003318 * Clear the balance status in fs_info and delete the balance item from disk.
3319 */
3320static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003321{
3322 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003323 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003324
3325 BUG_ON(!fs_info->balance_ctl);
3326
3327 spin_lock(&fs_info->balance_lock);
3328 fs_info->balance_ctl = NULL;
3329 spin_unlock(&fs_info->balance_lock);
3330
3331 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003332 ret = del_balance_item(fs_info);
3333 if (ret)
3334 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003335}
3336
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003337/*
3338 * Balance filters. Return 1 if chunk should be filtered out
3339 * (should not be balanced).
3340 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003341static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003342 struct btrfs_balance_args *bargs)
3343{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003344 chunk_type = chunk_to_extended(chunk_type) &
3345 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003346
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003347 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003348 return 0;
3349
3350 return 1;
3351}
3352
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003353static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003354 struct btrfs_balance_args *bargs)
3355{
David Sterba32da53862019-10-29 19:20:18 +01003356 struct btrfs_block_group *cache;
David Sterbabc309462015-10-20 18:22:13 +02003357 u64 chunk_used;
3358 u64 user_thresh_min;
3359 u64 user_thresh_max;
3360 int ret = 1;
3361
3362 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003363 chunk_used = cache->used;
David Sterbabc309462015-10-20 18:22:13 +02003364
3365 if (bargs->usage_min == 0)
3366 user_thresh_min = 0;
3367 else
David Sterbab3470b52019-10-23 18:48:22 +02003368 user_thresh_min = div_factor_fine(cache->length,
3369 bargs->usage_min);
David Sterbabc309462015-10-20 18:22:13 +02003370
3371 if (bargs->usage_max == 0)
3372 user_thresh_max = 1;
3373 else if (bargs->usage_max > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003374 user_thresh_max = cache->length;
David Sterbabc309462015-10-20 18:22:13 +02003375 else
David Sterbab3470b52019-10-23 18:48:22 +02003376 user_thresh_max = div_factor_fine(cache->length,
3377 bargs->usage_max);
David Sterbabc309462015-10-20 18:22:13 +02003378
3379 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3380 ret = 0;
3381
3382 btrfs_put_block_group(cache);
3383 return ret;
3384}
3385
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003386static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003387 u64 chunk_offset, struct btrfs_balance_args *bargs)
3388{
David Sterba32da53862019-10-29 19:20:18 +01003389 struct btrfs_block_group *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003390 u64 chunk_used, user_thresh;
3391 int ret = 1;
3392
3393 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
David Sterbabf38be62019-10-23 18:48:11 +02003394 chunk_used = cache->used;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003395
David Sterbabc309462015-10-20 18:22:13 +02003396 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003397 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003398 else if (bargs->usage > 100)
David Sterbab3470b52019-10-23 18:48:22 +02003399 user_thresh = cache->length;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003400 else
David Sterbab3470b52019-10-23 18:48:22 +02003401 user_thresh = div_factor_fine(cache->length, bargs->usage);
Ilya Dryomova105bb82013-01-21 15:15:56 +02003402
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003403 if (chunk_used < user_thresh)
3404 ret = 0;
3405
3406 btrfs_put_block_group(cache);
3407 return ret;
3408}
3409
Ilya Dryomov409d4042012-01-16 22:04:47 +02003410static int chunk_devid_filter(struct extent_buffer *leaf,
3411 struct btrfs_chunk *chunk,
3412 struct btrfs_balance_args *bargs)
3413{
3414 struct btrfs_stripe *stripe;
3415 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3416 int i;
3417
3418 for (i = 0; i < num_stripes; i++) {
3419 stripe = btrfs_stripe_nr(chunk, i);
3420 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3421 return 0;
3422 }
3423
3424 return 1;
3425}
3426
David Sterba946c9252019-05-17 11:43:34 +02003427static u64 calc_data_stripes(u64 type, int num_stripes)
3428{
3429 const int index = btrfs_bg_flags_to_raid_index(type);
3430 const int ncopies = btrfs_raid_array[index].ncopies;
3431 const int nparity = btrfs_raid_array[index].nparity;
3432
3433 if (nparity)
3434 return num_stripes - nparity;
3435 else
3436 return num_stripes / ncopies;
3437}
3438
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003439/* [pstart, pend) */
3440static int chunk_drange_filter(struct extent_buffer *leaf,
3441 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003442 struct btrfs_balance_args *bargs)
3443{
3444 struct btrfs_stripe *stripe;
3445 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3446 u64 stripe_offset;
3447 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003448 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003449 int factor;
3450 int i;
3451
3452 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3453 return 0;
3454
David Sterba946c9252019-05-17 11:43:34 +02003455 type = btrfs_chunk_type(leaf, chunk);
3456 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003457
3458 for (i = 0; i < num_stripes; i++) {
3459 stripe = btrfs_stripe_nr(chunk, i);
3460 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3461 continue;
3462
3463 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3464 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003465 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003466
3467 if (stripe_offset < bargs->pend &&
3468 stripe_offset + stripe_length > bargs->pstart)
3469 return 0;
3470 }
3471
3472 return 1;
3473}
3474
Ilya Dryomovea671762012-01-16 22:04:48 +02003475/* [vstart, vend) */
3476static int chunk_vrange_filter(struct extent_buffer *leaf,
3477 struct btrfs_chunk *chunk,
3478 u64 chunk_offset,
3479 struct btrfs_balance_args *bargs)
3480{
3481 if (chunk_offset < bargs->vend &&
3482 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3483 /* at least part of the chunk is inside this vrange */
3484 return 0;
3485
3486 return 1;
3487}
3488
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003489static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3490 struct btrfs_chunk *chunk,
3491 struct btrfs_balance_args *bargs)
3492{
3493 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3494
3495 if (bargs->stripes_min <= num_stripes
3496 && num_stripes <= bargs->stripes_max)
3497 return 0;
3498
3499 return 1;
3500}
3501
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003502static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003503 struct btrfs_balance_args *bargs)
3504{
3505 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3506 return 0;
3507
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003508 chunk_type = chunk_to_extended(chunk_type) &
3509 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003510
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003511 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003512 return 1;
3513
3514 return 0;
3515}
3516
David Sterba6ec08962019-03-20 16:38:52 +01003517static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003518 struct btrfs_chunk *chunk, u64 chunk_offset)
3519{
David Sterba6ec08962019-03-20 16:38:52 +01003520 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003522 struct btrfs_balance_args *bargs = NULL;
3523 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3524
3525 /* type filter */
3526 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3527 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3528 return 0;
3529 }
3530
3531 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3532 bargs = &bctl->data;
3533 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3534 bargs = &bctl->sys;
3535 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3536 bargs = &bctl->meta;
3537
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003538 /* profiles filter */
3539 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3540 chunk_profiles_filter(chunk_type, bargs)) {
3541 return 0;
3542 }
3543
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003544 /* usage filter */
3545 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003546 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003547 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003548 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003549 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003550 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003551 }
3552
Ilya Dryomov409d4042012-01-16 22:04:47 +02003553 /* devid filter */
3554 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3555 chunk_devid_filter(leaf, chunk, bargs)) {
3556 return 0;
3557 }
3558
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003559 /* drange filter, makes sense only with devid filter */
3560 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003561 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003562 return 0;
3563 }
3564
Ilya Dryomovea671762012-01-16 22:04:48 +02003565 /* vrange filter */
3566 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3567 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3568 return 0;
3569 }
3570
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003571 /* stripes filter */
3572 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3573 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3574 return 0;
3575 }
3576
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003577 /* soft profile changing mode */
3578 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3579 chunk_soft_convert_filter(chunk_type, bargs)) {
3580 return 0;
3581 }
3582
David Sterba7d824b62014-05-07 17:37:51 +02003583 /*
3584 * limited by count, must be the last filter
3585 */
3586 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3587 if (bargs->limit == 0)
3588 return 0;
3589 else
3590 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003591 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3592 /*
3593 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003594 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003595 * about the count of all chunks that satisfy the filters.
3596 */
3597 if (bargs->limit_max == 0)
3598 return 0;
3599 else
3600 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003601 }
3602
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003603 return 1;
3604}
3605
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003606static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003607{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003608 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003609 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003610 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003611 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003612 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003613 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003614 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003615 struct extent_buffer *leaf;
3616 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003617 int ret;
3618 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003619 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003620 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003621 u64 limit_data = bctl->data.limit;
3622 u64 limit_meta = bctl->meta.limit;
3623 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003624 u32 count_data = 0;
3625 u32 count_meta = 0;
3626 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003627 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003628
Chris Masonec44a352008-04-28 15:29:52 -04003629 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003630 if (!path) {
3631 ret = -ENOMEM;
3632 goto error;
3633 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003634
3635 /* zero out stat counters */
3636 spin_lock(&fs_info->balance_lock);
3637 memset(&bctl->stat, 0, sizeof(bctl->stat));
3638 spin_unlock(&fs_info->balance_lock);
3639again:
David Sterba7d824b62014-05-07 17:37:51 +02003640 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003641 /*
3642 * The single value limit and min/max limits use the same bytes
3643 * in the
3644 */
David Sterba7d824b62014-05-07 17:37:51 +02003645 bctl->data.limit = limit_data;
3646 bctl->meta.limit = limit_meta;
3647 bctl->sys.limit = limit_sys;
3648 }
Chris Masonec44a352008-04-28 15:29:52 -04003649 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3650 key.offset = (u64)-1;
3651 key.type = BTRFS_CHUNK_ITEM_KEY;
3652
Chris Masond3977122009-01-05 21:25:51 -05003653 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003654 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003655 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003656 ret = -ECANCELED;
3657 goto error;
3658 }
3659
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003660 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003661 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003662 if (ret < 0) {
3663 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003664 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003665 }
Chris Masonec44a352008-04-28 15:29:52 -04003666
3667 /*
3668 * this shouldn't happen, it means the last relocate
3669 * failed
3670 */
3671 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003672 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003673
3674 ret = btrfs_previous_item(chunk_root, path, 0,
3675 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003676 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003677 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003678 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003679 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003680 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003681
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003682 leaf = path->nodes[0];
3683 slot = path->slots[0];
3684 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3685
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003686 if (found_key.objectid != key.objectid) {
3687 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003688 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003689 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003690
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003691 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003692 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003693
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003694 if (!counting) {
3695 spin_lock(&fs_info->balance_lock);
3696 bctl->stat.considered++;
3697 spin_unlock(&fs_info->balance_lock);
3698 }
3699
David Sterba6ec08962019-03-20 16:38:52 +01003700 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003701
David Sterbab3b4aa72011-04-21 01:20:15 +02003702 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003703 if (!ret) {
3704 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003705 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003706 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003707
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003708 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003709 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003710 spin_lock(&fs_info->balance_lock);
3711 bctl->stat.expected++;
3712 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003713
3714 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3715 count_data++;
3716 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3717 count_sys++;
3718 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3719 count_meta++;
3720
3721 goto loop;
3722 }
3723
3724 /*
3725 * Apply limit_min filter, no need to check if the LIMITS
3726 * filter is used, limit_min is 0 by default
3727 */
3728 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3729 count_data < bctl->data.limit_min)
3730 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3731 count_meta < bctl->meta.limit_min)
3732 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3733 count_sys < bctl->sys.limit_min)) {
3734 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003735 goto loop;
3736 }
3737
Liu Boa6f93c72017-11-15 16:28:11 -07003738 if (!chunk_reserved) {
3739 /*
3740 * We may be relocating the only data chunk we have,
3741 * which could potentially end up with losing data's
3742 * raid profile, so lets allocate an empty one in
3743 * advance.
3744 */
3745 ret = btrfs_may_alloc_data_chunk(fs_info,
3746 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003747 if (ret < 0) {
3748 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3749 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003750 } else if (ret == 1) {
3751 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003752 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003753 }
3754
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003755 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003756 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003757 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003758 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003759 } else if (ret == -ETXTBSY) {
3760 btrfs_info(fs_info,
3761 "skipping relocation of block group %llu due to active swapfile",
3762 found_key.offset);
3763 ret = 0;
3764 } else if (ret) {
3765 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003766 } else {
3767 spin_lock(&fs_info->balance_lock);
3768 bctl->stat.completed++;
3769 spin_unlock(&fs_info->balance_lock);
3770 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003771loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003772 if (found_key.offset == 0)
3773 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003774 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003775 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003776
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003777 if (counting) {
3778 btrfs_release_path(path);
3779 counting = false;
3780 goto again;
3781 }
Chris Masonec44a352008-04-28 15:29:52 -04003782error:
3783 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003784 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003785 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003786 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003787 if (!ret)
3788 ret = -ENOSPC;
3789 }
3790
Chris Masonec44a352008-04-28 15:29:52 -04003791 return ret;
3792}
3793
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003794/**
3795 * alloc_profile_is_valid - see if a given profile is valid and reduced
3796 * @flags: profile to validate
3797 * @extended: if true @flags is treated as an extended profile
3798 */
3799static int alloc_profile_is_valid(u64 flags, int extended)
3800{
3801 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3802 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3803
3804 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3805
3806 /* 1) check that all other bits are zeroed */
3807 if (flags & ~mask)
3808 return 0;
3809
3810 /* 2) see if profile is reduced */
3811 if (flags == 0)
3812 return !extended; /* "0" is valid for usual profiles */
3813
David Sterbac1499162019-10-01 19:44:42 +02003814 return has_single_bit_set(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003815}
3816
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003817static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3818{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003819 /* cancel requested || normal exit path */
3820 return atomic_read(&fs_info->balance_cancel_req) ||
3821 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3822 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003823}
3824
David Sterba5ba366c2020-02-27 21:00:52 +01003825/*
3826 * Validate target profile against allowed profiles and return true if it's OK.
3827 * Otherwise print the error message and return false.
3828 */
3829static inline int validate_convert_profile(struct btrfs_fs_info *fs_info,
3830 const struct btrfs_balance_args *bargs,
3831 u64 allowed, const char *type)
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003832{
David Sterba5ba366c2020-02-27 21:00:52 +01003833 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3834 return true;
3835
3836 /* Profile is valid and does not have bits outside of the allowed set */
3837 if (alloc_profile_is_valid(bargs->target, 1) &&
3838 (bargs->target & ~allowed) == 0)
3839 return true;
3840
3841 btrfs_err(fs_info, "balance: invalid convert %s profile %s",
3842 type, btrfs_bg_type_to_raid_name(bargs->target));
3843 return false;
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003844}
3845
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003846/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003847 * Fill @buf with textual description of balance filter flags @bargs, up to
3848 * @size_buf including the terminating null. The output may be trimmed if it
3849 * does not fit into the provided buffer.
3850 */
3851static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3852 u32 size_buf)
3853{
3854 int ret;
3855 u32 size_bp = size_buf;
3856 char *bp = buf;
3857 u64 flags = bargs->flags;
3858 char tmp_buf[128] = {'\0'};
3859
3860 if (!flags)
3861 return;
3862
3863#define CHECK_APPEND_NOARG(a) \
3864 do { \
3865 ret = snprintf(bp, size_bp, (a)); \
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_1ARG(a, v1) \
3873 do { \
3874 ret = snprintf(bp, size_bp, (a), (v1)); \
3875 if (ret < 0 || ret >= size_bp) \
3876 goto out_overflow; \
3877 size_bp -= ret; \
3878 bp += ret; \
3879 } while (0)
3880
3881#define CHECK_APPEND_2ARG(a, v1, v2) \
3882 do { \
3883 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3884 if (ret < 0 || ret >= size_bp) \
3885 goto out_overflow; \
3886 size_bp -= ret; \
3887 bp += ret; \
3888 } while (0)
3889
David Sterba158da512019-05-17 11:43:41 +02003890 if (flags & BTRFS_BALANCE_ARGS_CONVERT)
3891 CHECK_APPEND_1ARG("convert=%s,",
3892 btrfs_bg_type_to_raid_name(bargs->target));
Anand Jain56fc37d2018-11-20 16:12:56 +08003893
3894 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3895 CHECK_APPEND_NOARG("soft,");
3896
3897 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3898 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3899 sizeof(tmp_buf));
3900 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3901 }
3902
3903 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3904 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3905
3906 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3907 CHECK_APPEND_2ARG("usage=%u..%u,",
3908 bargs->usage_min, bargs->usage_max);
3909
3910 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3911 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3912
3913 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3914 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3915 bargs->pstart, bargs->pend);
3916
3917 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3918 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3919 bargs->vstart, bargs->vend);
3920
3921 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3922 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3923
3924 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3925 CHECK_APPEND_2ARG("limit=%u..%u,",
3926 bargs->limit_min, bargs->limit_max);
3927
3928 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3929 CHECK_APPEND_2ARG("stripes=%u..%u,",
3930 bargs->stripes_min, bargs->stripes_max);
3931
3932#undef CHECK_APPEND_2ARG
3933#undef CHECK_APPEND_1ARG
3934#undef CHECK_APPEND_NOARG
3935
3936out_overflow:
3937
3938 if (size_bp < size_buf)
3939 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3940 else
3941 buf[0] = '\0';
3942}
3943
3944static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3945{
3946 u32 size_buf = 1024;
3947 char tmp_buf[192] = {'\0'};
3948 char *buf;
3949 char *bp;
3950 u32 size_bp = size_buf;
3951 int ret;
3952 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3953
3954 buf = kzalloc(size_buf, GFP_KERNEL);
3955 if (!buf)
3956 return;
3957
3958 bp = buf;
3959
3960#define CHECK_APPEND_1ARG(a, v1) \
3961 do { \
3962 ret = snprintf(bp, size_bp, (a), (v1)); \
3963 if (ret < 0 || ret >= size_bp) \
3964 goto out_overflow; \
3965 size_bp -= ret; \
3966 bp += ret; \
3967 } while (0)
3968
3969 if (bctl->flags & BTRFS_BALANCE_FORCE)
3970 CHECK_APPEND_1ARG("%s", "-f ");
3971
3972 if (bctl->flags & BTRFS_BALANCE_DATA) {
3973 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
3974 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
3975 }
3976
3977 if (bctl->flags & BTRFS_BALANCE_METADATA) {
3978 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
3979 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
3980 }
3981
3982 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
3983 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
3984 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
3985 }
3986
3987#undef CHECK_APPEND_1ARG
3988
3989out_overflow:
3990
3991 if (size_bp < size_buf)
3992 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
3993 btrfs_info(fs_info, "balance: %s %s",
3994 (bctl->flags & BTRFS_BALANCE_RESUME) ?
3995 "resume" : "start", buf);
3996
3997 kfree(buf);
3998}
3999
4000/*
David Sterbadccdb072018-03-21 00:20:05 +01004001 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004002 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004003int btrfs_balance(struct btrfs_fs_info *fs_info,
4004 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004005 struct btrfs_ioctl_balance_args *bargs)
4006{
Adam Borowski14506122017-03-07 23:34:44 +01004007 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004008 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004009 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004010 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004011 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004012 unsigned seq;
Anand Jaine62869b2019-09-25 14:29:28 +08004013 bool reducing_redundancy;
David Sterba081db892019-05-17 11:43:27 +02004014 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004015
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004016 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004017 atomic_read(&fs_info->balance_pause_req) ||
Qu Wenruo726a3422020-02-17 14:16:52 +08004018 btrfs_should_cancel_balance(fs_info)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004019 ret = -EINVAL;
4020 goto out;
4021 }
4022
Ilya Dryomove4837f82012-03-27 17:09:17 +03004023 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4024 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4025 mixed = 1;
4026
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004027 /*
4028 * In case of mixed groups both data and meta should be picked,
4029 * and identical options should be given for both of them.
4030 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004031 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4032 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004033 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4034 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4035 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004036 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004037 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004038 ret = -EINVAL;
4039 goto out;
4040 }
4041 }
4042
Josef Bacikb35cf1f2020-01-10 11:11:24 -05004043 /*
4044 * rw_devices will not change at the moment, device add/delete/replace
4045 * are excluded by EXCL_OP
4046 */
4047 num_devices = fs_info->fs_devices->rw_devices;
Qu Wenruofab27352019-09-25 10:13:27 +08004048
4049 /*
4050 * SINGLE profile on-disk has no profile bit, but in-memory we have a
4051 * special bit for it, to make it easier to distinguish. Thus we need
4052 * to set it manually, or balance would refuse the profile.
4053 */
4054 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
David Sterba081db892019-05-17 11:43:27 +02004055 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4056 if (num_devices >= btrfs_raid_array[i].devs_min)
4057 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08004058
David Sterba5ba366c2020-02-27 21:00:52 +01004059 if (!validate_convert_profile(fs_info, &bctl->data, allowed, "data") ||
4060 !validate_convert_profile(fs_info, &bctl->meta, allowed, "metadata") ||
4061 !validate_convert_profile(fs_info, &bctl->sys, allowed, "system")) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004062 ret = -EINVAL;
4063 goto out;
4064 }
4065
David Sterba6079e122019-05-17 11:43:29 +02004066 /*
4067 * Allow to reduce metadata or system integrity only if force set for
4068 * profiles with redundancy (copies, parity)
4069 */
4070 allowed = 0;
4071 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4072 if (btrfs_raid_array[i].ncopies >= 2 ||
4073 btrfs_raid_array[i].tolerated_failures >= 1)
4074 allowed |= btrfs_raid_array[i].bg_flag;
4075 }
Miao Xiede98ced2013-01-29 10:13:12 +00004076 do {
4077 seq = read_seqbegin(&fs_info->profiles_lock);
4078
4079 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4080 (fs_info->avail_system_alloc_bits & allowed) &&
4081 !(bctl->sys.target & allowed)) ||
4082 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4083 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004084 !(bctl->meta.target & allowed)))
Anand Jaine62869b2019-09-25 14:29:28 +08004085 reducing_redundancy = true;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004086 else
Anand Jaine62869b2019-09-25 14:29:28 +08004087 reducing_redundancy = false;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004088
4089 /* if we're not converting, the target field is uninitialized */
4090 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4091 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4092 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4093 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004094 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004095
Anand Jaine62869b2019-09-25 14:29:28 +08004096 if (reducing_redundancy) {
Filipe Manana5a8067c2018-11-19 09:48:12 +00004097 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4098 btrfs_info(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004099 "balance: force reducing metadata redundancy");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004100 } else {
4101 btrfs_err(fs_info,
Anand Jaine62869b2019-09-25 14:29:28 +08004102 "balance: reduces metadata redundancy, use --force if you want this");
Filipe Manana5a8067c2018-11-19 09:48:12 +00004103 ret = -EINVAL;
4104 goto out;
4105 }
4106 }
4107
Adam Borowski14506122017-03-07 23:34:44 +01004108 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4109 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00004110 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004111 "balance: metadata profile %s has lower redundancy than data profile %s",
David Sterba158da512019-05-17 11:43:41 +02004112 btrfs_bg_type_to_raid_name(meta_target),
4113 btrfs_bg_type_to_raid_name(data_target));
Sam Tygieree592d02016-01-06 08:46:12 +00004114 }
4115
Filipe Manana9e967492019-04-22 16:44:09 +01004116 if (fs_info->send_in_progress) {
4117 btrfs_warn_rl(fs_info,
4118"cannot run balance while send operations are in progress (%d in progress)",
4119 fs_info->send_in_progress);
4120 ret = -EAGAIN;
4121 goto out;
4122 }
4123
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004124 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004125 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004126 goto out;
4127
Ilya Dryomov59641012012-01-16 22:04:48 +02004128 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4129 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004130 BUG_ON(fs_info->balance_ctl);
4131 spin_lock(&fs_info->balance_lock);
4132 fs_info->balance_ctl = bctl;
4133 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004134 } else {
4135 BUG_ON(ret != -EEXIST);
4136 spin_lock(&fs_info->balance_lock);
4137 update_balance_args(bctl);
4138 spin_unlock(&fs_info->balance_lock);
4139 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004140
David Sterba3009a622018-03-21 01:31:04 +01004141 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4142 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004143 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004144 mutex_unlock(&fs_info->balance_mutex);
4145
4146 ret = __btrfs_balance(fs_info);
4147
4148 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004149 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4150 btrfs_info(fs_info, "balance: paused");
Qu Wenruo44d354a2020-07-13 09:03:21 +08004151 /*
4152 * Balance can be canceled by:
4153 *
4154 * - Regular cancel request
4155 * Then ret == -ECANCELED and balance_cancel_req > 0
4156 *
4157 * - Fatal signal to "btrfs" process
4158 * Either the signal caught by wait_reserve_ticket() and callers
4159 * got -EINTR, or caught by btrfs_should_cancel_balance() and
4160 * got -ECANCELED.
4161 * Either way, in this case balance_cancel_req = 0, and
4162 * ret == -EINTR or ret == -ECANCELED.
4163 *
4164 * So here we only check the return value to catch canceled balance.
4165 */
4166 else if (ret == -ECANCELED || ret == -EINTR)
Anand Jain7333bd02018-11-20 16:12:57 +08004167 btrfs_info(fs_info, "balance: canceled");
4168 else
4169 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4170
David Sterba3009a622018-03-21 01:31:04 +01004171 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004172
4173 if (bargs) {
4174 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004175 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004176 }
4177
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004178 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4179 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004180 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004181 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004182 }
4183
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004184 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004185
4186 return ret;
4187out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004188 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004189 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004190 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004191 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004192 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4193
Ilya Dryomov59641012012-01-16 22:04:48 +02004194 return ret;
4195}
4196
4197static int balance_kthread(void *data)
4198{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004199 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004200 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004201
Ilya Dryomov59641012012-01-16 22:04:48 +02004202 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004203 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004204 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004205 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004206
Ilya Dryomov59641012012-01-16 22:04:48 +02004207 return ret;
4208}
4209
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004210int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4211{
4212 struct task_struct *tsk;
4213
David Sterba1354e1a2018-03-21 02:29:13 +01004214 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004215 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004216 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004217 return 0;
4218 }
David Sterba1354e1a2018-03-21 02:29:13 +01004219 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004220
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004221 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004222 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004223 return 0;
4224 }
4225
Anand Jain02ee6542018-05-17 15:16:51 +08004226 /*
4227 * A ro->rw remount sequence should continue with the paused balance
4228 * regardless of who pauses it, system or the user as of now, so set
4229 * the resume flag.
4230 */
4231 spin_lock(&fs_info->balance_lock);
4232 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4233 spin_unlock(&fs_info->balance_lock);
4234
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004235 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304236 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004237}
4238
Ilya Dryomov68310a52012-06-22 12:24:12 -06004239int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004240{
Ilya Dryomov59641012012-01-16 22:04:48 +02004241 struct btrfs_balance_control *bctl;
4242 struct btrfs_balance_item *item;
4243 struct btrfs_disk_balance_args disk_bargs;
4244 struct btrfs_path *path;
4245 struct extent_buffer *leaf;
4246 struct btrfs_key key;
4247 int ret;
4248
4249 path = btrfs_alloc_path();
4250 if (!path)
4251 return -ENOMEM;
4252
Ilya Dryomov68310a52012-06-22 12:24:12 -06004253 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004254 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004255 key.offset = 0;
4256
4257 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4258 if (ret < 0)
4259 goto out;
4260 if (ret > 0) { /* ret = -ENOENT; */
4261 ret = 0;
4262 goto out;
4263 }
4264
Ilya Dryomov59641012012-01-16 22:04:48 +02004265 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4266 if (!bctl) {
4267 ret = -ENOMEM;
4268 goto out;
4269 }
4270
Ilya Dryomov59641012012-01-16 22:04:48 +02004271 leaf = path->nodes[0];
4272 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4273
Ilya Dryomov68310a52012-06-22 12:24:12 -06004274 bctl->flags = btrfs_balance_flags(leaf, item);
4275 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004276
4277 btrfs_balance_data(leaf, item, &disk_bargs);
4278 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4279 btrfs_balance_meta(leaf, item, &disk_bargs);
4280 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4281 btrfs_balance_sys(leaf, item, &disk_bargs);
4282 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4283
David Sterbaeee95e32018-03-20 20:07:58 +01004284 /*
4285 * This should never happen, as the paused balance state is recovered
4286 * during mount without any chance of other exclusive ops to collide.
4287 *
4288 * This gives the exclusive op status to balance and keeps in paused
4289 * state until user intervention (cancel or umount). If the ownership
4290 * cannot be assigned, show a message but do not fail. The balance
4291 * is in a paused state and must have fs_info::balance_ctl properly
4292 * set up.
4293 */
4294 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4295 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004296 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004297
Ilya Dryomov68310a52012-06-22 12:24:12 -06004298 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004299 BUG_ON(fs_info->balance_ctl);
4300 spin_lock(&fs_info->balance_lock);
4301 fs_info->balance_ctl = bctl;
4302 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004303 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004304out:
4305 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004306 return ret;
4307}
4308
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004309int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4310{
4311 int ret = 0;
4312
4313 mutex_lock(&fs_info->balance_mutex);
4314 if (!fs_info->balance_ctl) {
4315 mutex_unlock(&fs_info->balance_mutex);
4316 return -ENOTCONN;
4317 }
4318
David Sterba3009a622018-03-21 01:31:04 +01004319 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004320 atomic_inc(&fs_info->balance_pause_req);
4321 mutex_unlock(&fs_info->balance_mutex);
4322
4323 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004324 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004325
4326 mutex_lock(&fs_info->balance_mutex);
4327 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004328 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004329 atomic_dec(&fs_info->balance_pause_req);
4330 } else {
4331 ret = -ENOTCONN;
4332 }
4333
4334 mutex_unlock(&fs_info->balance_mutex);
4335 return ret;
4336}
4337
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004338int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4339{
4340 mutex_lock(&fs_info->balance_mutex);
4341 if (!fs_info->balance_ctl) {
4342 mutex_unlock(&fs_info->balance_mutex);
4343 return -ENOTCONN;
4344 }
4345
David Sterbacf7d20f2018-03-21 01:45:32 +01004346 /*
4347 * A paused balance with the item stored on disk can be resumed at
4348 * mount time if the mount is read-write. Otherwise it's still paused
4349 * and we must not allow cancelling as it deletes the item.
4350 */
4351 if (sb_rdonly(fs_info->sb)) {
4352 mutex_unlock(&fs_info->balance_mutex);
4353 return -EROFS;
4354 }
4355
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004356 atomic_inc(&fs_info->balance_cancel_req);
4357 /*
4358 * if we are running just wait and return, balance item is
4359 * deleted in btrfs_balance in this case
4360 */
David Sterba3009a622018-03-21 01:31:04 +01004361 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004362 mutex_unlock(&fs_info->balance_mutex);
4363 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004364 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004365 mutex_lock(&fs_info->balance_mutex);
4366 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004367 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004368 /*
4369 * Lock released to allow other waiters to continue, we'll
4370 * reexamine the status again.
4371 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004372 mutex_lock(&fs_info->balance_mutex);
4373
David Sterbaa17c95d2018-03-20 17:28:05 +01004374 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004375 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004376 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004377 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004378 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004379 }
4380
David Sterba3009a622018-03-21 01:31:04 +01004381 BUG_ON(fs_info->balance_ctl ||
4382 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004383 atomic_dec(&fs_info->balance_cancel_req);
4384 mutex_unlock(&fs_info->balance_mutex);
4385 return 0;
4386}
4387
Nikolay Borisov97f4dd02020-02-18 16:56:08 +02004388int btrfs_uuid_scan_kthread(void *data)
Stefan Behrens803b2f52013-08-15 17:11:21 +02004389{
4390 struct btrfs_fs_info *fs_info = data;
4391 struct btrfs_root *root = fs_info->tree_root;
4392 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004393 struct btrfs_path *path = NULL;
4394 int ret = 0;
4395 struct extent_buffer *eb;
4396 int slot;
4397 struct btrfs_root_item root_item;
4398 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004399 struct btrfs_trans_handle *trans = NULL;
Josef Bacikc94bec22020-02-14 15:05:01 -05004400 bool closing = false;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004401
4402 path = btrfs_alloc_path();
4403 if (!path) {
4404 ret = -ENOMEM;
4405 goto out;
4406 }
4407
4408 key.objectid = 0;
4409 key.type = BTRFS_ROOT_ITEM_KEY;
4410 key.offset = 0;
4411
Stefan Behrens803b2f52013-08-15 17:11:21 +02004412 while (1) {
Josef Bacikc94bec22020-02-14 15:05:01 -05004413 if (btrfs_fs_closing(fs_info)) {
4414 closing = true;
4415 break;
4416 }
Anand Jain7c829b72018-03-07 17:29:18 +08004417 ret = btrfs_search_forward(root, &key, path,
4418 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004419 if (ret) {
4420 if (ret > 0)
4421 ret = 0;
4422 break;
4423 }
4424
4425 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4426 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4427 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4428 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4429 goto skip;
4430
4431 eb = path->nodes[0];
4432 slot = path->slots[0];
4433 item_size = btrfs_item_size_nr(eb, slot);
4434 if (item_size < sizeof(root_item))
4435 goto skip;
4436
Stefan Behrens803b2f52013-08-15 17:11:21 +02004437 read_extent_buffer(eb, &root_item,
4438 btrfs_item_ptr_offset(eb, slot),
4439 (int)sizeof(root_item));
4440 if (btrfs_root_refs(&root_item) == 0)
4441 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004442
4443 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4444 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4445 if (trans)
4446 goto update_tree;
4447
4448 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004449 /*
4450 * 1 - subvol uuid item
4451 * 1 - received_subvol uuid item
4452 */
4453 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4454 if (IS_ERR(trans)) {
4455 ret = PTR_ERR(trans);
4456 break;
4457 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004458 continue;
4459 } else {
4460 goto skip;
4461 }
4462update_tree:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004463 btrfs_release_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004464 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004465 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004466 BTRFS_UUID_KEY_SUBVOL,
4467 key.objectid);
4468 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004469 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004470 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004471 break;
4472 }
4473 }
4474
4475 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004476 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004477 root_item.received_uuid,
4478 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4479 key.objectid);
4480 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004481 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004482 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004483 break;
4484 }
4485 }
4486
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004487skip:
Josef Bacik9771a5cf2020-08-10 11:42:26 -04004488 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004489 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004490 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004491 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004492 if (ret)
4493 break;
4494 }
4495
Stefan Behrens803b2f52013-08-15 17:11:21 +02004496 if (key.offset < (u64)-1) {
4497 key.offset++;
4498 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4499 key.offset = 0;
4500 key.type = BTRFS_ROOT_ITEM_KEY;
4501 } else if (key.objectid < (u64)-1) {
4502 key.offset = 0;
4503 key.type = BTRFS_ROOT_ITEM_KEY;
4504 key.objectid++;
4505 } else {
4506 break;
4507 }
4508 cond_resched();
4509 }
4510
4511out:
4512 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004513 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004514 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004515 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004516 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Josef Bacikc94bec22020-02-14 15:05:01 -05004517 else if (!closing)
Josef Bacikafcdd122016-09-02 15:40:02 -04004518 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004519 up(&fs_info->uuid_tree_rescan_sem);
4520 return 0;
4521}
4522
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004523int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4524{
4525 struct btrfs_trans_handle *trans;
4526 struct btrfs_root *tree_root = fs_info->tree_root;
4527 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004528 struct task_struct *task;
4529 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004530
4531 /*
4532 * 1 - root node
4533 * 1 - root item
4534 */
4535 trans = btrfs_start_transaction(tree_root, 2);
4536 if (IS_ERR(trans))
4537 return PTR_ERR(trans);
4538
David Sterba9b7a2442019-03-20 13:20:49 +01004539 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004540 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004541 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004542 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004543 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004544 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004545 }
4546
4547 fs_info->uuid_root = uuid_root;
4548
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004549 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004550 if (ret)
4551 return ret;
4552
4553 down(&fs_info->uuid_tree_rescan_sem);
4554 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4555 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004556 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004557 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004558 up(&fs_info->uuid_tree_rescan_sem);
4559 return PTR_ERR(task);
4560 }
4561
4562 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004563}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004564
Chris Mason8f18cf12008-04-25 16:53:30 -04004565/*
4566 * shrinking a device means finding all of the device extents past
4567 * the new size, and then following the back refs to the chunks.
4568 * The chunk relocation code actually frees the device extent
4569 */
4570int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4571{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004572 struct btrfs_fs_info *fs_info = device->fs_info;
4573 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004574 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004575 struct btrfs_dev_extent *dev_extent = NULL;
4576 struct btrfs_path *path;
4577 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004578 u64 chunk_offset;
4579 int ret;
4580 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004581 int failed = 0;
4582 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004583 struct extent_buffer *l;
4584 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004585 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004586 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004587 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004588 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004589 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004590
4591 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004592 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004593 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004594
Anand Jain401e29c2017-12-04 12:54:55 +08004595 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004596 return -EINVAL;
4597
Chris Mason8f18cf12008-04-25 16:53:30 -04004598 path = btrfs_alloc_path();
4599 if (!path)
4600 return -ENOMEM;
4601
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004602 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004603
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004604 trans = btrfs_start_transaction(root, 0);
4605 if (IS_ERR(trans)) {
4606 btrfs_free_path(path);
4607 return PTR_ERR(trans);
4608 }
4609
David Sterba34441362016-10-04 19:34:27 +02004610 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004611
Miao Xie7cc8e582014-09-03 21:35:38 +08004612 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004613 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004614 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004615 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004616 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004617
4618 /*
4619 * Once the device's size has been set to the new size, ensure all
4620 * in-memory chunks are synced to disk so that the loop below sees them
4621 * and relocates them accordingly.
4622 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004623 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004624 mutex_unlock(&fs_info->chunk_mutex);
4625 ret = btrfs_commit_transaction(trans);
4626 if (ret)
4627 goto done;
4628 } else {
4629 mutex_unlock(&fs_info->chunk_mutex);
4630 btrfs_end_transaction(trans);
4631 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004632
Josef Bacikba1bf482009-09-11 16:11:19 -04004633again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004634 key.objectid = device->devid;
4635 key.offset = (u64)-1;
4636 key.type = BTRFS_DEV_EXTENT_KEY;
4637
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004638 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004639 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004640 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004641 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004642 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004643 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004644 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004645
4646 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004647 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004648 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004649 if (ret < 0)
4650 goto done;
4651 if (ret) {
4652 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004653 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004654 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004655 }
4656
4657 l = path->nodes[0];
4658 slot = path->slots[0];
4659 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4660
Josef Bacikba1bf482009-09-11 16:11:19 -04004661 if (key.objectid != device->devid) {
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);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004664 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004665 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004666
4667 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4668 length = btrfs_dev_extent_length(l, dev_extent);
4669
Josef Bacikba1bf482009-09-11 16:11:19 -04004670 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004671 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004672 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004673 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004674 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004675
Chris Mason8f18cf12008-04-25 16:53:30 -04004676 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004677 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004678
Liu Boa6f93c72017-11-15 16:28:11 -07004679 /*
4680 * We may be relocating the only data chunk we have,
4681 * which could potentially end up with losing data's
4682 * raid profile, so lets allocate an empty one in
4683 * advance.
4684 */
4685 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4686 if (ret < 0) {
4687 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4688 goto done;
4689 }
4690
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004691 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4692 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004693 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004694 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004695 } else if (ret) {
4696 if (ret == -ETXTBSY) {
4697 btrfs_warn(fs_info,
4698 "could not shrink block group %llu due to active swapfile",
4699 chunk_offset);
4700 }
4701 goto done;
4702 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004703 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004704
4705 if (failed && !retried) {
4706 failed = 0;
4707 retried = true;
4708 goto again;
4709 } else if (failed && retried) {
4710 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004711 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004712 }
4713
Chris Balld6397ba2009-04-27 07:29:03 -04004714 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004715 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004716 if (IS_ERR(trans)) {
4717 ret = PTR_ERR(trans);
4718 goto done;
4719 }
4720
David Sterba34441362016-10-04 19:34:27 +02004721 mutex_lock(&fs_info->chunk_mutex);
Qu Wenruoc57dd1f2020-07-31 19:29:11 +08004722 /* Clear all state bits beyond the shrunk device size */
4723 clear_extent_bits(&device->alloc_state, new_size, (u64)-1,
4724 CHUNK_STATE_MASK);
4725
Miao Xie7cc8e582014-09-03 21:35:38 +08004726 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004727 if (list_empty(&device->post_commit_list))
4728 list_add_tail(&device->post_commit_list,
4729 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004730
Chris Balld6397ba2009-04-27 07:29:03 -04004731 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004732 btrfs_set_super_total_bytes(super_copy,
4733 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004734 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004735
4736 /* Now btrfs_update_device() will change the on-disk size. */
4737 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004738 if (ret < 0) {
4739 btrfs_abort_transaction(trans, ret);
4740 btrfs_end_transaction(trans);
4741 } else {
4742 ret = btrfs_commit_transaction(trans);
4743 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004744done:
4745 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004746 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004747 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004748 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004749 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004750 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004751 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004752 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004753 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004754 return ret;
4755}
4756
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004757static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004758 struct btrfs_key *key,
4759 struct btrfs_chunk *chunk, int item_size)
4760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004761 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004762 struct btrfs_disk_key disk_key;
4763 u32 array_size;
4764 u8 *ptr;
4765
David Sterba34441362016-10-04 19:34:27 +02004766 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004767 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004768 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004769 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004770 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004771 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004772 }
Chris Mason0b86a832008-03-24 15:01:56 -04004773
4774 ptr = super_copy->sys_chunk_array + array_size;
4775 btrfs_cpu_key_to_disk(&disk_key, key);
4776 memcpy(ptr, &disk_key, sizeof(disk_key));
4777 ptr += sizeof(disk_key);
4778 memcpy(ptr, chunk, item_size);
4779 item_size += sizeof(disk_key);
4780 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004781 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004782
Chris Mason0b86a832008-03-24 15:01:56 -04004783 return 0;
4784}
4785
Miao Xieb2117a32011-01-05 10:07:28 +00004786/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004787 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004788 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004789static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004790{
Arne Jansen73c5de02011-04-12 12:07:57 +02004791 const struct btrfs_device_info *di_a = a;
4792 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004793
Arne Jansen73c5de02011-04-12 12:07:57 +02004794 if (di_a->max_avail > di_b->max_avail)
4795 return -1;
4796 if (di_a->max_avail < di_b->max_avail)
4797 return 1;
4798 if (di_a->total_avail > di_b->total_avail)
4799 return -1;
4800 if (di_a->total_avail < di_b->total_avail)
4801 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004802 return 0;
4803}
4804
David Woodhouse53b381b2013-01-29 18:40:14 -05004805static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4806{
Zhao Leiffe2d202015-01-20 15:11:44 +08004807 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004808 return;
4809
Miao Xieceda0862013-04-11 10:30:16 +00004810 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004811}
4812
David Sterbacfbb8252018-07-10 18:15:05 +02004813static void check_raid1c34_incompat_flag(struct btrfs_fs_info *info, u64 type)
4814{
4815 if (!(type & (BTRFS_BLOCK_GROUP_RAID1C3 | BTRFS_BLOCK_GROUP_RAID1C4)))
4816 return;
4817
4818 btrfs_set_fs_incompat(info, RAID1C34);
4819}
4820
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004821/*
4822 * Structure used internally for __btrfs_alloc_chunk() function.
4823 * Wraps needed parameters.
4824 */
4825struct alloc_chunk_ctl {
4826 u64 start;
4827 u64 type;
4828 /* Total number of stripes to allocate */
4829 int num_stripes;
4830 /* sub_stripes info for map */
4831 int sub_stripes;
4832 /* Stripes per device */
4833 int dev_stripes;
4834 /* Maximum number of devices to use */
4835 int devs_max;
4836 /* Minimum number of devices to use */
4837 int devs_min;
4838 /* ndevs has to be a multiple of this */
4839 int devs_increment;
4840 /* Number of copies */
4841 int ncopies;
4842 /* Number of stripes worth of bytes to store parity information */
4843 int nparity;
4844 u64 max_stripe_size;
4845 u64 max_chunk_size;
Naohiro Aota6aafb302020-02-25 12:56:15 +09004846 u64 dev_extent_min;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09004847 u64 stripe_size;
4848 u64 chunk_size;
4849 int ndevs;
4850};
4851
Naohiro Aota27c314d2020-02-25 12:56:11 +09004852static void init_alloc_chunk_ctl_policy_regular(
4853 struct btrfs_fs_devices *fs_devices,
4854 struct alloc_chunk_ctl *ctl)
4855{
4856 u64 type = ctl->type;
4857
4858 if (type & BTRFS_BLOCK_GROUP_DATA) {
4859 ctl->max_stripe_size = SZ_1G;
4860 ctl->max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
4861 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
4862 /* For larger filesystems, use larger metadata chunks */
4863 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4864 ctl->max_stripe_size = SZ_1G;
4865 else
4866 ctl->max_stripe_size = SZ_256M;
4867 ctl->max_chunk_size = ctl->max_stripe_size;
4868 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
4869 ctl->max_stripe_size = SZ_32M;
4870 ctl->max_chunk_size = 2 * ctl->max_stripe_size;
4871 ctl->devs_max = min_t(int, ctl->devs_max,
4872 BTRFS_MAX_DEVS_SYS_CHUNK);
4873 } else {
4874 BUG();
4875 }
4876
4877 /* We don't want a chunk larger than 10% of writable space */
4878 ctl->max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4879 ctl->max_chunk_size);
Naohiro Aota6aafb302020-02-25 12:56:15 +09004880 ctl->dev_extent_min = BTRFS_STRIPE_LEN * ctl->dev_stripes;
Naohiro Aota27c314d2020-02-25 12:56:11 +09004881}
4882
4883static void init_alloc_chunk_ctl(struct btrfs_fs_devices *fs_devices,
4884 struct alloc_chunk_ctl *ctl)
4885{
4886 int index = btrfs_bg_flags_to_raid_index(ctl->type);
4887
4888 ctl->sub_stripes = btrfs_raid_array[index].sub_stripes;
4889 ctl->dev_stripes = btrfs_raid_array[index].dev_stripes;
4890 ctl->devs_max = btrfs_raid_array[index].devs_max;
4891 if (!ctl->devs_max)
4892 ctl->devs_max = BTRFS_MAX_DEVS(fs_devices->fs_info);
4893 ctl->devs_min = btrfs_raid_array[index].devs_min;
4894 ctl->devs_increment = btrfs_raid_array[index].devs_increment;
4895 ctl->ncopies = btrfs_raid_array[index].ncopies;
4896 ctl->nparity = btrfs_raid_array[index].nparity;
4897 ctl->ndevs = 0;
4898
4899 switch (fs_devices->chunk_alloc_policy) {
4900 case BTRFS_CHUNK_ALLOC_REGULAR:
4901 init_alloc_chunk_ctl_policy_regular(fs_devices, ctl);
4902 break;
4903 default:
4904 BUG();
4905 }
4906}
4907
Naohiro Aota560156c2020-02-25 12:56:12 +09004908static int gather_device_info(struct btrfs_fs_devices *fs_devices,
4909 struct alloc_chunk_ctl *ctl,
4910 struct btrfs_device_info *devices_info)
4911{
4912 struct btrfs_fs_info *info = fs_devices->fs_info;
4913 struct btrfs_device *device;
4914 u64 total_avail;
4915 u64 dev_extent_want = ctl->max_stripe_size * ctl->dev_stripes;
Naohiro Aota560156c2020-02-25 12:56:12 +09004916 int ret;
4917 int ndevs = 0;
4918 u64 max_avail;
4919 u64 dev_offset;
4920
4921 /*
4922 * in the first pass through the devices list, we gather information
4923 * about the available holes on each device.
4924 */
4925 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
4926 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
4927 WARN(1, KERN_ERR
4928 "BTRFS: read-only device in alloc_list\n");
4929 continue;
4930 }
4931
4932 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4933 &device->dev_state) ||
4934 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
4935 continue;
4936
4937 if (device->total_bytes > device->bytes_used)
4938 total_avail = device->total_bytes - device->bytes_used;
4939 else
4940 total_avail = 0;
4941
4942 /* If there is no space on this device, skip it. */
Naohiro Aota6aafb302020-02-25 12:56:15 +09004943 if (total_avail < ctl->dev_extent_min)
Naohiro Aota560156c2020-02-25 12:56:12 +09004944 continue;
4945
4946 ret = find_free_dev_extent(device, dev_extent_want, &dev_offset,
4947 &max_avail);
4948 if (ret && ret != -ENOSPC)
4949 return ret;
4950
4951 if (ret == 0)
4952 max_avail = dev_extent_want;
4953
Naohiro Aota6aafb302020-02-25 12:56:15 +09004954 if (max_avail < ctl->dev_extent_min) {
Naohiro Aota560156c2020-02-25 12:56:12 +09004955 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4956 btrfs_debug(info,
4957 "%s: devid %llu has no free space, have=%llu want=%llu",
4958 __func__, device->devid, max_avail,
Naohiro Aota6aafb302020-02-25 12:56:15 +09004959 ctl->dev_extent_min);
Naohiro Aota560156c2020-02-25 12:56:12 +09004960 continue;
4961 }
4962
4963 if (ndevs == fs_devices->rw_devices) {
4964 WARN(1, "%s: found more than %llu devices\n",
4965 __func__, fs_devices->rw_devices);
4966 break;
4967 }
4968 devices_info[ndevs].dev_offset = dev_offset;
4969 devices_info[ndevs].max_avail = max_avail;
4970 devices_info[ndevs].total_avail = total_avail;
4971 devices_info[ndevs].dev = device;
4972 ++ndevs;
4973 }
4974 ctl->ndevs = ndevs;
4975
4976 /*
4977 * now sort the devices by hole size / available space
4978 */
4979 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4980 btrfs_cmp_device_info, NULL);
4981
4982 return 0;
4983}
4984
Naohiro Aota5badf512020-02-25 12:56:13 +09004985static int decide_stripe_size_regular(struct alloc_chunk_ctl *ctl,
4986 struct btrfs_device_info *devices_info)
4987{
4988 /* Number of stripes that count for block group size */
4989 int data_stripes;
4990
4991 /*
4992 * The primary goal is to maximize the number of stripes, so use as
4993 * many devices as possible, even if the stripes are not maximum sized.
4994 *
4995 * The DUP profile stores more than one stripe per device, the
4996 * max_avail is the total size so we have to adjust.
4997 */
4998 ctl->stripe_size = div_u64(devices_info[ctl->ndevs - 1].max_avail,
4999 ctl->dev_stripes);
5000 ctl->num_stripes = ctl->ndevs * ctl->dev_stripes;
5001
5002 /* This will have to be fixed for RAID1 and RAID10 over more drives */
5003 data_stripes = (ctl->num_stripes - ctl->nparity) / ctl->ncopies;
5004
5005 /*
5006 * Use the number of data stripes to figure out how big this chunk is
5007 * really going to be in terms of logical address space, and compare
5008 * that answer with the max chunk size. If it's higher, we try to
5009 * reduce stripe_size.
5010 */
5011 if (ctl->stripe_size * data_stripes > ctl->max_chunk_size) {
5012 /*
5013 * Reduce stripe_size, round it up to a 16MB boundary again and
5014 * then use it, unless it ends up being even bigger than the
5015 * previous value we had already.
5016 */
5017 ctl->stripe_size = min(round_up(div_u64(ctl->max_chunk_size,
5018 data_stripes), SZ_16M),
5019 ctl->stripe_size);
5020 }
5021
5022 /* Align to BTRFS_STRIPE_LEN */
5023 ctl->stripe_size = round_down(ctl->stripe_size, BTRFS_STRIPE_LEN);
5024 ctl->chunk_size = ctl->stripe_size * data_stripes;
5025
5026 return 0;
5027}
5028
5029static int decide_stripe_size(struct btrfs_fs_devices *fs_devices,
5030 struct alloc_chunk_ctl *ctl,
5031 struct btrfs_device_info *devices_info)
5032{
5033 struct btrfs_fs_info *info = fs_devices->fs_info;
5034
5035 /*
5036 * Round down to number of usable stripes, devs_increment can be any
5037 * number so we can't use round_down() that requires power of 2, while
5038 * rounddown is safe.
5039 */
5040 ctl->ndevs = rounddown(ctl->ndevs, ctl->devs_increment);
5041
5042 if (ctl->ndevs < ctl->devs_min) {
5043 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5044 btrfs_debug(info,
5045 "%s: not enough devices with free space: have=%d minimum required=%d",
5046 __func__, ctl->ndevs, ctl->devs_min);
5047 }
5048 return -ENOSPC;
5049 }
5050
5051 ctl->ndevs = min(ctl->ndevs, ctl->devs_max);
5052
5053 switch (fs_devices->chunk_alloc_policy) {
5054 case BTRFS_CHUNK_ALLOC_REGULAR:
5055 return decide_stripe_size_regular(ctl, devices_info);
5056 default:
5057 BUG();
5058 }
5059}
5060
Naohiro Aotadce580c2020-02-25 12:56:14 +09005061static int create_chunk(struct btrfs_trans_handle *trans,
5062 struct alloc_chunk_ctl *ctl,
5063 struct btrfs_device_info *devices_info)
5064{
5065 struct btrfs_fs_info *info = trans->fs_info;
5066 struct map_lookup *map = NULL;
5067 struct extent_map_tree *em_tree;
5068 struct extent_map *em;
5069 u64 start = ctl->start;
5070 u64 type = ctl->type;
5071 int ret;
5072 int i;
5073 int j;
5074
5075 map = kmalloc(map_lookup_size(ctl->num_stripes), GFP_NOFS);
5076 if (!map)
5077 return -ENOMEM;
5078 map->num_stripes = ctl->num_stripes;
5079
5080 for (i = 0; i < ctl->ndevs; ++i) {
5081 for (j = 0; j < ctl->dev_stripes; ++j) {
5082 int s = i * ctl->dev_stripes + j;
5083 map->stripes[s].dev = devices_info[i].dev;
5084 map->stripes[s].physical = devices_info[i].dev_offset +
5085 j * ctl->stripe_size;
5086 }
5087 }
5088 map->stripe_len = BTRFS_STRIPE_LEN;
5089 map->io_align = BTRFS_STRIPE_LEN;
5090 map->io_width = BTRFS_STRIPE_LEN;
5091 map->type = type;
5092 map->sub_stripes = ctl->sub_stripes;
5093
5094 trace_btrfs_chunk_alloc(info, map, start, ctl->chunk_size);
5095
5096 em = alloc_extent_map();
5097 if (!em) {
5098 kfree(map);
5099 return -ENOMEM;
5100 }
5101 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5102 em->map_lookup = map;
5103 em->start = start;
5104 em->len = ctl->chunk_size;
5105 em->block_start = 0;
5106 em->block_len = em->len;
5107 em->orig_block_len = ctl->stripe_size;
5108
5109 em_tree = &info->mapping_tree;
5110 write_lock(&em_tree->lock);
5111 ret = add_extent_mapping(em_tree, em, 0);
5112 if (ret) {
5113 write_unlock(&em_tree->lock);
5114 free_extent_map(em);
5115 return ret;
5116 }
5117 write_unlock(&em_tree->lock);
5118
5119 ret = btrfs_make_block_group(trans, 0, type, start, ctl->chunk_size);
5120 if (ret)
5121 goto error_del_extent;
5122
5123 for (i = 0; i < map->num_stripes; i++) {
5124 struct btrfs_device *dev = map->stripes[i].dev;
5125
5126 btrfs_device_set_bytes_used(dev,
5127 dev->bytes_used + ctl->stripe_size);
5128 if (list_empty(&dev->post_commit_list))
5129 list_add_tail(&dev->post_commit_list,
5130 &trans->transaction->dev_update_list);
5131 }
5132
5133 atomic64_sub(ctl->stripe_size * map->num_stripes,
5134 &info->free_chunk_space);
5135
5136 free_extent_map(em);
5137 check_raid56_incompat_flag(info, type);
5138 check_raid1c34_incompat_flag(info, type);
5139
5140 return 0;
5141
5142error_del_extent:
5143 write_lock(&em_tree->lock);
5144 remove_extent_mapping(em_tree, em);
5145 write_unlock(&em_tree->lock);
5146
5147 /* One for our allocation */
5148 free_extent_map(em);
5149 /* One for the tree reference */
5150 free_extent_map(em);
5151
5152 return ret;
5153}
5154
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005155int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00005156{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005157 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00005158 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Arne Jansen73c5de02011-04-12 12:07:57 +02005159 struct btrfs_device_info *devices_info = NULL;
Naohiro Aota4f2bafe2020-02-25 12:56:10 +09005160 struct alloc_chunk_ctl ctl;
Miao Xieb2117a32011-01-05 10:07:28 +00005161 int ret;
Miao Xieb2117a32011-01-05 10:07:28 +00005162
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005163 lockdep_assert_held(&info->chunk_mutex);
5164
Naohiro Aotab25c19f2020-02-25 12:56:07 +09005165 if (!alloc_profile_is_valid(type, 0)) {
5166 ASSERT(0);
5167 return -EINVAL;
5168 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005169
Qu Wenruo4117f202018-01-22 13:50:54 +08005170 if (list_empty(&fs_devices->alloc_list)) {
5171 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5172 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00005173 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005174 }
Miao Xieb2117a32011-01-05 10:07:28 +00005175
Naohiro Aota27c314d2020-02-25 12:56:11 +09005176 if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
5177 btrfs_err(info, "invalid chunk type 0x%llx requested", type);
5178 ASSERT(0);
5179 return -EINVAL;
Arne Jansen73c5de02011-04-12 12:07:57 +02005180 }
5181
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005182 ctl.start = find_next_chunk(info);
Naohiro Aota27c314d2020-02-25 12:56:11 +09005183 ctl.type = type;
5184 init_alloc_chunk_ctl(fs_devices, &ctl);
Miao Xieb2117a32011-01-05 10:07:28 +00005185
David Sterba31e818f2015-02-20 18:00:26 +01005186 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00005187 GFP_NOFS);
5188 if (!devices_info)
5189 return -ENOMEM;
5190
Naohiro Aota560156c2020-02-25 12:56:12 +09005191 ret = gather_device_info(fs_devices, &ctl, devices_info);
5192 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005193 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005194
Naohiro Aota5badf512020-02-25 12:56:13 +09005195 ret = decide_stripe_size(fs_devices, &ctl, devices_info);
5196 if (ret < 0)
Naohiro Aotadce580c2020-02-25 12:56:14 +09005197 goto out;
Arne Jansen73c5de02011-04-12 12:07:57 +02005198
Naohiro Aotadce580c2020-02-25 12:56:14 +09005199 ret = create_chunk(trans, &ctl, devices_info);
Chris Mason9b3f68b2008-04-18 10:29:51 -04005200
Naohiro Aotadce580c2020-02-25 12:56:14 +09005201out:
Miao Xieb2117a32011-01-05 10:07:28 +00005202 kfree(devices_info);
5203 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005204}
5205
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005206/*
5207 * Chunk allocation falls into two parts. The first part does work
5208 * that makes the new allocated chunk usable, but does not do any operation
5209 * that modifies the chunk tree. The second part does the work that
5210 * requires modifying the chunk tree. This division is important for the
5211 * bootstrap process of adding storage to a seed btrfs.
5212 */
Josef Bacik6df9a952013-06-27 13:22:46 -04005213int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005214 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005215{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005216 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005217 struct btrfs_root *extent_root = fs_info->extent_root;
5218 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005219 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005220 struct btrfs_device *device;
5221 struct btrfs_chunk *chunk;
5222 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005223 struct extent_map *em;
5224 struct map_lookup *map;
5225 size_t item_size;
5226 u64 dev_offset;
5227 u64 stripe_size;
5228 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005229 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005230
Omar Sandoval60ca8422018-05-16 16:34:31 -07005231 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005232 if (IS_ERR(em))
5233 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005234
Jeff Mahoney95617d62015-06-03 10:55:48 -04005235 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005236 item_size = btrfs_chunk_item_size(map->num_stripes);
5237 stripe_size = em->orig_block_len;
5238
5239 chunk = kzalloc(item_size, GFP_NOFS);
5240 if (!chunk) {
5241 ret = -ENOMEM;
5242 goto out;
5243 }
5244
Filipe Manana50460e32015-11-20 10:42:47 +00005245 /*
5246 * Take the device list mutex to prevent races with the final phase of
5247 * a device replace operation that replaces the device object associated
5248 * with the map's stripes, because the device object's id can change
5249 * at any time during that final phase of the device replace operation
5250 * (dev-replace.c:btrfs_dev_replace_finishing()).
5251 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005252 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005253 for (i = 0; i < map->num_stripes; i++) {
5254 device = map->stripes[i].dev;
5255 dev_offset = map->stripes[i].physical;
5256
Yan Zheng2b820322008-11-17 21:11:30 -05005257 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005258 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005259 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005260 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5261 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005262 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005263 break;
5264 }
5265 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005266 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005267 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005268 }
5269
Yan Zheng2b820322008-11-17 21:11:30 -05005270 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005271 for (i = 0; i < map->num_stripes; i++) {
5272 device = map->stripes[i].dev;
5273 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005274
5275 btrfs_set_stack_stripe_devid(stripe, device->devid);
5276 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5277 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5278 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005279 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005280 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005281
5282 btrfs_set_stack_chunk_length(chunk, chunk_size);
5283 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5284 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5285 btrfs_set_stack_chunk_type(chunk, map->type);
5286 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5287 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5288 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005289 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005290 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5291
5292 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5293 key.type = BTRFS_CHUNK_ITEM_KEY;
5294 key.offset = chunk_offset;
5295
5296 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005297 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5298 /*
5299 * TODO: Cleanup of inserted chunk root in case of
5300 * failure.
5301 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005302 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005303 }
liubo1abe9b82011-03-24 11:18:59 +00005304
Josef Bacik6df9a952013-06-27 13:22:46 -04005305out:
Yan Zheng2b820322008-11-17 21:11:30 -05005306 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005307 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005308 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005309}
5310
David Sterba6f8e0fc2019-03-20 16:29:13 +01005311static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005312{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005313 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005314 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005315 int ret;
5316
Jeff Mahoney1b868262017-05-17 11:38:35 -04005317 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005318 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005319 if (ret)
5320 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005321
Jeff Mahoney1b868262017-05-17 11:38:35 -04005322 alloc_profile = btrfs_system_alloc_profile(fs_info);
Nikolay Borisov11c67b12020-03-02 12:29:25 +02005323 ret = btrfs_alloc_chunk(trans, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005324 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005325}
5326
Miao Xied20983b2014-07-03 18:22:13 +08005327static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5328{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005329 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005330
David Sterbafc9a2ac2019-05-17 11:43:22 +02005331 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005332}
5333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005334int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005335{
5336 struct extent_map *em;
5337 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005338 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005339 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005340 int i;
5341
Omar Sandoval60ca8422018-05-16 16:34:31 -07005342 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005343 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005344 return 1;
5345
Jeff Mahoney95617d62015-06-03 10:55:48 -04005346 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005347 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005348 if (test_bit(BTRFS_DEV_STATE_MISSING,
5349 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005350 miss_ndevs++;
5351 continue;
5352 }
Anand Jainebbede42017-12-04 12:54:52 +08005353 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5354 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005355 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005356 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005357 }
5358 }
Miao Xied20983b2014-07-03 18:22:13 +08005359
5360 /*
5361 * If the number of missing devices is larger than max errors,
5362 * we can not write the data into that chunk successfully, so
5363 * set it readonly.
5364 */
5365 if (miss_ndevs > btrfs_chunk_max_errors(map))
5366 readonly = 1;
5367end:
Yan Zheng2b820322008-11-17 21:11:30 -05005368 free_extent_map(em);
5369 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005370}
5371
David Sterbac8bf1b62019-05-17 11:43:17 +02005372void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005373{
5374 struct extent_map *em;
5375
Chris Masond3977122009-01-05 21:25:51 -05005376 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005377 write_lock(&tree->lock);
5378 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005379 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005380 remove_extent_mapping(tree, em);
5381 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005382 if (!em)
5383 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005384 /* once for us */
5385 free_extent_map(em);
5386 /* once for the tree */
5387 free_extent_map(em);
5388 }
5389}
5390
Stefan Behrens5d964052012-11-05 14:59:07 +01005391int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005392{
5393 struct extent_map *em;
5394 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005395 int ret;
5396
Omar Sandoval60ca8422018-05-16 16:34:31 -07005397 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005398 if (IS_ERR(em))
5399 /*
5400 * We could return errors for these cases, but that could get
5401 * ugly and we'd probably do the same thing which is just not do
5402 * anything else and exit, so return 1 so the callers don't try
5403 * to use other copies.
5404 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005405 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005406
Jeff Mahoney95617d62015-06-03 10:55:48 -04005407 map = em->map_lookup;
David Sterbac7369b32019-05-31 15:39:31 +02005408 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1_MASK))
Chris Masonf1885912008-04-09 16:28:12 -04005409 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005410 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5411 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005412 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5413 ret = 2;
5414 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005415 /*
5416 * There could be two corrupted data stripes, we need
5417 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005418 *
Liu Bo8810f752018-01-02 13:36:41 -07005419 * Fail a stripe at a time on every retry except the
5420 * stripe under reconstruction.
5421 */
5422 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005423 else
5424 ret = 1;
5425 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005426
David Sterbacb5583d2018-09-07 16:11:23 +02005427 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005428 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5429 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005430 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005431 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005432
Chris Masonf1885912008-04-09 16:28:12 -04005433 return ret;
5434}
5435
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005436unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005437 u64 logical)
5438{
5439 struct extent_map *em;
5440 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005441 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005442
Omar Sandoval60ca8422018-05-16 16:34:31 -07005443 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005444
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005445 if (!WARN_ON(IS_ERR(em))) {
5446 map = em->map_lookup;
5447 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5448 len = map->stripe_len * nr_data_stripes(map);
5449 free_extent_map(em);
5450 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005451 return len;
5452}
5453
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005454int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005455{
5456 struct extent_map *em;
5457 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005458 int ret = 0;
5459
Omar Sandoval60ca8422018-05-16 16:34:31 -07005460 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005461
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005462 if(!WARN_ON(IS_ERR(em))) {
5463 map = em->map_lookup;
5464 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5465 ret = 1;
5466 free_extent_map(em);
5467 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005468 return ret;
5469}
5470
Stefan Behrens30d98612012-11-06 14:52:18 +01005471static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005472 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005473 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005474{
5475 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005476 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005477 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005478 int tolerance;
5479 struct btrfs_device *srcdev;
5480
Anand Jain99f92a72018-03-14 16:29:12 +08005481 ASSERT((map->type &
David Sterbac7369b32019-05-31 15:39:31 +02005482 (BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10)));
Anand Jain99f92a72018-03-14 16:29:12 +08005483
5484 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5485 num_stripes = map->sub_stripes;
5486 else
5487 num_stripes = map->num_stripes;
5488
Anand Jain8ba0ae72018-03-14 16:29:13 +08005489 preferred_mirror = first + current->pid % num_stripes;
5490
Stefan Behrens30d98612012-11-06 14:52:18 +01005491 if (dev_replace_is_ongoing &&
5492 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5493 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5494 srcdev = fs_info->dev_replace.srcdev;
5495 else
5496 srcdev = NULL;
5497
5498 /*
5499 * try to avoid the drive that is the source drive for a
5500 * dev-replace procedure, only choose it if no other non-missing
5501 * mirror is available
5502 */
5503 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005504 if (map->stripes[preferred_mirror].dev->bdev &&
5505 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5506 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005507 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005508 if (map->stripes[i].dev->bdev &&
5509 (tolerance || map->stripes[i].dev != srcdev))
5510 return i;
5511 }
Chris Masondfe25022008-05-13 13:46:40 -04005512 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005513
Chris Masondfe25022008-05-13 13:46:40 -04005514 /* we couldn't find one that doesn't fail. Just return something
5515 * and the io error handling code will clean up eventually
5516 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005517 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005518}
5519
David Woodhouse53b381b2013-01-29 18:40:14 -05005520/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005521static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005522{
David Woodhouse53b381b2013-01-29 18:40:14 -05005523 int i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005524 int again = 1;
5525
5526 while (again) {
5527 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005528 for (i = 0; i < num_stripes - 1; i++) {
David Sterbaeeb6f172019-11-28 15:31:17 +01005529 /* Swap if parity is on a smaller index */
5530 if (bbio->raid_map[i] > bbio->raid_map[i + 1]) {
5531 swap(bbio->stripes[i], bbio->stripes[i + 1]);
5532 swap(bbio->raid_map[i], bbio->raid_map[i + 1]);
David Woodhouse53b381b2013-01-29 18:40:14 -05005533 again = 1;
5534 }
5535 }
5536 }
5537}
5538
Zhao Lei6e9606d2015-01-20 15:11:34 +08005539static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5540{
5541 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005542 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005543 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005544 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005545 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005546 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005547 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005548 /*
5549 * plus the raid_map, which includes both the tgt dev
5550 * and the stripes
5551 */
5552 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005553 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005554
5555 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005556 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005557
Nikolay Borisov608769a2020-07-02 16:46:41 +03005558 bbio->tgtdev_map = (int *)(bbio->stripes + total_stripes);
5559 bbio->raid_map = (u64 *)(bbio->tgtdev_map + real_stripes);
5560
Zhao Lei6e9606d2015-01-20 15:11:34 +08005561 return bbio;
5562}
5563
5564void btrfs_get_bbio(struct btrfs_bio *bbio)
5565{
Elena Reshetova140475a2017-03-03 10:55:10 +02005566 WARN_ON(!refcount_read(&bbio->refs));
5567 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005568}
5569
5570void btrfs_put_bbio(struct btrfs_bio *bbio)
5571{
5572 if (!bbio)
5573 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005574 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005575 kfree(bbio);
5576}
5577
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005578/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5579/*
5580 * Please note that, discard won't be sent to target device of device
5581 * replace.
5582 */
5583static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005584 u64 logical, u64 *length_ret,
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005585 struct btrfs_bio **bbio_ret)
5586{
5587 struct extent_map *em;
5588 struct map_lookup *map;
5589 struct btrfs_bio *bbio;
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005590 u64 length = *length_ret;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005591 u64 offset;
5592 u64 stripe_nr;
5593 u64 stripe_nr_end;
5594 u64 stripe_end_offset;
5595 u64 stripe_cnt;
5596 u64 stripe_len;
5597 u64 stripe_offset;
5598 u64 num_stripes;
5599 u32 stripe_index;
5600 u32 factor = 0;
5601 u32 sub_stripes = 0;
5602 u64 stripes_per_dev = 0;
5603 u32 remaining_stripes = 0;
5604 u32 last_stripe = 0;
5605 int ret = 0;
5606 int i;
5607
5608 /* discard always return a bbio */
5609 ASSERT(bbio_ret);
5610
Omar Sandoval60ca8422018-05-16 16:34:31 -07005611 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005612 if (IS_ERR(em))
5613 return PTR_ERR(em);
5614
5615 map = em->map_lookup;
5616 /* we don't discard raid56 yet */
5617 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5618 ret = -EOPNOTSUPP;
5619 goto out;
5620 }
5621
5622 offset = logical - em->start;
Qu Wenruo2d974612019-10-23 21:57:26 +08005623 length = min_t(u64, em->start + em->len - logical, length);
Qu Wenruo6b7faad2019-10-23 21:57:27 +08005624 *length_ret = length;
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005625
5626 stripe_len = map->stripe_len;
5627 /*
5628 * stripe_nr counts the total number of stripes we have to stride
5629 * to get to this block
5630 */
5631 stripe_nr = div64_u64(offset, stripe_len);
5632
5633 /* stripe_offset is the offset of this block in its stripe */
5634 stripe_offset = offset - stripe_nr * stripe_len;
5635
5636 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005637 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005638 stripe_cnt = stripe_nr_end - stripe_nr;
5639 stripe_end_offset = stripe_nr_end * map->stripe_len -
5640 (offset + length);
5641 /*
5642 * after this, stripe_nr is the number of stripes on this
5643 * device we have to walk to find the data, and stripe_index is
5644 * the number of our device in the stripe array
5645 */
5646 num_stripes = 1;
5647 stripe_index = 0;
5648 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5649 BTRFS_BLOCK_GROUP_RAID10)) {
5650 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5651 sub_stripes = 1;
5652 else
5653 sub_stripes = map->sub_stripes;
5654
5655 factor = map->num_stripes / sub_stripes;
5656 num_stripes = min_t(u64, map->num_stripes,
5657 sub_stripes * stripe_cnt);
5658 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5659 stripe_index *= sub_stripes;
5660 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5661 &remaining_stripes);
5662 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5663 last_stripe *= sub_stripes;
David Sterbac7369b32019-05-31 15:39:31 +02005664 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1_MASK |
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005665 BTRFS_BLOCK_GROUP_DUP)) {
5666 num_stripes = map->num_stripes;
5667 } else {
5668 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5669 &stripe_index);
5670 }
5671
5672 bbio = alloc_btrfs_bio(num_stripes, 0);
5673 if (!bbio) {
5674 ret = -ENOMEM;
5675 goto out;
5676 }
5677
5678 for (i = 0; i < num_stripes; i++) {
5679 bbio->stripes[i].physical =
5680 map->stripes[stripe_index].physical +
5681 stripe_offset + stripe_nr * map->stripe_len;
5682 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5683
5684 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5685 BTRFS_BLOCK_GROUP_RAID10)) {
5686 bbio->stripes[i].length = stripes_per_dev *
5687 map->stripe_len;
5688
5689 if (i / sub_stripes < remaining_stripes)
5690 bbio->stripes[i].length +=
5691 map->stripe_len;
5692
5693 /*
5694 * Special for the first stripe and
5695 * the last stripe:
5696 *
5697 * |-------|...|-------|
5698 * |----------|
5699 * off end_off
5700 */
5701 if (i < sub_stripes)
5702 bbio->stripes[i].length -=
5703 stripe_offset;
5704
5705 if (stripe_index >= last_stripe &&
5706 stripe_index <= (last_stripe +
5707 sub_stripes - 1))
5708 bbio->stripes[i].length -=
5709 stripe_end_offset;
5710
5711 if (i == sub_stripes - 1)
5712 stripe_offset = 0;
5713 } else {
5714 bbio->stripes[i].length = length;
5715 }
5716
5717 stripe_index++;
5718 if (stripe_index == map->num_stripes) {
5719 stripe_index = 0;
5720 stripe_nr++;
5721 }
5722 }
5723
5724 *bbio_ret = bbio;
5725 bbio->map_type = map->type;
5726 bbio->num_stripes = num_stripes;
5727out:
5728 free_extent_map(em);
5729 return ret;
5730}
5731
Liu Bo5ab56092017-03-14 13:33:57 -07005732/*
5733 * In dev-replace case, for repair case (that's the only case where the mirror
5734 * is selected explicitly when calling btrfs_map_block), blocks left of the
5735 * left cursor can also be read from the target drive.
5736 *
5737 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5738 * array of stripes.
5739 * For READ, it also needs to be supported using the same mirror number.
5740 *
5741 * If the requested block is not left of the left cursor, EIO is returned. This
5742 * can happen because btrfs_num_copies() returns one more in the dev-replace
5743 * case.
5744 */
5745static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5746 u64 logical, u64 length,
5747 u64 srcdev_devid, int *mirror_num,
5748 u64 *physical)
5749{
5750 struct btrfs_bio *bbio = NULL;
5751 int num_stripes;
5752 int index_srcdev = 0;
5753 int found = 0;
5754 u64 physical_of_found = 0;
5755 int i;
5756 int ret = 0;
5757
5758 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5759 logical, &length, &bbio, 0, 0);
5760 if (ret) {
5761 ASSERT(bbio == NULL);
5762 return ret;
5763 }
5764
5765 num_stripes = bbio->num_stripes;
5766 if (*mirror_num > num_stripes) {
5767 /*
5768 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5769 * that means that the requested area is not left of the left
5770 * cursor
5771 */
5772 btrfs_put_bbio(bbio);
5773 return -EIO;
5774 }
5775
5776 /*
5777 * process the rest of the function using the mirror_num of the source
5778 * drive. Therefore look it up first. At the end, patch the device
5779 * pointer to the one of the target drive.
5780 */
5781 for (i = 0; i < num_stripes; i++) {
5782 if (bbio->stripes[i].dev->devid != srcdev_devid)
5783 continue;
5784
5785 /*
5786 * In case of DUP, in order to keep it simple, only add the
5787 * mirror with the lowest physical address
5788 */
5789 if (found &&
5790 physical_of_found <= bbio->stripes[i].physical)
5791 continue;
5792
5793 index_srcdev = i;
5794 found = 1;
5795 physical_of_found = bbio->stripes[i].physical;
5796 }
5797
5798 btrfs_put_bbio(bbio);
5799
5800 ASSERT(found);
5801 if (!found)
5802 return -EIO;
5803
5804 *mirror_num = index_srcdev + 1;
5805 *physical = physical_of_found;
5806 return ret;
5807}
5808
Liu Bo73c0f222017-03-14 13:33:58 -07005809static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5810 struct btrfs_bio **bbio_ret,
5811 struct btrfs_dev_replace *dev_replace,
5812 int *num_stripes_ret, int *max_errors_ret)
5813{
5814 struct btrfs_bio *bbio = *bbio_ret;
5815 u64 srcdev_devid = dev_replace->srcdev->devid;
5816 int tgtdev_indexes = 0;
5817 int num_stripes = *num_stripes_ret;
5818 int max_errors = *max_errors_ret;
5819 int i;
5820
5821 if (op == BTRFS_MAP_WRITE) {
5822 int index_where_to_add;
5823
5824 /*
5825 * duplicate the write operations while the dev replace
5826 * procedure is running. Since the copying of the old disk to
5827 * the new disk takes place at run time while the filesystem is
5828 * mounted writable, the regular write operations to the old
5829 * disk have to be duplicated to go to the new disk as well.
5830 *
5831 * Note that device->missing is handled by the caller, and that
5832 * the write to the old disk is already set up in the stripes
5833 * array.
5834 */
5835 index_where_to_add = num_stripes;
5836 for (i = 0; i < num_stripes; i++) {
5837 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5838 /* write to new disk, too */
5839 struct btrfs_bio_stripe *new =
5840 bbio->stripes + index_where_to_add;
5841 struct btrfs_bio_stripe *old =
5842 bbio->stripes + i;
5843
5844 new->physical = old->physical;
5845 new->length = old->length;
5846 new->dev = dev_replace->tgtdev;
5847 bbio->tgtdev_map[i] = index_where_to_add;
5848 index_where_to_add++;
5849 max_errors++;
5850 tgtdev_indexes++;
5851 }
5852 }
5853 num_stripes = index_where_to_add;
5854 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5855 int index_srcdev = 0;
5856 int found = 0;
5857 u64 physical_of_found = 0;
5858
5859 /*
5860 * During the dev-replace procedure, the target drive can also
5861 * be used to read data in case it is needed to repair a corrupt
5862 * block elsewhere. This is possible if the requested area is
5863 * left of the left cursor. In this area, the target drive is a
5864 * full copy of the source drive.
5865 */
5866 for (i = 0; i < num_stripes; i++) {
5867 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5868 /*
5869 * In case of DUP, in order to keep it simple,
5870 * only add the mirror with the lowest physical
5871 * address
5872 */
5873 if (found &&
5874 physical_of_found <=
5875 bbio->stripes[i].physical)
5876 continue;
5877 index_srcdev = i;
5878 found = 1;
5879 physical_of_found = bbio->stripes[i].physical;
5880 }
5881 }
5882 if (found) {
5883 struct btrfs_bio_stripe *tgtdev_stripe =
5884 bbio->stripes + num_stripes;
5885
5886 tgtdev_stripe->physical = physical_of_found;
5887 tgtdev_stripe->length =
5888 bbio->stripes[index_srcdev].length;
5889 tgtdev_stripe->dev = dev_replace->tgtdev;
5890 bbio->tgtdev_map[index_srcdev] = num_stripes;
5891
5892 tgtdev_indexes++;
5893 num_stripes++;
5894 }
5895 }
5896
5897 *num_stripes_ret = num_stripes;
5898 *max_errors_ret = max_errors;
5899 bbio->num_tgtdevs = tgtdev_indexes;
5900 *bbio_ret = bbio;
5901}
5902
Liu Bo2b19a1f2017-03-14 13:34:00 -07005903static bool need_full_stripe(enum btrfs_map_op op)
5904{
5905 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5906}
5907
Nikolay Borisov5f141122019-06-03 12:05:03 +03005908/*
5909 * btrfs_get_io_geometry - calculates the geomery of a particular (address, len)
5910 * tuple. This information is used to calculate how big a
5911 * particular bio can get before it straddles a stripe.
5912 *
5913 * @fs_info - the filesystem
5914 * @logical - address that we want to figure out the geometry of
5915 * @len - the length of IO we are going to perform, starting at @logical
5916 * @op - type of operation - write or read
5917 * @io_geom - pointer used to return values
5918 *
5919 * Returns < 0 in case a chunk for the given logical address cannot be found,
5920 * usually shouldn't happen unless @logical is corrupted, 0 otherwise.
5921 */
5922int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Nikolay Borisov89b798a2019-06-03 12:05:05 +03005923 u64 logical, u64 len, struct btrfs_io_geometry *io_geom)
Nikolay Borisov5f141122019-06-03 12:05:03 +03005924{
5925 struct extent_map *em;
5926 struct map_lookup *map;
5927 u64 offset;
5928 u64 stripe_offset;
5929 u64 stripe_nr;
5930 u64 stripe_len;
5931 u64 raid56_full_stripe_start = (u64)-1;
5932 int data_stripes;
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005933 int ret = 0;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005934
5935 ASSERT(op != BTRFS_MAP_DISCARD);
5936
5937 em = btrfs_get_chunk_map(fs_info, logical, len);
5938 if (IS_ERR(em))
5939 return PTR_ERR(em);
5940
5941 map = em->map_lookup;
5942 /* Offset of this logical address in the chunk */
5943 offset = logical - em->start;
5944 /* Len of a stripe in a chunk */
5945 stripe_len = map->stripe_len;
5946 /* Stripe wher this block falls in */
5947 stripe_nr = div64_u64(offset, stripe_len);
5948 /* Offset of stripe in the chunk */
5949 stripe_offset = stripe_nr * stripe_len;
5950 if (offset < stripe_offset) {
5951 btrfs_crit(fs_info,
5952"stripe math has gone wrong, stripe_offset=%llu offset=%llu start=%llu logical=%llu stripe_len=%llu",
5953 stripe_offset, offset, em->start, logical, stripe_len);
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02005954 ret = -EINVAL;
5955 goto out;
Nikolay Borisov5f141122019-06-03 12:05:03 +03005956 }
5957
5958 /* stripe_offset is the offset of this block in its stripe */
5959 stripe_offset = offset - stripe_offset;
5960 data_stripes = nr_data_stripes(map);
5961
5962 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5963 u64 max_len = stripe_len - stripe_offset;
5964
5965 /*
5966 * In case of raid56, we need to know the stripe aligned start
5967 */
5968 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5969 unsigned long full_stripe_len = stripe_len * data_stripes;
5970 raid56_full_stripe_start = offset;
5971
5972 /*
5973 * Allow a write of a full stripe, but make sure we
5974 * don't allow straddling of stripes
5975 */
5976 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5977 full_stripe_len);
5978 raid56_full_stripe_start *= full_stripe_len;
5979
5980 /*
5981 * For writes to RAID[56], allow a full stripeset across
5982 * all disks. For other RAID types and for RAID[56]
5983 * reads, just allow a single stripe (on a single disk).
5984 */
5985 if (op == BTRFS_MAP_WRITE) {
5986 max_len = stripe_len * data_stripes -
5987 (offset - raid56_full_stripe_start);
5988 }
5989 }
5990 len = min_t(u64, em->len - offset, max_len);
5991 } else {
5992 len = em->len - offset;
5993 }
5994
5995 io_geom->len = len;
5996 io_geom->offset = offset;
5997 io_geom->stripe_len = stripe_len;
5998 io_geom->stripe_nr = stripe_nr;
5999 io_geom->stripe_offset = stripe_offset;
6000 io_geom->raid56_stripe_offset = raid56_full_stripe_start;
6001
Johannes Thumshirn373c3b82019-07-15 15:16:12 +02006002out:
6003 /* once for us */
6004 free_extent_map(em);
6005 return ret;
Nikolay Borisov5f141122019-06-03 12:05:03 +03006006}
6007
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006008static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
6009 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006010 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006011 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006012 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04006013{
6014 struct extent_map *em;
6015 struct map_lookup *map;
Chris Mason593060d2008-03-25 16:50:33 -04006016 u64 stripe_offset;
6017 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006018 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01006019 u32 stripe_index;
David Sterbacff82672019-05-17 11:43:45 +02006020 int data_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04006021 int i;
Li Zefande11cc12011-12-01 12:55:47 +08006022 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04006023 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04006024 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006025 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006026 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01006027 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
6028 int dev_replace_is_ongoing = 0;
6029 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006030 int patch_the_first_stripe_for_dev_replace = 0;
6031 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006032 u64 raid56_full_stripe_start = (u64)-1;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006033 struct btrfs_io_geometry geom;
6034
6035 ASSERT(bbio_ret);
David Sterba75fb2e92018-08-03 00:36:29 +02006036 ASSERT(op != BTRFS_MAP_DISCARD);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006037
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006038 ret = btrfs_get_io_geometry(fs_info, op, logical, *length, &geom);
6039 if (ret < 0)
6040 return ret;
6041
Omar Sandoval60ca8422018-05-16 16:34:31 -07006042 em = btrfs_get_chunk_map(fs_info, logical, *length);
Dan Carpenterf1136982019-08-09 17:07:39 +03006043 ASSERT(!IS_ERR(em));
Jeff Mahoney95617d62015-06-03 10:55:48 -04006044 map = em->map_lookup;
Chris Mason593060d2008-03-25 16:50:33 -04006045
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006046 *length = geom.len;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03006047 stripe_len = geom.stripe_len;
6048 stripe_nr = geom.stripe_nr;
6049 stripe_offset = geom.stripe_offset;
6050 raid56_full_stripe_start = geom.raid56_stripe_offset;
David Sterbacff82672019-05-17 11:43:45 +02006051 data_stripes = nr_data_stripes(map);
Chris Mason593060d2008-03-25 16:50:33 -04006052
David Sterbacb5583d2018-09-07 16:11:23 +02006053 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006054 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006055 /*
6056 * Hold the semaphore for read during the whole operation, write is
6057 * requested at commit time but must wait.
6058 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006059 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006060 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006061
Stefan Behrensad6d6202012-11-06 15:06:47 +01006062 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006063 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006064 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6065 dev_replace->srcdev->devid,
6066 &mirror_num,
6067 &physical_to_patch_in_first_stripe);
6068 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006069 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006070 else
6071 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006072 } else if (mirror_num > map->num_stripes) {
6073 mirror_num = 0;
6074 }
6075
Chris Masonf2d8d742008-04-21 10:03:05 -04006076 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006077 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006078 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006079 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6080 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006081 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006082 mirror_num = 1;
David Sterbac7369b32019-05-31 15:39:31 +02006083 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006084 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006085 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006086 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006087 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006088 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006089 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006090 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006091 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006092 }
Chris Mason2fff7342008-04-29 14:12:09 -04006093
Chris Mason611f0e02008-04-03 16:29:03 -04006094 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006095 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006096 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006097 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006098 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006099 } else {
6100 mirror_num = 1;
6101 }
Chris Mason2fff7342008-04-29 14:12:09 -04006102
Chris Mason321aecc2008-04-16 10:49:51 -04006103 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006104 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006105
David Sterba47c57132015-02-20 18:43:47 +01006106 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006107 stripe_index *= map->sub_stripes;
6108
Anand Jainde483732017-10-12 16:43:00 +08006109 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006110 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006111 else if (mirror_num)
6112 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006113 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006114 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006115 stripe_index = find_live_mirror(fs_info, map,
6116 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006117 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006118 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006119 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006120
Zhao Leiffe2d202015-01-20 15:11:44 +08006121 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006122 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006123 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006124 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbacff82672019-05-17 11:43:45 +02006125 stripe_len * data_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05006126
6127 /* RAID[56] write or recovery. Return all stripes */
6128 num_stripes = map->num_stripes;
6129 max_errors = nr_parity_stripes(map);
6130
David Woodhouse53b381b2013-01-29 18:40:14 -05006131 *length = map->stripe_len;
6132 stripe_index = 0;
6133 stripe_offset = 0;
6134 } else {
6135 /*
6136 * Mirror #0 or #1 means the original data block.
6137 * Mirror #2 is RAID5 parity block.
6138 * Mirror #3 is RAID6 Q block.
6139 */
David Sterba47c57132015-02-20 18:43:47 +01006140 stripe_nr = div_u64_rem(stripe_nr,
David Sterbacff82672019-05-17 11:43:45 +02006141 data_stripes, &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006142 if (mirror_num > 1)
David Sterbacff82672019-05-17 11:43:45 +02006143 stripe_index = data_stripes + mirror_num - 2;
David Woodhouse53b381b2013-01-29 18:40:14 -05006144
6145 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006146 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6147 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006148 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006149 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006150 }
Chris Mason8790d502008-04-03 16:29:03 -04006151 } else {
6152 /*
David Sterba47c57132015-02-20 18:43:47 +01006153 * after this, stripe_nr is the number of stripes on this
6154 * device we have to walk to find the data, and stripe_index is
6155 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006156 */
David Sterba47c57132015-02-20 18:43:47 +01006157 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6158 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006159 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006160 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006161 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006162 btrfs_crit(fs_info,
6163 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006164 stripe_index, map->num_stripes);
6165 ret = -EINVAL;
6166 goto out;
6167 }
Chris Mason593060d2008-03-25 16:50:33 -04006168
Stefan Behrens472262f2012-11-06 14:43:46 +01006169 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006170 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006171 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006172 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006173 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006174 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006175 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006176 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006177
Zhao Lei6e9606d2015-01-20 15:11:34 +08006178 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006179 if (!bbio) {
6180 ret = -ENOMEM;
6181 goto out;
6182 }
Nikolay Borisov608769a2020-07-02 16:46:41 +03006183
6184 for (i = 0; i < num_stripes; i++) {
6185 bbio->stripes[i].physical = map->stripes[stripe_index].physical +
6186 stripe_offset + stripe_nr * map->stripe_len;
6187 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
6188 stripe_index++;
6189 }
Li Zefande11cc12011-12-01 12:55:47 +08006190
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006191 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006192 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6193 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006194 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006195 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006196
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006197 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006198 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006199
6200 /* Fill in the logical address of each stripe */
David Sterbacff82672019-05-17 11:43:45 +02006201 tmp = stripe_nr * data_stripes;
6202 for (i = 0; i < data_stripes; i++)
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006203 bbio->raid_map[(i+rot) % num_stripes] =
6204 em->start + (tmp + i) * map->stripe_len;
6205
6206 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6207 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6208 bbio->raid_map[(i+rot+1) % num_stripes] =
6209 RAID6_Q_STRIPE;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006210
Nikolay Borisov608769a2020-07-02 16:46:41 +03006211 sort_parity_stripes(bbio, num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006212 }
Li Zefande11cc12011-12-01 12:55:47 +08006213
Liu Bo2b19a1f2017-03-14 13:34:00 -07006214 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006215 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006216
Liu Bo73c0f222017-03-14 13:33:58 -07006217 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006218 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006219 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6220 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006221 }
6222
Li Zefande11cc12011-12-01 12:55:47 +08006223 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006224 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006225 bbio->num_stripes = num_stripes;
6226 bbio->max_errors = max_errors;
6227 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006228
6229 /*
6230 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6231 * mirror_num == num_stripes + 1 && dev_replace target drive is
6232 * available as a mirror
6233 */
6234 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6235 WARN_ON(num_stripes > 1);
6236 bbio->stripes[0].dev = dev_replace->tgtdev;
6237 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6238 bbio->mirror_num = map->num_stripes + 1;
6239 }
Chris Masoncea9e442008-04-09 16:28:12 -04006240out:
Liu Bo73beece2015-07-17 16:49:19 +08006241 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006242 lockdep_assert_held(&dev_replace->rwsem);
6243 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006244 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006245 }
Chris Mason0b86a832008-03-24 15:01:56 -04006246 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006247 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006248}
6249
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006250int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006251 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006252 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006253{
David Sterba75fb2e92018-08-03 00:36:29 +02006254 if (op == BTRFS_MAP_DISCARD)
6255 return __btrfs_map_block_for_discard(fs_info, logical,
6256 length, bbio_ret);
6257
Mike Christieb3d3fa52016-06-05 14:31:53 -05006258 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006259 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006260}
6261
Miao Xieaf8e2d12014-10-23 14:42:50 +08006262/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006263int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006264 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006265 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006266{
David Sterba825ad4c2017-03-28 14:45:22 +02006267 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006268}
6269
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006270static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006271{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006272 bio->bi_private = bbio->private;
6273 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006274 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006275
Zhao Lei6e9606d2015-01-20 15:11:34 +08006276 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006277}
6278
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006279static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006280{
Chris Mason9be33952013-05-17 18:30:14 -04006281 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006282 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006283
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006284 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006285 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006286 if (bio->bi_status == BLK_STS_IOERR ||
6287 bio->bi_status == BLK_STS_TARGET) {
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006288 struct btrfs_device *dev = btrfs_io_bio(bio)->device;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006289
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006290 ASSERT(dev->bdev);
6291 if (bio_op(bio) == REQ_OP_WRITE)
6292 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006293 BTRFS_DEV_STAT_WRITE_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006294 else if (!(bio->bi_opf & REQ_RAHEAD))
6295 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006296 BTRFS_DEV_STAT_READ_ERRS);
Nikolay Borisov3eee86c2020-07-02 15:23:31 +03006297 if (bio->bi_opf & REQ_PREFLUSH)
6298 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006299 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens442a4f62012-05-25 16:06:08 +02006300 }
6301 }
Chris Mason8790d502008-04-03 16:29:03 -04006302
Jan Schmidta1d3c472011-08-04 17:15:33 +02006303 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006304 is_orig_bio = 1;
6305
Miao Xiec404e0d2014-01-30 16:46:55 +08006306 btrfs_bio_counter_dec(bbio->fs_info);
6307
Jan Schmidta1d3c472011-08-04 17:15:33 +02006308 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006309 if (!is_orig_bio) {
6310 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006311 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006312 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006313
Chris Mason9be33952013-05-17 18:30:14 -04006314 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006315 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006316 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006317 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006318 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006319 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006320 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006321 /*
6322 * this bio is actually up to date, we didn't
6323 * go over the max number of errors
6324 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006325 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006326 }
Miao Xiec55f1392014-06-19 10:42:54 +08006327
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006328 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006329 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006330 bio_put(bio);
6331 }
Chris Mason8790d502008-04-03 16:29:03 -04006332}
6333
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006334static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006335 u64 physical, struct btrfs_device *dev)
Josef Bacikde1ee922012-10-19 16:50:56 -04006336{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006337 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006338
6339 bio->bi_private = bbio;
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006340 btrfs_io_bio(bio)->device = dev;
Josef Bacikde1ee922012-10-19 16:50:56 -04006341 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006342 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006343 btrfs_debug_in_rcu(fs_info,
6344 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6345 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
David Sterba1db45a32019-11-28 15:31:46 +01006346 (unsigned long)dev->bdev->bd_dev, rcu_str_deref(dev->name),
6347 dev->devid, bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006348 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006349
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006350 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006351
Chris Mason08635ba2019-07-10 12:28:14 -07006352 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006353}
6354
Josef Bacikde1ee922012-10-19 16:50:56 -04006355static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6356{
6357 atomic_inc(&bbio->error);
6358 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006359 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006360 WARN_ON(bio != bbio->orig_bio);
6361
Chris Mason9be33952013-05-17 18:30:14 -04006362 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006363 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006364 if (atomic_read(&bbio->error) > bbio->max_errors)
6365 bio->bi_status = BLK_STS_IOERR;
6366 else
6367 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006368 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006369 }
6370}
6371
Omar Sandoval58efbc92017-08-22 23:45:59 -07006372blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
Chris Mason08635ba2019-07-10 12:28:14 -07006373 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04006374{
Chris Mason0b86a832008-03-24 15:01:56 -04006375 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006376 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006377 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006378 u64 length = 0;
6379 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006380 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006381 int dev_nr;
6382 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006383 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006384
Kent Overstreet4f024f32013-10-11 15:44:27 -07006385 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006386 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006387
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006388 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006389 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006390 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006391 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006392 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006393 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006394 }
Chris Masoncea9e442008-04-09 16:28:12 -04006395
Jan Schmidta1d3c472011-08-04 17:15:33 +02006396 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006397 bbio->orig_bio = first_bio;
6398 bbio->private = first_bio->bi_private;
6399 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006400 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006401 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6402
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006403 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006404 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006405 /* In this case, map_length has been set to the length of
6406 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006407 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006408 ret = raid56_parity_write(fs_info, bio, bbio,
6409 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006410 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006411 ret = raid56_parity_recover(fs_info, bio, bbio,
6412 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006413 }
Miao Xie42452152014-11-25 16:39:28 +08006414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006415 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006416 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006417 }
6418
Chris Masoncea9e442008-04-09 16:28:12 -04006419 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006420 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006421 "mapping failed logical %llu bio len %llu len %llu",
6422 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006423 BUG();
6424 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006425
Zhao Lei08da7572015-02-12 15:42:16 +08006426 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006427 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006428 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6429 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006430 (bio_op(first_bio) == REQ_OP_WRITE &&
6431 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006432 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006433 continue;
6434 }
6435
David Sterba3aa8e072017-06-02 17:38:30 +02006436 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006437 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006438 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006439 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006440
Nikolay Borisovc31efbd2020-07-03 11:14:27 +03006441 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical, dev);
Chris Mason239b14b2008-03-24 15:02:07 -04006442 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006443 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006444 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006445}
6446
Anand Jain09ba3bc2019-01-19 14:48:55 +08006447/*
6448 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6449 * return NULL.
6450 *
6451 * If devid and uuid are both specified, the match must be exact, otherwise
6452 * only devid is used.
6453 *
6454 * If @seed is true, traverse through the seed devices.
6455 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006456struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006457 u64 devid, u8 *uuid, u8 *fsid,
6458 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006459{
Yan Zheng2b820322008-11-17 21:11:30 -05006460 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006461
Anand Jaine4319cd2019-01-17 23:32:31 +08006462 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006463 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006464 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08006465 list_for_each_entry(device, &fs_devices->devices,
6466 dev_list) {
6467 if (device->devid == devid &&
6468 (!uuid || memcmp(device->uuid, uuid,
6469 BTRFS_UUID_SIZE) == 0))
6470 return device;
6471 }
Yan Zheng2b820322008-11-17 21:11:30 -05006472 }
Anand Jain09ba3bc2019-01-19 14:48:55 +08006473 if (seed)
6474 fs_devices = fs_devices->seed;
6475 else
6476 return NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05006477 }
6478 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006479}
6480
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006481static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006482 u64 devid, u8 *dev_uuid)
6483{
6484 struct btrfs_device *device;
Josef Bacikfccc0002020-08-31 10:52:42 -04006485 unsigned int nofs_flag;
Chris Masondfe25022008-05-13 13:46:40 -04006486
Josef Bacikfccc0002020-08-31 10:52:42 -04006487 /*
6488 * We call this under the chunk_mutex, so we want to use NOFS for this
6489 * allocation, however we don't want to change btrfs_alloc_device() to
6490 * always do NOFS because we use it in a lot of other GFP_KERNEL safe
6491 * places.
6492 */
6493 nofs_flag = memalloc_nofs_save();
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006494 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
Josef Bacikfccc0002020-08-31 10:52:42 -04006495 memalloc_nofs_restore(nofs_flag);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006496 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006497 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006498
6499 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006500 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006501 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006502
Anand Jaine6e674b2017-12-04 12:54:54 +08006503 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006504 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006505
Chris Masondfe25022008-05-13 13:46:40 -04006506 return device;
6507}
6508
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006509/**
6510 * btrfs_alloc_device - allocate struct btrfs_device
6511 * @fs_info: used only for generating a new devid, can be NULL if
6512 * devid is provided (i.e. @devid != NULL).
6513 * @devid: a pointer to devid for this device. If NULL a new devid
6514 * is generated.
6515 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6516 * is generated.
6517 *
6518 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006519 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006520 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006521 */
6522struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6523 const u64 *devid,
6524 const u8 *uuid)
6525{
6526 struct btrfs_device *dev;
6527 u64 tmp;
6528
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306529 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006530 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006531
6532 dev = __alloc_device();
6533 if (IS_ERR(dev))
6534 return dev;
6535
6536 if (devid)
6537 tmp = *devid;
6538 else {
6539 int ret;
6540
6541 ret = find_next_devid(fs_info, &tmp);
6542 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006543 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006544 return ERR_PTR(ret);
6545 }
6546 }
6547 dev->devid = tmp;
6548
6549 if (uuid)
6550 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6551 else
6552 generate_random_uuid(dev->uuid);
6553
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006554 return dev;
6555}
6556
Anand Jain5a2b8e62017-10-09 11:07:45 +08006557static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006558 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006559{
Anand Jain2b902df2017-10-09 11:07:46 +08006560 if (error)
6561 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6562 devid, uuid);
6563 else
6564 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6565 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006566}
6567
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006568static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6569{
6570 int index = btrfs_bg_flags_to_raid_index(type);
6571 int ncopies = btrfs_raid_array[index].ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006572 const int nparity = btrfs_raid_array[index].nparity;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006573 int data_stripes;
6574
David Sterbae4f6c6b2019-05-14 01:59:54 +02006575 if (nparity)
6576 data_stripes = num_stripes - nparity;
6577 else
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006578 data_stripes = num_stripes / ncopies;
David Sterbae4f6c6b2019-05-14 01:59:54 +02006579
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006580 return div_u64(chunk_len, data_stripes);
6581}
6582
David Sterba9690ac02019-03-20 16:43:07 +01006583static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006584 struct btrfs_chunk *chunk)
6585{
David Sterba9690ac02019-03-20 16:43:07 +01006586 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006587 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006588 struct map_lookup *map;
6589 struct extent_map *em;
6590 u64 logical;
6591 u64 length;
6592 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006593 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006594 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006595 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006596 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006597
Chris Masone17cade2008-04-15 15:41:47 -04006598 logical = key->offset;
6599 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006600 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006601
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006602 /*
6603 * Only need to verify chunk item if we're reading from sys chunk array,
6604 * as chunk item in tree block is already verified by tree-checker.
6605 */
6606 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006607 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006608 if (ret)
6609 return ret;
6610 }
Chris Masona061fc82008-05-07 11:43:44 -04006611
David Sterbac8bf1b62019-05-17 11:43:17 +02006612 read_lock(&map_tree->lock);
6613 em = lookup_extent_mapping(map_tree, logical, 1);
6614 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006615
6616 /* already mapped? */
6617 if (em && em->start <= logical && em->start + em->len > logical) {
6618 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006619 return 0;
6620 } else if (em) {
6621 free_extent_map(em);
6622 }
Chris Mason0b86a832008-03-24 15:01:56 -04006623
David Sterba172ddd62011-04-21 00:48:27 +02006624 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006625 if (!em)
6626 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006627 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006628 if (!map) {
6629 free_extent_map(em);
6630 return -ENOMEM;
6631 }
6632
Wang Shilong298a8f92014-06-19 10:42:52 +08006633 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006634 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006635 em->start = logical;
6636 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006637 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006638 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006639 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006640
Chris Mason593060d2008-03-25 16:50:33 -04006641 map->num_stripes = num_stripes;
6642 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6643 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006644 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6645 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006646 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006647 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006648 em->orig_block_len = calc_stripe_length(map->type, em->len,
6649 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006650 for (i = 0; i < num_stripes; i++) {
6651 map->stripes[i].physical =
6652 btrfs_stripe_offset_nr(leaf, chunk, i);
6653 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006654 read_extent_buffer(leaf, uuid, (unsigned long)
6655 btrfs_stripe_dev_uuid_nr(chunk, i),
6656 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006657 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006658 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006659 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006660 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006661 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006662 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006663 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006664 }
Chris Masondfe25022008-05-13 13:46:40 -04006665 if (!map->stripes[i].dev) {
6666 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006667 add_missing_dev(fs_info->fs_devices, devid,
6668 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006669 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006670 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006671 btrfs_err(fs_info,
6672 "failed to init missing dev %llu: %ld",
6673 devid, PTR_ERR(map->stripes[i].dev));
6674 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006675 }
Anand Jain2b902df2017-10-09 11:07:46 +08006676 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006677 }
Anand Jaine12c9622017-12-04 12:54:53 +08006678 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6679 &(map->stripes[i].dev->dev_state));
6680
Chris Mason0b86a832008-03-24 15:01:56 -04006681 }
6682
David Sterbac8bf1b62019-05-17 11:43:17 +02006683 write_lock(&map_tree->lock);
6684 ret = add_extent_mapping(map_tree, em, 0);
6685 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006686 if (ret < 0) {
6687 btrfs_err(fs_info,
6688 "failed to add chunk map, start=%llu len=%llu: %d",
6689 em->start, em->len, ret);
6690 }
Chris Mason0b86a832008-03-24 15:01:56 -04006691 free_extent_map(em);
6692
Qu Wenruo64f64f42018-08-01 10:37:20 +08006693 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006694}
6695
Jeff Mahoney143bede2012-03-01 14:56:26 +01006696static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006697 struct btrfs_dev_item *dev_item,
6698 struct btrfs_device *device)
6699{
6700 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006701
6702 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006703 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6704 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006705 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006706 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006707 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006708 device->type = btrfs_device_type(leaf, dev_item);
6709 device->io_align = btrfs_device_io_align(leaf, dev_item);
6710 device->io_width = btrfs_device_io_width(leaf, dev_item);
6711 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006712 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006713 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006714
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006715 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006716 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006717}
6718
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006719static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006720 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006721{
6722 struct btrfs_fs_devices *fs_devices;
6723 int ret;
6724
David Sterbaa32bf9a2018-03-16 02:21:22 +01006725 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006726 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006727
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006728 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006729 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006730 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006731 return fs_devices;
6732
Yan Zheng2b820322008-11-17 21:11:30 -05006733 fs_devices = fs_devices->seed;
6734 }
6735
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006736 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006737 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006738 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006739 return ERR_PTR(-ENOENT);
6740
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006741 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006742 if (IS_ERR(fs_devices))
6743 return fs_devices;
6744
Johannes Thumshirn0395d842019-11-13 11:27:27 +01006745 fs_devices->seeding = true;
Miao Xie5f375832014-09-03 21:35:46 +08006746 fs_devices->opened = 1;
6747 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006748 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006749
6750 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
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006768 fs_devices->seed = fs_info->fs_devices->seed;
6769 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006770out:
Miao Xie5f375832014-09-03 21:35:46 +08006771 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006772}
6773
David Sterba17850752019-03-20 16:45:15 +01006774static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006775 struct btrfs_dev_item *dev_item)
6776{
David Sterba17850752019-03-20 16:45:15 +01006777 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006778 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006779 struct btrfs_device *device;
6780 u64 devid;
6781 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006782 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006783 u8 dev_uuid[BTRFS_UUID_SIZE];
6784
Chris Mason0b86a832008-03-24 15:01:56 -04006785 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006786 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006787 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006788 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006789 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006790
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006791 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006792 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006793 if (IS_ERR(fs_devices))
6794 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006795 }
6796
Anand Jaine4319cd2019-01-17 23:32:31 +08006797 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006798 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006799 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006800 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006801 btrfs_report_missing_device(fs_info, devid,
6802 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006803 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006804 }
Yan Zheng2b820322008-11-17 21:11:30 -05006805
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006806 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006807 if (IS_ERR(device)) {
6808 btrfs_err(fs_info,
6809 "failed to add missing dev %llu: %ld",
6810 devid, PTR_ERR(device));
6811 return PTR_ERR(device);
6812 }
Anand Jain2b902df2017-10-09 11:07:46 +08006813 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006814 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006815 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006816 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6817 btrfs_report_missing_device(fs_info,
6818 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006819 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006820 }
6821 btrfs_report_missing_device(fs_info, devid,
6822 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006823 }
Miao Xie5f375832014-09-03 21:35:46 +08006824
Anand Jaine6e674b2017-12-04 12:54:54 +08006825 if (!device->bdev &&
6826 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006827 /*
6828 * this happens when a device that was properly setup
6829 * in the device info lists suddenly goes bad.
6830 * device->bdev is NULL, and so we have to set
6831 * device->missing to one here
6832 */
Miao Xie5f375832014-09-03 21:35:46 +08006833 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006834 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006835 }
Miao Xie5f375832014-09-03 21:35:46 +08006836
6837 /* Move the device to its own fs_devices */
6838 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006839 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6840 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006841
6842 list_move(&device->dev_list, &fs_devices->devices);
6843 device->fs_devices->num_devices--;
6844 fs_devices->num_devices++;
6845
6846 device->fs_devices->missing_devices--;
6847 fs_devices->missing_devices++;
6848
6849 device->fs_devices = fs_devices;
6850 }
Yan Zheng2b820322008-11-17 21:11:30 -05006851 }
6852
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006853 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006854 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006855 if (device->generation !=
6856 btrfs_device_generation(leaf, dev_item))
6857 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006858 }
Chris Mason0b86a832008-03-24 15:01:56 -04006859
6860 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006861 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006862 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006863 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006864 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006865 atomic64_add(device->total_bytes - device->bytes_used,
6866 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006867 }
Chris Mason0b86a832008-03-24 15:01:56 -04006868 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006869 return ret;
6870}
6871
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006872int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006873{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006874 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006875 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006876 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006877 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006878 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006879 u8 *array_ptr;
6880 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006881 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006882 u32 num_stripes;
6883 u32 array_size;
6884 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006885 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006886 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006887 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006888
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006889 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006890 /*
6891 * This will create extent buffer of nodesize, superblock size is
6892 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6893 * overallocate but we can keep it as-is, only the first page is used.
6894 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006895 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006896 if (IS_ERR(sb))
6897 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006898 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006899 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006900 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006901 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006902 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006903 * pages up-to-date when the page is larger: extent does not cover the
6904 * whole page and consequently check_page_uptodate does not find all
6905 * the page's extents up-to-date (the hole beyond sb),
6906 * write_extent_buffer then triggers a WARN_ON.
6907 *
6908 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6909 * but sb spans only this function. Add an explicit SetPageUptodate call
6910 * to silence the warning eg. on PowerPC 64.
6911 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006912 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006913 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006914
Chris Masona061fc82008-05-07 11:43:44 -04006915 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006916 array_size = btrfs_super_sys_array_size(super_copy);
6917
David Sterba1ffb22c2014-10-31 19:02:42 +01006918 array_ptr = super_copy->sys_chunk_array;
6919 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6920 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006921
David Sterba1ffb22c2014-10-31 19:02:42 +01006922 while (cur_offset < array_size) {
6923 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006924 len = sizeof(*disk_key);
6925 if (cur_offset + len > array_size)
6926 goto out_short_read;
6927
Chris Mason0b86a832008-03-24 15:01:56 -04006928 btrfs_disk_key_to_cpu(&key, disk_key);
6929
David Sterba1ffb22c2014-10-31 19:02:42 +01006930 array_ptr += len;
6931 sb_array_offset += len;
6932 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006933
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006934 if (key.type != BTRFS_CHUNK_ITEM_KEY) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006935 btrfs_err(fs_info,
6936 "unexpected item type %u in sys_array at offset %u",
6937 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006938 ret = -EIO;
6939 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006940 }
Johannes Thumshirn32ab3d12019-10-18 11:58:23 +02006941
6942 chunk = (struct btrfs_chunk *)sb_array_offset;
6943 /*
6944 * At least one btrfs_chunk with one stripe must be present,
6945 * exact stripe count check comes afterwards
6946 */
6947 len = btrfs_chunk_item_size(1);
6948 if (cur_offset + len > array_size)
6949 goto out_short_read;
6950
6951 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6952 if (!num_stripes) {
6953 btrfs_err(fs_info,
6954 "invalid number of stripes %u in sys_array at offset %u",
6955 num_stripes, cur_offset);
6956 ret = -EIO;
6957 break;
6958 }
6959
6960 type = btrfs_chunk_type(sb, chunk);
6961 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
6962 btrfs_err(fs_info,
6963 "invalid chunk type %llu in sys_array at offset %u",
6964 type, cur_offset);
6965 ret = -EIO;
6966 break;
6967 }
6968
6969 len = btrfs_chunk_item_size(num_stripes);
6970 if (cur_offset + len > array_size)
6971 goto out_short_read;
6972
6973 ret = read_one_chunk(&key, sb, chunk);
6974 if (ret)
6975 break;
6976
David Sterba1ffb22c2014-10-31 19:02:42 +01006977 array_ptr += len;
6978 sb_array_offset += len;
6979 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006980 }
Liu Bod8651772016-06-03 17:41:42 -07006981 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006982 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006983 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006984
6985out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006986 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006987 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006988 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006989 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006990 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006991}
6992
Qu Wenruo21634a12017-03-09 09:34:36 +08006993/*
6994 * Check if all chunks in the fs are OK for read-write degraded mount
6995 *
Anand Jain6528b992017-12-18 17:08:59 +08006996 * If the @failing_dev is specified, it's accounted as missing.
6997 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006998 * Return true if all chunks meet the minimal RW mount requirements.
6999 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7000 */
Anand Jain6528b992017-12-18 17:08:59 +08007001bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7002 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007003{
David Sterbac8bf1b62019-05-17 11:43:17 +02007004 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08007005 struct extent_map *em;
7006 u64 next_start = 0;
7007 bool ret = true;
7008
David Sterbac8bf1b62019-05-17 11:43:17 +02007009 read_lock(&map_tree->lock);
7010 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7011 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007012 /* No chunk at all? Return false anyway */
7013 if (!em) {
7014 ret = false;
7015 goto out;
7016 }
7017 while (em) {
7018 struct map_lookup *map;
7019 int missing = 0;
7020 int max_tolerated;
7021 int i;
7022
7023 map = em->map_lookup;
7024 max_tolerated =
7025 btrfs_get_num_tolerated_disk_barrier_failures(
7026 map->type);
7027 for (i = 0; i < map->num_stripes; i++) {
7028 struct btrfs_device *dev = map->stripes[i].dev;
7029
Anand Jaine6e674b2017-12-04 12:54:54 +08007030 if (!dev || !dev->bdev ||
7031 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007032 dev->last_flush_error)
7033 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007034 else if (failing_dev && failing_dev == dev)
7035 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007036 }
7037 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007038 if (!failing_dev)
7039 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007040 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007041 em->start, missing, max_tolerated);
7042 free_extent_map(em);
7043 ret = false;
7044 goto out;
7045 }
7046 next_start = extent_map_end(em);
7047 free_extent_map(em);
7048
David Sterbac8bf1b62019-05-17 11:43:17 +02007049 read_lock(&map_tree->lock);
7050 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08007051 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02007052 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007053 }
7054out:
7055 return ret;
7056}
7057
David Sterbad85327b12020-07-08 22:55:14 +02007058static void readahead_tree_node_children(struct extent_buffer *node)
7059{
7060 int i;
7061 const int nr_items = btrfs_header_nritems(node);
7062
7063 for (i = 0; i < nr_items; i++) {
7064 u64 start;
7065
7066 start = btrfs_node_blockptr(node, i);
7067 readahead_tree_block(node->fs_info, start);
7068 }
7069}
7070
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007071int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007072{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007073 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007074 struct btrfs_path *path;
7075 struct extent_buffer *leaf;
7076 struct btrfs_key key;
7077 struct btrfs_key found_key;
7078 int ret;
7079 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007080 u64 total_dev = 0;
David Sterbad85327b12020-07-08 22:55:14 +02007081 u64 last_ra_node = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007082
Chris Mason0b86a832008-03-24 15:01:56 -04007083 path = btrfs_alloc_path();
7084 if (!path)
7085 return -ENOMEM;
7086
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007087 /*
7088 * uuid_mutex is needed only if we are mounting a sprout FS
7089 * otherwise we don't need it.
7090 */
Li Zefanb367e472011-12-07 11:38:24 +08007091 mutex_lock(&uuid_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007092
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007093 /*
Boris Burkov48cfa612020-07-16 13:29:46 -07007094 * It is possible for mount and umount to race in such a way that
7095 * we execute this code path, but open_fs_devices failed to clear
7096 * total_rw_bytes. We certainly want it cleared before reading the
7097 * device items, so clear it here.
7098 */
7099 fs_info->fs_devices->total_rw_bytes = 0;
7100
7101 /*
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007102 * Read all device items, and then all the chunk items. All
7103 * device items are found before any chunk item (their object id
7104 * is smaller than the lowest possible object id for a chunk
7105 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007106 */
7107 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7108 key.offset = 0;
7109 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007110 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007111 if (ret < 0)
7112 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007113 while (1) {
David Sterbad85327b12020-07-08 22:55:14 +02007114 struct extent_buffer *node;
7115
Chris Mason0b86a832008-03-24 15:01:56 -04007116 leaf = path->nodes[0];
7117 slot = path->slots[0];
7118 if (slot >= btrfs_header_nritems(leaf)) {
7119 ret = btrfs_next_leaf(root, path);
7120 if (ret == 0)
7121 continue;
7122 if (ret < 0)
7123 goto error;
7124 break;
7125 }
David Sterbad85327b12020-07-08 22:55:14 +02007126 /*
7127 * The nodes on level 1 are not locked but we don't need to do
7128 * that during mount time as nothing else can access the tree
7129 */
7130 node = path->nodes[1];
7131 if (node) {
7132 if (last_ra_node != node->start) {
7133 readahead_tree_node_children(node);
7134 last_ra_node = node->start;
7135 }
7136 }
Chris Mason0b86a832008-03-24 15:01:56 -04007137 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007138 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7139 struct btrfs_dev_item *dev_item;
7140 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007141 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01007142 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007143 if (ret)
7144 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007145 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007146 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7147 struct btrfs_chunk *chunk;
7148 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007149 mutex_lock(&fs_info->chunk_mutex);
David Sterba9690ac02019-03-20 16:43:07 +01007150 ret = read_one_chunk(&found_key, leaf, chunk);
Josef Bacik01d01ca2020-07-17 15:12:28 -04007151 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05007152 if (ret)
7153 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007154 }
7155 path->slots[0]++;
7156 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007157
7158 /*
7159 * After loading chunk tree, we've got all device information,
7160 * do another round of validation checks.
7161 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007162 if (total_dev != fs_info->fs_devices->total_devices) {
7163 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007164 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007165 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007166 total_dev);
7167 ret = -EINVAL;
7168 goto error;
7169 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007170 if (btrfs_super_total_bytes(fs_info->super_copy) <
7171 fs_info->fs_devices->total_rw_bytes) {
7172 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007173 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007174 btrfs_super_total_bytes(fs_info->super_copy),
7175 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007176 ret = -EINVAL;
7177 goto error;
7178 }
Chris Mason0b86a832008-03-24 15:01:56 -04007179 ret = 0;
7180error:
Li Zefanb367e472011-12-07 11:38:24 +08007181 mutex_unlock(&uuid_mutex);
7182
Yan Zheng2b820322008-11-17 21:11:30 -05007183 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007184 return ret;
7185}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007186
Miao Xiecb517ea2013-05-15 07:48:19 +00007187void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7188{
7189 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7190 struct btrfs_device *device;
7191
Liu Bo29cc83f2014-05-11 23:14:59 +08007192 while (fs_devices) {
7193 mutex_lock(&fs_devices->device_list_mutex);
7194 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007195 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007196 mutex_unlock(&fs_devices->device_list_mutex);
7197
7198 fs_devices = fs_devices->seed;
7199 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007200}
7201
David Sterba1dc990d2019-08-21 20:05:32 +02007202static u64 btrfs_dev_stats_value(const struct extent_buffer *eb,
7203 const struct btrfs_dev_stats_item *ptr,
7204 int index)
7205{
7206 u64 val;
7207
7208 read_extent_buffer(eb, &val,
7209 offsetof(struct btrfs_dev_stats_item, values) +
7210 ((unsigned long)ptr) + (index * sizeof(u64)),
7211 sizeof(val));
7212 return val;
7213}
7214
7215static void btrfs_set_dev_stats_value(struct extent_buffer *eb,
7216 struct btrfs_dev_stats_item *ptr,
7217 int index, u64 val)
7218{
7219 write_extent_buffer(eb, &val,
7220 offsetof(struct btrfs_dev_stats_item, values) +
7221 ((unsigned long)ptr) + (index * sizeof(u64)),
7222 sizeof(val));
7223}
7224
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007225int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7226{
7227 struct btrfs_key key;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007228 struct btrfs_root *dev_root = fs_info->dev_root;
7229 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7230 struct extent_buffer *eb;
7231 int slot;
7232 int ret = 0;
7233 struct btrfs_device *device;
7234 struct btrfs_path *path = NULL;
7235 int i;
7236
7237 path = btrfs_alloc_path();
Anand Jain3b80a9842019-08-21 17:26:32 +08007238 if (!path)
7239 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007240
7241 mutex_lock(&fs_devices->device_list_mutex);
7242 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7243 int item_size;
7244 struct btrfs_dev_stats_item *ptr;
7245
David Sterba242e2952016-01-25 17:51:31 +01007246 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7247 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007248 key.offset = device->devid;
7249 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7250 if (ret) {
Anand Jainae4b9b42019-08-07 16:21:20 +08007251 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7252 btrfs_dev_stat_set(device, i, 0);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007253 device->dev_stats_valid = 1;
7254 btrfs_release_path(path);
7255 continue;
7256 }
7257 slot = path->slots[0];
7258 eb = path->nodes[0];
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007259 item_size = btrfs_item_size_nr(eb, slot);
7260
7261 ptr = btrfs_item_ptr(eb, slot,
7262 struct btrfs_dev_stats_item);
7263
7264 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7265 if (item_size >= (1 + i) * sizeof(__le64))
7266 btrfs_dev_stat_set(device, i,
7267 btrfs_dev_stats_value(eb, ptr, i));
7268 else
Anand Jain4e411a72019-08-07 16:21:19 +08007269 btrfs_dev_stat_set(device, i, 0);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007270 }
7271
7272 device->dev_stats_valid = 1;
7273 btrfs_dev_stat_print_on_load(device);
7274 btrfs_release_path(path);
7275 }
7276 mutex_unlock(&fs_devices->device_list_mutex);
7277
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007278 btrfs_free_path(path);
7279 return ret < 0 ? ret : 0;
7280}
7281
7282static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007283 struct btrfs_device *device)
7284{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007285 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007286 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007287 struct btrfs_path *path;
7288 struct btrfs_key key;
7289 struct extent_buffer *eb;
7290 struct btrfs_dev_stats_item *ptr;
7291 int ret;
7292 int i;
7293
David Sterba242e2952016-01-25 17:51:31 +01007294 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7295 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007296 key.offset = device->devid;
7297
7298 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007299 if (!path)
7300 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007301 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7302 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007303 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007304 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007305 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007306 goto out;
7307 }
7308
7309 if (ret == 0 &&
7310 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7311 /* need to delete old one and insert a new one */
7312 ret = btrfs_del_item(trans, dev_root, path);
7313 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007314 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007315 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007316 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007317 goto out;
7318 }
7319 ret = 1;
7320 }
7321
7322 if (ret == 1) {
7323 /* need to insert a new item */
7324 btrfs_release_path(path);
7325 ret = btrfs_insert_empty_item(trans, dev_root, path,
7326 &key, sizeof(*ptr));
7327 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007328 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007329 "insert dev_stats item for device %s failed %d",
7330 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007331 goto out;
7332 }
7333 }
7334
7335 eb = path->nodes[0];
7336 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7337 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7338 btrfs_set_dev_stats_value(eb, ptr, i,
7339 btrfs_dev_stat_read(device, i));
7340 btrfs_mark_buffer_dirty(eb);
7341
7342out:
7343 btrfs_free_path(path);
7344 return ret;
7345}
7346
7347/*
7348 * called from commit_transaction. Writes all changed device stats to disk.
7349 */
David Sterba196c9d82019-03-20 16:50:38 +01007350int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007351{
David Sterba196c9d82019-03-20 16:50:38 +01007352 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007353 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7354 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007355 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007356 int ret = 0;
7357
7358 mutex_lock(&fs_devices->device_list_mutex);
7359 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007360 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7361 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007362 continue;
7363
Nikolay Borisov9deae962017-10-24 13:47:37 +03007364
7365 /*
7366 * There is a LOAD-LOAD control dependency between the value of
7367 * dev_stats_ccnt and updating the on-disk values which requires
7368 * reading the in-memory counters. Such control dependencies
7369 * require explicit read memory barriers.
7370 *
7371 * This memory barriers pairs with smp_mb__before_atomic in
7372 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7373 * barrier implied by atomic_xchg in
7374 * btrfs_dev_stats_read_and_reset
7375 */
7376 smp_rmb();
7377
Nikolay Borisov5495f192018-07-20 19:37:49 +03007378 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007379 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007380 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007381 }
7382 mutex_unlock(&fs_devices->device_list_mutex);
7383
7384 return ret;
7385}
7386
Stefan Behrens442a4f62012-05-25 16:06:08 +02007387void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7388{
7389 btrfs_dev_stat_inc(dev, index);
7390 btrfs_dev_stat_print_on_error(dev);
7391}
7392
Eric Sandeen48a3b632013-04-25 20:41:01 +00007393static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007394{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007395 if (!dev->dev_stats_valid)
7396 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007397 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007398 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007399 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007400 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7401 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7402 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007403 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7404 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007405}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007406
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007407static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7408{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007409 int i;
7410
7411 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7412 if (btrfs_dev_stat_read(dev, i) != 0)
7413 break;
7414 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7415 return; /* all values == 0, suppress message */
7416
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007417 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007418 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007419 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007420 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7421 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7422 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7423 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7424 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7425}
7426
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007427int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007428 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007429{
7430 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007431 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007432 int i;
7433
7434 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007435 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7436 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007437 mutex_unlock(&fs_devices->device_list_mutex);
7438
7439 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007440 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007441 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007442 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007443 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007444 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007445 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007446 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7447 if (stats->nr_items > i)
7448 stats->values[i] =
7449 btrfs_dev_stat_read_and_reset(dev, i);
7450 else
Anand Jain4e411a72019-08-07 16:21:19 +08007451 btrfs_dev_stat_set(dev, i, 0);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007452 }
Anand Jaina69976b2020-01-10 12:26:34 +08007453 btrfs_info(fs_info, "device stats zeroed by %s (%d)",
7454 current->comm, task_pid_nr(current));
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007455 } else {
7456 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7457 if (stats->nr_items > i)
7458 stats->values[i] = btrfs_dev_stat_read(dev, i);
7459 }
7460 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7461 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7462 return 0;
7463}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007464
Miao Xie935e5cc2014-09-03 21:35:33 +08007465/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007466 * Update the size and bytes used for each device where it changed. This is
7467 * delayed since we would otherwise get errors while writing out the
7468 * superblocks.
7469 *
7470 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007471 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007472void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007473{
Miao Xie935e5cc2014-09-03 21:35:33 +08007474 struct btrfs_device *curr, *next;
7475
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007476 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7477
7478 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007479 return;
7480
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007481 /*
7482 * We don't need the device_list_mutex here. This list is owned by the
7483 * transaction and the transaction must complete before the device is
7484 * released.
7485 */
7486 mutex_lock(&trans->fs_info->chunk_mutex);
7487 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7488 post_commit_list) {
7489 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007490 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007491 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007492 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007493 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007494}
Anand Jain5a13f432015-03-10 06:38:31 +08007495
7496void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7497{
7498 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7499 while (fs_devices) {
7500 fs_devices->fs_info = fs_info;
7501 fs_devices = fs_devices->seed;
7502 }
7503}
7504
7505void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7506{
7507 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7508 while (fs_devices) {
7509 fs_devices->fs_info = NULL;
7510 fs_devices = fs_devices->seed;
7511 }
7512}
David Sterba46df06b2018-07-13 20:46:30 +02007513
7514/*
7515 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7516 */
7517int btrfs_bg_type_to_factor(u64 flags)
7518{
David Sterba44b28ad2019-05-17 11:43:31 +02007519 const int index = btrfs_bg_flags_to_raid_index(flags);
7520
7521 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007522}
Qu Wenruocf90d882018-08-01 10:37:19 +08007523
7524
Qu Wenruocf90d882018-08-01 10:37:19 +08007525
7526static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7527 u64 chunk_offset, u64 devid,
7528 u64 physical_offset, u64 physical_len)
7529{
David Sterbac8bf1b62019-05-17 11:43:17 +02007530 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007531 struct extent_map *em;
7532 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007533 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007534 u64 stripe_len;
7535 bool found = false;
7536 int ret = 0;
7537 int i;
7538
7539 read_lock(&em_tree->lock);
7540 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7541 read_unlock(&em_tree->lock);
7542
7543 if (!em) {
7544 btrfs_err(fs_info,
7545"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7546 physical_offset, devid);
7547 ret = -EUCLEAN;
7548 goto out;
7549 }
7550
7551 map = em->map_lookup;
7552 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7553 if (physical_len != stripe_len) {
7554 btrfs_err(fs_info,
7555"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7556 physical_offset, devid, em->start, physical_len,
7557 stripe_len);
7558 ret = -EUCLEAN;
7559 goto out;
7560 }
7561
7562 for (i = 0; i < map->num_stripes; i++) {
7563 if (map->stripes[i].dev->devid == devid &&
7564 map->stripes[i].physical == physical_offset) {
7565 found = true;
7566 if (map->verified_stripes >= map->num_stripes) {
7567 btrfs_err(fs_info,
7568 "too many dev extents for chunk %llu found",
7569 em->start);
7570 ret = -EUCLEAN;
7571 goto out;
7572 }
7573 map->verified_stripes++;
7574 break;
7575 }
7576 }
7577 if (!found) {
7578 btrfs_err(fs_info,
7579 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7580 physical_offset, devid);
7581 ret = -EUCLEAN;
7582 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007583
7584 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007585 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007586 if (!dev) {
7587 btrfs_err(fs_info, "failed to find devid %llu", devid);
7588 ret = -EUCLEAN;
7589 goto out;
7590 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007591
7592 /* It's possible this device is a dummy for seed device */
7593 if (dev->disk_total_bytes == 0) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08007594 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7595 NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007596 if (!dev) {
7597 btrfs_err(fs_info, "failed to find seed devid %llu",
7598 devid);
7599 ret = -EUCLEAN;
7600 goto out;
7601 }
7602 }
7603
Qu Wenruo05a37c42018-10-05 17:45:55 +08007604 if (physical_offset + physical_len > dev->disk_total_bytes) {
7605 btrfs_err(fs_info,
7606"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7607 devid, physical_offset, physical_len,
7608 dev->disk_total_bytes);
7609 ret = -EUCLEAN;
7610 goto out;
7611 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007612out:
7613 free_extent_map(em);
7614 return ret;
7615}
7616
7617static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7618{
David Sterbac8bf1b62019-05-17 11:43:17 +02007619 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007620 struct extent_map *em;
7621 struct rb_node *node;
7622 int ret = 0;
7623
7624 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007625 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007626 em = rb_entry(node, struct extent_map, rb_node);
7627 if (em->map_lookup->num_stripes !=
7628 em->map_lookup->verified_stripes) {
7629 btrfs_err(fs_info,
7630 "chunk %llu has missing dev extent, have %d expect %d",
7631 em->start, em->map_lookup->verified_stripes,
7632 em->map_lookup->num_stripes);
7633 ret = -EUCLEAN;
7634 goto out;
7635 }
7636 }
7637out:
7638 read_unlock(&em_tree->lock);
7639 return ret;
7640}
7641
7642/*
7643 * Ensure that all dev extents are mapped to correct chunk, otherwise
7644 * later chunk allocation/free would cause unexpected behavior.
7645 *
7646 * NOTE: This will iterate through the whole device tree, which should be of
7647 * the same size level as the chunk tree. This slightly increases mount time.
7648 */
7649int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7650{
7651 struct btrfs_path *path;
7652 struct btrfs_root *root = fs_info->dev_root;
7653 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007654 u64 prev_devid = 0;
7655 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007656 int ret = 0;
7657
7658 key.objectid = 1;
7659 key.type = BTRFS_DEV_EXTENT_KEY;
7660 key.offset = 0;
7661
7662 path = btrfs_alloc_path();
7663 if (!path)
7664 return -ENOMEM;
7665
7666 path->reada = READA_FORWARD;
7667 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7668 if (ret < 0)
7669 goto out;
7670
7671 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7672 ret = btrfs_next_item(root, path);
7673 if (ret < 0)
7674 goto out;
7675 /* No dev extents at all? Not good */
7676 if (ret > 0) {
7677 ret = -EUCLEAN;
7678 goto out;
7679 }
7680 }
7681 while (1) {
7682 struct extent_buffer *leaf = path->nodes[0];
7683 struct btrfs_dev_extent *dext;
7684 int slot = path->slots[0];
7685 u64 chunk_offset;
7686 u64 physical_offset;
7687 u64 physical_len;
7688 u64 devid;
7689
7690 btrfs_item_key_to_cpu(leaf, &key, slot);
7691 if (key.type != BTRFS_DEV_EXTENT_KEY)
7692 break;
7693 devid = key.objectid;
7694 physical_offset = key.offset;
7695
7696 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7697 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7698 physical_len = btrfs_dev_extent_length(leaf, dext);
7699
Qu Wenruo5eb19382018-10-05 17:45:54 +08007700 /* Check if this dev extent overlaps with the previous one */
7701 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7702 btrfs_err(fs_info,
7703"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7704 devid, physical_offset, prev_dev_ext_end);
7705 ret = -EUCLEAN;
7706 goto out;
7707 }
7708
Qu Wenruocf90d882018-08-01 10:37:19 +08007709 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7710 physical_offset, physical_len);
7711 if (ret < 0)
7712 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007713 prev_devid = devid;
7714 prev_dev_ext_end = physical_offset + physical_len;
7715
Qu Wenruocf90d882018-08-01 10:37:19 +08007716 ret = btrfs_next_item(root, path);
7717 if (ret < 0)
7718 goto out;
7719 if (ret > 0) {
7720 ret = 0;
7721 break;
7722 }
7723 }
7724
7725 /* Ensure all chunks have corresponding dev extents */
7726 ret = verify_chunk_dev_extent_mapping(fs_info);
7727out:
7728 btrfs_free_path(path);
7729 return ret;
7730}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007731
7732/*
7733 * Check whether the given block group or device is pinned by any inode being
7734 * used as a swapfile.
7735 */
7736bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7737{
7738 struct btrfs_swapfile_pin *sp;
7739 struct rb_node *node;
7740
7741 spin_lock(&fs_info->swapfile_pins_lock);
7742 node = fs_info->swapfile_pins.rb_node;
7743 while (node) {
7744 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7745 if (ptr < sp->ptr)
7746 node = node->rb_left;
7747 else if (ptr > sp->ptr)
7748 node = node->rb_right;
7749 else
7750 break;
7751 }
7752 spin_unlock(&fs_info->swapfile_pins_lock);
7753 return node != NULL;
7754}