blob: 2e0860d14ad2f19a00fc9eb870fd5b6dbe659d55 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason0b86a832008-03-24 15:01:56 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason0b86a832008-03-24 15:01:56 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Chris Mason0b86a832008-03-24 15:01:56 -04006#include <linux/sched.h>
7#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -04009#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040010#include <linux/blkdev.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020011#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020012#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050013#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020014#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070015#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080016#include <linux/list_sort.h>
Chris Mason0b86a832008-03-24 15:01:56 -040017#include "ctree.h"
18#include "extent_map.h"
19#include "disk-io.h"
20#include "transaction.h"
21#include "print-tree.h"
22#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050023#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040024#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010025#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040026#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060027#include "math.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"
Chris Mason0b86a832008-03-24 15:01:56 -040031
Zhao Leiaf902042015-09-15 21:08:06 +080032const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
33 [BTRFS_RAID_RAID10] = {
34 .sub_stripes = 2,
35 .dev_stripes = 1,
36 .devs_max = 0, /* 0 == as many as possible */
37 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080038 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080039 .devs_increment = 2,
40 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020041 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080042 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080043 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080044 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080045 },
46 [BTRFS_RAID_RAID1] = {
47 .sub_stripes = 1,
48 .dev_stripes = 1,
49 .devs_max = 2,
50 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080051 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080052 .devs_increment = 2,
53 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020054 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080055 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080056 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080057 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080058 },
59 [BTRFS_RAID_DUP] = {
60 .sub_stripes = 1,
61 .dev_stripes = 2,
62 .devs_max = 1,
63 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080064 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080065 .devs_increment = 1,
66 .ncopies = 2,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020067 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080068 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080069 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080070 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080071 },
72 [BTRFS_RAID_RAID0] = {
73 .sub_stripes = 1,
74 .dev_stripes = 1,
75 .devs_max = 0,
76 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080077 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080078 .devs_increment = 1,
79 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020080 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080081 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +080082 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080083 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080084 },
85 [BTRFS_RAID_SINGLE] = {
86 .sub_stripes = 1,
87 .dev_stripes = 1,
88 .devs_max = 1,
89 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080090 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080091 .devs_increment = 1,
92 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +020093 .nparity = 0,
Anand Jained234672018-04-25 19:01:42 +080094 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +080095 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080096 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080097 },
98 [BTRFS_RAID_RAID5] = {
99 .sub_stripes = 1,
100 .dev_stripes = 1,
101 .devs_max = 0,
102 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800103 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800104 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200105 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200106 .nparity = 1,
Anand Jained234672018-04-25 19:01:42 +0800107 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800108 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800109 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800110 },
111 [BTRFS_RAID_RAID6] = {
112 .sub_stripes = 1,
113 .dev_stripes = 1,
114 .devs_max = 0,
115 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800116 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800117 .devs_increment = 1,
Hans van Kranenburgda612e32018-10-04 23:24:41 +0200118 .ncopies = 1,
Hans van Kranenburgb50836e2018-10-04 23:24:42 +0200119 .nparity = 2,
Anand Jained234672018-04-25 19:01:42 +0800120 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800121 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800122 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800123 },
124};
125
Anand Jained234672018-04-25 19:01:42 +0800126const char *get_raid_name(enum btrfs_raid_types type)
127{
128 if (type >= BTRFS_NR_RAID_TYPES)
129 return NULL;
130
131 return btrfs_raid_array[type].raid_name;
132}
133
Anand Jainf89e09c2018-11-20 16:12:55 +0800134/*
135 * Fill @buf with textual description of @bg_flags, no more than @size_buf
136 * bytes including terminating null byte.
137 */
138void btrfs_describe_block_groups(u64 bg_flags, char *buf, u32 size_buf)
139{
140 int i;
141 int ret;
142 char *bp = buf;
143 u64 flags = bg_flags;
144 u32 size_bp = size_buf;
145
146 if (!flags) {
147 strcpy(bp, "NONE");
148 return;
149 }
150
151#define DESCRIBE_FLAG(flag, desc) \
152 do { \
153 if (flags & (flag)) { \
154 ret = snprintf(bp, size_bp, "%s|", (desc)); \
155 if (ret < 0 || ret >= size_bp) \
156 goto out_overflow; \
157 size_bp -= ret; \
158 bp += ret; \
159 flags &= ~(flag); \
160 } \
161 } while (0)
162
163 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_DATA, "data");
164 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_SYSTEM, "system");
165 DESCRIBE_FLAG(BTRFS_BLOCK_GROUP_METADATA, "metadata");
166
167 DESCRIBE_FLAG(BTRFS_AVAIL_ALLOC_BIT_SINGLE, "single");
168 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
169 DESCRIBE_FLAG(btrfs_raid_array[i].bg_flag,
170 btrfs_raid_array[i].raid_name);
171#undef DESCRIBE_FLAG
172
173 if (flags) {
174 ret = snprintf(bp, size_bp, "0x%llx|", flags);
175 size_bp -= ret;
176 }
177
178 if (size_bp < size_buf)
179 buf[size_buf - size_bp - 1] = '\0'; /* remove last | */
180
181 /*
182 * The text is trimmed, it's up to the caller to provide sufficiently
183 * large buffer
184 */
185out_overflow:;
186}
187
David Sterba6f8e0fc2019-03-20 16:29:13 +0100188static int init_first_rw_device(struct btrfs_trans_handle *trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400189static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200190static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000191static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200192static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700193static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
194 enum btrfs_map_op op,
195 u64 logical, u64 *length,
196 struct btrfs_bio **bbio_ret,
197 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500198
David Sterba9c6b1c42017-06-16 22:30:00 +0200199/*
200 * Device locking
201 * ==============
202 *
203 * There are several mutexes that protect manipulation of devices and low-level
204 * structures like chunks but not block groups, extents or files
205 *
206 * uuid_mutex (global lock)
207 * ------------------------
208 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
209 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
210 * device) or requested by the device= mount option
211 *
212 * the mutex can be very coarse and can cover long-running operations
213 *
214 * protects: updates to fs_devices counters like missing devices, rw devices,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100215 * seeding, structure cloning, opening/closing devices at mount/umount time
David Sterba9c6b1c42017-06-16 22:30:00 +0200216 *
217 * global::fs_devs - add, remove, updates to the global list
218 *
219 * does not protect: manipulation of the fs_devices::devices list!
220 *
221 * btrfs_device::name - renames (write side), read is RCU
222 *
223 * fs_devices::device_list_mutex (per-fs, with RCU)
224 * ------------------------------------------------
225 * protects updates to fs_devices::devices, ie. adding and deleting
226 *
227 * simple list traversal with read-only actions can be done with RCU protection
228 *
229 * may be used to exclude some operations from running concurrently without any
230 * modifications to the list (see write_all_supers)
231 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200232 * balance_mutex
233 * -------------
234 * protects balance structures (status, state) and context accessed from
235 * several places (internally, ioctl)
236 *
237 * chunk_mutex
238 * -----------
239 * protects chunks, adding or removing during allocation, trim or when a new
240 * device is added/removed
241 *
242 * cleaner_mutex
243 * -------------
244 * a big lock that is held by the cleaner thread and prevents running subvolume
245 * cleaning together with relocation or delayed iputs
246 *
247 *
248 * Lock nesting
249 * ============
250 *
251 * uuid_mutex
252 * volume_mutex
253 * device_list_mutex
254 * chunk_mutex
255 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800256 *
257 *
258 * Exclusive operations, BTRFS_FS_EXCL_OP
259 * ======================================
260 *
261 * Maintains the exclusivity of the following operations that apply to the
262 * whole filesystem and cannot run in parallel.
263 *
264 * - Balance (*)
265 * - Device add
266 * - Device remove
267 * - Device replace (*)
268 * - Resize
269 *
270 * The device operations (as above) can be in one of the following states:
271 *
272 * - Running state
273 * - Paused state
274 * - Completed state
275 *
276 * Only device operations marked with (*) can go into the Paused state for the
277 * following reasons:
278 *
279 * - ioctl (only Balance can be Paused through ioctl)
280 * - filesystem remounted as read-only
281 * - filesystem unmounted and mounted as read-only
282 * - system power-cycle and filesystem mounted as read-only
283 * - filesystem or device errors leading to forced read-only
284 *
285 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
286 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
287 * A device operation in Paused or Running state can be canceled or resumed
288 * either by ioctl (Balance only) or when remounted as read-write.
289 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
290 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200291 */
292
Miao Xie67a2c452014-09-03 21:35:43 +0800293DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400294static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800295struct list_head *btrfs_get_fs_uuids(void)
296{
297 return &fs_uuids;
298}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400299
David Sterba2dfeca92017-06-14 02:48:07 +0200300/*
301 * alloc_fs_devices - allocate struct btrfs_fs_devices
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200302 * @fsid: if not NULL, copy the UUID to fs_devices::fsid
303 * @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
David Sterba2dfeca92017-06-14 02:48:07 +0200304 *
305 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
306 * The returned struct is not linked onto any lists and can be destroyed with
307 * kfree() right away.
308 */
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200309static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
310 const u8 *metadata_fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300311{
312 struct btrfs_fs_devices *fs_devs;
313
David Sterba78f2c9e2016-02-11 14:25:38 +0100314 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300315 if (!fs_devs)
316 return ERR_PTR(-ENOMEM);
317
318 mutex_init(&fs_devs->device_list_mutex);
319
320 INIT_LIST_HEAD(&fs_devs->devices);
321 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800322 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300323 if (fsid)
324 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300325
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200326 if (metadata_fsid)
327 memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
328 else if (fsid)
329 memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
330
Ilya Dryomov2208a372013-08-12 14:33:03 +0300331 return fs_devs;
332}
333
David Sterbaa425f9d2018-03-20 15:47:33 +0100334void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100335{
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200336 WARN_ON(!list_empty(&device->post_commit_list));
David Sterba48dae9c2017-10-30 18:10:25 +0100337 rcu_string_free(device->name);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200338 extent_io_tree_release(&device->alloc_state);
David Sterba48dae9c2017-10-30 18:10:25 +0100339 bio_put(device->flush_bio);
340 kfree(device);
341}
342
Yan Zhenge4404d62008-12-12 10:03:26 -0500343static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
344{
345 struct btrfs_device *device;
346 WARN_ON(fs_devices->opened);
347 while (!list_empty(&fs_devices->devices)) {
348 device = list_entry(fs_devices->devices.next,
349 struct btrfs_device, dev_list);
350 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100351 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500352 }
353 kfree(fs_devices);
354}
355
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000356static void btrfs_kobject_uevent(struct block_device *bdev,
357 enum kobject_action action)
358{
359 int ret;
360
361 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
362 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500363 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000364 action,
365 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
366 &disk_to_dev(bdev->bd_disk)->kobj);
367}
368
David Sterbaffc5a372018-02-19 17:24:15 +0100369void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370{
371 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372
Yan Zheng2b820322008-11-17 21:11:30 -0500373 while (!list_empty(&fs_uuids)) {
374 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800375 struct btrfs_fs_devices, fs_list);
376 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500377 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400378 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400379}
380
David Sterba48dae9c2017-10-30 18:10:25 +0100381/*
382 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
383 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100384 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100385 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300386static struct btrfs_device *__alloc_device(void)
387{
388 struct btrfs_device *dev;
389
David Sterba78f2c9e2016-02-11 14:25:38 +0100390 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300391 if (!dev)
392 return ERR_PTR(-ENOMEM);
393
David Sterbae0ae9992017-06-06 17:06:06 +0200394 /*
395 * Preallocate a bio that's always going to be used for flushing device
396 * barriers and matches the device lifespan
397 */
398 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
399 if (!dev->flush_bio) {
400 kfree(dev);
401 return ERR_PTR(-ENOMEM);
402 }
David Sterbae0ae9992017-06-06 17:06:06 +0200403
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300404 INIT_LIST_HEAD(&dev->dev_list);
405 INIT_LIST_HEAD(&dev->dev_alloc_list);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +0200406 INIT_LIST_HEAD(&dev->post_commit_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300407
408 spin_lock_init(&dev->io_lock);
409
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300410 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800411 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100412 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700413 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800414 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Jeff Mahoney1c11b632019-03-27 14:24:12 +0200415 extent_io_tree_init(NULL, &dev->alloc_state, 0, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300416
417 return dev;
418}
419
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200420static noinline struct btrfs_fs_devices *find_fsid(
421 const u8 *fsid, const u8 *metadata_fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400422{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400423 struct btrfs_fs_devices *fs_devices;
424
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200425 ASSERT(fsid);
426
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200427 if (metadata_fsid) {
428 /*
429 * Handle scanned device having completed its fsid change but
430 * belonging to a fs_devices that was created by first scanning
431 * a device which didn't have its fsid/metadata_uuid changed
432 * at all and the CHANGING_FSID_V2 flag set.
433 */
434 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
435 if (fs_devices->fsid_change &&
436 memcmp(metadata_fsid, fs_devices->fsid,
437 BTRFS_FSID_SIZE) == 0 &&
438 memcmp(fs_devices->fsid, fs_devices->metadata_uuid,
439 BTRFS_FSID_SIZE) == 0) {
440 return fs_devices;
441 }
442 }
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200443 /*
444 * Handle scanned device having completed its fsid change but
445 * belonging to a fs_devices that was created by a device that
446 * has an outdated pair of fsid/metadata_uuid and
447 * CHANGING_FSID_V2 flag set.
448 */
449 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
450 if (fs_devices->fsid_change &&
451 memcmp(fs_devices->metadata_uuid,
452 fs_devices->fsid, BTRFS_FSID_SIZE) != 0 &&
453 memcmp(metadata_fsid, fs_devices->metadata_uuid,
454 BTRFS_FSID_SIZE) == 0) {
455 return fs_devices;
456 }
457 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200458 }
459
460 /* Handle non-split brain cases */
Anand Jainc4babc52018-04-12 10:29:25 +0800461 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200462 if (metadata_fsid) {
463 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
464 && memcmp(metadata_fsid, fs_devices->metadata_uuid,
465 BTRFS_FSID_SIZE) == 0)
466 return fs_devices;
467 } else {
468 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
469 return fs_devices;
470 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400471 }
472 return NULL;
473}
474
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100475static int
476btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
477 int flush, struct block_device **bdev,
478 struct buffer_head **bh)
479{
480 int ret;
481
482 *bdev = blkdev_get_by_path(device_path, flags, holder);
483
484 if (IS_ERR(*bdev)) {
485 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100486 goto error;
487 }
488
489 if (flush)
490 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200491 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100492 if (ret) {
493 blkdev_put(*bdev, flags);
494 goto error;
495 }
496 invalidate_bdev(*bdev);
497 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800498 if (IS_ERR(*bh)) {
499 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100500 blkdev_put(*bdev, flags);
501 goto error;
502 }
503
504 return 0;
505
506error:
507 *bdev = NULL;
508 *bh = NULL;
509 return ret;
510}
511
Chris Masonffbd5172009-04-20 15:50:09 -0400512static void requeue_list(struct btrfs_pending_bios *pending_bios,
513 struct bio *head, struct bio *tail)
514{
515
516 struct bio *old_head;
517
518 old_head = pending_bios->head;
519 pending_bios->head = head;
520 if (pending_bios->tail)
521 tail->bi_next = old_head;
522 else
523 pending_bios->tail = tail;
524}
525
Chris Mason8b712842008-06-11 16:50:36 -0400526/*
527 * we try to collect pending bios for a device so we don't get a large
528 * number of procs sending bios down to the same device. This greatly
529 * improves the schedulers ability to collect and merge the bios.
530 *
531 * But, it also turns into a long list of bios to process and that is sure
532 * to eventually make the worker thread block. The solution here is to
533 * make some progress and then put this work struct back at the end of
534 * the list if the block device is congested. This way, multiple devices
535 * can make progress from a single worker thread.
536 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100537static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400538{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400539 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400540 struct bio *pending;
541 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400542 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400543 struct bio *tail;
544 struct bio *cur;
545 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400546 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400547 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400548 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400549 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000550 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400551 struct blk_plug plug;
552
553 /*
554 * this function runs all the bios we've collected for
555 * a particular device. We don't want to wander off to
556 * another device without first sending all of these down.
557 * So, setup a plug here and finish it off before we return
558 */
559 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400560
Jan Karaefa7c9f2017-02-02 15:56:53 +0100561 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400562
Chris Mason8b712842008-06-11 16:50:36 -0400563loop:
564 spin_lock(&device->io_lock);
565
Chris Masona6837052009-02-04 09:19:41 -0500566loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400567 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400568
Chris Mason8b712842008-06-11 16:50:36 -0400569 /* take all the bios off the list at once and process them
570 * later on (without the lock held). But, remember the
571 * tail and other pointers so the bios can be properly reinserted
572 * into the list if we hit congestion
573 */
Chris Masond84275c2009-06-09 15:39:08 -0400574 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400575 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400576 force_reg = 1;
577 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400578 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400579 force_reg = 0;
580 }
Chris Masonffbd5172009-04-20 15:50:09 -0400581
582 pending = pending_bios->head;
583 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400584 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400585
586 /*
587 * if pending was null this time around, no bios need processing
588 * at all and we can stop. Otherwise it'll loop back up again
589 * and do an additional check so no bios are missed.
590 *
591 * device->running_pending is used to synchronize with the
592 * schedule_bio code.
593 */
Chris Masonffbd5172009-04-20 15:50:09 -0400594 if (device->pending_sync_bios.head == NULL &&
595 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400596 again = 0;
597 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400598 } else {
599 again = 1;
600 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400601 }
Chris Masonffbd5172009-04-20 15:50:09 -0400602
603 pending_bios->head = NULL;
604 pending_bios->tail = NULL;
605
Chris Mason8b712842008-06-11 16:50:36 -0400606 spin_unlock(&device->io_lock);
607
Chris Masond3977122009-01-05 21:25:51 -0500608 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400609
610 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400611 /* we want to work on both lists, but do more bios on the
612 * sync list than the regular list
613 */
614 if ((num_run > 32 &&
615 pending_bios != &device->pending_sync_bios &&
616 device->pending_sync_bios.head) ||
617 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
618 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400619 spin_lock(&device->io_lock);
620 requeue_list(pending_bios, pending, tail);
621 goto loop_lock;
622 }
623
Chris Mason8b712842008-06-11 16:50:36 -0400624 cur = pending;
625 pending = pending->bi_next;
626 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400627
Jens Axboedac56212015-04-17 16:23:59 -0600628 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400629
Chris Mason2ab1ba62011-08-04 14:28:36 -0400630 /*
631 * if we're doing the sync list, record that our
632 * plug has some sync requests on it
633 *
634 * If we're doing the regular list and there are
635 * sync requests sitting around, unplug before
636 * we add more
637 */
638 if (pending_bios == &device->pending_sync_bios) {
639 sync_pending = 1;
640 } else if (sync_pending) {
641 blk_finish_plug(&plug);
642 blk_start_plug(&plug);
643 sync_pending = 0;
644 }
645
Mike Christie4e49ea42016-06-05 14:31:41 -0500646 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400647 num_run++;
648 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100649
650 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400651
652 /*
653 * we made progress, there is more work to do and the bdi
654 * is now congested. Back off and let other work structs
655 * run instead
656 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400657 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500658 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400659 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400660
Chris Masonb765ead2009-04-03 10:27:10 -0400661 ioc = current->io_context;
662
663 /*
664 * the main goal here is that we don't want to
665 * block if we're going to be able to submit
666 * more requests without blocking.
667 *
668 * This code does two great things, it pokes into
669 * the elevator code from a filesystem _and_
670 * it makes assumptions about how batching works.
671 */
672 if (ioc && ioc->nr_batch_requests > 0 &&
673 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
674 (last_waited == 0 ||
675 ioc->last_waited == last_waited)) {
676 /*
677 * we want to go through our batch of
678 * requests and stop. So, we copy out
679 * the ioc->last_waited time and test
680 * against it before looping
681 */
682 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100683 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400684 continue;
685 }
Chris Mason8b712842008-06-11 16:50:36 -0400686 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400687 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500688 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400689
690 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800691 btrfs_queue_work(fs_info->submit_workers,
692 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400693 goto done;
694 }
695 }
Chris Masonffbd5172009-04-20 15:50:09 -0400696
Chris Mason51684082010-03-10 15:33:32 -0500697 cond_resched();
698 if (again)
699 goto loop;
700
701 spin_lock(&device->io_lock);
702 if (device->pending_bios.head || device->pending_sync_bios.head)
703 goto loop_lock;
704 spin_unlock(&device->io_lock);
705
Chris Mason8b712842008-06-11 16:50:36 -0400706done:
Chris Mason211588a2011-04-19 20:12:40 -0400707 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400708}
709
Christoph Hellwigb2950862008-12-02 09:54:17 -0500710static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400711{
712 struct btrfs_device *device;
713
714 device = container_of(work, struct btrfs_device, work);
715 run_scheduled_bios(device);
716}
717
Anand Jain70bc7082019-01-04 13:31:53 +0800718static bool device_path_matched(const char *path, struct btrfs_device *device)
719{
720 int found;
721
722 rcu_read_lock();
723 found = strcmp(rcu_str_deref(device->name), path);
724 rcu_read_unlock();
725
726 return found == 0;
727}
728
Anand Jaind8367db2018-01-18 22:00:37 +0800729/*
730 * Search and remove all stale (devices which are not mounted) devices.
731 * When both inputs are NULL, it will search and release all stale devices.
732 * path: Optional. When provided will it release all unmounted devices
733 * matching this path only.
734 * skip_dev: Optional. Will skip this device when searching for the stale
735 * devices.
Anand Jain70bc7082019-01-04 13:31:53 +0800736 * Return: 0 for success or if @path is NULL.
737 * -EBUSY if @path is a mounted device.
738 * -ENOENT if @path does not match any device in the list.
Anand Jaind8367db2018-01-18 22:00:37 +0800739 */
Anand Jain70bc7082019-01-04 13:31:53 +0800740static int btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800741 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800742{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800743 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
744 struct btrfs_device *device, *tmp_device;
Anand Jain70bc7082019-01-04 13:31:53 +0800745 int ret = 0;
746
747 if (path)
748 ret = -ENOENT;
Anand Jain4fde46f2015-06-17 21:10:48 +0800749
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800750 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800751
Anand Jain70bc7082019-01-04 13:31:53 +0800752 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800753 list_for_each_entry_safe(device, tmp_device,
754 &fs_devices->devices, dev_list) {
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800755 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800756 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800757 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800758 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800759 if (path && !device_path_matched(path, device))
Anand Jain38cf6652018-01-18 22:00:34 +0800760 continue;
Anand Jain70bc7082019-01-04 13:31:53 +0800761 if (fs_devices->opened) {
762 /* for an already deleted device return 0 */
763 if (path && ret != 0)
764 ret = -EBUSY;
765 break;
766 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800767
Anand Jain4fde46f2015-06-17 21:10:48 +0800768 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800769 fs_devices->num_devices--;
770 list_del(&device->dev_list);
771 btrfs_free_device(device);
772
Anand Jain70bc7082019-01-04 13:31:53 +0800773 ret = 0;
Anand Jain7bcb8162018-05-29 17:23:20 +0800774 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200775 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800776 }
777 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain70bc7082019-01-04 13:31:53 +0800778
Anand Jain7bcb8162018-05-29 17:23:20 +0800779 if (fs_devices->num_devices == 0) {
780 btrfs_sysfs_remove_fsid(fs_devices);
781 list_del(&fs_devices->fs_list);
782 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800783 }
784 }
Anand Jain70bc7082019-01-04 13:31:53 +0800785
786 return ret;
Anand Jain4fde46f2015-06-17 21:10:48 +0800787}
788
Anand Jain0fb08bc2017-11-09 23:45:24 +0800789static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
790 struct btrfs_device *device, fmode_t flags,
791 void *holder)
792{
793 struct request_queue *q;
794 struct block_device *bdev;
795 struct buffer_head *bh;
796 struct btrfs_super_block *disk_super;
797 u64 devid;
798 int ret;
799
800 if (device->bdev)
801 return -EINVAL;
802 if (!device->name)
803 return -EINVAL;
804
805 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
806 &bdev, &bh);
807 if (ret)
808 return ret;
809
810 disk_super = (struct btrfs_super_block *)bh->b_data;
811 devid = btrfs_stack_device_id(&disk_super->dev_item);
812 if (devid != device->devid)
813 goto error_brelse;
814
815 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
816 goto error_brelse;
817
818 device->generation = btrfs_super_generation(disk_super);
819
820 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200821 if (btrfs_super_incompat_flags(disk_super) &
822 BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
823 pr_err(
824 "BTRFS: Invalid seeding and uuid-changed device detected\n");
825 goto error_brelse;
826 }
827
Anand Jainebbede42017-12-04 12:54:52 +0800828 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800829 fs_devices->seeding = 1;
830 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800831 if (bdev_read_only(bdev))
832 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
833 else
834 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800835 }
836
837 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800838 if (!blk_queue_nonrot(q))
839 fs_devices->rotating = 1;
840
841 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800842 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800843 device->mode = flags;
844
845 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800846 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
847 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800848 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800849 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800850 }
851 brelse(bh);
852
853 return 0;
854
855error_brelse:
856 brelse(bh);
857 blkdev_put(bdev, flags);
858
859 return -EINVAL;
860}
861
David Sterba60999ca2014-03-26 18:26:36 +0100862/*
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200863 * Handle scanned device having its CHANGING_FSID_V2 flag set and the fs_devices
864 * being created with a disk that has already completed its fsid change.
865 */
866static struct btrfs_fs_devices *find_fsid_inprogress(
867 struct btrfs_super_block *disk_super)
868{
869 struct btrfs_fs_devices *fs_devices;
870
871 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
872 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
873 BTRFS_FSID_SIZE) != 0 &&
874 memcmp(fs_devices->metadata_uuid, disk_super->fsid,
875 BTRFS_FSID_SIZE) == 0 && !fs_devices->fsid_change) {
876 return fs_devices;
877 }
878 }
879
880 return NULL;
881}
882
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200883
884static struct btrfs_fs_devices *find_fsid_changed(
885 struct btrfs_super_block *disk_super)
886{
887 struct btrfs_fs_devices *fs_devices;
888
889 /*
890 * Handles the case where scanned device is part of an fs that had
891 * multiple successful changes of FSID but curently device didn't
892 * observe it. Meaning our fsid will be different than theirs.
893 */
894 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
895 if (memcmp(fs_devices->metadata_uuid, fs_devices->fsid,
896 BTRFS_FSID_SIZE) != 0 &&
897 memcmp(fs_devices->metadata_uuid, disk_super->metadata_uuid,
898 BTRFS_FSID_SIZE) == 0 &&
899 memcmp(fs_devices->fsid, disk_super->fsid,
900 BTRFS_FSID_SIZE) != 0) {
901 return fs_devices;
902 }
903 }
904
905 return NULL;
906}
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200907/*
David Sterba60999ca2014-03-26 18:26:36 +0100908 * Add new device to list of registered devices
909 *
910 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800911 * device pointer which was just added or updated when successful
912 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100913 */
Anand Jaine124ece2018-01-18 22:02:35 +0800914static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800915 struct btrfs_super_block *disk_super,
916 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400917{
918 struct btrfs_device *device;
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200919 struct btrfs_fs_devices *fs_devices = NULL;
Josef Bacik606686e2012-06-04 14:03:51 -0400920 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400921 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800922 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200923 bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
924 BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
Nikolay Borisovd1a63002018-10-30 16:43:26 +0200925 bool fsid_change_in_progress = (btrfs_super_flags(disk_super) &
926 BTRFS_SUPER_FLAG_CHANGING_FSID_V2);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400927
Nikolay Borisovcc5de4e2018-10-30 16:43:28 +0200928 if (fsid_change_in_progress) {
929 if (!has_metadata_uuid) {
930 /*
931 * When we have an image which has CHANGING_FSID_V2 set
932 * it might belong to either a filesystem which has
933 * disks with completed fsid change or it might belong
934 * to fs with no UUID changes in effect, handle both.
935 */
936 fs_devices = find_fsid_inprogress(disk_super);
937 if (!fs_devices)
938 fs_devices = find_fsid(disk_super->fsid, NULL);
939 } else {
940 fs_devices = find_fsid_changed(disk_super);
941 }
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200942 } else if (has_metadata_uuid) {
943 fs_devices = find_fsid(disk_super->fsid,
944 disk_super->metadata_uuid);
945 } else {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200946 fs_devices = find_fsid(disk_super->fsid, NULL);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200947 }
948
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200949
Chris Mason8a4b83c2008-03-24 15:02:07 -0400950 if (!fs_devices) {
Nikolay Borisov7239ff42018-10-30 16:43:23 +0200951 if (has_metadata_uuid)
952 fs_devices = alloc_fs_devices(disk_super->fsid,
953 disk_super->metadata_uuid);
954 else
955 fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
956
Ilya Dryomov2208a372013-08-12 14:33:03 +0300957 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800958 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300959
Al Viro92900e52019-01-27 04:58:00 +0000960 fs_devices->fsid_change = fsid_change_in_progress;
961
Anand Jain9c6d1732018-05-29 14:10:20 +0800962 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800963 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300964
Chris Mason8a4b83c2008-03-24 15:02:07 -0400965 device = NULL;
966 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800967 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +0800968 device = btrfs_find_device(fs_devices, devid,
969 disk_super->dev_item.uuid, NULL, false);
Nikolay Borisov7a62d0f2018-10-30 16:43:27 +0200970
971 /*
972 * If this disk has been pulled into an fs devices created by
973 * a device which had the CHANGING_FSID_V2 flag then replace the
974 * metadata_uuid/fsid values of the fs_devices.
975 */
976 if (has_metadata_uuid && fs_devices->fsid_change &&
977 found_transid > fs_devices->latest_generation) {
978 memcpy(fs_devices->fsid, disk_super->fsid,
979 BTRFS_FSID_SIZE);
980 memcpy(fs_devices->metadata_uuid,
981 disk_super->metadata_uuid, BTRFS_FSID_SIZE);
982
983 fs_devices->fsid_change = false;
984 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400985 }
Miao Xie443f24f2014-07-24 11:37:15 +0800986
Chris Mason8a4b83c2008-03-24 15:02:07 -0400987 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800988 if (fs_devices->opened) {
989 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800990 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800991 }
Yan Zheng2b820322008-11-17 21:11:30 -0500992
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300993 device = btrfs_alloc_device(NULL, &devid,
994 disk_super->dev_item.uuid);
995 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800996 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400997 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800998 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400999 }
Josef Bacik606686e2012-06-04 14:03:51 -04001000
1001 name = rcu_string_strdup(path, GFP_NOFS);
1002 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001003 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +08001004 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001005 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001006 }
Josef Bacik606686e2012-06-04 14:03:51 -04001007 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +02001008
Xiao Guangrong1f781602011-04-20 10:09:16 +00001009 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01001010 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -04001011
Yan Zheng2b820322008-11-17 21:11:30 -05001012 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001013 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +08001014
1015 if (disk_super->label[0])
1016 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
1017 disk_super->label, devid, found_transid, path);
1018 else
1019 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
1020 disk_super->fsid, devid, found_transid, path);
1021
Josef Bacik606686e2012-06-04 14:03:51 -04001022 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +08001023 /*
1024 * When FS is already mounted.
1025 * 1. If you are here and if the device->name is NULL that
1026 * means this device was missing at time of FS mount.
1027 * 2. If you are here and if the device->name is different
1028 * from 'path' that means either
1029 * a. The same device disappeared and reappeared with
1030 * different name. or
1031 * b. The missing-disk-which-was-replaced, has
1032 * reappeared now.
1033 *
1034 * We must allow 1 and 2a above. But 2b would be a spurious
1035 * and unintentional.
1036 *
1037 * Further in case of 1 and 2a above, the disk at 'path'
1038 * would have missed some transaction when it was away and
1039 * in case of 2a the stale bdev has to be updated as well.
1040 * 2b must not be allowed at all time.
1041 */
1042
1043 /*
Chris Mason0f23ae72014-09-18 07:49:05 -07001044 * For now, we do allow update to btrfs_fs_device through the
1045 * btrfs dev scan cli after FS has been mounted. We're still
1046 * tracking a problem where systems fail mount by subvolume id
1047 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +08001048 */
Chris Mason0f23ae72014-09-18 07:49:05 -07001049 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +08001050 /*
1051 * That is if the FS is _not_ mounted and if you
1052 * are here, that means there is more than one
1053 * disk with same uuid and devid.We keep the one
1054 * with larger generation number or the last-in if
1055 * generation are equal.
1056 */
Anand Jain9c6d1732018-05-29 14:10:20 +08001057 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001058 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +08001059 }
Anand Jainb96de002014-07-03 18:22:05 +08001060
Anand Jaina9261d42018-10-15 10:45:17 +08001061 /*
1062 * We are going to replace the device path for a given devid,
1063 * make sure it's the same device if the device is mounted
1064 */
1065 if (device->bdev) {
1066 struct block_device *path_bdev;
1067
1068 path_bdev = lookup_bdev(path);
1069 if (IS_ERR(path_bdev)) {
1070 mutex_unlock(&fs_devices->device_list_mutex);
1071 return ERR_CAST(path_bdev);
1072 }
1073
1074 if (device->bdev != path_bdev) {
1075 bdput(path_bdev);
1076 mutex_unlock(&fs_devices->device_list_mutex);
1077 btrfs_warn_in_rcu(device->fs_info,
1078 "duplicate device fsid:devid for %pU:%llu old:%s new:%s",
1079 disk_super->fsid, devid,
1080 rcu_str_deref(device->name), path);
1081 return ERR_PTR(-EEXIST);
1082 }
1083 bdput(path_bdev);
1084 btrfs_info_in_rcu(device->fs_info,
1085 "device fsid %pU devid %llu moved old:%s new:%s",
1086 disk_super->fsid, devid,
1087 rcu_str_deref(device->name), path);
1088 }
1089
Josef Bacik606686e2012-06-04 14:03:51 -04001090 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +08001091 if (!name) {
1092 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001093 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +08001094 }
Josef Bacik606686e2012-06-04 14:03:51 -04001095 rcu_string_free(device->name);
1096 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +08001097 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05001098 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08001099 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05001100 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001101 }
1102
Anand Jain77bdae42014-07-03 18:22:06 +08001103 /*
1104 * Unmount does not free the btrfs_device struct but would zero
1105 * generation along with most of the other members. So just update
1106 * it back. We need it to pick the disk with largest generation
1107 * (as above).
1108 */
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001109 if (!fs_devices->opened) {
Anand Jain77bdae42014-07-03 18:22:06 +08001110 device->generation = found_transid;
Nikolay Borisovd1a63002018-10-30 16:43:26 +02001111 fs_devices->latest_generation = max_t(u64, found_transid,
1112 fs_devices->latest_generation);
1113 }
Anand Jain77bdae42014-07-03 18:22:06 +08001114
Anand Jainf2788d22018-01-18 22:02:34 +08001115 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
1116
Anand Jain9c6d1732018-05-29 14:10:20 +08001117 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +08001118 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001119}
1120
Yan Zhenge4404d62008-12-12 10:03:26 -05001121static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
1122{
1123 struct btrfs_fs_devices *fs_devices;
1124 struct btrfs_device *device;
1125 struct btrfs_device *orig_dev;
1126
Nikolay Borisov7239ff42018-10-30 16:43:23 +02001127 fs_devices = alloc_fs_devices(orig->fsid, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03001128 if (IS_ERR(fs_devices))
1129 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001130
Miao Xieadbbb862014-09-03 21:35:42 +08001131 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -04001132 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001133
1134 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -04001135 struct rcu_string *name;
1136
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001137 device = btrfs_alloc_device(NULL, &orig_dev->devid,
1138 orig_dev->uuid);
1139 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -05001140 goto error;
1141
Josef Bacik606686e2012-06-04 14:03:51 -04001142 /*
1143 * This is ok to do without rcu read locked because we hold the
1144 * uuid mutex so nothing we touch in here is going to disappear.
1145 */
Anand Jaine755f782014-06-30 17:12:47 +08001146 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +01001147 name = rcu_string_strdup(orig_dev->name->str,
1148 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +08001149 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +01001150 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +08001151 goto error;
1152 }
1153 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -04001154 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001155
Yan Zhenge4404d62008-12-12 10:03:26 -05001156 list_add(&device->dev_list, &fs_devices->devices);
1157 device->fs_devices = fs_devices;
1158 fs_devices->num_devices++;
1159 }
Miao Xieadbbb862014-09-03 21:35:42 +08001160 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001161 return fs_devices;
1162error:
Miao Xieadbbb862014-09-03 21:35:42 +08001163 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001164 free_fs_devices(fs_devices);
1165 return ERR_PTR(-ENOMEM);
1166}
1167
Anand Jain9b99b112018-02-27 12:41:59 +08001168/*
1169 * After we have read the system tree and know devids belonging to
1170 * this filesystem, remove the device which does not belong there.
1171 */
1172void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -04001173{
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001174 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +08001175 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001176
Chris Masondfe25022008-05-13 13:46:40 -04001177 mutex_lock(&uuid_mutex);
1178again:
Xiao Guangrong46224702011-04-20 10:08:47 +00001179 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001180 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08001181 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
1182 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +08001183 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1184 &device->dev_state) &&
1185 (!latest_dev ||
1186 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +08001187 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001188 }
Yan Zheng2b820322008-11-17 21:11:30 -05001189 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001190 }
Yan Zheng2b820322008-11-17 21:11:30 -05001191
Stefan Behrens8dabb742012-11-06 13:15:27 +01001192 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
1193 /*
1194 * In the first step, keep the device which has
1195 * the correct fsid and the devid that is used
1196 * for the dev_replace procedure.
1197 * In the second step, the dev_replace state is
1198 * read from the device tree and it is known
1199 * whether the procedure is really active or
1200 * not, which means whether this device is
1201 * used or whether it should be removed.
1202 */
Anand Jain401e29c2017-12-04 12:54:55 +08001203 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1204 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +01001205 continue;
1206 }
1207 }
Yan Zheng2b820322008-11-17 21:11:30 -05001208 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +01001209 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -05001210 device->bdev = NULL;
1211 fs_devices->open_devices--;
1212 }
Anand Jainebbede42017-12-04 12:54:52 +08001213 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05001214 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +08001215 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08001216 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
1217 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +01001218 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001219 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001220 list_del_init(&device->dev_list);
1221 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +01001222 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -04001223 }
Yan Zheng2b820322008-11-17 21:11:30 -05001224
1225 if (fs_devices->seed) {
1226 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001227 goto again;
1228 }
1229
Miao Xie443f24f2014-07-24 11:37:15 +08001230 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -05001231
Chris Masondfe25022008-05-13 13:46:40 -04001232 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -04001233}
Chris Masona0af4692008-05-13 16:03:06 -04001234
Anand Jain14238812016-07-22 06:04:53 +08001235static void btrfs_close_bdev(struct btrfs_device *device)
1236{
David Sterba08ffcae2017-06-19 16:55:35 +02001237 if (!device->bdev)
1238 return;
1239
Anand Jainebbede42017-12-04 12:54:52 +08001240 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001241 sync_blockdev(device->bdev);
1242 invalidate_bdev(device->bdev);
1243 }
1244
David Sterba08ffcae2017-06-19 16:55:35 +02001245 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001246}
1247
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001248static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001249{
1250 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1251 struct btrfs_device *new_device;
1252 struct rcu_string *name;
1253
1254 if (device->bdev)
1255 fs_devices->open_devices--;
1256
Anand Jainebbede42017-12-04 12:54:52 +08001257 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001258 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1259 list_del_init(&device->dev_alloc_list);
1260 fs_devices->rw_devices--;
1261 }
1262
Anand Jaine6e674b2017-12-04 12:54:54 +08001263 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001264 fs_devices->missing_devices--;
1265
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001266 btrfs_close_bdev(device);
1267
Anand Jainf448341a2016-06-14 18:55:25 +08001268 new_device = btrfs_alloc_device(NULL, &device->devid,
1269 device->uuid);
1270 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1271
1272 /* Safe because we are under uuid_mutex */
1273 if (device->name) {
1274 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1275 BUG_ON(!name); /* -ENOMEM */
1276 rcu_assign_pointer(new_device->name, name);
1277 }
1278
1279 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1280 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001281
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02001282 synchronize_rcu();
1283 btrfs_free_device(device);
Anand Jainf448341a2016-06-14 18:55:25 +08001284}
1285
Anand Jain0226e0e2018-04-12 10:29:27 +08001286static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001287{
Sasha Levin2037a092015-05-12 19:31:37 -04001288 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001289
Yan Zheng2b820322008-11-17 21:11:30 -05001290 if (--fs_devices->opened > 0)
1291 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001292
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001293 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001294 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001295 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001296 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001297 mutex_unlock(&fs_devices->device_list_mutex);
1298
Yan Zhenge4404d62008-12-12 10:03:26 -05001299 WARN_ON(fs_devices->open_devices);
1300 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001301 fs_devices->opened = 0;
1302 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001303
Chris Mason8a4b83c2008-03-24 15:02:07 -04001304 return 0;
1305}
1306
Yan Zheng2b820322008-11-17 21:11:30 -05001307int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1308{
Yan Zhenge4404d62008-12-12 10:03:26 -05001309 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001310 int ret;
1311
1312 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001313 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001314 if (!fs_devices->opened) {
1315 seed_devices = fs_devices->seed;
1316 fs_devices->seed = NULL;
1317 }
Yan Zheng2b820322008-11-17 21:11:30 -05001318 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001319
1320 while (seed_devices) {
1321 fs_devices = seed_devices;
1322 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001323 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001324 free_fs_devices(fs_devices);
1325 }
Yan Zheng2b820322008-11-17 21:11:30 -05001326 return ret;
1327}
1328
Anand Jain897fb572018-04-12 10:29:28 +08001329static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001330 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001331{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001332 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001333 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001334 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001335
Tejun Heod4d77622010-11-13 11:55:18 +01001336 flags |= FMODE_EXCL;
1337
Anand Jainf117e292018-04-12 10:29:26 +08001338 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001339 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001340 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001341 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001342
Anand Jain9f050db2017-11-09 23:45:25 +08001343 if (!latest_dev ||
1344 device->generation > latest_dev->generation)
1345 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001346 }
Chris Masona0af4692008-05-13 16:03:06 -04001347 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001348 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001349 goto out;
1350 }
Yan Zheng2b820322008-11-17 21:11:30 -05001351 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001352 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001353 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001354out:
Yan Zheng2b820322008-11-17 21:11:30 -05001355 return ret;
1356}
1357
Anand Jainf8e10cd2018-01-22 14:49:36 -08001358static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1359{
1360 struct btrfs_device *dev1, *dev2;
1361
1362 dev1 = list_entry(a, struct btrfs_device, dev_list);
1363 dev2 = list_entry(b, struct btrfs_device, dev_list);
1364
1365 if (dev1->devid < dev2->devid)
1366 return -1;
1367 else if (dev1->devid > dev2->devid)
1368 return 1;
1369 return 0;
1370}
1371
Yan Zheng2b820322008-11-17 21:11:30 -05001372int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001373 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001374{
1375 int ret;
1376
David Sterbaf5194e32018-06-19 17:09:47 +02001377 lockdep_assert_held(&uuid_mutex);
1378
Anand Jain542c5902018-04-12 10:29:34 +08001379 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001380 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001381 fs_devices->opened++;
1382 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001383 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001384 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001385 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001386 }
Anand Jain542c5902018-04-12 10:29:34 +08001387 mutex_unlock(&fs_devices->device_list_mutex);
1388
Chris Mason8a4b83c2008-03-24 15:02:07 -04001389 return ret;
1390}
1391
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001392static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001393{
1394 kunmap(page);
1395 put_page(page);
1396}
1397
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001398static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1399 struct page **page,
1400 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001401{
1402 void *p;
1403 pgoff_t index;
1404
1405 /* make sure our super fits in the device */
1406 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1407 return 1;
1408
1409 /* make sure our super fits in the page */
1410 if (sizeof(**disk_super) > PAGE_SIZE)
1411 return 1;
1412
1413 /* make sure our super doesn't straddle pages on disk */
1414 index = bytenr >> PAGE_SHIFT;
1415 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1416 return 1;
1417
1418 /* pull in the page with our super */
1419 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1420 index, GFP_KERNEL);
1421
1422 if (IS_ERR_OR_NULL(*page))
1423 return 1;
1424
1425 p = kmap(*page);
1426
1427 /* align our pointer to the offset of the super block */
Johannes Thumshirn70730172018-12-05 15:23:03 +01001428 *disk_super = p + offset_in_page(bytenr);
Anand Jain6cf86a002016-02-13 10:01:29 +08001429
1430 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1431 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1432 btrfs_release_disk_super(*page);
1433 return 1;
1434 }
1435
1436 if ((*disk_super)->label[0] &&
1437 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1438 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1439
1440 return 0;
1441}
1442
Anand Jain228a73a2019-01-04 13:31:54 +08001443int btrfs_forget_devices(const char *path)
1444{
1445 int ret;
1446
1447 mutex_lock(&uuid_mutex);
1448 ret = btrfs_free_stale_devices(strlen(path) ? path : NULL, NULL);
1449 mutex_unlock(&uuid_mutex);
1450
1451 return ret;
1452}
1453
David Sterba6f60cbd2013-02-15 11:31:02 -07001454/*
1455 * Look for a btrfs signature on a device. This may be called out of the mount path
1456 * and we are not allowed to call set_blocksize during the scan. The superblock
1457 * is read via pagecache
1458 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001459struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1460 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001461{
1462 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001463 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001464 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001465 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001466 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001467 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001468
David Sterba899f9302018-06-19 16:37:36 +02001469 lockdep_assert_held(&uuid_mutex);
1470
David Sterba6f60cbd2013-02-15 11:31:02 -07001471 /*
1472 * we would like to check all the supers, but that would make
1473 * a btrfs mount succeed after a mkfs from a different FS.
1474 * So, we need to add a special mount option to scan for
1475 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1476 */
1477 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001478 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001479
1480 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001481 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001482 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001483
Anand Jain05a5c552017-12-15 15:40:16 +08001484 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001485 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001486 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001487 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001488
Anand Jain4306a972018-05-29 12:28:37 +08001489 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001490 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001491 if (new_device_added)
1492 btrfs_free_stale_devices(path, device);
1493 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001494
Anand Jain6cf86a002016-02-13 10:01:29 +08001495 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001496
1497error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001498 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001499
Gu Jinxiang36350e92018-07-12 14:23:16 +08001500 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001501}
Chris Mason0b86a832008-03-24 15:01:56 -04001502
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001503/*
1504 * Try to find a chunk that intersects [start, start + len] range and when one
1505 * such is found, record the end of it in *start
1506 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001507static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
1508 u64 len)
Josef Bacik6df9a952013-06-27 13:22:46 -04001509{
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001510 u64 physical_start, physical_end;
Josef Bacik6df9a952013-06-27 13:22:46 -04001511
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001512 lockdep_assert_held(&device->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04001513
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001514 if (!find_first_extent_bit(&device->alloc_state, *start,
1515 &physical_start, &physical_end,
1516 CHUNK_ALLOCATED, NULL)) {
Filipe Mananac152b632015-05-14 10:46:03 +01001517
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001518 if (in_range(physical_start, *start, len) ||
1519 in_range(*start, physical_start,
1520 physical_end - physical_start)) {
1521 *start = physical_end + 1;
1522 return true;
Josef Bacik6df9a952013-06-27 13:22:46 -04001523 }
1524 }
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001525 return false;
Josef Bacik6df9a952013-06-27 13:22:46 -04001526}
1527
1528
Chris Mason0b86a832008-03-24 15:01:56 -04001529/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001530 * find_free_dev_extent_start - find free space in the specified device
1531 * @device: the device which we search the free space in
1532 * @num_bytes: the size of the free space that we need
1533 * @search_start: the position from which to begin the search
1534 * @start: store the start of the free space.
1535 * @len: the size of the free space. that we find, or the size
1536 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001537 *
Chris Mason0b86a832008-03-24 15:01:56 -04001538 * this uses a pretty simple search, the expectation is that it is
1539 * called very infrequently and that a given device has a small number
1540 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001541 *
1542 * @start is used to store the start of the free space if we find. But if we
1543 * don't find suitable free space, it will be used to store the start position
1544 * of the max free space.
1545 *
1546 * @len is used to store the size of the free space that we find.
1547 * But if we don't find suitable free space, it is used to store the size of
1548 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001549 */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001550int find_free_dev_extent_start(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001551 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001552{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001553 struct btrfs_fs_info *fs_info = device->fs_info;
1554 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001555 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001556 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001557 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001558 u64 hole_size;
1559 u64 max_hole_start;
1560 u64 max_hole_size;
1561 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001562 u64 search_end = device->total_bytes;
1563 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001564 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001565 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001566
1567 /*
1568 * We don't want to overwrite the superblock on the drive nor any area
1569 * used by the boot loader (grub for example), so we make sure to start
1570 * at an offset of at least 1MB.
1571 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001572 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001573
Josef Bacik6df9a952013-06-27 13:22:46 -04001574 path = btrfs_alloc_path();
1575 if (!path)
1576 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001577
Miao Xie7bfc8372011-01-05 10:07:26 +00001578 max_hole_start = search_start;
1579 max_hole_size = 0;
1580
Zhao Leif2ab7612015-02-16 18:52:17 +08001581again:
Anand Jain401e29c2017-12-04 12:54:55 +08001582 if (search_start >= search_end ||
1583 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001584 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001585 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001586 }
1587
David Sterbae4058b52015-11-27 16:31:35 +01001588 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001589 path->search_commit_root = 1;
1590 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001591
Chris Mason0b86a832008-03-24 15:01:56 -04001592 key.objectid = device->devid;
1593 key.offset = search_start;
1594 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001595
Li Zefan125ccb02011-12-08 15:07:24 +08001596 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001597 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001598 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001599 if (ret > 0) {
1600 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1601 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001602 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001603 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001604
Chris Mason0b86a832008-03-24 15:01:56 -04001605 while (1) {
1606 l = path->nodes[0];
1607 slot = path->slots[0];
1608 if (slot >= btrfs_header_nritems(l)) {
1609 ret = btrfs_next_leaf(root, path);
1610 if (ret == 0)
1611 continue;
1612 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001613 goto out;
1614
1615 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001616 }
1617 btrfs_item_key_to_cpu(l, &key, slot);
1618
1619 if (key.objectid < device->devid)
1620 goto next;
1621
1622 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001623 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001624
David Sterba962a2982014-06-04 18:41:45 +02001625 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001626 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001627
Miao Xie7bfc8372011-01-05 10:07:26 +00001628 if (key.offset > search_start) {
1629 hole_size = key.offset - search_start;
1630
Josef Bacik6df9a952013-06-27 13:22:46 -04001631 /*
1632 * Have to check before we set max_hole_start, otherwise
1633 * we could end up sending back this offset anyway.
1634 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001635 if (contains_pending_extent(device, &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001636 hole_size)) {
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001637 if (key.offset >= search_start)
Forrest Liu1b984502015-02-09 17:30:47 +08001638 hole_size = key.offset - search_start;
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001639 else
Forrest Liu1b984502015-02-09 17:30:47 +08001640 hole_size = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001641 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001642
Miao Xie7bfc8372011-01-05 10:07:26 +00001643 if (hole_size > max_hole_size) {
1644 max_hole_start = search_start;
1645 max_hole_size = hole_size;
1646 }
1647
1648 /*
1649 * If this free space is greater than which we need,
1650 * it must be the max free space that we have found
1651 * until now, so max_hole_start must point to the start
1652 * of this free space and the length of this free space
1653 * is stored in max_hole_size. Thus, we return
1654 * max_hole_start and max_hole_size and go back to the
1655 * caller.
1656 */
1657 if (hole_size >= num_bytes) {
1658 ret = 0;
1659 goto out;
1660 }
1661 }
1662
Chris Mason0b86a832008-03-24 15:01:56 -04001663 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001664 extent_end = key.offset + btrfs_dev_extent_length(l,
1665 dev_extent);
1666 if (extent_end > search_start)
1667 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001668next:
1669 path->slots[0]++;
1670 cond_resched();
1671 }
Chris Mason0b86a832008-03-24 15:01:56 -04001672
liubo38c01b92011-08-02 02:39:03 +00001673 /*
1674 * At this point, search_start should be the end of
1675 * allocated dev extents, and when shrinking the device,
1676 * search_end may be smaller than search_start.
1677 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001678 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001679 hole_size = search_end - search_start;
1680
Jeff Mahoney1c11b632019-03-27 14:24:12 +02001681 if (contains_pending_extent(device, &search_start, hole_size)) {
Zhao Leif2ab7612015-02-16 18:52:17 +08001682 btrfs_release_path(path);
1683 goto again;
1684 }
Chris Mason0b86a832008-03-24 15:01:56 -04001685
Zhao Leif2ab7612015-02-16 18:52:17 +08001686 if (hole_size > max_hole_size) {
1687 max_hole_start = search_start;
1688 max_hole_size = hole_size;
1689 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001690 }
1691
Miao Xie7bfc8372011-01-05 10:07:26 +00001692 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001693 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001694 ret = -ENOSPC;
1695 else
1696 ret = 0;
1697
1698out:
Yan Zheng2b820322008-11-17 21:11:30 -05001699 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001700 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001701 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001702 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001703 return ret;
1704}
1705
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001706int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Jeff Mahoney499f3772015-06-15 09:41:17 -04001707 u64 *start, u64 *len)
1708{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001709 /* FIXME use last free of some kind */
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02001710 return find_free_dev_extent_start(device, num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001711}
1712
Christoph Hellwigb2950862008-12-02 09:54:17 -05001713static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001714 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001715 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001717 struct btrfs_fs_info *fs_info = device->fs_info;
1718 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001719 int ret;
1720 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001721 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001722 struct btrfs_key found_key;
1723 struct extent_buffer *leaf = NULL;
1724 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001725
1726 path = btrfs_alloc_path();
1727 if (!path)
1728 return -ENOMEM;
1729
1730 key.objectid = device->devid;
1731 key.offset = start;
1732 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001733again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001734 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001735 if (ret > 0) {
1736 ret = btrfs_previous_item(root, path, key.objectid,
1737 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001738 if (ret)
1739 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001740 leaf = path->nodes[0];
1741 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1742 extent = btrfs_item_ptr(leaf, path->slots[0],
1743 struct btrfs_dev_extent);
1744 BUG_ON(found_key.offset > start || found_key.offset +
1745 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001746 key = found_key;
1747 btrfs_release_path(path);
1748 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001749 } else if (ret == 0) {
1750 leaf = path->nodes[0];
1751 extent = btrfs_item_ptr(leaf, path->slots[0],
1752 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001753 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001754 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001755 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001756 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001757
Miao Xie2196d6e2014-09-03 21:35:41 +08001758 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1759
Chris Mason8f18cf12008-04-25 16:53:30 -04001760 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001761 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001762 btrfs_handle_fs_error(fs_info, ret,
1763 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001764 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001765 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001766 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001767out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001768 btrfs_free_path(path);
1769 return ret;
1770}
1771
Eric Sandeen48a3b632013-04-25 20:41:01 +00001772static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1773 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001774 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001775{
1776 int ret;
1777 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001778 struct btrfs_fs_info *fs_info = device->fs_info;
1779 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001780 struct btrfs_dev_extent *extent;
1781 struct extent_buffer *leaf;
1782 struct btrfs_key key;
1783
Anand Jaine12c9622017-12-04 12:54:53 +08001784 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001785 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001786 path = btrfs_alloc_path();
1787 if (!path)
1788 return -ENOMEM;
1789
Chris Mason0b86a832008-03-24 15:01:56 -04001790 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001791 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001792 key.type = BTRFS_DEV_EXTENT_KEY;
1793 ret = btrfs_insert_empty_item(trans, root, path, &key,
1794 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001795 if (ret)
1796 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001797
1798 leaf = path->nodes[0];
1799 extent = btrfs_item_ptr(leaf, path->slots[0],
1800 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001801 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1802 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001803 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1804 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001805 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1806
Chris Mason0b86a832008-03-24 15:01:56 -04001807 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1808 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001809out:
Chris Mason0b86a832008-03-24 15:01:56 -04001810 btrfs_free_path(path);
1811 return ret;
1812}
1813
Josef Bacik6df9a952013-06-27 13:22:46 -04001814static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001815{
Josef Bacik6df9a952013-06-27 13:22:46 -04001816 struct extent_map_tree *em_tree;
1817 struct extent_map *em;
1818 struct rb_node *n;
1819 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001820
David Sterbac8bf1b62019-05-17 11:43:17 +02001821 em_tree = &fs_info->mapping_tree;
Josef Bacik6df9a952013-06-27 13:22:46 -04001822 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001823 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001824 if (n) {
1825 em = rb_entry(n, struct extent_map, rb_node);
1826 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001827 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001828 read_unlock(&em_tree->lock);
1829
Chris Mason0b86a832008-03-24 15:01:56 -04001830 return ret;
1831}
1832
Ilya Dryomov53f10652013-08-12 14:33:01 +03001833static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1834 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001835{
1836 int ret;
1837 struct btrfs_key key;
1838 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001839 struct btrfs_path *path;
1840
Yan Zheng2b820322008-11-17 21:11:30 -05001841 path = btrfs_alloc_path();
1842 if (!path)
1843 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001844
1845 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1846 key.type = BTRFS_DEV_ITEM_KEY;
1847 key.offset = (u64)-1;
1848
Ilya Dryomov53f10652013-08-12 14:33:01 +03001849 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001850 if (ret < 0)
1851 goto error;
1852
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001853 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001854
Ilya Dryomov53f10652013-08-12 14:33:01 +03001855 ret = btrfs_previous_item(fs_info->chunk_root, path,
1856 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001857 BTRFS_DEV_ITEM_KEY);
1858 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001859 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001860 } else {
1861 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1862 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001863 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001864 }
1865 ret = 0;
1866error:
Yan Zheng2b820322008-11-17 21:11:30 -05001867 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001868 return ret;
1869}
1870
1871/*
1872 * the device information is stored in the chunk root
1873 * the btrfs_device struct should be fully filled in
1874 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001875static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001876 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001877{
1878 int ret;
1879 struct btrfs_path *path;
1880 struct btrfs_dev_item *dev_item;
1881 struct extent_buffer *leaf;
1882 struct btrfs_key key;
1883 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001884
Chris Mason0b86a832008-03-24 15:01:56 -04001885 path = btrfs_alloc_path();
1886 if (!path)
1887 return -ENOMEM;
1888
Chris Mason0b86a832008-03-24 15:01:56 -04001889 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1890 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001891 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001892
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001893 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1894 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001895 if (ret)
1896 goto out;
1897
1898 leaf = path->nodes[0];
1899 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1900
1901 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001902 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001903 btrfs_set_device_type(leaf, dev_item, device->type);
1904 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1905 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1906 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001907 btrfs_set_device_total_bytes(leaf, dev_item,
1908 btrfs_device_get_disk_total_bytes(device));
1909 btrfs_set_device_bytes_used(leaf, dev_item,
1910 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001911 btrfs_set_device_group(leaf, dev_item, 0);
1912 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1913 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001914 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001915
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001916 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001917 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001918 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001919 write_extent_buffer(leaf, trans->fs_info->fs_devices->metadata_uuid,
1920 ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001921 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001922
Yan Zheng2b820322008-11-17 21:11:30 -05001923 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001924out:
1925 btrfs_free_path(path);
1926 return ret;
1927}
Chris Mason8f18cf12008-04-25 16:53:30 -04001928
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001929/*
1930 * Function to update ctime/mtime for a given device path.
1931 * Mainly used for ctime/mtime based probe like libblkid.
1932 */
David Sterbada353f62017-02-14 17:55:53 +01001933static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001934{
1935 struct file *filp;
1936
1937 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001938 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001939 return;
1940 file_update_time(filp);
1941 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001942}
1943
David Sterbaf331a952019-03-20 16:31:53 +01001944static int btrfs_rm_dev_item(struct btrfs_device *device)
Chris Masona061fc82008-05-07 11:43:44 -04001945{
David Sterbaf331a952019-03-20 16:31:53 +01001946 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001947 int ret;
1948 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001949 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001950 struct btrfs_trans_handle *trans;
1951
Chris Masona061fc82008-05-07 11:43:44 -04001952 path = btrfs_alloc_path();
1953 if (!path)
1954 return -ENOMEM;
1955
Yan, Zhenga22285a2010-05-16 10:48:46 -04001956 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001957 if (IS_ERR(trans)) {
1958 btrfs_free_path(path);
1959 return PTR_ERR(trans);
1960 }
Chris Masona061fc82008-05-07 11:43:44 -04001961 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1962 key.type = BTRFS_DEV_ITEM_KEY;
1963 key.offset = device->devid;
1964
1965 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001966 if (ret) {
1967 if (ret > 0)
1968 ret = -ENOENT;
1969 btrfs_abort_transaction(trans, ret);
1970 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001971 goto out;
1972 }
1973
1974 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001975 if (ret) {
1976 btrfs_abort_transaction(trans, ret);
1977 btrfs_end_transaction(trans);
1978 }
1979
Chris Masona061fc82008-05-07 11:43:44 -04001980out:
1981 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001982 if (!ret)
1983 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001984 return ret;
1985}
1986
David Sterba3cc31a02016-02-15 16:00:26 +01001987/*
1988 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1989 * filesystem. It's up to the caller to adjust that number regarding eg. device
1990 * replace.
1991 */
1992static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1993 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001994{
Chris Masona061fc82008-05-07 11:43:44 -04001995 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001996 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001997 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001998
Miao Xiede98ced2013-01-29 10:13:12 +00001999 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08002000 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00002001
Anand Jainbd45ffb2016-02-13 10:01:34 +08002002 all_avail = fs_info->avail_data_alloc_bits |
2003 fs_info->avail_system_alloc_bits |
2004 fs_info->avail_metadata_alloc_bits;
2005 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08002006
David Sterba418775a2016-02-15 16:28:14 +01002007 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08002008 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01002009 continue;
Chris Masona061fc82008-05-07 11:43:44 -04002010
David Sterba418775a2016-02-15 16:28:14 +01002011 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08002012 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04002013
David Sterba418775a2016-02-15 16:28:14 +01002014 if (ret)
2015 return ret;
2016 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08002017 }
2018
2019 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002020}
2021
Omar Sandovalc9162bd2017-08-22 23:46:04 -07002022static struct btrfs_device * btrfs_find_next_active_device(
2023 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08002024{
2025 struct btrfs_device *next_device;
2026
2027 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
2028 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08002029 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
2030 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08002031 return next_device;
2032 }
2033
2034 return NULL;
2035}
2036
2037/*
2038 * Helper function to check if the given device is part of s_bdev / latest_bdev
2039 * and replace it with the provided or the next active device, in the context
2040 * where this function called, there should be always be another device (or
2041 * this_dev) which is active.
2042 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002043void btrfs_assign_next_active_device(struct btrfs_device *device,
2044 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08002045{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002046 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08002047 struct btrfs_device *next_device;
2048
2049 if (this_dev)
2050 next_device = this_dev;
2051 else
2052 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
2053 device);
2054 ASSERT(next_device);
2055
2056 if (fs_info->sb->s_bdev &&
2057 (fs_info->sb->s_bdev == device->bdev))
2058 fs_info->sb->s_bdev = next_device->bdev;
2059
2060 if (fs_info->fs_devices->latest_bdev == device->bdev)
2061 fs_info->fs_devices->latest_bdev = next_device->bdev;
2062}
2063
Anand Jain1da73962018-08-10 13:53:21 +08002064/*
2065 * Return btrfs_fs_devices::num_devices excluding the device that's being
2066 * currently replaced.
2067 */
2068static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
2069{
2070 u64 num_devices = fs_info->fs_devices->num_devices;
2071
David Sterbacb5583d2018-09-07 16:11:23 +02002072 down_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002073 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
2074 ASSERT(num_devices > 1);
2075 num_devices--;
2076 }
David Sterbacb5583d2018-09-07 16:11:23 +02002077 up_read(&fs_info->dev_replace.rwsem);
Anand Jain1da73962018-08-10 13:53:21 +08002078
2079 return num_devices;
2080}
2081
David Sterbada353f62017-02-14 17:55:53 +01002082int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
2083 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08002084{
2085 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002086 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002087 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002088 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002089 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08002090
2091 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002092
Anand Jain1da73962018-08-10 13:53:21 +08002093 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04002094
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002095 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08002096 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04002097 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002098
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002099 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
2100
2101 if (IS_ERR(device)) {
2102 if (PTR_ERR(device) == -ENOENT &&
2103 strcmp(device_path, "missing") == 0)
2104 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2105 else
2106 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04002107 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002108 }
Yan Zheng2b820322008-11-17 21:11:30 -05002109
Omar Sandovaleede2bf2016-11-03 10:28:12 -07002110 if (btrfs_pinned_by_swapfile(fs_info, device)) {
2111 btrfs_warn_in_rcu(fs_info,
2112 "cannot remove device %s (devid %llu) due to active swapfile",
2113 rcu_str_deref(device->name), device->devid);
2114 ret = -ETXTBSY;
2115 goto out;
2116 }
2117
Anand Jain401e29c2017-12-04 12:54:55 +08002118 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00002119 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08002120 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002121 }
2122
Anand Jainebbede42017-12-04 12:54:52 +08002123 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
2124 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00002125 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08002126 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05002127 }
2128
Anand Jainebbede42017-12-04 12:54:52 +08002129 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002130 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002131 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002132 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02002133 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002134 }
Chris Masona061fc82008-05-07 11:43:44 -04002135
Carey Underwoodd7901552013-03-04 16:37:06 -06002136 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002137 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06002138 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002139 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002140 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002141
Stefan Behrens63a212a2012-11-05 18:29:28 +01002142 /*
2143 * TODO: the superblock still includes this device in its num_devices
2144 * counter although write_all_supers() is not locked out. This
2145 * could give a filesystem state which requires a degraded mount.
2146 */
David Sterbaf331a952019-03-20 16:31:53 +01002147 ret = btrfs_rm_dev_item(device);
Chris Masona061fc82008-05-07 11:43:44 -04002148 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002149 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002150
Anand Jaine12c9622017-12-04 12:54:53 +08002151 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
David Sterba163e97e2019-03-20 16:32:55 +01002152 btrfs_scrub_cancel_dev(device);
Chris Masone5e9a522009-06-10 15:17:02 -04002153
2154 /*
2155 * the device list mutex makes sure that we don't change
2156 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002157 * the device supers. Whoever is writing all supers, should
2158 * lock the device list mutex before getting the number of
2159 * devices in the super block (super_copy). Conversely,
2160 * whoever updates the number of devices in the super block
2161 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002162 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002163
Anand Jain41a52a02018-04-12 10:29:31 +08002164 /*
2165 * In normal cases the cur_devices == fs_devices. But in case
2166 * of deleting a seed device, the cur_devices should point to
2167 * its own fs_devices listed under the fs_devices->seed.
2168 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002169 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002170 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002171 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002172
Anand Jain41a52a02018-04-12 10:29:31 +08002173 cur_devices->num_devices--;
2174 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08002175 /* Update total_devices of the parent fs_devices if it's seed */
2176 if (cur_devices != fs_devices)
2177 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002178
Anand Jaine6e674b2017-12-04 12:54:54 +08002179 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002180 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002181
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002182 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002183
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002184 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002185 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002186 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002187 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002188 }
Anand Jain99994cd2014-06-03 11:36:00 +08002189
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2191 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002192 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002193
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002194 /*
2195 * at this point, the device is zero sized and detached from
2196 * the devices list. All that's left is to zero out the old
2197 * supers and free the device.
2198 */
Anand Jainebbede42017-12-04 12:54:52 +08002199 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002200 btrfs_scratch_superblocks(device->bdev, device->name->str);
2201
2202 btrfs_close_bdev(device);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002203 synchronize_rcu();
2204 btrfs_free_device(device);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002205
Xiao Guangrong1f781602011-04-20 10:09:16 +00002206 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002207 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002208 if (fs_devices->seed == cur_devices) {
2209 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002210 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002211 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002212 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002213 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002214 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002215 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002216 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002217 }
2218
Chris Masona061fc82008-05-07 11:43:44 -04002219out:
2220 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002221 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002222
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002223error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002224 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002225 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002226 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002227 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002228 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002229 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002230 }
Anand Jain24fc5722016-02-13 10:01:36 +08002231 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002232}
2233
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002234void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002235{
Anand Jaind51908c2014-08-13 14:24:19 +08002236 struct btrfs_fs_devices *fs_devices;
2237
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002238 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002239
Anand Jain25e8e912014-08-20 10:56:56 +08002240 /*
2241 * in case of fs with no seed, srcdev->fs_devices will point
2242 * to fs_devices of fs_info. However when the dev being replaced is
2243 * a seed dev it will point to the seed's local fs_devices. In short
2244 * srcdev will have its correct fs_devices in both the cases.
2245 */
2246 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002247
Stefan Behrense93c89c2012-11-05 17:33:06 +01002248 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002249 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002250 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002251 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002252 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002253
Anand Jainebbede42017-12-04 12:54:52 +08002254 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002255 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002256
Miao Xie82372bc2014-09-03 21:35:44 +08002257 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002258 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002259}
2260
David Sterba65237ee2019-03-20 16:34:54 +01002261void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev)
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002262{
David Sterba65237ee2019-03-20 16:34:54 +01002263 struct btrfs_fs_info *fs_info = srcdev->fs_info;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002264 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002265
Anand Jainebbede42017-12-04 12:54:52 +08002266 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002267 /* zero out the old super if it is writable */
2268 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2269 }
Anand Jain14238812016-07-22 06:04:53 +08002270
2271 btrfs_close_bdev(srcdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002272 synchronize_rcu();
2273 btrfs_free_device(srcdev);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002274
Anand Jain94d5f0c2014-08-13 14:24:22 +08002275 /* if this is no devs we rather delete the fs_devices */
2276 if (!fs_devices->num_devices) {
2277 struct btrfs_fs_devices *tmp_fs_devices;
2278
Anand Jain6dd38f82017-10-17 06:53:50 +08002279 /*
2280 * On a mounted FS, num_devices can't be zero unless it's a
2281 * seed. In case of a seed device being replaced, the replace
2282 * target added to the sprout FS, so there will be no more
2283 * device left under the seed FS.
2284 */
2285 ASSERT(fs_devices->seeding);
2286
Anand Jain94d5f0c2014-08-13 14:24:22 +08002287 tmp_fs_devices = fs_info->fs_devices;
2288 while (tmp_fs_devices) {
2289 if (tmp_fs_devices->seed == fs_devices) {
2290 tmp_fs_devices->seed = fs_devices->seed;
2291 break;
2292 }
2293 tmp_fs_devices = tmp_fs_devices->seed;
2294 }
2295 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002296 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002297 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002298 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002299}
2300
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002301void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002302{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002303 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002304
Anand Jaind9a071f2018-04-12 10:29:38 +08002305 WARN_ON(!tgtdev);
2306 mutex_lock(&fs_devices->device_list_mutex);
2307
2308 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002309
Anand Jain779bf3fe2016-04-18 16:51:23 +08002310 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002311 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002312
Anand Jaind9a071f2018-04-12 10:29:38 +08002313 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002314
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002315 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002316
Stefan Behrense93c89c2012-11-05 17:33:06 +01002317 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002318
Anand Jaind9a071f2018-04-12 10:29:38 +08002319 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002320
2321 /*
2322 * The update_dev_time() with in btrfs_scratch_superblocks()
2323 * may lead to a call to btrfs_show_devname() which will try
2324 * to hold device_list_mutex. And here this device
2325 * is already out of device list, so we don't have to hold
2326 * the device_list_mutex lock.
2327 */
2328 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002329
2330 btrfs_close_bdev(tgtdev);
Nikolay Borisov8e75fd82019-03-27 14:24:11 +02002331 synchronize_rcu();
2332 btrfs_free_device(tgtdev);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002333}
2334
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002335static struct btrfs_device *btrfs_find_device_by_path(
2336 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002337{
2338 int ret = 0;
2339 struct btrfs_super_block *disk_super;
2340 u64 devid;
2341 u8 *dev_uuid;
2342 struct block_device *bdev;
2343 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002344 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002345
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002346 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002347 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002348 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002349 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002350 disk_super = (struct btrfs_super_block *)bh->b_data;
2351 devid = btrfs_stack_device_id(&disk_super->dev_item);
2352 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002353 if (btrfs_fs_incompat(fs_info, METADATA_UUID))
Anand Jaine4319cd2019-01-17 23:32:31 +08002354 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002355 disk_super->metadata_uuid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002356 else
Anand Jaine4319cd2019-01-17 23:32:31 +08002357 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002358 disk_super->fsid, true);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002359
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002360 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002361 if (!device)
2362 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002363 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002364 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002365}
2366
Yan Zheng2b820322008-11-17 21:11:30 -05002367/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002368 * Lookup a device given by device id, or the path if the id is 0.
2369 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002370struct btrfs_device *btrfs_find_device_by_devspec(
Anand Jain6e927ce2019-01-17 23:32:29 +08002371 struct btrfs_fs_info *fs_info, u64 devid,
2372 const char *device_path)
Anand Jain24e04742016-02-13 10:01:35 +08002373{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002374 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002375
David Sterba5c5c0df2016-02-15 16:39:55 +01002376 if (devid) {
Anand Jaine4319cd2019-01-17 23:32:31 +08002377 device = btrfs_find_device(fs_info->fs_devices, devid, NULL,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002378 NULL, true);
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002379 if (!device)
2380 return ERR_PTR(-ENOENT);
Anand Jain6e927ce2019-01-17 23:32:29 +08002381 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002382 }
Anand Jain6e927ce2019-01-17 23:32:29 +08002383
2384 if (!device_path || !device_path[0])
2385 return ERR_PTR(-EINVAL);
2386
2387 if (strcmp(device_path, "missing") == 0) {
2388 /* Find first missing device */
2389 list_for_each_entry(device, &fs_info->fs_devices->devices,
2390 dev_list) {
2391 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2392 &device->dev_state) && !device->bdev)
2393 return device;
2394 }
2395 return ERR_PTR(-ENOENT);
2396 }
2397
2398 return btrfs_find_device_by_path(fs_info, device_path);
Anand Jain24e04742016-02-13 10:01:35 +08002399}
2400
Yan Zheng2b820322008-11-17 21:11:30 -05002401/*
2402 * does all the dirty work required for changing file system's UUID.
2403 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002404static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002405{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002406 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002407 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002408 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002410 struct btrfs_device *device;
2411 u64 super_flags;
2412
David Sterbaa32bf9a2018-03-16 02:21:22 +01002413 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002414 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002415 return -EINVAL;
2416
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002417 seed_devices = alloc_fs_devices(NULL, NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002418 if (IS_ERR(seed_devices))
2419 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002420
Yan Zhenge4404d62008-12-12 10:03:26 -05002421 old_devices = clone_fs_devices(fs_devices);
2422 if (IS_ERR(old_devices)) {
2423 kfree(seed_devices);
2424 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002425 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002426
Anand Jainc4babc52018-04-12 10:29:25 +08002427 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002428
Yan Zhenge4404d62008-12-12 10:03:26 -05002429 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2430 seed_devices->opened = 1;
2431 INIT_LIST_HEAD(&seed_devices->devices);
2432 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002433 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002434
Anand Jain321a4bf2018-07-16 22:58:09 +08002435 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002436 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2437 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002438 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002439 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002440
David Sterba34441362016-10-04 19:34:27 +02002441 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002442 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002443 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002444
Yan Zheng2b820322008-11-17 21:11:30 -05002445 fs_devices->seeding = 0;
2446 fs_devices->num_devices = 0;
2447 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002448 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002449 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002450 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002451
2452 generate_random_uuid(fs_devices->fsid);
Nikolay Borisov7239ff42018-10-30 16:43:23 +02002453 memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002454 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002455 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002456
Yan Zheng2b820322008-11-17 21:11:30 -05002457 super_flags = btrfs_super_flags(disk_super) &
2458 ~BTRFS_SUPER_FLAG_SEEDING;
2459 btrfs_set_super_flags(disk_super, super_flags);
2460
2461 return 0;
2462}
2463
2464/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002465 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002466 */
David Sterba5c4666292019-03-20 16:36:39 +01002467static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05002468{
David Sterba5c4666292019-03-20 16:36:39 +01002469 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002470 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002471 struct btrfs_path *path;
2472 struct extent_buffer *leaf;
2473 struct btrfs_dev_item *dev_item;
2474 struct btrfs_device *device;
2475 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002476 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002477 u8 dev_uuid[BTRFS_UUID_SIZE];
2478 u64 devid;
2479 int ret;
2480
2481 path = btrfs_alloc_path();
2482 if (!path)
2483 return -ENOMEM;
2484
Yan Zheng2b820322008-11-17 21:11:30 -05002485 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2486 key.offset = 0;
2487 key.type = BTRFS_DEV_ITEM_KEY;
2488
2489 while (1) {
2490 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2491 if (ret < 0)
2492 goto error;
2493
2494 leaf = path->nodes[0];
2495next_slot:
2496 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2497 ret = btrfs_next_leaf(root, path);
2498 if (ret > 0)
2499 break;
2500 if (ret < 0)
2501 goto error;
2502 leaf = path->nodes[0];
2503 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002504 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002505 continue;
2506 }
2507
2508 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2509 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2510 key.type != BTRFS_DEV_ITEM_KEY)
2511 break;
2512
2513 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2514 struct btrfs_dev_item);
2515 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002516 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002517 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002518 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002519 BTRFS_FSID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08002520 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08002521 fs_uuid, true);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002522 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002523
2524 if (device->fs_devices->seeding) {
2525 btrfs_set_device_generation(leaf, dev_item,
2526 device->generation);
2527 btrfs_mark_buffer_dirty(leaf);
2528 }
2529
2530 path->slots[0]++;
2531 goto next_slot;
2532 }
2533 ret = 0;
2534error:
2535 btrfs_free_path(path);
2536 return ret;
2537}
2538
David Sterbada353f62017-02-14 17:55:53 +01002539int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002540{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002541 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002542 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002543 struct btrfs_trans_handle *trans;
2544 struct btrfs_device *device;
2545 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002546 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002547 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002548 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002549 u64 orig_super_total_bytes;
2550 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002551 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002552 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002553 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002554
Anand Jain5da54bc2018-07-03 13:14:50 +08002555 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002556 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002557
Li Zefana5d16332011-12-07 20:08:40 -05002558 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002559 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002560 if (IS_ERR(bdev))
2561 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002562
Anand Jain5da54bc2018-07-03 13:14:50 +08002563 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002564 seeding_dev = 1;
2565 down_write(&sb->s_umount);
2566 mutex_lock(&uuid_mutex);
2567 }
2568
Chris Mason8c8bee12008-09-29 11:19:10 -04002569 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002570
Anand Jain5da54bc2018-07-03 13:14:50 +08002571 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002572 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002573 if (device->bdev == bdev) {
2574 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002575 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002576 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002577 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002578 }
2579 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002580 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002581
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002583 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002584 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002585 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002586 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002587 }
2588
David Sterba78f2c9e2016-02-11 14:25:38 +01002589 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002590 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002591 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002592 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002593 }
Josef Bacik606686e2012-06-04 14:03:51 -04002594 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002595
Yan, Zhenga22285a2010-05-16 10:48:46 -04002596 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002597 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002598 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002599 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002600 }
2601
Josef Bacikd5e20032011-08-04 14:52:27 +00002602 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002603 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002604 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002605 device->io_width = fs_info->sectorsize;
2606 device->io_align = fs_info->sectorsize;
2607 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002608 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2609 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002610 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002611 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002612 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002613 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002614 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002615 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002616 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002617 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002618 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002619
Yan Zheng2b820322008-11-17 21:11:30 -05002620 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002621 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002622 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002623 if (ret) {
2624 btrfs_abort_transaction(trans, ret);
2625 goto error_trans;
2626 }
Yan Zheng2b820322008-11-17 21:11:30 -05002627 }
2628
Anand Jain5da54bc2018-07-03 13:14:50 +08002629 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002630
Anand Jain5da54bc2018-07-03 13:14:50 +08002631 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002632 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002633 list_add_rcu(&device->dev_list, &fs_devices->devices);
2634 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2635 fs_devices->num_devices++;
2636 fs_devices->open_devices++;
2637 fs_devices->rw_devices++;
2638 fs_devices->total_devices++;
2639 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002640
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002641 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002642
Anand Jaine884f4f2017-04-04 18:40:19 +08002643 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002644 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002645
Naohiro Aota39379fa2018-07-27 09:04:55 +09002646 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002647 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002648 round_down(orig_super_total_bytes + device->total_bytes,
2649 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002650
Naohiro Aota39379fa2018-07-27 09:04:55 +09002651 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2652 btrfs_set_super_num_devices(fs_info->super_copy,
2653 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002654
2655 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002656 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002657
Miao Xie2196d6e2014-09-03 21:35:41 +08002658 /*
2659 * we've got more storage, clear any full flags on the space
2660 * infos
2661 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002662 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002663
David Sterba34441362016-10-04 19:34:27 +02002664 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002665 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002666
Yan Zheng2b820322008-11-17 21:11:30 -05002667 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002668 mutex_lock(&fs_info->chunk_mutex);
David Sterba6f8e0fc2019-03-20 16:29:13 +01002669 ret = init_first_rw_device(trans);
David Sterba34441362016-10-04 19:34:27 +02002670 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002672 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002673 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002674 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002675 }
2676
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002677 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002678 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002679 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002680 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002681 }
2682
2683 if (seeding_dev) {
2684 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2685
David Sterba5c4666292019-03-20 16:36:39 +01002686 ret = btrfs_finish_sprout(trans);
David Sterba005d6422012-09-18 07:52:32 -06002687 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002688 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002689 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002690 }
Anand Jainb2373f22014-06-03 11:36:03 +08002691
2692 /* Sprouting would change fsid of the mounted root,
2693 * so rename the fsid on the sysfs
2694 */
2695 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Nikolay Borisovde37aa52018-10-30 16:43:24 +02002696 fs_info->fs_devices->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002697 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002698 btrfs_warn(fs_info,
2699 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002700 }
2701
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002702 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002703
2704 if (seeding_dev) {
2705 mutex_unlock(&uuid_mutex);
2706 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002707 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002708
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002709 if (ret) /* transaction commit */
2710 return ret;
2711
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002712 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002713 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002714 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002715 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002716 trans = btrfs_attach_transaction(root);
2717 if (IS_ERR(trans)) {
2718 if (PTR_ERR(trans) == -ENOENT)
2719 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002720 ret = PTR_ERR(trans);
2721 trans = NULL;
2722 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002723 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002724 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002725 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002726
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002727 /* Update ctime/mtime for libblkid */
2728 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002729 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002730
Anand Jaind31c32f2017-09-28 14:51:10 +08002731error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002732 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002733 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2734 mutex_lock(&fs_info->chunk_mutex);
2735 list_del_rcu(&device->dev_list);
2736 list_del(&device->dev_alloc_list);
2737 fs_info->fs_devices->num_devices--;
2738 fs_info->fs_devices->open_devices--;
2739 fs_info->fs_devices->rw_devices--;
2740 fs_info->fs_devices->total_devices--;
2741 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2742 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2743 btrfs_set_super_total_bytes(fs_info->super_copy,
2744 orig_super_total_bytes);
2745 btrfs_set_super_num_devices(fs_info->super_copy,
2746 orig_super_num_devices);
2747 mutex_unlock(&fs_info->chunk_mutex);
2748 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002749error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002750 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002751 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002752 if (trans)
2753 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002754error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002755 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002756error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002757 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002758 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002759 mutex_unlock(&uuid_mutex);
2760 up_write(&sb->s_umount);
2761 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002762 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002763}
2764
Chris Masond3977122009-01-05 21:25:51 -05002765static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2766 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002767{
2768 int ret;
2769 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002770 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002771 struct btrfs_dev_item *dev_item;
2772 struct extent_buffer *leaf;
2773 struct btrfs_key key;
2774
Chris Mason0b86a832008-03-24 15:01:56 -04002775 path = btrfs_alloc_path();
2776 if (!path)
2777 return -ENOMEM;
2778
2779 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2780 key.type = BTRFS_DEV_ITEM_KEY;
2781 key.offset = device->devid;
2782
2783 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2784 if (ret < 0)
2785 goto out;
2786
2787 if (ret > 0) {
2788 ret = -ENOENT;
2789 goto out;
2790 }
2791
2792 leaf = path->nodes[0];
2793 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2794
2795 btrfs_set_device_id(leaf, dev_item, device->devid);
2796 btrfs_set_device_type(leaf, dev_item, device->type);
2797 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2798 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2799 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002800 btrfs_set_device_total_bytes(leaf, dev_item,
2801 btrfs_device_get_disk_total_bytes(device));
2802 btrfs_set_device_bytes_used(leaf, dev_item,
2803 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002804 btrfs_mark_buffer_dirty(leaf);
2805
2806out:
2807 btrfs_free_path(path);
2808 return ret;
2809}
2810
Miao Xie2196d6e2014-09-03 21:35:41 +08002811int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002812 struct btrfs_device *device, u64 new_size)
2813{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002814 struct btrfs_fs_info *fs_info = device->fs_info;
2815 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie2196d6e2014-09-03 21:35:41 +08002816 u64 old_total;
2817 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002818
Anand Jainebbede42017-12-04 12:54:52 +08002819 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002820 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002821
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002822 new_size = round_down(new_size, fs_info->sectorsize);
2823
David Sterba34441362016-10-04 19:34:27 +02002824 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002825 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002826 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002827
Stefan Behrens63a212a2012-11-05 18:29:28 +01002828 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002829 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002830 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002831 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002832 }
Yan Zheng2b820322008-11-17 21:11:30 -05002833
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002834 btrfs_set_super_total_bytes(super_copy,
2835 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002836 device->fs_devices->total_rw_bytes += diff;
2837
Miao Xie7cc8e582014-09-03 21:35:38 +08002838 btrfs_device_set_total_bytes(device, new_size);
2839 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002840 btrfs_clear_space_info_full(device->fs_info);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02002841 if (list_empty(&device->post_commit_list))
2842 list_add_tail(&device->post_commit_list,
2843 &trans->transaction->dev_update_list);
David Sterba34441362016-10-04 19:34:27 +02002844 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002845
Chris Mason8f18cf12008-04-25 16:53:30 -04002846 return btrfs_update_device(trans, device);
2847}
2848
Nikolay Borisovf4208792018-07-20 19:37:52 +03002849static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002850{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002851 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002852 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002853 int ret;
2854 struct btrfs_path *path;
2855 struct btrfs_key key;
2856
Chris Mason8f18cf12008-04-25 16:53:30 -04002857 path = btrfs_alloc_path();
2858 if (!path)
2859 return -ENOMEM;
2860
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002861 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002862 key.offset = chunk_offset;
2863 key.type = BTRFS_CHUNK_ITEM_KEY;
2864
2865 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002866 if (ret < 0)
2867 goto out;
2868 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002869 btrfs_handle_fs_error(fs_info, -ENOENT,
2870 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002871 ret = -ENOENT;
2872 goto out;
2873 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002874
2875 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002876 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002877 btrfs_handle_fs_error(fs_info, ret,
2878 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002879out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002880 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002881 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002882}
2883
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002884static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002885{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002886 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002887 struct btrfs_disk_key *disk_key;
2888 struct btrfs_chunk *chunk;
2889 u8 *ptr;
2890 int ret = 0;
2891 u32 num_stripes;
2892 u32 array_size;
2893 u32 len = 0;
2894 u32 cur;
2895 struct btrfs_key key;
2896
David Sterba34441362016-10-04 19:34:27 +02002897 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002898 array_size = btrfs_super_sys_array_size(super_copy);
2899
2900 ptr = super_copy->sys_chunk_array;
2901 cur = 0;
2902
2903 while (cur < array_size) {
2904 disk_key = (struct btrfs_disk_key *)ptr;
2905 btrfs_disk_key_to_cpu(&key, disk_key);
2906
2907 len = sizeof(*disk_key);
2908
2909 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2910 chunk = (struct btrfs_chunk *)(ptr + len);
2911 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2912 len += btrfs_chunk_item_size(num_stripes);
2913 } else {
2914 ret = -EIO;
2915 break;
2916 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002917 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002918 key.offset == chunk_offset) {
2919 memmove(ptr, ptr + len, array_size - (cur + len));
2920 array_size -= len;
2921 btrfs_set_super_sys_array_size(super_copy, array_size);
2922 } else {
2923 ptr += len;
2924 cur += len;
2925 }
2926 }
David Sterba34441362016-10-04 19:34:27 +02002927 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002928 return ret;
2929}
2930
Omar Sandoval60ca8422018-05-16 16:34:31 -07002931/*
2932 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2933 * @logical: Logical block offset in bytes.
2934 * @length: Length of extent in bytes.
2935 *
2936 * Return: Chunk mapping or ERR_PTR.
2937 */
2938struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2939 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002940{
2941 struct extent_map_tree *em_tree;
2942 struct extent_map *em;
2943
David Sterbac8bf1b62019-05-17 11:43:17 +02002944 em_tree = &fs_info->mapping_tree;
Liu Bo592d92e2017-03-14 13:33:55 -07002945 read_lock(&em_tree->lock);
2946 em = lookup_extent_mapping(em_tree, logical, length);
2947 read_unlock(&em_tree->lock);
2948
2949 if (!em) {
2950 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2951 logical, length);
2952 return ERR_PTR(-EINVAL);
2953 }
2954
2955 if (em->start > logical || em->start + em->len < logical) {
2956 btrfs_crit(fs_info,
2957 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2958 logical, length, em->start, em->start + em->len);
2959 free_extent_map(em);
2960 return ERR_PTR(-EINVAL);
2961 }
2962
2963 /* callers are responsible for dropping em's ref. */
2964 return em;
2965}
2966
Nikolay Borisov97aff912018-07-20 19:37:53 +03002967int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002968{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002969 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002970 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002971 struct map_lookup *map;
2972 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002973 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002974 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002975
Omar Sandoval60ca8422018-05-16 16:34:31 -07002976 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002977 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002978 /*
2979 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002980 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002981 * do anything we still error out.
2982 */
2983 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002984 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002985 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002986 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002987 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002988 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002989 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002990
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002991 /*
2992 * Take the device list mutex to prevent races with the final phase of
2993 * a device replace operation that replaces the device object associated
2994 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2995 */
2996 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002997 for (i = 0; i < map->num_stripes; i++) {
2998 struct btrfs_device *device = map->stripes[i].dev;
2999 ret = btrfs_free_dev_extent(trans, device,
3000 map->stripes[i].physical,
3001 &dev_extent_len);
3002 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003003 mutex_unlock(&fs_devices->device_list_mutex);
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
3008 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02003009 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003010 btrfs_device_set_bytes_used(device,
3011 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03003012 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003013 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02003014 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003015 }
3016
Nikolay Borisov64bc6c22018-10-26 14:43:19 +03003017 ret = btrfs_update_device(trans, device);
3018 if (ret) {
3019 mutex_unlock(&fs_devices->device_list_mutex);
3020 btrfs_abort_transaction(trans, ret);
3021 goto out;
Josef Bacik47ab2a62014-09-18 11:20:02 -04003022 }
3023 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01003024 mutex_unlock(&fs_devices->device_list_mutex);
3025
Nikolay Borisovf4208792018-07-20 19:37:52 +03003026 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003027 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003028 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003029 goto out;
3030 }
3031
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003032 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003033
3034 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03003035 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003036 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003037 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003038 goto out;
3039 }
3040 }
3041
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03003042 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003043 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003044 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04003045 goto out;
3046 }
3047
Josef Bacik47ab2a62014-09-18 11:20:02 -04003048out:
3049 /* once for us */
3050 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04003051 return ret;
3052}
3053
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003054static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04003055{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003056 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07003057 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04003058 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003059
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003060 /*
3061 * Prevent races with automatic removal of unused block groups.
3062 * After we relocate and before we remove the chunk with offset
3063 * chunk_offset, automatic removal of the block group can kick in,
3064 * resulting in a failure when calling btrfs_remove_chunk() below.
3065 *
3066 * Make sure to acquire this mutex before doing a tree search (dev
3067 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
3068 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
3069 * we release the path used to search the chunk/dev tree and before
3070 * the current task acquires this mutex and calls us.
3071 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01003072 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003074 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003075 if (ret)
3076 return -ENOSPC;
3077
Chris Mason8f18cf12008-04-25 16:53:30 -04003078 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003079 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003081 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003082 if (ret)
3083 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003084
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003085 /*
3086 * We add the kobjects here (and after forcing data chunk creation)
3087 * since relocation is the only place we'll create chunks of a new
3088 * type at runtime. The only place where we'll remove the last
3089 * chunk of a type is the call immediately below this one. Even
3090 * so, we're protected against races with the cleaner thread since
3091 * we're covered by the delete_unused_bgs_mutex.
3092 */
3093 btrfs_add_raid_kobjects(fs_info);
3094
Chris Mason19c4d2f2016-10-10 13:43:31 -07003095 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3096 chunk_offset);
3097 if (IS_ERR(trans)) {
3098 ret = PTR_ERR(trans);
3099 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3100 return ret;
3101 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003102
Chris Mason19c4d2f2016-10-10 13:43:31 -07003103 /*
3104 * step two, delete the device extents and the
3105 * chunk tree entries
3106 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03003107 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003108 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003109 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003110}
3111
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003112static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003113{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003114 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003115 struct btrfs_path *path;
3116 struct extent_buffer *leaf;
3117 struct btrfs_chunk *chunk;
3118 struct btrfs_key key;
3119 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003120 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003121 bool retried = false;
3122 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003123 int ret;
3124
3125 path = btrfs_alloc_path();
3126 if (!path)
3127 return -ENOMEM;
3128
Josef Bacikba1bf482009-09-11 16:11:19 -04003129again:
Yan Zheng2b820322008-11-17 21:11:30 -05003130 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3131 key.offset = (u64)-1;
3132 key.type = BTRFS_CHUNK_ITEM_KEY;
3133
3134 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003135 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003136 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003137 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003139 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003140 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003141 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003142
3143 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3144 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003145 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003146 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003147 if (ret < 0)
3148 goto error;
3149 if (ret > 0)
3150 break;
3151
3152 leaf = path->nodes[0];
3153 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3154
3155 chunk = btrfs_item_ptr(leaf, path->slots[0],
3156 struct btrfs_chunk);
3157 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003158 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003159
3160 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003161 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003162 if (ret == -ENOSPC)
3163 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303164 else
3165 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003166 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003167 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003168
3169 if (found_key.offset == 0)
3170 break;
3171 key.offset = found_key.offset - 1;
3172 }
3173 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003174 if (failed && !retried) {
3175 failed = 0;
3176 retried = true;
3177 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303178 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003179 ret = -ENOSPC;
3180 }
Yan Zheng2b820322008-11-17 21:11:30 -05003181error:
3182 btrfs_free_path(path);
3183 return ret;
3184}
3185
Liu Boa6f93c72017-11-15 16:28:11 -07003186/*
3187 * return 1 : allocate a data chunk successfully,
3188 * return <0: errors during allocating a data chunk,
3189 * return 0 : no need to allocate a data chunk.
3190 */
3191static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3192 u64 chunk_offset)
3193{
3194 struct btrfs_block_group_cache *cache;
3195 u64 bytes_used;
3196 u64 chunk_type;
3197
3198 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3199 ASSERT(cache);
3200 chunk_type = cache->flags;
3201 btrfs_put_block_group(cache);
3202
3203 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3204 spin_lock(&fs_info->data_sinfo->lock);
3205 bytes_used = fs_info->data_sinfo->bytes_used;
3206 spin_unlock(&fs_info->data_sinfo->lock);
3207
3208 if (!bytes_used) {
3209 struct btrfs_trans_handle *trans;
3210 int ret;
3211
3212 trans = btrfs_join_transaction(fs_info->tree_root);
3213 if (IS_ERR(trans))
3214 return PTR_ERR(trans);
3215
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003216 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003217 BTRFS_BLOCK_GROUP_DATA);
3218 btrfs_end_transaction(trans);
3219 if (ret < 0)
3220 return ret;
3221
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003222 btrfs_add_raid_kobjects(fs_info);
3223
Liu Boa6f93c72017-11-15 16:28:11 -07003224 return 1;
3225 }
3226 }
3227 return 0;
3228}
3229
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003230static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003231 struct btrfs_balance_control *bctl)
3232{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003233 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003234 struct btrfs_trans_handle *trans;
3235 struct btrfs_balance_item *item;
3236 struct btrfs_disk_balance_args disk_bargs;
3237 struct btrfs_path *path;
3238 struct extent_buffer *leaf;
3239 struct btrfs_key key;
3240 int ret, err;
3241
3242 path = btrfs_alloc_path();
3243 if (!path)
3244 return -ENOMEM;
3245
3246 trans = btrfs_start_transaction(root, 0);
3247 if (IS_ERR(trans)) {
3248 btrfs_free_path(path);
3249 return PTR_ERR(trans);
3250 }
3251
3252 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003253 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003254 key.offset = 0;
3255
3256 ret = btrfs_insert_empty_item(trans, root, path, &key,
3257 sizeof(*item));
3258 if (ret)
3259 goto out;
3260
3261 leaf = path->nodes[0];
3262 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3263
David Sterbab159fa22016-11-08 18:09:03 +01003264 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003265
3266 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3267 btrfs_set_balance_data(leaf, item, &disk_bargs);
3268 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3269 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3270 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3271 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3272
3273 btrfs_set_balance_flags(leaf, item, bctl->flags);
3274
3275 btrfs_mark_buffer_dirty(leaf);
3276out:
3277 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003278 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003279 if (err && !ret)
3280 ret = err;
3281 return ret;
3282}
3283
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003284static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003285{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003286 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003287 struct btrfs_trans_handle *trans;
3288 struct btrfs_path *path;
3289 struct btrfs_key key;
3290 int ret, err;
3291
3292 path = btrfs_alloc_path();
3293 if (!path)
3294 return -ENOMEM;
3295
3296 trans = btrfs_start_transaction(root, 0);
3297 if (IS_ERR(trans)) {
3298 btrfs_free_path(path);
3299 return PTR_ERR(trans);
3300 }
3301
3302 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003303 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003304 key.offset = 0;
3305
3306 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3307 if (ret < 0)
3308 goto out;
3309 if (ret > 0) {
3310 ret = -ENOENT;
3311 goto out;
3312 }
3313
3314 ret = btrfs_del_item(trans, root, path);
3315out:
3316 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003317 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003318 if (err && !ret)
3319 ret = err;
3320 return ret;
3321}
3322
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003323/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003324 * This is a heuristic used to reduce the number of chunks balanced on
3325 * resume after balance was interrupted.
3326 */
3327static void update_balance_args(struct btrfs_balance_control *bctl)
3328{
3329 /*
3330 * Turn on soft mode for chunk types that were being converted.
3331 */
3332 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3333 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3334 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3335 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3336 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3337 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3338
3339 /*
3340 * Turn on usage filter if is not already used. The idea is
3341 * that chunks that we have already balanced should be
3342 * reasonably full. Don't do it for chunks that are being
3343 * converted - that will keep us from relocating unconverted
3344 * (albeit full) chunks.
3345 */
3346 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003347 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003348 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3349 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3350 bctl->data.usage = 90;
3351 }
3352 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003353 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003354 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3355 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3356 bctl->sys.usage = 90;
3357 }
3358 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003359 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003360 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3361 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3362 bctl->meta.usage = 90;
3363 }
3364}
3365
3366/*
David Sterba149196a2018-03-20 20:23:09 +01003367 * Clear the balance status in fs_info and delete the balance item from disk.
3368 */
3369static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003370{
3371 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003372 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003373
3374 BUG_ON(!fs_info->balance_ctl);
3375
3376 spin_lock(&fs_info->balance_lock);
3377 fs_info->balance_ctl = NULL;
3378 spin_unlock(&fs_info->balance_lock);
3379
3380 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003381 ret = del_balance_item(fs_info);
3382 if (ret)
3383 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003384}
3385
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003386/*
3387 * Balance filters. Return 1 if chunk should be filtered out
3388 * (should not be balanced).
3389 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003390static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003391 struct btrfs_balance_args *bargs)
3392{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003393 chunk_type = chunk_to_extended(chunk_type) &
3394 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003395
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003396 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003397 return 0;
3398
3399 return 1;
3400}
3401
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003402static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003403 struct btrfs_balance_args *bargs)
3404{
3405 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003406 u64 chunk_used;
3407 u64 user_thresh_min;
3408 u64 user_thresh_max;
3409 int ret = 1;
3410
3411 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3412 chunk_used = btrfs_block_group_used(&cache->item);
3413
3414 if (bargs->usage_min == 0)
3415 user_thresh_min = 0;
3416 else
3417 user_thresh_min = div_factor_fine(cache->key.offset,
3418 bargs->usage_min);
3419
3420 if (bargs->usage_max == 0)
3421 user_thresh_max = 1;
3422 else if (bargs->usage_max > 100)
3423 user_thresh_max = cache->key.offset;
3424 else
3425 user_thresh_max = div_factor_fine(cache->key.offset,
3426 bargs->usage_max);
3427
3428 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3429 ret = 0;
3430
3431 btrfs_put_block_group(cache);
3432 return ret;
3433}
3434
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003435static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003436 u64 chunk_offset, struct btrfs_balance_args *bargs)
3437{
3438 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003439 u64 chunk_used, user_thresh;
3440 int ret = 1;
3441
3442 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3443 chunk_used = btrfs_block_group_used(&cache->item);
3444
David Sterbabc309462015-10-20 18:22:13 +02003445 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003446 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003447 else if (bargs->usage > 100)
3448 user_thresh = cache->key.offset;
3449 else
3450 user_thresh = div_factor_fine(cache->key.offset,
3451 bargs->usage);
3452
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003453 if (chunk_used < user_thresh)
3454 ret = 0;
3455
3456 btrfs_put_block_group(cache);
3457 return ret;
3458}
3459
Ilya Dryomov409d4042012-01-16 22:04:47 +02003460static int chunk_devid_filter(struct extent_buffer *leaf,
3461 struct btrfs_chunk *chunk,
3462 struct btrfs_balance_args *bargs)
3463{
3464 struct btrfs_stripe *stripe;
3465 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3466 int i;
3467
3468 for (i = 0; i < num_stripes; i++) {
3469 stripe = btrfs_stripe_nr(chunk, i);
3470 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3471 return 0;
3472 }
3473
3474 return 1;
3475}
3476
David Sterba946c9252019-05-17 11:43:34 +02003477static u64 calc_data_stripes(u64 type, int num_stripes)
3478{
3479 const int index = btrfs_bg_flags_to_raid_index(type);
3480 const int ncopies = btrfs_raid_array[index].ncopies;
3481 const int nparity = btrfs_raid_array[index].nparity;
3482
3483 if (nparity)
3484 return num_stripes - nparity;
3485 else
3486 return num_stripes / ncopies;
3487}
3488
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003489/* [pstart, pend) */
3490static int chunk_drange_filter(struct extent_buffer *leaf,
3491 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003492 struct btrfs_balance_args *bargs)
3493{
3494 struct btrfs_stripe *stripe;
3495 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3496 u64 stripe_offset;
3497 u64 stripe_length;
David Sterba946c9252019-05-17 11:43:34 +02003498 u64 type;
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003499 int factor;
3500 int i;
3501
3502 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3503 return 0;
3504
David Sterba946c9252019-05-17 11:43:34 +02003505 type = btrfs_chunk_type(leaf, chunk);
3506 factor = calc_data_stripes(type, num_stripes);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003507
3508 for (i = 0; i < num_stripes; i++) {
3509 stripe = btrfs_stripe_nr(chunk, i);
3510 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3511 continue;
3512
3513 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3514 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003515 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003516
3517 if (stripe_offset < bargs->pend &&
3518 stripe_offset + stripe_length > bargs->pstart)
3519 return 0;
3520 }
3521
3522 return 1;
3523}
3524
Ilya Dryomovea671762012-01-16 22:04:48 +02003525/* [vstart, vend) */
3526static int chunk_vrange_filter(struct extent_buffer *leaf,
3527 struct btrfs_chunk *chunk,
3528 u64 chunk_offset,
3529 struct btrfs_balance_args *bargs)
3530{
3531 if (chunk_offset < bargs->vend &&
3532 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3533 /* at least part of the chunk is inside this vrange */
3534 return 0;
3535
3536 return 1;
3537}
3538
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003539static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3540 struct btrfs_chunk *chunk,
3541 struct btrfs_balance_args *bargs)
3542{
3543 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3544
3545 if (bargs->stripes_min <= num_stripes
3546 && num_stripes <= bargs->stripes_max)
3547 return 0;
3548
3549 return 1;
3550}
3551
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003552static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003553 struct btrfs_balance_args *bargs)
3554{
3555 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3556 return 0;
3557
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003558 chunk_type = chunk_to_extended(chunk_type) &
3559 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003560
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003561 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003562 return 1;
3563
3564 return 0;
3565}
3566
David Sterba6ec08962019-03-20 16:38:52 +01003567static int should_balance_chunk(struct extent_buffer *leaf,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003568 struct btrfs_chunk *chunk, u64 chunk_offset)
3569{
David Sterba6ec08962019-03-20 16:38:52 +01003570 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003571 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003572 struct btrfs_balance_args *bargs = NULL;
3573 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3574
3575 /* type filter */
3576 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3577 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3578 return 0;
3579 }
3580
3581 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3582 bargs = &bctl->data;
3583 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3584 bargs = &bctl->sys;
3585 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3586 bargs = &bctl->meta;
3587
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003588 /* profiles filter */
3589 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3590 chunk_profiles_filter(chunk_type, bargs)) {
3591 return 0;
3592 }
3593
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003594 /* usage filter */
3595 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003596 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003597 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003598 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003599 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003600 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003601 }
3602
Ilya Dryomov409d4042012-01-16 22:04:47 +02003603 /* devid filter */
3604 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3605 chunk_devid_filter(leaf, chunk, bargs)) {
3606 return 0;
3607 }
3608
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003609 /* drange filter, makes sense only with devid filter */
3610 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003611 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003612 return 0;
3613 }
3614
Ilya Dryomovea671762012-01-16 22:04:48 +02003615 /* vrange filter */
3616 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3617 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3618 return 0;
3619 }
3620
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003621 /* stripes filter */
3622 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3623 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3624 return 0;
3625 }
3626
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003627 /* soft profile changing mode */
3628 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3629 chunk_soft_convert_filter(chunk_type, bargs)) {
3630 return 0;
3631 }
3632
David Sterba7d824b62014-05-07 17:37:51 +02003633 /*
3634 * limited by count, must be the last filter
3635 */
3636 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3637 if (bargs->limit == 0)
3638 return 0;
3639 else
3640 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003641 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3642 /*
3643 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003644 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003645 * about the count of all chunks that satisfy the filters.
3646 */
3647 if (bargs->limit_max == 0)
3648 return 0;
3649 else
3650 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003651 }
3652
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003653 return 1;
3654}
3655
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003656static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003657{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003658 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003659 struct btrfs_root *chunk_root = fs_info->chunk_root;
David Sterba12907fc2015-10-10 17:16:50 +02003660 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003661 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003662 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003663 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003664 struct btrfs_key found_key;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003665 struct extent_buffer *leaf;
3666 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003667 int ret;
3668 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003669 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003670 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003671 u64 limit_data = bctl->data.limit;
3672 u64 limit_meta = bctl->meta.limit;
3673 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003674 u32 count_data = 0;
3675 u32 count_meta = 0;
3676 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003677 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003678
Chris Masonec44a352008-04-28 15:29:52 -04003679 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003680 if (!path) {
3681 ret = -ENOMEM;
3682 goto error;
3683 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003684
3685 /* zero out stat counters */
3686 spin_lock(&fs_info->balance_lock);
3687 memset(&bctl->stat, 0, sizeof(bctl->stat));
3688 spin_unlock(&fs_info->balance_lock);
3689again:
David Sterba7d824b62014-05-07 17:37:51 +02003690 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003691 /*
3692 * The single value limit and min/max limits use the same bytes
3693 * in the
3694 */
David Sterba7d824b62014-05-07 17:37:51 +02003695 bctl->data.limit = limit_data;
3696 bctl->meta.limit = limit_meta;
3697 bctl->sys.limit = limit_sys;
3698 }
Chris Masonec44a352008-04-28 15:29:52 -04003699 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3700 key.offset = (u64)-1;
3701 key.type = BTRFS_CHUNK_ITEM_KEY;
3702
Chris Masond3977122009-01-05 21:25:51 -05003703 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003704 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003705 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003706 ret = -ECANCELED;
3707 goto error;
3708 }
3709
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003710 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003711 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003712 if (ret < 0) {
3713 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003714 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003715 }
Chris Masonec44a352008-04-28 15:29:52 -04003716
3717 /*
3718 * this shouldn't happen, it means the last relocate
3719 * failed
3720 */
3721 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003722 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003723
3724 ret = btrfs_previous_item(chunk_root, path, 0,
3725 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003726 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003727 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003728 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003729 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003730 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003731
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003732 leaf = path->nodes[0];
3733 slot = path->slots[0];
3734 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3735
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003736 if (found_key.objectid != key.objectid) {
3737 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003738 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003739 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003740
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003741 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003742 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003743
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003744 if (!counting) {
3745 spin_lock(&fs_info->balance_lock);
3746 bctl->stat.considered++;
3747 spin_unlock(&fs_info->balance_lock);
3748 }
3749
David Sterba6ec08962019-03-20 16:38:52 +01003750 ret = should_balance_chunk(leaf, chunk, found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003751
David Sterbab3b4aa72011-04-21 01:20:15 +02003752 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003753 if (!ret) {
3754 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003755 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003756 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003757
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003758 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003759 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003760 spin_lock(&fs_info->balance_lock);
3761 bctl->stat.expected++;
3762 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003763
3764 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3765 count_data++;
3766 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3767 count_sys++;
3768 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3769 count_meta++;
3770
3771 goto loop;
3772 }
3773
3774 /*
3775 * Apply limit_min filter, no need to check if the LIMITS
3776 * filter is used, limit_min is 0 by default
3777 */
3778 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3779 count_data < bctl->data.limit_min)
3780 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3781 count_meta < bctl->meta.limit_min)
3782 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3783 count_sys < bctl->sys.limit_min)) {
3784 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003785 goto loop;
3786 }
3787
Liu Boa6f93c72017-11-15 16:28:11 -07003788 if (!chunk_reserved) {
3789 /*
3790 * We may be relocating the only data chunk we have,
3791 * which could potentially end up with losing data's
3792 * raid profile, so lets allocate an empty one in
3793 * advance.
3794 */
3795 ret = btrfs_may_alloc_data_chunk(fs_info,
3796 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003797 if (ret < 0) {
3798 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3799 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003800 } else if (ret == 1) {
3801 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003802 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003803 }
3804
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003805 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003806 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003807 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003808 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003809 } else if (ret == -ETXTBSY) {
3810 btrfs_info(fs_info,
3811 "skipping relocation of block group %llu due to active swapfile",
3812 found_key.offset);
3813 ret = 0;
3814 } else if (ret) {
3815 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003816 } else {
3817 spin_lock(&fs_info->balance_lock);
3818 bctl->stat.completed++;
3819 spin_unlock(&fs_info->balance_lock);
3820 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003821loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003822 if (found_key.offset == 0)
3823 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003824 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003825 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003826
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003827 if (counting) {
3828 btrfs_release_path(path);
3829 counting = false;
3830 goto again;
3831 }
Chris Masonec44a352008-04-28 15:29:52 -04003832error:
3833 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003834 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003835 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003836 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003837 if (!ret)
3838 ret = -ENOSPC;
3839 }
3840
Chris Masonec44a352008-04-28 15:29:52 -04003841 return ret;
3842}
3843
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003844/**
3845 * alloc_profile_is_valid - see if a given profile is valid and reduced
3846 * @flags: profile to validate
3847 * @extended: if true @flags is treated as an extended profile
3848 */
3849static int alloc_profile_is_valid(u64 flags, int extended)
3850{
3851 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3852 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3853
3854 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3855
3856 /* 1) check that all other bits are zeroed */
3857 if (flags & ~mask)
3858 return 0;
3859
3860 /* 2) see if profile is reduced */
3861 if (flags == 0)
3862 return !extended; /* "0" is valid for usual profiles */
3863
3864 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003865 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003866}
3867
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003868static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3869{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003870 /* cancel requested || normal exit path */
3871 return atomic_read(&fs_info->balance_cancel_req) ||
3872 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3873 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003874}
3875
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003876/* Non-zero return value signifies invalidity */
3877static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3878 u64 allowed)
3879{
3880 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3881 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3882 (bctl_arg->target & ~allowed)));
3883}
3884
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003885/*
Anand Jain56fc37d2018-11-20 16:12:56 +08003886 * Fill @buf with textual description of balance filter flags @bargs, up to
3887 * @size_buf including the terminating null. The output may be trimmed if it
3888 * does not fit into the provided buffer.
3889 */
3890static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
3891 u32 size_buf)
3892{
3893 int ret;
3894 u32 size_bp = size_buf;
3895 char *bp = buf;
3896 u64 flags = bargs->flags;
3897 char tmp_buf[128] = {'\0'};
3898
3899 if (!flags)
3900 return;
3901
3902#define CHECK_APPEND_NOARG(a) \
3903 do { \
3904 ret = snprintf(bp, size_bp, (a)); \
3905 if (ret < 0 || ret >= size_bp) \
3906 goto out_overflow; \
3907 size_bp -= ret; \
3908 bp += ret; \
3909 } while (0)
3910
3911#define CHECK_APPEND_1ARG(a, v1) \
3912 do { \
3913 ret = snprintf(bp, size_bp, (a), (v1)); \
3914 if (ret < 0 || ret >= size_bp) \
3915 goto out_overflow; \
3916 size_bp -= ret; \
3917 bp += ret; \
3918 } while (0)
3919
3920#define CHECK_APPEND_2ARG(a, v1, v2) \
3921 do { \
3922 ret = snprintf(bp, size_bp, (a), (v1), (v2)); \
3923 if (ret < 0 || ret >= size_bp) \
3924 goto out_overflow; \
3925 size_bp -= ret; \
3926 bp += ret; \
3927 } while (0)
3928
3929 if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
3930 int index = btrfs_bg_flags_to_raid_index(bargs->target);
3931
3932 CHECK_APPEND_1ARG("convert=%s,", get_raid_name(index));
3933 }
3934
3935 if (flags & BTRFS_BALANCE_ARGS_SOFT)
3936 CHECK_APPEND_NOARG("soft,");
3937
3938 if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
3939 btrfs_describe_block_groups(bargs->profiles, tmp_buf,
3940 sizeof(tmp_buf));
3941 CHECK_APPEND_1ARG("profiles=%s,", tmp_buf);
3942 }
3943
3944 if (flags & BTRFS_BALANCE_ARGS_USAGE)
3945 CHECK_APPEND_1ARG("usage=%llu,", bargs->usage);
3946
3947 if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE)
3948 CHECK_APPEND_2ARG("usage=%u..%u,",
3949 bargs->usage_min, bargs->usage_max);
3950
3951 if (flags & BTRFS_BALANCE_ARGS_DEVID)
3952 CHECK_APPEND_1ARG("devid=%llu,", bargs->devid);
3953
3954 if (flags & BTRFS_BALANCE_ARGS_DRANGE)
3955 CHECK_APPEND_2ARG("drange=%llu..%llu,",
3956 bargs->pstart, bargs->pend);
3957
3958 if (flags & BTRFS_BALANCE_ARGS_VRANGE)
3959 CHECK_APPEND_2ARG("vrange=%llu..%llu,",
3960 bargs->vstart, bargs->vend);
3961
3962 if (flags & BTRFS_BALANCE_ARGS_LIMIT)
3963 CHECK_APPEND_1ARG("limit=%llu,", bargs->limit);
3964
3965 if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)
3966 CHECK_APPEND_2ARG("limit=%u..%u,",
3967 bargs->limit_min, bargs->limit_max);
3968
3969 if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE)
3970 CHECK_APPEND_2ARG("stripes=%u..%u,",
3971 bargs->stripes_min, bargs->stripes_max);
3972
3973#undef CHECK_APPEND_2ARG
3974#undef CHECK_APPEND_1ARG
3975#undef CHECK_APPEND_NOARG
3976
3977out_overflow:
3978
3979 if (size_bp < size_buf)
3980 buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
3981 else
3982 buf[0] = '\0';
3983}
3984
3985static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
3986{
3987 u32 size_buf = 1024;
3988 char tmp_buf[192] = {'\0'};
3989 char *buf;
3990 char *bp;
3991 u32 size_bp = size_buf;
3992 int ret;
3993 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3994
3995 buf = kzalloc(size_buf, GFP_KERNEL);
3996 if (!buf)
3997 return;
3998
3999 bp = buf;
4000
4001#define CHECK_APPEND_1ARG(a, v1) \
4002 do { \
4003 ret = snprintf(bp, size_bp, (a), (v1)); \
4004 if (ret < 0 || ret >= size_bp) \
4005 goto out_overflow; \
4006 size_bp -= ret; \
4007 bp += ret; \
4008 } while (0)
4009
4010 if (bctl->flags & BTRFS_BALANCE_FORCE)
4011 CHECK_APPEND_1ARG("%s", "-f ");
4012
4013 if (bctl->flags & BTRFS_BALANCE_DATA) {
4014 describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
4015 CHECK_APPEND_1ARG("-d%s ", tmp_buf);
4016 }
4017
4018 if (bctl->flags & BTRFS_BALANCE_METADATA) {
4019 describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
4020 CHECK_APPEND_1ARG("-m%s ", tmp_buf);
4021 }
4022
4023 if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
4024 describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
4025 CHECK_APPEND_1ARG("-s%s ", tmp_buf);
4026 }
4027
4028#undef CHECK_APPEND_1ARG
4029
4030out_overflow:
4031
4032 if (size_bp < size_buf)
4033 buf[size_buf - size_bp - 1] = '\0'; /* remove last " " */
4034 btrfs_info(fs_info, "balance: %s %s",
4035 (bctl->flags & BTRFS_BALANCE_RESUME) ?
4036 "resume" : "start", buf);
4037
4038 kfree(buf);
4039}
4040
4041/*
David Sterbadccdb072018-03-21 00:20:05 +01004042 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004043 */
David Sterba6fcf6e22018-05-07 17:44:03 +02004044int btrfs_balance(struct btrfs_fs_info *fs_info,
4045 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004046 struct btrfs_ioctl_balance_args *bargs)
4047{
Adam Borowski14506122017-03-07 23:34:44 +01004048 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004049 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03004050 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004051 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01004052 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00004053 unsigned seq;
Filipe Manana5a8067c2018-11-19 09:48:12 +00004054 bool reducing_integrity;
David Sterba081db892019-05-17 11:43:27 +02004055 int i;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004056
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004057 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004058 atomic_read(&fs_info->balance_pause_req) ||
4059 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004060 ret = -EINVAL;
4061 goto out;
4062 }
4063
Ilya Dryomove4837f82012-03-27 17:09:17 +03004064 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
4065 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
4066 mixed = 1;
4067
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004068 /*
4069 * In case of mixed groups both data and meta should be picked,
4070 * and identical options should be given for both of them.
4071 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03004072 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
4073 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004074 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
4075 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
4076 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004077 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004078 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02004079 ret = -EINVAL;
4080 goto out;
4081 }
4082 }
4083
Anand Jain1da73962018-08-10 13:53:21 +08004084 num_devices = btrfs_num_devices(fs_info);
David Sterba081db892019-05-17 11:43:27 +02004085 allowed = 0;
4086 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++)
4087 if (num_devices >= btrfs_raid_array[i].devs_min)
4088 allowed |= btrfs_raid_array[i].bg_flag;
Anand Jain1da73962018-08-10 13:53:21 +08004089
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004090 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004091 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
4092
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004093 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004094 "balance: invalid convert data profile %s",
4095 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004096 ret = -EINVAL;
4097 goto out;
4098 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004099 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004100 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
4101
Frank Holtonefe120a2013-12-20 11:37:06 -05004102 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004103 "balance: invalid convert metadata profile %s",
4104 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004105 ret = -EINVAL;
4106 goto out;
4107 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00004108 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004109 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
4110
Frank Holtonefe120a2013-12-20 11:37:06 -05004111 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004112 "balance: invalid convert system profile %s",
4113 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004114 ret = -EINVAL;
4115 goto out;
4116 }
4117
David Sterba6079e122019-05-17 11:43:29 +02004118 /*
4119 * Allow to reduce metadata or system integrity only if force set for
4120 * profiles with redundancy (copies, parity)
4121 */
4122 allowed = 0;
4123 for (i = 0; i < ARRAY_SIZE(btrfs_raid_array); i++) {
4124 if (btrfs_raid_array[i].ncopies >= 2 ||
4125 btrfs_raid_array[i].tolerated_failures >= 1)
4126 allowed |= btrfs_raid_array[i].bg_flag;
4127 }
Miao Xiede98ced2013-01-29 10:13:12 +00004128 do {
4129 seq = read_seqbegin(&fs_info->profiles_lock);
4130
4131 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4132 (fs_info->avail_system_alloc_bits & allowed) &&
4133 !(bctl->sys.target & allowed)) ||
4134 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
4135 (fs_info->avail_metadata_alloc_bits & allowed) &&
Filipe Manana5a8067c2018-11-19 09:48:12 +00004136 !(bctl->meta.target & allowed)))
4137 reducing_integrity = true;
4138 else
4139 reducing_integrity = false;
4140
4141 /* if we're not converting, the target field is uninitialized */
4142 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4143 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
4144 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
4145 bctl->data.target : fs_info->avail_data_alloc_bits;
Miao Xiede98ced2013-01-29 10:13:12 +00004146 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02004147
Filipe Manana5a8067c2018-11-19 09:48:12 +00004148 if (reducing_integrity) {
4149 if (bctl->flags & BTRFS_BALANCE_FORCE) {
4150 btrfs_info(fs_info,
4151 "balance: force reducing metadata integrity");
4152 } else {
4153 btrfs_err(fs_info,
4154 "balance: reduces metadata integrity, use --force if you want this");
4155 ret = -EINVAL;
4156 goto out;
4157 }
4158 }
4159
Adam Borowski14506122017-03-07 23:34:44 +01004160 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
4161 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004162 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
4163 int data_index = btrfs_bg_flags_to_raid_index(data_target);
4164
Sam Tygieree592d02016-01-06 08:46:12 +00004165 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004166 "balance: metadata profile %s has lower redundancy than data profile %s",
4167 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00004168 }
4169
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004170 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02004171 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02004172 goto out;
4173
Ilya Dryomov59641012012-01-16 22:04:48 +02004174 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
4175 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01004176 BUG_ON(fs_info->balance_ctl);
4177 spin_lock(&fs_info->balance_lock);
4178 fs_info->balance_ctl = bctl;
4179 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02004180 } else {
4181 BUG_ON(ret != -EEXIST);
4182 spin_lock(&fs_info->balance_lock);
4183 update_balance_args(bctl);
4184 spin_unlock(&fs_info->balance_lock);
4185 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004186
David Sterba3009a622018-03-21 01:31:04 +01004187 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
4188 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Anand Jain56fc37d2018-11-20 16:12:56 +08004189 describe_balance_start_or_resume(fs_info);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004190 mutex_unlock(&fs_info->balance_mutex);
4191
4192 ret = __btrfs_balance(fs_info);
4193
4194 mutex_lock(&fs_info->balance_mutex);
Anand Jain7333bd02018-11-20 16:12:57 +08004195 if (ret == -ECANCELED && atomic_read(&fs_info->balance_pause_req))
4196 btrfs_info(fs_info, "balance: paused");
4197 else if (ret == -ECANCELED && atomic_read(&fs_info->balance_cancel_req))
4198 btrfs_info(fs_info, "balance: canceled");
4199 else
4200 btrfs_info(fs_info, "balance: ended with status: %d", ret);
4201
David Sterba3009a622018-03-21 01:31:04 +01004202 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004203
4204 if (bargs) {
4205 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01004206 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004207 }
4208
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004209 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4210 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01004211 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004212 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004213 }
4214
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004215 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004216
4217 return ret;
4218out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004219 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01004220 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004221 else
Ilya Dryomov59641012012-01-16 22:04:48 +02004222 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01004223 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4224
Ilya Dryomov59641012012-01-16 22:04:48 +02004225 return ret;
4226}
4227
4228static int balance_kthread(void *data)
4229{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004230 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004231 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004232
Ilya Dryomov59641012012-01-16 22:04:48 +02004233 mutex_lock(&fs_info->balance_mutex);
Anand Jain56fc37d2018-11-20 16:12:56 +08004234 if (fs_info->balance_ctl)
David Sterba6fcf6e22018-05-07 17:44:03 +02004235 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov59641012012-01-16 22:04:48 +02004236 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004237
Ilya Dryomov59641012012-01-16 22:04:48 +02004238 return ret;
4239}
4240
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004241int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4242{
4243 struct task_struct *tsk;
4244
David Sterba1354e1a2018-03-21 02:29:13 +01004245 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004246 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01004247 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004248 return 0;
4249 }
David Sterba1354e1a2018-03-21 02:29:13 +01004250 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004251
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004252 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08004253 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004254 return 0;
4255 }
4256
Anand Jain02ee6542018-05-17 15:16:51 +08004257 /*
4258 * A ro->rw remount sequence should continue with the paused balance
4259 * regardless of who pauses it, system or the user as of now, so set
4260 * the resume flag.
4261 */
4262 spin_lock(&fs_info->balance_lock);
4263 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
4264 spin_unlock(&fs_info->balance_lock);
4265
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004266 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304267 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004268}
4269
Ilya Dryomov68310a52012-06-22 12:24:12 -06004270int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004271{
Ilya Dryomov59641012012-01-16 22:04:48 +02004272 struct btrfs_balance_control *bctl;
4273 struct btrfs_balance_item *item;
4274 struct btrfs_disk_balance_args disk_bargs;
4275 struct btrfs_path *path;
4276 struct extent_buffer *leaf;
4277 struct btrfs_key key;
4278 int ret;
4279
4280 path = btrfs_alloc_path();
4281 if (!path)
4282 return -ENOMEM;
4283
Ilya Dryomov68310a52012-06-22 12:24:12 -06004284 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004285 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004286 key.offset = 0;
4287
4288 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4289 if (ret < 0)
4290 goto out;
4291 if (ret > 0) { /* ret = -ENOENT; */
4292 ret = 0;
4293 goto out;
4294 }
4295
Ilya Dryomov59641012012-01-16 22:04:48 +02004296 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4297 if (!bctl) {
4298 ret = -ENOMEM;
4299 goto out;
4300 }
4301
Ilya Dryomov59641012012-01-16 22:04:48 +02004302 leaf = path->nodes[0];
4303 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4304
Ilya Dryomov68310a52012-06-22 12:24:12 -06004305 bctl->flags = btrfs_balance_flags(leaf, item);
4306 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004307
4308 btrfs_balance_data(leaf, item, &disk_bargs);
4309 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4310 btrfs_balance_meta(leaf, item, &disk_bargs);
4311 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4312 btrfs_balance_sys(leaf, item, &disk_bargs);
4313 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4314
David Sterbaeee95e32018-03-20 20:07:58 +01004315 /*
4316 * This should never happen, as the paused balance state is recovered
4317 * during mount without any chance of other exclusive ops to collide.
4318 *
4319 * This gives the exclusive op status to balance and keeps in paused
4320 * state until user intervention (cancel or umount). If the ownership
4321 * cannot be assigned, show a message but do not fail. The balance
4322 * is in a paused state and must have fs_info::balance_ctl properly
4323 * set up.
4324 */
4325 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4326 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004327 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004328
Ilya Dryomov68310a52012-06-22 12:24:12 -06004329 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004330 BUG_ON(fs_info->balance_ctl);
4331 spin_lock(&fs_info->balance_lock);
4332 fs_info->balance_ctl = bctl;
4333 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004334 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004335out:
4336 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004337 return ret;
4338}
4339
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004340int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4341{
4342 int ret = 0;
4343
4344 mutex_lock(&fs_info->balance_mutex);
4345 if (!fs_info->balance_ctl) {
4346 mutex_unlock(&fs_info->balance_mutex);
4347 return -ENOTCONN;
4348 }
4349
David Sterba3009a622018-03-21 01:31:04 +01004350 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004351 atomic_inc(&fs_info->balance_pause_req);
4352 mutex_unlock(&fs_info->balance_mutex);
4353
4354 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004355 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004356
4357 mutex_lock(&fs_info->balance_mutex);
4358 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004359 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004360 atomic_dec(&fs_info->balance_pause_req);
4361 } else {
4362 ret = -ENOTCONN;
4363 }
4364
4365 mutex_unlock(&fs_info->balance_mutex);
4366 return ret;
4367}
4368
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004369int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4370{
4371 mutex_lock(&fs_info->balance_mutex);
4372 if (!fs_info->balance_ctl) {
4373 mutex_unlock(&fs_info->balance_mutex);
4374 return -ENOTCONN;
4375 }
4376
David Sterbacf7d20f2018-03-21 01:45:32 +01004377 /*
4378 * A paused balance with the item stored on disk can be resumed at
4379 * mount time if the mount is read-write. Otherwise it's still paused
4380 * and we must not allow cancelling as it deletes the item.
4381 */
4382 if (sb_rdonly(fs_info->sb)) {
4383 mutex_unlock(&fs_info->balance_mutex);
4384 return -EROFS;
4385 }
4386
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004387 atomic_inc(&fs_info->balance_cancel_req);
4388 /*
4389 * if we are running just wait and return, balance item is
4390 * deleted in btrfs_balance in this case
4391 */
David Sterba3009a622018-03-21 01:31:04 +01004392 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004393 mutex_unlock(&fs_info->balance_mutex);
4394 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004395 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004396 mutex_lock(&fs_info->balance_mutex);
4397 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004398 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004399 /*
4400 * Lock released to allow other waiters to continue, we'll
4401 * reexamine the status again.
4402 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004403 mutex_lock(&fs_info->balance_mutex);
4404
David Sterbaa17c95d2018-03-20 17:28:05 +01004405 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004406 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004407 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004408 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004409 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004410 }
4411
David Sterba3009a622018-03-21 01:31:04 +01004412 BUG_ON(fs_info->balance_ctl ||
4413 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004414 atomic_dec(&fs_info->balance_cancel_req);
4415 mutex_unlock(&fs_info->balance_mutex);
4416 return 0;
4417}
4418
Stefan Behrens803b2f52013-08-15 17:11:21 +02004419static int btrfs_uuid_scan_kthread(void *data)
4420{
4421 struct btrfs_fs_info *fs_info = data;
4422 struct btrfs_root *root = fs_info->tree_root;
4423 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004424 struct btrfs_path *path = NULL;
4425 int ret = 0;
4426 struct extent_buffer *eb;
4427 int slot;
4428 struct btrfs_root_item root_item;
4429 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004430 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004431
4432 path = btrfs_alloc_path();
4433 if (!path) {
4434 ret = -ENOMEM;
4435 goto out;
4436 }
4437
4438 key.objectid = 0;
4439 key.type = BTRFS_ROOT_ITEM_KEY;
4440 key.offset = 0;
4441
Stefan Behrens803b2f52013-08-15 17:11:21 +02004442 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004443 ret = btrfs_search_forward(root, &key, path,
4444 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004445 if (ret) {
4446 if (ret > 0)
4447 ret = 0;
4448 break;
4449 }
4450
4451 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4452 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4453 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4454 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4455 goto skip;
4456
4457 eb = path->nodes[0];
4458 slot = path->slots[0];
4459 item_size = btrfs_item_size_nr(eb, slot);
4460 if (item_size < sizeof(root_item))
4461 goto skip;
4462
Stefan Behrens803b2f52013-08-15 17:11:21 +02004463 read_extent_buffer(eb, &root_item,
4464 btrfs_item_ptr_offset(eb, slot),
4465 (int)sizeof(root_item));
4466 if (btrfs_root_refs(&root_item) == 0)
4467 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004468
4469 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4470 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4471 if (trans)
4472 goto update_tree;
4473
4474 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004475 /*
4476 * 1 - subvol uuid item
4477 * 1 - received_subvol uuid item
4478 */
4479 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4480 if (IS_ERR(trans)) {
4481 ret = PTR_ERR(trans);
4482 break;
4483 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004484 continue;
4485 } else {
4486 goto skip;
4487 }
4488update_tree:
4489 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004490 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004491 BTRFS_UUID_KEY_SUBVOL,
4492 key.objectid);
4493 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004494 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004495 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004496 break;
4497 }
4498 }
4499
4500 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004501 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004502 root_item.received_uuid,
4503 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4504 key.objectid);
4505 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004506 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004507 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004508 break;
4509 }
4510 }
4511
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004512skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004513 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004514 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004515 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004516 if (ret)
4517 break;
4518 }
4519
Stefan Behrens803b2f52013-08-15 17:11:21 +02004520 btrfs_release_path(path);
4521 if (key.offset < (u64)-1) {
4522 key.offset++;
4523 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4524 key.offset = 0;
4525 key.type = BTRFS_ROOT_ITEM_KEY;
4526 } else if (key.objectid < (u64)-1) {
4527 key.offset = 0;
4528 key.type = BTRFS_ROOT_ITEM_KEY;
4529 key.objectid++;
4530 } else {
4531 break;
4532 }
4533 cond_resched();
4534 }
4535
4536out:
4537 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004538 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004539 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004540 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004541 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004542 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004543 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004544 up(&fs_info->uuid_tree_rescan_sem);
4545 return 0;
4546}
4547
Stefan Behrens70f80172013-08-15 17:11:23 +02004548/*
4549 * Callback for btrfs_uuid_tree_iterate().
4550 * returns:
4551 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004552 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004553 * > 0 if the check failed, which means the caller shall remove the entry.
4554 */
4555static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4556 u8 *uuid, u8 type, u64 subid)
4557{
4558 struct btrfs_key key;
4559 int ret = 0;
4560 struct btrfs_root *subvol_root;
4561
4562 if (type != BTRFS_UUID_KEY_SUBVOL &&
4563 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4564 goto out;
4565
4566 key.objectid = subid;
4567 key.type = BTRFS_ROOT_ITEM_KEY;
4568 key.offset = (u64)-1;
4569 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4570 if (IS_ERR(subvol_root)) {
4571 ret = PTR_ERR(subvol_root);
4572 if (ret == -ENOENT)
4573 ret = 1;
4574 goto out;
4575 }
4576
4577 switch (type) {
4578 case BTRFS_UUID_KEY_SUBVOL:
4579 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4580 ret = 1;
4581 break;
4582 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4583 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4584 BTRFS_UUID_SIZE))
4585 ret = 1;
4586 break;
4587 }
4588
4589out:
4590 return ret;
4591}
4592
4593static int btrfs_uuid_rescan_kthread(void *data)
4594{
4595 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4596 int ret;
4597
4598 /*
4599 * 1st step is to iterate through the existing UUID tree and
4600 * to delete all entries that contain outdated data.
4601 * 2nd step is to add all missing entries to the UUID tree.
4602 */
4603 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4604 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004605 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004606 up(&fs_info->uuid_tree_rescan_sem);
4607 return ret;
4608 }
4609 return btrfs_uuid_scan_kthread(data);
4610}
4611
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004612int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4613{
4614 struct btrfs_trans_handle *trans;
4615 struct btrfs_root *tree_root = fs_info->tree_root;
4616 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004617 struct task_struct *task;
4618 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004619
4620 /*
4621 * 1 - root node
4622 * 1 - root item
4623 */
4624 trans = btrfs_start_transaction(tree_root, 2);
4625 if (IS_ERR(trans))
4626 return PTR_ERR(trans);
4627
David Sterba9b7a2442019-03-20 13:20:49 +01004628 uuid_root = btrfs_create_tree(trans, BTRFS_UUID_TREE_OBJECTID);
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004629 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004630 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004631 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004632 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004633 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004634 }
4635
4636 fs_info->uuid_root = uuid_root;
4637
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004638 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004639 if (ret)
4640 return ret;
4641
4642 down(&fs_info->uuid_tree_rescan_sem);
4643 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4644 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004645 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004646 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004647 up(&fs_info->uuid_tree_rescan_sem);
4648 return PTR_ERR(task);
4649 }
4650
4651 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004652}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004653
Stefan Behrens70f80172013-08-15 17:11:23 +02004654int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4655{
4656 struct task_struct *task;
4657
4658 down(&fs_info->uuid_tree_rescan_sem);
4659 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4660 if (IS_ERR(task)) {
4661 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004662 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004663 up(&fs_info->uuid_tree_rescan_sem);
4664 return PTR_ERR(task);
4665 }
4666
4667 return 0;
4668}
4669
Chris Mason8f18cf12008-04-25 16:53:30 -04004670/*
4671 * shrinking a device means finding all of the device extents past
4672 * the new size, and then following the back refs to the chunks.
4673 * The chunk relocation code actually frees the device extent
4674 */
4675int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4676{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004677 struct btrfs_fs_info *fs_info = device->fs_info;
4678 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004679 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004680 struct btrfs_dev_extent *dev_extent = NULL;
4681 struct btrfs_path *path;
4682 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004683 u64 chunk_offset;
4684 int ret;
4685 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004686 int failed = 0;
4687 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004688 struct extent_buffer *l;
4689 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004690 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004691 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004692 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004693 u64 diff;
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004694 u64 start;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004695
4696 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004697 start = new_size;
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004698 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004699
Anand Jain401e29c2017-12-04 12:54:55 +08004700 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004701 return -EINVAL;
4702
Chris Mason8f18cf12008-04-25 16:53:30 -04004703 path = btrfs_alloc_path();
4704 if (!path)
4705 return -ENOMEM;
4706
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004707 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004708
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004709 trans = btrfs_start_transaction(root, 0);
4710 if (IS_ERR(trans)) {
4711 btrfs_free_path(path);
4712 return PTR_ERR(trans);
4713 }
4714
David Sterba34441362016-10-04 19:34:27 +02004715 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004716
Miao Xie7cc8e582014-09-03 21:35:38 +08004717 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004718 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004719 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004720 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004721 }
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004722
4723 /*
4724 * Once the device's size has been set to the new size, ensure all
4725 * in-memory chunks are synced to disk so that the loop below sees them
4726 * and relocates them accordingly.
4727 */
Jeff Mahoney1c11b632019-03-27 14:24:12 +02004728 if (contains_pending_extent(device, &start, diff)) {
Nikolay Borisov61d0d0d2019-03-25 14:31:23 +02004729 mutex_unlock(&fs_info->chunk_mutex);
4730 ret = btrfs_commit_transaction(trans);
4731 if (ret)
4732 goto done;
4733 } else {
4734 mutex_unlock(&fs_info->chunk_mutex);
4735 btrfs_end_transaction(trans);
4736 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004737
Josef Bacikba1bf482009-09-11 16:11:19 -04004738again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004739 key.objectid = device->devid;
4740 key.offset = (u64)-1;
4741 key.type = BTRFS_DEV_EXTENT_KEY;
4742
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004743 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004744 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004745 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004746 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004747 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004748 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004749 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004750
4751 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004752 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004753 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004754 if (ret < 0)
4755 goto done;
4756 if (ret) {
4757 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004758 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004759 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004760 }
4761
4762 l = path->nodes[0];
4763 slot = path->slots[0];
4764 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4765
Josef Bacikba1bf482009-09-11 16:11:19 -04004766 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004767 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004768 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004769 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004770 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004771
4772 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4773 length = btrfs_dev_extent_length(l, dev_extent);
4774
Josef Bacikba1bf482009-09-11 16:11:19 -04004775 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004776 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004777 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004778 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004779 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004780
Chris Mason8f18cf12008-04-25 16:53:30 -04004781 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004782 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004783
Liu Boa6f93c72017-11-15 16:28:11 -07004784 /*
4785 * We may be relocating the only data chunk we have,
4786 * which could potentially end up with losing data's
4787 * raid profile, so lets allocate an empty one in
4788 * advance.
4789 */
4790 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4791 if (ret < 0) {
4792 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4793 goto done;
4794 }
4795
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004796 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4797 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004798 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004799 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004800 } else if (ret) {
4801 if (ret == -ETXTBSY) {
4802 btrfs_warn(fs_info,
4803 "could not shrink block group %llu due to active swapfile",
4804 chunk_offset);
4805 }
4806 goto done;
4807 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004808 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004809
4810 if (failed && !retried) {
4811 failed = 0;
4812 retried = true;
4813 goto again;
4814 } else if (failed && retried) {
4815 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004816 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004817 }
4818
Chris Balld6397ba2009-04-27 07:29:03 -04004819 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004820 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004821 if (IS_ERR(trans)) {
4822 ret = PTR_ERR(trans);
4823 goto done;
4824 }
4825
David Sterba34441362016-10-04 19:34:27 +02004826 mutex_lock(&fs_info->chunk_mutex);
Miao Xie7cc8e582014-09-03 21:35:38 +08004827 btrfs_device_set_disk_total_bytes(device, new_size);
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02004828 if (list_empty(&device->post_commit_list))
4829 list_add_tail(&device->post_commit_list,
4830 &trans->transaction->dev_update_list);
Chris Balld6397ba2009-04-27 07:29:03 -04004831
Chris Balld6397ba2009-04-27 07:29:03 -04004832 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004833 btrfs_set_super_total_bytes(super_copy,
4834 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004835 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004836
4837 /* Now btrfs_update_device() will change the on-disk size. */
4838 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004839 if (ret < 0) {
4840 btrfs_abort_transaction(trans, ret);
4841 btrfs_end_transaction(trans);
4842 } else {
4843 ret = btrfs_commit_transaction(trans);
4844 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004845done:
4846 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004847 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004848 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004849 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004850 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004851 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004852 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004853 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004854 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004855 return ret;
4856}
4857
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004858static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004859 struct btrfs_key *key,
4860 struct btrfs_chunk *chunk, int item_size)
4861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004862 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004863 struct btrfs_disk_key disk_key;
4864 u32 array_size;
4865 u8 *ptr;
4866
David Sterba34441362016-10-04 19:34:27 +02004867 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004868 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004869 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004870 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004871 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004872 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004873 }
Chris Mason0b86a832008-03-24 15:01:56 -04004874
4875 ptr = super_copy->sys_chunk_array + array_size;
4876 btrfs_cpu_key_to_disk(&disk_key, key);
4877 memcpy(ptr, &disk_key, sizeof(disk_key));
4878 ptr += sizeof(disk_key);
4879 memcpy(ptr, chunk, item_size);
4880 item_size += sizeof(disk_key);
4881 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004882 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004883
Chris Mason0b86a832008-03-24 15:01:56 -04004884 return 0;
4885}
4886
Miao Xieb2117a32011-01-05 10:07:28 +00004887/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004888 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004889 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004890static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004891{
Arne Jansen73c5de02011-04-12 12:07:57 +02004892 const struct btrfs_device_info *di_a = a;
4893 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004894
Arne Jansen73c5de02011-04-12 12:07:57 +02004895 if (di_a->max_avail > di_b->max_avail)
4896 return -1;
4897 if (di_a->max_avail < di_b->max_avail)
4898 return 1;
4899 if (di_a->total_avail > di_b->total_avail)
4900 return -1;
4901 if (di_a->total_avail < di_b->total_avail)
4902 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004903 return 0;
4904}
4905
David Woodhouse53b381b2013-01-29 18:40:14 -05004906static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4907{
Zhao Leiffe2d202015-01-20 15:11:44 +08004908 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004909 return;
4910
Miao Xieceda0862013-04-11 10:30:16 +00004911 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004912}
4913
Miao Xieb2117a32011-01-05 10:07:28 +00004914static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004915 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004916{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004917 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004918 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004919 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004920 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004921 struct extent_map_tree *em_tree;
4922 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004923 struct btrfs_device_info *devices_info = NULL;
4924 u64 total_avail;
4925 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004926 int data_stripes; /* number of stripes that count for
4927 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004928 int sub_stripes; /* sub_stripes info for map */
4929 int dev_stripes; /* stripes per dev */
4930 int devs_max; /* max devs to use */
4931 int devs_min; /* min devs needed */
4932 int devs_increment; /* ndevs has to be a multiple of this */
4933 int ncopies; /* how many copies to data has */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004934 int nparity; /* number of stripes worth of bytes to
4935 store parity information */
Miao Xieb2117a32011-01-05 10:07:28 +00004936 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004937 u64 max_stripe_size;
4938 u64 max_chunk_size;
4939 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004940 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004941 int ndevs;
4942 int i;
4943 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004944 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004945
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004946 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004947
Qu Wenruo4117f202018-01-22 13:50:54 +08004948 if (list_empty(&fs_devices->alloc_list)) {
4949 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4950 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004951 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004952 }
Miao Xieb2117a32011-01-05 10:07:28 +00004953
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004954 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004955
Liu Bo31e50222012-11-21 14:18:10 +00004956 sub_stripes = btrfs_raid_array[index].sub_stripes;
4957 dev_stripes = btrfs_raid_array[index].dev_stripes;
4958 devs_max = btrfs_raid_array[index].devs_max;
David Sterbae3ecdb32019-05-17 11:43:38 +02004959 if (!devs_max)
4960 devs_max = BTRFS_MAX_DEVS(info);
Liu Bo31e50222012-11-21 14:18:10 +00004961 devs_min = btrfs_raid_array[index].devs_min;
4962 devs_increment = btrfs_raid_array[index].devs_increment;
4963 ncopies = btrfs_raid_array[index].ncopies;
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02004964 nparity = btrfs_raid_array[index].nparity;
Arne Jansen73c5de02011-04-12 12:07:57 +02004965
4966 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004967 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08004968 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Arne Jansen73c5de02011-04-12 12:07:57 +02004969 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004970 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004971 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4972 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004973 else
Byongho Leeee221842015-12-15 01:42:10 +09004974 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004975 max_chunk_size = max_stripe_size;
4976 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004977 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004978 max_chunk_size = 2 * max_stripe_size;
4979 } else {
David Sterba351fd352014-05-15 16:48:20 +02004980 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004981 type);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004982 BUG();
Arne Jansen73c5de02011-04-12 12:07:57 +02004983 }
4984
Andrea Gelmini52042d82018-11-28 12:05:13 +01004985 /* We don't want a chunk larger than 10% of writable space */
Arne Jansen73c5de02011-04-12 12:07:57 +02004986 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4987 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004988
David Sterba31e818f2015-02-20 18:00:26 +01004989 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004990 GFP_NOFS);
4991 if (!devices_info)
4992 return -ENOMEM;
4993
Arne Jansen73c5de02011-04-12 12:07:57 +02004994 /*
4995 * in the first pass through the devices list, we gather information
4996 * about the available holes on each device.
4997 */
4998 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004999 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005000 u64 max_avail;
5001 u64 dev_offset;
5002
Anand Jainebbede42017-12-04 12:54:52 +08005003 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00005004 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05005005 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02005006 continue;
5007 }
5008
Anand Jaine12c9622017-12-04 12:54:53 +08005009 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
5010 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08005011 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02005012 continue;
5013
5014 if (device->total_bytes > device->bytes_used)
5015 total_avail = device->total_bytes - device->bytes_used;
5016 else
5017 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00005018
5019 /* If there is no space on this device, skip it. */
5020 if (total_avail == 0)
5021 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02005022
Nikolay Borisov60dfdf22019-03-27 14:24:14 +02005023 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02005024 max_stripe_size * dev_stripes,
5025 &dev_offset, &max_avail);
5026 if (ret && ret != -ENOSPC)
5027 goto error;
5028
5029 if (ret == 0)
5030 max_avail = max_stripe_size * dev_stripes;
5031
Qu Wenruo4117f202018-01-22 13:50:54 +08005032 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
5033 if (btrfs_test_opt(info, ENOSPC_DEBUG))
5034 btrfs_debug(info,
5035 "%s: devid %llu has no free space, have=%llu want=%u",
5036 __func__, device->devid, max_avail,
5037 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005038 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08005039 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005040
Eric Sandeen063d0062013-01-31 00:55:01 +00005041 if (ndevs == fs_devices->rw_devices) {
5042 WARN(1, "%s: found more than %llu devices\n",
5043 __func__, fs_devices->rw_devices);
5044 break;
5045 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005046 devices_info[ndevs].dev_offset = dev_offset;
5047 devices_info[ndevs].max_avail = max_avail;
5048 devices_info[ndevs].total_avail = total_avail;
5049 devices_info[ndevs].dev = device;
5050 ++ndevs;
5051 }
5052
5053 /*
5054 * now sort the devices by hole size / available space
5055 */
5056 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
5057 btrfs_cmp_device_info, NULL);
5058
5059 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03005060 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02005061
Qu Wenruoba89b802018-01-31 13:56:15 +08005062 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02005063 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08005064 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
5065 btrfs_debug(info,
5066 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08005067 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08005068 }
Arne Jansen73c5de02011-04-12 12:07:57 +02005069 goto error;
5070 }
5071
Nikolay Borisovf148ef42017-06-27 10:02:26 +03005072 ndevs = min(ndevs, devs_max);
5073
Arne Jansen73c5de02011-04-12 12:07:57 +02005074 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005075 * The primary goal is to maximize the number of stripes, so use as
5076 * many devices as possible, even if the stripes are not maximum sized.
5077 *
5078 * The DUP profile stores more than one stripe per device, the
5079 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02005080 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01005081 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02005082 num_stripes = ndevs * dev_stripes;
5083
David Woodhouse53b381b2013-01-29 18:40:14 -05005084 /*
5085 * this will have to be fixed for RAID1 and RAID10 over
5086 * more drives
5087 */
Hans van Kranenburgb50836e2018-10-04 23:24:42 +02005088 data_stripes = (num_stripes - nparity) / ncopies;
Chris Mason86db2572013-02-20 16:23:40 -05005089
5090 /*
5091 * Use the number of data stripes to figure out how big this chunk
5092 * is really going to be in terms of logical address space,
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005093 * and compare that answer with the max chunk size. If it's higher,
5094 * we try to reduce stripe_size.
Chris Mason86db2572013-02-20 16:23:40 -05005095 */
5096 if (stripe_size * data_stripes > max_chunk_size) {
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005097 /*
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005098 * Reduce stripe_size, round it up to a 16MB boundary again and
5099 * then use it, unless it ends up being even bigger than the
5100 * previous value we had already.
Chris Mason86db2572013-02-20 16:23:40 -05005101 */
Hans van Kranenburgbaf92112018-10-04 23:24:40 +02005102 stripe_size = min(round_up(div_u64(max_chunk_size,
5103 data_stripes), SZ_16M),
Qu Wenruo793ff2c2018-01-31 14:16:34 +08005104 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05005105 }
5106
Ilya Dryomov37db63a2012-04-13 17:05:08 +03005107 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005108 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02005109
Miao Xieb2117a32011-01-05 10:07:28 +00005110 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
5111 if (!map) {
5112 ret = -ENOMEM;
5113 goto error;
5114 }
5115 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04005116
Arne Jansen73c5de02011-04-12 12:07:57 +02005117 for (i = 0; i < ndevs; ++i) {
5118 for (j = 0; j < dev_stripes; ++j) {
5119 int s = i * dev_stripes + j;
5120 map->stripes[s].dev = devices_info[i].dev;
5121 map->stripes[s].physical = devices_info[i].dev_offset +
5122 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04005123 }
Chris Mason6324fbf2008-03-24 15:01:59 -04005124 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03005125 map->stripe_len = BTRFS_STRIPE_LEN;
5126 map->io_align = BTRFS_STRIPE_LEN;
5127 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04005128 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04005129 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005130
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005131 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005132
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005133 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00005134
David Sterba172ddd62011-04-21 00:48:27 +02005135 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05005136 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08005137 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00005138 ret = -ENOMEM;
5139 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05005140 }
Wang Shilong298a8f92014-06-19 10:42:52 +08005141 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005142 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05005143 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005144 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005145 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005146 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04005147 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005148
David Sterbac8bf1b62019-05-17 11:43:17 +02005149 em_tree = &info->mapping_tree;
Chris Mason890871b2009-09-02 16:24:52 -04005150 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005151 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005152 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005153 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005154 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07005155 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005156 }
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03005157 write_unlock(&em_tree->lock);
5158
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02005159 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005160 if (ret)
5161 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05005162
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02005163 for (i = 0; i < map->num_stripes; i++) {
5164 struct btrfs_device *dev = map->stripes[i].dev;
5165
5166 btrfs_device_set_bytes_used(dev, dev->bytes_used + stripe_size);
5167 if (list_empty(&dev->post_commit_list))
5168 list_add_tail(&dev->post_commit_list,
5169 &trans->transaction->dev_update_list);
5170 }
Miao Xie43530c42014-09-03 21:35:36 +08005171
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03005172 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08005173
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005174 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005175 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05005176
Miao Xieb2117a32011-01-05 10:07:28 +00005177 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05005178 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00005179
Josef Bacik6df9a952013-06-27 13:22:46 -04005180error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05005181 write_lock(&em_tree->lock);
5182 remove_extent_mapping(em_tree, em);
5183 write_unlock(&em_tree->lock);
5184
5185 /* One for our allocation */
5186 free_extent_map(em);
5187 /* One for the tree reference */
5188 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00005189error:
Miao Xieb2117a32011-01-05 10:07:28 +00005190 kfree(devices_info);
5191 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005192}
5193
Josef Bacik6df9a952013-06-27 13:22:46 -04005194int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03005195 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05005196{
Nikolay Borisov97aff912018-07-20 19:37:53 +03005197 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04005198 struct btrfs_root *extent_root = fs_info->extent_root;
5199 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05005200 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05005201 struct btrfs_device *device;
5202 struct btrfs_chunk *chunk;
5203 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005204 struct extent_map *em;
5205 struct map_lookup *map;
5206 size_t item_size;
5207 u64 dev_offset;
5208 u64 stripe_size;
5209 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08005210 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005211
Omar Sandoval60ca8422018-05-16 16:34:31 -07005212 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07005213 if (IS_ERR(em))
5214 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005215
Jeff Mahoney95617d62015-06-03 10:55:48 -04005216 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005217 item_size = btrfs_chunk_item_size(map->num_stripes);
5218 stripe_size = em->orig_block_len;
5219
5220 chunk = kzalloc(item_size, GFP_NOFS);
5221 if (!chunk) {
5222 ret = -ENOMEM;
5223 goto out;
5224 }
5225
Filipe Manana50460e32015-11-20 10:42:47 +00005226 /*
5227 * Take the device list mutex to prevent races with the final phase of
5228 * a device replace operation that replaces the device object associated
5229 * with the map's stripes, because the device object's id can change
5230 * at any time during that final phase of the device replace operation
5231 * (dev-replace.c:btrfs_dev_replace_finishing()).
5232 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005233 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005234 for (i = 0; i < map->num_stripes; i++) {
5235 device = map->stripes[i].dev;
5236 dev_offset = map->stripes[i].physical;
5237
Yan Zheng2b820322008-11-17 21:11:30 -05005238 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005239 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005240 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005241 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5242 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005243 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005244 break;
5245 }
5246 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005247 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005248 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005249 }
5250
Yan Zheng2b820322008-11-17 21:11:30 -05005251 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005252 for (i = 0; i < map->num_stripes; i++) {
5253 device = map->stripes[i].dev;
5254 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005255
5256 btrfs_set_stack_stripe_devid(stripe, device->devid);
5257 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5258 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5259 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005260 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005261 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005262
5263 btrfs_set_stack_chunk_length(chunk, chunk_size);
5264 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5265 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5266 btrfs_set_stack_chunk_type(chunk, map->type);
5267 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5268 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5269 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005270 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005271 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5272
5273 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5274 key.type = BTRFS_CHUNK_ITEM_KEY;
5275 key.offset = chunk_offset;
5276
5277 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005278 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5279 /*
5280 * TODO: Cleanup of inserted chunk root in case of
5281 * failure.
5282 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005283 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005284 }
liubo1abe9b82011-03-24 11:18:59 +00005285
Josef Bacik6df9a952013-06-27 13:22:46 -04005286out:
Yan Zheng2b820322008-11-17 21:11:30 -05005287 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005288 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005289 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005290}
5291
5292/*
Andrea Gelmini52042d82018-11-28 12:05:13 +01005293 * Chunk allocation falls into two parts. The first part does work
5294 * that makes the new allocated chunk usable, but does not do any operation
5295 * that modifies the chunk tree. The second part does the work that
5296 * requires modifying the chunk tree. This division is important for the
Yan Zheng2b820322008-11-17 21:11:30 -05005297 * bootstrap process of adding storage to a seed btrfs.
5298 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005299int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005300{
5301 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005302
Nikolay Borisovc216b202018-06-20 15:49:06 +03005303 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5304 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005305 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005306}
5307
David Sterba6f8e0fc2019-03-20 16:29:13 +01005308static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
Yan Zheng2b820322008-11-17 21:11:30 -05005309{
David Sterba6f8e0fc2019-03-20 16:29:13 +01005310 struct btrfs_fs_info *fs_info = trans->fs_info;
Yan Zheng2b820322008-11-17 21:11:30 -05005311 u64 chunk_offset;
5312 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005313 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005314 int ret;
5315
Josef Bacik6df9a952013-06-27 13:22:46 -04005316 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005317 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005318 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005319 if (ret)
5320 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005321
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005322 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005323 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005324 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005325 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005326}
5327
Miao Xied20983b2014-07-03 18:22:13 +08005328static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5329{
David Sterbafc9a2ac2019-05-17 11:43:22 +02005330 const int index = btrfs_bg_flags_to_raid_index(map->type);
Miao Xied20983b2014-07-03 18:22:13 +08005331
David Sterbafc9a2ac2019-05-17 11:43:22 +02005332 return btrfs_raid_array[index].tolerated_failures;
Yan Zheng2b820322008-11-17 21:11:30 -05005333}
5334
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005335int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005336{
5337 struct extent_map *em;
5338 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005339 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005340 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005341 int i;
5342
Omar Sandoval60ca8422018-05-16 16:34:31 -07005343 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005344 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005345 return 1;
5346
Jeff Mahoney95617d62015-06-03 10:55:48 -04005347 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005348 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005349 if (test_bit(BTRFS_DEV_STATE_MISSING,
5350 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005351 miss_ndevs++;
5352 continue;
5353 }
Anand Jainebbede42017-12-04 12:54:52 +08005354 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5355 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005356 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005357 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005358 }
5359 }
Miao Xied20983b2014-07-03 18:22:13 +08005360
5361 /*
5362 * If the number of missing devices is larger than max errors,
5363 * we can not write the data into that chunk successfully, so
5364 * set it readonly.
5365 */
5366 if (miss_ndevs > btrfs_chunk_max_errors(map))
5367 readonly = 1;
5368end:
Yan Zheng2b820322008-11-17 21:11:30 -05005369 free_extent_map(em);
5370 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005371}
5372
David Sterbac8bf1b62019-05-17 11:43:17 +02005373void btrfs_mapping_tree_free(struct extent_map_tree *tree)
Chris Mason0b86a832008-03-24 15:01:56 -04005374{
5375 struct extent_map *em;
5376
Chris Masond3977122009-01-05 21:25:51 -05005377 while (1) {
David Sterbac8bf1b62019-05-17 11:43:17 +02005378 write_lock(&tree->lock);
5379 em = lookup_extent_mapping(tree, 0, (u64)-1);
Chris Mason0b86a832008-03-24 15:01:56 -04005380 if (em)
David Sterbac8bf1b62019-05-17 11:43:17 +02005381 remove_extent_mapping(tree, em);
5382 write_unlock(&tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005383 if (!em)
5384 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005385 /* once for us */
5386 free_extent_map(em);
5387 /* once for the tree */
5388 free_extent_map(em);
5389 }
5390}
5391
Stefan Behrens5d964052012-11-05 14:59:07 +01005392int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005393{
5394 struct extent_map *em;
5395 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005396 int ret;
5397
Omar Sandoval60ca8422018-05-16 16:34:31 -07005398 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005399 if (IS_ERR(em))
5400 /*
5401 * We could return errors for these cases, but that could get
5402 * ugly and we'd probably do the same thing which is just not do
5403 * anything else and exit, so return 1 so the callers don't try
5404 * to use other copies.
5405 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005406 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005407
Jeff Mahoney95617d62015-06-03 10:55:48 -04005408 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005409 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5410 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005411 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5412 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005413 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5414 ret = 2;
5415 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005416 /*
5417 * There could be two corrupted data stripes, we need
5418 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005419 *
Liu Bo8810f752018-01-02 13:36:41 -07005420 * Fail a stripe at a time on every retry except the
5421 * stripe under reconstruction.
5422 */
5423 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005424 else
5425 ret = 1;
5426 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005427
David Sterbacb5583d2018-09-07 16:11:23 +02005428 down_read(&fs_info->dev_replace.rwsem);
Liu Bo6fad8232017-03-14 13:33:59 -07005429 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5430 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005431 ret++;
David Sterbacb5583d2018-09-07 16:11:23 +02005432 up_read(&fs_info->dev_replace.rwsem);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005433
Chris Masonf1885912008-04-09 16:28:12 -04005434 return ret;
5435}
5436
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005437unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005438 u64 logical)
5439{
5440 struct extent_map *em;
5441 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005442 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005443
Omar Sandoval60ca8422018-05-16 16:34:31 -07005444 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005445
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005446 if (!WARN_ON(IS_ERR(em))) {
5447 map = em->map_lookup;
5448 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5449 len = map->stripe_len * nr_data_stripes(map);
5450 free_extent_map(em);
5451 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005452 return len;
5453}
5454
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005455int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005456{
5457 struct extent_map *em;
5458 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005459 int ret = 0;
5460
Omar Sandoval60ca8422018-05-16 16:34:31 -07005461 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005462
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005463 if(!WARN_ON(IS_ERR(em))) {
5464 map = em->map_lookup;
5465 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5466 ret = 1;
5467 free_extent_map(em);
5468 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005469 return ret;
5470}
5471
Stefan Behrens30d98612012-11-06 14:52:18 +01005472static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005473 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005474 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005475{
5476 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005477 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005478 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005479 int tolerance;
5480 struct btrfs_device *srcdev;
5481
Anand Jain99f92a72018-03-14 16:29:12 +08005482 ASSERT((map->type &
5483 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5484
5485 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5486 num_stripes = map->sub_stripes;
5487 else
5488 num_stripes = map->num_stripes;
5489
Anand Jain8ba0ae72018-03-14 16:29:13 +08005490 preferred_mirror = first + current->pid % num_stripes;
5491
Stefan Behrens30d98612012-11-06 14:52:18 +01005492 if (dev_replace_is_ongoing &&
5493 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5494 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5495 srcdev = fs_info->dev_replace.srcdev;
5496 else
5497 srcdev = NULL;
5498
5499 /*
5500 * try to avoid the drive that is the source drive for a
5501 * dev-replace procedure, only choose it if no other non-missing
5502 * mirror is available
5503 */
5504 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005505 if (map->stripes[preferred_mirror].dev->bdev &&
5506 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5507 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005508 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005509 if (map->stripes[i].dev->bdev &&
5510 (tolerance || map->stripes[i].dev != srcdev))
5511 return i;
5512 }
Chris Masondfe25022008-05-13 13:46:40 -04005513 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005514
Chris Masondfe25022008-05-13 13:46:40 -04005515 /* we couldn't find one that doesn't fail. Just return something
5516 * and the io error handling code will clean up eventually
5517 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005518 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005519}
5520
David Woodhouse53b381b2013-01-29 18:40:14 -05005521static inline int parity_smaller(u64 a, u64 b)
5522{
5523 return a > b;
5524}
5525
5526/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005527static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005528{
5529 struct btrfs_bio_stripe s;
5530 int i;
5531 u64 l;
5532 int again = 1;
5533
5534 while (again) {
5535 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005536 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005537 if (parity_smaller(bbio->raid_map[i],
5538 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005539 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005540 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005541 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005542 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005543 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005544 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005545
David Woodhouse53b381b2013-01-29 18:40:14 -05005546 again = 1;
5547 }
5548 }
5549 }
5550}
5551
Zhao Lei6e9606d2015-01-20 15:11:34 +08005552static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5553{
5554 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005555 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005556 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005557 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005558 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005559 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005560 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005561 /*
5562 * plus the raid_map, which includes both the tgt dev
5563 * and the stripes
5564 */
5565 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005566 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005567
5568 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005569 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005570
5571 return bbio;
5572}
5573
5574void btrfs_get_bbio(struct btrfs_bio *bbio)
5575{
Elena Reshetova140475a2017-03-03 10:55:10 +02005576 WARN_ON(!refcount_read(&bbio->refs));
5577 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005578}
5579
5580void btrfs_put_bbio(struct btrfs_bio *bbio)
5581{
5582 if (!bbio)
5583 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005584 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005585 kfree(bbio);
5586}
5587
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005588/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5589/*
5590 * Please note that, discard won't be sent to target device of device
5591 * replace.
5592 */
5593static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5594 u64 logical, u64 length,
5595 struct btrfs_bio **bbio_ret)
5596{
5597 struct extent_map *em;
5598 struct map_lookup *map;
5599 struct btrfs_bio *bbio;
5600 u64 offset;
5601 u64 stripe_nr;
5602 u64 stripe_nr_end;
5603 u64 stripe_end_offset;
5604 u64 stripe_cnt;
5605 u64 stripe_len;
5606 u64 stripe_offset;
5607 u64 num_stripes;
5608 u32 stripe_index;
5609 u32 factor = 0;
5610 u32 sub_stripes = 0;
5611 u64 stripes_per_dev = 0;
5612 u32 remaining_stripes = 0;
5613 u32 last_stripe = 0;
5614 int ret = 0;
5615 int i;
5616
5617 /* discard always return a bbio */
5618 ASSERT(bbio_ret);
5619
Omar Sandoval60ca8422018-05-16 16:34:31 -07005620 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005621 if (IS_ERR(em))
5622 return PTR_ERR(em);
5623
5624 map = em->map_lookup;
5625 /* we don't discard raid56 yet */
5626 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5627 ret = -EOPNOTSUPP;
5628 goto out;
5629 }
5630
5631 offset = logical - em->start;
5632 length = min_t(u64, em->len - offset, length);
5633
5634 stripe_len = map->stripe_len;
5635 /*
5636 * stripe_nr counts the total number of stripes we have to stride
5637 * to get to this block
5638 */
5639 stripe_nr = div64_u64(offset, stripe_len);
5640
5641 /* stripe_offset is the offset of this block in its stripe */
5642 stripe_offset = offset - stripe_nr * stripe_len;
5643
5644 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005645 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005646 stripe_cnt = stripe_nr_end - stripe_nr;
5647 stripe_end_offset = stripe_nr_end * map->stripe_len -
5648 (offset + length);
5649 /*
5650 * after this, stripe_nr is the number of stripes on this
5651 * device we have to walk to find the data, and stripe_index is
5652 * the number of our device in the stripe array
5653 */
5654 num_stripes = 1;
5655 stripe_index = 0;
5656 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5657 BTRFS_BLOCK_GROUP_RAID10)) {
5658 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5659 sub_stripes = 1;
5660 else
5661 sub_stripes = map->sub_stripes;
5662
5663 factor = map->num_stripes / sub_stripes;
5664 num_stripes = min_t(u64, map->num_stripes,
5665 sub_stripes * stripe_cnt);
5666 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5667 stripe_index *= sub_stripes;
5668 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5669 &remaining_stripes);
5670 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5671 last_stripe *= sub_stripes;
5672 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5673 BTRFS_BLOCK_GROUP_DUP)) {
5674 num_stripes = map->num_stripes;
5675 } else {
5676 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5677 &stripe_index);
5678 }
5679
5680 bbio = alloc_btrfs_bio(num_stripes, 0);
5681 if (!bbio) {
5682 ret = -ENOMEM;
5683 goto out;
5684 }
5685
5686 for (i = 0; i < num_stripes; i++) {
5687 bbio->stripes[i].physical =
5688 map->stripes[stripe_index].physical +
5689 stripe_offset + stripe_nr * map->stripe_len;
5690 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5691
5692 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5693 BTRFS_BLOCK_GROUP_RAID10)) {
5694 bbio->stripes[i].length = stripes_per_dev *
5695 map->stripe_len;
5696
5697 if (i / sub_stripes < remaining_stripes)
5698 bbio->stripes[i].length +=
5699 map->stripe_len;
5700
5701 /*
5702 * Special for the first stripe and
5703 * the last stripe:
5704 *
5705 * |-------|...|-------|
5706 * |----------|
5707 * off end_off
5708 */
5709 if (i < sub_stripes)
5710 bbio->stripes[i].length -=
5711 stripe_offset;
5712
5713 if (stripe_index >= last_stripe &&
5714 stripe_index <= (last_stripe +
5715 sub_stripes - 1))
5716 bbio->stripes[i].length -=
5717 stripe_end_offset;
5718
5719 if (i == sub_stripes - 1)
5720 stripe_offset = 0;
5721 } else {
5722 bbio->stripes[i].length = length;
5723 }
5724
5725 stripe_index++;
5726 if (stripe_index == map->num_stripes) {
5727 stripe_index = 0;
5728 stripe_nr++;
5729 }
5730 }
5731
5732 *bbio_ret = bbio;
5733 bbio->map_type = map->type;
5734 bbio->num_stripes = num_stripes;
5735out:
5736 free_extent_map(em);
5737 return ret;
5738}
5739
Liu Bo5ab56092017-03-14 13:33:57 -07005740/*
5741 * In dev-replace case, for repair case (that's the only case where the mirror
5742 * is selected explicitly when calling btrfs_map_block), blocks left of the
5743 * left cursor can also be read from the target drive.
5744 *
5745 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5746 * array of stripes.
5747 * For READ, it also needs to be supported using the same mirror number.
5748 *
5749 * If the requested block is not left of the left cursor, EIO is returned. This
5750 * can happen because btrfs_num_copies() returns one more in the dev-replace
5751 * case.
5752 */
5753static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5754 u64 logical, u64 length,
5755 u64 srcdev_devid, int *mirror_num,
5756 u64 *physical)
5757{
5758 struct btrfs_bio *bbio = NULL;
5759 int num_stripes;
5760 int index_srcdev = 0;
5761 int found = 0;
5762 u64 physical_of_found = 0;
5763 int i;
5764 int ret = 0;
5765
5766 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5767 logical, &length, &bbio, 0, 0);
5768 if (ret) {
5769 ASSERT(bbio == NULL);
5770 return ret;
5771 }
5772
5773 num_stripes = bbio->num_stripes;
5774 if (*mirror_num > num_stripes) {
5775 /*
5776 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5777 * that means that the requested area is not left of the left
5778 * cursor
5779 */
5780 btrfs_put_bbio(bbio);
5781 return -EIO;
5782 }
5783
5784 /*
5785 * process the rest of the function using the mirror_num of the source
5786 * drive. Therefore look it up first. At the end, patch the device
5787 * pointer to the one of the target drive.
5788 */
5789 for (i = 0; i < num_stripes; i++) {
5790 if (bbio->stripes[i].dev->devid != srcdev_devid)
5791 continue;
5792
5793 /*
5794 * In case of DUP, in order to keep it simple, only add the
5795 * mirror with the lowest physical address
5796 */
5797 if (found &&
5798 physical_of_found <= bbio->stripes[i].physical)
5799 continue;
5800
5801 index_srcdev = i;
5802 found = 1;
5803 physical_of_found = bbio->stripes[i].physical;
5804 }
5805
5806 btrfs_put_bbio(bbio);
5807
5808 ASSERT(found);
5809 if (!found)
5810 return -EIO;
5811
5812 *mirror_num = index_srcdev + 1;
5813 *physical = physical_of_found;
5814 return ret;
5815}
5816
Liu Bo73c0f222017-03-14 13:33:58 -07005817static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5818 struct btrfs_bio **bbio_ret,
5819 struct btrfs_dev_replace *dev_replace,
5820 int *num_stripes_ret, int *max_errors_ret)
5821{
5822 struct btrfs_bio *bbio = *bbio_ret;
5823 u64 srcdev_devid = dev_replace->srcdev->devid;
5824 int tgtdev_indexes = 0;
5825 int num_stripes = *num_stripes_ret;
5826 int max_errors = *max_errors_ret;
5827 int i;
5828
5829 if (op == BTRFS_MAP_WRITE) {
5830 int index_where_to_add;
5831
5832 /*
5833 * duplicate the write operations while the dev replace
5834 * procedure is running. Since the copying of the old disk to
5835 * the new disk takes place at run time while the filesystem is
5836 * mounted writable, the regular write operations to the old
5837 * disk have to be duplicated to go to the new disk as well.
5838 *
5839 * Note that device->missing is handled by the caller, and that
5840 * the write to the old disk is already set up in the stripes
5841 * array.
5842 */
5843 index_where_to_add = num_stripes;
5844 for (i = 0; i < num_stripes; i++) {
5845 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5846 /* write to new disk, too */
5847 struct btrfs_bio_stripe *new =
5848 bbio->stripes + index_where_to_add;
5849 struct btrfs_bio_stripe *old =
5850 bbio->stripes + i;
5851
5852 new->physical = old->physical;
5853 new->length = old->length;
5854 new->dev = dev_replace->tgtdev;
5855 bbio->tgtdev_map[i] = index_where_to_add;
5856 index_where_to_add++;
5857 max_errors++;
5858 tgtdev_indexes++;
5859 }
5860 }
5861 num_stripes = index_where_to_add;
5862 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5863 int index_srcdev = 0;
5864 int found = 0;
5865 u64 physical_of_found = 0;
5866
5867 /*
5868 * During the dev-replace procedure, the target drive can also
5869 * be used to read data in case it is needed to repair a corrupt
5870 * block elsewhere. This is possible if the requested area is
5871 * left of the left cursor. In this area, the target drive is a
5872 * full copy of the source drive.
5873 */
5874 for (i = 0; i < num_stripes; i++) {
5875 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5876 /*
5877 * In case of DUP, in order to keep it simple,
5878 * only add the mirror with the lowest physical
5879 * address
5880 */
5881 if (found &&
5882 physical_of_found <=
5883 bbio->stripes[i].physical)
5884 continue;
5885 index_srcdev = i;
5886 found = 1;
5887 physical_of_found = bbio->stripes[i].physical;
5888 }
5889 }
5890 if (found) {
5891 struct btrfs_bio_stripe *tgtdev_stripe =
5892 bbio->stripes + num_stripes;
5893
5894 tgtdev_stripe->physical = physical_of_found;
5895 tgtdev_stripe->length =
5896 bbio->stripes[index_srcdev].length;
5897 tgtdev_stripe->dev = dev_replace->tgtdev;
5898 bbio->tgtdev_map[index_srcdev] = num_stripes;
5899
5900 tgtdev_indexes++;
5901 num_stripes++;
5902 }
5903 }
5904
5905 *num_stripes_ret = num_stripes;
5906 *max_errors_ret = max_errors;
5907 bbio->num_tgtdevs = tgtdev_indexes;
5908 *bbio_ret = bbio;
5909}
5910
Liu Bo2b19a1f2017-03-14 13:34:00 -07005911static bool need_full_stripe(enum btrfs_map_op op)
5912{
5913 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5914}
5915
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005916static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5917 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005918 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005919 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005920 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005921{
5922 struct extent_map *em;
5923 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005924 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005925 u64 stripe_offset;
5926 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005927 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005928 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005929 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005930 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005931 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005932 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005933 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005934 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005935 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5936 int dev_replace_is_ongoing = 0;
5937 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005938 int patch_the_first_stripe_for_dev_replace = 0;
5939 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005940 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005941
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005942 if (op == BTRFS_MAP_DISCARD)
5943 return __btrfs_map_block_for_discard(fs_info, logical,
5944 *length, bbio_ret);
5945
Omar Sandoval60ca8422018-05-16 16:34:31 -07005946 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07005947 if (IS_ERR(em))
5948 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005949
Jeff Mahoney95617d62015-06-03 10:55:48 -04005950 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005951 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005952
David Woodhouse53b381b2013-01-29 18:40:14 -05005953 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005954 stripe_nr = offset;
5955 /*
5956 * stripe_nr counts the total number of stripes we have to stride
5957 * to get to this block
5958 */
David Sterba47c57132015-02-20 18:43:47 +01005959 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005960
David Woodhouse53b381b2013-01-29 18:40:14 -05005961 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005962 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005963 btrfs_crit(fs_info,
5964 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005965 stripe_offset, offset, em->start, logical,
5966 stripe_len);
5967 free_extent_map(em);
5968 return -EINVAL;
5969 }
Chris Mason593060d2008-03-25 16:50:33 -04005970
5971 /* stripe_offset is the offset of this block in its stripe*/
5972 stripe_offset = offset - stripe_offset;
5973
David Woodhouse53b381b2013-01-29 18:40:14 -05005974 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005975 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005976 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5977 raid56_full_stripe_start = offset;
5978
5979 /* allow a write of a full stripe, but make sure we don't
5980 * allow straddling of stripes
5981 */
David Sterba47c57132015-02-20 18:43:47 +01005982 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5983 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005984 raid56_full_stripe_start *= full_stripe_len;
5985 }
5986
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005987 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005988 u64 max_len;
5989 /* For writes to RAID[56], allow a full stripeset across all disks.
5990 For other RAID types and for RAID[56] reads, just allow a single
5991 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005992 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005993 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005994 max_len = stripe_len * nr_data_stripes(map) -
5995 (offset - raid56_full_stripe_start);
5996 } else {
5997 /* we limit the length of each bio to what fits in a stripe */
5998 max_len = stripe_len - stripe_offset;
5999 }
6000 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04006001 } else {
6002 *length = em->len - offset;
6003 }
Chris Masonf2d8d742008-04-21 10:03:05 -04006004
Nikolay Borisovda12fe52018-11-27 20:57:58 +02006005 /*
6006 * This is for when we're called from btrfs_bio_fits_in_stripe and all
6007 * it cares about is the length
6008 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006009 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04006010 goto out;
6011
David Sterbacb5583d2018-09-07 16:11:23 +02006012 down_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006013 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
David Sterba53176dd2018-04-05 01:41:06 +02006014 /*
6015 * Hold the semaphore for read during the whole operation, write is
6016 * requested at commit time but must wait.
6017 */
Stefan Behrens472262f2012-11-06 14:43:46 +01006018 if (!dev_replace_is_ongoing)
David Sterbacb5583d2018-09-07 16:11:23 +02006019 up_read(&dev_replace->rwsem);
Stefan Behrens472262f2012-11-06 14:43:46 +01006020
Stefan Behrensad6d6202012-11-06 15:06:47 +01006021 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006022 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07006023 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
6024 dev_replace->srcdev->devid,
6025 &mirror_num,
6026 &physical_to_patch_in_first_stripe);
6027 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006028 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07006029 else
6030 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006031 } else if (mirror_num > map->num_stripes) {
6032 mirror_num = 0;
6033 }
6034
Chris Masonf2d8d742008-04-21 10:03:05 -04006035 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04006036 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006037 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01006038 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6039 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006040 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08006041 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00006042 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08006043 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006044 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04006045 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04006046 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006047 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01006048 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01006049 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006050 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006051 }
Chris Mason2fff7342008-04-29 14:12:09 -04006052
Chris Mason611f0e02008-04-03 16:29:03 -04006053 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08006054 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04006055 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006056 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04006057 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006058 } else {
6059 mirror_num = 1;
6060 }
Chris Mason2fff7342008-04-29 14:12:09 -04006061
Chris Mason321aecc2008-04-16 10:49:51 -04006062 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01006063 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006064
David Sterba47c57132015-02-20 18:43:47 +01006065 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04006066 stripe_index *= map->sub_stripes;
6067
Anand Jainde483732017-10-12 16:43:00 +08006068 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04006069 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04006070 else if (mirror_num)
6071 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04006072 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04006073 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01006074 stripe_index = find_live_mirror(fs_info, map,
6075 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01006076 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04006077 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04006078 }
David Woodhouse53b381b2013-01-29 18:40:14 -05006079
Zhao Leiffe2d202015-01-20 15:11:44 +08006080 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08006081 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006082 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07006083 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01006084 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006085
6086 /* RAID[56] write or recovery. Return all stripes */
6087 num_stripes = map->num_stripes;
6088 max_errors = nr_parity_stripes(map);
6089
David Woodhouse53b381b2013-01-29 18:40:14 -05006090 *length = map->stripe_len;
6091 stripe_index = 0;
6092 stripe_offset = 0;
6093 } else {
6094 /*
6095 * Mirror #0 or #1 means the original data block.
6096 * Mirror #2 is RAID5 parity block.
6097 * Mirror #3 is RAID6 Q block.
6098 */
David Sterba47c57132015-02-20 18:43:47 +01006099 stripe_nr = div_u64_rem(stripe_nr,
6100 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05006101 if (mirror_num > 1)
6102 stripe_index = nr_data_stripes(map) +
6103 mirror_num - 2;
6104
6105 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01006106 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
6107 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08006108 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08006109 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05006110 }
Chris Mason8790d502008-04-03 16:29:03 -04006111 } else {
6112 /*
David Sterba47c57132015-02-20 18:43:47 +01006113 * after this, stripe_nr is the number of stripes on this
6114 * device we have to walk to find the data, and stripe_index is
6115 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04006116 */
David Sterba47c57132015-02-20 18:43:47 +01006117 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
6118 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006119 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04006120 }
Josef Bacike042d1e2016-04-12 12:54:40 -04006121 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006122 btrfs_crit(fs_info,
6123 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04006124 stripe_index, map->num_stripes);
6125 ret = -EINVAL;
6126 goto out;
6127 }
Chris Mason593060d2008-03-25 16:50:33 -04006128
Stefan Behrens472262f2012-11-06 14:43:46 +01006129 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07006130 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006131 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006132 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006133 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01006134 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08006135 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006136 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08006137
Zhao Lei6e9606d2015-01-20 15:11:34 +08006138 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08006139 if (!bbio) {
6140 ret = -ENOMEM;
6141 goto out;
6142 }
Liu Bo6fad8232017-03-14 13:33:59 -07006143 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08006144 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08006145
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006146 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07006147 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
6148 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006149 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01006150 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006151
6152 bbio->raid_map = (u64 *)((void *)bbio->stripes +
6153 sizeof(struct btrfs_bio_stripe) *
6154 num_alloc_stripes +
6155 sizeof(int) * tgtdev_indexes);
6156
6157 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01006158 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006159
6160 /* Fill in the logical address of each stripe */
6161 tmp = stripe_nr * nr_data_stripes(map);
6162 for (i = 0; i < nr_data_stripes(map); i++)
6163 bbio->raid_map[(i+rot) % num_stripes] =
6164 em->start + (tmp + i) * map->stripe_len;
6165
6166 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
6167 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
6168 bbio->raid_map[(i+rot+1) % num_stripes] =
6169 RAID6_Q_STRIPE;
6170 }
6171
Li Zefanec9ef7a2011-12-01 14:06:42 +08006172
Liu Bo0b3d4cd2017-03-14 13:33:56 -07006173 for (i = 0; i < num_stripes; i++) {
6174 bbio->stripes[i].physical =
6175 map->stripes[stripe_index].physical +
6176 stripe_offset +
6177 stripe_nr * map->stripe_len;
6178 bbio->stripes[i].dev =
6179 map->stripes[stripe_index].dev;
6180 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04006181 }
Li Zefande11cc12011-12-01 12:55:47 +08006182
Liu Bo2b19a1f2017-03-14 13:34:00 -07006183 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08006184 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08006185
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006186 if (bbio->raid_map)
6187 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08006188
Liu Bo73c0f222017-03-14 13:33:58 -07006189 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07006190 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07006191 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
6192 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01006193 }
6194
Li Zefande11cc12011-12-01 12:55:47 +08006195 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08006196 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08006197 bbio->num_stripes = num_stripes;
6198 bbio->max_errors = max_errors;
6199 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01006200
6201 /*
6202 * this is the case that REQ_READ && dev_replace_is_ongoing &&
6203 * mirror_num == num_stripes + 1 && dev_replace target drive is
6204 * available as a mirror
6205 */
6206 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6207 WARN_ON(num_stripes > 1);
6208 bbio->stripes[0].dev = dev_replace->tgtdev;
6209 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6210 bbio->mirror_num = map->num_stripes + 1;
6211 }
Chris Masoncea9e442008-04-09 16:28:12 -04006212out:
Liu Bo73beece2015-07-17 16:49:19 +08006213 if (dev_replace_is_ongoing) {
David Sterba53176dd2018-04-05 01:41:06 +02006214 lockdep_assert_held(&dev_replace->rwsem);
6215 /* Unlock and let waiting writers proceed */
David Sterbacb5583d2018-09-07 16:11:23 +02006216 up_read(&dev_replace->rwsem);
Liu Bo73beece2015-07-17 16:49:19 +08006217 }
Chris Mason0b86a832008-03-24 15:01:56 -04006218 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006219 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006220}
6221
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006222int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006223 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006224 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006225{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006226 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006227 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006228}
6229
Miao Xieaf8e2d12014-10-23 14:42:50 +08006230/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006231int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006232 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006233 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006234{
David Sterba825ad4c2017-03-28 14:45:22 +02006235 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006236}
6237
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03006238int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
6239 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05006240{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006241 struct extent_map *em;
6242 struct map_lookup *map;
6243 u64 *buf;
6244 u64 bytenr;
6245 u64 length;
6246 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006247 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006248 int i, j, nr = 0;
6249
Omar Sandoval60ca8422018-05-16 16:34:31 -07006250 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07006251 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006252 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006253
Jeff Mahoney95617d62015-06-03 10:55:48 -04006254 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006255 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006256 rmap_len = map->stripe_len;
6257
Yan Zhenga512bbf2008-12-08 16:46:26 -05006258 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006259 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006260 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006261 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006262 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006263 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006264 rmap_len = map->stripe_len * nr_data_stripes(map);
6265 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006266
David Sterba31e818f2015-02-20 18:00:26 +01006267 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006268 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006269
6270 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006271 if (map->stripes[i].physical > physical ||
6272 map->stripes[i].physical + length <= physical)
6273 continue;
6274
6275 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006276 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006277
6278 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6279 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006280 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006281 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6282 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006283 } /* else if RAID[56], multiply by nr_data_stripes().
6284 * Alternatively, just use rmap_len below instead of
6285 * map->stripe_len */
6286
6287 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006288 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006289 for (j = 0; j < nr; j++) {
6290 if (buf[j] == bytenr)
6291 break;
6292 }
Chris Mason934d3752008-12-08 16:43:10 -05006293 if (j == nr) {
6294 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006295 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006296 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006297 }
6298
Yan Zhenga512bbf2008-12-08 16:46:26 -05006299 *logical = buf;
6300 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006301 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006302
6303 free_extent_map(em);
6304 return 0;
6305}
6306
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006307static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006308{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006309 bio->bi_private = bbio->private;
6310 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006311 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006312
Zhao Lei6e9606d2015-01-20 15:11:34 +08006313 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006314}
6315
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006316static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006317{
Chris Mason9be33952013-05-17 18:30:14 -04006318 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006319 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006320
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006321 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006322 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006323 if (bio->bi_status == BLK_STS_IOERR ||
6324 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006325 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006326 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006327 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006328
6329 BUG_ON(stripe_index >= bbio->num_stripes);
6330 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006331 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006332 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006333 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006334 BTRFS_DEV_STAT_WRITE_ERRS);
David Sterba0cc068e2019-03-07 15:40:50 +01006335 else if (!(bio->bi_opf & REQ_RAHEAD))
Anand Jain1cb34c82017-10-21 01:45:33 +08006336 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006337 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006338 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006339 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006340 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006341 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006342 }
6343 }
Chris Mason8790d502008-04-03 16:29:03 -04006344
Jan Schmidta1d3c472011-08-04 17:15:33 +02006345 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006346 is_orig_bio = 1;
6347
Miao Xiec404e0d2014-01-30 16:46:55 +08006348 btrfs_bio_counter_dec(bbio->fs_info);
6349
Jan Schmidta1d3c472011-08-04 17:15:33 +02006350 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006351 if (!is_orig_bio) {
6352 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006353 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006354 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006355
Chris Mason9be33952013-05-17 18:30:14 -04006356 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006357 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006358 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006359 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006360 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006361 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006362 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006363 /*
6364 * this bio is actually up to date, we didn't
6365 * go over the max number of errors
6366 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006367 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006368 }
Miao Xiec55f1392014-06-19 10:42:54 +08006369
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006370 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006371 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006372 bio_put(bio);
6373 }
Chris Mason8790d502008-04-03 16:29:03 -04006374}
6375
Chris Mason8b712842008-06-11 16:50:36 -04006376/*
6377 * see run_scheduled_bios for a description of why bios are collected for
6378 * async submit.
6379 *
6380 * This will add one bio to the pending list for a device and make sure
6381 * the work struct is scheduled.
6382 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006383static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006384 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006385{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006386 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006387 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006388 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006389
6390 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006391 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006392 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006393 return;
Chris Mason8b712842008-06-11 16:50:36 -04006394 }
6395
Chris Mason492bb6de2008-07-31 16:29:02 -04006396 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006397 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006398
6399 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006400 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006401 pending_bios = &device->pending_sync_bios;
6402 else
6403 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006404
Chris Masonffbd5172009-04-20 15:50:09 -04006405 if (pending_bios->tail)
6406 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006407
Chris Masonffbd5172009-04-20 15:50:09 -04006408 pending_bios->tail = bio;
6409 if (!pending_bios->head)
6410 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006411 if (device->running_pending)
6412 should_queue = 0;
6413
6414 spin_unlock(&device->io_lock);
6415
6416 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006417 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006418}
6419
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006420static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6421 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006422{
6423 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006424 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006425
6426 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006427 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006428 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006429 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006430 btrfs_debug_in_rcu(fs_info,
6431 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6432 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6433 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6434 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006435 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006436
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006437 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006438
Josef Bacikde1ee922012-10-19 16:50:56 -04006439 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006440 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006441 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006442 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006443}
6444
Josef Bacikde1ee922012-10-19 16:50:56 -04006445static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6446{
6447 atomic_inc(&bbio->error);
6448 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006449 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006450 WARN_ON(bio != bbio->orig_bio);
6451
Chris Mason9be33952013-05-17 18:30:14 -04006452 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006453 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006454 if (atomic_read(&bbio->error) > bbio->max_errors)
6455 bio->bi_status = BLK_STS_IOERR;
6456 else
6457 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006458 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006459 }
6460}
6461
Omar Sandoval58efbc92017-08-22 23:45:59 -07006462blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6463 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006464{
Chris Mason0b86a832008-03-24 15:01:56 -04006465 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006466 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006467 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006468 u64 length = 0;
6469 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006470 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006471 int dev_nr;
6472 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006473 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006474
Kent Overstreet4f024f32013-10-11 15:44:27 -07006475 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006476 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006477
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006478 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006479 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006480 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006481 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006482 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006483 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006484 }
Chris Masoncea9e442008-04-09 16:28:12 -04006485
Jan Schmidta1d3c472011-08-04 17:15:33 +02006486 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006487 bbio->orig_bio = first_bio;
6488 bbio->private = first_bio->bi_private;
6489 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006490 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006491 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6492
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006493 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006494 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006495 /* In this case, map_length has been set to the length of
6496 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006497 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006498 ret = raid56_parity_write(fs_info, bio, bbio,
6499 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006500 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006501 ret = raid56_parity_recover(fs_info, bio, bbio,
6502 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006503 }
Miao Xie42452152014-11-25 16:39:28 +08006504
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006505 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006506 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006507 }
6508
Chris Masoncea9e442008-04-09 16:28:12 -04006509 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006510 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006511 "mapping failed logical %llu bio len %llu len %llu",
6512 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006513 BUG();
6514 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006515
Zhao Lei08da7572015-02-12 15:42:16 +08006516 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006517 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006518 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6519 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006520 (bio_op(first_bio) == REQ_OP_WRITE &&
6521 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006522 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006523 continue;
6524 }
6525
David Sterba3aa8e072017-06-02 17:38:30 +02006526 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006527 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006528 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006529 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006530
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006531 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6532 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006533 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006534 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006535 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006536}
6537
Anand Jain09ba3bc2019-01-19 14:48:55 +08006538/*
6539 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
6540 * return NULL.
6541 *
6542 * If devid and uuid are both specified, the match must be exact, otherwise
6543 * only devid is used.
6544 *
6545 * If @seed is true, traverse through the seed devices.
6546 */
Anand Jaine4319cd2019-01-17 23:32:31 +08006547struct btrfs_device *btrfs_find_device(struct btrfs_fs_devices *fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006548 u64 devid, u8 *uuid, u8 *fsid,
6549 bool seed)
Chris Mason0b86a832008-03-24 15:01:56 -04006550{
Yan Zheng2b820322008-11-17 21:11:30 -05006551 struct btrfs_device *device;
Chris Mason0b86a832008-03-24 15:01:56 -04006552
Anand Jaine4319cd2019-01-17 23:32:31 +08006553 while (fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006554 if (!fsid ||
Anand Jaine4319cd2019-01-17 23:32:31 +08006555 !memcmp(fs_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08006556 list_for_each_entry(device, &fs_devices->devices,
6557 dev_list) {
6558 if (device->devid == devid &&
6559 (!uuid || memcmp(device->uuid, uuid,
6560 BTRFS_UUID_SIZE) == 0))
6561 return device;
6562 }
Yan Zheng2b820322008-11-17 21:11:30 -05006563 }
Anand Jain09ba3bc2019-01-19 14:48:55 +08006564 if (seed)
6565 fs_devices = fs_devices->seed;
6566 else
6567 return NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05006568 }
6569 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006570}
6571
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006573 u64 devid, u8 *dev_uuid)
6574{
6575 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006576
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006577 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6578 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006579 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006580
6581 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006582 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006583 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006584
Anand Jaine6e674b2017-12-04 12:54:54 +08006585 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006586 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006587
Chris Masondfe25022008-05-13 13:46:40 -04006588 return device;
6589}
6590
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006591/**
6592 * btrfs_alloc_device - allocate struct btrfs_device
6593 * @fs_info: used only for generating a new devid, can be NULL if
6594 * devid is provided (i.e. @devid != NULL).
6595 * @devid: a pointer to devid for this device. If NULL a new devid
6596 * is generated.
6597 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6598 * is generated.
6599 *
6600 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006601 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006602 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006603 */
6604struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6605 const u64 *devid,
6606 const u8 *uuid)
6607{
6608 struct btrfs_device *dev;
6609 u64 tmp;
6610
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306611 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006612 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006613
6614 dev = __alloc_device();
6615 if (IS_ERR(dev))
6616 return dev;
6617
6618 if (devid)
6619 tmp = *devid;
6620 else {
6621 int ret;
6622
6623 ret = find_next_devid(fs_info, &tmp);
6624 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006625 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006626 return ERR_PTR(ret);
6627 }
6628 }
6629 dev->devid = tmp;
6630
6631 if (uuid)
6632 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6633 else
6634 generate_random_uuid(dev->uuid);
6635
Liu Bo9e0af232014-08-15 23:36:53 +08006636 btrfs_init_work(&dev->work, btrfs_submit_helper,
6637 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006638
6639 return dev;
6640}
6641
Anand Jain5a2b8e62017-10-09 11:07:45 +08006642static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006643 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006644{
Anand Jain2b902df2017-10-09 11:07:46 +08006645 if (error)
6646 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6647 devid, uuid);
6648 else
6649 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6650 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006651}
6652
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006653static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
6654{
6655 int index = btrfs_bg_flags_to_raid_index(type);
6656 int ncopies = btrfs_raid_array[index].ncopies;
6657 int data_stripes;
6658
6659 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
6660 case BTRFS_BLOCK_GROUP_RAID5:
6661 data_stripes = num_stripes - 1;
6662 break;
6663 case BTRFS_BLOCK_GROUP_RAID6:
6664 data_stripes = num_stripes - 2;
6665 break;
6666 default:
6667 data_stripes = num_stripes / ncopies;
6668 break;
6669 }
6670 return div_u64(chunk_len, data_stripes);
6671}
6672
David Sterba9690ac02019-03-20 16:43:07 +01006673static int read_one_chunk(struct btrfs_key *key, struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006674 struct btrfs_chunk *chunk)
6675{
David Sterba9690ac02019-03-20 16:43:07 +01006676 struct btrfs_fs_info *fs_info = leaf->fs_info;
David Sterbac8bf1b62019-05-17 11:43:17 +02006677 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006678 struct map_lookup *map;
6679 struct extent_map *em;
6680 u64 logical;
6681 u64 length;
6682 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006683 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006684 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006685 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006686 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006687
Chris Masone17cade2008-04-15 15:41:47 -04006688 logical = key->offset;
6689 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006690 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006691
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006692 /*
6693 * Only need to verify chunk item if we're reading from sys chunk array,
6694 * as chunk item in tree block is already verified by tree-checker.
6695 */
6696 if (leaf->start == BTRFS_SUPER_INFO_OFFSET) {
David Sterbaddaf1d52019-03-20 16:40:48 +01006697 ret = btrfs_check_chunk_valid(leaf, chunk, logical);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08006698 if (ret)
6699 return ret;
6700 }
Chris Masona061fc82008-05-07 11:43:44 -04006701
David Sterbac8bf1b62019-05-17 11:43:17 +02006702 read_lock(&map_tree->lock);
6703 em = lookup_extent_mapping(map_tree, logical, 1);
6704 read_unlock(&map_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006705
6706 /* already mapped? */
6707 if (em && em->start <= logical && em->start + em->len > logical) {
6708 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006709 return 0;
6710 } else if (em) {
6711 free_extent_map(em);
6712 }
Chris Mason0b86a832008-03-24 15:01:56 -04006713
David Sterba172ddd62011-04-21 00:48:27 +02006714 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006715 if (!em)
6716 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006717 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006718 if (!map) {
6719 free_extent_map(em);
6720 return -ENOMEM;
6721 }
6722
Wang Shilong298a8f92014-06-19 10:42:52 +08006723 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006724 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006725 em->start = logical;
6726 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006727 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006728 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006729 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006730
Chris Mason593060d2008-03-25 16:50:33 -04006731 map->num_stripes = num_stripes;
6732 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6733 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006734 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6735 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006736 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006737 map->verified_stripes = 0;
Nikolay Borisov39e264a2019-03-25 14:31:25 +02006738 em->orig_block_len = calc_stripe_length(map->type, em->len,
6739 map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04006740 for (i = 0; i < num_stripes; i++) {
6741 map->stripes[i].physical =
6742 btrfs_stripe_offset_nr(leaf, chunk, i);
6743 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006744 read_extent_buffer(leaf, uuid, (unsigned long)
6745 btrfs_stripe_dev_uuid_nr(chunk, i),
6746 BTRFS_UUID_SIZE);
Anand Jaine4319cd2019-01-17 23:32:31 +08006747 map->stripes[i].dev = btrfs_find_device(fs_info->fs_devices,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006748 devid, uuid, NULL, true);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006749 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006750 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006751 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006752 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006753 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006754 }
Chris Masondfe25022008-05-13 13:46:40 -04006755 if (!map->stripes[i].dev) {
6756 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006757 add_missing_dev(fs_info->fs_devices, devid,
6758 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006759 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006760 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006761 btrfs_err(fs_info,
6762 "failed to init missing dev %llu: %ld",
6763 devid, PTR_ERR(map->stripes[i].dev));
6764 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006765 }
Anand Jain2b902df2017-10-09 11:07:46 +08006766 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006767 }
Anand Jaine12c9622017-12-04 12:54:53 +08006768 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6769 &(map->stripes[i].dev->dev_state));
6770
Chris Mason0b86a832008-03-24 15:01:56 -04006771 }
6772
David Sterbac8bf1b62019-05-17 11:43:17 +02006773 write_lock(&map_tree->lock);
6774 ret = add_extent_mapping(map_tree, em, 0);
6775 write_unlock(&map_tree->lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006776 if (ret < 0) {
6777 btrfs_err(fs_info,
6778 "failed to add chunk map, start=%llu len=%llu: %d",
6779 em->start, em->len, ret);
6780 }
Chris Mason0b86a832008-03-24 15:01:56 -04006781 free_extent_map(em);
6782
Qu Wenruo64f64f42018-08-01 10:37:20 +08006783 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006784}
6785
Jeff Mahoney143bede2012-03-01 14:56:26 +01006786static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006787 struct btrfs_dev_item *dev_item,
6788 struct btrfs_device *device)
6789{
6790 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006791
6792 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006793 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6794 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006795 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006796 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006797 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006798 device->type = btrfs_device_type(leaf, dev_item);
6799 device->io_align = btrfs_device_io_align(leaf, dev_item);
6800 device->io_width = btrfs_device_io_width(leaf, dev_item);
6801 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006802 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006803 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006804
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006805 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006806 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006807}
6808
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006809static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006810 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006811{
6812 struct btrfs_fs_devices *fs_devices;
6813 int ret;
6814
David Sterbaa32bf9a2018-03-16 02:21:22 +01006815 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006816 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006817
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006818 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006819 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006820 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006821 return fs_devices;
6822
Yan Zheng2b820322008-11-17 21:11:30 -05006823 fs_devices = fs_devices->seed;
6824 }
6825
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006826 fs_devices = find_fsid(fsid, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05006827 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006828 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006829 return ERR_PTR(-ENOENT);
6830
Nikolay Borisov7239ff42018-10-30 16:43:23 +02006831 fs_devices = alloc_fs_devices(fsid, NULL);
Miao Xie5f375832014-09-03 21:35:46 +08006832 if (IS_ERR(fs_devices))
6833 return fs_devices;
6834
6835 fs_devices->seeding = 1;
6836 fs_devices->opened = 1;
6837 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006838 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006839
6840 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006841 if (IS_ERR(fs_devices))
6842 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006843
Anand Jain897fb572018-04-12 10:29:28 +08006844 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006845 if (ret) {
6846 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006847 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006848 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006849 }
Yan Zheng2b820322008-11-17 21:11:30 -05006850
6851 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006852 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006853 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006854 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006855 goto out;
6856 }
6857
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006858 fs_devices->seed = fs_info->fs_devices->seed;
6859 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006860out:
Miao Xie5f375832014-09-03 21:35:46 +08006861 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006862}
6863
David Sterba17850752019-03-20 16:45:15 +01006864static int read_one_dev(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006865 struct btrfs_dev_item *dev_item)
6866{
David Sterba17850752019-03-20 16:45:15 +01006867 struct btrfs_fs_info *fs_info = leaf->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006868 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006869 struct btrfs_device *device;
6870 u64 devid;
6871 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006872 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006873 u8 dev_uuid[BTRFS_UUID_SIZE];
6874
Chris Mason0b86a832008-03-24 15:01:56 -04006875 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006876 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006877 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006878 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006879 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006880
Nikolay Borisovde37aa52018-10-30 16:43:24 +02006881 if (memcmp(fs_uuid, fs_devices->metadata_uuid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006882 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006883 if (IS_ERR(fs_devices))
6884 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006885 }
6886
Anand Jaine4319cd2019-01-17 23:32:31 +08006887 device = btrfs_find_device(fs_info->fs_devices, devid, dev_uuid,
Anand Jain09ba3bc2019-01-19 14:48:55 +08006888 fs_uuid, true);
Miao Xie5f375832014-09-03 21:35:46 +08006889 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006890 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006891 btrfs_report_missing_device(fs_info, devid,
6892 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006893 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006894 }
Yan Zheng2b820322008-11-17 21:11:30 -05006895
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006896 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006897 if (IS_ERR(device)) {
6898 btrfs_err(fs_info,
6899 "failed to add missing dev %llu: %ld",
6900 devid, PTR_ERR(device));
6901 return PTR_ERR(device);
6902 }
Anand Jain2b902df2017-10-09 11:07:46 +08006903 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006904 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006905 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006906 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6907 btrfs_report_missing_device(fs_info,
6908 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006909 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006910 }
6911 btrfs_report_missing_device(fs_info, devid,
6912 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006913 }
Miao Xie5f375832014-09-03 21:35:46 +08006914
Anand Jaine6e674b2017-12-04 12:54:54 +08006915 if (!device->bdev &&
6916 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006917 /*
6918 * this happens when a device that was properly setup
6919 * in the device info lists suddenly goes bad.
6920 * device->bdev is NULL, and so we have to set
6921 * device->missing to one here
6922 */
Miao Xie5f375832014-09-03 21:35:46 +08006923 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006924 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006925 }
Miao Xie5f375832014-09-03 21:35:46 +08006926
6927 /* Move the device to its own fs_devices */
6928 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006929 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6930 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006931
6932 list_move(&device->dev_list, &fs_devices->devices);
6933 device->fs_devices->num_devices--;
6934 fs_devices->num_devices++;
6935
6936 device->fs_devices->missing_devices--;
6937 fs_devices->missing_devices++;
6938
6939 device->fs_devices = fs_devices;
6940 }
Yan Zheng2b820322008-11-17 21:11:30 -05006941 }
6942
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006943 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006944 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006945 if (device->generation !=
6946 btrfs_device_generation(leaf, dev_item))
6947 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006948 }
Chris Mason0b86a832008-03-24 15:01:56 -04006949
6950 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006951 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006952 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006953 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006954 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006955 atomic64_add(device->total_bytes - device->bytes_used,
6956 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006957 }
Chris Mason0b86a832008-03-24 15:01:56 -04006958 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006959 return ret;
6960}
6961
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006962int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006963{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006964 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006965 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006966 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006967 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006968 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006969 u8 *array_ptr;
6970 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006971 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006972 u32 num_stripes;
6973 u32 array_size;
6974 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006975 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006976 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006977 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006978
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006979 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006980 /*
6981 * This will create extent buffer of nodesize, superblock size is
6982 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6983 * overallocate but we can keep it as-is, only the first page is used.
6984 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006985 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006986 if (IS_ERR(sb))
6987 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006988 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006989 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006990 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006991 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006992 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006993 * pages up-to-date when the page is larger: extent does not cover the
6994 * whole page and consequently check_page_uptodate does not find all
6995 * the page's extents up-to-date (the hole beyond sb),
6996 * write_extent_buffer then triggers a WARN_ON.
6997 *
6998 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6999 * but sb spans only this function. Add an explicit SetPageUptodate call
7000 * to silence the warning eg. on PowerPC 64.
7001 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007002 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04007003 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05007004
Chris Masona061fc82008-05-07 11:43:44 -04007005 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04007006 array_size = btrfs_super_sys_array_size(super_copy);
7007
David Sterba1ffb22c2014-10-31 19:02:42 +01007008 array_ptr = super_copy->sys_chunk_array;
7009 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
7010 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007011
David Sterba1ffb22c2014-10-31 19:02:42 +01007012 while (cur_offset < array_size) {
7013 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01007014 len = sizeof(*disk_key);
7015 if (cur_offset + len > array_size)
7016 goto out_short_read;
7017
Chris Mason0b86a832008-03-24 15:01:56 -04007018 btrfs_disk_key_to_cpu(&key, disk_key);
7019
David Sterba1ffb22c2014-10-31 19:02:42 +01007020 array_ptr += len;
7021 sb_array_offset += len;
7022 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007023
Chris Mason0d81ba52008-03-24 15:02:07 -04007024 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01007025 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01007026 /*
7027 * At least one btrfs_chunk with one stripe must be
7028 * present, exact stripe count check comes afterwards
7029 */
7030 len = btrfs_chunk_item_size(1);
7031 if (cur_offset + len > array_size)
7032 goto out_short_read;
7033
7034 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01007035 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007036 btrfs_err(fs_info,
7037 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01007038 num_stripes, cur_offset);
7039 ret = -EIO;
7040 break;
7041 }
7042
Liu Boe06cd3d2016-06-03 12:05:15 -07007043 type = btrfs_chunk_type(sb, chunk);
7044 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007045 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07007046 "invalid chunk type %llu in sys_array at offset %u",
7047 type, cur_offset);
7048 ret = -EIO;
7049 break;
7050 }
7051
David Sterbae3540ea2014-11-05 15:24:51 +01007052 len = btrfs_chunk_item_size(num_stripes);
7053 if (cur_offset + len > array_size)
7054 goto out_short_read;
7055
David Sterba9690ac02019-03-20 16:43:07 +01007056 ret = read_one_chunk(&key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04007057 if (ret)
7058 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007059 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007060 btrfs_err(fs_info,
7061 "unexpected item type %u in sys_array at offset %u",
7062 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04007063 ret = -EIO;
7064 break;
Chris Mason0b86a832008-03-24 15:01:56 -04007065 }
David Sterba1ffb22c2014-10-31 19:02:42 +01007066 array_ptr += len;
7067 sb_array_offset += len;
7068 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04007069 }
Liu Bod8651772016-06-03 17:41:42 -07007070 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007071 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04007072 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01007073
7074out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007075 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01007076 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07007077 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07007078 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01007079 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04007080}
7081
Qu Wenruo21634a12017-03-09 09:34:36 +08007082/*
7083 * Check if all chunks in the fs are OK for read-write degraded mount
7084 *
Anand Jain6528b992017-12-18 17:08:59 +08007085 * If the @failing_dev is specified, it's accounted as missing.
7086 *
Qu Wenruo21634a12017-03-09 09:34:36 +08007087 * Return true if all chunks meet the minimal RW mount requirements.
7088 * Return false if any chunk doesn't meet the minimal RW mount requirements.
7089 */
Anand Jain6528b992017-12-18 17:08:59 +08007090bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
7091 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08007092{
David Sterbac8bf1b62019-05-17 11:43:17 +02007093 struct extent_map_tree *map_tree = &fs_info->mapping_tree;
Qu Wenruo21634a12017-03-09 09:34:36 +08007094 struct extent_map *em;
7095 u64 next_start = 0;
7096 bool ret = true;
7097
David Sterbac8bf1b62019-05-17 11:43:17 +02007098 read_lock(&map_tree->lock);
7099 em = lookup_extent_mapping(map_tree, 0, (u64)-1);
7100 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007101 /* No chunk at all? Return false anyway */
7102 if (!em) {
7103 ret = false;
7104 goto out;
7105 }
7106 while (em) {
7107 struct map_lookup *map;
7108 int missing = 0;
7109 int max_tolerated;
7110 int i;
7111
7112 map = em->map_lookup;
7113 max_tolerated =
7114 btrfs_get_num_tolerated_disk_barrier_failures(
7115 map->type);
7116 for (i = 0; i < map->num_stripes; i++) {
7117 struct btrfs_device *dev = map->stripes[i].dev;
7118
Anand Jaine6e674b2017-12-04 12:54:54 +08007119 if (!dev || !dev->bdev ||
7120 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08007121 dev->last_flush_error)
7122 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08007123 else if (failing_dev && failing_dev == dev)
7124 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08007125 }
7126 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08007127 if (!failing_dev)
7128 btrfs_warn(fs_info,
Andrea Gelmini52042d82018-11-28 12:05:13 +01007129 "chunk %llu missing %d devices, max tolerance is %d for writable mount",
Qu Wenruo21634a12017-03-09 09:34:36 +08007130 em->start, missing, max_tolerated);
7131 free_extent_map(em);
7132 ret = false;
7133 goto out;
7134 }
7135 next_start = extent_map_end(em);
7136 free_extent_map(em);
7137
David Sterbac8bf1b62019-05-17 11:43:17 +02007138 read_lock(&map_tree->lock);
7139 em = lookup_extent_mapping(map_tree, next_start,
Qu Wenruo21634a12017-03-09 09:34:36 +08007140 (u64)(-1) - next_start);
David Sterbac8bf1b62019-05-17 11:43:17 +02007141 read_unlock(&map_tree->lock);
Qu Wenruo21634a12017-03-09 09:34:36 +08007142 }
7143out:
7144 return ret;
7145}
7146
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007147int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04007148{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04007149 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04007150 struct btrfs_path *path;
7151 struct extent_buffer *leaf;
7152 struct btrfs_key key;
7153 struct btrfs_key found_key;
7154 int ret;
7155 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007156 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007157
Chris Mason0b86a832008-03-24 15:01:56 -04007158 path = btrfs_alloc_path();
7159 if (!path)
7160 return -ENOMEM;
7161
Anand Jain3dd0f7a2018-04-12 10:29:32 +08007162 /*
7163 * uuid_mutex is needed only if we are mounting a sprout FS
7164 * otherwise we don't need it.
7165 */
Li Zefanb367e472011-12-07 11:38:24 +08007166 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02007167 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007168
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007169 /*
7170 * Read all device items, and then all the chunk items. All
7171 * device items are found before any chunk item (their object id
7172 * is smaller than the lowest possible object id for a chunk
7173 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04007174 */
7175 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
7176 key.offset = 0;
7177 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007178 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007179 if (ret < 0)
7180 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007181 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007182 leaf = path->nodes[0];
7183 slot = path->slots[0];
7184 if (slot >= btrfs_header_nritems(leaf)) {
7185 ret = btrfs_next_leaf(root, path);
7186 if (ret == 0)
7187 continue;
7188 if (ret < 0)
7189 goto error;
7190 break;
7191 }
7192 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007193 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7194 struct btrfs_dev_item *dev_item;
7195 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007196 struct btrfs_dev_item);
David Sterba17850752019-03-20 16:45:15 +01007197 ret = read_one_dev(leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007198 if (ret)
7199 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007200 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007201 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7202 struct btrfs_chunk *chunk;
7203 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba9690ac02019-03-20 16:43:07 +01007204 ret = read_one_chunk(&found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007205 if (ret)
7206 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007207 }
7208 path->slots[0]++;
7209 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007210
7211 /*
7212 * After loading chunk tree, we've got all device information,
7213 * do another round of validation checks.
7214 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007215 if (total_dev != fs_info->fs_devices->total_devices) {
7216 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007217 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007218 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007219 total_dev);
7220 ret = -EINVAL;
7221 goto error;
7222 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007223 if (btrfs_super_total_bytes(fs_info->super_copy) <
7224 fs_info->fs_devices->total_rw_bytes) {
7225 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007226 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007227 btrfs_super_total_bytes(fs_info->super_copy),
7228 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007229 ret = -EINVAL;
7230 goto error;
7231 }
Chris Mason0b86a832008-03-24 15:01:56 -04007232 ret = 0;
7233error:
David Sterba34441362016-10-04 19:34:27 +02007234 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007235 mutex_unlock(&uuid_mutex);
7236
Yan Zheng2b820322008-11-17 21:11:30 -05007237 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007238 return ret;
7239}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007240
Miao Xiecb517ea2013-05-15 07:48:19 +00007241void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7242{
7243 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7244 struct btrfs_device *device;
7245
Liu Bo29cc83f2014-05-11 23:14:59 +08007246 while (fs_devices) {
7247 mutex_lock(&fs_devices->device_list_mutex);
7248 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007249 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007250 mutex_unlock(&fs_devices->device_list_mutex);
7251
7252 fs_devices = fs_devices->seed;
7253 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007254}
7255
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007256static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7257{
7258 int i;
7259
7260 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7261 btrfs_dev_stat_reset(dev, i);
7262}
7263
7264int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7265{
7266 struct btrfs_key key;
7267 struct btrfs_key found_key;
7268 struct btrfs_root *dev_root = fs_info->dev_root;
7269 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7270 struct extent_buffer *eb;
7271 int slot;
7272 int ret = 0;
7273 struct btrfs_device *device;
7274 struct btrfs_path *path = NULL;
7275 int i;
7276
7277 path = btrfs_alloc_path();
7278 if (!path) {
7279 ret = -ENOMEM;
7280 goto out;
7281 }
7282
7283 mutex_lock(&fs_devices->device_list_mutex);
7284 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7285 int item_size;
7286 struct btrfs_dev_stats_item *ptr;
7287
David Sterba242e2952016-01-25 17:51:31 +01007288 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7289 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007290 key.offset = device->devid;
7291 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7292 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007293 __btrfs_reset_dev_stats(device);
7294 device->dev_stats_valid = 1;
7295 btrfs_release_path(path);
7296 continue;
7297 }
7298 slot = path->slots[0];
7299 eb = path->nodes[0];
7300 btrfs_item_key_to_cpu(eb, &found_key, slot);
7301 item_size = btrfs_item_size_nr(eb, slot);
7302
7303 ptr = btrfs_item_ptr(eb, slot,
7304 struct btrfs_dev_stats_item);
7305
7306 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7307 if (item_size >= (1 + i) * sizeof(__le64))
7308 btrfs_dev_stat_set(device, i,
7309 btrfs_dev_stats_value(eb, ptr, i));
7310 else
7311 btrfs_dev_stat_reset(device, i);
7312 }
7313
7314 device->dev_stats_valid = 1;
7315 btrfs_dev_stat_print_on_load(device);
7316 btrfs_release_path(path);
7317 }
7318 mutex_unlock(&fs_devices->device_list_mutex);
7319
7320out:
7321 btrfs_free_path(path);
7322 return ret < 0 ? ret : 0;
7323}
7324
7325static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007326 struct btrfs_device *device)
7327{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007328 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007329 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007330 struct btrfs_path *path;
7331 struct btrfs_key key;
7332 struct extent_buffer *eb;
7333 struct btrfs_dev_stats_item *ptr;
7334 int ret;
7335 int i;
7336
David Sterba242e2952016-01-25 17:51:31 +01007337 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7338 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007339 key.offset = device->devid;
7340
7341 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007342 if (!path)
7343 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007344 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7345 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007346 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007347 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007348 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007349 goto out;
7350 }
7351
7352 if (ret == 0 &&
7353 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7354 /* need to delete old one and insert a new one */
7355 ret = btrfs_del_item(trans, dev_root, path);
7356 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007357 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007358 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007359 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007360 goto out;
7361 }
7362 ret = 1;
7363 }
7364
7365 if (ret == 1) {
7366 /* need to insert a new item */
7367 btrfs_release_path(path);
7368 ret = btrfs_insert_empty_item(trans, dev_root, path,
7369 &key, sizeof(*ptr));
7370 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007371 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007372 "insert dev_stats item for device %s failed %d",
7373 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007374 goto out;
7375 }
7376 }
7377
7378 eb = path->nodes[0];
7379 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7380 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7381 btrfs_set_dev_stats_value(eb, ptr, i,
7382 btrfs_dev_stat_read(device, i));
7383 btrfs_mark_buffer_dirty(eb);
7384
7385out:
7386 btrfs_free_path(path);
7387 return ret;
7388}
7389
7390/*
7391 * called from commit_transaction. Writes all changed device stats to disk.
7392 */
David Sterba196c9d82019-03-20 16:50:38 +01007393int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007394{
David Sterba196c9d82019-03-20 16:50:38 +01007395 struct btrfs_fs_info *fs_info = trans->fs_info;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007396 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7397 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007398 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007399 int ret = 0;
7400
7401 mutex_lock(&fs_devices->device_list_mutex);
7402 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007403 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7404 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007405 continue;
7406
Nikolay Borisov9deae962017-10-24 13:47:37 +03007407
7408 /*
7409 * There is a LOAD-LOAD control dependency between the value of
7410 * dev_stats_ccnt and updating the on-disk values which requires
7411 * reading the in-memory counters. Such control dependencies
7412 * require explicit read memory barriers.
7413 *
7414 * This memory barriers pairs with smp_mb__before_atomic in
7415 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7416 * barrier implied by atomic_xchg in
7417 * btrfs_dev_stats_read_and_reset
7418 */
7419 smp_rmb();
7420
Nikolay Borisov5495f192018-07-20 19:37:49 +03007421 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007422 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007423 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007424 }
7425 mutex_unlock(&fs_devices->device_list_mutex);
7426
7427 return ret;
7428}
7429
Stefan Behrens442a4f62012-05-25 16:06:08 +02007430void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7431{
7432 btrfs_dev_stat_inc(dev, index);
7433 btrfs_dev_stat_print_on_error(dev);
7434}
7435
Eric Sandeen48a3b632013-04-25 20:41:01 +00007436static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007437{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007438 if (!dev->dev_stats_valid)
7439 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007440 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007441 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007442 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007443 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7444 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7445 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007446 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7447 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007448}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007449
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007450static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7451{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007452 int i;
7453
7454 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7455 if (btrfs_dev_stat_read(dev, i) != 0)
7456 break;
7457 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7458 return; /* all values == 0, suppress message */
7459
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007460 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007461 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007462 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007463 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7464 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7465 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7466 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7467 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7468}
7469
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007470int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007471 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007472{
7473 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007474 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007475 int i;
7476
7477 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain09ba3bc2019-01-19 14:48:55 +08007478 dev = btrfs_find_device(fs_info->fs_devices, stats->devid, NULL, NULL,
7479 true);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007480 mutex_unlock(&fs_devices->device_list_mutex);
7481
7482 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007483 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007484 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007485 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007486 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007487 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007488 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007489 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7490 if (stats->nr_items > i)
7491 stats->values[i] =
7492 btrfs_dev_stat_read_and_reset(dev, i);
7493 else
7494 btrfs_dev_stat_reset(dev, i);
7495 }
7496 } else {
7497 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7498 if (stats->nr_items > i)
7499 stats->values[i] = btrfs_dev_stat_read(dev, i);
7500 }
7501 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7502 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7503 return 0;
7504}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007505
David Sterbada353f62017-02-14 17:55:53 +01007506void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007507{
7508 struct buffer_head *bh;
7509 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007510 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007511
Anand Jain12b1c262015-08-14 18:32:59 +08007512 if (!bdev)
7513 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007514
Anand Jain12b1c262015-08-14 18:32:59 +08007515 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7516 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007517
Anand Jain12b1c262015-08-14 18:32:59 +08007518 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7519 continue;
7520
7521 disk_super = (struct btrfs_super_block *)bh->b_data;
7522
7523 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7524 set_buffer_dirty(bh);
7525 sync_dirty_buffer(bh);
7526 brelse(bh);
7527 }
7528
7529 /* Notify udev that device has changed */
7530 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7531
7532 /* Update ctime/mtime for device path for libblkid */
7533 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007534}
Miao Xie935e5cc2014-09-03 21:35:33 +08007535
7536/*
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007537 * Update the size and bytes used for each device where it changed. This is
7538 * delayed since we would otherwise get errors while writing out the
7539 * superblocks.
7540 *
7541 * Must be invoked during transaction commit.
Miao Xie935e5cc2014-09-03 21:35:33 +08007542 */
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007543void btrfs_commit_device_sizes(struct btrfs_transaction *trans)
Miao Xie935e5cc2014-09-03 21:35:33 +08007544{
Miao Xie935e5cc2014-09-03 21:35:33 +08007545 struct btrfs_device *curr, *next;
7546
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007547 ASSERT(trans->state == TRANS_STATE_COMMIT_DOING);
7548
7549 if (list_empty(&trans->dev_update_list))
Miao Xie935e5cc2014-09-03 21:35:33 +08007550 return;
7551
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007552 /*
7553 * We don't need the device_list_mutex here. This list is owned by the
7554 * transaction and the transaction must complete before the device is
7555 * released.
7556 */
7557 mutex_lock(&trans->fs_info->chunk_mutex);
7558 list_for_each_entry_safe(curr, next, &trans->dev_update_list,
7559 post_commit_list) {
7560 list_del_init(&curr->post_commit_list);
Miao Xie935e5cc2014-09-03 21:35:33 +08007561 curr->commit_total_bytes = curr->disk_total_bytes;
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007562 curr->commit_bytes_used = curr->bytes_used;
Miao Xie935e5cc2014-09-03 21:35:33 +08007563 }
Nikolay Borisovbbbf7242019-03-25 14:31:22 +02007564 mutex_unlock(&trans->fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007565}
Anand Jain5a13f432015-03-10 06:38:31 +08007566
7567void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7568{
7569 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7570 while (fs_devices) {
7571 fs_devices->fs_info = fs_info;
7572 fs_devices = fs_devices->seed;
7573 }
7574}
7575
7576void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7577{
7578 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7579 while (fs_devices) {
7580 fs_devices->fs_info = NULL;
7581 fs_devices = fs_devices->seed;
7582 }
7583}
David Sterba46df06b2018-07-13 20:46:30 +02007584
7585/*
7586 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7587 */
7588int btrfs_bg_type_to_factor(u64 flags)
7589{
David Sterba44b28ad2019-05-17 11:43:31 +02007590 const int index = btrfs_bg_flags_to_raid_index(flags);
7591
7592 return btrfs_raid_array[index].ncopies;
David Sterba46df06b2018-07-13 20:46:30 +02007593}
Qu Wenruocf90d882018-08-01 10:37:19 +08007594
7595
Qu Wenruocf90d882018-08-01 10:37:19 +08007596
7597static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7598 u64 chunk_offset, u64 devid,
7599 u64 physical_offset, u64 physical_len)
7600{
David Sterbac8bf1b62019-05-17 11:43:17 +02007601 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007602 struct extent_map *em;
7603 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007604 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007605 u64 stripe_len;
7606 bool found = false;
7607 int ret = 0;
7608 int i;
7609
7610 read_lock(&em_tree->lock);
7611 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7612 read_unlock(&em_tree->lock);
7613
7614 if (!em) {
7615 btrfs_err(fs_info,
7616"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7617 physical_offset, devid);
7618 ret = -EUCLEAN;
7619 goto out;
7620 }
7621
7622 map = em->map_lookup;
7623 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7624 if (physical_len != stripe_len) {
7625 btrfs_err(fs_info,
7626"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7627 physical_offset, devid, em->start, physical_len,
7628 stripe_len);
7629 ret = -EUCLEAN;
7630 goto out;
7631 }
7632
7633 for (i = 0; i < map->num_stripes; i++) {
7634 if (map->stripes[i].dev->devid == devid &&
7635 map->stripes[i].physical == physical_offset) {
7636 found = true;
7637 if (map->verified_stripes >= map->num_stripes) {
7638 btrfs_err(fs_info,
7639 "too many dev extents for chunk %llu found",
7640 em->start);
7641 ret = -EUCLEAN;
7642 goto out;
7643 }
7644 map->verified_stripes++;
7645 break;
7646 }
7647 }
7648 if (!found) {
7649 btrfs_err(fs_info,
7650 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7651 physical_offset, devid);
7652 ret = -EUCLEAN;
7653 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007654
7655 /* Make sure no dev extent is beyond device bondary */
Anand Jain09ba3bc2019-01-19 14:48:55 +08007656 dev = btrfs_find_device(fs_info->fs_devices, devid, NULL, NULL, true);
Qu Wenruo05a37c42018-10-05 17:45:55 +08007657 if (!dev) {
7658 btrfs_err(fs_info, "failed to find devid %llu", devid);
7659 ret = -EUCLEAN;
7660 goto out;
7661 }
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007662
7663 /* It's possible this device is a dummy for seed device */
7664 if (dev->disk_total_bytes == 0) {
Anand Jain09ba3bc2019-01-19 14:48:55 +08007665 dev = btrfs_find_device(fs_info->fs_devices->seed, devid, NULL,
7666 NULL, false);
Qu Wenruo1b3922a2019-01-08 14:08:18 +08007667 if (!dev) {
7668 btrfs_err(fs_info, "failed to find seed devid %llu",
7669 devid);
7670 ret = -EUCLEAN;
7671 goto out;
7672 }
7673 }
7674
Qu Wenruo05a37c42018-10-05 17:45:55 +08007675 if (physical_offset + physical_len > dev->disk_total_bytes) {
7676 btrfs_err(fs_info,
7677"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7678 devid, physical_offset, physical_len,
7679 dev->disk_total_bytes);
7680 ret = -EUCLEAN;
7681 goto out;
7682 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007683out:
7684 free_extent_map(em);
7685 return ret;
7686}
7687
7688static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7689{
David Sterbac8bf1b62019-05-17 11:43:17 +02007690 struct extent_map_tree *em_tree = &fs_info->mapping_tree;
Qu Wenruocf90d882018-08-01 10:37:19 +08007691 struct extent_map *em;
7692 struct rb_node *node;
7693 int ret = 0;
7694
7695 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007696 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007697 em = rb_entry(node, struct extent_map, rb_node);
7698 if (em->map_lookup->num_stripes !=
7699 em->map_lookup->verified_stripes) {
7700 btrfs_err(fs_info,
7701 "chunk %llu has missing dev extent, have %d expect %d",
7702 em->start, em->map_lookup->verified_stripes,
7703 em->map_lookup->num_stripes);
7704 ret = -EUCLEAN;
7705 goto out;
7706 }
7707 }
7708out:
7709 read_unlock(&em_tree->lock);
7710 return ret;
7711}
7712
7713/*
7714 * Ensure that all dev extents are mapped to correct chunk, otherwise
7715 * later chunk allocation/free would cause unexpected behavior.
7716 *
7717 * NOTE: This will iterate through the whole device tree, which should be of
7718 * the same size level as the chunk tree. This slightly increases mount time.
7719 */
7720int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7721{
7722 struct btrfs_path *path;
7723 struct btrfs_root *root = fs_info->dev_root;
7724 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007725 u64 prev_devid = 0;
7726 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007727 int ret = 0;
7728
7729 key.objectid = 1;
7730 key.type = BTRFS_DEV_EXTENT_KEY;
7731 key.offset = 0;
7732
7733 path = btrfs_alloc_path();
7734 if (!path)
7735 return -ENOMEM;
7736
7737 path->reada = READA_FORWARD;
7738 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7739 if (ret < 0)
7740 goto out;
7741
7742 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7743 ret = btrfs_next_item(root, path);
7744 if (ret < 0)
7745 goto out;
7746 /* No dev extents at all? Not good */
7747 if (ret > 0) {
7748 ret = -EUCLEAN;
7749 goto out;
7750 }
7751 }
7752 while (1) {
7753 struct extent_buffer *leaf = path->nodes[0];
7754 struct btrfs_dev_extent *dext;
7755 int slot = path->slots[0];
7756 u64 chunk_offset;
7757 u64 physical_offset;
7758 u64 physical_len;
7759 u64 devid;
7760
7761 btrfs_item_key_to_cpu(leaf, &key, slot);
7762 if (key.type != BTRFS_DEV_EXTENT_KEY)
7763 break;
7764 devid = key.objectid;
7765 physical_offset = key.offset;
7766
7767 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7768 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7769 physical_len = btrfs_dev_extent_length(leaf, dext);
7770
Qu Wenruo5eb19382018-10-05 17:45:54 +08007771 /* Check if this dev extent overlaps with the previous one */
7772 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7773 btrfs_err(fs_info,
7774"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7775 devid, physical_offset, prev_dev_ext_end);
7776 ret = -EUCLEAN;
7777 goto out;
7778 }
7779
Qu Wenruocf90d882018-08-01 10:37:19 +08007780 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7781 physical_offset, physical_len);
7782 if (ret < 0)
7783 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007784 prev_devid = devid;
7785 prev_dev_ext_end = physical_offset + physical_len;
7786
Qu Wenruocf90d882018-08-01 10:37:19 +08007787 ret = btrfs_next_item(root, path);
7788 if (ret < 0)
7789 goto out;
7790 if (ret > 0) {
7791 ret = 0;
7792 break;
7793 }
7794 }
7795
7796 /* Ensure all chunks have corresponding dev extents */
7797 ret = verify_chunk_dev_extent_mapping(fs_info);
7798out:
7799 btrfs_free_path(path);
7800 return ret;
7801}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007802
7803/*
7804 * Check whether the given block group or device is pinned by any inode being
7805 * used as a swapfile.
7806 */
7807bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7808{
7809 struct btrfs_swapfile_pin *sp;
7810 struct rb_node *node;
7811
7812 spin_lock(&fs_info->swapfile_pins_lock);
7813 node = fs_info->swapfile_pins.rb_node;
7814 while (node) {
7815 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7816 if (ptr < sp->ptr)
7817 node = node->rb_left;
7818 else if (ptr > sp->ptr)
7819 node = node->rb_right;
7820 else
7821 break;
7822 }
7823 spin_unlock(&fs_info->swapfile_pins_lock);
7824 return node != NULL;
7825}