blob: 459cc2a2a31c0ef7456e60a751fe6c3d94d5c56c [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"
Chris Mason0b86a832008-03-24 15:01:56 -040030
Zhao Leiaf902042015-09-15 21:08:06 +080031const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
32 [BTRFS_RAID_RAID10] = {
33 .sub_stripes = 2,
34 .dev_stripes = 1,
35 .devs_max = 0, /* 0 == as many as possible */
36 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080037 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080038 .devs_increment = 2,
39 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +080040 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080041 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080042 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080043 },
44 [BTRFS_RAID_RAID1] = {
45 .sub_stripes = 1,
46 .dev_stripes = 1,
47 .devs_max = 2,
48 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080049 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080050 .devs_increment = 2,
51 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +080052 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080053 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080054 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080055 },
56 [BTRFS_RAID_DUP] = {
57 .sub_stripes = 1,
58 .dev_stripes = 2,
59 .devs_max = 1,
60 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080061 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080062 .devs_increment = 1,
63 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +080064 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080065 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080066 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080067 },
68 [BTRFS_RAID_RAID0] = {
69 .sub_stripes = 1,
70 .dev_stripes = 1,
71 .devs_max = 0,
72 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080073 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080074 .devs_increment = 1,
75 .ncopies = 1,
Anand Jained234672018-04-25 19:01:42 +080076 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +080077 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080078 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080079 },
80 [BTRFS_RAID_SINGLE] = {
81 .sub_stripes = 1,
82 .dev_stripes = 1,
83 .devs_max = 1,
84 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080085 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080086 .devs_increment = 1,
87 .ncopies = 1,
Anand Jained234672018-04-25 19:01:42 +080088 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +080089 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080090 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080091 },
92 [BTRFS_RAID_RAID5] = {
93 .sub_stripes = 1,
94 .dev_stripes = 1,
95 .devs_max = 0,
96 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080097 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080098 .devs_increment = 1,
99 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +0800100 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800101 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800102 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800103 },
104 [BTRFS_RAID_RAID6] = {
105 .sub_stripes = 1,
106 .dev_stripes = 1,
107 .devs_max = 0,
108 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800109 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800110 .devs_increment = 1,
111 .ncopies = 3,
Anand Jained234672018-04-25 19:01:42 +0800112 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800113 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800114 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800115 },
116};
117
Anand Jained234672018-04-25 19:01:42 +0800118const char *get_raid_name(enum btrfs_raid_types type)
119{
120 if (type >= BTRFS_NR_RAID_TYPES)
121 return NULL;
122
123 return btrfs_raid_array[type].raid_name;
124}
125
Yan Zheng2b820322008-11-17 21:11:30 -0500126static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100127 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400128static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200129static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000130static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200131static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700132static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
133 enum btrfs_map_op op,
134 u64 logical, u64 *length,
135 struct btrfs_bio **bbio_ret,
136 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500137
David Sterba9c6b1c42017-06-16 22:30:00 +0200138/*
139 * Device locking
140 * ==============
141 *
142 * There are several mutexes that protect manipulation of devices and low-level
143 * structures like chunks but not block groups, extents or files
144 *
145 * uuid_mutex (global lock)
146 * ------------------------
147 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
148 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
149 * device) or requested by the device= mount option
150 *
151 * the mutex can be very coarse and can cover long-running operations
152 *
153 * protects: updates to fs_devices counters like missing devices, rw devices,
154 * seeding, structure cloning, openning/closing devices at mount/umount time
155 *
156 * global::fs_devs - add, remove, updates to the global list
157 *
158 * does not protect: manipulation of the fs_devices::devices list!
159 *
160 * btrfs_device::name - renames (write side), read is RCU
161 *
162 * fs_devices::device_list_mutex (per-fs, with RCU)
163 * ------------------------------------------------
164 * protects updates to fs_devices::devices, ie. adding and deleting
165 *
166 * simple list traversal with read-only actions can be done with RCU protection
167 *
168 * may be used to exclude some operations from running concurrently without any
169 * modifications to the list (see write_all_supers)
170 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200171 * balance_mutex
172 * -------------
173 * protects balance structures (status, state) and context accessed from
174 * several places (internally, ioctl)
175 *
176 * chunk_mutex
177 * -----------
178 * protects chunks, adding or removing during allocation, trim or when a new
179 * device is added/removed
180 *
181 * cleaner_mutex
182 * -------------
183 * a big lock that is held by the cleaner thread and prevents running subvolume
184 * cleaning together with relocation or delayed iputs
185 *
186 *
187 * Lock nesting
188 * ============
189 *
190 * uuid_mutex
191 * volume_mutex
192 * device_list_mutex
193 * chunk_mutex
194 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800195 *
196 *
197 * Exclusive operations, BTRFS_FS_EXCL_OP
198 * ======================================
199 *
200 * Maintains the exclusivity of the following operations that apply to the
201 * whole filesystem and cannot run in parallel.
202 *
203 * - Balance (*)
204 * - Device add
205 * - Device remove
206 * - Device replace (*)
207 * - Resize
208 *
209 * The device operations (as above) can be in one of the following states:
210 *
211 * - Running state
212 * - Paused state
213 * - Completed state
214 *
215 * Only device operations marked with (*) can go into the Paused state for the
216 * following reasons:
217 *
218 * - ioctl (only Balance can be Paused through ioctl)
219 * - filesystem remounted as read-only
220 * - filesystem unmounted and mounted as read-only
221 * - system power-cycle and filesystem mounted as read-only
222 * - filesystem or device errors leading to forced read-only
223 *
224 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
225 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
226 * A device operation in Paused or Running state can be canceled or resumed
227 * either by ioctl (Balance only) or when remounted as read-write.
228 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
229 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200230 */
231
Miao Xie67a2c452014-09-03 21:35:43 +0800232DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400233static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800234struct list_head *btrfs_get_fs_uuids(void)
235{
236 return &fs_uuids;
237}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400238
David Sterba2dfeca92017-06-14 02:48:07 +0200239/*
240 * alloc_fs_devices - allocate struct btrfs_fs_devices
241 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
242 *
243 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
244 * The returned struct is not linked onto any lists and can be destroyed with
245 * kfree() right away.
246 */
247static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300248{
249 struct btrfs_fs_devices *fs_devs;
250
David Sterba78f2c9e2016-02-11 14:25:38 +0100251 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300252 if (!fs_devs)
253 return ERR_PTR(-ENOMEM);
254
255 mutex_init(&fs_devs->device_list_mutex);
256
257 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800258 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300259 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800260 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300261 if (fsid)
262 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300263
264 return fs_devs;
265}
266
David Sterbaa425f9d2018-03-20 15:47:33 +0100267void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100268{
269 rcu_string_free(device->name);
270 bio_put(device->flush_bio);
271 kfree(device);
272}
273
Yan Zhenge4404d62008-12-12 10:03:26 -0500274static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
275{
276 struct btrfs_device *device;
277 WARN_ON(fs_devices->opened);
278 while (!list_empty(&fs_devices->devices)) {
279 device = list_entry(fs_devices->devices.next,
280 struct btrfs_device, dev_list);
281 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100282 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500283 }
284 kfree(fs_devices);
285}
286
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000287static void btrfs_kobject_uevent(struct block_device *bdev,
288 enum kobject_action action)
289{
290 int ret;
291
292 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
293 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500294 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000295 action,
296 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
297 &disk_to_dev(bdev->bd_disk)->kobj);
298}
299
David Sterbaffc5a372018-02-19 17:24:15 +0100300void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400301{
302 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400303
Yan Zheng2b820322008-11-17 21:11:30 -0500304 while (!list_empty(&fs_uuids)) {
305 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800306 struct btrfs_fs_devices, fs_list);
307 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500308 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400309 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400310}
311
David Sterba48dae9c2017-10-30 18:10:25 +0100312/*
313 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
314 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100315 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100316 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300317static struct btrfs_device *__alloc_device(void)
318{
319 struct btrfs_device *dev;
320
David Sterba78f2c9e2016-02-11 14:25:38 +0100321 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300322 if (!dev)
323 return ERR_PTR(-ENOMEM);
324
David Sterbae0ae9992017-06-06 17:06:06 +0200325 /*
326 * Preallocate a bio that's always going to be used for flushing device
327 * barriers and matches the device lifespan
328 */
329 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
330 if (!dev->flush_bio) {
331 kfree(dev);
332 return ERR_PTR(-ENOMEM);
333 }
David Sterbae0ae9992017-06-06 17:06:06 +0200334
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300335 INIT_LIST_HEAD(&dev->dev_list);
336 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800337 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300338
339 spin_lock_init(&dev->io_lock);
340
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300341 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800342 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100343 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700344 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800345 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300346
347 return dev;
348}
349
David Sterba35c70102017-06-15 19:51:51 +0200350/*
351 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
352 * return NULL.
353 *
354 * If devid and uuid are both specified, the match must be exact, otherwise
355 * only devid is used.
356 */
357static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
358 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359{
360 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400361
Anand Jain636d2c92018-04-12 10:29:29 +0800362 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400363 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400364 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400365 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400366 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400367 }
368 return NULL;
369}
370
Chris Masona1b32a52008-09-05 16:09:51 -0400371static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400373 struct btrfs_fs_devices *fs_devices;
374
Anand Jainc4babc52018-04-12 10:29:25 +0800375 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400376 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
377 return fs_devices;
378 }
379 return NULL;
380}
381
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100382static int
383btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
384 int flush, struct block_device **bdev,
385 struct buffer_head **bh)
386{
387 int ret;
388
389 *bdev = blkdev_get_by_path(device_path, flags, holder);
390
391 if (IS_ERR(*bdev)) {
392 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100393 goto error;
394 }
395
396 if (flush)
397 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200398 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100399 if (ret) {
400 blkdev_put(*bdev, flags);
401 goto error;
402 }
403 invalidate_bdev(*bdev);
404 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800405 if (IS_ERR(*bh)) {
406 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100407 blkdev_put(*bdev, flags);
408 goto error;
409 }
410
411 return 0;
412
413error:
414 *bdev = NULL;
415 *bh = NULL;
416 return ret;
417}
418
Chris Masonffbd5172009-04-20 15:50:09 -0400419static void requeue_list(struct btrfs_pending_bios *pending_bios,
420 struct bio *head, struct bio *tail)
421{
422
423 struct bio *old_head;
424
425 old_head = pending_bios->head;
426 pending_bios->head = head;
427 if (pending_bios->tail)
428 tail->bi_next = old_head;
429 else
430 pending_bios->tail = tail;
431}
432
Chris Mason8b712842008-06-11 16:50:36 -0400433/*
434 * we try to collect pending bios for a device so we don't get a large
435 * number of procs sending bios down to the same device. This greatly
436 * improves the schedulers ability to collect and merge the bios.
437 *
438 * But, it also turns into a long list of bios to process and that is sure
439 * to eventually make the worker thread block. The solution here is to
440 * make some progress and then put this work struct back at the end of
441 * the list if the block device is congested. This way, multiple devices
442 * can make progress from a single worker thread.
443 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100444static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400445{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400447 struct bio *pending;
448 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400449 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400450 struct bio *tail;
451 struct bio *cur;
452 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400453 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400454 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400455 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400456 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000457 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400458 struct blk_plug plug;
459
460 /*
461 * this function runs all the bios we've collected for
462 * a particular device. We don't want to wander off to
463 * another device without first sending all of these down.
464 * So, setup a plug here and finish it off before we return
465 */
466 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400467
Jan Karaefa7c9f2017-02-02 15:56:53 +0100468 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400469
Chris Mason8b712842008-06-11 16:50:36 -0400470loop:
471 spin_lock(&device->io_lock);
472
Chris Masona6837052009-02-04 09:19:41 -0500473loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400474 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400475
Chris Mason8b712842008-06-11 16:50:36 -0400476 /* take all the bios off the list at once and process them
477 * later on (without the lock held). But, remember the
478 * tail and other pointers so the bios can be properly reinserted
479 * into the list if we hit congestion
480 */
Chris Masond84275c2009-06-09 15:39:08 -0400481 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400482 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400483 force_reg = 1;
484 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400485 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400486 force_reg = 0;
487 }
Chris Masonffbd5172009-04-20 15:50:09 -0400488
489 pending = pending_bios->head;
490 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400491 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400492
493 /*
494 * if pending was null this time around, no bios need processing
495 * at all and we can stop. Otherwise it'll loop back up again
496 * and do an additional check so no bios are missed.
497 *
498 * device->running_pending is used to synchronize with the
499 * schedule_bio code.
500 */
Chris Masonffbd5172009-04-20 15:50:09 -0400501 if (device->pending_sync_bios.head == NULL &&
502 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400503 again = 0;
504 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400505 } else {
506 again = 1;
507 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400508 }
Chris Masonffbd5172009-04-20 15:50:09 -0400509
510 pending_bios->head = NULL;
511 pending_bios->tail = NULL;
512
Chris Mason8b712842008-06-11 16:50:36 -0400513 spin_unlock(&device->io_lock);
514
Chris Masond3977122009-01-05 21:25:51 -0500515 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400516
517 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400518 /* we want to work on both lists, but do more bios on the
519 * sync list than the regular list
520 */
521 if ((num_run > 32 &&
522 pending_bios != &device->pending_sync_bios &&
523 device->pending_sync_bios.head) ||
524 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
525 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400526 spin_lock(&device->io_lock);
527 requeue_list(pending_bios, pending, tail);
528 goto loop_lock;
529 }
530
Chris Mason8b712842008-06-11 16:50:36 -0400531 cur = pending;
532 pending = pending->bi_next;
533 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400534
Jens Axboedac56212015-04-17 16:23:59 -0600535 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400536
Chris Mason2ab1ba62011-08-04 14:28:36 -0400537 /*
538 * if we're doing the sync list, record that our
539 * plug has some sync requests on it
540 *
541 * If we're doing the regular list and there are
542 * sync requests sitting around, unplug before
543 * we add more
544 */
545 if (pending_bios == &device->pending_sync_bios) {
546 sync_pending = 1;
547 } else if (sync_pending) {
548 blk_finish_plug(&plug);
549 blk_start_plug(&plug);
550 sync_pending = 0;
551 }
552
Mike Christie4e49ea42016-06-05 14:31:41 -0500553 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400554 num_run++;
555 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100556
557 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400558
559 /*
560 * we made progress, there is more work to do and the bdi
561 * is now congested. Back off and let other work structs
562 * run instead
563 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400564 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500565 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400566 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400567
Chris Masonb765ead2009-04-03 10:27:10 -0400568 ioc = current->io_context;
569
570 /*
571 * the main goal here is that we don't want to
572 * block if we're going to be able to submit
573 * more requests without blocking.
574 *
575 * This code does two great things, it pokes into
576 * the elevator code from a filesystem _and_
577 * it makes assumptions about how batching works.
578 */
579 if (ioc && ioc->nr_batch_requests > 0 &&
580 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
581 (last_waited == 0 ||
582 ioc->last_waited == last_waited)) {
583 /*
584 * we want to go through our batch of
585 * requests and stop. So, we copy out
586 * the ioc->last_waited time and test
587 * against it before looping
588 */
589 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100590 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400591 continue;
592 }
Chris Mason8b712842008-06-11 16:50:36 -0400593 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400594 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500595 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400596
597 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800598 btrfs_queue_work(fs_info->submit_workers,
599 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400600 goto done;
601 }
602 }
Chris Masonffbd5172009-04-20 15:50:09 -0400603
Chris Mason51684082010-03-10 15:33:32 -0500604 cond_resched();
605 if (again)
606 goto loop;
607
608 spin_lock(&device->io_lock);
609 if (device->pending_bios.head || device->pending_sync_bios.head)
610 goto loop_lock;
611 spin_unlock(&device->io_lock);
612
Chris Mason8b712842008-06-11 16:50:36 -0400613done:
Chris Mason211588a2011-04-19 20:12:40 -0400614 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400615}
616
Christoph Hellwigb2950862008-12-02 09:54:17 -0500617static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400618{
619 struct btrfs_device *device;
620
621 device = container_of(work, struct btrfs_device, work);
622 run_scheduled_bios(device);
623}
624
Anand Jaind8367db2018-01-18 22:00:37 +0800625/*
626 * Search and remove all stale (devices which are not mounted) devices.
627 * When both inputs are NULL, it will search and release all stale devices.
628 * path: Optional. When provided will it release all unmounted devices
629 * matching this path only.
630 * skip_dev: Optional. Will skip this device when searching for the stale
631 * devices.
632 */
633static void btrfs_free_stale_devices(const char *path,
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800634 struct btrfs_device *skip_device)
Anand Jain4fde46f2015-06-17 21:10:48 +0800635{
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800636 struct btrfs_fs_devices *fs_devices, *tmp_fs_devices;
637 struct btrfs_device *device, *tmp_device;
Anand Jain4fde46f2015-06-17 21:10:48 +0800638
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800639 list_for_each_entry_safe(fs_devices, tmp_fs_devices, &fs_uuids, fs_list) {
Anand Jain7bcb8162018-05-29 17:23:20 +0800640 mutex_lock(&fs_devices->device_list_mutex);
641 if (fs_devices->opened) {
642 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain4fde46f2015-06-17 21:10:48 +0800643 continue;
Anand Jain7bcb8162018-05-29 17:23:20 +0800644 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800645
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800646 list_for_each_entry_safe(device, tmp_device,
647 &fs_devices->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800648 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800649
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800650 if (skip_device && skip_device == device)
Anand Jaind8367db2018-01-18 22:00:37 +0800651 continue;
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800652 if (path && !device->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800653 continue;
654
Anand Jain4fde46f2015-06-17 21:10:48 +0800655 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800656 if (path)
Anand Jainfa6d2ae2018-05-29 15:33:08 +0800657 not_found = strcmp(rcu_str_deref(device->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800658 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800659 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800660 if (not_found)
661 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800662
Anand Jain4fde46f2015-06-17 21:10:48 +0800663 /* delete the stale device */
Anand Jain7bcb8162018-05-29 17:23:20 +0800664 fs_devices->num_devices--;
665 list_del(&device->dev_list);
666 btrfs_free_device(device);
667
668 if (fs_devices->num_devices == 0)
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200669 break;
Anand Jain7bcb8162018-05-29 17:23:20 +0800670 }
671 mutex_unlock(&fs_devices->device_list_mutex);
672 if (fs_devices->num_devices == 0) {
673 btrfs_sysfs_remove_fsid(fs_devices);
674 list_del(&fs_devices->fs_list);
675 free_fs_devices(fs_devices);
Anand Jain4fde46f2015-06-17 21:10:48 +0800676 }
677 }
678}
679
Anand Jain0fb08bc2017-11-09 23:45:24 +0800680static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
681 struct btrfs_device *device, fmode_t flags,
682 void *holder)
683{
684 struct request_queue *q;
685 struct block_device *bdev;
686 struct buffer_head *bh;
687 struct btrfs_super_block *disk_super;
688 u64 devid;
689 int ret;
690
691 if (device->bdev)
692 return -EINVAL;
693 if (!device->name)
694 return -EINVAL;
695
696 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
697 &bdev, &bh);
698 if (ret)
699 return ret;
700
701 disk_super = (struct btrfs_super_block *)bh->b_data;
702 devid = btrfs_stack_device_id(&disk_super->dev_item);
703 if (devid != device->devid)
704 goto error_brelse;
705
706 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
707 goto error_brelse;
708
709 device->generation = btrfs_super_generation(disk_super);
710
711 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Anand Jainebbede42017-12-04 12:54:52 +0800712 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800713 fs_devices->seeding = 1;
714 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800715 if (bdev_read_only(bdev))
716 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
717 else
718 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800719 }
720
721 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800722 if (!blk_queue_nonrot(q))
723 fs_devices->rotating = 1;
724
725 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800726 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800727 device->mode = flags;
728
729 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800730 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
731 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800732 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800733 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800734 }
735 brelse(bh);
736
737 return 0;
738
739error_brelse:
740 brelse(bh);
741 blkdev_put(bdev, flags);
742
743 return -EINVAL;
744}
745
David Sterba60999ca2014-03-26 18:26:36 +0100746/*
747 * Add new device to list of registered devices
748 *
749 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800750 * device pointer which was just added or updated when successful
751 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100752 */
Anand Jaine124ece2018-01-18 22:02:35 +0800753static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain4306a972018-05-29 12:28:37 +0800754 struct btrfs_super_block *disk_super,
755 bool *new_device_added)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756{
757 struct btrfs_device *device;
758 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400759 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800761 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400762
763 fs_devices = find_fsid(disk_super->fsid);
764 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300765 fs_devices = alloc_fs_devices(disk_super->fsid);
766 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800767 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300768
Anand Jain9c6d1732018-05-29 14:10:20 +0800769 mutex_lock(&fs_devices->device_list_mutex);
Anand Jainc4babc52018-04-12 10:29:25 +0800770 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300771
Chris Mason8a4b83c2008-03-24 15:02:07 -0400772 device = NULL;
773 } else {
Anand Jain9c6d1732018-05-29 14:10:20 +0800774 mutex_lock(&fs_devices->device_list_mutex);
David Sterba35c70102017-06-15 19:51:51 +0200775 device = find_device(fs_devices, devid,
776 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400777 }
Miao Xie443f24f2014-07-24 11:37:15 +0800778
Chris Mason8a4b83c2008-03-24 15:02:07 -0400779 if (!device) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800780 if (fs_devices->opened) {
781 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800782 return ERR_PTR(-EBUSY);
Anand Jain9c6d1732018-05-29 14:10:20 +0800783 }
Yan Zheng2b820322008-11-17 21:11:30 -0500784
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300785 device = btrfs_alloc_device(NULL, &devid,
786 disk_super->dev_item.uuid);
787 if (IS_ERR(device)) {
Anand Jain9c6d1732018-05-29 14:10:20 +0800788 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400789 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800790 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400791 }
Josef Bacik606686e2012-06-04 14:03:51 -0400792
793 name = rcu_string_strdup(path, GFP_NOFS);
794 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100795 btrfs_free_device(device);
Anand Jain9c6d1732018-05-29 14:10:20 +0800796 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800797 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400798 }
Josef Bacik606686e2012-06-04 14:03:51 -0400799 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200800
Xiao Guangrong1f781602011-04-20 10:09:16 +0000801 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100802 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400803
Yan Zheng2b820322008-11-17 21:11:30 -0500804 device->fs_devices = fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +0800805 *new_device_added = true;
Anand Jain327f18c2018-01-18 22:02:33 +0800806
807 if (disk_super->label[0])
808 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
809 disk_super->label, devid, found_transid, path);
810 else
811 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
812 disk_super->fsid, devid, found_transid, path);
813
Josef Bacik606686e2012-06-04 14:03:51 -0400814 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800815 /*
816 * When FS is already mounted.
817 * 1. If you are here and if the device->name is NULL that
818 * means this device was missing at time of FS mount.
819 * 2. If you are here and if the device->name is different
820 * from 'path' that means either
821 * a. The same device disappeared and reappeared with
822 * different name. or
823 * b. The missing-disk-which-was-replaced, has
824 * reappeared now.
825 *
826 * We must allow 1 and 2a above. But 2b would be a spurious
827 * and unintentional.
828 *
829 * Further in case of 1 and 2a above, the disk at 'path'
830 * would have missed some transaction when it was away and
831 * in case of 2a the stale bdev has to be updated as well.
832 * 2b must not be allowed at all time.
833 */
834
835 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700836 * For now, we do allow update to btrfs_fs_device through the
837 * btrfs dev scan cli after FS has been mounted. We're still
838 * tracking a problem where systems fail mount by subvolume id
839 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800840 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700841 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800842 /*
843 * That is if the FS is _not_ mounted and if you
844 * are here, that means there is more than one
845 * disk with same uuid and devid.We keep the one
846 * with larger generation number or the last-in if
847 * generation are equal.
848 */
Anand Jain9c6d1732018-05-29 14:10:20 +0800849 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800850 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800851 }
Anand Jainb96de002014-07-03 18:22:05 +0800852
Josef Bacik606686e2012-06-04 14:03:51 -0400853 name = rcu_string_strdup(path, GFP_NOFS);
Anand Jain9c6d1732018-05-29 14:10:20 +0800854 if (!name) {
855 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800856 return ERR_PTR(-ENOMEM);
Anand Jain9c6d1732018-05-29 14:10:20 +0800857 }
Josef Bacik606686e2012-06-04 14:03:51 -0400858 rcu_string_free(device->name);
859 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800860 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500861 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800862 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500863 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400864 }
865
Anand Jain77bdae42014-07-03 18:22:06 +0800866 /*
867 * Unmount does not free the btrfs_device struct but would zero
868 * generation along with most of the other members. So just update
869 * it back. We need it to pick the disk with largest generation
870 * (as above).
871 */
872 if (!fs_devices->opened)
873 device->generation = found_transid;
874
Anand Jainf2788d22018-01-18 22:02:34 +0800875 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
876
Anand Jain9c6d1732018-05-29 14:10:20 +0800877 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jaine124ece2018-01-18 22:02:35 +0800878 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400879}
880
Yan Zhenge4404d62008-12-12 10:03:26 -0500881static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
882{
883 struct btrfs_fs_devices *fs_devices;
884 struct btrfs_device *device;
885 struct btrfs_device *orig_dev;
886
Ilya Dryomov2208a372013-08-12 14:33:03 +0300887 fs_devices = alloc_fs_devices(orig->fsid);
888 if (IS_ERR(fs_devices))
889 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500890
Miao Xieadbbb862014-09-03 21:35:42 +0800891 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400892 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500893
Xiao Guangrong46224702011-04-20 10:08:47 +0000894 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500895 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400896 struct rcu_string *name;
897
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300898 device = btrfs_alloc_device(NULL, &orig_dev->devid,
899 orig_dev->uuid);
900 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500901 goto error;
902
Josef Bacik606686e2012-06-04 14:03:51 -0400903 /*
904 * This is ok to do without rcu read locked because we hold the
905 * uuid mutex so nothing we touch in here is going to disappear.
906 */
Anand Jaine755f782014-06-30 17:12:47 +0800907 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100908 name = rcu_string_strdup(orig_dev->name->str,
909 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800910 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100911 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800912 goto error;
913 }
914 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400915 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500916
Yan Zhenge4404d62008-12-12 10:03:26 -0500917 list_add(&device->dev_list, &fs_devices->devices);
918 device->fs_devices = fs_devices;
919 fs_devices->num_devices++;
920 }
Miao Xieadbbb862014-09-03 21:35:42 +0800921 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500922 return fs_devices;
923error:
Miao Xieadbbb862014-09-03 21:35:42 +0800924 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500925 free_fs_devices(fs_devices);
926 return ERR_PTR(-ENOMEM);
927}
928
Anand Jain9b99b112018-02-27 12:41:59 +0800929/*
930 * After we have read the system tree and know devids belonging to
931 * this filesystem, remove the device which does not belong there.
932 */
933void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400934{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500935 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800936 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500937
Chris Masondfe25022008-05-13 13:46:40 -0400938 mutex_lock(&uuid_mutex);
939again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000940 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500941 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +0800942 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
943 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +0800944 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
945 &device->dev_state) &&
946 (!latest_dev ||
947 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +0800948 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500949 }
Yan Zheng2b820322008-11-17 21:11:30 -0500950 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500951 }
Yan Zheng2b820322008-11-17 21:11:30 -0500952
Stefan Behrens8dabb742012-11-06 13:15:27 +0100953 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
954 /*
955 * In the first step, keep the device which has
956 * the correct fsid and the devid that is used
957 * for the dev_replace procedure.
958 * In the second step, the dev_replace state is
959 * read from the device tree and it is known
960 * whether the procedure is really active or
961 * not, which means whether this device is
962 * used or whether it should be removed.
963 */
Anand Jain401e29c2017-12-04 12:54:55 +0800964 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
965 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +0100966 continue;
967 }
968 }
Yan Zheng2b820322008-11-17 21:11:30 -0500969 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100970 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500971 device->bdev = NULL;
972 fs_devices->open_devices--;
973 }
Anand Jainebbede42017-12-04 12:54:52 +0800974 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500975 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +0800976 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +0800977 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
978 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +0100979 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500980 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500981 list_del_init(&device->dev_list);
982 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +0100983 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400984 }
Yan Zheng2b820322008-11-17 21:11:30 -0500985
986 if (fs_devices->seed) {
987 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500988 goto again;
989 }
990
Miao Xie443f24f2014-07-24 11:37:15 +0800991 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500992
Chris Masondfe25022008-05-13 13:46:40 -0400993 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400994}
Chris Masona0af4692008-05-13 16:03:06 -0400995
David Sterbaf06c5962017-06-06 17:08:23 +0200996static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000997{
998 struct btrfs_device *device;
999
1000 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +01001001 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001002}
1003
Anand Jain14238812016-07-22 06:04:53 +08001004static void btrfs_close_bdev(struct btrfs_device *device)
1005{
David Sterba08ffcae2017-06-19 16:55:35 +02001006 if (!device->bdev)
1007 return;
1008
Anand Jainebbede42017-12-04 12:54:52 +08001009 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001010 sync_blockdev(device->bdev);
1011 invalidate_bdev(device->bdev);
1012 }
1013
David Sterba08ffcae2017-06-19 16:55:35 +02001014 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001015}
1016
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001017static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001018{
1019 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1020 struct btrfs_device *new_device;
1021 struct rcu_string *name;
1022
1023 if (device->bdev)
1024 fs_devices->open_devices--;
1025
Anand Jainebbede42017-12-04 12:54:52 +08001026 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001027 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1028 list_del_init(&device->dev_alloc_list);
1029 fs_devices->rw_devices--;
1030 }
1031
Anand Jaine6e674b2017-12-04 12:54:54 +08001032 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001033 fs_devices->missing_devices--;
1034
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001035 btrfs_close_bdev(device);
1036
Anand Jainf448341a2016-06-14 18:55:25 +08001037 new_device = btrfs_alloc_device(NULL, &device->devid,
1038 device->uuid);
1039 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1040
1041 /* Safe because we are under uuid_mutex */
1042 if (device->name) {
1043 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1044 BUG_ON(!name); /* -ENOMEM */
1045 rcu_assign_pointer(new_device->name, name);
1046 }
1047
1048 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1049 new_device->fs_devices = device->fs_devices;
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001050
1051 call_rcu(&device->rcu, free_device_rcu);
Anand Jainf448341a2016-06-14 18:55:25 +08001052}
1053
Anand Jain0226e0e2018-04-12 10:29:27 +08001054static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001055{
Sasha Levin2037a092015-05-12 19:31:37 -04001056 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -05001057
Yan Zheng2b820322008-11-17 21:11:30 -05001058 if (--fs_devices->opened > 0)
1059 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001060
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001061 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001062 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Nikolay Borisov959b1c02018-06-29 08:26:05 +03001063 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001064 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001065 mutex_unlock(&fs_devices->device_list_mutex);
1066
Yan Zhenge4404d62008-12-12 10:03:26 -05001067 WARN_ON(fs_devices->open_devices);
1068 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001069 fs_devices->opened = 0;
1070 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001071
Chris Mason8a4b83c2008-03-24 15:02:07 -04001072 return 0;
1073}
1074
Yan Zheng2b820322008-11-17 21:11:30 -05001075int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1076{
Yan Zhenge4404d62008-12-12 10:03:26 -05001077 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001078 int ret;
1079
1080 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001081 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001082 if (!fs_devices->opened) {
1083 seed_devices = fs_devices->seed;
1084 fs_devices->seed = NULL;
1085 }
Yan Zheng2b820322008-11-17 21:11:30 -05001086 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001087
1088 while (seed_devices) {
1089 fs_devices = seed_devices;
1090 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001091 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001092 free_fs_devices(fs_devices);
1093 }
Yan Zheng2b820322008-11-17 21:11:30 -05001094 return ret;
1095}
1096
Anand Jain897fb572018-04-12 10:29:28 +08001097static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001098 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001099{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001100 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001101 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001102 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001103
Tejun Heod4d77622010-11-13 11:55:18 +01001104 flags |= FMODE_EXCL;
1105
Anand Jainf117e292018-04-12 10:29:26 +08001106 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001107 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001108 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001109 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001110
Anand Jain9f050db2017-11-09 23:45:25 +08001111 if (!latest_dev ||
1112 device->generation > latest_dev->generation)
1113 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001114 }
Chris Masona0af4692008-05-13 16:03:06 -04001115 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001116 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001117 goto out;
1118 }
Yan Zheng2b820322008-11-17 21:11:30 -05001119 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001120 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001121 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001122out:
Yan Zheng2b820322008-11-17 21:11:30 -05001123 return ret;
1124}
1125
Anand Jainf8e10cd2018-01-22 14:49:36 -08001126static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1127{
1128 struct btrfs_device *dev1, *dev2;
1129
1130 dev1 = list_entry(a, struct btrfs_device, dev_list);
1131 dev2 = list_entry(b, struct btrfs_device, dev_list);
1132
1133 if (dev1->devid < dev2->devid)
1134 return -1;
1135 else if (dev1->devid > dev2->devid)
1136 return 1;
1137 return 0;
1138}
1139
Yan Zheng2b820322008-11-17 21:11:30 -05001140int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001141 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001142{
1143 int ret;
1144
David Sterbaf5194e32018-06-19 17:09:47 +02001145 lockdep_assert_held(&uuid_mutex);
1146
Anand Jain542c5902018-04-12 10:29:34 +08001147 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001148 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001149 fs_devices->opened++;
1150 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001151 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001152 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001153 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001154 }
Anand Jain542c5902018-04-12 10:29:34 +08001155 mutex_unlock(&fs_devices->device_list_mutex);
1156
Chris Mason8a4b83c2008-03-24 15:02:07 -04001157 return ret;
1158}
1159
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001160static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001161{
1162 kunmap(page);
1163 put_page(page);
1164}
1165
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001166static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1167 struct page **page,
1168 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001169{
1170 void *p;
1171 pgoff_t index;
1172
1173 /* make sure our super fits in the device */
1174 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1175 return 1;
1176
1177 /* make sure our super fits in the page */
1178 if (sizeof(**disk_super) > PAGE_SIZE)
1179 return 1;
1180
1181 /* make sure our super doesn't straddle pages on disk */
1182 index = bytenr >> PAGE_SHIFT;
1183 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1184 return 1;
1185
1186 /* pull in the page with our super */
1187 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1188 index, GFP_KERNEL);
1189
1190 if (IS_ERR_OR_NULL(*page))
1191 return 1;
1192
1193 p = kmap(*page);
1194
1195 /* align our pointer to the offset of the super block */
1196 *disk_super = p + (bytenr & ~PAGE_MASK);
1197
1198 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1199 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1200 btrfs_release_disk_super(*page);
1201 return 1;
1202 }
1203
1204 if ((*disk_super)->label[0] &&
1205 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1206 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1207
1208 return 0;
1209}
1210
David Sterba6f60cbd2013-02-15 11:31:02 -07001211/*
1212 * Look for a btrfs signature on a device. This may be called out of the mount path
1213 * and we are not allowed to call set_blocksize during the scan. The superblock
1214 * is read via pagecache
1215 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001216int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001217 struct btrfs_fs_devices **fs_devices_ret)
1218{
1219 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001220 bool new_device_added = false;
Anand Jaine124ece2018-01-18 22:02:35 +08001221 struct btrfs_device *device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001222 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001223 struct page *page;
Anand Jaine124ece2018-01-18 22:02:35 +08001224 int ret = 0;
David Sterba6f60cbd2013-02-15 11:31:02 -07001225 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001226
David Sterba899f9302018-06-19 16:37:36 +02001227 lockdep_assert_held(&uuid_mutex);
1228
David Sterba6f60cbd2013-02-15 11:31:02 -07001229 /*
1230 * we would like to check all the supers, but that would make
1231 * a btrfs mount succeed after a mkfs from a different FS.
1232 * So, we need to add a special mount option to scan for
1233 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1234 */
1235 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001236 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001237
1238 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001239 if (IS_ERR(bdev))
1240 return PTR_ERR(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001241
Anand Jain05a5c552017-12-15 15:40:16 +08001242 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1243 ret = -EINVAL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001244 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001245 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001246
Anand Jain4306a972018-05-29 12:28:37 +08001247 device = device_list_add(path, disk_super, &new_device_added);
1248 if (IS_ERR(device)) {
Anand Jaine124ece2018-01-18 22:02:35 +08001249 ret = PTR_ERR(device);
Anand Jain4306a972018-05-29 12:28:37 +08001250 } else {
Anand Jaine124ece2018-01-18 22:02:35 +08001251 *fs_devices_ret = device->fs_devices;
Anand Jain4306a972018-05-29 12:28:37 +08001252 if (new_device_added)
1253 btrfs_free_stale_devices(path, device);
1254 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001255
Anand Jain6cf86a002016-02-13 10:01:29 +08001256 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001257
1258error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001259 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001260
Chris Mason8a4b83c2008-03-24 15:02:07 -04001261 return ret;
1262}
Chris Mason0b86a832008-03-24 15:01:56 -04001263
Jeff Mahoney499f3772015-06-15 09:41:17 -04001264static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001265 struct btrfs_device *device,
1266 u64 *start, u64 len)
1267{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001268 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001269 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001270 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001271 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001272 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001273
Jeff Mahoney499f3772015-06-15 09:41:17 -04001274 if (transaction)
1275 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001276again:
1277 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001278 struct map_lookup *map;
1279 int i;
1280
Jeff Mahoney95617d62015-06-03 10:55:48 -04001281 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001282 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001283 u64 end;
1284
Josef Bacik6df9a952013-06-27 13:22:46 -04001285 if (map->stripes[i].dev != device)
1286 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001287 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001288 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001289 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001290 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001291 /*
1292 * Make sure that while processing the pinned list we do
1293 * not override our *start with a lower value, because
1294 * we can have pinned chunks that fall within this
1295 * device hole and that have lower physical addresses
1296 * than the pending chunks we processed before. If we
1297 * do not take this special care we can end up getting
1298 * 2 pending chunks that start at the same physical
1299 * device offsets because the end offset of a pinned
1300 * chunk can be equal to the start offset of some
1301 * pending chunk.
1302 */
1303 end = map->stripes[i].physical + em->orig_block_len;
1304 if (end > *start) {
1305 *start = end;
1306 ret = 1;
1307 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001308 }
1309 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001310 if (search_list != &fs_info->pinned_chunks) {
1311 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001312 goto again;
1313 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001314
1315 return ret;
1316}
1317
1318
Chris Mason0b86a832008-03-24 15:01:56 -04001319/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001320 * find_free_dev_extent_start - find free space in the specified device
1321 * @device: the device which we search the free space in
1322 * @num_bytes: the size of the free space that we need
1323 * @search_start: the position from which to begin the search
1324 * @start: store the start of the free space.
1325 * @len: the size of the free space. that we find, or the size
1326 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001327 *
Chris Mason0b86a832008-03-24 15:01:56 -04001328 * this uses a pretty simple search, the expectation is that it is
1329 * called very infrequently and that a given device has a small number
1330 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001331 *
1332 * @start is used to store the start of the free space if we find. But if we
1333 * don't find suitable free space, it will be used to store the start position
1334 * of the max free space.
1335 *
1336 * @len is used to store the size of the free space that we find.
1337 * But if we don't find suitable free space, it is used to store the size of
1338 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001339 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001340int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1341 struct btrfs_device *device, u64 num_bytes,
1342 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001344 struct btrfs_fs_info *fs_info = device->fs_info;
1345 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001346 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001347 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001348 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001349 u64 hole_size;
1350 u64 max_hole_start;
1351 u64 max_hole_size;
1352 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001353 u64 search_end = device->total_bytes;
1354 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001355 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001356 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001357
1358 /*
1359 * We don't want to overwrite the superblock on the drive nor any area
1360 * used by the boot loader (grub for example), so we make sure to start
1361 * at an offset of at least 1MB.
1362 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001363 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001364
Josef Bacik6df9a952013-06-27 13:22:46 -04001365 path = btrfs_alloc_path();
1366 if (!path)
1367 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001368
Miao Xie7bfc8372011-01-05 10:07:26 +00001369 max_hole_start = search_start;
1370 max_hole_size = 0;
1371
Zhao Leif2ab7612015-02-16 18:52:17 +08001372again:
Anand Jain401e29c2017-12-04 12:54:55 +08001373 if (search_start >= search_end ||
1374 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001375 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001376 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001377 }
1378
David Sterbae4058b52015-11-27 16:31:35 +01001379 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001380 path->search_commit_root = 1;
1381 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001382
Chris Mason0b86a832008-03-24 15:01:56 -04001383 key.objectid = device->devid;
1384 key.offset = search_start;
1385 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001386
Li Zefan125ccb02011-12-08 15:07:24 +08001387 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001388 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001389 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001390 if (ret > 0) {
1391 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1392 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001393 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001394 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001395
Chris Mason0b86a832008-03-24 15:01:56 -04001396 while (1) {
1397 l = path->nodes[0];
1398 slot = path->slots[0];
1399 if (slot >= btrfs_header_nritems(l)) {
1400 ret = btrfs_next_leaf(root, path);
1401 if (ret == 0)
1402 continue;
1403 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001404 goto out;
1405
1406 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001407 }
1408 btrfs_item_key_to_cpu(l, &key, slot);
1409
1410 if (key.objectid < device->devid)
1411 goto next;
1412
1413 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001414 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001415
David Sterba962a2982014-06-04 18:41:45 +02001416 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001417 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001418
Miao Xie7bfc8372011-01-05 10:07:26 +00001419 if (key.offset > search_start) {
1420 hole_size = key.offset - search_start;
1421
Josef Bacik6df9a952013-06-27 13:22:46 -04001422 /*
1423 * Have to check before we set max_hole_start, otherwise
1424 * we could end up sending back this offset anyway.
1425 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001426 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001427 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001428 hole_size)) {
1429 if (key.offset >= search_start) {
1430 hole_size = key.offset - search_start;
1431 } else {
1432 WARN_ON_ONCE(1);
1433 hole_size = 0;
1434 }
1435 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001436
Miao Xie7bfc8372011-01-05 10:07:26 +00001437 if (hole_size > max_hole_size) {
1438 max_hole_start = search_start;
1439 max_hole_size = hole_size;
1440 }
1441
1442 /*
1443 * If this free space is greater than which we need,
1444 * it must be the max free space that we have found
1445 * until now, so max_hole_start must point to the start
1446 * of this free space and the length of this free space
1447 * is stored in max_hole_size. Thus, we return
1448 * max_hole_start and max_hole_size and go back to the
1449 * caller.
1450 */
1451 if (hole_size >= num_bytes) {
1452 ret = 0;
1453 goto out;
1454 }
1455 }
1456
Chris Mason0b86a832008-03-24 15:01:56 -04001457 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001458 extent_end = key.offset + btrfs_dev_extent_length(l,
1459 dev_extent);
1460 if (extent_end > search_start)
1461 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001462next:
1463 path->slots[0]++;
1464 cond_resched();
1465 }
Chris Mason0b86a832008-03-24 15:01:56 -04001466
liubo38c01b92011-08-02 02:39:03 +00001467 /*
1468 * At this point, search_start should be the end of
1469 * allocated dev extents, and when shrinking the device,
1470 * search_end may be smaller than search_start.
1471 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001472 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001473 hole_size = search_end - search_start;
1474
Jeff Mahoney499f3772015-06-15 09:41:17 -04001475 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001476 hole_size)) {
1477 btrfs_release_path(path);
1478 goto again;
1479 }
Chris Mason0b86a832008-03-24 15:01:56 -04001480
Zhao Leif2ab7612015-02-16 18:52:17 +08001481 if (hole_size > max_hole_size) {
1482 max_hole_start = search_start;
1483 max_hole_size = hole_size;
1484 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001485 }
1486
Miao Xie7bfc8372011-01-05 10:07:26 +00001487 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001488 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001489 ret = -ENOSPC;
1490 else
1491 ret = 0;
1492
1493out:
Yan Zheng2b820322008-11-17 21:11:30 -05001494 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001495 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001496 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001497 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001498 return ret;
1499}
1500
Jeff Mahoney499f3772015-06-15 09:41:17 -04001501int find_free_dev_extent(struct btrfs_trans_handle *trans,
1502 struct btrfs_device *device, u64 num_bytes,
1503 u64 *start, u64 *len)
1504{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001505 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001506 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001507 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001508}
1509
Christoph Hellwigb2950862008-12-02 09:54:17 -05001510static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001511 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001512 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001513{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001514 struct btrfs_fs_info *fs_info = device->fs_info;
1515 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001516 int ret;
1517 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001518 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001519 struct btrfs_key found_key;
1520 struct extent_buffer *leaf = NULL;
1521 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001522
1523 path = btrfs_alloc_path();
1524 if (!path)
1525 return -ENOMEM;
1526
1527 key.objectid = device->devid;
1528 key.offset = start;
1529 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001530again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001531 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001532 if (ret > 0) {
1533 ret = btrfs_previous_item(root, path, key.objectid,
1534 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001535 if (ret)
1536 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001537 leaf = path->nodes[0];
1538 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1539 extent = btrfs_item_ptr(leaf, path->slots[0],
1540 struct btrfs_dev_extent);
1541 BUG_ON(found_key.offset > start || found_key.offset +
1542 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001543 key = found_key;
1544 btrfs_release_path(path);
1545 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001546 } else if (ret == 0) {
1547 leaf = path->nodes[0];
1548 extent = btrfs_item_ptr(leaf, path->slots[0],
1549 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001550 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001551 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001552 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001553 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001554
Miao Xie2196d6e2014-09-03 21:35:41 +08001555 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1556
Chris Mason8f18cf12008-04-25 16:53:30 -04001557 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001558 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001559 btrfs_handle_fs_error(fs_info, ret,
1560 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001561 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001562 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001563 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001564out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001565 btrfs_free_path(path);
1566 return ret;
1567}
1568
Eric Sandeen48a3b632013-04-25 20:41:01 +00001569static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1570 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001571 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001572{
1573 int ret;
1574 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001575 struct btrfs_fs_info *fs_info = device->fs_info;
1576 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001577 struct btrfs_dev_extent *extent;
1578 struct extent_buffer *leaf;
1579 struct btrfs_key key;
1580
Anand Jaine12c9622017-12-04 12:54:53 +08001581 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001582 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001583 path = btrfs_alloc_path();
1584 if (!path)
1585 return -ENOMEM;
1586
Chris Mason0b86a832008-03-24 15:01:56 -04001587 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001588 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001589 key.type = BTRFS_DEV_EXTENT_KEY;
1590 ret = btrfs_insert_empty_item(trans, root, path, &key,
1591 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001592 if (ret)
1593 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001594
1595 leaf = path->nodes[0];
1596 extent = btrfs_item_ptr(leaf, path->slots[0],
1597 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001598 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1599 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001600 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1601 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001602 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1603
Chris Mason0b86a832008-03-24 15:01:56 -04001604 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1605 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001606out:
Chris Mason0b86a832008-03-24 15:01:56 -04001607 btrfs_free_path(path);
1608 return ret;
1609}
1610
Josef Bacik6df9a952013-06-27 13:22:46 -04001611static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001612{
Josef Bacik6df9a952013-06-27 13:22:46 -04001613 struct extent_map_tree *em_tree;
1614 struct extent_map *em;
1615 struct rb_node *n;
1616 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001617
Josef Bacik6df9a952013-06-27 13:22:46 -04001618 em_tree = &fs_info->mapping_tree.map_tree;
1619 read_lock(&em_tree->lock);
1620 n = rb_last(&em_tree->map);
1621 if (n) {
1622 em = rb_entry(n, struct extent_map, rb_node);
1623 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001624 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001625 read_unlock(&em_tree->lock);
1626
Chris Mason0b86a832008-03-24 15:01:56 -04001627 return ret;
1628}
1629
Ilya Dryomov53f10652013-08-12 14:33:01 +03001630static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1631 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001632{
1633 int ret;
1634 struct btrfs_key key;
1635 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001636 struct btrfs_path *path;
1637
Yan Zheng2b820322008-11-17 21:11:30 -05001638 path = btrfs_alloc_path();
1639 if (!path)
1640 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001641
1642 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1643 key.type = BTRFS_DEV_ITEM_KEY;
1644 key.offset = (u64)-1;
1645
Ilya Dryomov53f10652013-08-12 14:33:01 +03001646 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001647 if (ret < 0)
1648 goto error;
1649
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001650 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001651
Ilya Dryomov53f10652013-08-12 14:33:01 +03001652 ret = btrfs_previous_item(fs_info->chunk_root, path,
1653 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001654 BTRFS_DEV_ITEM_KEY);
1655 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001656 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001657 } else {
1658 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1659 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001660 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001661 }
1662 ret = 0;
1663error:
Yan Zheng2b820322008-11-17 21:11:30 -05001664 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001665 return ret;
1666}
1667
1668/*
1669 * the device information is stored in the chunk root
1670 * the btrfs_device struct should be fully filled in
1671 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001672static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001673 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001674 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001675{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001676 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001677 int ret;
1678 struct btrfs_path *path;
1679 struct btrfs_dev_item *dev_item;
1680 struct extent_buffer *leaf;
1681 struct btrfs_key key;
1682 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001683
Chris Mason0b86a832008-03-24 15:01:56 -04001684 path = btrfs_alloc_path();
1685 if (!path)
1686 return -ENOMEM;
1687
Chris Mason0b86a832008-03-24 15:01:56 -04001688 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1689 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001690 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001691
1692 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001693 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001694 if (ret)
1695 goto out;
1696
1697 leaf = path->nodes[0];
1698 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1699
1700 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001701 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001702 btrfs_set_device_type(leaf, dev_item, device->type);
1703 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1704 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1705 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001706 btrfs_set_device_total_bytes(leaf, dev_item,
1707 btrfs_device_get_disk_total_bytes(device));
1708 btrfs_set_device_bytes_used(leaf, dev_item,
1709 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001710 btrfs_set_device_group(leaf, dev_item, 0);
1711 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1712 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001713 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001714
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001715 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001716 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001717 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001718 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001719 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001720
Yan Zheng2b820322008-11-17 21:11:30 -05001721 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001722out:
1723 btrfs_free_path(path);
1724 return ret;
1725}
Chris Mason8f18cf12008-04-25 16:53:30 -04001726
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001727/*
1728 * Function to update ctime/mtime for a given device path.
1729 * Mainly used for ctime/mtime based probe like libblkid.
1730 */
David Sterbada353f62017-02-14 17:55:53 +01001731static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001732{
1733 struct file *filp;
1734
1735 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001736 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001737 return;
1738 file_update_time(filp);
1739 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001740}
1741
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001742static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001743 struct btrfs_device *device)
1744{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001745 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001746 int ret;
1747 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001748 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001749 struct btrfs_trans_handle *trans;
1750
Chris Masona061fc82008-05-07 11:43:44 -04001751 path = btrfs_alloc_path();
1752 if (!path)
1753 return -ENOMEM;
1754
Yan, Zhenga22285a2010-05-16 10:48:46 -04001755 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001756 if (IS_ERR(trans)) {
1757 btrfs_free_path(path);
1758 return PTR_ERR(trans);
1759 }
Chris Masona061fc82008-05-07 11:43:44 -04001760 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1761 key.type = BTRFS_DEV_ITEM_KEY;
1762 key.offset = device->devid;
1763
1764 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001765 if (ret) {
1766 if (ret > 0)
1767 ret = -ENOENT;
1768 btrfs_abort_transaction(trans, ret);
1769 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001770 goto out;
1771 }
1772
1773 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001774 if (ret) {
1775 btrfs_abort_transaction(trans, ret);
1776 btrfs_end_transaction(trans);
1777 }
1778
Chris Masona061fc82008-05-07 11:43:44 -04001779out:
1780 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001781 if (!ret)
1782 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001783 return ret;
1784}
1785
David Sterba3cc31a02016-02-15 16:00:26 +01001786/*
1787 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1788 * filesystem. It's up to the caller to adjust that number regarding eg. device
1789 * replace.
1790 */
1791static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1792 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001793{
Chris Masona061fc82008-05-07 11:43:44 -04001794 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001795 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001796 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001797
Miao Xiede98ced2013-01-29 10:13:12 +00001798 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001799 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001800
Anand Jainbd45ffb2016-02-13 10:01:34 +08001801 all_avail = fs_info->avail_data_alloc_bits |
1802 fs_info->avail_system_alloc_bits |
1803 fs_info->avail_metadata_alloc_bits;
1804 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001805
David Sterba418775a2016-02-15 16:28:14 +01001806 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001807 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001808 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001809
David Sterba418775a2016-02-15 16:28:14 +01001810 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001811 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001812
David Sterba418775a2016-02-15 16:28:14 +01001813 if (ret)
1814 return ret;
1815 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001816 }
1817
1818 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001819}
1820
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001821static struct btrfs_device * btrfs_find_next_active_device(
1822 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001823{
1824 struct btrfs_device *next_device;
1825
1826 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1827 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001828 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1829 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001830 return next_device;
1831 }
1832
1833 return NULL;
1834}
1835
1836/*
1837 * Helper function to check if the given device is part of s_bdev / latest_bdev
1838 * and replace it with the provided or the next active device, in the context
1839 * where this function called, there should be always be another device (or
1840 * this_dev) which is active.
1841 */
1842void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1843 struct btrfs_device *device, struct btrfs_device *this_dev)
1844{
1845 struct btrfs_device *next_device;
1846
1847 if (this_dev)
1848 next_device = this_dev;
1849 else
1850 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1851 device);
1852 ASSERT(next_device);
1853
1854 if (fs_info->sb->s_bdev &&
1855 (fs_info->sb->s_bdev == device->bdev))
1856 fs_info->sb->s_bdev = next_device->bdev;
1857
1858 if (fs_info->fs_devices->latest_bdev == device->bdev)
1859 fs_info->fs_devices->latest_bdev = next_device->bdev;
1860}
1861
David Sterbada353f62017-02-14 17:55:53 +01001862int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1863 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001864{
1865 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001866 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001867 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001868 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001869 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001870
1871 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001872
Anand Jainb5185192018-04-12 10:29:30 +08001873 num_devices = fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01001874 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001875 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001876 WARN_ON(num_devices < 1);
1877 num_devices--;
1878 }
David Sterba7e79cb82018-03-24 02:11:38 +01001879 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Chris Masona061fc82008-05-07 11:43:44 -04001880
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001882 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001883 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001884
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001885 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1886 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001887 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001888 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001889
Anand Jain401e29c2017-12-04 12:54:55 +08001890 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001891 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001892 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001893 }
1894
Anand Jainebbede42017-12-04 12:54:52 +08001895 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1896 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001897 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001898 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001899 }
1900
Anand Jainebbede42017-12-04 12:54:52 +08001901 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001902 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001903 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001904 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001905 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001906 }
Chris Masona061fc82008-05-07 11:43:44 -04001907
Carey Underwoodd7901552013-03-04 16:37:06 -06001908 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001909 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001910 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001911 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001912 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001913
Stefan Behrens63a212a2012-11-05 18:29:28 +01001914 /*
1915 * TODO: the superblock still includes this device in its num_devices
1916 * counter although write_all_supers() is not locked out. This
1917 * could give a filesystem state which requires a degraded mount.
1918 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001919 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001920 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001921 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001922
Anand Jaine12c9622017-12-04 12:54:53 +08001923 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001924 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001925
1926 /*
1927 * the device list mutex makes sure that we don't change
1928 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001929 * the device supers. Whoever is writing all supers, should
1930 * lock the device list mutex before getting the number of
1931 * devices in the super block (super_copy). Conversely,
1932 * whoever updates the number of devices in the super block
1933 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001934 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001935
Anand Jain41a52a02018-04-12 10:29:31 +08001936 /*
1937 * In normal cases the cur_devices == fs_devices. But in case
1938 * of deleting a seed device, the cur_devices should point to
1939 * its own fs_devices listed under the fs_devices->seed.
1940 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001941 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001942 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001943 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001944
Anand Jain41a52a02018-04-12 10:29:31 +08001945 cur_devices->num_devices--;
1946 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08001947 /* Update total_devices of the parent fs_devices if it's seed */
1948 if (cur_devices != fs_devices)
1949 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001950
Anand Jaine6e674b2017-12-04 12:54:54 +08001951 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08001952 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001953
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05001955
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001956 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08001957 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001958 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08001959 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001960 }
Anand Jain99994cd2014-06-03 11:36:00 +08001961
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001962 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
1963 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08001964 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001965
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001966 /*
1967 * at this point, the device is zero sized and detached from
1968 * the devices list. All that's left is to zero out the old
1969 * supers and free the device.
1970 */
Anand Jainebbede42017-12-04 12:54:52 +08001971 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001972 btrfs_scratch_superblocks(device->bdev, device->name->str);
1973
1974 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001975 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001976
Xiao Guangrong1f781602011-04-20 10:09:16 +00001977 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001978 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001979 if (fs_devices->seed == cur_devices) {
1980 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001981 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001982 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001983 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001984 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001985 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08001986 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001987 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001988 }
1989
Chris Masona061fc82008-05-07 11:43:44 -04001990out:
1991 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001992 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08001993
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001994error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08001995 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001996 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001997 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08001998 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001999 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002000 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002001 }
Anand Jain24fc5722016-02-13 10:01:36 +08002002 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002003}
2004
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002005void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2006 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002007{
Anand Jaind51908c2014-08-13 14:24:19 +08002008 struct btrfs_fs_devices *fs_devices;
2009
David Sterbaa32bf9a2018-03-16 02:21:22 +01002010 lockdep_assert_held(&fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002011
Anand Jain25e8e912014-08-20 10:56:56 +08002012 /*
2013 * in case of fs with no seed, srcdev->fs_devices will point
2014 * to fs_devices of fs_info. However when the dev being replaced is
2015 * a seed dev it will point to the seed's local fs_devices. In short
2016 * srcdev will have its correct fs_devices in both the cases.
2017 */
2018 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002019
Stefan Behrense93c89c2012-11-05 17:33:06 +01002020 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002021 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002022 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002023 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002024 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002025
Anand Jainebbede42017-12-04 12:54:52 +08002026 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002027 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002028
Miao Xie82372bc2014-09-03 21:35:44 +08002029 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002030 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002031}
2032
2033void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2034 struct btrfs_device *srcdev)
2035{
2036 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002037
Anand Jainebbede42017-12-04 12:54:52 +08002038 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002039 /* zero out the old super if it is writable */
2040 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2041 }
Anand Jain14238812016-07-22 06:04:53 +08002042
2043 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002044 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002045
Anand Jain94d5f0c2014-08-13 14:24:22 +08002046 /* if this is no devs we rather delete the fs_devices */
2047 if (!fs_devices->num_devices) {
2048 struct btrfs_fs_devices *tmp_fs_devices;
2049
Anand Jain6dd38f82017-10-17 06:53:50 +08002050 /*
2051 * On a mounted FS, num_devices can't be zero unless it's a
2052 * seed. In case of a seed device being replaced, the replace
2053 * target added to the sprout FS, so there will be no more
2054 * device left under the seed FS.
2055 */
2056 ASSERT(fs_devices->seeding);
2057
Anand Jain94d5f0c2014-08-13 14:24:22 +08002058 tmp_fs_devices = fs_info->fs_devices;
2059 while (tmp_fs_devices) {
2060 if (tmp_fs_devices->seed == fs_devices) {
2061 tmp_fs_devices->seed = fs_devices->seed;
2062 break;
2063 }
2064 tmp_fs_devices = tmp_fs_devices->seed;
2065 }
2066 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002067 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002068 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002069 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002070}
2071
2072void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2073 struct btrfs_device *tgtdev)
2074{
Anand Jaind9a071f2018-04-12 10:29:38 +08002075 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002076
Anand Jaind9a071f2018-04-12 10:29:38 +08002077 WARN_ON(!tgtdev);
2078 mutex_lock(&fs_devices->device_list_mutex);
2079
2080 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002081
Anand Jain779bf3fe2016-04-18 16:51:23 +08002082 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002083 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002084
Anand Jaind9a071f2018-04-12 10:29:38 +08002085 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002086
Anand Jain88acff62016-05-03 17:44:43 +08002087 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002088
Stefan Behrense93c89c2012-11-05 17:33:06 +01002089 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002090
Anand Jaind9a071f2018-04-12 10:29:38 +08002091 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002092
2093 /*
2094 * The update_dev_time() with in btrfs_scratch_superblocks()
2095 * may lead to a call to btrfs_show_devname() which will try
2096 * to hold device_list_mutex. And here this device
2097 * is already out of device list, so we don't have to hold
2098 * the device_list_mutex lock.
2099 */
2100 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002101
2102 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002103 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002104}
2105
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002106static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002107 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002108 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002109{
2110 int ret = 0;
2111 struct btrfs_super_block *disk_super;
2112 u64 devid;
2113 u8 *dev_uuid;
2114 struct block_device *bdev;
2115 struct buffer_head *bh;
2116
2117 *device = NULL;
2118 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002119 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002120 if (ret)
2121 return ret;
2122 disk_super = (struct btrfs_super_block *)bh->b_data;
2123 devid = btrfs_stack_device_id(&disk_super->dev_item);
2124 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002125 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002126 brelse(bh);
2127 if (!*device)
2128 ret = -ENOENT;
2129 blkdev_put(bdev, FMODE_READ);
2130 return ret;
2131}
2132
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002133int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002134 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002135 struct btrfs_device **device)
2136{
2137 *device = NULL;
2138 if (strcmp(device_path, "missing") == 0) {
2139 struct list_head *devices;
2140 struct btrfs_device *tmp;
2141
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002142 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002143 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002144 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2145 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002146 *device = tmp;
2147 break;
2148 }
2149 }
2150
Anand Jaind74a6252015-08-14 18:32:56 +08002151 if (!*device)
2152 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002153
2154 return 0;
2155 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002156 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002157 }
2158}
2159
Yan Zheng2b820322008-11-17 21:11:30 -05002160/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002161 * Lookup a device given by device id, or the path if the id is 0.
2162 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002163int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002164 const char *devpath,
2165 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002166{
2167 int ret;
2168
David Sterba5c5c0df2016-02-15 16:39:55 +01002169 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002170 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002171 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002172 if (!*device)
2173 ret = -ENOENT;
2174 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002175 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002176 return -EINVAL;
2177
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002178 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002179 device);
2180 }
2181 return ret;
2182}
2183
Yan Zheng2b820322008-11-17 21:11:30 -05002184/*
2185 * does all the dirty work required for changing file system's UUID.
2186 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002187static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002188{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002189 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002190 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002191 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002192 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002193 struct btrfs_device *device;
2194 u64 super_flags;
2195
David Sterbaa32bf9a2018-03-16 02:21:22 +01002196 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002197 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002198 return -EINVAL;
2199
David Sterba2dfeca92017-06-14 02:48:07 +02002200 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002201 if (IS_ERR(seed_devices))
2202 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002203
Yan Zhenge4404d62008-12-12 10:03:26 -05002204 old_devices = clone_fs_devices(fs_devices);
2205 if (IS_ERR(old_devices)) {
2206 kfree(seed_devices);
2207 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002208 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002209
Anand Jainc4babc52018-04-12 10:29:25 +08002210 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002211
Yan Zhenge4404d62008-12-12 10:03:26 -05002212 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2213 seed_devices->opened = 1;
2214 INIT_LIST_HEAD(&seed_devices->devices);
2215 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002216 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002217
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002218 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002219 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2220 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002221 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002222 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002223
David Sterba34441362016-10-04 19:34:27 +02002224 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002225 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002226 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002227
Yan Zheng2b820322008-11-17 21:11:30 -05002228 fs_devices->seeding = 0;
2229 fs_devices->num_devices = 0;
2230 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002231 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002232 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002233 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002234
2235 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002236 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002237 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002238 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002239
Yan Zheng2b820322008-11-17 21:11:30 -05002240 super_flags = btrfs_super_flags(disk_super) &
2241 ~BTRFS_SUPER_FLAG_SEEDING;
2242 btrfs_set_super_flags(disk_super, super_flags);
2243
2244 return 0;
2245}
2246
2247/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002248 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002249 */
2250static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002251 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002252{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002253 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002254 struct btrfs_path *path;
2255 struct extent_buffer *leaf;
2256 struct btrfs_dev_item *dev_item;
2257 struct btrfs_device *device;
2258 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002259 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002260 u8 dev_uuid[BTRFS_UUID_SIZE];
2261 u64 devid;
2262 int ret;
2263
2264 path = btrfs_alloc_path();
2265 if (!path)
2266 return -ENOMEM;
2267
Yan Zheng2b820322008-11-17 21:11:30 -05002268 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2269 key.offset = 0;
2270 key.type = BTRFS_DEV_ITEM_KEY;
2271
2272 while (1) {
2273 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2274 if (ret < 0)
2275 goto error;
2276
2277 leaf = path->nodes[0];
2278next_slot:
2279 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2280 ret = btrfs_next_leaf(root, path);
2281 if (ret > 0)
2282 break;
2283 if (ret < 0)
2284 goto error;
2285 leaf = path->nodes[0];
2286 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002287 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002288 continue;
2289 }
2290
2291 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2292 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2293 key.type != BTRFS_DEV_ITEM_KEY)
2294 break;
2295
2296 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2297 struct btrfs_dev_item);
2298 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002299 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002300 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002301 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002302 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002303 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002304 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002305
2306 if (device->fs_devices->seeding) {
2307 btrfs_set_device_generation(leaf, dev_item,
2308 device->generation);
2309 btrfs_mark_buffer_dirty(leaf);
2310 }
2311
2312 path->slots[0]++;
2313 goto next_slot;
2314 }
2315 ret = 0;
2316error:
2317 btrfs_free_path(path);
2318 return ret;
2319}
2320
David Sterbada353f62017-02-14 17:55:53 +01002321int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002322{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002323 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002324 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002325 struct btrfs_trans_handle *trans;
2326 struct btrfs_device *device;
2327 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002328 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002329 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002330 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002331 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002332 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002333 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002334 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002335
Anand Jain5da54bc2018-07-03 13:14:50 +08002336 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002337 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002338
Li Zefana5d163332011-12-07 20:08:40 -05002339 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002340 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002341 if (IS_ERR(bdev))
2342 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002343
Anand Jain5da54bc2018-07-03 13:14:50 +08002344 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002345 seeding_dev = 1;
2346 down_write(&sb->s_umount);
2347 mutex_lock(&uuid_mutex);
2348 }
2349
Chris Mason8c8bee12008-09-29 11:19:10 -04002350 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002351
Anand Jain5da54bc2018-07-03 13:14:50 +08002352 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002353 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002354 if (device->bdev == bdev) {
2355 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002356 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002357 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002358 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002359 }
2360 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002361 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002362
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002363 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002364 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002365 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002366 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002367 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002368 }
2369
David Sterba78f2c9e2016-02-11 14:25:38 +01002370 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002371 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002372 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002373 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002374 }
Josef Bacik606686e2012-06-04 14:03:51 -04002375 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002376
Yan, Zhenga22285a2010-05-16 10:48:46 -04002377 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002378 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002379 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002380 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002381 }
2382
Josef Bacikd5e20032011-08-04 14:52:27 +00002383 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002384 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002385 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002386 device->io_width = fs_info->sectorsize;
2387 device->io_align = fs_info->sectorsize;
2388 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002389 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2390 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002391 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002392 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002393 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002394 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002395 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002396 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002397 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002398 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002399 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002400
Yan Zheng2b820322008-11-17 21:11:30 -05002401 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002402 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002403 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002404 if (ret) {
2405 btrfs_abort_transaction(trans, ret);
2406 goto error_trans;
2407 }
Yan Zheng2b820322008-11-17 21:11:30 -05002408 }
2409
Anand Jain5da54bc2018-07-03 13:14:50 +08002410 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002411
Anand Jain5da54bc2018-07-03 13:14:50 +08002412 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002413 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002414 list_add_rcu(&device->dev_list, &fs_devices->devices);
2415 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2416 fs_devices->num_devices++;
2417 fs_devices->open_devices++;
2418 fs_devices->rw_devices++;
2419 fs_devices->total_devices++;
2420 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002421
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002422 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002423
Anand Jaine884f4f2017-04-04 18:40:19 +08002424 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002425 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002426
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002427 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2428 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002429 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002430
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002431 tmp = btrfs_super_num_devices(fs_info->super_copy);
2432 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002433
2434 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002435 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002436
Miao Xie2196d6e2014-09-03 21:35:41 +08002437 /*
2438 * we've got more storage, clear any full flags on the space
2439 * infos
2440 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002441 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002442
David Sterba34441362016-10-04 19:34:27 +02002443 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002444 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002445
Yan Zheng2b820322008-11-17 21:11:30 -05002446 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002447 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002448 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002449 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002450 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002451 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002452 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002453 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002454 }
2455
Anand Jainc74a0b02017-11-06 16:36:15 +08002456 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002457 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002458 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002459 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002460 }
2461
2462 if (seeding_dev) {
2463 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2464
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002465 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002466 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002467 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002468 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002469 }
Anand Jainb2373f22014-06-03 11:36:03 +08002470
2471 /* Sprouting would change fsid of the mounted root,
2472 * so rename the fsid on the sysfs
2473 */
2474 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002475 fs_info->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002476 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002477 btrfs_warn(fs_info,
2478 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002479 }
2480
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002481 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002482
2483 if (seeding_dev) {
2484 mutex_unlock(&uuid_mutex);
2485 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002486 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002487
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002488 if (ret) /* transaction commit */
2489 return ret;
2490
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002491 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002492 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002494 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002495 trans = btrfs_attach_transaction(root);
2496 if (IS_ERR(trans)) {
2497 if (PTR_ERR(trans) == -ENOENT)
2498 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002499 ret = PTR_ERR(trans);
2500 trans = NULL;
2501 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002502 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002503 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002504 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002505
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002506 /* Update ctime/mtime for libblkid */
2507 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002508 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002509
Anand Jaind31c32f2017-09-28 14:51:10 +08002510error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002511 btrfs_sysfs_rm_device_link(fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002512error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002513 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002514 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002515 if (trans)
2516 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002517error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002518 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002519error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002520 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002521 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002522 mutex_unlock(&uuid_mutex);
2523 up_write(&sb->s_umount);
2524 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002525 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002526}
2527
Chris Masond3977122009-01-05 21:25:51 -05002528static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2529 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002530{
2531 int ret;
2532 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002533 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002534 struct btrfs_dev_item *dev_item;
2535 struct extent_buffer *leaf;
2536 struct btrfs_key key;
2537
Chris Mason0b86a832008-03-24 15:01:56 -04002538 path = btrfs_alloc_path();
2539 if (!path)
2540 return -ENOMEM;
2541
2542 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2543 key.type = BTRFS_DEV_ITEM_KEY;
2544 key.offset = device->devid;
2545
2546 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2547 if (ret < 0)
2548 goto out;
2549
2550 if (ret > 0) {
2551 ret = -ENOENT;
2552 goto out;
2553 }
2554
2555 leaf = path->nodes[0];
2556 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2557
2558 btrfs_set_device_id(leaf, dev_item, device->devid);
2559 btrfs_set_device_type(leaf, dev_item, device->type);
2560 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2561 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2562 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002563 btrfs_set_device_total_bytes(leaf, dev_item,
2564 btrfs_device_get_disk_total_bytes(device));
2565 btrfs_set_device_bytes_used(leaf, dev_item,
2566 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002567 btrfs_mark_buffer_dirty(leaf);
2568
2569out:
2570 btrfs_free_path(path);
2571 return ret;
2572}
2573
Miao Xie2196d6e2014-09-03 21:35:41 +08002574int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002575 struct btrfs_device *device, u64 new_size)
2576{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002577 struct btrfs_fs_info *fs_info = device->fs_info;
2578 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002579 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002580 u64 old_total;
2581 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002582
Anand Jainebbede42017-12-04 12:54:52 +08002583 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002584 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002585
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002586 new_size = round_down(new_size, fs_info->sectorsize);
2587
David Sterba34441362016-10-04 19:34:27 +02002588 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002589 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002590 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002591
Stefan Behrens63a212a2012-11-05 18:29:28 +01002592 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002593 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002594 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002595 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002596 }
Yan Zheng2b820322008-11-17 21:11:30 -05002597
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002598 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002599
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002600 btrfs_set_super_total_bytes(super_copy,
2601 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002602 device->fs_devices->total_rw_bytes += diff;
2603
Miao Xie7cc8e582014-09-03 21:35:38 +08002604 btrfs_device_set_total_bytes(device, new_size);
2605 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002606 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002607 if (list_empty(&device->resized_list))
2608 list_add_tail(&device->resized_list,
2609 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002610 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002611
Chris Mason8f18cf12008-04-25 16:53:30 -04002612 return btrfs_update_device(trans, device);
2613}
2614
2615static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002616 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002617{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002618 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002619 int ret;
2620 struct btrfs_path *path;
2621 struct btrfs_key key;
2622
Chris Mason8f18cf12008-04-25 16:53:30 -04002623 path = btrfs_alloc_path();
2624 if (!path)
2625 return -ENOMEM;
2626
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002627 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002628 key.offset = chunk_offset;
2629 key.type = BTRFS_CHUNK_ITEM_KEY;
2630
2631 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002632 if (ret < 0)
2633 goto out;
2634 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002635 btrfs_handle_fs_error(fs_info, -ENOENT,
2636 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002637 ret = -ENOENT;
2638 goto out;
2639 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002640
2641 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002642 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002643 btrfs_handle_fs_error(fs_info, ret,
2644 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002645out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002646 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002647 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002648}
2649
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002650static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002651{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002652 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002653 struct btrfs_disk_key *disk_key;
2654 struct btrfs_chunk *chunk;
2655 u8 *ptr;
2656 int ret = 0;
2657 u32 num_stripes;
2658 u32 array_size;
2659 u32 len = 0;
2660 u32 cur;
2661 struct btrfs_key key;
2662
David Sterba34441362016-10-04 19:34:27 +02002663 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002664 array_size = btrfs_super_sys_array_size(super_copy);
2665
2666 ptr = super_copy->sys_chunk_array;
2667 cur = 0;
2668
2669 while (cur < array_size) {
2670 disk_key = (struct btrfs_disk_key *)ptr;
2671 btrfs_disk_key_to_cpu(&key, disk_key);
2672
2673 len = sizeof(*disk_key);
2674
2675 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2676 chunk = (struct btrfs_chunk *)(ptr + len);
2677 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2678 len += btrfs_chunk_item_size(num_stripes);
2679 } else {
2680 ret = -EIO;
2681 break;
2682 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002683 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002684 key.offset == chunk_offset) {
2685 memmove(ptr, ptr + len, array_size - (cur + len));
2686 array_size -= len;
2687 btrfs_set_super_sys_array_size(super_copy, array_size);
2688 } else {
2689 ptr += len;
2690 cur += len;
2691 }
2692 }
David Sterba34441362016-10-04 19:34:27 +02002693 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002694 return ret;
2695}
2696
Liu Bo592d92e2017-03-14 13:33:55 -07002697static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2698 u64 logical, u64 length)
2699{
2700 struct extent_map_tree *em_tree;
2701 struct extent_map *em;
2702
2703 em_tree = &fs_info->mapping_tree.map_tree;
2704 read_lock(&em_tree->lock);
2705 em = lookup_extent_mapping(em_tree, logical, length);
2706 read_unlock(&em_tree->lock);
2707
2708 if (!em) {
2709 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2710 logical, length);
2711 return ERR_PTR(-EINVAL);
2712 }
2713
2714 if (em->start > logical || em->start + em->len < logical) {
2715 btrfs_crit(fs_info,
2716 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2717 logical, length, em->start, em->start + em->len);
2718 free_extent_map(em);
2719 return ERR_PTR(-EINVAL);
2720 }
2721
2722 /* callers are responsible for dropping em's ref. */
2723 return em;
2724}
2725
Josef Bacik47ab2a62014-09-18 11:20:02 -04002726int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002727 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002728{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002729 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002730 struct map_lookup *map;
2731 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002732 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002733 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002734
Liu Bo592d92e2017-03-14 13:33:55 -07002735 em = get_chunk_map(fs_info, chunk_offset, 1);
2736 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002737 /*
2738 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002739 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002740 * do anything we still error out.
2741 */
2742 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002743 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002744 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002745 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002746 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002747 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002748 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002749
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002750 /*
2751 * Take the device list mutex to prevent races with the final phase of
2752 * a device replace operation that replaces the device object associated
2753 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2754 */
2755 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002756 for (i = 0; i < map->num_stripes; i++) {
2757 struct btrfs_device *device = map->stripes[i].dev;
2758 ret = btrfs_free_dev_extent(trans, device,
2759 map->stripes[i].physical,
2760 &dev_extent_len);
2761 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002762 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002763 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002764 goto out;
2765 }
2766
2767 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002768 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002769 btrfs_device_set_bytes_used(device,
2770 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002771 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002772 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002773 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002774 }
2775
2776 if (map->stripes[i].dev) {
2777 ret = btrfs_update_device(trans, map->stripes[i].dev);
2778 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002779 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002780 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002781 goto out;
2782 }
2783 }
2784 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002785 mutex_unlock(&fs_devices->device_list_mutex);
2786
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002787 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002788 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002789 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002790 goto out;
2791 }
2792
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002793 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002794
2795 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002796 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002797 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002798 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002799 goto out;
2800 }
2801 }
2802
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03002803 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002804 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002805 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002806 goto out;
2807 }
2808
Josef Bacik47ab2a62014-09-18 11:20:02 -04002809out:
2810 /* once for us */
2811 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002812 return ret;
2813}
2814
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002815static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002816{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002817 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002818 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002819 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002820
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002821 /*
2822 * Prevent races with automatic removal of unused block groups.
2823 * After we relocate and before we remove the chunk with offset
2824 * chunk_offset, automatic removal of the block group can kick in,
2825 * resulting in a failure when calling btrfs_remove_chunk() below.
2826 *
2827 * Make sure to acquire this mutex before doing a tree search (dev
2828 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2829 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2830 * we release the path used to search the chunk/dev tree and before
2831 * the current task acquires this mutex and calls us.
2832 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002833 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002834
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002835 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002836 if (ret)
2837 return -ENOSPC;
2838
Chris Mason8f18cf12008-04-25 16:53:30 -04002839 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002840 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002841 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002842 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002843 if (ret)
2844 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002845
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002846 /*
2847 * We add the kobjects here (and after forcing data chunk creation)
2848 * since relocation is the only place we'll create chunks of a new
2849 * type at runtime. The only place where we'll remove the last
2850 * chunk of a type is the call immediately below this one. Even
2851 * so, we're protected against races with the cleaner thread since
2852 * we're covered by the delete_unused_bgs_mutex.
2853 */
2854 btrfs_add_raid_kobjects(fs_info);
2855
Chris Mason19c4d2f2016-10-10 13:43:31 -07002856 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2857 chunk_offset);
2858 if (IS_ERR(trans)) {
2859 ret = PTR_ERR(trans);
2860 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2861 return ret;
2862 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002863
Chris Mason19c4d2f2016-10-10 13:43:31 -07002864 /*
2865 * step two, delete the device extents and the
2866 * chunk tree entries
2867 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002868 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002869 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002870 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002871}
2872
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002873static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002874{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002876 struct btrfs_path *path;
2877 struct extent_buffer *leaf;
2878 struct btrfs_chunk *chunk;
2879 struct btrfs_key key;
2880 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002881 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002882 bool retried = false;
2883 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002884 int ret;
2885
2886 path = btrfs_alloc_path();
2887 if (!path)
2888 return -ENOMEM;
2889
Josef Bacikba1bf482009-09-11 16:11:19 -04002890again:
Yan Zheng2b820322008-11-17 21:11:30 -05002891 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2892 key.offset = (u64)-1;
2893 key.type = BTRFS_CHUNK_ITEM_KEY;
2894
2895 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002896 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002897 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002898 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002900 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002901 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002902 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002903
2904 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2905 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002906 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002907 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002908 if (ret < 0)
2909 goto error;
2910 if (ret > 0)
2911 break;
2912
2913 leaf = path->nodes[0];
2914 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2915
2916 chunk = btrfs_item_ptr(leaf, path->slots[0],
2917 struct btrfs_chunk);
2918 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002919 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002920
2921 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002922 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002923 if (ret == -ENOSPC)
2924 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302925 else
2926 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002927 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002929
2930 if (found_key.offset == 0)
2931 break;
2932 key.offset = found_key.offset - 1;
2933 }
2934 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002935 if (failed && !retried) {
2936 failed = 0;
2937 retried = true;
2938 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302939 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002940 ret = -ENOSPC;
2941 }
Yan Zheng2b820322008-11-17 21:11:30 -05002942error:
2943 btrfs_free_path(path);
2944 return ret;
2945}
2946
Liu Boa6f93c72017-11-15 16:28:11 -07002947/*
2948 * return 1 : allocate a data chunk successfully,
2949 * return <0: errors during allocating a data chunk,
2950 * return 0 : no need to allocate a data chunk.
2951 */
2952static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
2953 u64 chunk_offset)
2954{
2955 struct btrfs_block_group_cache *cache;
2956 u64 bytes_used;
2957 u64 chunk_type;
2958
2959 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2960 ASSERT(cache);
2961 chunk_type = cache->flags;
2962 btrfs_put_block_group(cache);
2963
2964 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
2965 spin_lock(&fs_info->data_sinfo->lock);
2966 bytes_used = fs_info->data_sinfo->bytes_used;
2967 spin_unlock(&fs_info->data_sinfo->lock);
2968
2969 if (!bytes_used) {
2970 struct btrfs_trans_handle *trans;
2971 int ret;
2972
2973 trans = btrfs_join_transaction(fs_info->tree_root);
2974 if (IS_ERR(trans))
2975 return PTR_ERR(trans);
2976
Nikolay Borisov43a7e992018-06-20 15:49:15 +03002977 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07002978 BTRFS_BLOCK_GROUP_DATA);
2979 btrfs_end_transaction(trans);
2980 if (ret < 0)
2981 return ret;
2982
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002983 btrfs_add_raid_kobjects(fs_info);
2984
Liu Boa6f93c72017-11-15 16:28:11 -07002985 return 1;
2986 }
2987 }
2988 return 0;
2989}
2990
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002991static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002992 struct btrfs_balance_control *bctl)
2993{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002994 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002995 struct btrfs_trans_handle *trans;
2996 struct btrfs_balance_item *item;
2997 struct btrfs_disk_balance_args disk_bargs;
2998 struct btrfs_path *path;
2999 struct extent_buffer *leaf;
3000 struct btrfs_key key;
3001 int ret, err;
3002
3003 path = btrfs_alloc_path();
3004 if (!path)
3005 return -ENOMEM;
3006
3007 trans = btrfs_start_transaction(root, 0);
3008 if (IS_ERR(trans)) {
3009 btrfs_free_path(path);
3010 return PTR_ERR(trans);
3011 }
3012
3013 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003014 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003015 key.offset = 0;
3016
3017 ret = btrfs_insert_empty_item(trans, root, path, &key,
3018 sizeof(*item));
3019 if (ret)
3020 goto out;
3021
3022 leaf = path->nodes[0];
3023 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3024
David Sterbab159fa22016-11-08 18:09:03 +01003025 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003026
3027 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3028 btrfs_set_balance_data(leaf, item, &disk_bargs);
3029 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3030 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3031 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3032 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3033
3034 btrfs_set_balance_flags(leaf, item, bctl->flags);
3035
3036 btrfs_mark_buffer_dirty(leaf);
3037out:
3038 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003039 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003040 if (err && !ret)
3041 ret = err;
3042 return ret;
3043}
3044
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003045static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003046{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003047 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003048 struct btrfs_trans_handle *trans;
3049 struct btrfs_path *path;
3050 struct btrfs_key key;
3051 int ret, err;
3052
3053 path = btrfs_alloc_path();
3054 if (!path)
3055 return -ENOMEM;
3056
3057 trans = btrfs_start_transaction(root, 0);
3058 if (IS_ERR(trans)) {
3059 btrfs_free_path(path);
3060 return PTR_ERR(trans);
3061 }
3062
3063 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003064 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003065 key.offset = 0;
3066
3067 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3068 if (ret < 0)
3069 goto out;
3070 if (ret > 0) {
3071 ret = -ENOENT;
3072 goto out;
3073 }
3074
3075 ret = btrfs_del_item(trans, root, path);
3076out:
3077 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003078 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003079 if (err && !ret)
3080 ret = err;
3081 return ret;
3082}
3083
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003084/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003085 * This is a heuristic used to reduce the number of chunks balanced on
3086 * resume after balance was interrupted.
3087 */
3088static void update_balance_args(struct btrfs_balance_control *bctl)
3089{
3090 /*
3091 * Turn on soft mode for chunk types that were being converted.
3092 */
3093 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3094 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3095 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3096 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3097 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3098 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3099
3100 /*
3101 * Turn on usage filter if is not already used. The idea is
3102 * that chunks that we have already balanced should be
3103 * reasonably full. Don't do it for chunks that are being
3104 * converted - that will keep us from relocating unconverted
3105 * (albeit full) chunks.
3106 */
3107 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003108 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003109 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3110 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3111 bctl->data.usage = 90;
3112 }
3113 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003114 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003115 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3116 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3117 bctl->sys.usage = 90;
3118 }
3119 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003120 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003121 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3122 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3123 bctl->meta.usage = 90;
3124 }
3125}
3126
3127/*
David Sterba149196a2018-03-20 20:23:09 +01003128 * Clear the balance status in fs_info and delete the balance item from disk.
3129 */
3130static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003131{
3132 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003133 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003134
3135 BUG_ON(!fs_info->balance_ctl);
3136
3137 spin_lock(&fs_info->balance_lock);
3138 fs_info->balance_ctl = NULL;
3139 spin_unlock(&fs_info->balance_lock);
3140
3141 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003142 ret = del_balance_item(fs_info);
3143 if (ret)
3144 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003145}
3146
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003147/*
3148 * Balance filters. Return 1 if chunk should be filtered out
3149 * (should not be balanced).
3150 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003151static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003152 struct btrfs_balance_args *bargs)
3153{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003154 chunk_type = chunk_to_extended(chunk_type) &
3155 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003156
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003157 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003158 return 0;
3159
3160 return 1;
3161}
3162
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003163static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003164 struct btrfs_balance_args *bargs)
3165{
3166 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003167 u64 chunk_used;
3168 u64 user_thresh_min;
3169 u64 user_thresh_max;
3170 int ret = 1;
3171
3172 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3173 chunk_used = btrfs_block_group_used(&cache->item);
3174
3175 if (bargs->usage_min == 0)
3176 user_thresh_min = 0;
3177 else
3178 user_thresh_min = div_factor_fine(cache->key.offset,
3179 bargs->usage_min);
3180
3181 if (bargs->usage_max == 0)
3182 user_thresh_max = 1;
3183 else if (bargs->usage_max > 100)
3184 user_thresh_max = cache->key.offset;
3185 else
3186 user_thresh_max = div_factor_fine(cache->key.offset,
3187 bargs->usage_max);
3188
3189 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3190 ret = 0;
3191
3192 btrfs_put_block_group(cache);
3193 return ret;
3194}
3195
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003196static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003197 u64 chunk_offset, struct btrfs_balance_args *bargs)
3198{
3199 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003200 u64 chunk_used, user_thresh;
3201 int ret = 1;
3202
3203 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3204 chunk_used = btrfs_block_group_used(&cache->item);
3205
David Sterbabc309462015-10-20 18:22:13 +02003206 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003207 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003208 else if (bargs->usage > 100)
3209 user_thresh = cache->key.offset;
3210 else
3211 user_thresh = div_factor_fine(cache->key.offset,
3212 bargs->usage);
3213
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003214 if (chunk_used < user_thresh)
3215 ret = 0;
3216
3217 btrfs_put_block_group(cache);
3218 return ret;
3219}
3220
Ilya Dryomov409d4042012-01-16 22:04:47 +02003221static int chunk_devid_filter(struct extent_buffer *leaf,
3222 struct btrfs_chunk *chunk,
3223 struct btrfs_balance_args *bargs)
3224{
3225 struct btrfs_stripe *stripe;
3226 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3227 int i;
3228
3229 for (i = 0; i < num_stripes; i++) {
3230 stripe = btrfs_stripe_nr(chunk, i);
3231 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3232 return 0;
3233 }
3234
3235 return 1;
3236}
3237
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003238/* [pstart, pend) */
3239static int chunk_drange_filter(struct extent_buffer *leaf,
3240 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003241 struct btrfs_balance_args *bargs)
3242{
3243 struct btrfs_stripe *stripe;
3244 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3245 u64 stripe_offset;
3246 u64 stripe_length;
3247 int factor;
3248 int i;
3249
3250 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3251 return 0;
3252
3253 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003254 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3255 factor = num_stripes / 2;
3256 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3257 factor = num_stripes - 1;
3258 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3259 factor = num_stripes - 2;
3260 } else {
3261 factor = num_stripes;
3262 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003263
3264 for (i = 0; i < num_stripes; i++) {
3265 stripe = btrfs_stripe_nr(chunk, i);
3266 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3267 continue;
3268
3269 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3270 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003271 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003272
3273 if (stripe_offset < bargs->pend &&
3274 stripe_offset + stripe_length > bargs->pstart)
3275 return 0;
3276 }
3277
3278 return 1;
3279}
3280
Ilya Dryomovea671762012-01-16 22:04:48 +02003281/* [vstart, vend) */
3282static int chunk_vrange_filter(struct extent_buffer *leaf,
3283 struct btrfs_chunk *chunk,
3284 u64 chunk_offset,
3285 struct btrfs_balance_args *bargs)
3286{
3287 if (chunk_offset < bargs->vend &&
3288 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3289 /* at least part of the chunk is inside this vrange */
3290 return 0;
3291
3292 return 1;
3293}
3294
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003295static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3296 struct btrfs_chunk *chunk,
3297 struct btrfs_balance_args *bargs)
3298{
3299 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3300
3301 if (bargs->stripes_min <= num_stripes
3302 && num_stripes <= bargs->stripes_max)
3303 return 0;
3304
3305 return 1;
3306}
3307
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003308static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003309 struct btrfs_balance_args *bargs)
3310{
3311 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3312 return 0;
3313
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003314 chunk_type = chunk_to_extended(chunk_type) &
3315 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003316
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003317 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003318 return 1;
3319
3320 return 0;
3321}
3322
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003323static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003324 struct extent_buffer *leaf,
3325 struct btrfs_chunk *chunk, u64 chunk_offset)
3326{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003327 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003328 struct btrfs_balance_args *bargs = NULL;
3329 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3330
3331 /* type filter */
3332 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3333 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3334 return 0;
3335 }
3336
3337 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3338 bargs = &bctl->data;
3339 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3340 bargs = &bctl->sys;
3341 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3342 bargs = &bctl->meta;
3343
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003344 /* profiles filter */
3345 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3346 chunk_profiles_filter(chunk_type, bargs)) {
3347 return 0;
3348 }
3349
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003350 /* usage filter */
3351 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003352 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003353 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003354 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003355 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003356 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003357 }
3358
Ilya Dryomov409d4042012-01-16 22:04:47 +02003359 /* devid filter */
3360 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3361 chunk_devid_filter(leaf, chunk, bargs)) {
3362 return 0;
3363 }
3364
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003365 /* drange filter, makes sense only with devid filter */
3366 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003367 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003368 return 0;
3369 }
3370
Ilya Dryomovea671762012-01-16 22:04:48 +02003371 /* vrange filter */
3372 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3373 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3374 return 0;
3375 }
3376
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003377 /* stripes filter */
3378 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3379 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3380 return 0;
3381 }
3382
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003383 /* soft profile changing mode */
3384 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3385 chunk_soft_convert_filter(chunk_type, bargs)) {
3386 return 0;
3387 }
3388
David Sterba7d824b62014-05-07 17:37:51 +02003389 /*
3390 * limited by count, must be the last filter
3391 */
3392 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3393 if (bargs->limit == 0)
3394 return 0;
3395 else
3396 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003397 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3398 /*
3399 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003400 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003401 * about the count of all chunks that satisfy the filters.
3402 */
3403 if (bargs->limit_max == 0)
3404 return 0;
3405 else
3406 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003407 }
3408
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003409 return 1;
3410}
3411
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003412static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003413{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003414 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003415 struct btrfs_root *chunk_root = fs_info->chunk_root;
3416 struct btrfs_root *dev_root = fs_info->dev_root;
3417 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003418 struct btrfs_device *device;
3419 u64 old_size;
3420 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003421 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003422 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003423 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003424 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003425 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003426 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003427 struct extent_buffer *leaf;
3428 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003429 int ret;
3430 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003431 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003432 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003433 u64 limit_data = bctl->data.limit;
3434 u64 limit_meta = bctl->meta.limit;
3435 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003436 u32 count_data = 0;
3437 u32 count_meta = 0;
3438 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003439 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003440
Chris Masonec44a352008-04-28 15:29:52 -04003441 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003442 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003443 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003444 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003445 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003446 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003447 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003448 btrfs_device_get_total_bytes(device) -
3449 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003450 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003451 continue;
3452
3453 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003454 if (ret == -ENOSPC)
3455 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003456 if (ret) {
3457 /* btrfs_shrink_device never returns ret > 0 */
3458 WARN_ON(ret > 0);
3459 goto error;
3460 }
Chris Masonec44a352008-04-28 15:29:52 -04003461
Yan, Zhenga22285a2010-05-16 10:48:46 -04003462 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003463 if (IS_ERR(trans)) {
3464 ret = PTR_ERR(trans);
3465 btrfs_info_in_rcu(fs_info,
3466 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3467 rcu_str_deref(device->name), ret,
3468 old_size, old_size - size_to_free);
3469 goto error;
3470 }
Chris Masonec44a352008-04-28 15:29:52 -04003471
3472 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003473 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003474 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003475 /* btrfs_grow_device never returns ret > 0 */
3476 WARN_ON(ret > 0);
3477 btrfs_info_in_rcu(fs_info,
3478 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3479 rcu_str_deref(device->name), ret,
3480 old_size, old_size - size_to_free);
3481 goto error;
3482 }
Chris Masonec44a352008-04-28 15:29:52 -04003483
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003484 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003485 }
3486
3487 /* step two, relocate all the chunks */
3488 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003489 if (!path) {
3490 ret = -ENOMEM;
3491 goto error;
3492 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003493
3494 /* zero out stat counters */
3495 spin_lock(&fs_info->balance_lock);
3496 memset(&bctl->stat, 0, sizeof(bctl->stat));
3497 spin_unlock(&fs_info->balance_lock);
3498again:
David Sterba7d824b62014-05-07 17:37:51 +02003499 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003500 /*
3501 * The single value limit and min/max limits use the same bytes
3502 * in the
3503 */
David Sterba7d824b62014-05-07 17:37:51 +02003504 bctl->data.limit = limit_data;
3505 bctl->meta.limit = limit_meta;
3506 bctl->sys.limit = limit_sys;
3507 }
Chris Masonec44a352008-04-28 15:29:52 -04003508 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3509 key.offset = (u64)-1;
3510 key.type = BTRFS_CHUNK_ITEM_KEY;
3511
Chris Masond3977122009-01-05 21:25:51 -05003512 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003513 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003514 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003515 ret = -ECANCELED;
3516 goto error;
3517 }
3518
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003519 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003520 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003521 if (ret < 0) {
3522 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003523 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003524 }
Chris Masonec44a352008-04-28 15:29:52 -04003525
3526 /*
3527 * this shouldn't happen, it means the last relocate
3528 * failed
3529 */
3530 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003531 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003532
3533 ret = btrfs_previous_item(chunk_root, path, 0,
3534 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003535 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003536 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003537 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003538 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003539 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003540
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003541 leaf = path->nodes[0];
3542 slot = path->slots[0];
3543 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3544
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003545 if (found_key.objectid != key.objectid) {
3546 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003547 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003548 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003549
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003550 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003551 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003552
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003553 if (!counting) {
3554 spin_lock(&fs_info->balance_lock);
3555 bctl->stat.considered++;
3556 spin_unlock(&fs_info->balance_lock);
3557 }
3558
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003559 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003560 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003561
David Sterbab3b4aa72011-04-21 01:20:15 +02003562 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003563 if (!ret) {
3564 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003565 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003566 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003567
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003568 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003569 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003570 spin_lock(&fs_info->balance_lock);
3571 bctl->stat.expected++;
3572 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003573
3574 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3575 count_data++;
3576 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3577 count_sys++;
3578 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3579 count_meta++;
3580
3581 goto loop;
3582 }
3583
3584 /*
3585 * Apply limit_min filter, no need to check if the LIMITS
3586 * filter is used, limit_min is 0 by default
3587 */
3588 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3589 count_data < bctl->data.limit_min)
3590 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3591 count_meta < bctl->meta.limit_min)
3592 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3593 count_sys < bctl->sys.limit_min)) {
3594 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003595 goto loop;
3596 }
3597
Liu Boa6f93c72017-11-15 16:28:11 -07003598 if (!chunk_reserved) {
3599 /*
3600 * We may be relocating the only data chunk we have,
3601 * which could potentially end up with losing data's
3602 * raid profile, so lets allocate an empty one in
3603 * advance.
3604 */
3605 ret = btrfs_may_alloc_data_chunk(fs_info,
3606 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003607 if (ret < 0) {
3608 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3609 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003610 } else if (ret == 1) {
3611 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003612 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003613 }
3614
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003615 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003616 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003617 if (ret && ret != -ENOSPC)
3618 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003619 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003620 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003621 } else {
3622 spin_lock(&fs_info->balance_lock);
3623 bctl->stat.completed++;
3624 spin_unlock(&fs_info->balance_lock);
3625 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003626loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003627 if (found_key.offset == 0)
3628 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003629 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003630 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003631
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003632 if (counting) {
3633 btrfs_release_path(path);
3634 counting = false;
3635 goto again;
3636 }
Chris Masonec44a352008-04-28 15:29:52 -04003637error:
3638 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003639 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003640 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003641 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003642 if (!ret)
3643 ret = -ENOSPC;
3644 }
3645
Chris Masonec44a352008-04-28 15:29:52 -04003646 return ret;
3647}
3648
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003649/**
3650 * alloc_profile_is_valid - see if a given profile is valid and reduced
3651 * @flags: profile to validate
3652 * @extended: if true @flags is treated as an extended profile
3653 */
3654static int alloc_profile_is_valid(u64 flags, int extended)
3655{
3656 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3657 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3658
3659 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3660
3661 /* 1) check that all other bits are zeroed */
3662 if (flags & ~mask)
3663 return 0;
3664
3665 /* 2) see if profile is reduced */
3666 if (flags == 0)
3667 return !extended; /* "0" is valid for usual profiles */
3668
3669 /* true if exactly one bit set */
3670 return (flags & (flags - 1)) == 0;
3671}
3672
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003673static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3674{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003675 /* cancel requested || normal exit path */
3676 return atomic_read(&fs_info->balance_cancel_req) ||
3677 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3678 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003679}
3680
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003681/* Non-zero return value signifies invalidity */
3682static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3683 u64 allowed)
3684{
3685 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3686 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3687 (bctl_arg->target & ~allowed)));
3688}
3689
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003690/*
David Sterbadccdb072018-03-21 00:20:05 +01003691 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003692 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003693int btrfs_balance(struct btrfs_fs_info *fs_info,
3694 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003695 struct btrfs_ioctl_balance_args *bargs)
3696{
Adam Borowski14506122017-03-07 23:34:44 +01003697 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003698 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003699 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003700 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003701 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003702 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003703
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003704 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003705 atomic_read(&fs_info->balance_pause_req) ||
3706 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003707 ret = -EINVAL;
3708 goto out;
3709 }
3710
Ilya Dryomove4837f82012-03-27 17:09:17 +03003711 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3712 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3713 mixed = 1;
3714
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003715 /*
3716 * In case of mixed groups both data and meta should be picked,
3717 * and identical options should be given for both of them.
3718 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003719 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3720 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003721 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3722 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3723 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003724 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003725 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003726 ret = -EINVAL;
3727 goto out;
3728 }
3729 }
3730
Stefan Behrens8dabb742012-11-06 13:15:27 +01003731 num_devices = fs_info->fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01003732 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003733 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3734 BUG_ON(num_devices < 1);
3735 num_devices--;
3736 }
David Sterba7e79cb82018-03-24 02:11:38 +01003737 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003738 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3739 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003740 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003741 if (num_devices > 2)
3742 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3743 if (num_devices > 3)
3744 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3745 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003746 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003747 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
3748
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003749 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003750 "balance: invalid convert data profile %s",
3751 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003752 ret = -EINVAL;
3753 goto out;
3754 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003755 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003756 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
3757
Frank Holtonefe120a2013-12-20 11:37:06 -05003758 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003759 "balance: invalid convert metadata profile %s",
3760 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003761 ret = -EINVAL;
3762 goto out;
3763 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003764 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003765 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
3766
Frank Holtonefe120a2013-12-20 11:37:06 -05003767 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003768 "balance: invalid convert system profile %s",
3769 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003770 ret = -EINVAL;
3771 goto out;
3772 }
3773
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003774 /* allow to reduce meta or sys integrity only if force set */
3775 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003776 BTRFS_BLOCK_GROUP_RAID10 |
3777 BTRFS_BLOCK_GROUP_RAID5 |
3778 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003779 do {
3780 seq = read_seqbegin(&fs_info->profiles_lock);
3781
3782 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3783 (fs_info->avail_system_alloc_bits & allowed) &&
3784 !(bctl->sys.target & allowed)) ||
3785 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3786 (fs_info->avail_metadata_alloc_bits & allowed) &&
3787 !(bctl->meta.target & allowed))) {
3788 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003789 btrfs_info(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003790 "balance: force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003791 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003792 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003793 "balance: reduces metadata integrity, use --force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003794 ret = -EINVAL;
3795 goto out;
3796 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003797 }
Miao Xiede98ced2013-01-29 10:13:12 +00003798 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003799
Adam Borowski14506122017-03-07 23:34:44 +01003800 /* if we're not converting, the target field is uninitialized */
3801 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3802 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3803 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3804 bctl->data.target : fs_info->avail_data_alloc_bits;
3805 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3806 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003807 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
3808 int data_index = btrfs_bg_flags_to_raid_index(data_target);
3809
Sam Tygieree592d02016-01-06 08:46:12 +00003810 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003811 "balance: metadata profile %s has lower redundancy than data profile %s",
3812 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00003813 }
3814
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003815 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003816 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003817 goto out;
3818
Ilya Dryomov59641012012-01-16 22:04:48 +02003819 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3820 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01003821 BUG_ON(fs_info->balance_ctl);
3822 spin_lock(&fs_info->balance_lock);
3823 fs_info->balance_ctl = bctl;
3824 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02003825 } else {
3826 BUG_ON(ret != -EEXIST);
3827 spin_lock(&fs_info->balance_lock);
3828 update_balance_args(bctl);
3829 spin_unlock(&fs_info->balance_lock);
3830 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003831
David Sterba3009a622018-03-21 01:31:04 +01003832 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
3833 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003834 mutex_unlock(&fs_info->balance_mutex);
3835
3836 ret = __btrfs_balance(fs_info);
3837
3838 mutex_lock(&fs_info->balance_mutex);
David Sterba3009a622018-03-21 01:31:04 +01003839 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003840
3841 if (bargs) {
3842 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01003843 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003844 }
3845
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003846 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3847 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01003848 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003849 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003850 }
3851
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003852 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003853
3854 return ret;
3855out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003856 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01003857 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003858 else
Ilya Dryomov59641012012-01-16 22:04:48 +02003859 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01003860 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3861
Ilya Dryomov59641012012-01-16 22:04:48 +02003862 return ret;
3863}
3864
3865static int balance_kthread(void *data)
3866{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003867 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003868 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003869
Ilya Dryomov59641012012-01-16 22:04:48 +02003870 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003871 if (fs_info->balance_ctl) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003872 btrfs_info(fs_info, "balance: resuming");
David Sterba6fcf6e22018-05-07 17:44:03 +02003873 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003874 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003875 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003876
Ilya Dryomov59641012012-01-16 22:04:48 +02003877 return ret;
3878}
3879
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003880int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3881{
3882 struct task_struct *tsk;
3883
David Sterba1354e1a2018-03-21 02:29:13 +01003884 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003885 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01003886 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003887 return 0;
3888 }
David Sterba1354e1a2018-03-21 02:29:13 +01003889 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003890
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003891 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003892 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003893 return 0;
3894 }
3895
Anand Jain02ee6542018-05-17 15:16:51 +08003896 /*
3897 * A ro->rw remount sequence should continue with the paused balance
3898 * regardless of who pauses it, system or the user as of now, so set
3899 * the resume flag.
3900 */
3901 spin_lock(&fs_info->balance_lock);
3902 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
3903 spin_unlock(&fs_info->balance_lock);
3904
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003905 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303906 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003907}
3908
Ilya Dryomov68310a52012-06-22 12:24:12 -06003909int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003910{
Ilya Dryomov59641012012-01-16 22:04:48 +02003911 struct btrfs_balance_control *bctl;
3912 struct btrfs_balance_item *item;
3913 struct btrfs_disk_balance_args disk_bargs;
3914 struct btrfs_path *path;
3915 struct extent_buffer *leaf;
3916 struct btrfs_key key;
3917 int ret;
3918
3919 path = btrfs_alloc_path();
3920 if (!path)
3921 return -ENOMEM;
3922
Ilya Dryomov68310a52012-06-22 12:24:12 -06003923 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003924 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003925 key.offset = 0;
3926
3927 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3928 if (ret < 0)
3929 goto out;
3930 if (ret > 0) { /* ret = -ENOENT; */
3931 ret = 0;
3932 goto out;
3933 }
3934
Ilya Dryomov59641012012-01-16 22:04:48 +02003935 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3936 if (!bctl) {
3937 ret = -ENOMEM;
3938 goto out;
3939 }
3940
Ilya Dryomov59641012012-01-16 22:04:48 +02003941 leaf = path->nodes[0];
3942 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3943
Ilya Dryomov68310a52012-06-22 12:24:12 -06003944 bctl->flags = btrfs_balance_flags(leaf, item);
3945 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003946
3947 btrfs_balance_data(leaf, item, &disk_bargs);
3948 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3949 btrfs_balance_meta(leaf, item, &disk_bargs);
3950 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3951 btrfs_balance_sys(leaf, item, &disk_bargs);
3952 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3953
David Sterbaeee95e32018-03-20 20:07:58 +01003954 /*
3955 * This should never happen, as the paused balance state is recovered
3956 * during mount without any chance of other exclusive ops to collide.
3957 *
3958 * This gives the exclusive op status to balance and keeps in paused
3959 * state until user intervention (cancel or umount). If the ownership
3960 * cannot be assigned, show a message but do not fail. The balance
3961 * is in a paused state and must have fs_info::balance_ctl properly
3962 * set up.
3963 */
3964 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
3965 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003966 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003967
Ilya Dryomov68310a52012-06-22 12:24:12 -06003968 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01003969 BUG_ON(fs_info->balance_ctl);
3970 spin_lock(&fs_info->balance_lock);
3971 fs_info->balance_ctl = bctl;
3972 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06003973 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003974out:
3975 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003976 return ret;
3977}
3978
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003979int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3980{
3981 int ret = 0;
3982
3983 mutex_lock(&fs_info->balance_mutex);
3984 if (!fs_info->balance_ctl) {
3985 mutex_unlock(&fs_info->balance_mutex);
3986 return -ENOTCONN;
3987 }
3988
David Sterba3009a622018-03-21 01:31:04 +01003989 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003990 atomic_inc(&fs_info->balance_pause_req);
3991 mutex_unlock(&fs_info->balance_mutex);
3992
3993 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01003994 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003995
3996 mutex_lock(&fs_info->balance_mutex);
3997 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01003998 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003999 atomic_dec(&fs_info->balance_pause_req);
4000 } else {
4001 ret = -ENOTCONN;
4002 }
4003
4004 mutex_unlock(&fs_info->balance_mutex);
4005 return ret;
4006}
4007
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004008int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4009{
4010 mutex_lock(&fs_info->balance_mutex);
4011 if (!fs_info->balance_ctl) {
4012 mutex_unlock(&fs_info->balance_mutex);
4013 return -ENOTCONN;
4014 }
4015
David Sterbacf7d20f2018-03-21 01:45:32 +01004016 /*
4017 * A paused balance with the item stored on disk can be resumed at
4018 * mount time if the mount is read-write. Otherwise it's still paused
4019 * and we must not allow cancelling as it deletes the item.
4020 */
4021 if (sb_rdonly(fs_info->sb)) {
4022 mutex_unlock(&fs_info->balance_mutex);
4023 return -EROFS;
4024 }
4025
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004026 atomic_inc(&fs_info->balance_cancel_req);
4027 /*
4028 * if we are running just wait and return, balance item is
4029 * deleted in btrfs_balance in this case
4030 */
David Sterba3009a622018-03-21 01:31:04 +01004031 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004032 mutex_unlock(&fs_info->balance_mutex);
4033 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004034 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004035 mutex_lock(&fs_info->balance_mutex);
4036 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004037 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004038 /*
4039 * Lock released to allow other waiters to continue, we'll
4040 * reexamine the status again.
4041 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004042 mutex_lock(&fs_info->balance_mutex);
4043
David Sterbaa17c95d2018-03-20 17:28:05 +01004044 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004045 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004046 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004047 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004048 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004049 }
4050
David Sterba3009a622018-03-21 01:31:04 +01004051 BUG_ON(fs_info->balance_ctl ||
4052 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004053 atomic_dec(&fs_info->balance_cancel_req);
4054 mutex_unlock(&fs_info->balance_mutex);
4055 return 0;
4056}
4057
Stefan Behrens803b2f52013-08-15 17:11:21 +02004058static int btrfs_uuid_scan_kthread(void *data)
4059{
4060 struct btrfs_fs_info *fs_info = data;
4061 struct btrfs_root *root = fs_info->tree_root;
4062 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004063 struct btrfs_path *path = NULL;
4064 int ret = 0;
4065 struct extent_buffer *eb;
4066 int slot;
4067 struct btrfs_root_item root_item;
4068 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004069 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004070
4071 path = btrfs_alloc_path();
4072 if (!path) {
4073 ret = -ENOMEM;
4074 goto out;
4075 }
4076
4077 key.objectid = 0;
4078 key.type = BTRFS_ROOT_ITEM_KEY;
4079 key.offset = 0;
4080
Stefan Behrens803b2f52013-08-15 17:11:21 +02004081 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004082 ret = btrfs_search_forward(root, &key, path,
4083 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004084 if (ret) {
4085 if (ret > 0)
4086 ret = 0;
4087 break;
4088 }
4089
4090 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4091 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4092 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4093 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4094 goto skip;
4095
4096 eb = path->nodes[0];
4097 slot = path->slots[0];
4098 item_size = btrfs_item_size_nr(eb, slot);
4099 if (item_size < sizeof(root_item))
4100 goto skip;
4101
Stefan Behrens803b2f52013-08-15 17:11:21 +02004102 read_extent_buffer(eb, &root_item,
4103 btrfs_item_ptr_offset(eb, slot),
4104 (int)sizeof(root_item));
4105 if (btrfs_root_refs(&root_item) == 0)
4106 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004107
4108 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4109 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4110 if (trans)
4111 goto update_tree;
4112
4113 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004114 /*
4115 * 1 - subvol uuid item
4116 * 1 - received_subvol uuid item
4117 */
4118 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4119 if (IS_ERR(trans)) {
4120 ret = PTR_ERR(trans);
4121 break;
4122 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004123 continue;
4124 } else {
4125 goto skip;
4126 }
4127update_tree:
4128 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004129 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004130 BTRFS_UUID_KEY_SUBVOL,
4131 key.objectid);
4132 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004133 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004134 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004135 break;
4136 }
4137 }
4138
4139 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004140 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004141 root_item.received_uuid,
4142 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4143 key.objectid);
4144 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004145 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004146 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004147 break;
4148 }
4149 }
4150
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004151skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004152 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004153 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004154 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004155 if (ret)
4156 break;
4157 }
4158
Stefan Behrens803b2f52013-08-15 17:11:21 +02004159 btrfs_release_path(path);
4160 if (key.offset < (u64)-1) {
4161 key.offset++;
4162 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4163 key.offset = 0;
4164 key.type = BTRFS_ROOT_ITEM_KEY;
4165 } else if (key.objectid < (u64)-1) {
4166 key.offset = 0;
4167 key.type = BTRFS_ROOT_ITEM_KEY;
4168 key.objectid++;
4169 } else {
4170 break;
4171 }
4172 cond_resched();
4173 }
4174
4175out:
4176 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004177 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004178 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004179 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004180 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004181 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004182 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004183 up(&fs_info->uuid_tree_rescan_sem);
4184 return 0;
4185}
4186
Stefan Behrens70f80172013-08-15 17:11:23 +02004187/*
4188 * Callback for btrfs_uuid_tree_iterate().
4189 * returns:
4190 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004191 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004192 * > 0 if the check failed, which means the caller shall remove the entry.
4193 */
4194static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4195 u8 *uuid, u8 type, u64 subid)
4196{
4197 struct btrfs_key key;
4198 int ret = 0;
4199 struct btrfs_root *subvol_root;
4200
4201 if (type != BTRFS_UUID_KEY_SUBVOL &&
4202 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4203 goto out;
4204
4205 key.objectid = subid;
4206 key.type = BTRFS_ROOT_ITEM_KEY;
4207 key.offset = (u64)-1;
4208 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4209 if (IS_ERR(subvol_root)) {
4210 ret = PTR_ERR(subvol_root);
4211 if (ret == -ENOENT)
4212 ret = 1;
4213 goto out;
4214 }
4215
4216 switch (type) {
4217 case BTRFS_UUID_KEY_SUBVOL:
4218 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4219 ret = 1;
4220 break;
4221 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4222 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4223 BTRFS_UUID_SIZE))
4224 ret = 1;
4225 break;
4226 }
4227
4228out:
4229 return ret;
4230}
4231
4232static int btrfs_uuid_rescan_kthread(void *data)
4233{
4234 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4235 int ret;
4236
4237 /*
4238 * 1st step is to iterate through the existing UUID tree and
4239 * to delete all entries that contain outdated data.
4240 * 2nd step is to add all missing entries to the UUID tree.
4241 */
4242 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4243 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004244 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004245 up(&fs_info->uuid_tree_rescan_sem);
4246 return ret;
4247 }
4248 return btrfs_uuid_scan_kthread(data);
4249}
4250
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004251int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4252{
4253 struct btrfs_trans_handle *trans;
4254 struct btrfs_root *tree_root = fs_info->tree_root;
4255 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004256 struct task_struct *task;
4257 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004258
4259 /*
4260 * 1 - root node
4261 * 1 - root item
4262 */
4263 trans = btrfs_start_transaction(tree_root, 2);
4264 if (IS_ERR(trans))
4265 return PTR_ERR(trans);
4266
4267 uuid_root = btrfs_create_tree(trans, fs_info,
4268 BTRFS_UUID_TREE_OBJECTID);
4269 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004270 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004271 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004272 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004273 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004274 }
4275
4276 fs_info->uuid_root = uuid_root;
4277
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004278 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004279 if (ret)
4280 return ret;
4281
4282 down(&fs_info->uuid_tree_rescan_sem);
4283 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4284 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004285 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004286 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004287 up(&fs_info->uuid_tree_rescan_sem);
4288 return PTR_ERR(task);
4289 }
4290
4291 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004292}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004293
Stefan Behrens70f80172013-08-15 17:11:23 +02004294int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4295{
4296 struct task_struct *task;
4297
4298 down(&fs_info->uuid_tree_rescan_sem);
4299 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4300 if (IS_ERR(task)) {
4301 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004302 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004303 up(&fs_info->uuid_tree_rescan_sem);
4304 return PTR_ERR(task);
4305 }
4306
4307 return 0;
4308}
4309
Chris Mason8f18cf12008-04-25 16:53:30 -04004310/*
4311 * shrinking a device means finding all of the device extents past
4312 * the new size, and then following the back refs to the chunks.
4313 * The chunk relocation code actually frees the device extent
4314 */
4315int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4316{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004317 struct btrfs_fs_info *fs_info = device->fs_info;
4318 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004319 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004320 struct btrfs_dev_extent *dev_extent = NULL;
4321 struct btrfs_path *path;
4322 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004323 u64 chunk_offset;
4324 int ret;
4325 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004326 int failed = 0;
4327 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004328 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004329 struct extent_buffer *l;
4330 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004331 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004332 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004333 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004334 u64 diff;
4335
4336 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004337 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004338
Anand Jain401e29c2017-12-04 12:54:55 +08004339 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004340 return -EINVAL;
4341
Chris Mason8f18cf12008-04-25 16:53:30 -04004342 path = btrfs_alloc_path();
4343 if (!path)
4344 return -ENOMEM;
4345
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004346 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004347
David Sterba34441362016-10-04 19:34:27 +02004348 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004349
Miao Xie7cc8e582014-09-03 21:35:38 +08004350 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004351 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004352 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004353 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004354 }
David Sterba34441362016-10-04 19:34:27 +02004355 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004356
Josef Bacikba1bf482009-09-11 16:11:19 -04004357again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004358 key.objectid = device->devid;
4359 key.offset = (u64)-1;
4360 key.type = BTRFS_DEV_EXTENT_KEY;
4361
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004362 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004363 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004365 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004366 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004367 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004368 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004369
4370 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004371 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004372 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004373 if (ret < 0)
4374 goto done;
4375 if (ret) {
4376 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004377 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004378 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004379 }
4380
4381 l = path->nodes[0];
4382 slot = path->slots[0];
4383 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4384
Josef Bacikba1bf482009-09-11 16:11:19 -04004385 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004386 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004387 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004388 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004389 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004390
4391 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4392 length = btrfs_dev_extent_length(l, dev_extent);
4393
Josef Bacikba1bf482009-09-11 16:11:19 -04004394 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004395 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004396 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004397 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004398 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004399
Chris Mason8f18cf12008-04-25 16:53:30 -04004400 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004401 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004402
Liu Boa6f93c72017-11-15 16:28:11 -07004403 /*
4404 * We may be relocating the only data chunk we have,
4405 * which could potentially end up with losing data's
4406 * raid profile, so lets allocate an empty one in
4407 * advance.
4408 */
4409 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4410 if (ret < 0) {
4411 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4412 goto done;
4413 }
4414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004415 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4416 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004417 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004418 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004419 if (ret == -ENOSPC)
4420 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004421 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004422
4423 if (failed && !retried) {
4424 failed = 0;
4425 retried = true;
4426 goto again;
4427 } else if (failed && retried) {
4428 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004429 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004430 }
4431
Chris Balld6397ba2009-04-27 07:29:03 -04004432 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004433 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004434 if (IS_ERR(trans)) {
4435 ret = PTR_ERR(trans);
4436 goto done;
4437 }
4438
David Sterba34441362016-10-04 19:34:27 +02004439 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004440
4441 /*
4442 * We checked in the above loop all device extents that were already in
4443 * the device tree. However before we have updated the device's
4444 * total_bytes to the new size, we might have had chunk allocations that
4445 * have not complete yet (new block groups attached to transaction
4446 * handles), and therefore their device extents were not yet in the
4447 * device tree and we missed them in the loop above. So if we have any
4448 * pending chunk using a device extent that overlaps the device range
4449 * that we can not use anymore, commit the current transaction and
4450 * repeat the search on the device tree - this way we guarantee we will
4451 * not have chunks using device extents that end beyond 'new_size'.
4452 */
4453 if (!checked_pending_chunks) {
4454 u64 start = new_size;
4455 u64 len = old_size - new_size;
4456
Jeff Mahoney499f3772015-06-15 09:41:17 -04004457 if (contains_pending_extent(trans->transaction, device,
4458 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004459 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004460 checked_pending_chunks = true;
4461 failed = 0;
4462 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004463 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004464 if (ret)
4465 goto done;
4466 goto again;
4467 }
4468 }
4469
Miao Xie7cc8e582014-09-03 21:35:38 +08004470 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004471 if (list_empty(&device->resized_list))
4472 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004473 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004474
Chris Balld6397ba2009-04-27 07:29:03 -04004475 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004476 btrfs_set_super_total_bytes(super_copy,
4477 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004478 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004479
4480 /* Now btrfs_update_device() will change the on-disk size. */
4481 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004482 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004483done:
4484 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004485 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004486 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004487 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004488 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004489 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004490 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004491 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004492 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004493 return ret;
4494}
4495
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004496static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004497 struct btrfs_key *key,
4498 struct btrfs_chunk *chunk, int item_size)
4499{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004500 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004501 struct btrfs_disk_key disk_key;
4502 u32 array_size;
4503 u8 *ptr;
4504
David Sterba34441362016-10-04 19:34:27 +02004505 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004506 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004507 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004508 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004509 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004510 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004511 }
Chris Mason0b86a832008-03-24 15:01:56 -04004512
4513 ptr = super_copy->sys_chunk_array + array_size;
4514 btrfs_cpu_key_to_disk(&disk_key, key);
4515 memcpy(ptr, &disk_key, sizeof(disk_key));
4516 ptr += sizeof(disk_key);
4517 memcpy(ptr, chunk, item_size);
4518 item_size += sizeof(disk_key);
4519 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004520 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004521
Chris Mason0b86a832008-03-24 15:01:56 -04004522 return 0;
4523}
4524
Miao Xieb2117a32011-01-05 10:07:28 +00004525/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004526 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004527 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004528static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004529{
Arne Jansen73c5de02011-04-12 12:07:57 +02004530 const struct btrfs_device_info *di_a = a;
4531 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004532
Arne Jansen73c5de02011-04-12 12:07:57 +02004533 if (di_a->max_avail > di_b->max_avail)
4534 return -1;
4535 if (di_a->max_avail < di_b->max_avail)
4536 return 1;
4537 if (di_a->total_avail > di_b->total_avail)
4538 return -1;
4539 if (di_a->total_avail < di_b->total_avail)
4540 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004541 return 0;
4542}
4543
David Woodhouse53b381b2013-01-29 18:40:14 -05004544static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4545{
Zhao Leiffe2d202015-01-20 15:11:44 +08004546 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004547 return;
4548
Miao Xieceda0862013-04-11 10:30:16 +00004549 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004550}
4551
Qu Wenruo062d4d12018-01-30 18:20:46 +08004552#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004553 - sizeof(struct btrfs_chunk)) \
4554 / sizeof(struct btrfs_stripe) + 1)
4555
4556#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4557 - 2 * sizeof(struct btrfs_disk_key) \
4558 - 2 * sizeof(struct btrfs_chunk)) \
4559 / sizeof(struct btrfs_stripe) + 1)
4560
Miao Xieb2117a32011-01-05 10:07:28 +00004561static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004562 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004563{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004564 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004565 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004566 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004567 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004568 struct extent_map_tree *em_tree;
4569 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004570 struct btrfs_device_info *devices_info = NULL;
4571 u64 total_avail;
4572 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004573 int data_stripes; /* number of stripes that count for
4574 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004575 int sub_stripes; /* sub_stripes info for map */
4576 int dev_stripes; /* stripes per dev */
4577 int devs_max; /* max devs to use */
4578 int devs_min; /* min devs needed */
4579 int devs_increment; /* ndevs has to be a multiple of this */
4580 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004581 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004582 u64 max_stripe_size;
4583 u64 max_chunk_size;
4584 u64 stripe_size;
4585 u64 num_bytes;
4586 int ndevs;
4587 int i;
4588 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004589 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004590
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004591 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004592
Qu Wenruo4117f202018-01-22 13:50:54 +08004593 if (list_empty(&fs_devices->alloc_list)) {
4594 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4595 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004596 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004597 }
Miao Xieb2117a32011-01-05 10:07:28 +00004598
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004599 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004600
Liu Bo31e50222012-11-21 14:18:10 +00004601 sub_stripes = btrfs_raid_array[index].sub_stripes;
4602 dev_stripes = btrfs_raid_array[index].dev_stripes;
4603 devs_max = btrfs_raid_array[index].devs_max;
4604 devs_min = btrfs_raid_array[index].devs_min;
4605 devs_increment = btrfs_raid_array[index].devs_increment;
4606 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004607
4608 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004609 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08004610 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004611 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004612 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004613 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004614 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004615 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4616 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004617 else
Byongho Leeee221842015-12-15 01:42:10 +09004618 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004619 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004620 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004621 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004622 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004623 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004624 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004625 if (!devs_max)
4626 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004627 } else {
David Sterba351fd352014-05-15 16:48:20 +02004628 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004629 type);
4630 BUG_ON(1);
4631 }
4632
4633 /* we don't want a chunk larger than 10% of writeable space */
4634 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4635 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004636
David Sterba31e818f2015-02-20 18:00:26 +01004637 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004638 GFP_NOFS);
4639 if (!devices_info)
4640 return -ENOMEM;
4641
Arne Jansen73c5de02011-04-12 12:07:57 +02004642 /*
4643 * in the first pass through the devices list, we gather information
4644 * about the available holes on each device.
4645 */
4646 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004647 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004648 u64 max_avail;
4649 u64 dev_offset;
4650
Anand Jainebbede42017-12-04 12:54:52 +08004651 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004652 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004653 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004654 continue;
4655 }
4656
Anand Jaine12c9622017-12-04 12:54:53 +08004657 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4658 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004659 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004660 continue;
4661
4662 if (device->total_bytes > device->bytes_used)
4663 total_avail = device->total_bytes - device->bytes_used;
4664 else
4665 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004666
4667 /* If there is no space on this device, skip it. */
4668 if (total_avail == 0)
4669 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004670
Josef Bacik6df9a952013-06-27 13:22:46 -04004671 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004672 max_stripe_size * dev_stripes,
4673 &dev_offset, &max_avail);
4674 if (ret && ret != -ENOSPC)
4675 goto error;
4676
4677 if (ret == 0)
4678 max_avail = max_stripe_size * dev_stripes;
4679
Qu Wenruo4117f202018-01-22 13:50:54 +08004680 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4681 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4682 btrfs_debug(info,
4683 "%s: devid %llu has no free space, have=%llu want=%u",
4684 __func__, device->devid, max_avail,
4685 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004686 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004687 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004688
Eric Sandeen063d0062013-01-31 00:55:01 +00004689 if (ndevs == fs_devices->rw_devices) {
4690 WARN(1, "%s: found more than %llu devices\n",
4691 __func__, fs_devices->rw_devices);
4692 break;
4693 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004694 devices_info[ndevs].dev_offset = dev_offset;
4695 devices_info[ndevs].max_avail = max_avail;
4696 devices_info[ndevs].total_avail = total_avail;
4697 devices_info[ndevs].dev = device;
4698 ++ndevs;
4699 }
4700
4701 /*
4702 * now sort the devices by hole size / available space
4703 */
4704 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4705 btrfs_cmp_device_info, NULL);
4706
4707 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004708 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004709
Qu Wenruoba89b802018-01-31 13:56:15 +08004710 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004711 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004712 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4713 btrfs_debug(info,
4714 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004715 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004716 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004717 goto error;
4718 }
4719
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004720 ndevs = min(ndevs, devs_max);
4721
Arne Jansen73c5de02011-04-12 12:07:57 +02004722 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004723 * The primary goal is to maximize the number of stripes, so use as
4724 * many devices as possible, even if the stripes are not maximum sized.
4725 *
4726 * The DUP profile stores more than one stripe per device, the
4727 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004728 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004729 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004730 num_stripes = ndevs * dev_stripes;
4731
David Woodhouse53b381b2013-01-29 18:40:14 -05004732 /*
4733 * this will have to be fixed for RAID1 and RAID10 over
4734 * more drives
4735 */
4736 data_stripes = num_stripes / ncopies;
4737
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004738 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004739 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004740
4741 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004742 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004743
4744 /*
4745 * Use the number of data stripes to figure out how big this chunk
4746 * is really going to be in terms of logical address space,
4747 * and compare that answer with the max chunk size
4748 */
4749 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004750 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004751
4752 /* bump the answer up to a 16MB boundary */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004753 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004754
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004755 /*
4756 * But don't go higher than the limits we found while searching
4757 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004758 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004759 stripe_size = min(devices_info[ndevs - 1].max_avail,
4760 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004761 }
4762
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004763 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004764 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004765
Miao Xieb2117a32011-01-05 10:07:28 +00004766 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4767 if (!map) {
4768 ret = -ENOMEM;
4769 goto error;
4770 }
4771 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004772
Arne Jansen73c5de02011-04-12 12:07:57 +02004773 for (i = 0; i < ndevs; ++i) {
4774 for (j = 0; j < dev_stripes; ++j) {
4775 int s = i * dev_stripes + j;
4776 map->stripes[s].dev = devices_info[i].dev;
4777 map->stripes[s].physical = devices_info[i].dev_offset +
4778 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004779 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004780 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004781 map->stripe_len = BTRFS_STRIPE_LEN;
4782 map->io_align = BTRFS_STRIPE_LEN;
4783 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004784 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004785 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004786
David Woodhouse53b381b2013-01-29 18:40:14 -05004787 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004788
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004789 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004790
David Sterba172ddd62011-04-21 00:48:27 +02004791 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004792 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004793 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004794 ret = -ENOMEM;
4795 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004796 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004797 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004798 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004799 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004800 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004801 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004802 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004803 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004804
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004805 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004806 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004807 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004808 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004809 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004810 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004811 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004812 }
Yan Zheng2b820322008-11-17 21:11:30 -05004813
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004814 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4815 refcount_inc(&em->refs);
4816 write_unlock(&em_tree->lock);
4817
Nikolay Borisove7e02092018-06-20 15:48:55 +03004818 ret = btrfs_make_block_group(trans, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004819 if (ret)
4820 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004821
Miao Xie7cc8e582014-09-03 21:35:38 +08004822 for (i = 0; i < map->num_stripes; i++) {
4823 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4824 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4825 }
Miao Xie43530c42014-09-03 21:35:36 +08004826
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004827 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004828
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004829 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004830 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004831
Miao Xieb2117a32011-01-05 10:07:28 +00004832 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004833 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004834
Josef Bacik6df9a952013-06-27 13:22:46 -04004835error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004836 write_lock(&em_tree->lock);
4837 remove_extent_mapping(em_tree, em);
4838 write_unlock(&em_tree->lock);
4839
4840 /* One for our allocation */
4841 free_extent_map(em);
4842 /* One for the tree reference */
4843 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004844 /* One for the pending_chunks list reference */
4845 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004846error:
Miao Xieb2117a32011-01-05 10:07:28 +00004847 kfree(devices_info);
4848 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004849}
4850
Josef Bacik6df9a952013-06-27 13:22:46 -04004851int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004852 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004853 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004854{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004855 struct btrfs_root *extent_root = fs_info->extent_root;
4856 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004857 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004858 struct btrfs_device *device;
4859 struct btrfs_chunk *chunk;
4860 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004861 struct extent_map *em;
4862 struct map_lookup *map;
4863 size_t item_size;
4864 u64 dev_offset;
4865 u64 stripe_size;
4866 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004867 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004868
Liu Bo592d92e2017-03-14 13:33:55 -07004869 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4870 if (IS_ERR(em))
4871 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004872
Jeff Mahoney95617d62015-06-03 10:55:48 -04004873 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004874 item_size = btrfs_chunk_item_size(map->num_stripes);
4875 stripe_size = em->orig_block_len;
4876
4877 chunk = kzalloc(item_size, GFP_NOFS);
4878 if (!chunk) {
4879 ret = -ENOMEM;
4880 goto out;
4881 }
4882
Filipe Manana50460e32015-11-20 10:42:47 +00004883 /*
4884 * Take the device list mutex to prevent races with the final phase of
4885 * a device replace operation that replaces the device object associated
4886 * with the map's stripes, because the device object's id can change
4887 * at any time during that final phase of the device replace operation
4888 * (dev-replace.c:btrfs_dev_replace_finishing()).
4889 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004890 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004891 for (i = 0; i < map->num_stripes; i++) {
4892 device = map->stripes[i].dev;
4893 dev_offset = map->stripes[i].physical;
4894
Yan Zheng2b820322008-11-17 21:11:30 -05004895 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004896 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004897 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004898 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4899 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004900 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004901 break;
4902 }
4903 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004904 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004905 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004906 }
4907
Yan Zheng2b820322008-11-17 21:11:30 -05004908 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004909 for (i = 0; i < map->num_stripes; i++) {
4910 device = map->stripes[i].dev;
4911 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004912
4913 btrfs_set_stack_stripe_devid(stripe, device->devid);
4914 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4915 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4916 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004917 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004918 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004919
4920 btrfs_set_stack_chunk_length(chunk, chunk_size);
4921 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4922 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4923 btrfs_set_stack_chunk_type(chunk, map->type);
4924 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4925 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4926 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004927 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05004928 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4929
4930 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4931 key.type = BTRFS_CHUNK_ITEM_KEY;
4932 key.offset = chunk_offset;
4933
4934 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004935 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4936 /*
4937 * TODO: Cleanup of inserted chunk root in case of
4938 * failure.
4939 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004940 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004941 }
liubo1abe9b82011-03-24 11:18:59 +00004942
Josef Bacik6df9a952013-06-27 13:22:46 -04004943out:
Yan Zheng2b820322008-11-17 21:11:30 -05004944 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004945 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004946 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004947}
4948
4949/*
4950 * Chunk allocation falls into two parts. The first part does works
4951 * that make the new allocated chunk useable, but not do any operation
4952 * that modifies the chunk tree. The second part does the works that
4953 * require modifying the chunk tree. This division is important for the
4954 * bootstrap process of adding storage to a seed btrfs.
4955 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03004956int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05004957{
4958 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004959
Nikolay Borisovc216b202018-06-20 15:49:06 +03004960 lockdep_assert_held(&trans->fs_info->chunk_mutex);
4961 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004962 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004963}
4964
Chris Masond3977122009-01-05 21:25:51 -05004965static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01004966 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05004967{
4968 u64 chunk_offset;
4969 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004970 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004971 int ret;
4972
Josef Bacik6df9a952013-06-27 13:22:46 -04004973 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004974 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004975 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004976 if (ret)
4977 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004978
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004979 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04004980 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01004981 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004982 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004983}
4984
Miao Xied20983b2014-07-03 18:22:13 +08004985static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4986{
4987 int max_errors;
4988
4989 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4990 BTRFS_BLOCK_GROUP_RAID10 |
4991 BTRFS_BLOCK_GROUP_RAID5 |
4992 BTRFS_BLOCK_GROUP_DUP)) {
4993 max_errors = 1;
4994 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4995 max_errors = 2;
4996 } else {
4997 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004998 }
4999
Miao Xied20983b2014-07-03 18:22:13 +08005000 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005001}
5002
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005003int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005004{
5005 struct extent_map *em;
5006 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005007 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005008 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005009 int i;
5010
Liu Bo592d92e2017-03-14 13:33:55 -07005011 em = get_chunk_map(fs_info, chunk_offset, 1);
5012 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005013 return 1;
5014
Jeff Mahoney95617d62015-06-03 10:55:48 -04005015 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005016 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005017 if (test_bit(BTRFS_DEV_STATE_MISSING,
5018 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005019 miss_ndevs++;
5020 continue;
5021 }
Anand Jainebbede42017-12-04 12:54:52 +08005022 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5023 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005024 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005025 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005026 }
5027 }
Miao Xied20983b2014-07-03 18:22:13 +08005028
5029 /*
5030 * If the number of missing devices is larger than max errors,
5031 * we can not write the data into that chunk successfully, so
5032 * set it readonly.
5033 */
5034 if (miss_ndevs > btrfs_chunk_max_errors(map))
5035 readonly = 1;
5036end:
Yan Zheng2b820322008-11-17 21:11:30 -05005037 free_extent_map(em);
5038 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005039}
5040
5041void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5042{
David Sterbaa8067e02011-04-21 00:34:43 +02005043 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005044}
5045
5046void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5047{
5048 struct extent_map *em;
5049
Chris Masond3977122009-01-05 21:25:51 -05005050 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005051 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005052 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5053 if (em)
5054 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005055 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005056 if (!em)
5057 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005058 /* once for us */
5059 free_extent_map(em);
5060 /* once for the tree */
5061 free_extent_map(em);
5062 }
5063}
5064
Stefan Behrens5d964052012-11-05 14:59:07 +01005065int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005066{
5067 struct extent_map *em;
5068 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005069 int ret;
5070
Liu Bo592d92e2017-03-14 13:33:55 -07005071 em = get_chunk_map(fs_info, logical, len);
5072 if (IS_ERR(em))
5073 /*
5074 * We could return errors for these cases, but that could get
5075 * ugly and we'd probably do the same thing which is just not do
5076 * anything else and exit, so return 1 so the callers don't try
5077 * to use other copies.
5078 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005079 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005080
Jeff Mahoney95617d62015-06-03 10:55:48 -04005081 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005082 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5083 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005084 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5085 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005086 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5087 ret = 2;
5088 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005089 /*
5090 * There could be two corrupted data stripes, we need
5091 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005092 *
Liu Bo8810f752018-01-02 13:36:41 -07005093 * Fail a stripe at a time on every retry except the
5094 * stripe under reconstruction.
5095 */
5096 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005097 else
5098 ret = 1;
5099 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005100
David Sterba7e79cb82018-03-24 02:11:38 +01005101 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005102 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5103 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005104 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005105 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005106
Chris Masonf1885912008-04-09 16:28:12 -04005107 return ret;
5108}
5109
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005110unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005111 u64 logical)
5112{
5113 struct extent_map *em;
5114 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005115 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005116
Liu Bo592d92e2017-03-14 13:33:55 -07005117 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005118
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005119 if (!WARN_ON(IS_ERR(em))) {
5120 map = em->map_lookup;
5121 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5122 len = map->stripe_len * nr_data_stripes(map);
5123 free_extent_map(em);
5124 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005125 return len;
5126}
5127
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005128int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005129{
5130 struct extent_map *em;
5131 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005132 int ret = 0;
5133
Liu Bo592d92e2017-03-14 13:33:55 -07005134 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005135
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005136 if(!WARN_ON(IS_ERR(em))) {
5137 map = em->map_lookup;
5138 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5139 ret = 1;
5140 free_extent_map(em);
5141 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005142 return ret;
5143}
5144
Stefan Behrens30d98612012-11-06 14:52:18 +01005145static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005146 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005147 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005148{
5149 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005150 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005151 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005152 int tolerance;
5153 struct btrfs_device *srcdev;
5154
Anand Jain99f92a72018-03-14 16:29:12 +08005155 ASSERT((map->type &
5156 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5157
5158 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5159 num_stripes = map->sub_stripes;
5160 else
5161 num_stripes = map->num_stripes;
5162
Anand Jain8ba0ae72018-03-14 16:29:13 +08005163 preferred_mirror = first + current->pid % num_stripes;
5164
Stefan Behrens30d98612012-11-06 14:52:18 +01005165 if (dev_replace_is_ongoing &&
5166 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5167 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5168 srcdev = fs_info->dev_replace.srcdev;
5169 else
5170 srcdev = NULL;
5171
5172 /*
5173 * try to avoid the drive that is the source drive for a
5174 * dev-replace procedure, only choose it if no other non-missing
5175 * mirror is available
5176 */
5177 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005178 if (map->stripes[preferred_mirror].dev->bdev &&
5179 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5180 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005181 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005182 if (map->stripes[i].dev->bdev &&
5183 (tolerance || map->stripes[i].dev != srcdev))
5184 return i;
5185 }
Chris Masondfe25022008-05-13 13:46:40 -04005186 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005187
Chris Masondfe25022008-05-13 13:46:40 -04005188 /* we couldn't find one that doesn't fail. Just return something
5189 * and the io error handling code will clean up eventually
5190 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005191 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005192}
5193
David Woodhouse53b381b2013-01-29 18:40:14 -05005194static inline int parity_smaller(u64 a, u64 b)
5195{
5196 return a > b;
5197}
5198
5199/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005200static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005201{
5202 struct btrfs_bio_stripe s;
5203 int i;
5204 u64 l;
5205 int again = 1;
5206
5207 while (again) {
5208 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005209 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005210 if (parity_smaller(bbio->raid_map[i],
5211 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005212 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005213 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005214 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005215 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005216 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005217 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005218
David Woodhouse53b381b2013-01-29 18:40:14 -05005219 again = 1;
5220 }
5221 }
5222 }
5223}
5224
Zhao Lei6e9606d2015-01-20 15:11:34 +08005225static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5226{
5227 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005228 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005229 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005230 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005231 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005232 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005233 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005234 /*
5235 * plus the raid_map, which includes both the tgt dev
5236 * and the stripes
5237 */
5238 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005239 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005240
5241 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005242 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005243
5244 return bbio;
5245}
5246
5247void btrfs_get_bbio(struct btrfs_bio *bbio)
5248{
Elena Reshetova140475a2017-03-03 10:55:10 +02005249 WARN_ON(!refcount_read(&bbio->refs));
5250 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005251}
5252
5253void btrfs_put_bbio(struct btrfs_bio *bbio)
5254{
5255 if (!bbio)
5256 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005257 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005258 kfree(bbio);
5259}
5260
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005261/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5262/*
5263 * Please note that, discard won't be sent to target device of device
5264 * replace.
5265 */
5266static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5267 u64 logical, u64 length,
5268 struct btrfs_bio **bbio_ret)
5269{
5270 struct extent_map *em;
5271 struct map_lookup *map;
5272 struct btrfs_bio *bbio;
5273 u64 offset;
5274 u64 stripe_nr;
5275 u64 stripe_nr_end;
5276 u64 stripe_end_offset;
5277 u64 stripe_cnt;
5278 u64 stripe_len;
5279 u64 stripe_offset;
5280 u64 num_stripes;
5281 u32 stripe_index;
5282 u32 factor = 0;
5283 u32 sub_stripes = 0;
5284 u64 stripes_per_dev = 0;
5285 u32 remaining_stripes = 0;
5286 u32 last_stripe = 0;
5287 int ret = 0;
5288 int i;
5289
5290 /* discard always return a bbio */
5291 ASSERT(bbio_ret);
5292
5293 em = get_chunk_map(fs_info, logical, length);
5294 if (IS_ERR(em))
5295 return PTR_ERR(em);
5296
5297 map = em->map_lookup;
5298 /* we don't discard raid56 yet */
5299 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5300 ret = -EOPNOTSUPP;
5301 goto out;
5302 }
5303
5304 offset = logical - em->start;
5305 length = min_t(u64, em->len - offset, length);
5306
5307 stripe_len = map->stripe_len;
5308 /*
5309 * stripe_nr counts the total number of stripes we have to stride
5310 * to get to this block
5311 */
5312 stripe_nr = div64_u64(offset, stripe_len);
5313
5314 /* stripe_offset is the offset of this block in its stripe */
5315 stripe_offset = offset - stripe_nr * stripe_len;
5316
5317 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005318 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005319 stripe_cnt = stripe_nr_end - stripe_nr;
5320 stripe_end_offset = stripe_nr_end * map->stripe_len -
5321 (offset + length);
5322 /*
5323 * after this, stripe_nr is the number of stripes on this
5324 * device we have to walk to find the data, and stripe_index is
5325 * the number of our device in the stripe array
5326 */
5327 num_stripes = 1;
5328 stripe_index = 0;
5329 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5330 BTRFS_BLOCK_GROUP_RAID10)) {
5331 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5332 sub_stripes = 1;
5333 else
5334 sub_stripes = map->sub_stripes;
5335
5336 factor = map->num_stripes / sub_stripes;
5337 num_stripes = min_t(u64, map->num_stripes,
5338 sub_stripes * stripe_cnt);
5339 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5340 stripe_index *= sub_stripes;
5341 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5342 &remaining_stripes);
5343 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5344 last_stripe *= sub_stripes;
5345 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5346 BTRFS_BLOCK_GROUP_DUP)) {
5347 num_stripes = map->num_stripes;
5348 } else {
5349 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5350 &stripe_index);
5351 }
5352
5353 bbio = alloc_btrfs_bio(num_stripes, 0);
5354 if (!bbio) {
5355 ret = -ENOMEM;
5356 goto out;
5357 }
5358
5359 for (i = 0; i < num_stripes; i++) {
5360 bbio->stripes[i].physical =
5361 map->stripes[stripe_index].physical +
5362 stripe_offset + stripe_nr * map->stripe_len;
5363 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5364
5365 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5366 BTRFS_BLOCK_GROUP_RAID10)) {
5367 bbio->stripes[i].length = stripes_per_dev *
5368 map->stripe_len;
5369
5370 if (i / sub_stripes < remaining_stripes)
5371 bbio->stripes[i].length +=
5372 map->stripe_len;
5373
5374 /*
5375 * Special for the first stripe and
5376 * the last stripe:
5377 *
5378 * |-------|...|-------|
5379 * |----------|
5380 * off end_off
5381 */
5382 if (i < sub_stripes)
5383 bbio->stripes[i].length -=
5384 stripe_offset;
5385
5386 if (stripe_index >= last_stripe &&
5387 stripe_index <= (last_stripe +
5388 sub_stripes - 1))
5389 bbio->stripes[i].length -=
5390 stripe_end_offset;
5391
5392 if (i == sub_stripes - 1)
5393 stripe_offset = 0;
5394 } else {
5395 bbio->stripes[i].length = length;
5396 }
5397
5398 stripe_index++;
5399 if (stripe_index == map->num_stripes) {
5400 stripe_index = 0;
5401 stripe_nr++;
5402 }
5403 }
5404
5405 *bbio_ret = bbio;
5406 bbio->map_type = map->type;
5407 bbio->num_stripes = num_stripes;
5408out:
5409 free_extent_map(em);
5410 return ret;
5411}
5412
Liu Bo5ab56092017-03-14 13:33:57 -07005413/*
5414 * In dev-replace case, for repair case (that's the only case where the mirror
5415 * is selected explicitly when calling btrfs_map_block), blocks left of the
5416 * left cursor can also be read from the target drive.
5417 *
5418 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5419 * array of stripes.
5420 * For READ, it also needs to be supported using the same mirror number.
5421 *
5422 * If the requested block is not left of the left cursor, EIO is returned. This
5423 * can happen because btrfs_num_copies() returns one more in the dev-replace
5424 * case.
5425 */
5426static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5427 u64 logical, u64 length,
5428 u64 srcdev_devid, int *mirror_num,
5429 u64 *physical)
5430{
5431 struct btrfs_bio *bbio = NULL;
5432 int num_stripes;
5433 int index_srcdev = 0;
5434 int found = 0;
5435 u64 physical_of_found = 0;
5436 int i;
5437 int ret = 0;
5438
5439 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5440 logical, &length, &bbio, 0, 0);
5441 if (ret) {
5442 ASSERT(bbio == NULL);
5443 return ret;
5444 }
5445
5446 num_stripes = bbio->num_stripes;
5447 if (*mirror_num > num_stripes) {
5448 /*
5449 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5450 * that means that the requested area is not left of the left
5451 * cursor
5452 */
5453 btrfs_put_bbio(bbio);
5454 return -EIO;
5455 }
5456
5457 /*
5458 * process the rest of the function using the mirror_num of the source
5459 * drive. Therefore look it up first. At the end, patch the device
5460 * pointer to the one of the target drive.
5461 */
5462 for (i = 0; i < num_stripes; i++) {
5463 if (bbio->stripes[i].dev->devid != srcdev_devid)
5464 continue;
5465
5466 /*
5467 * In case of DUP, in order to keep it simple, only add the
5468 * mirror with the lowest physical address
5469 */
5470 if (found &&
5471 physical_of_found <= bbio->stripes[i].physical)
5472 continue;
5473
5474 index_srcdev = i;
5475 found = 1;
5476 physical_of_found = bbio->stripes[i].physical;
5477 }
5478
5479 btrfs_put_bbio(bbio);
5480
5481 ASSERT(found);
5482 if (!found)
5483 return -EIO;
5484
5485 *mirror_num = index_srcdev + 1;
5486 *physical = physical_of_found;
5487 return ret;
5488}
5489
Liu Bo73c0f222017-03-14 13:33:58 -07005490static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5491 struct btrfs_bio **bbio_ret,
5492 struct btrfs_dev_replace *dev_replace,
5493 int *num_stripes_ret, int *max_errors_ret)
5494{
5495 struct btrfs_bio *bbio = *bbio_ret;
5496 u64 srcdev_devid = dev_replace->srcdev->devid;
5497 int tgtdev_indexes = 0;
5498 int num_stripes = *num_stripes_ret;
5499 int max_errors = *max_errors_ret;
5500 int i;
5501
5502 if (op == BTRFS_MAP_WRITE) {
5503 int index_where_to_add;
5504
5505 /*
5506 * duplicate the write operations while the dev replace
5507 * procedure is running. Since the copying of the old disk to
5508 * the new disk takes place at run time while the filesystem is
5509 * mounted writable, the regular write operations to the old
5510 * disk have to be duplicated to go to the new disk as well.
5511 *
5512 * Note that device->missing is handled by the caller, and that
5513 * the write to the old disk is already set up in the stripes
5514 * array.
5515 */
5516 index_where_to_add = num_stripes;
5517 for (i = 0; i < num_stripes; i++) {
5518 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5519 /* write to new disk, too */
5520 struct btrfs_bio_stripe *new =
5521 bbio->stripes + index_where_to_add;
5522 struct btrfs_bio_stripe *old =
5523 bbio->stripes + i;
5524
5525 new->physical = old->physical;
5526 new->length = old->length;
5527 new->dev = dev_replace->tgtdev;
5528 bbio->tgtdev_map[i] = index_where_to_add;
5529 index_where_to_add++;
5530 max_errors++;
5531 tgtdev_indexes++;
5532 }
5533 }
5534 num_stripes = index_where_to_add;
5535 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5536 int index_srcdev = 0;
5537 int found = 0;
5538 u64 physical_of_found = 0;
5539
5540 /*
5541 * During the dev-replace procedure, the target drive can also
5542 * be used to read data in case it is needed to repair a corrupt
5543 * block elsewhere. This is possible if the requested area is
5544 * left of the left cursor. In this area, the target drive is a
5545 * full copy of the source drive.
5546 */
5547 for (i = 0; i < num_stripes; i++) {
5548 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5549 /*
5550 * In case of DUP, in order to keep it simple,
5551 * only add the mirror with the lowest physical
5552 * address
5553 */
5554 if (found &&
5555 physical_of_found <=
5556 bbio->stripes[i].physical)
5557 continue;
5558 index_srcdev = i;
5559 found = 1;
5560 physical_of_found = bbio->stripes[i].physical;
5561 }
5562 }
5563 if (found) {
5564 struct btrfs_bio_stripe *tgtdev_stripe =
5565 bbio->stripes + num_stripes;
5566
5567 tgtdev_stripe->physical = physical_of_found;
5568 tgtdev_stripe->length =
5569 bbio->stripes[index_srcdev].length;
5570 tgtdev_stripe->dev = dev_replace->tgtdev;
5571 bbio->tgtdev_map[index_srcdev] = num_stripes;
5572
5573 tgtdev_indexes++;
5574 num_stripes++;
5575 }
5576 }
5577
5578 *num_stripes_ret = num_stripes;
5579 *max_errors_ret = max_errors;
5580 bbio->num_tgtdevs = tgtdev_indexes;
5581 *bbio_ret = bbio;
5582}
5583
Liu Bo2b19a1f2017-03-14 13:34:00 -07005584static bool need_full_stripe(enum btrfs_map_op op)
5585{
5586 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5587}
5588
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005589static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5590 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005591 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005592 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005593 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005594{
5595 struct extent_map *em;
5596 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005597 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005598 u64 stripe_offset;
5599 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005600 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005601 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005602 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005603 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005604 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005605 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005606 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005607 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005608 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5609 int dev_replace_is_ongoing = 0;
5610 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005611 int patch_the_first_stripe_for_dev_replace = 0;
5612 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005613 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005614
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005615 if (op == BTRFS_MAP_DISCARD)
5616 return __btrfs_map_block_for_discard(fs_info, logical,
5617 *length, bbio_ret);
5618
Liu Bo592d92e2017-03-14 13:33:55 -07005619 em = get_chunk_map(fs_info, logical, *length);
5620 if (IS_ERR(em))
5621 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005622
Jeff Mahoney95617d62015-06-03 10:55:48 -04005623 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005624 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005625
David Woodhouse53b381b2013-01-29 18:40:14 -05005626 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005627 stripe_nr = offset;
5628 /*
5629 * stripe_nr counts the total number of stripes we have to stride
5630 * to get to this block
5631 */
David Sterba47c57132015-02-20 18:43:47 +01005632 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005633
David Woodhouse53b381b2013-01-29 18:40:14 -05005634 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005635 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005636 btrfs_crit(fs_info,
5637 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005638 stripe_offset, offset, em->start, logical,
5639 stripe_len);
5640 free_extent_map(em);
5641 return -EINVAL;
5642 }
Chris Mason593060d2008-03-25 16:50:33 -04005643
5644 /* stripe_offset is the offset of this block in its stripe*/
5645 stripe_offset = offset - stripe_offset;
5646
David Woodhouse53b381b2013-01-29 18:40:14 -05005647 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005648 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005649 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5650 raid56_full_stripe_start = offset;
5651
5652 /* allow a write of a full stripe, but make sure we don't
5653 * allow straddling of stripes
5654 */
David Sterba47c57132015-02-20 18:43:47 +01005655 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5656 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005657 raid56_full_stripe_start *= full_stripe_len;
5658 }
5659
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005660 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005661 u64 max_len;
5662 /* For writes to RAID[56], allow a full stripeset across all disks.
5663 For other RAID types and for RAID[56] reads, just allow a single
5664 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005665 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005666 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005667 max_len = stripe_len * nr_data_stripes(map) -
5668 (offset - raid56_full_stripe_start);
5669 } else {
5670 /* we limit the length of each bio to what fits in a stripe */
5671 max_len = stripe_len - stripe_offset;
5672 }
5673 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005674 } else {
5675 *length = em->len - offset;
5676 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005677
David Woodhouse53b381b2013-01-29 18:40:14 -05005678 /* This is for when we're called from btrfs_merge_bio_hook() and all
5679 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005680 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005681 goto out;
5682
David Sterba7e79cb82018-03-24 02:11:38 +01005683 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005684 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5685 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01005686 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005687 else
5688 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005689
Stefan Behrensad6d6202012-11-06 15:06:47 +01005690 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005691 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005692 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5693 dev_replace->srcdev->devid,
5694 &mirror_num,
5695 &physical_to_patch_in_first_stripe);
5696 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005697 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005698 else
5699 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005700 } else if (mirror_num > map->num_stripes) {
5701 mirror_num = 0;
5702 }
5703
Chris Masonf2d8d742008-04-21 10:03:05 -04005704 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005705 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005706 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005707 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5708 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005709 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005710 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005711 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005712 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005713 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005714 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005715 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005716 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005717 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005718 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005719 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005720 }
Chris Mason2fff7342008-04-29 14:12:09 -04005721
Chris Mason611f0e02008-04-03 16:29:03 -04005722 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005723 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005724 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005725 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005726 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005727 } else {
5728 mirror_num = 1;
5729 }
Chris Mason2fff7342008-04-29 14:12:09 -04005730
Chris Mason321aecc2008-04-16 10:49:51 -04005731 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005732 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005733
David Sterba47c57132015-02-20 18:43:47 +01005734 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005735 stripe_index *= map->sub_stripes;
5736
Anand Jainde483732017-10-12 16:43:00 +08005737 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005738 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005739 else if (mirror_num)
5740 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005741 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005742 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005743 stripe_index = find_live_mirror(fs_info, map,
5744 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01005745 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005746 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005747 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005748
Zhao Leiffe2d202015-01-20 15:11:44 +08005749 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005750 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005751 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005752 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005753 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005754
5755 /* RAID[56] write or recovery. Return all stripes */
5756 num_stripes = map->num_stripes;
5757 max_errors = nr_parity_stripes(map);
5758
David Woodhouse53b381b2013-01-29 18:40:14 -05005759 *length = map->stripe_len;
5760 stripe_index = 0;
5761 stripe_offset = 0;
5762 } else {
5763 /*
5764 * Mirror #0 or #1 means the original data block.
5765 * Mirror #2 is RAID5 parity block.
5766 * Mirror #3 is RAID6 Q block.
5767 */
David Sterba47c57132015-02-20 18:43:47 +01005768 stripe_nr = div_u64_rem(stripe_nr,
5769 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005770 if (mirror_num > 1)
5771 stripe_index = nr_data_stripes(map) +
5772 mirror_num - 2;
5773
5774 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005775 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5776 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005777 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005778 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005779 }
Chris Mason8790d502008-04-03 16:29:03 -04005780 } else {
5781 /*
David Sterba47c57132015-02-20 18:43:47 +01005782 * after this, stripe_nr is the number of stripes on this
5783 * device we have to walk to find the data, and stripe_index is
5784 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005785 */
David Sterba47c57132015-02-20 18:43:47 +01005786 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5787 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005788 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005789 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005790 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005791 btrfs_crit(fs_info,
5792 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005793 stripe_index, map->num_stripes);
5794 ret = -EINVAL;
5795 goto out;
5796 }
Chris Mason593060d2008-03-25 16:50:33 -04005797
Stefan Behrens472262f2012-11-06 14:43:46 +01005798 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005799 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005800 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005801 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005802 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005803 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005804 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005805 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005806
Zhao Lei6e9606d2015-01-20 15:11:34 +08005807 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005808 if (!bbio) {
5809 ret = -ENOMEM;
5810 goto out;
5811 }
Liu Bo6fad8232017-03-14 13:33:59 -07005812 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005813 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005814
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005815 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005816 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5817 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005818 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005819 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005820
5821 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5822 sizeof(struct btrfs_bio_stripe) *
5823 num_alloc_stripes +
5824 sizeof(int) * tgtdev_indexes);
5825
5826 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005827 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005828
5829 /* Fill in the logical address of each stripe */
5830 tmp = stripe_nr * nr_data_stripes(map);
5831 for (i = 0; i < nr_data_stripes(map); i++)
5832 bbio->raid_map[(i+rot) % num_stripes] =
5833 em->start + (tmp + i) * map->stripe_len;
5834
5835 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5836 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5837 bbio->raid_map[(i+rot+1) % num_stripes] =
5838 RAID6_Q_STRIPE;
5839 }
5840
Li Zefanec9ef7a2011-12-01 14:06:42 +08005841
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005842 for (i = 0; i < num_stripes; i++) {
5843 bbio->stripes[i].physical =
5844 map->stripes[stripe_index].physical +
5845 stripe_offset +
5846 stripe_nr * map->stripe_len;
5847 bbio->stripes[i].dev =
5848 map->stripes[stripe_index].dev;
5849 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005850 }
Li Zefande11cc12011-12-01 12:55:47 +08005851
Liu Bo2b19a1f2017-03-14 13:34:00 -07005852 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005853 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005854
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005855 if (bbio->raid_map)
5856 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005857
Liu Bo73c0f222017-03-14 13:33:58 -07005858 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005859 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005860 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5861 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005862 }
5863
Li Zefande11cc12011-12-01 12:55:47 +08005864 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005865 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005866 bbio->num_stripes = num_stripes;
5867 bbio->max_errors = max_errors;
5868 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005869
5870 /*
5871 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5872 * mirror_num == num_stripes + 1 && dev_replace target drive is
5873 * available as a mirror
5874 */
5875 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5876 WARN_ON(num_stripes > 1);
5877 bbio->stripes[0].dev = dev_replace->tgtdev;
5878 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5879 bbio->mirror_num = map->num_stripes + 1;
5880 }
Chris Masoncea9e442008-04-09 16:28:12 -04005881out:
Liu Bo73beece2015-07-17 16:49:19 +08005882 if (dev_replace_is_ongoing) {
5883 btrfs_dev_replace_clear_lock_blocking(dev_replace);
David Sterba7e79cb82018-03-24 02:11:38 +01005884 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005885 }
Chris Mason0b86a832008-03-24 15:01:56 -04005886 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005887 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005888}
5889
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005890int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005891 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005892 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005893{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005894 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005895 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005896}
5897
Miao Xieaf8e2d12014-10-23 14:42:50 +08005898/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005899int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005900 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005901 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005902{
David Sterba825ad4c2017-03-28 14:45:22 +02005903 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005904}
5905
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03005906int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
5907 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05005908{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005909 struct extent_map *em;
5910 struct map_lookup *map;
5911 u64 *buf;
5912 u64 bytenr;
5913 u64 length;
5914 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005915 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005916 int i, j, nr = 0;
5917
Liu Bo592d92e2017-03-14 13:33:55 -07005918 em = get_chunk_map(fs_info, chunk_start, 1);
5919 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005920 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005921
Jeff Mahoney95617d62015-06-03 10:55:48 -04005922 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005923 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005924 rmap_len = map->stripe_len;
5925
Yan Zhenga512bbf2008-12-08 16:46:26 -05005926 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005927 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005928 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005929 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005930 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005931 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005932 rmap_len = map->stripe_len * nr_data_stripes(map);
5933 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005934
David Sterba31e818f2015-02-20 18:00:26 +01005935 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005936 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005937
5938 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05005939 if (map->stripes[i].physical > physical ||
5940 map->stripes[i].physical + length <= physical)
5941 continue;
5942
5943 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07005944 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005945
5946 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5947 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005948 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005949 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5950 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005951 } /* else if RAID[56], multiply by nr_data_stripes().
5952 * Alternatively, just use rmap_len below instead of
5953 * map->stripe_len */
5954
5955 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005956 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005957 for (j = 0; j < nr; j++) {
5958 if (buf[j] == bytenr)
5959 break;
5960 }
Chris Mason934d3752008-12-08 16:43:10 -05005961 if (j == nr) {
5962 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005963 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005964 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005965 }
5966
Yan Zhenga512bbf2008-12-08 16:46:26 -05005967 *logical = buf;
5968 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005969 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005970
5971 free_extent_map(em);
5972 return 0;
5973}
5974
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005975static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005976{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005977 bio->bi_private = bbio->private;
5978 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005979 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005980
Zhao Lei6e9606d2015-01-20 15:11:34 +08005981 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005982}
5983
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005984static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005985{
Chris Mason9be33952013-05-17 18:30:14 -04005986 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005987 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005988
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005989 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005990 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005991 if (bio->bi_status == BLK_STS_IOERR ||
5992 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005993 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005994 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005995 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005996
5997 BUG_ON(stripe_index >= bbio->num_stripes);
5998 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005999 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006000 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006001 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006002 BTRFS_DEV_STAT_WRITE_ERRS);
6003 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006004 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006005 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006006 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006007 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006008 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006009 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006010 }
6011 }
Chris Mason8790d502008-04-03 16:29:03 -04006012
Jan Schmidta1d3c472011-08-04 17:15:33 +02006013 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006014 is_orig_bio = 1;
6015
Miao Xiec404e0d2014-01-30 16:46:55 +08006016 btrfs_bio_counter_dec(bbio->fs_info);
6017
Jan Schmidta1d3c472011-08-04 17:15:33 +02006018 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006019 if (!is_orig_bio) {
6020 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006021 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006022 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006023
Chris Mason9be33952013-05-17 18:30:14 -04006024 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006025 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006026 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006027 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006028 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006029 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006030 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006031 /*
6032 * this bio is actually up to date, we didn't
6033 * go over the max number of errors
6034 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006035 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006036 }
Miao Xiec55f1392014-06-19 10:42:54 +08006037
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006038 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006039 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006040 bio_put(bio);
6041 }
Chris Mason8790d502008-04-03 16:29:03 -04006042}
6043
Chris Mason8b712842008-06-11 16:50:36 -04006044/*
6045 * see run_scheduled_bios for a description of why bios are collected for
6046 * async submit.
6047 *
6048 * This will add one bio to the pending list for a device and make sure
6049 * the work struct is scheduled.
6050 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006051static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006052 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006054 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006055 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006056 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006057
Anand Jaine6e674b2017-12-04 12:54:54 +08006058 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
6059 !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006060 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006061 return;
6062 }
6063
Chris Mason8b712842008-06-11 16:50:36 -04006064 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006065 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006066 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006067 return;
Chris Mason8b712842008-06-11 16:50:36 -04006068 }
6069
Chris Mason492bb6de2008-07-31 16:29:02 -04006070 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006071 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006072
6073 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006074 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006075 pending_bios = &device->pending_sync_bios;
6076 else
6077 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006078
Chris Masonffbd5172009-04-20 15:50:09 -04006079 if (pending_bios->tail)
6080 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006081
Chris Masonffbd5172009-04-20 15:50:09 -04006082 pending_bios->tail = bio;
6083 if (!pending_bios->head)
6084 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006085 if (device->running_pending)
6086 should_queue = 0;
6087
6088 spin_unlock(&device->io_lock);
6089
6090 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006091 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006092}
6093
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006094static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6095 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006096{
6097 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006098 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006099
6100 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006101 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006102 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006103 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006104#ifdef DEBUG
6105 {
6106 struct rcu_string *name;
6107
6108 rcu_read_lock();
6109 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006110 btrfs_debug(fs_info,
6111 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6112 bio_op(bio), bio->bi_opf,
6113 (u64)bio->bi_iter.bi_sector,
6114 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6115 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006116 rcu_read_unlock();
6117 }
6118#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006119 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006120
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006121 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006122
Josef Bacikde1ee922012-10-19 16:50:56 -04006123 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006124 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006125 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006126 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006127}
6128
Josef Bacikde1ee922012-10-19 16:50:56 -04006129static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6130{
6131 atomic_inc(&bbio->error);
6132 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006133 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006134 WARN_ON(bio != bbio->orig_bio);
6135
Chris Mason9be33952013-05-17 18:30:14 -04006136 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006137 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006138 if (atomic_read(&bbio->error) > bbio->max_errors)
6139 bio->bi_status = BLK_STS_IOERR;
6140 else
6141 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006142 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006143 }
6144}
6145
Omar Sandoval58efbc92017-08-22 23:45:59 -07006146blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6147 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006148{
Chris Mason0b86a832008-03-24 15:01:56 -04006149 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006150 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006151 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006152 u64 length = 0;
6153 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006154 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006155 int dev_nr;
6156 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006157 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006158
Kent Overstreet4f024f32013-10-11 15:44:27 -07006159 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006160 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006161
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006163 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006164 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006165 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006166 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006167 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006168 }
Chris Masoncea9e442008-04-09 16:28:12 -04006169
Jan Schmidta1d3c472011-08-04 17:15:33 +02006170 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006171 bbio->orig_bio = first_bio;
6172 bbio->private = first_bio->bi_private;
6173 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006174 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006175 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6176
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006177 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006178 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006179 /* In this case, map_length has been set to the length of
6180 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006181 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006182 ret = raid56_parity_write(fs_info, bio, bbio,
6183 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006184 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006185 ret = raid56_parity_recover(fs_info, bio, bbio,
6186 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006187 }
Miao Xie42452152014-11-25 16:39:28 +08006188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006189 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006190 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006191 }
6192
Chris Masoncea9e442008-04-09 16:28:12 -04006193 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006194 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006195 "mapping failed logical %llu bio len %llu len %llu",
6196 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006197 BUG();
6198 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006199
Zhao Lei08da7572015-02-12 15:42:16 +08006200 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006201 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006202 if (!dev || !dev->bdev ||
Anand Jainebbede42017-12-04 12:54:52 +08006203 (bio_op(first_bio) == REQ_OP_WRITE &&
6204 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006205 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006206 continue;
6207 }
6208
David Sterba3aa8e072017-06-02 17:38:30 +02006209 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006210 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006211 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006212 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006213
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006214 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6215 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006216 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006217 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006218 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006219}
6220
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006221struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006222 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006223{
Yan Zheng2b820322008-11-17 21:11:30 -05006224 struct btrfs_device *device;
6225 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006226
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006227 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006228 while (cur_devices) {
6229 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006230 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006231 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006232 if (device)
6233 return device;
6234 }
6235 cur_devices = cur_devices->seed;
6236 }
6237 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006238}
6239
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006240static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006241 u64 devid, u8 *dev_uuid)
6242{
6243 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006244
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006245 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6246 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006247 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006248
6249 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006250 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006251 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006252
Anand Jaine6e674b2017-12-04 12:54:54 +08006253 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006254 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006255
Chris Masondfe25022008-05-13 13:46:40 -04006256 return device;
6257}
6258
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006259/**
6260 * btrfs_alloc_device - allocate struct btrfs_device
6261 * @fs_info: used only for generating a new devid, can be NULL if
6262 * devid is provided (i.e. @devid != NULL).
6263 * @devid: a pointer to devid for this device. If NULL a new devid
6264 * is generated.
6265 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6266 * is generated.
6267 *
6268 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006269 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006270 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006271 */
6272struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6273 const u64 *devid,
6274 const u8 *uuid)
6275{
6276 struct btrfs_device *dev;
6277 u64 tmp;
6278
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306279 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006280 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006281
6282 dev = __alloc_device();
6283 if (IS_ERR(dev))
6284 return dev;
6285
6286 if (devid)
6287 tmp = *devid;
6288 else {
6289 int ret;
6290
6291 ret = find_next_devid(fs_info, &tmp);
6292 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006293 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006294 return ERR_PTR(ret);
6295 }
6296 }
6297 dev->devid = tmp;
6298
6299 if (uuid)
6300 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6301 else
6302 generate_random_uuid(dev->uuid);
6303
Liu Bo9e0af232014-08-15 23:36:53 +08006304 btrfs_init_work(&dev->work, btrfs_submit_helper,
6305 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006306
6307 return dev;
6308}
6309
Liu Boe06cd3d2016-06-03 12:05:15 -07006310/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006311static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006312 struct extent_buffer *leaf,
6313 struct btrfs_chunk *chunk, u64 logical)
6314{
6315 u64 length;
6316 u64 stripe_len;
6317 u16 num_stripes;
6318 u16 sub_stripes;
6319 u64 type;
6320
6321 length = btrfs_chunk_length(leaf, chunk);
6322 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6323 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6324 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6325 type = btrfs_chunk_type(leaf, chunk);
6326
6327 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006328 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006329 num_stripes);
6330 return -EIO;
6331 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006332 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6333 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006334 return -EIO;
6335 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006336 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6337 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006338 btrfs_chunk_sector_size(leaf, chunk));
6339 return -EIO;
6340 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006341 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6342 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006343 return -EIO;
6344 }
6345 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006346 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006347 stripe_len);
6348 return -EIO;
6349 }
6350 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6351 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006352 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006353 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6354 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6355 btrfs_chunk_type(leaf, chunk));
6356 return -EIO;
6357 }
6358 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6359 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6360 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6361 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6362 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6363 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6364 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006365 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006366 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6367 num_stripes, sub_stripes,
6368 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6369 return -EIO;
6370 }
6371
6372 return 0;
6373}
6374
Anand Jain5a2b8e62017-10-09 11:07:45 +08006375static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006376 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006377{
Anand Jain2b902df2017-10-09 11:07:46 +08006378 if (error)
6379 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6380 devid, uuid);
6381 else
6382 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6383 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006384}
6385
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006386static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006387 struct extent_buffer *leaf,
6388 struct btrfs_chunk *chunk)
6389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006390 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006391 struct map_lookup *map;
6392 struct extent_map *em;
6393 u64 logical;
6394 u64 length;
6395 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006396 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006397 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006398 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006399 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006400
Chris Masone17cade2008-04-15 15:41:47 -04006401 logical = key->offset;
6402 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006403 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006405 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006406 if (ret)
6407 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006408
Chris Mason890871b2009-09-02 16:24:52 -04006409 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006410 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006411 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006412
6413 /* already mapped? */
6414 if (em && em->start <= logical && em->start + em->len > logical) {
6415 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006416 return 0;
6417 } else if (em) {
6418 free_extent_map(em);
6419 }
Chris Mason0b86a832008-03-24 15:01:56 -04006420
David Sterba172ddd62011-04-21 00:48:27 +02006421 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006422 if (!em)
6423 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006424 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006425 if (!map) {
6426 free_extent_map(em);
6427 return -ENOMEM;
6428 }
6429
Wang Shilong298a8f92014-06-19 10:42:52 +08006430 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006431 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006432 em->start = logical;
6433 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006434 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006435 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006436 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006437
Chris Mason593060d2008-03-25 16:50:33 -04006438 map->num_stripes = num_stripes;
6439 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6440 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006441 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6442 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006443 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006444 for (i = 0; i < num_stripes; i++) {
6445 map->stripes[i].physical =
6446 btrfs_stripe_offset_nr(leaf, chunk, i);
6447 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006448 read_extent_buffer(leaf, uuid, (unsigned long)
6449 btrfs_stripe_dev_uuid_nr(chunk, i),
6450 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006451 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006452 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006453 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006454 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006455 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006456 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006457 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006458 }
Chris Masondfe25022008-05-13 13:46:40 -04006459 if (!map->stripes[i].dev) {
6460 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 add_missing_dev(fs_info->fs_devices, devid,
6462 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006463 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006464 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006465 btrfs_err(fs_info,
6466 "failed to init missing dev %llu: %ld",
6467 devid, PTR_ERR(map->stripes[i].dev));
6468 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006469 }
Anand Jain2b902df2017-10-09 11:07:46 +08006470 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006471 }
Anand Jaine12c9622017-12-04 12:54:53 +08006472 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6473 &(map->stripes[i].dev->dev_state));
6474
Chris Mason0b86a832008-03-24 15:01:56 -04006475 }
6476
Chris Mason890871b2009-09-02 16:24:52 -04006477 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006478 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006479 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006480 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006481 free_extent_map(em);
6482
6483 return 0;
6484}
6485
Jeff Mahoney143bede2012-03-01 14:56:26 +01006486static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006487 struct btrfs_dev_item *dev_item,
6488 struct btrfs_device *device)
6489{
6490 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006491
6492 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006493 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6494 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006495 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006496 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006497 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006498 device->type = btrfs_device_type(leaf, dev_item);
6499 device->io_align = btrfs_device_io_align(leaf, dev_item);
6500 device->io_width = btrfs_device_io_width(leaf, dev_item);
6501 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006502 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006503 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006504
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006505 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006506 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006507}
6508
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006509static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006510 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006511{
6512 struct btrfs_fs_devices *fs_devices;
6513 int ret;
6514
David Sterbaa32bf9a2018-03-16 02:21:22 +01006515 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006516 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006517
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006518 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006519 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006520 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006521 return fs_devices;
6522
Yan Zheng2b820322008-11-17 21:11:30 -05006523 fs_devices = fs_devices->seed;
6524 }
6525
6526 fs_devices = find_fsid(fsid);
6527 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006528 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006529 return ERR_PTR(-ENOENT);
6530
6531 fs_devices = alloc_fs_devices(fsid);
6532 if (IS_ERR(fs_devices))
6533 return fs_devices;
6534
6535 fs_devices->seeding = 1;
6536 fs_devices->opened = 1;
6537 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006538 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006539
6540 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006541 if (IS_ERR(fs_devices))
6542 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006543
Anand Jain897fb572018-04-12 10:29:28 +08006544 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006545 if (ret) {
6546 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006547 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006548 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006549 }
Yan Zheng2b820322008-11-17 21:11:30 -05006550
6551 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006552 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006553 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006554 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006555 goto out;
6556 }
6557
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006558 fs_devices->seed = fs_info->fs_devices->seed;
6559 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006560out:
Miao Xie5f375832014-09-03 21:35:46 +08006561 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006562}
6563
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006564static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006565 struct extent_buffer *leaf,
6566 struct btrfs_dev_item *dev_item)
6567{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006568 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006569 struct btrfs_device *device;
6570 u64 devid;
6571 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006572 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006573 u8 dev_uuid[BTRFS_UUID_SIZE];
6574
Chris Mason0b86a832008-03-24 15:01:56 -04006575 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006576 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006577 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006578 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006579 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006580
Anand Jain44880fd2017-07-29 17:50:09 +08006581 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006582 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006583 if (IS_ERR(fs_devices))
6584 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006585 }
6586
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006587 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006588 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006589 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006590 btrfs_report_missing_device(fs_info, devid,
6591 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006592 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006593 }
Yan Zheng2b820322008-11-17 21:11:30 -05006594
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006595 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006596 if (IS_ERR(device)) {
6597 btrfs_err(fs_info,
6598 "failed to add missing dev %llu: %ld",
6599 devid, PTR_ERR(device));
6600 return PTR_ERR(device);
6601 }
Anand Jain2b902df2017-10-09 11:07:46 +08006602 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006603 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006604 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006605 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6606 btrfs_report_missing_device(fs_info,
6607 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006608 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006609 }
6610 btrfs_report_missing_device(fs_info, devid,
6611 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006612 }
Miao Xie5f375832014-09-03 21:35:46 +08006613
Anand Jaine6e674b2017-12-04 12:54:54 +08006614 if (!device->bdev &&
6615 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006616 /*
6617 * this happens when a device that was properly setup
6618 * in the device info lists suddenly goes bad.
6619 * device->bdev is NULL, and so we have to set
6620 * device->missing to one here
6621 */
Miao Xie5f375832014-09-03 21:35:46 +08006622 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006623 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006624 }
Miao Xie5f375832014-09-03 21:35:46 +08006625
6626 /* Move the device to its own fs_devices */
6627 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006628 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6629 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006630
6631 list_move(&device->dev_list, &fs_devices->devices);
6632 device->fs_devices->num_devices--;
6633 fs_devices->num_devices++;
6634
6635 device->fs_devices->missing_devices--;
6636 fs_devices->missing_devices++;
6637
6638 device->fs_devices = fs_devices;
6639 }
Yan Zheng2b820322008-11-17 21:11:30 -05006640 }
6641
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006642 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006643 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006644 if (device->generation !=
6645 btrfs_device_generation(leaf, dev_item))
6646 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006647 }
Chris Mason0b86a832008-03-24 15:01:56 -04006648
6649 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006650 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006651 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006652 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006653 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006654 atomic64_add(device->total_bytes - device->bytes_used,
6655 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006656 }
Chris Mason0b86a832008-03-24 15:01:56 -04006657 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006658 return ret;
6659}
6660
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006661int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006662{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006663 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006664 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006665 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006666 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006667 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006668 u8 *array_ptr;
6669 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006670 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006671 u32 num_stripes;
6672 u32 array_size;
6673 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006674 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006675 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006676 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006677
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006678 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006679 /*
6680 * This will create extent buffer of nodesize, superblock size is
6681 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6682 * overallocate but we can keep it as-is, only the first page is used.
6683 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006684 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006685 if (IS_ERR(sb))
6686 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006687 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006688 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006689 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006690 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006691 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006692 * pages up-to-date when the page is larger: extent does not cover the
6693 * whole page and consequently check_page_uptodate does not find all
6694 * the page's extents up-to-date (the hole beyond sb),
6695 * write_extent_buffer then triggers a WARN_ON.
6696 *
6697 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6698 * but sb spans only this function. Add an explicit SetPageUptodate call
6699 * to silence the warning eg. on PowerPC 64.
6700 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006701 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006702 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006703
Chris Masona061fc82008-05-07 11:43:44 -04006704 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006705 array_size = btrfs_super_sys_array_size(super_copy);
6706
David Sterba1ffb22c2014-10-31 19:02:42 +01006707 array_ptr = super_copy->sys_chunk_array;
6708 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6709 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006710
David Sterba1ffb22c2014-10-31 19:02:42 +01006711 while (cur_offset < array_size) {
6712 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006713 len = sizeof(*disk_key);
6714 if (cur_offset + len > array_size)
6715 goto out_short_read;
6716
Chris Mason0b86a832008-03-24 15:01:56 -04006717 btrfs_disk_key_to_cpu(&key, disk_key);
6718
David Sterba1ffb22c2014-10-31 19:02:42 +01006719 array_ptr += len;
6720 sb_array_offset += len;
6721 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006722
Chris Mason0d81ba52008-03-24 15:02:07 -04006723 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006724 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006725 /*
6726 * At least one btrfs_chunk with one stripe must be
6727 * present, exact stripe count check comes afterwards
6728 */
6729 len = btrfs_chunk_item_size(1);
6730 if (cur_offset + len > array_size)
6731 goto out_short_read;
6732
6733 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006734 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006735 btrfs_err(fs_info,
6736 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006737 num_stripes, cur_offset);
6738 ret = -EIO;
6739 break;
6740 }
6741
Liu Boe06cd3d2016-06-03 12:05:15 -07006742 type = btrfs_chunk_type(sb, chunk);
6743 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006744 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006745 "invalid chunk type %llu in sys_array at offset %u",
6746 type, cur_offset);
6747 ret = -EIO;
6748 break;
6749 }
6750
David Sterbae3540ea2014-11-05 15:24:51 +01006751 len = btrfs_chunk_item_size(num_stripes);
6752 if (cur_offset + len > array_size)
6753 goto out_short_read;
6754
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006755 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006756 if (ret)
6757 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006758 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006759 btrfs_err(fs_info,
6760 "unexpected item type %u in sys_array at offset %u",
6761 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006762 ret = -EIO;
6763 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006764 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006765 array_ptr += len;
6766 sb_array_offset += len;
6767 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006768 }
Liu Bod8651772016-06-03 17:41:42 -07006769 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006770 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006771 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006772
6773out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006774 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006775 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006776 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006777 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006778 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006779}
6780
Qu Wenruo21634a12017-03-09 09:34:36 +08006781/*
6782 * Check if all chunks in the fs are OK for read-write degraded mount
6783 *
Anand Jain6528b992017-12-18 17:08:59 +08006784 * If the @failing_dev is specified, it's accounted as missing.
6785 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006786 * Return true if all chunks meet the minimal RW mount requirements.
6787 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6788 */
Anand Jain6528b992017-12-18 17:08:59 +08006789bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6790 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006791{
6792 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6793 struct extent_map *em;
6794 u64 next_start = 0;
6795 bool ret = true;
6796
6797 read_lock(&map_tree->map_tree.lock);
6798 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6799 read_unlock(&map_tree->map_tree.lock);
6800 /* No chunk at all? Return false anyway */
6801 if (!em) {
6802 ret = false;
6803 goto out;
6804 }
6805 while (em) {
6806 struct map_lookup *map;
6807 int missing = 0;
6808 int max_tolerated;
6809 int i;
6810
6811 map = em->map_lookup;
6812 max_tolerated =
6813 btrfs_get_num_tolerated_disk_barrier_failures(
6814 map->type);
6815 for (i = 0; i < map->num_stripes; i++) {
6816 struct btrfs_device *dev = map->stripes[i].dev;
6817
Anand Jaine6e674b2017-12-04 12:54:54 +08006818 if (!dev || !dev->bdev ||
6819 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006820 dev->last_flush_error)
6821 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006822 else if (failing_dev && failing_dev == dev)
6823 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006824 }
6825 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006826 if (!failing_dev)
6827 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006828 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6829 em->start, missing, max_tolerated);
6830 free_extent_map(em);
6831 ret = false;
6832 goto out;
6833 }
6834 next_start = extent_map_end(em);
6835 free_extent_map(em);
6836
6837 read_lock(&map_tree->map_tree.lock);
6838 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6839 (u64)(-1) - next_start);
6840 read_unlock(&map_tree->map_tree.lock);
6841 }
6842out:
6843 return ret;
6844}
6845
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006846int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006847{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006848 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006849 struct btrfs_path *path;
6850 struct extent_buffer *leaf;
6851 struct btrfs_key key;
6852 struct btrfs_key found_key;
6853 int ret;
6854 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006855 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006856
Chris Mason0b86a832008-03-24 15:01:56 -04006857 path = btrfs_alloc_path();
6858 if (!path)
6859 return -ENOMEM;
6860
Anand Jain3dd0f7a2018-04-12 10:29:32 +08006861 /*
6862 * uuid_mutex is needed only if we are mounting a sprout FS
6863 * otherwise we don't need it.
6864 */
Li Zefanb367e472011-12-07 11:38:24 +08006865 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006866 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006867
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006868 /*
6869 * Read all device items, and then all the chunk items. All
6870 * device items are found before any chunk item (their object id
6871 * is smaller than the lowest possible object id for a chunk
6872 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006873 */
6874 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6875 key.offset = 0;
6876 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006877 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006878 if (ret < 0)
6879 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006880 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006881 leaf = path->nodes[0];
6882 slot = path->slots[0];
6883 if (slot >= btrfs_header_nritems(leaf)) {
6884 ret = btrfs_next_leaf(root, path);
6885 if (ret == 0)
6886 continue;
6887 if (ret < 0)
6888 goto error;
6889 break;
6890 }
6891 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006892 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6893 struct btrfs_dev_item *dev_item;
6894 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006895 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006896 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006897 if (ret)
6898 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006899 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006900 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6901 struct btrfs_chunk *chunk;
6902 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006903 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006904 if (ret)
6905 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006906 }
6907 path->slots[0]++;
6908 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006909
6910 /*
6911 * After loading chunk tree, we've got all device information,
6912 * do another round of validation checks.
6913 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006914 if (total_dev != fs_info->fs_devices->total_devices) {
6915 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006916 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006917 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006918 total_dev);
6919 ret = -EINVAL;
6920 goto error;
6921 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006922 if (btrfs_super_total_bytes(fs_info->super_copy) <
6923 fs_info->fs_devices->total_rw_bytes) {
6924 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006925 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006926 btrfs_super_total_bytes(fs_info->super_copy),
6927 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006928 ret = -EINVAL;
6929 goto error;
6930 }
Chris Mason0b86a832008-03-24 15:01:56 -04006931 ret = 0;
6932error:
David Sterba34441362016-10-04 19:34:27 +02006933 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006934 mutex_unlock(&uuid_mutex);
6935
Yan Zheng2b820322008-11-17 21:11:30 -05006936 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006937 return ret;
6938}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006939
Miao Xiecb517ea2013-05-15 07:48:19 +00006940void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6941{
6942 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6943 struct btrfs_device *device;
6944
Liu Bo29cc83f2014-05-11 23:14:59 +08006945 while (fs_devices) {
6946 mutex_lock(&fs_devices->device_list_mutex);
6947 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04006948 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08006949 mutex_unlock(&fs_devices->device_list_mutex);
6950
6951 fs_devices = fs_devices->seed;
6952 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006953}
6954
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006955static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6956{
6957 int i;
6958
6959 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6960 btrfs_dev_stat_reset(dev, i);
6961}
6962
6963int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6964{
6965 struct btrfs_key key;
6966 struct btrfs_key found_key;
6967 struct btrfs_root *dev_root = fs_info->dev_root;
6968 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6969 struct extent_buffer *eb;
6970 int slot;
6971 int ret = 0;
6972 struct btrfs_device *device;
6973 struct btrfs_path *path = NULL;
6974 int i;
6975
6976 path = btrfs_alloc_path();
6977 if (!path) {
6978 ret = -ENOMEM;
6979 goto out;
6980 }
6981
6982 mutex_lock(&fs_devices->device_list_mutex);
6983 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6984 int item_size;
6985 struct btrfs_dev_stats_item *ptr;
6986
David Sterba242e2952016-01-25 17:51:31 +01006987 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6988 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006989 key.offset = device->devid;
6990 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6991 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006992 __btrfs_reset_dev_stats(device);
6993 device->dev_stats_valid = 1;
6994 btrfs_release_path(path);
6995 continue;
6996 }
6997 slot = path->slots[0];
6998 eb = path->nodes[0];
6999 btrfs_item_key_to_cpu(eb, &found_key, slot);
7000 item_size = btrfs_item_size_nr(eb, slot);
7001
7002 ptr = btrfs_item_ptr(eb, slot,
7003 struct btrfs_dev_stats_item);
7004
7005 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7006 if (item_size >= (1 + i) * sizeof(__le64))
7007 btrfs_dev_stat_set(device, i,
7008 btrfs_dev_stats_value(eb, ptr, i));
7009 else
7010 btrfs_dev_stat_reset(device, i);
7011 }
7012
7013 device->dev_stats_valid = 1;
7014 btrfs_dev_stat_print_on_load(device);
7015 btrfs_release_path(path);
7016 }
7017 mutex_unlock(&fs_devices->device_list_mutex);
7018
7019out:
7020 btrfs_free_path(path);
7021 return ret < 0 ? ret : 0;
7022}
7023
7024static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007025 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007026 struct btrfs_device *device)
7027{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007028 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007029 struct btrfs_path *path;
7030 struct btrfs_key key;
7031 struct extent_buffer *eb;
7032 struct btrfs_dev_stats_item *ptr;
7033 int ret;
7034 int i;
7035
David Sterba242e2952016-01-25 17:51:31 +01007036 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7037 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007038 key.offset = device->devid;
7039
7040 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007041 if (!path)
7042 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007043 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7044 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007045 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007046 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007047 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007048 goto out;
7049 }
7050
7051 if (ret == 0 &&
7052 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7053 /* need to delete old one and insert a new one */
7054 ret = btrfs_del_item(trans, dev_root, path);
7055 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007056 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007057 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007058 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007059 goto out;
7060 }
7061 ret = 1;
7062 }
7063
7064 if (ret == 1) {
7065 /* need to insert a new item */
7066 btrfs_release_path(path);
7067 ret = btrfs_insert_empty_item(trans, dev_root, path,
7068 &key, sizeof(*ptr));
7069 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007070 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007071 "insert dev_stats item for device %s failed %d",
7072 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007073 goto out;
7074 }
7075 }
7076
7077 eb = path->nodes[0];
7078 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7079 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7080 btrfs_set_dev_stats_value(eb, ptr, i,
7081 btrfs_dev_stat_read(device, i));
7082 btrfs_mark_buffer_dirty(eb);
7083
7084out:
7085 btrfs_free_path(path);
7086 return ret;
7087}
7088
7089/*
7090 * called from commit_transaction. Writes all changed device stats to disk.
7091 */
7092int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7093 struct btrfs_fs_info *fs_info)
7094{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007095 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7096 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007097 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007098 int ret = 0;
7099
7100 mutex_lock(&fs_devices->device_list_mutex);
7101 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007102 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7103 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007104 continue;
7105
Nikolay Borisov9deae962017-10-24 13:47:37 +03007106
7107 /*
7108 * There is a LOAD-LOAD control dependency between the value of
7109 * dev_stats_ccnt and updating the on-disk values which requires
7110 * reading the in-memory counters. Such control dependencies
7111 * require explicit read memory barriers.
7112 *
7113 * This memory barriers pairs with smp_mb__before_atomic in
7114 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7115 * barrier implied by atomic_xchg in
7116 * btrfs_dev_stats_read_and_reset
7117 */
7118 smp_rmb();
7119
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007120 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007121 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007122 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007123 }
7124 mutex_unlock(&fs_devices->device_list_mutex);
7125
7126 return ret;
7127}
7128
Stefan Behrens442a4f62012-05-25 16:06:08 +02007129void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7130{
7131 btrfs_dev_stat_inc(dev, index);
7132 btrfs_dev_stat_print_on_error(dev);
7133}
7134
Eric Sandeen48a3b632013-04-25 20:41:01 +00007135static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007136{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007137 if (!dev->dev_stats_valid)
7138 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007139 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007140 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007141 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007142 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7143 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7144 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007145 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7146 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007147}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007148
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007149static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7150{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007151 int i;
7152
7153 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7154 if (btrfs_dev_stat_read(dev, i) != 0)
7155 break;
7156 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7157 return; /* all values == 0, suppress message */
7158
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007159 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007160 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007161 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007162 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7163 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7164 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7165 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7166 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7167}
7168
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007169int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007170 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007171{
7172 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007173 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007174 int i;
7175
7176 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007177 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007178 mutex_unlock(&fs_devices->device_list_mutex);
7179
7180 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007181 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007182 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007183 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007184 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007185 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007186 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007187 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7188 if (stats->nr_items > i)
7189 stats->values[i] =
7190 btrfs_dev_stat_read_and_reset(dev, i);
7191 else
7192 btrfs_dev_stat_reset(dev, i);
7193 }
7194 } else {
7195 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7196 if (stats->nr_items > i)
7197 stats->values[i] = btrfs_dev_stat_read(dev, i);
7198 }
7199 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7200 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7201 return 0;
7202}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007203
David Sterbada353f62017-02-14 17:55:53 +01007204void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007205{
7206 struct buffer_head *bh;
7207 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007208 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007209
Anand Jain12b1c262015-08-14 18:32:59 +08007210 if (!bdev)
7211 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007212
Anand Jain12b1c262015-08-14 18:32:59 +08007213 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7214 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007215
Anand Jain12b1c262015-08-14 18:32:59 +08007216 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7217 continue;
7218
7219 disk_super = (struct btrfs_super_block *)bh->b_data;
7220
7221 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7222 set_buffer_dirty(bh);
7223 sync_dirty_buffer(bh);
7224 brelse(bh);
7225 }
7226
7227 /* Notify udev that device has changed */
7228 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7229
7230 /* Update ctime/mtime for device path for libblkid */
7231 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007232}
Miao Xie935e5cc2014-09-03 21:35:33 +08007233
7234/*
7235 * Update the size of all devices, which is used for writing out the
7236 * super blocks.
7237 */
7238void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7239{
7240 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7241 struct btrfs_device *curr, *next;
7242
7243 if (list_empty(&fs_devices->resized_devices))
7244 return;
7245
7246 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007247 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007248 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7249 resized_list) {
7250 list_del_init(&curr->resized_list);
7251 curr->commit_total_bytes = curr->disk_total_bytes;
7252 }
David Sterba34441362016-10-04 19:34:27 +02007253 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007254 mutex_unlock(&fs_devices->device_list_mutex);
7255}
Miao Xiece7213c2014-09-03 21:35:34 +08007256
7257/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007258void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007259{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007260 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007261 struct extent_map *em;
7262 struct map_lookup *map;
7263 struct btrfs_device *dev;
7264 int i;
7265
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007266 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007267 return;
7268
7269 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007270 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007271 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007272 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007273
7274 for (i = 0; i < map->num_stripes; i++) {
7275 dev = map->stripes[i].dev;
7276 dev->commit_bytes_used = dev->bytes_used;
7277 }
7278 }
David Sterba34441362016-10-04 19:34:27 +02007279 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007280}
Anand Jain5a13f432015-03-10 06:38:31 +08007281
7282void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7283{
7284 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7285 while (fs_devices) {
7286 fs_devices->fs_info = fs_info;
7287 fs_devices = fs_devices->seed;
7288 }
7289}
7290
7291void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7292{
7293 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7294 while (fs_devices) {
7295 fs_devices->fs_info = NULL;
7296 fs_devices = fs_devices->seed;
7297 }
7298}