blob: 8c5ae9943d694ceeff3994a44eb169e348c855e0 [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 */
Gu Jinxiang36350e92018-07-12 14:23:16 +08001216struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,
1217 void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001218{
1219 struct btrfs_super_block *disk_super;
Anand Jain4306a972018-05-29 12:28:37 +08001220 bool new_device_added = false;
Gu Jinxiang36350e92018-07-12 14:23:16 +08001221 struct btrfs_device *device = NULL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001222 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001223 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001224 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001225
David Sterba899f9302018-06-19 16:37:36 +02001226 lockdep_assert_held(&uuid_mutex);
1227
David Sterba6f60cbd2013-02-15 11:31:02 -07001228 /*
1229 * we would like to check all the supers, but that would make
1230 * a btrfs mount succeed after a mkfs from a different FS.
1231 * So, we need to add a special mount option to scan for
1232 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1233 */
1234 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001235 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001236
1237 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001238 if (IS_ERR(bdev))
Gu Jinxiang36350e92018-07-12 14:23:16 +08001239 return ERR_CAST(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001240
Anand Jain05a5c552017-12-15 15:40:16 +08001241 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
Gu Jinxiang36350e92018-07-12 14:23:16 +08001242 device = ERR_PTR(-EINVAL);
David Sterba6f60cbd2013-02-15 11:31:02 -07001243 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001244 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001245
Anand Jain4306a972018-05-29 12:28:37 +08001246 device = device_list_add(path, disk_super, &new_device_added);
Gu Jinxiang36350e92018-07-12 14:23:16 +08001247 if (!IS_ERR(device)) {
Anand Jain4306a972018-05-29 12:28:37 +08001248 if (new_device_added)
1249 btrfs_free_stale_devices(path, device);
1250 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001251
Anand Jain6cf86a002016-02-13 10:01:29 +08001252 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001253
1254error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001255 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001256
Gu Jinxiang36350e92018-07-12 14:23:16 +08001257 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001258}
Chris Mason0b86a832008-03-24 15:01:56 -04001259
Jeff Mahoney499f3772015-06-15 09:41:17 -04001260static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001261 struct btrfs_device *device,
1262 u64 *start, u64 len)
1263{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001264 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001265 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001266 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001267 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001268 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001269
Jeff Mahoney499f3772015-06-15 09:41:17 -04001270 if (transaction)
1271 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001272again:
1273 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001274 struct map_lookup *map;
1275 int i;
1276
Jeff Mahoney95617d62015-06-03 10:55:48 -04001277 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001278 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001279 u64 end;
1280
Josef Bacik6df9a952013-06-27 13:22:46 -04001281 if (map->stripes[i].dev != device)
1282 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001283 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001284 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001285 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001286 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001287 /*
1288 * Make sure that while processing the pinned list we do
1289 * not override our *start with a lower value, because
1290 * we can have pinned chunks that fall within this
1291 * device hole and that have lower physical addresses
1292 * than the pending chunks we processed before. If we
1293 * do not take this special care we can end up getting
1294 * 2 pending chunks that start at the same physical
1295 * device offsets because the end offset of a pinned
1296 * chunk can be equal to the start offset of some
1297 * pending chunk.
1298 */
1299 end = map->stripes[i].physical + em->orig_block_len;
1300 if (end > *start) {
1301 *start = end;
1302 ret = 1;
1303 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001304 }
1305 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001306 if (search_list != &fs_info->pinned_chunks) {
1307 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001308 goto again;
1309 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001310
1311 return ret;
1312}
1313
1314
Chris Mason0b86a832008-03-24 15:01:56 -04001315/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001316 * find_free_dev_extent_start - find free space in the specified device
1317 * @device: the device which we search the free space in
1318 * @num_bytes: the size of the free space that we need
1319 * @search_start: the position from which to begin the search
1320 * @start: store the start of the free space.
1321 * @len: the size of the free space. that we find, or the size
1322 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001323 *
Chris Mason0b86a832008-03-24 15:01:56 -04001324 * this uses a pretty simple search, the expectation is that it is
1325 * called very infrequently and that a given device has a small number
1326 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001327 *
1328 * @start is used to store the start of the free space if we find. But if we
1329 * don't find suitable free space, it will be used to store the start position
1330 * of the max free space.
1331 *
1332 * @len is used to store the size of the free space that we find.
1333 * But if we don't find suitable free space, it is used to store the size of
1334 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001335 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001336int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1337 struct btrfs_device *device, u64 num_bytes,
1338 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001339{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001340 struct btrfs_fs_info *fs_info = device->fs_info;
1341 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001342 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001343 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001344 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001345 u64 hole_size;
1346 u64 max_hole_start;
1347 u64 max_hole_size;
1348 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001349 u64 search_end = device->total_bytes;
1350 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001351 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001352 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001353
1354 /*
1355 * We don't want to overwrite the superblock on the drive nor any area
1356 * used by the boot loader (grub for example), so we make sure to start
1357 * at an offset of at least 1MB.
1358 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001359 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001360
Josef Bacik6df9a952013-06-27 13:22:46 -04001361 path = btrfs_alloc_path();
1362 if (!path)
1363 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001364
Miao Xie7bfc8372011-01-05 10:07:26 +00001365 max_hole_start = search_start;
1366 max_hole_size = 0;
1367
Zhao Leif2ab7612015-02-16 18:52:17 +08001368again:
Anand Jain401e29c2017-12-04 12:54:55 +08001369 if (search_start >= search_end ||
1370 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001371 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001372 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001373 }
1374
David Sterbae4058b52015-11-27 16:31:35 +01001375 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001376 path->search_commit_root = 1;
1377 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001378
Chris Mason0b86a832008-03-24 15:01:56 -04001379 key.objectid = device->devid;
1380 key.offset = search_start;
1381 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001382
Li Zefan125ccb02011-12-08 15:07:24 +08001383 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001384 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001385 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001386 if (ret > 0) {
1387 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1388 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001389 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001390 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001391
Chris Mason0b86a832008-03-24 15:01:56 -04001392 while (1) {
1393 l = path->nodes[0];
1394 slot = path->slots[0];
1395 if (slot >= btrfs_header_nritems(l)) {
1396 ret = btrfs_next_leaf(root, path);
1397 if (ret == 0)
1398 continue;
1399 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001400 goto out;
1401
1402 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001403 }
1404 btrfs_item_key_to_cpu(l, &key, slot);
1405
1406 if (key.objectid < device->devid)
1407 goto next;
1408
1409 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001410 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001411
David Sterba962a2982014-06-04 18:41:45 +02001412 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001413 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001414
Miao Xie7bfc8372011-01-05 10:07:26 +00001415 if (key.offset > search_start) {
1416 hole_size = key.offset - search_start;
1417
Josef Bacik6df9a952013-06-27 13:22:46 -04001418 /*
1419 * Have to check before we set max_hole_start, otherwise
1420 * we could end up sending back this offset anyway.
1421 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001422 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001423 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001424 hole_size)) {
1425 if (key.offset >= search_start) {
1426 hole_size = key.offset - search_start;
1427 } else {
1428 WARN_ON_ONCE(1);
1429 hole_size = 0;
1430 }
1431 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001432
Miao Xie7bfc8372011-01-05 10:07:26 +00001433 if (hole_size > max_hole_size) {
1434 max_hole_start = search_start;
1435 max_hole_size = hole_size;
1436 }
1437
1438 /*
1439 * If this free space is greater than which we need,
1440 * it must be the max free space that we have found
1441 * until now, so max_hole_start must point to the start
1442 * of this free space and the length of this free space
1443 * is stored in max_hole_size. Thus, we return
1444 * max_hole_start and max_hole_size and go back to the
1445 * caller.
1446 */
1447 if (hole_size >= num_bytes) {
1448 ret = 0;
1449 goto out;
1450 }
1451 }
1452
Chris Mason0b86a832008-03-24 15:01:56 -04001453 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001454 extent_end = key.offset + btrfs_dev_extent_length(l,
1455 dev_extent);
1456 if (extent_end > search_start)
1457 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001458next:
1459 path->slots[0]++;
1460 cond_resched();
1461 }
Chris Mason0b86a832008-03-24 15:01:56 -04001462
liubo38c01b92011-08-02 02:39:03 +00001463 /*
1464 * At this point, search_start should be the end of
1465 * allocated dev extents, and when shrinking the device,
1466 * search_end may be smaller than search_start.
1467 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001468 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001469 hole_size = search_end - search_start;
1470
Jeff Mahoney499f3772015-06-15 09:41:17 -04001471 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001472 hole_size)) {
1473 btrfs_release_path(path);
1474 goto again;
1475 }
Chris Mason0b86a832008-03-24 15:01:56 -04001476
Zhao Leif2ab7612015-02-16 18:52:17 +08001477 if (hole_size > max_hole_size) {
1478 max_hole_start = search_start;
1479 max_hole_size = hole_size;
1480 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001481 }
1482
Miao Xie7bfc8372011-01-05 10:07:26 +00001483 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001484 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001485 ret = -ENOSPC;
1486 else
1487 ret = 0;
1488
1489out:
Yan Zheng2b820322008-11-17 21:11:30 -05001490 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001491 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001492 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001493 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001494 return ret;
1495}
1496
Jeff Mahoney499f3772015-06-15 09:41:17 -04001497int find_free_dev_extent(struct btrfs_trans_handle *trans,
1498 struct btrfs_device *device, u64 num_bytes,
1499 u64 *start, u64 *len)
1500{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001501 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001502 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001503 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001504}
1505
Christoph Hellwigb2950862008-12-02 09:54:17 -05001506static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001507 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001508 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001509{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001510 struct btrfs_fs_info *fs_info = device->fs_info;
1511 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001512 int ret;
1513 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001514 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001515 struct btrfs_key found_key;
1516 struct extent_buffer *leaf = NULL;
1517 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001518
1519 path = btrfs_alloc_path();
1520 if (!path)
1521 return -ENOMEM;
1522
1523 key.objectid = device->devid;
1524 key.offset = start;
1525 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001526again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001527 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001528 if (ret > 0) {
1529 ret = btrfs_previous_item(root, path, key.objectid,
1530 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001531 if (ret)
1532 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001533 leaf = path->nodes[0];
1534 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1535 extent = btrfs_item_ptr(leaf, path->slots[0],
1536 struct btrfs_dev_extent);
1537 BUG_ON(found_key.offset > start || found_key.offset +
1538 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001539 key = found_key;
1540 btrfs_release_path(path);
1541 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001542 } else if (ret == 0) {
1543 leaf = path->nodes[0];
1544 extent = btrfs_item_ptr(leaf, path->slots[0],
1545 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001547 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001548 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001549 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001550
Miao Xie2196d6e2014-09-03 21:35:41 +08001551 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1552
Chris Mason8f18cf12008-04-25 16:53:30 -04001553 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001554 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001555 btrfs_handle_fs_error(fs_info, ret,
1556 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001557 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001558 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001559 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001560out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001561 btrfs_free_path(path);
1562 return ret;
1563}
1564
Eric Sandeen48a3b632013-04-25 20:41:01 +00001565static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1566 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001567 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001568{
1569 int ret;
1570 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001571 struct btrfs_fs_info *fs_info = device->fs_info;
1572 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001573 struct btrfs_dev_extent *extent;
1574 struct extent_buffer *leaf;
1575 struct btrfs_key key;
1576
Anand Jaine12c9622017-12-04 12:54:53 +08001577 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001578 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001579 path = btrfs_alloc_path();
1580 if (!path)
1581 return -ENOMEM;
1582
Chris Mason0b86a832008-03-24 15:01:56 -04001583 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001584 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001585 key.type = BTRFS_DEV_EXTENT_KEY;
1586 ret = btrfs_insert_empty_item(trans, root, path, &key,
1587 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001588 if (ret)
1589 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001590
1591 leaf = path->nodes[0];
1592 extent = btrfs_item_ptr(leaf, path->slots[0],
1593 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001594 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1595 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001596 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1597 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001598 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1599
Chris Mason0b86a832008-03-24 15:01:56 -04001600 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1601 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001602out:
Chris Mason0b86a832008-03-24 15:01:56 -04001603 btrfs_free_path(path);
1604 return ret;
1605}
1606
Josef Bacik6df9a952013-06-27 13:22:46 -04001607static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001608{
Josef Bacik6df9a952013-06-27 13:22:46 -04001609 struct extent_map_tree *em_tree;
1610 struct extent_map *em;
1611 struct rb_node *n;
1612 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001613
Josef Bacik6df9a952013-06-27 13:22:46 -04001614 em_tree = &fs_info->mapping_tree.map_tree;
1615 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08001616 n = rb_last(&em_tree->map.rb_root);
Josef Bacik6df9a952013-06-27 13:22:46 -04001617 if (n) {
1618 em = rb_entry(n, struct extent_map, rb_node);
1619 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001620 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001621 read_unlock(&em_tree->lock);
1622
Chris Mason0b86a832008-03-24 15:01:56 -04001623 return ret;
1624}
1625
Ilya Dryomov53f10652013-08-12 14:33:01 +03001626static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1627 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001628{
1629 int ret;
1630 struct btrfs_key key;
1631 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001632 struct btrfs_path *path;
1633
Yan Zheng2b820322008-11-17 21:11:30 -05001634 path = btrfs_alloc_path();
1635 if (!path)
1636 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001637
1638 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1639 key.type = BTRFS_DEV_ITEM_KEY;
1640 key.offset = (u64)-1;
1641
Ilya Dryomov53f10652013-08-12 14:33:01 +03001642 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001643 if (ret < 0)
1644 goto error;
1645
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001646 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001647
Ilya Dryomov53f10652013-08-12 14:33:01 +03001648 ret = btrfs_previous_item(fs_info->chunk_root, path,
1649 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001650 BTRFS_DEV_ITEM_KEY);
1651 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001652 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001653 } else {
1654 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1655 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001656 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001657 }
1658 ret = 0;
1659error:
Yan Zheng2b820322008-11-17 21:11:30 -05001660 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001661 return ret;
1662}
1663
1664/*
1665 * the device information is stored in the chunk root
1666 * the btrfs_device struct should be fully filled in
1667 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001668static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001669 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001670{
1671 int ret;
1672 struct btrfs_path *path;
1673 struct btrfs_dev_item *dev_item;
1674 struct extent_buffer *leaf;
1675 struct btrfs_key key;
1676 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001677
Chris Mason0b86a832008-03-24 15:01:56 -04001678 path = btrfs_alloc_path();
1679 if (!path)
1680 return -ENOMEM;
1681
Chris Mason0b86a832008-03-24 15:01:56 -04001682 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1683 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001684 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001685
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001686 ret = btrfs_insert_empty_item(trans, trans->fs_info->chunk_root, path,
1687 &key, sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001688 if (ret)
1689 goto out;
1690
1691 leaf = path->nodes[0];
1692 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1693
1694 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001695 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001696 btrfs_set_device_type(leaf, dev_item, device->type);
1697 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1698 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1699 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001700 btrfs_set_device_total_bytes(leaf, dev_item,
1701 btrfs_device_get_disk_total_bytes(device));
1702 btrfs_set_device_bytes_used(leaf, dev_item,
1703 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001704 btrfs_set_device_group(leaf, dev_item, 0);
1705 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1706 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001707 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001708
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001709 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001710 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001711 ptr = btrfs_device_fsid(dev_item);
Nikolay Borisov8e87e852018-07-20 19:37:47 +03001712 write_extent_buffer(leaf, trans->fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001713 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001714
Yan Zheng2b820322008-11-17 21:11:30 -05001715 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001716out:
1717 btrfs_free_path(path);
1718 return ret;
1719}
Chris Mason8f18cf12008-04-25 16:53:30 -04001720
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001721/*
1722 * Function to update ctime/mtime for a given device path.
1723 * Mainly used for ctime/mtime based probe like libblkid.
1724 */
David Sterbada353f62017-02-14 17:55:53 +01001725static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001726{
1727 struct file *filp;
1728
1729 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001730 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001731 return;
1732 file_update_time(filp);
1733 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001734}
1735
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001736static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001737 struct btrfs_device *device)
1738{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001739 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001740 int ret;
1741 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001742 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001743 struct btrfs_trans_handle *trans;
1744
Chris Masona061fc82008-05-07 11:43:44 -04001745 path = btrfs_alloc_path();
1746 if (!path)
1747 return -ENOMEM;
1748
Yan, Zhenga22285a2010-05-16 10:48:46 -04001749 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001750 if (IS_ERR(trans)) {
1751 btrfs_free_path(path);
1752 return PTR_ERR(trans);
1753 }
Chris Masona061fc82008-05-07 11:43:44 -04001754 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1755 key.type = BTRFS_DEV_ITEM_KEY;
1756 key.offset = device->devid;
1757
1758 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001759 if (ret) {
1760 if (ret > 0)
1761 ret = -ENOENT;
1762 btrfs_abort_transaction(trans, ret);
1763 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001764 goto out;
1765 }
1766
1767 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001768 if (ret) {
1769 btrfs_abort_transaction(trans, ret);
1770 btrfs_end_transaction(trans);
1771 }
1772
Chris Masona061fc82008-05-07 11:43:44 -04001773out:
1774 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001775 if (!ret)
1776 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001777 return ret;
1778}
1779
David Sterba3cc31a02016-02-15 16:00:26 +01001780/*
1781 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1782 * filesystem. It's up to the caller to adjust that number regarding eg. device
1783 * replace.
1784 */
1785static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1786 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001787{
Chris Masona061fc82008-05-07 11:43:44 -04001788 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001789 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001790 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001791
Miao Xiede98ced2013-01-29 10:13:12 +00001792 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001793 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001794
Anand Jainbd45ffb2016-02-13 10:01:34 +08001795 all_avail = fs_info->avail_data_alloc_bits |
1796 fs_info->avail_system_alloc_bits |
1797 fs_info->avail_metadata_alloc_bits;
1798 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001799
David Sterba418775a2016-02-15 16:28:14 +01001800 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001801 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001802 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001803
David Sterba418775a2016-02-15 16:28:14 +01001804 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001805 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001806
David Sterba418775a2016-02-15 16:28:14 +01001807 if (ret)
1808 return ret;
1809 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001810 }
1811
1812 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001813}
1814
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001815static struct btrfs_device * btrfs_find_next_active_device(
1816 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001817{
1818 struct btrfs_device *next_device;
1819
1820 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1821 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001822 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1823 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001824 return next_device;
1825 }
1826
1827 return NULL;
1828}
1829
1830/*
1831 * Helper function to check if the given device is part of s_bdev / latest_bdev
1832 * and replace it with the provided or the next active device, in the context
1833 * where this function called, there should be always be another device (or
1834 * this_dev) which is active.
1835 */
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001836void btrfs_assign_next_active_device(struct btrfs_device *device,
1837 struct btrfs_device *this_dev)
Anand Jain88acff62016-05-03 17:44:43 +08001838{
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001839 struct btrfs_fs_info *fs_info = device->fs_info;
Anand Jain88acff62016-05-03 17:44:43 +08001840 struct btrfs_device *next_device;
1841
1842 if (this_dev)
1843 next_device = this_dev;
1844 else
1845 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1846 device);
1847 ASSERT(next_device);
1848
1849 if (fs_info->sb->s_bdev &&
1850 (fs_info->sb->s_bdev == device->bdev))
1851 fs_info->sb->s_bdev = next_device->bdev;
1852
1853 if (fs_info->fs_devices->latest_bdev == device->bdev)
1854 fs_info->fs_devices->latest_bdev = next_device->bdev;
1855}
1856
Anand Jain1da73962018-08-10 13:53:21 +08001857/*
1858 * Return btrfs_fs_devices::num_devices excluding the device that's being
1859 * currently replaced.
1860 */
1861static u64 btrfs_num_devices(struct btrfs_fs_info *fs_info)
1862{
1863 u64 num_devices = fs_info->fs_devices->num_devices;
1864
1865 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
1866 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1867 ASSERT(num_devices > 1);
1868 num_devices--;
1869 }
1870 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
1871
1872 return num_devices;
1873}
1874
David Sterbada353f62017-02-14 17:55:53 +01001875int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1876 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001877{
1878 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001879 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001880 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001881 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001882 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001883
1884 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001885
Anand Jain1da73962018-08-10 13:53:21 +08001886 num_devices = btrfs_num_devices(fs_info);
Chris Masona061fc82008-05-07 11:43:44 -04001887
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001889 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001890 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001891
Nikolay Borisova27a94c2018-09-03 12:46:14 +03001892 device = btrfs_find_device_by_devspec(fs_info, devid, device_path);
1893
1894 if (IS_ERR(device)) {
1895 if (PTR_ERR(device) == -ENOENT &&
1896 strcmp(device_path, "missing") == 0)
1897 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
1898 else
1899 ret = PTR_ERR(device);
Chris Masona061fc82008-05-07 11:43:44 -04001900 goto out;
Nikolay Borisova27a94c2018-09-03 12:46:14 +03001901 }
Yan Zheng2b820322008-11-17 21:11:30 -05001902
Omar Sandovaleede2bf2016-11-03 10:28:12 -07001903 if (btrfs_pinned_by_swapfile(fs_info, device)) {
1904 btrfs_warn_in_rcu(fs_info,
1905 "cannot remove device %s (devid %llu) due to active swapfile",
1906 rcu_str_deref(device->name), device->devid);
1907 ret = -ETXTBSY;
1908 goto out;
1909 }
1910
Anand Jain401e29c2017-12-04 12:54:55 +08001911 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001912 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001913 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001914 }
1915
Anand Jainebbede42017-12-04 12:54:52 +08001916 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1917 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001918 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001919 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001920 }
1921
Anand Jainebbede42017-12-04 12:54:52 +08001922 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001923 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001924 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001925 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001926 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001927 }
Chris Masona061fc82008-05-07 11:43:44 -04001928
Carey Underwoodd7901552013-03-04 16:37:06 -06001929 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001930 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001931 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001932 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001933 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001934
Stefan Behrens63a212a2012-11-05 18:29:28 +01001935 /*
1936 * TODO: the superblock still includes this device in its num_devices
1937 * counter although write_all_supers() is not locked out. This
1938 * could give a filesystem state which requires a degraded mount.
1939 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001940 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001941 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001942 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001943
Anand Jaine12c9622017-12-04 12:54:53 +08001944 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001945 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001946
1947 /*
1948 * the device list mutex makes sure that we don't change
1949 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001950 * the device supers. Whoever is writing all supers, should
1951 * lock the device list mutex before getting the number of
1952 * devices in the super block (super_copy). Conversely,
1953 * whoever updates the number of devices in the super block
1954 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001955 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001956
Anand Jain41a52a02018-04-12 10:29:31 +08001957 /*
1958 * In normal cases the cur_devices == fs_devices. But in case
1959 * of deleting a seed device, the cur_devices should point to
1960 * its own fs_devices listed under the fs_devices->seed.
1961 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001962 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001963 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001964 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001965
Anand Jain41a52a02018-04-12 10:29:31 +08001966 cur_devices->num_devices--;
1967 cur_devices->total_devices--;
Anand Jainb4993e62018-07-03 17:07:23 +08001968 /* Update total_devices of the parent fs_devices if it's seed */
1969 if (cur_devices != fs_devices)
1970 fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001971
Anand Jaine6e674b2017-12-04 12:54:54 +08001972 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08001973 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001974
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03001975 btrfs_assign_next_active_device(device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05001976
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001977 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08001978 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001979 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08001980 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001981 }
Anand Jain99994cd2014-06-03 11:36:00 +08001982
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001983 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
1984 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08001985 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001986
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001987 /*
1988 * at this point, the device is zero sized and detached from
1989 * the devices list. All that's left is to zero out the old
1990 * supers and free the device.
1991 */
Anand Jainebbede42017-12-04 12:54:52 +08001992 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001993 btrfs_scratch_superblocks(device->bdev, device->name->str);
1994
1995 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001996 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04001997
Xiao Guangrong1f781602011-04-20 10:09:16 +00001998 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001999 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002000 if (fs_devices->seed == cur_devices) {
2001 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002002 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002003 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002004 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002005 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002006 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002007 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002008 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002009 }
2010
Chris Masona061fc82008-05-07 11:43:44 -04002011out:
2012 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002013 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002014
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002015error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002016 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002017 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002018 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002019 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002020 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002021 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002022 }
Anand Jain24fc5722016-02-13 10:01:36 +08002023 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002024}
2025
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002026void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002027{
Anand Jaind51908c2014-08-13 14:24:19 +08002028 struct btrfs_fs_devices *fs_devices;
2029
Nikolay Borisov68a9db52018-07-20 19:37:48 +03002030 lockdep_assert_held(&srcdev->fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002031
Anand Jain25e8e912014-08-20 10:56:56 +08002032 /*
2033 * in case of fs with no seed, srcdev->fs_devices will point
2034 * to fs_devices of fs_info. However when the dev being replaced is
2035 * a seed dev it will point to the seed's local fs_devices. In short
2036 * srcdev will have its correct fs_devices in both the cases.
2037 */
2038 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002039
Stefan Behrense93c89c2012-11-05 17:33:06 +01002040 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002041 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002042 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002043 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002044 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002045
Anand Jainebbede42017-12-04 12:54:52 +08002046 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002047 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002048
Miao Xie82372bc2014-09-03 21:35:44 +08002049 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002050 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002051}
2052
2053void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2054 struct btrfs_device *srcdev)
2055{
2056 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002057
Anand Jainebbede42017-12-04 12:54:52 +08002058 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002059 /* zero out the old super if it is writable */
2060 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2061 }
Anand Jain14238812016-07-22 06:04:53 +08002062
2063 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002064 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002065
Anand Jain94d5f0c2014-08-13 14:24:22 +08002066 /* if this is no devs we rather delete the fs_devices */
2067 if (!fs_devices->num_devices) {
2068 struct btrfs_fs_devices *tmp_fs_devices;
2069
Anand Jain6dd38f82017-10-17 06:53:50 +08002070 /*
2071 * On a mounted FS, num_devices can't be zero unless it's a
2072 * seed. In case of a seed device being replaced, the replace
2073 * target added to the sprout FS, so there will be no more
2074 * device left under the seed FS.
2075 */
2076 ASSERT(fs_devices->seeding);
2077
Anand Jain94d5f0c2014-08-13 14:24:22 +08002078 tmp_fs_devices = fs_info->fs_devices;
2079 while (tmp_fs_devices) {
2080 if (tmp_fs_devices->seed == fs_devices) {
2081 tmp_fs_devices->seed = fs_devices->seed;
2082 break;
2083 }
2084 tmp_fs_devices = tmp_fs_devices->seed;
2085 }
2086 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002087 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002088 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002089 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002090}
2091
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002092void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002093{
Nikolay Borisov4f5ad7b2018-07-20 19:37:51 +03002094 struct btrfs_fs_devices *fs_devices = tgtdev->fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002095
Anand Jaind9a071f2018-04-12 10:29:38 +08002096 WARN_ON(!tgtdev);
2097 mutex_lock(&fs_devices->device_list_mutex);
2098
2099 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002100
Anand Jain779bf3fe2016-04-18 16:51:23 +08002101 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002102 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002103
Anand Jaind9a071f2018-04-12 10:29:38 +08002104 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002105
Nikolay Borisovd6507cf2018-07-20 19:37:50 +03002106 btrfs_assign_next_active_device(tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002107
Stefan Behrense93c89c2012-11-05 17:33:06 +01002108 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002109
Anand Jaind9a071f2018-04-12 10:29:38 +08002110 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002111
2112 /*
2113 * The update_dev_time() with in btrfs_scratch_superblocks()
2114 * may lead to a call to btrfs_show_devname() which will try
2115 * to hold device_list_mutex. And here this device
2116 * is already out of device list, so we don't have to hold
2117 * the device_list_mutex lock.
2118 */
2119 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002120
2121 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002122 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002123}
2124
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002125static struct btrfs_device *btrfs_find_device_by_path(
2126 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002127{
2128 int ret = 0;
2129 struct btrfs_super_block *disk_super;
2130 u64 devid;
2131 u8 *dev_uuid;
2132 struct block_device *bdev;
2133 struct buffer_head *bh;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002134 struct btrfs_device *device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002135
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002136 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002137 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002138 if (ret)
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002139 return ERR_PTR(ret);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002140 disk_super = (struct btrfs_super_block *)bh->b_data;
2141 devid = btrfs_stack_device_id(&disk_super->dev_item);
2142 dev_uuid = disk_super->dev_item.uuid;
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002143 device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002144 brelse(bh);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002145 if (!device)
2146 device = ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002147 blkdev_put(bdev, FMODE_READ);
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002148 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002149}
2150
Nikolay Borisov6c050402018-09-03 12:46:13 +03002151static struct btrfs_device *btrfs_find_device_missing_or_by_path(
2152 struct btrfs_fs_info *fs_info, const char *device_path)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002153{
Nikolay Borisov6c050402018-09-03 12:46:13 +03002154 struct btrfs_device *device = NULL;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002155 if (strcmp(device_path, "missing") == 0) {
2156 struct list_head *devices;
2157 struct btrfs_device *tmp;
2158
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002159 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002160 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002161 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2162 &tmp->dev_state) && !tmp->bdev) {
Nikolay Borisov6c050402018-09-03 12:46:13 +03002163 device = tmp;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002164 break;
2165 }
2166 }
2167
Nikolay Borisov6c050402018-09-03 12:46:13 +03002168 if (!device)
2169 return ERR_PTR(-ENOENT);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002170 } else {
Nikolay Borisov6c050402018-09-03 12:46:13 +03002171 device = btrfs_find_device_by_path(fs_info, device_path);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002172 }
Nikolay Borisovb444ad42018-09-03 12:46:12 +03002173
Nikolay Borisov6c050402018-09-03 12:46:13 +03002174 return device;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002175}
2176
Yan Zheng2b820322008-11-17 21:11:30 -05002177/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002178 * Lookup a device given by device id, or the path if the id is 0.
2179 */
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002180struct btrfs_device *btrfs_find_device_by_devspec(
2181 struct btrfs_fs_info *fs_info, u64 devid, const char *devpath)
Anand Jain24e04742016-02-13 10:01:35 +08002182{
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002183 struct btrfs_device *device;
Anand Jain24e04742016-02-13 10:01:35 +08002184
David Sterba5c5c0df2016-02-15 16:39:55 +01002185 if (devid) {
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002186 device = btrfs_find_device(fs_info, devid, NULL, NULL);
2187 if (!device)
2188 return ERR_PTR(-ENOENT);
Anand Jain24e04742016-02-13 10:01:35 +08002189 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002190 if (!devpath || !devpath[0])
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002191 return ERR_PTR(-EINVAL);
2192 device = btrfs_find_device_missing_or_by_path(fs_info, devpath);
Anand Jain24e04742016-02-13 10:01:35 +08002193 }
Nikolay Borisova27a94c2018-09-03 12:46:14 +03002194 return device;
Anand Jain24e04742016-02-13 10:01:35 +08002195}
2196
Yan Zheng2b820322008-11-17 21:11:30 -05002197/*
2198 * does all the dirty work required for changing file system's UUID.
2199 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002200static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002201{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002203 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002204 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002206 struct btrfs_device *device;
2207 u64 super_flags;
2208
David Sterbaa32bf9a2018-03-16 02:21:22 +01002209 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002210 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002211 return -EINVAL;
2212
David Sterba2dfeca92017-06-14 02:48:07 +02002213 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002214 if (IS_ERR(seed_devices))
2215 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002216
Yan Zhenge4404d62008-12-12 10:03:26 -05002217 old_devices = clone_fs_devices(fs_devices);
2218 if (IS_ERR(old_devices)) {
2219 kfree(seed_devices);
2220 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002221 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002222
Anand Jainc4babc52018-04-12 10:29:25 +08002223 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002224
Yan Zhenge4404d62008-12-12 10:03:26 -05002225 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2226 seed_devices->opened = 1;
2227 INIT_LIST_HEAD(&seed_devices->devices);
2228 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002229 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002230
Anand Jain321a4bf2018-07-16 22:58:09 +08002231 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002232 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2233 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002234 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002235 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002236
David Sterba34441362016-10-04 19:34:27 +02002237 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002238 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002239 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002240
Yan Zheng2b820322008-11-17 21:11:30 -05002241 fs_devices->seeding = 0;
2242 fs_devices->num_devices = 0;
2243 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002244 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002245 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002246 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002247
2248 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002249 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002250 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Anand Jain321a4bf2018-07-16 22:58:09 +08002251 mutex_unlock(&fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002252
Yan Zheng2b820322008-11-17 21:11:30 -05002253 super_flags = btrfs_super_flags(disk_super) &
2254 ~BTRFS_SUPER_FLAG_SEEDING;
2255 btrfs_set_super_flags(disk_super, super_flags);
2256
2257 return 0;
2258}
2259
2260/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002261 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002262 */
2263static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002264 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002265{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002266 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002267 struct btrfs_path *path;
2268 struct extent_buffer *leaf;
2269 struct btrfs_dev_item *dev_item;
2270 struct btrfs_device *device;
2271 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002272 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002273 u8 dev_uuid[BTRFS_UUID_SIZE];
2274 u64 devid;
2275 int ret;
2276
2277 path = btrfs_alloc_path();
2278 if (!path)
2279 return -ENOMEM;
2280
Yan Zheng2b820322008-11-17 21:11:30 -05002281 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2282 key.offset = 0;
2283 key.type = BTRFS_DEV_ITEM_KEY;
2284
2285 while (1) {
2286 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2287 if (ret < 0)
2288 goto error;
2289
2290 leaf = path->nodes[0];
2291next_slot:
2292 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2293 ret = btrfs_next_leaf(root, path);
2294 if (ret > 0)
2295 break;
2296 if (ret < 0)
2297 goto error;
2298 leaf = path->nodes[0];
2299 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002300 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002301 continue;
2302 }
2303
2304 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2305 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2306 key.type != BTRFS_DEV_ITEM_KEY)
2307 break;
2308
2309 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2310 struct btrfs_dev_item);
2311 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002312 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002313 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002314 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002315 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002317 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002318
2319 if (device->fs_devices->seeding) {
2320 btrfs_set_device_generation(leaf, dev_item,
2321 device->generation);
2322 btrfs_mark_buffer_dirty(leaf);
2323 }
2324
2325 path->slots[0]++;
2326 goto next_slot;
2327 }
2328 ret = 0;
2329error:
2330 btrfs_free_path(path);
2331 return ret;
2332}
2333
David Sterbada353f62017-02-14 17:55:53 +01002334int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002335{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002336 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002337 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002338 struct btrfs_trans_handle *trans;
2339 struct btrfs_device *device;
2340 struct block_device *bdev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002341 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002342 struct rcu_string *name;
Anand Jain5da54bc2018-07-03 13:14:50 +08002343 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Naohiro Aota39379fa2018-07-27 09:04:55 +09002344 u64 orig_super_total_bytes;
2345 u64 orig_super_num_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002346 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002347 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002348 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002349
Anand Jain5da54bc2018-07-03 13:14:50 +08002350 if (sb_rdonly(sb) && !fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002351 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002352
Li Zefana5d16332011-12-07 20:08:40 -05002353 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002354 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002355 if (IS_ERR(bdev))
2356 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002357
Anand Jain5da54bc2018-07-03 13:14:50 +08002358 if (fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002359 seeding_dev = 1;
2360 down_write(&sb->s_umount);
2361 mutex_lock(&uuid_mutex);
2362 }
2363
Chris Mason8c8bee12008-09-29 11:19:10 -04002364 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002365
Anand Jain5da54bc2018-07-03 13:14:50 +08002366 mutex_lock(&fs_devices->device_list_mutex);
Anand Jain694c51f2018-07-03 13:14:51 +08002367 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002368 if (device->bdev == bdev) {
2369 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002370 mutex_unlock(
Anand Jain5da54bc2018-07-03 13:14:50 +08002371 &fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002372 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002373 }
2374 }
Anand Jain5da54bc2018-07-03 13:14:50 +08002375 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002376
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002377 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002378 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002379 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002380 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002381 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002382 }
2383
David Sterba78f2c9e2016-02-11 14:25:38 +01002384 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002385 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002386 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002387 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002388 }
Josef Bacik606686e2012-06-04 14:03:51 -04002389 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002390
Yan, Zhenga22285a2010-05-16 10:48:46 -04002391 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002392 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002393 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002394 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002395 }
2396
Josef Bacikd5e20032011-08-04 14:52:27 +00002397 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002398 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002399 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002400 device->io_width = fs_info->sectorsize;
2401 device->io_align = fs_info->sectorsize;
2402 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002403 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2404 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002405 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002406 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002407 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002408 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002409 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002410 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002411 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002412 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002413 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002414
Yan Zheng2b820322008-11-17 21:11:30 -05002415 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002416 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002417 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002418 if (ret) {
2419 btrfs_abort_transaction(trans, ret);
2420 goto error_trans;
2421 }
Yan Zheng2b820322008-11-17 21:11:30 -05002422 }
2423
Anand Jain5da54bc2018-07-03 13:14:50 +08002424 device->fs_devices = fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002425
Anand Jain5da54bc2018-07-03 13:14:50 +08002426 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002427 mutex_lock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002428 list_add_rcu(&device->dev_list, &fs_devices->devices);
2429 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
2430 fs_devices->num_devices++;
2431 fs_devices->open_devices++;
2432 fs_devices->rw_devices++;
2433 fs_devices->total_devices++;
2434 fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002435
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002436 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002437
Anand Jaine884f4f2017-04-04 18:40:19 +08002438 if (!blk_queue_nonrot(q))
Anand Jain5da54bc2018-07-03 13:14:50 +08002439 fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002440
Naohiro Aota39379fa2018-07-27 09:04:55 +09002441 orig_super_total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002442 btrfs_set_super_total_bytes(fs_info->super_copy,
Naohiro Aota39379fa2018-07-27 09:04:55 +09002443 round_down(orig_super_total_bytes + device->total_bytes,
2444 fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002445
Naohiro Aota39379fa2018-07-27 09:04:55 +09002446 orig_super_num_devices = btrfs_super_num_devices(fs_info->super_copy);
2447 btrfs_set_super_num_devices(fs_info->super_copy,
2448 orig_super_num_devices + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002449
2450 /* add sysfs device entry */
Anand Jain5da54bc2018-07-03 13:14:50 +08002451 btrfs_sysfs_add_device_link(fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002452
Miao Xie2196d6e2014-09-03 21:35:41 +08002453 /*
2454 * we've got more storage, clear any full flags on the space
2455 * infos
2456 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002458
David Sterba34441362016-10-04 19:34:27 +02002459 mutex_unlock(&fs_info->chunk_mutex);
Anand Jain5da54bc2018-07-03 13:14:50 +08002460 mutex_unlock(&fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002461
Yan Zheng2b820322008-11-17 21:11:30 -05002462 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002463 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002464 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002465 mutex_unlock(&fs_info->chunk_mutex);
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 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002470 }
2471
Nikolay Borisov8e87e852018-07-20 19:37:47 +03002472 ret = btrfs_add_dev_item(trans, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002473 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002474 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002475 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002476 }
2477
2478 if (seeding_dev) {
2479 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2480
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002481 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002482 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002483 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002484 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002485 }
Anand Jainb2373f22014-06-03 11:36:03 +08002486
2487 /* Sprouting would change fsid of the mounted root,
2488 * so rename the fsid on the sysfs
2489 */
2490 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002491 fs_info->fsid);
Anand Jain5da54bc2018-07-03 13:14:50 +08002492 if (kobject_rename(&fs_devices->fsid_kobj, fsid_buf))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 btrfs_warn(fs_info,
2494 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002495 }
2496
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002497 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002498
2499 if (seeding_dev) {
2500 mutex_unlock(&uuid_mutex);
2501 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002502 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002503
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002504 if (ret) /* transaction commit */
2505 return ret;
2506
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002507 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002508 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002510 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002511 trans = btrfs_attach_transaction(root);
2512 if (IS_ERR(trans)) {
2513 if (PTR_ERR(trans) == -ENOENT)
2514 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002515 ret = PTR_ERR(trans);
2516 trans = NULL;
2517 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002518 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002519 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002520 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002521
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002522 /* Update ctime/mtime for libblkid */
2523 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002524 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002525
Anand Jaind31c32f2017-09-28 14:51:10 +08002526error_sysfs:
Anand Jain5da54bc2018-07-03 13:14:50 +08002527 btrfs_sysfs_rm_device_link(fs_devices, device);
Naohiro Aota39379fa2018-07-27 09:04:55 +09002528 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2529 mutex_lock(&fs_info->chunk_mutex);
2530 list_del_rcu(&device->dev_list);
2531 list_del(&device->dev_alloc_list);
2532 fs_info->fs_devices->num_devices--;
2533 fs_info->fs_devices->open_devices--;
2534 fs_info->fs_devices->rw_devices--;
2535 fs_info->fs_devices->total_devices--;
2536 fs_info->fs_devices->total_rw_bytes -= device->total_bytes;
2537 atomic64_sub(device->total_bytes, &fs_info->free_chunk_space);
2538 btrfs_set_super_total_bytes(fs_info->super_copy,
2539 orig_super_total_bytes);
2540 btrfs_set_super_num_devices(fs_info->super_copy,
2541 orig_super_num_devices);
2542 mutex_unlock(&fs_info->chunk_mutex);
2543 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002544error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002545 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002546 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002547 if (trans)
2548 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002549error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002550 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002551error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002552 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002553 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002554 mutex_unlock(&uuid_mutex);
2555 up_write(&sb->s_umount);
2556 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002557 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002558}
2559
Chris Masond3977122009-01-05 21:25:51 -05002560static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2561 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002562{
2563 int ret;
2564 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002565 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002566 struct btrfs_dev_item *dev_item;
2567 struct extent_buffer *leaf;
2568 struct btrfs_key key;
2569
Chris Mason0b86a832008-03-24 15:01:56 -04002570 path = btrfs_alloc_path();
2571 if (!path)
2572 return -ENOMEM;
2573
2574 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2575 key.type = BTRFS_DEV_ITEM_KEY;
2576 key.offset = device->devid;
2577
2578 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2579 if (ret < 0)
2580 goto out;
2581
2582 if (ret > 0) {
2583 ret = -ENOENT;
2584 goto out;
2585 }
2586
2587 leaf = path->nodes[0];
2588 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2589
2590 btrfs_set_device_id(leaf, dev_item, device->devid);
2591 btrfs_set_device_type(leaf, dev_item, device->type);
2592 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2593 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2594 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002595 btrfs_set_device_total_bytes(leaf, dev_item,
2596 btrfs_device_get_disk_total_bytes(device));
2597 btrfs_set_device_bytes_used(leaf, dev_item,
2598 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002599 btrfs_mark_buffer_dirty(leaf);
2600
2601out:
2602 btrfs_free_path(path);
2603 return ret;
2604}
2605
Miao Xie2196d6e2014-09-03 21:35:41 +08002606int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002607 struct btrfs_device *device, u64 new_size)
2608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002609 struct btrfs_fs_info *fs_info = device->fs_info;
2610 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002611 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002612 u64 old_total;
2613 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002614
Anand Jainebbede42017-12-04 12:54:52 +08002615 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002616 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002617
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002618 new_size = round_down(new_size, fs_info->sectorsize);
2619
David Sterba34441362016-10-04 19:34:27 +02002620 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002621 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002622 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002623
Stefan Behrens63a212a2012-11-05 18:29:28 +01002624 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002625 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002626 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002627 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002628 }
Yan Zheng2b820322008-11-17 21:11:30 -05002629
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002630 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002631
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002632 btrfs_set_super_total_bytes(super_copy,
2633 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002634 device->fs_devices->total_rw_bytes += diff;
2635
Miao Xie7cc8e582014-09-03 21:35:38 +08002636 btrfs_device_set_total_bytes(device, new_size);
2637 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002638 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002639 if (list_empty(&device->resized_list))
2640 list_add_tail(&device->resized_list,
2641 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002642 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002643
Chris Mason8f18cf12008-04-25 16:53:30 -04002644 return btrfs_update_device(trans, device);
2645}
2646
Nikolay Borisovf4208792018-07-20 19:37:52 +03002647static int btrfs_free_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002648{
Nikolay Borisovf4208792018-07-20 19:37:52 +03002649 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002650 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002651 int ret;
2652 struct btrfs_path *path;
2653 struct btrfs_key key;
2654
Chris Mason8f18cf12008-04-25 16:53:30 -04002655 path = btrfs_alloc_path();
2656 if (!path)
2657 return -ENOMEM;
2658
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002659 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002660 key.offset = chunk_offset;
2661 key.type = BTRFS_CHUNK_ITEM_KEY;
2662
2663 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002664 if (ret < 0)
2665 goto out;
2666 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 btrfs_handle_fs_error(fs_info, -ENOENT,
2668 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002669 ret = -ENOENT;
2670 goto out;
2671 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002672
2673 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002674 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 btrfs_handle_fs_error(fs_info, ret,
2676 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002677out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002678 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002679 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002680}
2681
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002682static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002683{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002684 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002685 struct btrfs_disk_key *disk_key;
2686 struct btrfs_chunk *chunk;
2687 u8 *ptr;
2688 int ret = 0;
2689 u32 num_stripes;
2690 u32 array_size;
2691 u32 len = 0;
2692 u32 cur;
2693 struct btrfs_key key;
2694
David Sterba34441362016-10-04 19:34:27 +02002695 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002696 array_size = btrfs_super_sys_array_size(super_copy);
2697
2698 ptr = super_copy->sys_chunk_array;
2699 cur = 0;
2700
2701 while (cur < array_size) {
2702 disk_key = (struct btrfs_disk_key *)ptr;
2703 btrfs_disk_key_to_cpu(&key, disk_key);
2704
2705 len = sizeof(*disk_key);
2706
2707 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2708 chunk = (struct btrfs_chunk *)(ptr + len);
2709 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2710 len += btrfs_chunk_item_size(num_stripes);
2711 } else {
2712 ret = -EIO;
2713 break;
2714 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002715 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002716 key.offset == chunk_offset) {
2717 memmove(ptr, ptr + len, array_size - (cur + len));
2718 array_size -= len;
2719 btrfs_set_super_sys_array_size(super_copy, array_size);
2720 } else {
2721 ptr += len;
2722 cur += len;
2723 }
2724 }
David Sterba34441362016-10-04 19:34:27 +02002725 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002726 return ret;
2727}
2728
Omar Sandoval60ca8422018-05-16 16:34:31 -07002729/*
2730 * btrfs_get_chunk_map() - Find the mapping containing the given logical extent.
2731 * @logical: Logical block offset in bytes.
2732 * @length: Length of extent in bytes.
2733 *
2734 * Return: Chunk mapping or ERR_PTR.
2735 */
2736struct extent_map *btrfs_get_chunk_map(struct btrfs_fs_info *fs_info,
2737 u64 logical, u64 length)
Liu Bo592d92e2017-03-14 13:33:55 -07002738{
2739 struct extent_map_tree *em_tree;
2740 struct extent_map *em;
2741
2742 em_tree = &fs_info->mapping_tree.map_tree;
2743 read_lock(&em_tree->lock);
2744 em = lookup_extent_mapping(em_tree, logical, length);
2745 read_unlock(&em_tree->lock);
2746
2747 if (!em) {
2748 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2749 logical, length);
2750 return ERR_PTR(-EINVAL);
2751 }
2752
2753 if (em->start > logical || em->start + em->len < logical) {
2754 btrfs_crit(fs_info,
2755 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2756 logical, length, em->start, em->start + em->len);
2757 free_extent_map(em);
2758 return ERR_PTR(-EINVAL);
2759 }
2760
2761 /* callers are responsible for dropping em's ref. */
2762 return em;
2763}
2764
Nikolay Borisov97aff912018-07-20 19:37:53 +03002765int btrfs_remove_chunk(struct btrfs_trans_handle *trans, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002766{
Nikolay Borisov97aff912018-07-20 19:37:53 +03002767 struct btrfs_fs_info *fs_info = trans->fs_info;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002768 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002769 struct map_lookup *map;
2770 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002771 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002772 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002773
Omar Sandoval60ca8422018-05-16 16:34:31 -07002774 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07002775 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002776 /*
2777 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002778 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002779 * do anything we still error out.
2780 */
2781 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002782 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002783 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002784 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002785 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisov451a2c12018-06-20 15:49:07 +03002786 check_system_chunk(trans, map->type);
David Sterba34441362016-10-04 19:34:27 +02002787 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002788
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002789 /*
2790 * Take the device list mutex to prevent races with the final phase of
2791 * a device replace operation that replaces the device object associated
2792 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2793 */
2794 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002795 for (i = 0; i < map->num_stripes; i++) {
2796 struct btrfs_device *device = map->stripes[i].dev;
2797 ret = btrfs_free_dev_extent(trans, device,
2798 map->stripes[i].physical,
2799 &dev_extent_len);
2800 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002801 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002802 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002803 goto out;
2804 }
2805
2806 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002807 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002808 btrfs_device_set_bytes_used(device,
2809 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002810 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002812 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002813 }
2814
2815 if (map->stripes[i].dev) {
2816 ret = btrfs_update_device(trans, map->stripes[i].dev);
2817 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002818 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002819 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002820 goto out;
2821 }
2822 }
2823 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002824 mutex_unlock(&fs_devices->device_list_mutex);
2825
Nikolay Borisovf4208792018-07-20 19:37:52 +03002826 ret = btrfs_free_chunk(trans, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002827 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002828 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002829 goto out;
2830 }
2831
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002832 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002833
2834 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002835 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002836 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002837 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002838 goto out;
2839 }
2840 }
2841
Nikolay Borisov5a98ec02018-06-20 15:48:56 +03002842 ret = btrfs_remove_block_group(trans, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002843 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002844 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002845 goto out;
2846 }
2847
Josef Bacik47ab2a62014-09-18 11:20:02 -04002848out:
2849 /* once for us */
2850 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002851 return ret;
2852}
2853
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002854static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002855{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002856 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002857 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002858 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002859
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002860 /*
2861 * Prevent races with automatic removal of unused block groups.
2862 * After we relocate and before we remove the chunk with offset
2863 * chunk_offset, automatic removal of the block group can kick in,
2864 * resulting in a failure when calling btrfs_remove_chunk() below.
2865 *
2866 * Make sure to acquire this mutex before doing a tree search (dev
2867 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2868 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2869 * we release the path used to search the chunk/dev tree and before
2870 * the current task acquires this mutex and calls us.
2871 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002872 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002873
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002875 if (ret)
2876 return -ENOSPC;
2877
Chris Mason8f18cf12008-04-25 16:53:30 -04002878 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002879 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002880 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002881 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002882 if (ret)
2883 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002884
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002885 /*
2886 * We add the kobjects here (and after forcing data chunk creation)
2887 * since relocation is the only place we'll create chunks of a new
2888 * type at runtime. The only place where we'll remove the last
2889 * chunk of a type is the call immediately below this one. Even
2890 * so, we're protected against races with the cleaner thread since
2891 * we're covered by the delete_unused_bgs_mutex.
2892 */
2893 btrfs_add_raid_kobjects(fs_info);
2894
Chris Mason19c4d2f2016-10-10 13:43:31 -07002895 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2896 chunk_offset);
2897 if (IS_ERR(trans)) {
2898 ret = PTR_ERR(trans);
2899 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2900 return ret;
2901 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002902
Chris Mason19c4d2f2016-10-10 13:43:31 -07002903 /*
2904 * step two, delete the device extents and the
2905 * chunk tree entries
2906 */
Nikolay Borisov97aff912018-07-20 19:37:53 +03002907 ret = btrfs_remove_chunk(trans, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002908 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002909 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002910}
2911
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002912static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002913{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002914 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002915 struct btrfs_path *path;
2916 struct extent_buffer *leaf;
2917 struct btrfs_chunk *chunk;
2918 struct btrfs_key key;
2919 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002920 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002921 bool retried = false;
2922 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002923 int ret;
2924
2925 path = btrfs_alloc_path();
2926 if (!path)
2927 return -ENOMEM;
2928
Josef Bacikba1bf482009-09-11 16:11:19 -04002929again:
Yan Zheng2b820322008-11-17 21:11:30 -05002930 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2931 key.offset = (u64)-1;
2932 key.type = BTRFS_CHUNK_ITEM_KEY;
2933
2934 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002935 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002936 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002937 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002939 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002940 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002941 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002942
2943 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2944 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002945 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002946 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002947 if (ret < 0)
2948 goto error;
2949 if (ret > 0)
2950 break;
2951
2952 leaf = path->nodes[0];
2953 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2954
2955 chunk = btrfs_item_ptr(leaf, path->slots[0],
2956 struct btrfs_chunk);
2957 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002958 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002959
2960 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002961 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002962 if (ret == -ENOSPC)
2963 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302964 else
2965 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002966 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002967 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002968
2969 if (found_key.offset == 0)
2970 break;
2971 key.offset = found_key.offset - 1;
2972 }
2973 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002974 if (failed && !retried) {
2975 failed = 0;
2976 retried = true;
2977 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302978 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002979 ret = -ENOSPC;
2980 }
Yan Zheng2b820322008-11-17 21:11:30 -05002981error:
2982 btrfs_free_path(path);
2983 return ret;
2984}
2985
Liu Boa6f93c72017-11-15 16:28:11 -07002986/*
2987 * return 1 : allocate a data chunk successfully,
2988 * return <0: errors during allocating a data chunk,
2989 * return 0 : no need to allocate a data chunk.
2990 */
2991static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
2992 u64 chunk_offset)
2993{
2994 struct btrfs_block_group_cache *cache;
2995 u64 bytes_used;
2996 u64 chunk_type;
2997
2998 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2999 ASSERT(cache);
3000 chunk_type = cache->flags;
3001 btrfs_put_block_group(cache);
3002
3003 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3004 spin_lock(&fs_info->data_sinfo->lock);
3005 bytes_used = fs_info->data_sinfo->bytes_used;
3006 spin_unlock(&fs_info->data_sinfo->lock);
3007
3008 if (!bytes_used) {
3009 struct btrfs_trans_handle *trans;
3010 int ret;
3011
3012 trans = btrfs_join_transaction(fs_info->tree_root);
3013 if (IS_ERR(trans))
3014 return PTR_ERR(trans);
3015
Nikolay Borisov43a7e992018-06-20 15:49:15 +03003016 ret = btrfs_force_chunk_alloc(trans,
Liu Boa6f93c72017-11-15 16:28:11 -07003017 BTRFS_BLOCK_GROUP_DATA);
3018 btrfs_end_transaction(trans);
3019 if (ret < 0)
3020 return ret;
3021
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003022 btrfs_add_raid_kobjects(fs_info);
3023
Liu Boa6f93c72017-11-15 16:28:11 -07003024 return 1;
3025 }
3026 }
3027 return 0;
3028}
3029
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003030static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003031 struct btrfs_balance_control *bctl)
3032{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003033 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003034 struct btrfs_trans_handle *trans;
3035 struct btrfs_balance_item *item;
3036 struct btrfs_disk_balance_args disk_bargs;
3037 struct btrfs_path *path;
3038 struct extent_buffer *leaf;
3039 struct btrfs_key key;
3040 int ret, err;
3041
3042 path = btrfs_alloc_path();
3043 if (!path)
3044 return -ENOMEM;
3045
3046 trans = btrfs_start_transaction(root, 0);
3047 if (IS_ERR(trans)) {
3048 btrfs_free_path(path);
3049 return PTR_ERR(trans);
3050 }
3051
3052 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003053 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003054 key.offset = 0;
3055
3056 ret = btrfs_insert_empty_item(trans, root, path, &key,
3057 sizeof(*item));
3058 if (ret)
3059 goto out;
3060
3061 leaf = path->nodes[0];
3062 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3063
David Sterbab159fa22016-11-08 18:09:03 +01003064 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003065
3066 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3067 btrfs_set_balance_data(leaf, item, &disk_bargs);
3068 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3069 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3070 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3071 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3072
3073 btrfs_set_balance_flags(leaf, item, bctl->flags);
3074
3075 btrfs_mark_buffer_dirty(leaf);
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
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003084static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003085{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003086 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003087 struct btrfs_trans_handle *trans;
3088 struct btrfs_path *path;
3089 struct btrfs_key key;
3090 int ret, err;
3091
3092 path = btrfs_alloc_path();
3093 if (!path)
3094 return -ENOMEM;
3095
3096 trans = btrfs_start_transaction(root, 0);
3097 if (IS_ERR(trans)) {
3098 btrfs_free_path(path);
3099 return PTR_ERR(trans);
3100 }
3101
3102 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003103 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003104 key.offset = 0;
3105
3106 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3107 if (ret < 0)
3108 goto out;
3109 if (ret > 0) {
3110 ret = -ENOENT;
3111 goto out;
3112 }
3113
3114 ret = btrfs_del_item(trans, root, path);
3115out:
3116 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003117 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003118 if (err && !ret)
3119 ret = err;
3120 return ret;
3121}
3122
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003123/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003124 * This is a heuristic used to reduce the number of chunks balanced on
3125 * resume after balance was interrupted.
3126 */
3127static void update_balance_args(struct btrfs_balance_control *bctl)
3128{
3129 /*
3130 * Turn on soft mode for chunk types that were being converted.
3131 */
3132 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3133 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3134 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3135 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3136 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3137 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3138
3139 /*
3140 * Turn on usage filter if is not already used. The idea is
3141 * that chunks that we have already balanced should be
3142 * reasonably full. Don't do it for chunks that are being
3143 * converted - that will keep us from relocating unconverted
3144 * (albeit full) chunks.
3145 */
3146 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003147 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003148 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3149 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3150 bctl->data.usage = 90;
3151 }
3152 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003153 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003154 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3155 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3156 bctl->sys.usage = 90;
3157 }
3158 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003159 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003160 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3161 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3162 bctl->meta.usage = 90;
3163 }
3164}
3165
3166/*
David Sterba149196a2018-03-20 20:23:09 +01003167 * Clear the balance status in fs_info and delete the balance item from disk.
3168 */
3169static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003170{
3171 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003172 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003173
3174 BUG_ON(!fs_info->balance_ctl);
3175
3176 spin_lock(&fs_info->balance_lock);
3177 fs_info->balance_ctl = NULL;
3178 spin_unlock(&fs_info->balance_lock);
3179
3180 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003181 ret = del_balance_item(fs_info);
3182 if (ret)
3183 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003184}
3185
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003186/*
3187 * Balance filters. Return 1 if chunk should be filtered out
3188 * (should not be balanced).
3189 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003190static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003191 struct btrfs_balance_args *bargs)
3192{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003193 chunk_type = chunk_to_extended(chunk_type) &
3194 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003195
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003196 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003197 return 0;
3198
3199 return 1;
3200}
3201
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003202static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003203 struct btrfs_balance_args *bargs)
3204{
3205 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003206 u64 chunk_used;
3207 u64 user_thresh_min;
3208 u64 user_thresh_max;
3209 int ret = 1;
3210
3211 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3212 chunk_used = btrfs_block_group_used(&cache->item);
3213
3214 if (bargs->usage_min == 0)
3215 user_thresh_min = 0;
3216 else
3217 user_thresh_min = div_factor_fine(cache->key.offset,
3218 bargs->usage_min);
3219
3220 if (bargs->usage_max == 0)
3221 user_thresh_max = 1;
3222 else if (bargs->usage_max > 100)
3223 user_thresh_max = cache->key.offset;
3224 else
3225 user_thresh_max = div_factor_fine(cache->key.offset,
3226 bargs->usage_max);
3227
3228 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3229 ret = 0;
3230
3231 btrfs_put_block_group(cache);
3232 return ret;
3233}
3234
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003235static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003236 u64 chunk_offset, struct btrfs_balance_args *bargs)
3237{
3238 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003239 u64 chunk_used, user_thresh;
3240 int ret = 1;
3241
3242 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3243 chunk_used = btrfs_block_group_used(&cache->item);
3244
David Sterbabc309462015-10-20 18:22:13 +02003245 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003246 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003247 else if (bargs->usage > 100)
3248 user_thresh = cache->key.offset;
3249 else
3250 user_thresh = div_factor_fine(cache->key.offset,
3251 bargs->usage);
3252
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003253 if (chunk_used < user_thresh)
3254 ret = 0;
3255
3256 btrfs_put_block_group(cache);
3257 return ret;
3258}
3259
Ilya Dryomov409d4042012-01-16 22:04:47 +02003260static int chunk_devid_filter(struct extent_buffer *leaf,
3261 struct btrfs_chunk *chunk,
3262 struct btrfs_balance_args *bargs)
3263{
3264 struct btrfs_stripe *stripe;
3265 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3266 int i;
3267
3268 for (i = 0; i < num_stripes; i++) {
3269 stripe = btrfs_stripe_nr(chunk, i);
3270 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3271 return 0;
3272 }
3273
3274 return 1;
3275}
3276
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003277/* [pstart, pend) */
3278static int chunk_drange_filter(struct extent_buffer *leaf,
3279 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003280 struct btrfs_balance_args *bargs)
3281{
3282 struct btrfs_stripe *stripe;
3283 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3284 u64 stripe_offset;
3285 u64 stripe_length;
3286 int factor;
3287 int i;
3288
3289 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3290 return 0;
3291
3292 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003293 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3294 factor = num_stripes / 2;
3295 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3296 factor = num_stripes - 1;
3297 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3298 factor = num_stripes - 2;
3299 } else {
3300 factor = num_stripes;
3301 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003302
3303 for (i = 0; i < num_stripes; i++) {
3304 stripe = btrfs_stripe_nr(chunk, i);
3305 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3306 continue;
3307
3308 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3309 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003310 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003311
3312 if (stripe_offset < bargs->pend &&
3313 stripe_offset + stripe_length > bargs->pstart)
3314 return 0;
3315 }
3316
3317 return 1;
3318}
3319
Ilya Dryomovea671762012-01-16 22:04:48 +02003320/* [vstart, vend) */
3321static int chunk_vrange_filter(struct extent_buffer *leaf,
3322 struct btrfs_chunk *chunk,
3323 u64 chunk_offset,
3324 struct btrfs_balance_args *bargs)
3325{
3326 if (chunk_offset < bargs->vend &&
3327 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3328 /* at least part of the chunk is inside this vrange */
3329 return 0;
3330
3331 return 1;
3332}
3333
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003334static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3335 struct btrfs_chunk *chunk,
3336 struct btrfs_balance_args *bargs)
3337{
3338 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3339
3340 if (bargs->stripes_min <= num_stripes
3341 && num_stripes <= bargs->stripes_max)
3342 return 0;
3343
3344 return 1;
3345}
3346
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003347static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003348 struct btrfs_balance_args *bargs)
3349{
3350 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3351 return 0;
3352
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003353 chunk_type = chunk_to_extended(chunk_type) &
3354 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003355
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003356 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003357 return 1;
3358
3359 return 0;
3360}
3361
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003362static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003363 struct extent_buffer *leaf,
3364 struct btrfs_chunk *chunk, u64 chunk_offset)
3365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003366 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003367 struct btrfs_balance_args *bargs = NULL;
3368 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3369
3370 /* type filter */
3371 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3372 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3373 return 0;
3374 }
3375
3376 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3377 bargs = &bctl->data;
3378 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3379 bargs = &bctl->sys;
3380 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3381 bargs = &bctl->meta;
3382
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003383 /* profiles filter */
3384 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3385 chunk_profiles_filter(chunk_type, bargs)) {
3386 return 0;
3387 }
3388
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003389 /* usage filter */
3390 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003391 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003392 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003393 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003395 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003396 }
3397
Ilya Dryomov409d4042012-01-16 22:04:47 +02003398 /* devid filter */
3399 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3400 chunk_devid_filter(leaf, chunk, bargs)) {
3401 return 0;
3402 }
3403
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003404 /* drange filter, makes sense only with devid filter */
3405 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003406 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003407 return 0;
3408 }
3409
Ilya Dryomovea671762012-01-16 22:04:48 +02003410 /* vrange filter */
3411 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3412 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3413 return 0;
3414 }
3415
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003416 /* stripes filter */
3417 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3418 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3419 return 0;
3420 }
3421
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003422 /* soft profile changing mode */
3423 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3424 chunk_soft_convert_filter(chunk_type, bargs)) {
3425 return 0;
3426 }
3427
David Sterba7d824b62014-05-07 17:37:51 +02003428 /*
3429 * limited by count, must be the last filter
3430 */
3431 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3432 if (bargs->limit == 0)
3433 return 0;
3434 else
3435 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003436 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3437 /*
3438 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003439 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003440 * about the count of all chunks that satisfy the filters.
3441 */
3442 if (bargs->limit_max == 0)
3443 return 0;
3444 else
3445 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003446 }
3447
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003448 return 1;
3449}
3450
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003451static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003452{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003453 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003454 struct btrfs_root *chunk_root = fs_info->chunk_root;
3455 struct btrfs_root *dev_root = fs_info->dev_root;
3456 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003457 struct btrfs_device *device;
3458 u64 old_size;
3459 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003460 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003461 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003462 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003463 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003464 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003465 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003466 struct extent_buffer *leaf;
3467 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003468 int ret;
3469 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003470 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003471 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003472 u64 limit_data = bctl->data.limit;
3473 u64 limit_meta = bctl->meta.limit;
3474 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003475 u32 count_data = 0;
3476 u32 count_meta = 0;
3477 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003478 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003479
Chris Masonec44a352008-04-28 15:29:52 -04003480 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003481 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003482 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003483 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003484 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003485 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003486 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003487 btrfs_device_get_total_bytes(device) -
3488 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003489 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003490 continue;
3491
3492 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003493 if (ret == -ENOSPC)
3494 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003495 if (ret) {
3496 /* btrfs_shrink_device never returns ret > 0 */
3497 WARN_ON(ret > 0);
3498 goto error;
3499 }
Chris Masonec44a352008-04-28 15:29:52 -04003500
Yan, Zhenga22285a2010-05-16 10:48:46 -04003501 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003502 if (IS_ERR(trans)) {
3503 ret = PTR_ERR(trans);
3504 btrfs_info_in_rcu(fs_info,
3505 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3506 rcu_str_deref(device->name), ret,
3507 old_size, old_size - size_to_free);
3508 goto error;
3509 }
Chris Masonec44a352008-04-28 15:29:52 -04003510
3511 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003512 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003513 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003514 /* btrfs_grow_device never returns ret > 0 */
3515 WARN_ON(ret > 0);
3516 btrfs_info_in_rcu(fs_info,
3517 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3518 rcu_str_deref(device->name), ret,
3519 old_size, old_size - size_to_free);
3520 goto error;
3521 }
Chris Masonec44a352008-04-28 15:29:52 -04003522
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003523 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003524 }
3525
3526 /* step two, relocate all the chunks */
3527 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003528 if (!path) {
3529 ret = -ENOMEM;
3530 goto error;
3531 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003532
3533 /* zero out stat counters */
3534 spin_lock(&fs_info->balance_lock);
3535 memset(&bctl->stat, 0, sizeof(bctl->stat));
3536 spin_unlock(&fs_info->balance_lock);
3537again:
David Sterba7d824b62014-05-07 17:37:51 +02003538 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003539 /*
3540 * The single value limit and min/max limits use the same bytes
3541 * in the
3542 */
David Sterba7d824b62014-05-07 17:37:51 +02003543 bctl->data.limit = limit_data;
3544 bctl->meta.limit = limit_meta;
3545 bctl->sys.limit = limit_sys;
3546 }
Chris Masonec44a352008-04-28 15:29:52 -04003547 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3548 key.offset = (u64)-1;
3549 key.type = BTRFS_CHUNK_ITEM_KEY;
3550
Chris Masond3977122009-01-05 21:25:51 -05003551 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003552 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003553 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003554 ret = -ECANCELED;
3555 goto error;
3556 }
3557
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003558 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003559 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003560 if (ret < 0) {
3561 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003562 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003563 }
Chris Masonec44a352008-04-28 15:29:52 -04003564
3565 /*
3566 * this shouldn't happen, it means the last relocate
3567 * failed
3568 */
3569 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003570 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003571
3572 ret = btrfs_previous_item(chunk_root, path, 0,
3573 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003574 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003575 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003576 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003577 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003578 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003579
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003580 leaf = path->nodes[0];
3581 slot = path->slots[0];
3582 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3583
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003584 if (found_key.objectid != key.objectid) {
3585 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003586 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003587 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003588
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003589 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003590 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003591
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003592 if (!counting) {
3593 spin_lock(&fs_info->balance_lock);
3594 bctl->stat.considered++;
3595 spin_unlock(&fs_info->balance_lock);
3596 }
3597
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003598 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003599 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003600
David Sterbab3b4aa72011-04-21 01:20:15 +02003601 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003602 if (!ret) {
3603 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003604 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003605 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003606
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003607 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003608 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003609 spin_lock(&fs_info->balance_lock);
3610 bctl->stat.expected++;
3611 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003612
3613 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3614 count_data++;
3615 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3616 count_sys++;
3617 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3618 count_meta++;
3619
3620 goto loop;
3621 }
3622
3623 /*
3624 * Apply limit_min filter, no need to check if the LIMITS
3625 * filter is used, limit_min is 0 by default
3626 */
3627 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3628 count_data < bctl->data.limit_min)
3629 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3630 count_meta < bctl->meta.limit_min)
3631 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3632 count_sys < bctl->sys.limit_min)) {
3633 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003634 goto loop;
3635 }
3636
Liu Boa6f93c72017-11-15 16:28:11 -07003637 if (!chunk_reserved) {
3638 /*
3639 * We may be relocating the only data chunk we have,
3640 * which could potentially end up with losing data's
3641 * raid profile, so lets allocate an empty one in
3642 * advance.
3643 */
3644 ret = btrfs_may_alloc_data_chunk(fs_info,
3645 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003646 if (ret < 0) {
3647 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3648 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003649 } else if (ret == 1) {
3650 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003651 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003652 }
3653
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003654 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003655 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003656 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003657 enospc_errors++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07003658 } else if (ret == -ETXTBSY) {
3659 btrfs_info(fs_info,
3660 "skipping relocation of block group %llu due to active swapfile",
3661 found_key.offset);
3662 ret = 0;
3663 } else if (ret) {
3664 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003665 } else {
3666 spin_lock(&fs_info->balance_lock);
3667 bctl->stat.completed++;
3668 spin_unlock(&fs_info->balance_lock);
3669 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003670loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003671 if (found_key.offset == 0)
3672 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003673 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003674 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003675
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003676 if (counting) {
3677 btrfs_release_path(path);
3678 counting = false;
3679 goto again;
3680 }
Chris Masonec44a352008-04-28 15:29:52 -04003681error:
3682 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003683 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003684 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003685 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003686 if (!ret)
3687 ret = -ENOSPC;
3688 }
3689
Chris Masonec44a352008-04-28 15:29:52 -04003690 return ret;
3691}
3692
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003693/**
3694 * alloc_profile_is_valid - see if a given profile is valid and reduced
3695 * @flags: profile to validate
3696 * @extended: if true @flags is treated as an extended profile
3697 */
3698static int alloc_profile_is_valid(u64 flags, int extended)
3699{
3700 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3701 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3702
3703 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3704
3705 /* 1) check that all other bits are zeroed */
3706 if (flags & ~mask)
3707 return 0;
3708
3709 /* 2) see if profile is reduced */
3710 if (flags == 0)
3711 return !extended; /* "0" is valid for usual profiles */
3712
3713 /* true if exactly one bit set */
David Sterba818255f2018-09-21 14:26:34 +02003714 return is_power_of_2(flags);
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003715}
3716
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003717static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3718{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003719 /* cancel requested || normal exit path */
3720 return atomic_read(&fs_info->balance_cancel_req) ||
3721 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3722 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003723}
3724
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003725/* Non-zero return value signifies invalidity */
3726static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3727 u64 allowed)
3728{
3729 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3730 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3731 (bctl_arg->target & ~allowed)));
3732}
3733
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003734/*
David Sterbadccdb072018-03-21 00:20:05 +01003735 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003736 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003737int btrfs_balance(struct btrfs_fs_info *fs_info,
3738 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003739 struct btrfs_ioctl_balance_args *bargs)
3740{
Adam Borowski14506122017-03-07 23:34:44 +01003741 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003742 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003743 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003744 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003745 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003746 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003747
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003748 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003749 atomic_read(&fs_info->balance_pause_req) ||
3750 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003751 ret = -EINVAL;
3752 goto out;
3753 }
3754
Ilya Dryomove4837f82012-03-27 17:09:17 +03003755 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3756 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3757 mixed = 1;
3758
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003759 /*
3760 * In case of mixed groups both data and meta should be picked,
3761 * and identical options should be given for both of them.
3762 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003763 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3764 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003765 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3766 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3767 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003768 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003769 "balance: mixed groups data and metadata options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003770 ret = -EINVAL;
3771 goto out;
3772 }
3773 }
3774
Anand Jain1da73962018-08-10 13:53:21 +08003775 num_devices = btrfs_num_devices(fs_info);
3776
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003777 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3778 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003779 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003780 if (num_devices > 2)
3781 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3782 if (num_devices > 3)
3783 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3784 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003785 if (validate_convert_profile(&bctl->data, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003786 int index = btrfs_bg_flags_to_raid_index(bctl->data.target);
3787
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003788 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003789 "balance: invalid convert data profile %s",
3790 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003791 ret = -EINVAL;
3792 goto out;
3793 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003794 if (validate_convert_profile(&bctl->meta, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003795 int index = btrfs_bg_flags_to_raid_index(bctl->meta.target);
3796
Frank Holtonefe120a2013-12-20 11:37:06 -05003797 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003798 "balance: invalid convert metadata profile %s",
3799 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003800 ret = -EINVAL;
3801 goto out;
3802 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003803 if (validate_convert_profile(&bctl->sys, allowed)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003804 int index = btrfs_bg_flags_to_raid_index(bctl->sys.target);
3805
Frank Holtonefe120a2013-12-20 11:37:06 -05003806 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003807 "balance: invalid convert system profile %s",
3808 get_raid_name(index));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003809 ret = -EINVAL;
3810 goto out;
3811 }
3812
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003813 /* allow to reduce meta or sys integrity only if force set */
3814 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003815 BTRFS_BLOCK_GROUP_RAID10 |
3816 BTRFS_BLOCK_GROUP_RAID5 |
3817 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003818 do {
3819 seq = read_seqbegin(&fs_info->profiles_lock);
3820
3821 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3822 (fs_info->avail_system_alloc_bits & allowed) &&
3823 !(bctl->sys.target & allowed)) ||
3824 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3825 (fs_info->avail_metadata_alloc_bits & allowed) &&
3826 !(bctl->meta.target & allowed))) {
3827 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003828 btrfs_info(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003829 "balance: force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003830 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003831 btrfs_err(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003832 "balance: reduces metadata integrity, use --force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003833 ret = -EINVAL;
3834 goto out;
3835 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003836 }
Miao Xiede98ced2013-01-29 10:13:12 +00003837 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003838
Adam Borowski14506122017-03-07 23:34:44 +01003839 /* if we're not converting, the target field is uninitialized */
3840 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3841 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3842 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3843 bctl->data.target : fs_info->avail_data_alloc_bits;
3844 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3845 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003846 int meta_index = btrfs_bg_flags_to_raid_index(meta_target);
3847 int data_index = btrfs_bg_flags_to_raid_index(data_target);
3848
Sam Tygieree592d02016-01-06 08:46:12 +00003849 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08003850 "balance: metadata profile %s has lower redundancy than data profile %s",
3851 get_raid_name(meta_index), get_raid_name(data_index));
Sam Tygieree592d02016-01-06 08:46:12 +00003852 }
3853
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003854 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003855 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003856 goto out;
3857
Ilya Dryomov59641012012-01-16 22:04:48 +02003858 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3859 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01003860 BUG_ON(fs_info->balance_ctl);
3861 spin_lock(&fs_info->balance_lock);
3862 fs_info->balance_ctl = bctl;
3863 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02003864 } else {
3865 BUG_ON(ret != -EEXIST);
3866 spin_lock(&fs_info->balance_lock);
3867 update_balance_args(bctl);
3868 spin_unlock(&fs_info->balance_lock);
3869 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003870
David Sterba3009a622018-03-21 01:31:04 +01003871 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
3872 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003873 mutex_unlock(&fs_info->balance_mutex);
3874
3875 ret = __btrfs_balance(fs_info);
3876
3877 mutex_lock(&fs_info->balance_mutex);
David Sterba3009a622018-03-21 01:31:04 +01003878 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003879
3880 if (bargs) {
3881 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01003882 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003883 }
3884
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003885 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3886 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01003887 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003888 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003889 }
3890
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003891 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003892
3893 return ret;
3894out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003895 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01003896 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003897 else
Ilya Dryomov59641012012-01-16 22:04:48 +02003898 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01003899 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3900
Ilya Dryomov59641012012-01-16 22:04:48 +02003901 return ret;
3902}
3903
3904static int balance_kthread(void *data)
3905{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003906 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003907 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003908
Ilya Dryomov59641012012-01-16 22:04:48 +02003909 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003910 if (fs_info->balance_ctl) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003911 btrfs_info(fs_info, "balance: resuming");
David Sterba6fcf6e22018-05-07 17:44:03 +02003912 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003913 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003914 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003915
Ilya Dryomov59641012012-01-16 22:04:48 +02003916 return ret;
3917}
3918
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003919int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3920{
3921 struct task_struct *tsk;
3922
David Sterba1354e1a2018-03-21 02:29:13 +01003923 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003924 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01003925 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003926 return 0;
3927 }
David Sterba1354e1a2018-03-21 02:29:13 +01003928 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003929
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003930 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Anand Jain6dac13f2018-05-16 10:51:26 +08003931 btrfs_info(fs_info, "balance: resume skipped");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003932 return 0;
3933 }
3934
Anand Jain02ee6542018-05-17 15:16:51 +08003935 /*
3936 * A ro->rw remount sequence should continue with the paused balance
3937 * regardless of who pauses it, system or the user as of now, so set
3938 * the resume flag.
3939 */
3940 spin_lock(&fs_info->balance_lock);
3941 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
3942 spin_unlock(&fs_info->balance_lock);
3943
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003944 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303945 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003946}
3947
Ilya Dryomov68310a52012-06-22 12:24:12 -06003948int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003949{
Ilya Dryomov59641012012-01-16 22:04:48 +02003950 struct btrfs_balance_control *bctl;
3951 struct btrfs_balance_item *item;
3952 struct btrfs_disk_balance_args disk_bargs;
3953 struct btrfs_path *path;
3954 struct extent_buffer *leaf;
3955 struct btrfs_key key;
3956 int ret;
3957
3958 path = btrfs_alloc_path();
3959 if (!path)
3960 return -ENOMEM;
3961
Ilya Dryomov68310a52012-06-22 12:24:12 -06003962 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003963 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003964 key.offset = 0;
3965
3966 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3967 if (ret < 0)
3968 goto out;
3969 if (ret > 0) { /* ret = -ENOENT; */
3970 ret = 0;
3971 goto out;
3972 }
3973
Ilya Dryomov59641012012-01-16 22:04:48 +02003974 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3975 if (!bctl) {
3976 ret = -ENOMEM;
3977 goto out;
3978 }
3979
Ilya Dryomov59641012012-01-16 22:04:48 +02003980 leaf = path->nodes[0];
3981 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3982
Ilya Dryomov68310a52012-06-22 12:24:12 -06003983 bctl->flags = btrfs_balance_flags(leaf, item);
3984 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003985
3986 btrfs_balance_data(leaf, item, &disk_bargs);
3987 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3988 btrfs_balance_meta(leaf, item, &disk_bargs);
3989 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3990 btrfs_balance_sys(leaf, item, &disk_bargs);
3991 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3992
David Sterbaeee95e32018-03-20 20:07:58 +01003993 /*
3994 * This should never happen, as the paused balance state is recovered
3995 * during mount without any chance of other exclusive ops to collide.
3996 *
3997 * This gives the exclusive op status to balance and keeps in paused
3998 * state until user intervention (cancel or umount). If the ownership
3999 * cannot be assigned, show a message but do not fail. The balance
4000 * is in a paused state and must have fs_info::balance_ctl properly
4001 * set up.
4002 */
4003 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4004 btrfs_warn(fs_info,
Anand Jain6dac13f2018-05-16 10:51:26 +08004005 "balance: cannot set exclusive op status, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004006
Ilya Dryomov68310a52012-06-22 12:24:12 -06004007 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004008 BUG_ON(fs_info->balance_ctl);
4009 spin_lock(&fs_info->balance_lock);
4010 fs_info->balance_ctl = bctl;
4011 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004012 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004013out:
4014 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004015 return ret;
4016}
4017
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004018int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4019{
4020 int ret = 0;
4021
4022 mutex_lock(&fs_info->balance_mutex);
4023 if (!fs_info->balance_ctl) {
4024 mutex_unlock(&fs_info->balance_mutex);
4025 return -ENOTCONN;
4026 }
4027
David Sterba3009a622018-03-21 01:31:04 +01004028 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004029 atomic_inc(&fs_info->balance_pause_req);
4030 mutex_unlock(&fs_info->balance_mutex);
4031
4032 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004033 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004034
4035 mutex_lock(&fs_info->balance_mutex);
4036 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004037 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004038 atomic_dec(&fs_info->balance_pause_req);
4039 } else {
4040 ret = -ENOTCONN;
4041 }
4042
4043 mutex_unlock(&fs_info->balance_mutex);
4044 return ret;
4045}
4046
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004047int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4048{
4049 mutex_lock(&fs_info->balance_mutex);
4050 if (!fs_info->balance_ctl) {
4051 mutex_unlock(&fs_info->balance_mutex);
4052 return -ENOTCONN;
4053 }
4054
David Sterbacf7d20f2018-03-21 01:45:32 +01004055 /*
4056 * A paused balance with the item stored on disk can be resumed at
4057 * mount time if the mount is read-write. Otherwise it's still paused
4058 * and we must not allow cancelling as it deletes the item.
4059 */
4060 if (sb_rdonly(fs_info->sb)) {
4061 mutex_unlock(&fs_info->balance_mutex);
4062 return -EROFS;
4063 }
4064
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004065 atomic_inc(&fs_info->balance_cancel_req);
4066 /*
4067 * if we are running just wait and return, balance item is
4068 * deleted in btrfs_balance in this case
4069 */
David Sterba3009a622018-03-21 01:31:04 +01004070 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004071 mutex_unlock(&fs_info->balance_mutex);
4072 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004073 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004074 mutex_lock(&fs_info->balance_mutex);
4075 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004076 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004077 /*
4078 * Lock released to allow other waiters to continue, we'll
4079 * reexamine the status again.
4080 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004081 mutex_lock(&fs_info->balance_mutex);
4082
David Sterbaa17c95d2018-03-20 17:28:05 +01004083 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004084 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004085 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Anand Jain6dac13f2018-05-16 10:51:26 +08004086 btrfs_info(fs_info, "balance: canceled");
David Sterbaa17c95d2018-03-20 17:28:05 +01004087 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004088 }
4089
David Sterba3009a622018-03-21 01:31:04 +01004090 BUG_ON(fs_info->balance_ctl ||
4091 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004092 atomic_dec(&fs_info->balance_cancel_req);
4093 mutex_unlock(&fs_info->balance_mutex);
4094 return 0;
4095}
4096
Stefan Behrens803b2f52013-08-15 17:11:21 +02004097static int btrfs_uuid_scan_kthread(void *data)
4098{
4099 struct btrfs_fs_info *fs_info = data;
4100 struct btrfs_root *root = fs_info->tree_root;
4101 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004102 struct btrfs_path *path = NULL;
4103 int ret = 0;
4104 struct extent_buffer *eb;
4105 int slot;
4106 struct btrfs_root_item root_item;
4107 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004108 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004109
4110 path = btrfs_alloc_path();
4111 if (!path) {
4112 ret = -ENOMEM;
4113 goto out;
4114 }
4115
4116 key.objectid = 0;
4117 key.type = BTRFS_ROOT_ITEM_KEY;
4118 key.offset = 0;
4119
Stefan Behrens803b2f52013-08-15 17:11:21 +02004120 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004121 ret = btrfs_search_forward(root, &key, path,
4122 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004123 if (ret) {
4124 if (ret > 0)
4125 ret = 0;
4126 break;
4127 }
4128
4129 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4130 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4131 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4132 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4133 goto skip;
4134
4135 eb = path->nodes[0];
4136 slot = path->slots[0];
4137 item_size = btrfs_item_size_nr(eb, slot);
4138 if (item_size < sizeof(root_item))
4139 goto skip;
4140
Stefan Behrens803b2f52013-08-15 17:11:21 +02004141 read_extent_buffer(eb, &root_item,
4142 btrfs_item_ptr_offset(eb, slot),
4143 (int)sizeof(root_item));
4144 if (btrfs_root_refs(&root_item) == 0)
4145 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004146
4147 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4148 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4149 if (trans)
4150 goto update_tree;
4151
4152 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004153 /*
4154 * 1 - subvol uuid item
4155 * 1 - received_subvol uuid item
4156 */
4157 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4158 if (IS_ERR(trans)) {
4159 ret = PTR_ERR(trans);
4160 break;
4161 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004162 continue;
4163 } else {
4164 goto skip;
4165 }
4166update_tree:
4167 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004168 ret = btrfs_uuid_tree_add(trans, root_item.uuid,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004169 BTRFS_UUID_KEY_SUBVOL,
4170 key.objectid);
4171 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004172 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004173 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004174 break;
4175 }
4176 }
4177
4178 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Lu Fengqicdb345a2018-05-29 15:01:53 +08004179 ret = btrfs_uuid_tree_add(trans,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004180 root_item.received_uuid,
4181 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4182 key.objectid);
4183 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004184 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004185 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004186 break;
4187 }
4188 }
4189
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004190skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004191 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004192 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004193 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004194 if (ret)
4195 break;
4196 }
4197
Stefan Behrens803b2f52013-08-15 17:11:21 +02004198 btrfs_release_path(path);
4199 if (key.offset < (u64)-1) {
4200 key.offset++;
4201 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4202 key.offset = 0;
4203 key.type = BTRFS_ROOT_ITEM_KEY;
4204 } else if (key.objectid < (u64)-1) {
4205 key.offset = 0;
4206 key.type = BTRFS_ROOT_ITEM_KEY;
4207 key.objectid++;
4208 } else {
4209 break;
4210 }
4211 cond_resched();
4212 }
4213
4214out:
4215 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004216 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004217 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004218 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004219 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004220 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004221 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004222 up(&fs_info->uuid_tree_rescan_sem);
4223 return 0;
4224}
4225
Stefan Behrens70f80172013-08-15 17:11:23 +02004226/*
4227 * Callback for btrfs_uuid_tree_iterate().
4228 * returns:
4229 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004230 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004231 * > 0 if the check failed, which means the caller shall remove the entry.
4232 */
4233static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4234 u8 *uuid, u8 type, u64 subid)
4235{
4236 struct btrfs_key key;
4237 int ret = 0;
4238 struct btrfs_root *subvol_root;
4239
4240 if (type != BTRFS_UUID_KEY_SUBVOL &&
4241 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4242 goto out;
4243
4244 key.objectid = subid;
4245 key.type = BTRFS_ROOT_ITEM_KEY;
4246 key.offset = (u64)-1;
4247 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4248 if (IS_ERR(subvol_root)) {
4249 ret = PTR_ERR(subvol_root);
4250 if (ret == -ENOENT)
4251 ret = 1;
4252 goto out;
4253 }
4254
4255 switch (type) {
4256 case BTRFS_UUID_KEY_SUBVOL:
4257 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4258 ret = 1;
4259 break;
4260 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4261 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4262 BTRFS_UUID_SIZE))
4263 ret = 1;
4264 break;
4265 }
4266
4267out:
4268 return ret;
4269}
4270
4271static int btrfs_uuid_rescan_kthread(void *data)
4272{
4273 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4274 int ret;
4275
4276 /*
4277 * 1st step is to iterate through the existing UUID tree and
4278 * to delete all entries that contain outdated data.
4279 * 2nd step is to add all missing entries to the UUID tree.
4280 */
4281 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4282 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004283 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004284 up(&fs_info->uuid_tree_rescan_sem);
4285 return ret;
4286 }
4287 return btrfs_uuid_scan_kthread(data);
4288}
4289
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004290int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4291{
4292 struct btrfs_trans_handle *trans;
4293 struct btrfs_root *tree_root = fs_info->tree_root;
4294 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004295 struct task_struct *task;
4296 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004297
4298 /*
4299 * 1 - root node
4300 * 1 - root item
4301 */
4302 trans = btrfs_start_transaction(tree_root, 2);
4303 if (IS_ERR(trans))
4304 return PTR_ERR(trans);
4305
4306 uuid_root = btrfs_create_tree(trans, fs_info,
4307 BTRFS_UUID_TREE_OBJECTID);
4308 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004309 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004310 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004311 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004312 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004313 }
4314
4315 fs_info->uuid_root = uuid_root;
4316
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004317 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004318 if (ret)
4319 return ret;
4320
4321 down(&fs_info->uuid_tree_rescan_sem);
4322 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4323 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004324 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004325 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004326 up(&fs_info->uuid_tree_rescan_sem);
4327 return PTR_ERR(task);
4328 }
4329
4330 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004331}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004332
Stefan Behrens70f80172013-08-15 17:11:23 +02004333int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4334{
4335 struct task_struct *task;
4336
4337 down(&fs_info->uuid_tree_rescan_sem);
4338 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4339 if (IS_ERR(task)) {
4340 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004341 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004342 up(&fs_info->uuid_tree_rescan_sem);
4343 return PTR_ERR(task);
4344 }
4345
4346 return 0;
4347}
4348
Chris Mason8f18cf12008-04-25 16:53:30 -04004349/*
4350 * shrinking a device means finding all of the device extents past
4351 * the new size, and then following the back refs to the chunks.
4352 * The chunk relocation code actually frees the device extent
4353 */
4354int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4355{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004356 struct btrfs_fs_info *fs_info = device->fs_info;
4357 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004358 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004359 struct btrfs_dev_extent *dev_extent = NULL;
4360 struct btrfs_path *path;
4361 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004362 u64 chunk_offset;
4363 int ret;
4364 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004365 int failed = 0;
4366 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004367 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004368 struct extent_buffer *l;
4369 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004370 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004371 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004372 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004373 u64 diff;
4374
4375 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004376 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004377
Anand Jain401e29c2017-12-04 12:54:55 +08004378 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004379 return -EINVAL;
4380
Chris Mason8f18cf12008-04-25 16:53:30 -04004381 path = btrfs_alloc_path();
4382 if (!path)
4383 return -ENOMEM;
4384
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004385 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004386
David Sterba34441362016-10-04 19:34:27 +02004387 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004388
Miao Xie7cc8e582014-09-03 21:35:38 +08004389 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004390 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004391 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004392 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004393 }
David Sterba34441362016-10-04 19:34:27 +02004394 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004395
Josef Bacikba1bf482009-09-11 16:11:19 -04004396again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004397 key.objectid = device->devid;
4398 key.offset = (u64)-1;
4399 key.type = BTRFS_DEV_EXTENT_KEY;
4400
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004401 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004402 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004404 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004405 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004406 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004407 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004408
4409 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004410 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004411 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004412 if (ret < 0)
4413 goto done;
4414 if (ret) {
4415 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004416 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004417 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004418 }
4419
4420 l = path->nodes[0];
4421 slot = path->slots[0];
4422 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4423
Josef Bacikba1bf482009-09-11 16:11:19 -04004424 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004425 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004426 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004427 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004428 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004429
4430 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4431 length = btrfs_dev_extent_length(l, dev_extent);
4432
Josef Bacikba1bf482009-09-11 16:11:19 -04004433 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004434 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004435 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004436 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004437 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004438
Chris Mason8f18cf12008-04-25 16:53:30 -04004439 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004440 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004441
Liu Boa6f93c72017-11-15 16:28:11 -07004442 /*
4443 * We may be relocating the only data chunk we have,
4444 * which could potentially end up with losing data's
4445 * raid profile, so lets allocate an empty one in
4446 * advance.
4447 */
4448 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4449 if (ret < 0) {
4450 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4451 goto done;
4452 }
4453
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004454 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4455 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004456 if (ret == -ENOSPC) {
Josef Bacikba1bf482009-09-11 16:11:19 -04004457 failed++;
Omar Sandovaleede2bf2016-11-03 10:28:12 -07004458 } else if (ret) {
4459 if (ret == -ETXTBSY) {
4460 btrfs_warn(fs_info,
4461 "could not shrink block group %llu due to active swapfile",
4462 chunk_offset);
4463 }
4464 goto done;
4465 }
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004466 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004467
4468 if (failed && !retried) {
4469 failed = 0;
4470 retried = true;
4471 goto again;
4472 } else if (failed && retried) {
4473 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004474 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004475 }
4476
Chris Balld6397ba2009-04-27 07:29:03 -04004477 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004478 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004479 if (IS_ERR(trans)) {
4480 ret = PTR_ERR(trans);
4481 goto done;
4482 }
4483
David Sterba34441362016-10-04 19:34:27 +02004484 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004485
4486 /*
4487 * We checked in the above loop all device extents that were already in
4488 * the device tree. However before we have updated the device's
4489 * total_bytes to the new size, we might have had chunk allocations that
4490 * have not complete yet (new block groups attached to transaction
4491 * handles), and therefore their device extents were not yet in the
4492 * device tree and we missed them in the loop above. So if we have any
4493 * pending chunk using a device extent that overlaps the device range
4494 * that we can not use anymore, commit the current transaction and
4495 * repeat the search on the device tree - this way we guarantee we will
4496 * not have chunks using device extents that end beyond 'new_size'.
4497 */
4498 if (!checked_pending_chunks) {
4499 u64 start = new_size;
4500 u64 len = old_size - new_size;
4501
Jeff Mahoney499f3772015-06-15 09:41:17 -04004502 if (contains_pending_extent(trans->transaction, device,
4503 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004504 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004505 checked_pending_chunks = true;
4506 failed = 0;
4507 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004508 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004509 if (ret)
4510 goto done;
4511 goto again;
4512 }
4513 }
4514
Miao Xie7cc8e582014-09-03 21:35:38 +08004515 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004516 if (list_empty(&device->resized_list))
4517 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004518 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004519
Chris Balld6397ba2009-04-27 07:29:03 -04004520 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004521 btrfs_set_super_total_bytes(super_copy,
4522 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004523 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004524
4525 /* Now btrfs_update_device() will change the on-disk size. */
4526 ret = btrfs_update_device(trans, device);
Anand Jain801660b2018-08-06 18:12:37 +08004527 if (ret < 0) {
4528 btrfs_abort_transaction(trans, ret);
4529 btrfs_end_transaction(trans);
4530 } else {
4531 ret = btrfs_commit_transaction(trans);
4532 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004533done:
4534 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004535 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004536 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004537 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004538 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004539 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004540 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004541 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004542 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004543 return ret;
4544}
4545
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004546static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004547 struct btrfs_key *key,
4548 struct btrfs_chunk *chunk, int item_size)
4549{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004550 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004551 struct btrfs_disk_key disk_key;
4552 u32 array_size;
4553 u8 *ptr;
4554
David Sterba34441362016-10-04 19:34:27 +02004555 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004556 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004557 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004558 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004559 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004560 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004561 }
Chris Mason0b86a832008-03-24 15:01:56 -04004562
4563 ptr = super_copy->sys_chunk_array + array_size;
4564 btrfs_cpu_key_to_disk(&disk_key, key);
4565 memcpy(ptr, &disk_key, sizeof(disk_key));
4566 ptr += sizeof(disk_key);
4567 memcpy(ptr, chunk, item_size);
4568 item_size += sizeof(disk_key);
4569 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004570 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004571
Chris Mason0b86a832008-03-24 15:01:56 -04004572 return 0;
4573}
4574
Miao Xieb2117a32011-01-05 10:07:28 +00004575/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004576 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004577 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004578static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004579{
Arne Jansen73c5de02011-04-12 12:07:57 +02004580 const struct btrfs_device_info *di_a = a;
4581 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004582
Arne Jansen73c5de02011-04-12 12:07:57 +02004583 if (di_a->max_avail > di_b->max_avail)
4584 return -1;
4585 if (di_a->max_avail < di_b->max_avail)
4586 return 1;
4587 if (di_a->total_avail > di_b->total_avail)
4588 return -1;
4589 if (di_a->total_avail < di_b->total_avail)
4590 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004591 return 0;
4592}
4593
David Woodhouse53b381b2013-01-29 18:40:14 -05004594static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4595{
Zhao Leiffe2d202015-01-20 15:11:44 +08004596 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004597 return;
4598
Miao Xieceda0862013-04-11 10:30:16 +00004599 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004600}
4601
Qu Wenruo062d4d12018-01-30 18:20:46 +08004602#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004603 - sizeof(struct btrfs_chunk)) \
4604 / sizeof(struct btrfs_stripe) + 1)
4605
4606#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4607 - 2 * sizeof(struct btrfs_disk_key) \
4608 - 2 * sizeof(struct btrfs_chunk)) \
4609 / sizeof(struct btrfs_stripe) + 1)
4610
Miao Xieb2117a32011-01-05 10:07:28 +00004611static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004612 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004613{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004614 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004615 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004616 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004617 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004618 struct extent_map_tree *em_tree;
4619 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004620 struct btrfs_device_info *devices_info = NULL;
4621 u64 total_avail;
4622 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004623 int data_stripes; /* number of stripes that count for
4624 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004625 int sub_stripes; /* sub_stripes info for map */
4626 int dev_stripes; /* stripes per dev */
4627 int devs_max; /* max devs to use */
4628 int devs_min; /* min devs needed */
4629 int devs_increment; /* ndevs has to be a multiple of this */
4630 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004631 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004632 u64 max_stripe_size;
4633 u64 max_chunk_size;
4634 u64 stripe_size;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004635 u64 chunk_size;
Arne Jansen73c5de02011-04-12 12:07:57 +02004636 int ndevs;
4637 int i;
4638 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004639 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004640
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004641 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004642
Qu Wenruo4117f202018-01-22 13:50:54 +08004643 if (list_empty(&fs_devices->alloc_list)) {
4644 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4645 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004646 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004647 }
Miao Xieb2117a32011-01-05 10:07:28 +00004648
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004649 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004650
Liu Bo31e50222012-11-21 14:18:10 +00004651 sub_stripes = btrfs_raid_array[index].sub_stripes;
4652 dev_stripes = btrfs_raid_array[index].dev_stripes;
4653 devs_max = btrfs_raid_array[index].devs_max;
4654 devs_min = btrfs_raid_array[index].devs_min;
4655 devs_increment = btrfs_raid_array[index].devs_increment;
4656 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004657
4658 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004659 max_stripe_size = SZ_1G;
Qu Wenruofce466e2018-07-03 17:10:05 +08004660 max_chunk_size = BTRFS_MAX_DATA_CHUNK_SIZE;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004661 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004662 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004663 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004664 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004665 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4666 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004667 else
Byongho Leeee221842015-12-15 01:42:10 +09004668 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004669 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004670 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004671 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004672 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004673 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004674 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004675 if (!devs_max)
4676 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004677 } else {
David Sterba351fd352014-05-15 16:48:20 +02004678 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004679 type);
4680 BUG_ON(1);
4681 }
4682
4683 /* we don't want a chunk larger than 10% of writeable space */
4684 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4685 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004686
David Sterba31e818f2015-02-20 18:00:26 +01004687 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004688 GFP_NOFS);
4689 if (!devices_info)
4690 return -ENOMEM;
4691
Arne Jansen73c5de02011-04-12 12:07:57 +02004692 /*
4693 * in the first pass through the devices list, we gather information
4694 * about the available holes on each device.
4695 */
4696 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004697 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004698 u64 max_avail;
4699 u64 dev_offset;
4700
Anand Jainebbede42017-12-04 12:54:52 +08004701 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004702 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004703 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004704 continue;
4705 }
4706
Anand Jaine12c9622017-12-04 12:54:53 +08004707 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4708 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004709 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004710 continue;
4711
4712 if (device->total_bytes > device->bytes_used)
4713 total_avail = device->total_bytes - device->bytes_used;
4714 else
4715 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004716
4717 /* If there is no space on this device, skip it. */
4718 if (total_avail == 0)
4719 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004720
Josef Bacik6df9a952013-06-27 13:22:46 -04004721 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004722 max_stripe_size * dev_stripes,
4723 &dev_offset, &max_avail);
4724 if (ret && ret != -ENOSPC)
4725 goto error;
4726
4727 if (ret == 0)
4728 max_avail = max_stripe_size * dev_stripes;
4729
Qu Wenruo4117f202018-01-22 13:50:54 +08004730 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4731 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4732 btrfs_debug(info,
4733 "%s: devid %llu has no free space, have=%llu want=%u",
4734 __func__, device->devid, max_avail,
4735 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004736 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004737 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004738
Eric Sandeen063d0062013-01-31 00:55:01 +00004739 if (ndevs == fs_devices->rw_devices) {
4740 WARN(1, "%s: found more than %llu devices\n",
4741 __func__, fs_devices->rw_devices);
4742 break;
4743 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004744 devices_info[ndevs].dev_offset = dev_offset;
4745 devices_info[ndevs].max_avail = max_avail;
4746 devices_info[ndevs].total_avail = total_avail;
4747 devices_info[ndevs].dev = device;
4748 ++ndevs;
4749 }
4750
4751 /*
4752 * now sort the devices by hole size / available space
4753 */
4754 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4755 btrfs_cmp_device_info, NULL);
4756
4757 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004758 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004759
Qu Wenruoba89b802018-01-31 13:56:15 +08004760 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004761 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004762 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4763 btrfs_debug(info,
4764 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004765 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004766 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004767 goto error;
4768 }
4769
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004770 ndevs = min(ndevs, devs_max);
4771
Arne Jansen73c5de02011-04-12 12:07:57 +02004772 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004773 * The primary goal is to maximize the number of stripes, so use as
4774 * many devices as possible, even if the stripes are not maximum sized.
4775 *
4776 * The DUP profile stores more than one stripe per device, the
4777 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004778 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004779 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004780 num_stripes = ndevs * dev_stripes;
4781
David Woodhouse53b381b2013-01-29 18:40:14 -05004782 /*
4783 * this will have to be fixed for RAID1 and RAID10 over
4784 * more drives
4785 */
4786 data_stripes = num_stripes / ncopies;
4787
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004788 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004789 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004790
4791 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004792 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004793
4794 /*
4795 * Use the number of data stripes to figure out how big this chunk
4796 * is really going to be in terms of logical address space,
4797 * and compare that answer with the max chunk size
4798 */
4799 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004800 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004801
4802 /* bump the answer up to a 16MB boundary */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004803 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004804
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004805 /*
4806 * But don't go higher than the limits we found while searching
4807 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004808 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004809 stripe_size = min(devices_info[ndevs - 1].max_avail,
4810 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004811 }
4812
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004813 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004814 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004815
Miao Xieb2117a32011-01-05 10:07:28 +00004816 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4817 if (!map) {
4818 ret = -ENOMEM;
4819 goto error;
4820 }
4821 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004822
Arne Jansen73c5de02011-04-12 12:07:57 +02004823 for (i = 0; i < ndevs; ++i) {
4824 for (j = 0; j < dev_stripes; ++j) {
4825 int s = i * dev_stripes + j;
4826 map->stripes[s].dev = devices_info[i].dev;
4827 map->stripes[s].physical = devices_info[i].dev_offset +
4828 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004829 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004830 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004831 map->stripe_len = BTRFS_STRIPE_LEN;
4832 map->io_align = BTRFS_STRIPE_LEN;
4833 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004834 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004835 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004836
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004837 chunk_size = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004838
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004839 trace_btrfs_chunk_alloc(info, map, start, chunk_size);
liubo1abe9b82011-03-24 11:18:59 +00004840
David Sterba172ddd62011-04-21 00:48:27 +02004841 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004842 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004843 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004844 ret = -ENOMEM;
4845 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004846 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004847 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004848 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004849 em->start = start;
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004850 em->len = chunk_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004851 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004852 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004853 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004854
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004855 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004856 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004857 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004858 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004859 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004860 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004861 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004862 }
Yan Zheng2b820322008-11-17 21:11:30 -05004863
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004864 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4865 refcount_inc(&em->refs);
4866 write_unlock(&em_tree->lock);
4867
Hans van Kranenburg23f0ff12018-10-04 23:24:39 +02004868 ret = btrfs_make_block_group(trans, 0, type, start, chunk_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004869 if (ret)
4870 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004871
Hans van Kranenburg2f29df42018-10-04 23:24:38 +02004872 for (i = 0; i < map->num_stripes; i++)
4873 btrfs_device_set_bytes_used(map->stripes[i].dev,
4874 map->stripes[i].dev->bytes_used + stripe_size);
Miao Xie43530c42014-09-03 21:35:36 +08004875
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004876 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004877
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004878 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004880
Miao Xieb2117a32011-01-05 10:07:28 +00004881 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004882 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004883
Josef Bacik6df9a952013-06-27 13:22:46 -04004884error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004885 write_lock(&em_tree->lock);
4886 remove_extent_mapping(em_tree, em);
4887 write_unlock(&em_tree->lock);
4888
4889 /* One for our allocation */
4890 free_extent_map(em);
4891 /* One for the tree reference */
4892 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004893 /* One for the pending_chunks list reference */
4894 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004895error:
Miao Xieb2117a32011-01-05 10:07:28 +00004896 kfree(devices_info);
4897 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004898}
4899
Josef Bacik6df9a952013-06-27 13:22:46 -04004900int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Nikolay Borisov97aff912018-07-20 19:37:53 +03004901 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004902{
Nikolay Borisov97aff912018-07-20 19:37:53 +03004903 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004904 struct btrfs_root *extent_root = fs_info->extent_root;
4905 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004906 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004907 struct btrfs_device *device;
4908 struct btrfs_chunk *chunk;
4909 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004910 struct extent_map *em;
4911 struct map_lookup *map;
4912 size_t item_size;
4913 u64 dev_offset;
4914 u64 stripe_size;
4915 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004916 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004917
Omar Sandoval60ca8422018-05-16 16:34:31 -07004918 em = btrfs_get_chunk_map(fs_info, chunk_offset, chunk_size);
Liu Bo592d92e2017-03-14 13:33:55 -07004919 if (IS_ERR(em))
4920 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004921
Jeff Mahoney95617d62015-06-03 10:55:48 -04004922 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004923 item_size = btrfs_chunk_item_size(map->num_stripes);
4924 stripe_size = em->orig_block_len;
4925
4926 chunk = kzalloc(item_size, GFP_NOFS);
4927 if (!chunk) {
4928 ret = -ENOMEM;
4929 goto out;
4930 }
4931
Filipe Manana50460e32015-11-20 10:42:47 +00004932 /*
4933 * Take the device list mutex to prevent races with the final phase of
4934 * a device replace operation that replaces the device object associated
4935 * with the map's stripes, because the device object's id can change
4936 * at any time during that final phase of the device replace operation
4937 * (dev-replace.c:btrfs_dev_replace_finishing()).
4938 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004939 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004940 for (i = 0; i < map->num_stripes; i++) {
4941 device = map->stripes[i].dev;
4942 dev_offset = map->stripes[i].physical;
4943
Yan Zheng2b820322008-11-17 21:11:30 -05004944 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004945 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004946 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004947 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4948 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004949 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004950 break;
4951 }
4952 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004953 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004954 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004955 }
4956
Yan Zheng2b820322008-11-17 21:11:30 -05004957 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004958 for (i = 0; i < map->num_stripes; i++) {
4959 device = map->stripes[i].dev;
4960 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004961
4962 btrfs_set_stack_stripe_devid(stripe, device->devid);
4963 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4964 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4965 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004966 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004967 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004968
4969 btrfs_set_stack_chunk_length(chunk, chunk_size);
4970 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4971 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4972 btrfs_set_stack_chunk_type(chunk, map->type);
4973 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4974 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4975 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004976 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05004977 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4978
4979 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4980 key.type = BTRFS_CHUNK_ITEM_KEY;
4981 key.offset = chunk_offset;
4982
4983 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004984 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4985 /*
4986 * TODO: Cleanup of inserted chunk root in case of
4987 * failure.
4988 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004989 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004990 }
liubo1abe9b82011-03-24 11:18:59 +00004991
Josef Bacik6df9a952013-06-27 13:22:46 -04004992out:
Yan Zheng2b820322008-11-17 21:11:30 -05004993 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004994 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004995 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004996}
4997
4998/*
4999 * Chunk allocation falls into two parts. The first part does works
5000 * that make the new allocated chunk useable, but not do any operation
5001 * that modifies the chunk tree. The second part does the works that
5002 * require modifying the chunk tree. This division is important for the
5003 * bootstrap process of adding storage to a seed btrfs.
5004 */
Nikolay Borisovc216b202018-06-20 15:49:06 +03005005int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005006{
5007 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005008
Nikolay Borisovc216b202018-06-20 15:49:06 +03005009 lockdep_assert_held(&trans->fs_info->chunk_mutex);
5010 chunk_offset = find_next_chunk(trans->fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005011 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005012}
5013
Chris Masond3977122009-01-05 21:25:51 -05005014static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005015 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005016{
5017 u64 chunk_offset;
5018 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005019 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005020 int ret;
5021
Josef Bacik6df9a952013-06-27 13:22:46 -04005022 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005023 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005024 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005025 if (ret)
5026 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005027
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005029 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005030 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005031 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005032}
5033
Miao Xied20983b2014-07-03 18:22:13 +08005034static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5035{
5036 int max_errors;
5037
5038 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5039 BTRFS_BLOCK_GROUP_RAID10 |
5040 BTRFS_BLOCK_GROUP_RAID5 |
5041 BTRFS_BLOCK_GROUP_DUP)) {
5042 max_errors = 1;
5043 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5044 max_errors = 2;
5045 } else {
5046 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005047 }
5048
Miao Xied20983b2014-07-03 18:22:13 +08005049 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005050}
5051
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005052int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005053{
5054 struct extent_map *em;
5055 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005056 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005057 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005058 int i;
5059
Omar Sandoval60ca8422018-05-16 16:34:31 -07005060 em = btrfs_get_chunk_map(fs_info, chunk_offset, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005061 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005062 return 1;
5063
Jeff Mahoney95617d62015-06-03 10:55:48 -04005064 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005065 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005066 if (test_bit(BTRFS_DEV_STATE_MISSING,
5067 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005068 miss_ndevs++;
5069 continue;
5070 }
Anand Jainebbede42017-12-04 12:54:52 +08005071 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5072 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005073 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005074 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005075 }
5076 }
Miao Xied20983b2014-07-03 18:22:13 +08005077
5078 /*
5079 * If the number of missing devices is larger than max errors,
5080 * we can not write the data into that chunk successfully, so
5081 * set it readonly.
5082 */
5083 if (miss_ndevs > btrfs_chunk_max_errors(map))
5084 readonly = 1;
5085end:
Yan Zheng2b820322008-11-17 21:11:30 -05005086 free_extent_map(em);
5087 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005088}
5089
5090void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5091{
David Sterbaa8067e02011-04-21 00:34:43 +02005092 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005093}
5094
5095void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5096{
5097 struct extent_map *em;
5098
Chris Masond3977122009-01-05 21:25:51 -05005099 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005100 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005101 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5102 if (em)
5103 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005104 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005105 if (!em)
5106 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005107 /* once for us */
5108 free_extent_map(em);
5109 /* once for the tree */
5110 free_extent_map(em);
5111 }
5112}
5113
Stefan Behrens5d964052012-11-05 14:59:07 +01005114int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005115{
5116 struct extent_map *em;
5117 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005118 int ret;
5119
Omar Sandoval60ca8422018-05-16 16:34:31 -07005120 em = btrfs_get_chunk_map(fs_info, logical, len);
Liu Bo592d92e2017-03-14 13:33:55 -07005121 if (IS_ERR(em))
5122 /*
5123 * We could return errors for these cases, but that could get
5124 * ugly and we'd probably do the same thing which is just not do
5125 * anything else and exit, so return 1 so the callers don't try
5126 * to use other copies.
5127 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005128 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005129
Jeff Mahoney95617d62015-06-03 10:55:48 -04005130 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005131 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5132 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005133 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5134 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005135 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5136 ret = 2;
5137 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005138 /*
5139 * There could be two corrupted data stripes, we need
5140 * to loop retry in order to rebuild the correct data.
Nikolay Borisove7e02092018-06-20 15:48:55 +03005141 *
Liu Bo8810f752018-01-02 13:36:41 -07005142 * Fail a stripe at a time on every retry except the
5143 * stripe under reconstruction.
5144 */
5145 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005146 else
5147 ret = 1;
5148 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005149
David Sterba7e79cb82018-03-24 02:11:38 +01005150 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005151 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5152 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005153 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005154 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005155
Chris Masonf1885912008-04-09 16:28:12 -04005156 return ret;
5157}
5158
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005159unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005160 u64 logical)
5161{
5162 struct extent_map *em;
5163 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005164 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005165
Omar Sandoval60ca8422018-05-16 16:34:31 -07005166 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005167
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005168 if (!WARN_ON(IS_ERR(em))) {
5169 map = em->map_lookup;
5170 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5171 len = map->stripe_len * nr_data_stripes(map);
5172 free_extent_map(em);
5173 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005174 return len;
5175}
5176
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005177int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005178{
5179 struct extent_map *em;
5180 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005181 int ret = 0;
5182
Omar Sandoval60ca8422018-05-16 16:34:31 -07005183 em = btrfs_get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005184
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005185 if(!WARN_ON(IS_ERR(em))) {
5186 map = em->map_lookup;
5187 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5188 ret = 1;
5189 free_extent_map(em);
5190 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005191 return ret;
5192}
5193
Stefan Behrens30d98612012-11-06 14:52:18 +01005194static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005195 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005196 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005197{
5198 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005199 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005200 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005201 int tolerance;
5202 struct btrfs_device *srcdev;
5203
Anand Jain99f92a72018-03-14 16:29:12 +08005204 ASSERT((map->type &
5205 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5206
5207 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5208 num_stripes = map->sub_stripes;
5209 else
5210 num_stripes = map->num_stripes;
5211
Anand Jain8ba0ae72018-03-14 16:29:13 +08005212 preferred_mirror = first + current->pid % num_stripes;
5213
Stefan Behrens30d98612012-11-06 14:52:18 +01005214 if (dev_replace_is_ongoing &&
5215 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5216 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5217 srcdev = fs_info->dev_replace.srcdev;
5218 else
5219 srcdev = NULL;
5220
5221 /*
5222 * try to avoid the drive that is the source drive for a
5223 * dev-replace procedure, only choose it if no other non-missing
5224 * mirror is available
5225 */
5226 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005227 if (map->stripes[preferred_mirror].dev->bdev &&
5228 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5229 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005230 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005231 if (map->stripes[i].dev->bdev &&
5232 (tolerance || map->stripes[i].dev != srcdev))
5233 return i;
5234 }
Chris Masondfe25022008-05-13 13:46:40 -04005235 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005236
Chris Masondfe25022008-05-13 13:46:40 -04005237 /* we couldn't find one that doesn't fail. Just return something
5238 * and the io error handling code will clean up eventually
5239 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005240 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005241}
5242
David Woodhouse53b381b2013-01-29 18:40:14 -05005243static inline int parity_smaller(u64 a, u64 b)
5244{
5245 return a > b;
5246}
5247
5248/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005249static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005250{
5251 struct btrfs_bio_stripe s;
5252 int i;
5253 u64 l;
5254 int again = 1;
5255
5256 while (again) {
5257 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005258 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005259 if (parity_smaller(bbio->raid_map[i],
5260 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005261 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005262 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005263 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005264 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005265 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005266 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005267
David Woodhouse53b381b2013-01-29 18:40:14 -05005268 again = 1;
5269 }
5270 }
5271 }
5272}
5273
Zhao Lei6e9606d2015-01-20 15:11:34 +08005274static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5275{
5276 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005277 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005278 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005279 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005280 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005281 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005282 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005283 /*
5284 * plus the raid_map, which includes both the tgt dev
5285 * and the stripes
5286 */
5287 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005288 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005289
5290 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005291 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005292
5293 return bbio;
5294}
5295
5296void btrfs_get_bbio(struct btrfs_bio *bbio)
5297{
Elena Reshetova140475a2017-03-03 10:55:10 +02005298 WARN_ON(!refcount_read(&bbio->refs));
5299 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005300}
5301
5302void btrfs_put_bbio(struct btrfs_bio *bbio)
5303{
5304 if (!bbio)
5305 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005306 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005307 kfree(bbio);
5308}
5309
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005310/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5311/*
5312 * Please note that, discard won't be sent to target device of device
5313 * replace.
5314 */
5315static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5316 u64 logical, u64 length,
5317 struct btrfs_bio **bbio_ret)
5318{
5319 struct extent_map *em;
5320 struct map_lookup *map;
5321 struct btrfs_bio *bbio;
5322 u64 offset;
5323 u64 stripe_nr;
5324 u64 stripe_nr_end;
5325 u64 stripe_end_offset;
5326 u64 stripe_cnt;
5327 u64 stripe_len;
5328 u64 stripe_offset;
5329 u64 num_stripes;
5330 u32 stripe_index;
5331 u32 factor = 0;
5332 u32 sub_stripes = 0;
5333 u64 stripes_per_dev = 0;
5334 u32 remaining_stripes = 0;
5335 u32 last_stripe = 0;
5336 int ret = 0;
5337 int i;
5338
5339 /* discard always return a bbio */
5340 ASSERT(bbio_ret);
5341
Omar Sandoval60ca8422018-05-16 16:34:31 -07005342 em = btrfs_get_chunk_map(fs_info, logical, length);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005343 if (IS_ERR(em))
5344 return PTR_ERR(em);
5345
5346 map = em->map_lookup;
5347 /* we don't discard raid56 yet */
5348 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5349 ret = -EOPNOTSUPP;
5350 goto out;
5351 }
5352
5353 offset = logical - em->start;
5354 length = min_t(u64, em->len - offset, length);
5355
5356 stripe_len = map->stripe_len;
5357 /*
5358 * stripe_nr counts the total number of stripes we have to stride
5359 * to get to this block
5360 */
5361 stripe_nr = div64_u64(offset, stripe_len);
5362
5363 /* stripe_offset is the offset of this block in its stripe */
5364 stripe_offset = offset - stripe_nr * stripe_len;
5365
5366 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005367 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005368 stripe_cnt = stripe_nr_end - stripe_nr;
5369 stripe_end_offset = stripe_nr_end * map->stripe_len -
5370 (offset + length);
5371 /*
5372 * after this, stripe_nr is the number of stripes on this
5373 * device we have to walk to find the data, and stripe_index is
5374 * the number of our device in the stripe array
5375 */
5376 num_stripes = 1;
5377 stripe_index = 0;
5378 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5379 BTRFS_BLOCK_GROUP_RAID10)) {
5380 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5381 sub_stripes = 1;
5382 else
5383 sub_stripes = map->sub_stripes;
5384
5385 factor = map->num_stripes / sub_stripes;
5386 num_stripes = min_t(u64, map->num_stripes,
5387 sub_stripes * stripe_cnt);
5388 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5389 stripe_index *= sub_stripes;
5390 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5391 &remaining_stripes);
5392 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5393 last_stripe *= sub_stripes;
5394 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5395 BTRFS_BLOCK_GROUP_DUP)) {
5396 num_stripes = map->num_stripes;
5397 } else {
5398 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5399 &stripe_index);
5400 }
5401
5402 bbio = alloc_btrfs_bio(num_stripes, 0);
5403 if (!bbio) {
5404 ret = -ENOMEM;
5405 goto out;
5406 }
5407
5408 for (i = 0; i < num_stripes; i++) {
5409 bbio->stripes[i].physical =
5410 map->stripes[stripe_index].physical +
5411 stripe_offset + stripe_nr * map->stripe_len;
5412 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5413
5414 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5415 BTRFS_BLOCK_GROUP_RAID10)) {
5416 bbio->stripes[i].length = stripes_per_dev *
5417 map->stripe_len;
5418
5419 if (i / sub_stripes < remaining_stripes)
5420 bbio->stripes[i].length +=
5421 map->stripe_len;
5422
5423 /*
5424 * Special for the first stripe and
5425 * the last stripe:
5426 *
5427 * |-------|...|-------|
5428 * |----------|
5429 * off end_off
5430 */
5431 if (i < sub_stripes)
5432 bbio->stripes[i].length -=
5433 stripe_offset;
5434
5435 if (stripe_index >= last_stripe &&
5436 stripe_index <= (last_stripe +
5437 sub_stripes - 1))
5438 bbio->stripes[i].length -=
5439 stripe_end_offset;
5440
5441 if (i == sub_stripes - 1)
5442 stripe_offset = 0;
5443 } else {
5444 bbio->stripes[i].length = length;
5445 }
5446
5447 stripe_index++;
5448 if (stripe_index == map->num_stripes) {
5449 stripe_index = 0;
5450 stripe_nr++;
5451 }
5452 }
5453
5454 *bbio_ret = bbio;
5455 bbio->map_type = map->type;
5456 bbio->num_stripes = num_stripes;
5457out:
5458 free_extent_map(em);
5459 return ret;
5460}
5461
Liu Bo5ab56092017-03-14 13:33:57 -07005462/*
5463 * In dev-replace case, for repair case (that's the only case where the mirror
5464 * is selected explicitly when calling btrfs_map_block), blocks left of the
5465 * left cursor can also be read from the target drive.
5466 *
5467 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5468 * array of stripes.
5469 * For READ, it also needs to be supported using the same mirror number.
5470 *
5471 * If the requested block is not left of the left cursor, EIO is returned. This
5472 * can happen because btrfs_num_copies() returns one more in the dev-replace
5473 * case.
5474 */
5475static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5476 u64 logical, u64 length,
5477 u64 srcdev_devid, int *mirror_num,
5478 u64 *physical)
5479{
5480 struct btrfs_bio *bbio = NULL;
5481 int num_stripes;
5482 int index_srcdev = 0;
5483 int found = 0;
5484 u64 physical_of_found = 0;
5485 int i;
5486 int ret = 0;
5487
5488 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5489 logical, &length, &bbio, 0, 0);
5490 if (ret) {
5491 ASSERT(bbio == NULL);
5492 return ret;
5493 }
5494
5495 num_stripes = bbio->num_stripes;
5496 if (*mirror_num > num_stripes) {
5497 /*
5498 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5499 * that means that the requested area is not left of the left
5500 * cursor
5501 */
5502 btrfs_put_bbio(bbio);
5503 return -EIO;
5504 }
5505
5506 /*
5507 * process the rest of the function using the mirror_num of the source
5508 * drive. Therefore look it up first. At the end, patch the device
5509 * pointer to the one of the target drive.
5510 */
5511 for (i = 0; i < num_stripes; i++) {
5512 if (bbio->stripes[i].dev->devid != srcdev_devid)
5513 continue;
5514
5515 /*
5516 * In case of DUP, in order to keep it simple, only add the
5517 * mirror with the lowest physical address
5518 */
5519 if (found &&
5520 physical_of_found <= bbio->stripes[i].physical)
5521 continue;
5522
5523 index_srcdev = i;
5524 found = 1;
5525 physical_of_found = bbio->stripes[i].physical;
5526 }
5527
5528 btrfs_put_bbio(bbio);
5529
5530 ASSERT(found);
5531 if (!found)
5532 return -EIO;
5533
5534 *mirror_num = index_srcdev + 1;
5535 *physical = physical_of_found;
5536 return ret;
5537}
5538
Liu Bo73c0f222017-03-14 13:33:58 -07005539static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5540 struct btrfs_bio **bbio_ret,
5541 struct btrfs_dev_replace *dev_replace,
5542 int *num_stripes_ret, int *max_errors_ret)
5543{
5544 struct btrfs_bio *bbio = *bbio_ret;
5545 u64 srcdev_devid = dev_replace->srcdev->devid;
5546 int tgtdev_indexes = 0;
5547 int num_stripes = *num_stripes_ret;
5548 int max_errors = *max_errors_ret;
5549 int i;
5550
5551 if (op == BTRFS_MAP_WRITE) {
5552 int index_where_to_add;
5553
5554 /*
5555 * duplicate the write operations while the dev replace
5556 * procedure is running. Since the copying of the old disk to
5557 * the new disk takes place at run time while the filesystem is
5558 * mounted writable, the regular write operations to the old
5559 * disk have to be duplicated to go to the new disk as well.
5560 *
5561 * Note that device->missing is handled by the caller, and that
5562 * the write to the old disk is already set up in the stripes
5563 * array.
5564 */
5565 index_where_to_add = num_stripes;
5566 for (i = 0; i < num_stripes; i++) {
5567 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5568 /* write to new disk, too */
5569 struct btrfs_bio_stripe *new =
5570 bbio->stripes + index_where_to_add;
5571 struct btrfs_bio_stripe *old =
5572 bbio->stripes + i;
5573
5574 new->physical = old->physical;
5575 new->length = old->length;
5576 new->dev = dev_replace->tgtdev;
5577 bbio->tgtdev_map[i] = index_where_to_add;
5578 index_where_to_add++;
5579 max_errors++;
5580 tgtdev_indexes++;
5581 }
5582 }
5583 num_stripes = index_where_to_add;
5584 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5585 int index_srcdev = 0;
5586 int found = 0;
5587 u64 physical_of_found = 0;
5588
5589 /*
5590 * During the dev-replace procedure, the target drive can also
5591 * be used to read data in case it is needed to repair a corrupt
5592 * block elsewhere. This is possible if the requested area is
5593 * left of the left cursor. In this area, the target drive is a
5594 * full copy of the source drive.
5595 */
5596 for (i = 0; i < num_stripes; i++) {
5597 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5598 /*
5599 * In case of DUP, in order to keep it simple,
5600 * only add the mirror with the lowest physical
5601 * address
5602 */
5603 if (found &&
5604 physical_of_found <=
5605 bbio->stripes[i].physical)
5606 continue;
5607 index_srcdev = i;
5608 found = 1;
5609 physical_of_found = bbio->stripes[i].physical;
5610 }
5611 }
5612 if (found) {
5613 struct btrfs_bio_stripe *tgtdev_stripe =
5614 bbio->stripes + num_stripes;
5615
5616 tgtdev_stripe->physical = physical_of_found;
5617 tgtdev_stripe->length =
5618 bbio->stripes[index_srcdev].length;
5619 tgtdev_stripe->dev = dev_replace->tgtdev;
5620 bbio->tgtdev_map[index_srcdev] = num_stripes;
5621
5622 tgtdev_indexes++;
5623 num_stripes++;
5624 }
5625 }
5626
5627 *num_stripes_ret = num_stripes;
5628 *max_errors_ret = max_errors;
5629 bbio->num_tgtdevs = tgtdev_indexes;
5630 *bbio_ret = bbio;
5631}
5632
Liu Bo2b19a1f2017-03-14 13:34:00 -07005633static bool need_full_stripe(enum btrfs_map_op op)
5634{
5635 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5636}
5637
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005638static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5639 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005640 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005641 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005642 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005643{
5644 struct extent_map *em;
5645 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005646 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005647 u64 stripe_offset;
5648 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005649 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005650 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005651 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005652 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005653 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005654 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005655 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005656 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005657 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5658 int dev_replace_is_ongoing = 0;
5659 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005660 int patch_the_first_stripe_for_dev_replace = 0;
5661 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005662 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005663
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005664 if (op == BTRFS_MAP_DISCARD)
5665 return __btrfs_map_block_for_discard(fs_info, logical,
5666 *length, bbio_ret);
5667
Omar Sandoval60ca8422018-05-16 16:34:31 -07005668 em = btrfs_get_chunk_map(fs_info, logical, *length);
Liu Bo592d92e2017-03-14 13:33:55 -07005669 if (IS_ERR(em))
5670 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005671
Jeff Mahoney95617d62015-06-03 10:55:48 -04005672 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005673 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005674
David Woodhouse53b381b2013-01-29 18:40:14 -05005675 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005676 stripe_nr = offset;
5677 /*
5678 * stripe_nr counts the total number of stripes we have to stride
5679 * to get to this block
5680 */
David Sterba47c57132015-02-20 18:43:47 +01005681 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005682
David Woodhouse53b381b2013-01-29 18:40:14 -05005683 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005684 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005685 btrfs_crit(fs_info,
5686 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005687 stripe_offset, offset, em->start, logical,
5688 stripe_len);
5689 free_extent_map(em);
5690 return -EINVAL;
5691 }
Chris Mason593060d2008-03-25 16:50:33 -04005692
5693 /* stripe_offset is the offset of this block in its stripe*/
5694 stripe_offset = offset - stripe_offset;
5695
David Woodhouse53b381b2013-01-29 18:40:14 -05005696 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005697 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005698 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5699 raid56_full_stripe_start = offset;
5700
5701 /* allow a write of a full stripe, but make sure we don't
5702 * allow straddling of stripes
5703 */
David Sterba47c57132015-02-20 18:43:47 +01005704 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5705 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005706 raid56_full_stripe_start *= full_stripe_len;
5707 }
5708
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005709 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005710 u64 max_len;
5711 /* For writes to RAID[56], allow a full stripeset across all disks.
5712 For other RAID types and for RAID[56] reads, just allow a single
5713 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005714 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005715 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005716 max_len = stripe_len * nr_data_stripes(map) -
5717 (offset - raid56_full_stripe_start);
5718 } else {
5719 /* we limit the length of each bio to what fits in a stripe */
5720 max_len = stripe_len - stripe_offset;
5721 }
5722 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005723 } else {
5724 *length = em->len - offset;
5725 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005726
David Woodhouse53b381b2013-01-29 18:40:14 -05005727 /* This is for when we're called from btrfs_merge_bio_hook() and all
5728 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005729 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005730 goto out;
5731
David Sterba7e79cb82018-03-24 02:11:38 +01005732 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005733 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5734 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01005735 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005736 else
5737 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005738
Stefan Behrensad6d6202012-11-06 15:06:47 +01005739 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005740 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005741 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5742 dev_replace->srcdev->devid,
5743 &mirror_num,
5744 &physical_to_patch_in_first_stripe);
5745 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005746 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005747 else
5748 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005749 } else if (mirror_num > map->num_stripes) {
5750 mirror_num = 0;
5751 }
5752
Chris Masonf2d8d742008-04-21 10:03:05 -04005753 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005754 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005755 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005756 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5757 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005758 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005759 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005760 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005761 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005762 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005763 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005764 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005765 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005766 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005767 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005768 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005769 }
Chris Mason2fff7342008-04-29 14:12:09 -04005770
Chris Mason611f0e02008-04-03 16:29:03 -04005771 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005772 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005773 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005774 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005775 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005776 } else {
5777 mirror_num = 1;
5778 }
Chris Mason2fff7342008-04-29 14:12:09 -04005779
Chris Mason321aecc2008-04-16 10:49:51 -04005780 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005781 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005782
David Sterba47c57132015-02-20 18:43:47 +01005783 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005784 stripe_index *= map->sub_stripes;
5785
Anand Jainde483732017-10-12 16:43:00 +08005786 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005787 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005788 else if (mirror_num)
5789 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005790 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005791 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005792 stripe_index = find_live_mirror(fs_info, map,
5793 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01005794 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005795 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005796 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005797
Zhao Leiffe2d202015-01-20 15:11:44 +08005798 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005799 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005800 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005801 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005802 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005803
5804 /* RAID[56] write or recovery. Return all stripes */
5805 num_stripes = map->num_stripes;
5806 max_errors = nr_parity_stripes(map);
5807
David Woodhouse53b381b2013-01-29 18:40:14 -05005808 *length = map->stripe_len;
5809 stripe_index = 0;
5810 stripe_offset = 0;
5811 } else {
5812 /*
5813 * Mirror #0 or #1 means the original data block.
5814 * Mirror #2 is RAID5 parity block.
5815 * Mirror #3 is RAID6 Q block.
5816 */
David Sterba47c57132015-02-20 18:43:47 +01005817 stripe_nr = div_u64_rem(stripe_nr,
5818 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005819 if (mirror_num > 1)
5820 stripe_index = nr_data_stripes(map) +
5821 mirror_num - 2;
5822
5823 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005824 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5825 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005826 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005827 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005828 }
Chris Mason8790d502008-04-03 16:29:03 -04005829 } else {
5830 /*
David Sterba47c57132015-02-20 18:43:47 +01005831 * after this, stripe_nr is the number of stripes on this
5832 * device we have to walk to find the data, and stripe_index is
5833 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005834 */
David Sterba47c57132015-02-20 18:43:47 +01005835 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5836 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005837 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005838 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005839 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005840 btrfs_crit(fs_info,
5841 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005842 stripe_index, map->num_stripes);
5843 ret = -EINVAL;
5844 goto out;
5845 }
Chris Mason593060d2008-03-25 16:50:33 -04005846
Stefan Behrens472262f2012-11-06 14:43:46 +01005847 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005848 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005849 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005850 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005851 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005852 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005853 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005854 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005855
Zhao Lei6e9606d2015-01-20 15:11:34 +08005856 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005857 if (!bbio) {
5858 ret = -ENOMEM;
5859 goto out;
5860 }
Liu Bo6fad8232017-03-14 13:33:59 -07005861 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005862 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005863
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005864 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005865 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5866 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005867 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005868 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005869
5870 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5871 sizeof(struct btrfs_bio_stripe) *
5872 num_alloc_stripes +
5873 sizeof(int) * tgtdev_indexes);
5874
5875 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005876 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005877
5878 /* Fill in the logical address of each stripe */
5879 tmp = stripe_nr * nr_data_stripes(map);
5880 for (i = 0; i < nr_data_stripes(map); i++)
5881 bbio->raid_map[(i+rot) % num_stripes] =
5882 em->start + (tmp + i) * map->stripe_len;
5883
5884 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5885 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5886 bbio->raid_map[(i+rot+1) % num_stripes] =
5887 RAID6_Q_STRIPE;
5888 }
5889
Li Zefanec9ef7a2011-12-01 14:06:42 +08005890
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005891 for (i = 0; i < num_stripes; i++) {
5892 bbio->stripes[i].physical =
5893 map->stripes[stripe_index].physical +
5894 stripe_offset +
5895 stripe_nr * map->stripe_len;
5896 bbio->stripes[i].dev =
5897 map->stripes[stripe_index].dev;
5898 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005899 }
Li Zefande11cc12011-12-01 12:55:47 +08005900
Liu Bo2b19a1f2017-03-14 13:34:00 -07005901 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005902 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005903
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005904 if (bbio->raid_map)
5905 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005906
Liu Bo73c0f222017-03-14 13:33:58 -07005907 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005908 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005909 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5910 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005911 }
5912
Li Zefande11cc12011-12-01 12:55:47 +08005913 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005914 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005915 bbio->num_stripes = num_stripes;
5916 bbio->max_errors = max_errors;
5917 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005918
5919 /*
5920 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5921 * mirror_num == num_stripes + 1 && dev_replace target drive is
5922 * available as a mirror
5923 */
5924 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5925 WARN_ON(num_stripes > 1);
5926 bbio->stripes[0].dev = dev_replace->tgtdev;
5927 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5928 bbio->mirror_num = map->num_stripes + 1;
5929 }
Chris Masoncea9e442008-04-09 16:28:12 -04005930out:
Liu Bo73beece2015-07-17 16:49:19 +08005931 if (dev_replace_is_ongoing) {
David Sterba7fb2ece2018-08-24 17:33:58 +02005932 ASSERT(atomic_read(&dev_replace->blocking_readers) > 0);
5933 btrfs_dev_replace_read_lock(dev_replace);
5934 /* Barrier implied by atomic_dec_and_test */
5935 if (atomic_dec_and_test(&dev_replace->blocking_readers))
5936 cond_wake_up_nomb(&dev_replace->read_lock_wq);
David Sterba7e79cb82018-03-24 02:11:38 +01005937 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005938 }
Chris Mason0b86a832008-03-24 15:01:56 -04005939 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005940 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005941}
5942
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005943int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005944 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005945 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005946{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005947 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005948 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005949}
5950
Miao Xieaf8e2d12014-10-23 14:42:50 +08005951/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005952int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005953 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005954 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005955{
David Sterba825ad4c2017-03-28 14:45:22 +02005956 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005957}
5958
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03005959int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
5960 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05005961{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005962 struct extent_map *em;
5963 struct map_lookup *map;
5964 u64 *buf;
5965 u64 bytenr;
5966 u64 length;
5967 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005968 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005969 int i, j, nr = 0;
5970
Omar Sandoval60ca8422018-05-16 16:34:31 -07005971 em = btrfs_get_chunk_map(fs_info, chunk_start, 1);
Liu Bo592d92e2017-03-14 13:33:55 -07005972 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005973 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005974
Jeff Mahoney95617d62015-06-03 10:55:48 -04005975 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005976 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005977 rmap_len = map->stripe_len;
5978
Yan Zhenga512bbf2008-12-08 16:46:26 -05005979 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005980 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005981 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005982 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005983 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005984 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005985 rmap_len = map->stripe_len * nr_data_stripes(map);
5986 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005987
David Sterba31e818f2015-02-20 18:00:26 +01005988 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005989 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005990
5991 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05005992 if (map->stripes[i].physical > physical ||
5993 map->stripes[i].physical + length <= physical)
5994 continue;
5995
5996 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07005997 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005998
5999 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6000 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006001 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006002 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6003 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006004 } /* else if RAID[56], multiply by nr_data_stripes().
6005 * Alternatively, just use rmap_len below instead of
6006 * map->stripe_len */
6007
6008 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006009 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006010 for (j = 0; j < nr; j++) {
6011 if (buf[j] == bytenr)
6012 break;
6013 }
Chris Mason934d3752008-12-08 16:43:10 -05006014 if (j == nr) {
6015 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006016 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006017 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006018 }
6019
Yan Zhenga512bbf2008-12-08 16:46:26 -05006020 *logical = buf;
6021 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006022 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006023
6024 free_extent_map(em);
6025 return 0;
6026}
6027
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006028static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006029{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006030 bio->bi_private = bbio->private;
6031 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006032 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006033
Zhao Lei6e9606d2015-01-20 15:11:34 +08006034 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006035}
6036
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006037static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006038{
Chris Mason9be33952013-05-17 18:30:14 -04006039 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006040 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006041
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006042 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006043 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006044 if (bio->bi_status == BLK_STS_IOERR ||
6045 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006046 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006047 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006048 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006049
6050 BUG_ON(stripe_index >= bbio->num_stripes);
6051 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006052 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006053 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006054 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006055 BTRFS_DEV_STAT_WRITE_ERRS);
6056 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006057 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006058 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006059 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006060 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006061 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006062 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006063 }
6064 }
Chris Mason8790d502008-04-03 16:29:03 -04006065
Jan Schmidta1d3c472011-08-04 17:15:33 +02006066 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006067 is_orig_bio = 1;
6068
Miao Xiec404e0d2014-01-30 16:46:55 +08006069 btrfs_bio_counter_dec(bbio->fs_info);
6070
Jan Schmidta1d3c472011-08-04 17:15:33 +02006071 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006072 if (!is_orig_bio) {
6073 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006074 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006075 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006076
Chris Mason9be33952013-05-17 18:30:14 -04006077 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006078 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006079 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006080 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006081 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006082 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006083 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006084 /*
6085 * this bio is actually up to date, we didn't
6086 * go over the max number of errors
6087 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006088 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006089 }
Miao Xiec55f1392014-06-19 10:42:54 +08006090
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006091 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006092 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006093 bio_put(bio);
6094 }
Chris Mason8790d502008-04-03 16:29:03 -04006095}
6096
Chris Mason8b712842008-06-11 16:50:36 -04006097/*
6098 * see run_scheduled_bios for a description of why bios are collected for
6099 * async submit.
6100 *
6101 * This will add one bio to the pending list for a device and make sure
6102 * the work struct is scheduled.
6103 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006104static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006105 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006106{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006107 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006108 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006109 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006110
6111 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006112 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006113 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006114 return;
Chris Mason8b712842008-06-11 16:50:36 -04006115 }
6116
Chris Mason492bb6de2008-07-31 16:29:02 -04006117 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006118 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006119
6120 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006121 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006122 pending_bios = &device->pending_sync_bios;
6123 else
6124 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006125
Chris Masonffbd5172009-04-20 15:50:09 -04006126 if (pending_bios->tail)
6127 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006128
Chris Masonffbd5172009-04-20 15:50:09 -04006129 pending_bios->tail = bio;
6130 if (!pending_bios->head)
6131 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006132 if (device->running_pending)
6133 should_queue = 0;
6134
6135 spin_unlock(&device->io_lock);
6136
6137 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006138 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006139}
6140
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006141static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6142 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006143{
6144 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006145 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006146
6147 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006148 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006149 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006150 bio->bi_iter.bi_sector = physical >> 9;
Misono Tomohiro672d5992018-08-02 16:19:07 +09006151 btrfs_debug_in_rcu(fs_info,
6152 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6153 bio_op(bio), bio->bi_opf, (u64)bio->bi_iter.bi_sector,
6154 (u_long)dev->bdev->bd_dev, rcu_str_deref(dev->name), dev->devid,
6155 bio->bi_iter.bi_size);
Christoph Hellwig74d46992017-08-23 19:10:32 +02006156 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006157
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006158 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006159
Josef Bacikde1ee922012-10-19 16:50:56 -04006160 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006161 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006162 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006163 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006164}
6165
Josef Bacikde1ee922012-10-19 16:50:56 -04006166static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6167{
6168 atomic_inc(&bbio->error);
6169 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006170 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006171 WARN_ON(bio != bbio->orig_bio);
6172
Chris Mason9be33952013-05-17 18:30:14 -04006173 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006174 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006175 if (atomic_read(&bbio->error) > bbio->max_errors)
6176 bio->bi_status = BLK_STS_IOERR;
6177 else
6178 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006179 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006180 }
6181}
6182
Omar Sandoval58efbc92017-08-22 23:45:59 -07006183blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6184 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006185{
Chris Mason0b86a832008-03-24 15:01:56 -04006186 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006187 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006188 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006189 u64 length = 0;
6190 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006191 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006192 int dev_nr;
6193 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006194 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006195
Kent Overstreet4f024f32013-10-11 15:44:27 -07006196 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006197 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006198
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006199 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006200 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006201 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006202 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006203 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006204 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006205 }
Chris Masoncea9e442008-04-09 16:28:12 -04006206
Jan Schmidta1d3c472011-08-04 17:15:33 +02006207 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006208 bbio->orig_bio = first_bio;
6209 bbio->private = first_bio->bi_private;
6210 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006211 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006212 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6213
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006214 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006215 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006216 /* In this case, map_length has been set to the length of
6217 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006218 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006219 ret = raid56_parity_write(fs_info, bio, bbio,
6220 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006221 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006222 ret = raid56_parity_recover(fs_info, bio, bbio,
6223 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006224 }
Miao Xie42452152014-11-25 16:39:28 +08006225
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006226 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006227 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006228 }
6229
Chris Masoncea9e442008-04-09 16:28:12 -04006230 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006231 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006232 "mapping failed logical %llu bio len %llu len %llu",
6233 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006234 BUG();
6235 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006236
Zhao Lei08da7572015-02-12 15:42:16 +08006237 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006238 dev = bbio->stripes[dev_nr].dev;
Nikolay Borisovfc8a1682018-11-08 16:16:38 +02006239 if (!dev || !dev->bdev || test_bit(BTRFS_DEV_STATE_MISSING,
6240 &dev->dev_state) ||
Anand Jainebbede42017-12-04 12:54:52 +08006241 (bio_op(first_bio) == REQ_OP_WRITE &&
6242 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006243 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006244 continue;
6245 }
6246
David Sterba3aa8e072017-06-02 17:38:30 +02006247 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006248 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006249 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006250 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006251
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006252 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6253 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006254 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006255 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006256 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006257}
6258
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006259struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006260 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006261{
Yan Zheng2b820322008-11-17 21:11:30 -05006262 struct btrfs_device *device;
6263 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006264
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006265 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006266 while (cur_devices) {
6267 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006268 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006269 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006270 if (device)
6271 return device;
6272 }
6273 cur_devices = cur_devices->seed;
6274 }
6275 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006276}
6277
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006278static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006279 u64 devid, u8 *dev_uuid)
6280{
6281 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006282
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006283 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6284 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006285 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006286
6287 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006288 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006289 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006290
Anand Jaine6e674b2017-12-04 12:54:54 +08006291 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006292 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006293
Chris Masondfe25022008-05-13 13:46:40 -04006294 return device;
6295}
6296
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006297/**
6298 * btrfs_alloc_device - allocate struct btrfs_device
6299 * @fs_info: used only for generating a new devid, can be NULL if
6300 * devid is provided (i.e. @devid != NULL).
6301 * @devid: a pointer to devid for this device. If NULL a new devid
6302 * is generated.
6303 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6304 * is generated.
6305 *
6306 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006307 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006308 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006309 */
6310struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6311 const u64 *devid,
6312 const u8 *uuid)
6313{
6314 struct btrfs_device *dev;
6315 u64 tmp;
6316
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306317 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006318 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006319
6320 dev = __alloc_device();
6321 if (IS_ERR(dev))
6322 return dev;
6323
6324 if (devid)
6325 tmp = *devid;
6326 else {
6327 int ret;
6328
6329 ret = find_next_devid(fs_info, &tmp);
6330 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006331 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006332 return ERR_PTR(ret);
6333 }
6334 }
6335 dev->devid = tmp;
6336
6337 if (uuid)
6338 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6339 else
6340 generate_random_uuid(dev->uuid);
6341
Liu Bo9e0af232014-08-15 23:36:53 +08006342 btrfs_init_work(&dev->work, btrfs_submit_helper,
6343 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006344
6345 return dev;
6346}
6347
Liu Boe06cd3d2016-06-03 12:05:15 -07006348/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006349static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006350 struct extent_buffer *leaf,
6351 struct btrfs_chunk *chunk, u64 logical)
6352{
6353 u64 length;
6354 u64 stripe_len;
6355 u16 num_stripes;
6356 u16 sub_stripes;
6357 u64 type;
Gu Jinxiang315409b2018-07-04 18:16:39 +08006358 u64 features;
6359 bool mixed = false;
Liu Boe06cd3d2016-06-03 12:05:15 -07006360
6361 length = btrfs_chunk_length(leaf, chunk);
6362 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6363 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6364 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6365 type = btrfs_chunk_type(leaf, chunk);
6366
6367 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006368 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006369 num_stripes);
6370 return -EIO;
6371 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006372 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6373 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006374 return -EIO;
6375 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006376 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6377 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006378 btrfs_chunk_sector_size(leaf, chunk));
6379 return -EIO;
6380 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006381 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6382 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006383 return -EIO;
6384 }
6385 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006386 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006387 stripe_len);
6388 return -EIO;
6389 }
6390 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6391 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006392 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006393 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6394 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6395 btrfs_chunk_type(leaf, chunk));
6396 return -EIO;
6397 }
Gu Jinxiang315409b2018-07-04 18:16:39 +08006398
6399 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
6400 btrfs_err(fs_info, "missing chunk type flag: 0x%llx", type);
6401 return -EIO;
6402 }
6403
6404 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
6405 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
6406 btrfs_err(fs_info,
6407 "system chunk with data or metadata type: 0x%llx", type);
6408 return -EIO;
6409 }
6410
6411 features = btrfs_super_incompat_flags(fs_info->super_copy);
6412 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
6413 mixed = true;
6414
6415 if (!mixed) {
6416 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
6417 (type & BTRFS_BLOCK_GROUP_DATA)) {
6418 btrfs_err(fs_info,
6419 "mixed chunk type in non-mixed mode: 0x%llx", type);
6420 return -EIO;
6421 }
6422 }
6423
Liu Boe06cd3d2016-06-03 12:05:15 -07006424 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6425 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6426 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6427 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6428 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6429 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6430 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006431 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006432 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6433 num_stripes, sub_stripes,
6434 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6435 return -EIO;
6436 }
6437
6438 return 0;
6439}
6440
Anand Jain5a2b8e62017-10-09 11:07:45 +08006441static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006442 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006443{
Anand Jain2b902df2017-10-09 11:07:46 +08006444 if (error)
6445 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6446 devid, uuid);
6447 else
6448 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6449 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006450}
6451
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006452static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006453 struct extent_buffer *leaf,
6454 struct btrfs_chunk *chunk)
6455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006456 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006457 struct map_lookup *map;
6458 struct extent_map *em;
6459 u64 logical;
6460 u64 length;
6461 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006462 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006463 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006464 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006465 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006466
Chris Masone17cade2008-04-15 15:41:47 -04006467 logical = key->offset;
6468 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006469 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006470
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006471 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006472 if (ret)
6473 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006474
Chris Mason890871b2009-09-02 16:24:52 -04006475 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006476 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006477 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006478
6479 /* already mapped? */
6480 if (em && em->start <= logical && em->start + em->len > logical) {
6481 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006482 return 0;
6483 } else if (em) {
6484 free_extent_map(em);
6485 }
Chris Mason0b86a832008-03-24 15:01:56 -04006486
David Sterba172ddd62011-04-21 00:48:27 +02006487 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006488 if (!em)
6489 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006490 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006491 if (!map) {
6492 free_extent_map(em);
6493 return -ENOMEM;
6494 }
6495
Wang Shilong298a8f92014-06-19 10:42:52 +08006496 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006497 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006498 em->start = logical;
6499 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006500 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006501 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006502 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006503
Chris Mason593060d2008-03-25 16:50:33 -04006504 map->num_stripes = num_stripes;
6505 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6506 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006507 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6508 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006509 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Qu Wenruocf90d882018-08-01 10:37:19 +08006510 map->verified_stripes = 0;
Chris Mason593060d2008-03-25 16:50:33 -04006511 for (i = 0; i < num_stripes; i++) {
6512 map->stripes[i].physical =
6513 btrfs_stripe_offset_nr(leaf, chunk, i);
6514 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006515 read_extent_buffer(leaf, uuid, (unsigned long)
6516 btrfs_stripe_dev_uuid_nr(chunk, i),
6517 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006518 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006519 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006520 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006521 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006522 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006523 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006524 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006525 }
Chris Masondfe25022008-05-13 13:46:40 -04006526 if (!map->stripes[i].dev) {
6527 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006528 add_missing_dev(fs_info->fs_devices, devid,
6529 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006530 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006531 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006532 btrfs_err(fs_info,
6533 "failed to init missing dev %llu: %ld",
6534 devid, PTR_ERR(map->stripes[i].dev));
6535 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006536 }
Anand Jain2b902df2017-10-09 11:07:46 +08006537 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006538 }
Anand Jaine12c9622017-12-04 12:54:53 +08006539 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6540 &(map->stripes[i].dev->dev_state));
6541
Chris Mason0b86a832008-03-24 15:01:56 -04006542 }
6543
Chris Mason890871b2009-09-02 16:24:52 -04006544 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006545 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006546 write_unlock(&map_tree->map_tree.lock);
Qu Wenruo64f64f42018-08-01 10:37:20 +08006547 if (ret < 0) {
6548 btrfs_err(fs_info,
6549 "failed to add chunk map, start=%llu len=%llu: %d",
6550 em->start, em->len, ret);
6551 }
Chris Mason0b86a832008-03-24 15:01:56 -04006552 free_extent_map(em);
6553
Qu Wenruo64f64f42018-08-01 10:37:20 +08006554 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006555}
6556
Jeff Mahoney143bede2012-03-01 14:56:26 +01006557static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006558 struct btrfs_dev_item *dev_item,
6559 struct btrfs_device *device)
6560{
6561 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006562
6563 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006564 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6565 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006566 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006567 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006568 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006569 device->type = btrfs_device_type(leaf, dev_item);
6570 device->io_align = btrfs_device_io_align(leaf, dev_item);
6571 device->io_width = btrfs_device_io_width(leaf, dev_item);
6572 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006573 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006574 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006575
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006576 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006577 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006578}
6579
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006580static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006581 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006582{
6583 struct btrfs_fs_devices *fs_devices;
6584 int ret;
6585
David Sterbaa32bf9a2018-03-16 02:21:22 +01006586 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006587 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006588
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006589 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006590 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006591 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006592 return fs_devices;
6593
Yan Zheng2b820322008-11-17 21:11:30 -05006594 fs_devices = fs_devices->seed;
6595 }
6596
6597 fs_devices = find_fsid(fsid);
6598 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006599 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006600 return ERR_PTR(-ENOENT);
6601
6602 fs_devices = alloc_fs_devices(fsid);
6603 if (IS_ERR(fs_devices))
6604 return fs_devices;
6605
6606 fs_devices->seeding = 1;
6607 fs_devices->opened = 1;
6608 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006609 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006610
6611 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006612 if (IS_ERR(fs_devices))
6613 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006614
Anand Jain897fb572018-04-12 10:29:28 +08006615 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006616 if (ret) {
6617 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006618 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006619 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006620 }
Yan Zheng2b820322008-11-17 21:11:30 -05006621
6622 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006623 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006624 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006625 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006626 goto out;
6627 }
6628
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006629 fs_devices->seed = fs_info->fs_devices->seed;
6630 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006631out:
Miao Xie5f375832014-09-03 21:35:46 +08006632 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006633}
6634
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006635static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006636 struct extent_buffer *leaf,
6637 struct btrfs_dev_item *dev_item)
6638{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006639 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006640 struct btrfs_device *device;
6641 u64 devid;
6642 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006643 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006644 u8 dev_uuid[BTRFS_UUID_SIZE];
6645
Chris Mason0b86a832008-03-24 15:01:56 -04006646 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006647 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006648 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006649 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006650 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006651
Anand Jain44880fd2017-07-29 17:50:09 +08006652 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006653 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006654 if (IS_ERR(fs_devices))
6655 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006656 }
6657
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006658 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006659 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006660 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006661 btrfs_report_missing_device(fs_info, devid,
6662 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006663 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006664 }
Yan Zheng2b820322008-11-17 21:11:30 -05006665
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006666 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006667 if (IS_ERR(device)) {
6668 btrfs_err(fs_info,
6669 "failed to add missing dev %llu: %ld",
6670 devid, PTR_ERR(device));
6671 return PTR_ERR(device);
6672 }
Anand Jain2b902df2017-10-09 11:07:46 +08006673 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006674 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006675 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006676 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6677 btrfs_report_missing_device(fs_info,
6678 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006679 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006680 }
6681 btrfs_report_missing_device(fs_info, devid,
6682 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006683 }
Miao Xie5f375832014-09-03 21:35:46 +08006684
Anand Jaine6e674b2017-12-04 12:54:54 +08006685 if (!device->bdev &&
6686 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006687 /*
6688 * this happens when a device that was properly setup
6689 * in the device info lists suddenly goes bad.
6690 * device->bdev is NULL, and so we have to set
6691 * device->missing to one here
6692 */
Miao Xie5f375832014-09-03 21:35:46 +08006693 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006694 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006695 }
Miao Xie5f375832014-09-03 21:35:46 +08006696
6697 /* Move the device to its own fs_devices */
6698 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006699 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6700 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006701
6702 list_move(&device->dev_list, &fs_devices->devices);
6703 device->fs_devices->num_devices--;
6704 fs_devices->num_devices++;
6705
6706 device->fs_devices->missing_devices--;
6707 fs_devices->missing_devices++;
6708
6709 device->fs_devices = fs_devices;
6710 }
Yan Zheng2b820322008-11-17 21:11:30 -05006711 }
6712
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006713 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006714 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006715 if (device->generation !=
6716 btrfs_device_generation(leaf, dev_item))
6717 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006718 }
Chris Mason0b86a832008-03-24 15:01:56 -04006719
6720 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006721 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006722 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006723 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006724 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006725 atomic64_add(device->total_bytes - device->bytes_used,
6726 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006727 }
Chris Mason0b86a832008-03-24 15:01:56 -04006728 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006729 return ret;
6730}
6731
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006732int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006733{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006734 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006735 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006736 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006737 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006738 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006739 u8 *array_ptr;
6740 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006741 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006742 u32 num_stripes;
6743 u32 array_size;
6744 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006745 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006746 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006747 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006748
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006749 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006750 /*
6751 * This will create extent buffer of nodesize, superblock size is
6752 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6753 * overallocate but we can keep it as-is, only the first page is used.
6754 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006755 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006756 if (IS_ERR(sb))
6757 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006758 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006759 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006760 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006761 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006762 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006763 * pages up-to-date when the page is larger: extent does not cover the
6764 * whole page and consequently check_page_uptodate does not find all
6765 * the page's extents up-to-date (the hole beyond sb),
6766 * write_extent_buffer then triggers a WARN_ON.
6767 *
6768 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6769 * but sb spans only this function. Add an explicit SetPageUptodate call
6770 * to silence the warning eg. on PowerPC 64.
6771 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006772 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006773 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006774
Chris Masona061fc82008-05-07 11:43:44 -04006775 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006776 array_size = btrfs_super_sys_array_size(super_copy);
6777
David Sterba1ffb22c2014-10-31 19:02:42 +01006778 array_ptr = super_copy->sys_chunk_array;
6779 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6780 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006781
David Sterba1ffb22c2014-10-31 19:02:42 +01006782 while (cur_offset < array_size) {
6783 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006784 len = sizeof(*disk_key);
6785 if (cur_offset + len > array_size)
6786 goto out_short_read;
6787
Chris Mason0b86a832008-03-24 15:01:56 -04006788 btrfs_disk_key_to_cpu(&key, disk_key);
6789
David Sterba1ffb22c2014-10-31 19:02:42 +01006790 array_ptr += len;
6791 sb_array_offset += len;
6792 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006793
Chris Mason0d81ba52008-03-24 15:02:07 -04006794 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006795 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006796 /*
6797 * At least one btrfs_chunk with one stripe must be
6798 * present, exact stripe count check comes afterwards
6799 */
6800 len = btrfs_chunk_item_size(1);
6801 if (cur_offset + len > array_size)
6802 goto out_short_read;
6803
6804 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006805 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006806 btrfs_err(fs_info,
6807 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006808 num_stripes, cur_offset);
6809 ret = -EIO;
6810 break;
6811 }
6812
Liu Boe06cd3d2016-06-03 12:05:15 -07006813 type = btrfs_chunk_type(sb, chunk);
6814 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006815 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006816 "invalid chunk type %llu in sys_array at offset %u",
6817 type, cur_offset);
6818 ret = -EIO;
6819 break;
6820 }
6821
David Sterbae3540ea2014-11-05 15:24:51 +01006822 len = btrfs_chunk_item_size(num_stripes);
6823 if (cur_offset + len > array_size)
6824 goto out_short_read;
6825
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006826 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006827 if (ret)
6828 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006829 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006830 btrfs_err(fs_info,
6831 "unexpected item type %u in sys_array at offset %u",
6832 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006833 ret = -EIO;
6834 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006835 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006836 array_ptr += len;
6837 sb_array_offset += len;
6838 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006839 }
Liu Bod8651772016-06-03 17:41:42 -07006840 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006841 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006842 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006843
6844out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006845 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006846 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006847 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006848 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006849 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006850}
6851
Qu Wenruo21634a12017-03-09 09:34:36 +08006852/*
6853 * Check if all chunks in the fs are OK for read-write degraded mount
6854 *
Anand Jain6528b992017-12-18 17:08:59 +08006855 * If the @failing_dev is specified, it's accounted as missing.
6856 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006857 * Return true if all chunks meet the minimal RW mount requirements.
6858 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6859 */
Anand Jain6528b992017-12-18 17:08:59 +08006860bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6861 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006862{
6863 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6864 struct extent_map *em;
6865 u64 next_start = 0;
6866 bool ret = true;
6867
6868 read_lock(&map_tree->map_tree.lock);
6869 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6870 read_unlock(&map_tree->map_tree.lock);
6871 /* No chunk at all? Return false anyway */
6872 if (!em) {
6873 ret = false;
6874 goto out;
6875 }
6876 while (em) {
6877 struct map_lookup *map;
6878 int missing = 0;
6879 int max_tolerated;
6880 int i;
6881
6882 map = em->map_lookup;
6883 max_tolerated =
6884 btrfs_get_num_tolerated_disk_barrier_failures(
6885 map->type);
6886 for (i = 0; i < map->num_stripes; i++) {
6887 struct btrfs_device *dev = map->stripes[i].dev;
6888
Anand Jaine6e674b2017-12-04 12:54:54 +08006889 if (!dev || !dev->bdev ||
6890 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006891 dev->last_flush_error)
6892 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006893 else if (failing_dev && failing_dev == dev)
6894 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006895 }
6896 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006897 if (!failing_dev)
6898 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006899 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6900 em->start, missing, max_tolerated);
6901 free_extent_map(em);
6902 ret = false;
6903 goto out;
6904 }
6905 next_start = extent_map_end(em);
6906 free_extent_map(em);
6907
6908 read_lock(&map_tree->map_tree.lock);
6909 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6910 (u64)(-1) - next_start);
6911 read_unlock(&map_tree->map_tree.lock);
6912 }
6913out:
6914 return ret;
6915}
6916
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006917int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006918{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006919 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006920 struct btrfs_path *path;
6921 struct extent_buffer *leaf;
6922 struct btrfs_key key;
6923 struct btrfs_key found_key;
6924 int ret;
6925 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006926 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006927
Chris Mason0b86a832008-03-24 15:01:56 -04006928 path = btrfs_alloc_path();
6929 if (!path)
6930 return -ENOMEM;
6931
Anand Jain3dd0f7a2018-04-12 10:29:32 +08006932 /*
6933 * uuid_mutex is needed only if we are mounting a sprout FS
6934 * otherwise we don't need it.
6935 */
Li Zefanb367e472011-12-07 11:38:24 +08006936 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006937 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006938
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006939 /*
6940 * Read all device items, and then all the chunk items. All
6941 * device items are found before any chunk item (their object id
6942 * is smaller than the lowest possible object id for a chunk
6943 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006944 */
6945 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6946 key.offset = 0;
6947 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006948 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006949 if (ret < 0)
6950 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006951 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006952 leaf = path->nodes[0];
6953 slot = path->slots[0];
6954 if (slot >= btrfs_header_nritems(leaf)) {
6955 ret = btrfs_next_leaf(root, path);
6956 if (ret == 0)
6957 continue;
6958 if (ret < 0)
6959 goto error;
6960 break;
6961 }
6962 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006963 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6964 struct btrfs_dev_item *dev_item;
6965 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006966 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006967 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006968 if (ret)
6969 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006970 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006971 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6972 struct btrfs_chunk *chunk;
6973 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006974 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006975 if (ret)
6976 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006977 }
6978 path->slots[0]++;
6979 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006980
6981 /*
6982 * After loading chunk tree, we've got all device information,
6983 * do another round of validation checks.
6984 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006985 if (total_dev != fs_info->fs_devices->total_devices) {
6986 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006987 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006988 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006989 total_dev);
6990 ret = -EINVAL;
6991 goto error;
6992 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006993 if (btrfs_super_total_bytes(fs_info->super_copy) <
6994 fs_info->fs_devices->total_rw_bytes) {
6995 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006996 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006997 btrfs_super_total_bytes(fs_info->super_copy),
6998 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006999 ret = -EINVAL;
7000 goto error;
7001 }
Chris Mason0b86a832008-03-24 15:01:56 -04007002 ret = 0;
7003error:
David Sterba34441362016-10-04 19:34:27 +02007004 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007005 mutex_unlock(&uuid_mutex);
7006
Yan Zheng2b820322008-11-17 21:11:30 -05007007 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007008 return ret;
7009}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007010
Miao Xiecb517ea2013-05-15 07:48:19 +00007011void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7012{
7013 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7014 struct btrfs_device *device;
7015
Liu Bo29cc83f2014-05-11 23:14:59 +08007016 while (fs_devices) {
7017 mutex_lock(&fs_devices->device_list_mutex);
7018 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007019 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007020 mutex_unlock(&fs_devices->device_list_mutex);
7021
7022 fs_devices = fs_devices->seed;
7023 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007024}
7025
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007026static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7027{
7028 int i;
7029
7030 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7031 btrfs_dev_stat_reset(dev, i);
7032}
7033
7034int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7035{
7036 struct btrfs_key key;
7037 struct btrfs_key found_key;
7038 struct btrfs_root *dev_root = fs_info->dev_root;
7039 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7040 struct extent_buffer *eb;
7041 int slot;
7042 int ret = 0;
7043 struct btrfs_device *device;
7044 struct btrfs_path *path = NULL;
7045 int i;
7046
7047 path = btrfs_alloc_path();
7048 if (!path) {
7049 ret = -ENOMEM;
7050 goto out;
7051 }
7052
7053 mutex_lock(&fs_devices->device_list_mutex);
7054 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7055 int item_size;
7056 struct btrfs_dev_stats_item *ptr;
7057
David Sterba242e2952016-01-25 17:51:31 +01007058 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7059 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007060 key.offset = device->devid;
7061 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7062 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007063 __btrfs_reset_dev_stats(device);
7064 device->dev_stats_valid = 1;
7065 btrfs_release_path(path);
7066 continue;
7067 }
7068 slot = path->slots[0];
7069 eb = path->nodes[0];
7070 btrfs_item_key_to_cpu(eb, &found_key, slot);
7071 item_size = btrfs_item_size_nr(eb, slot);
7072
7073 ptr = btrfs_item_ptr(eb, slot,
7074 struct btrfs_dev_stats_item);
7075
7076 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7077 if (item_size >= (1 + i) * sizeof(__le64))
7078 btrfs_dev_stat_set(device, i,
7079 btrfs_dev_stats_value(eb, ptr, i));
7080 else
7081 btrfs_dev_stat_reset(device, i);
7082 }
7083
7084 device->dev_stats_valid = 1;
7085 btrfs_dev_stat_print_on_load(device);
7086 btrfs_release_path(path);
7087 }
7088 mutex_unlock(&fs_devices->device_list_mutex);
7089
7090out:
7091 btrfs_free_path(path);
7092 return ret < 0 ? ret : 0;
7093}
7094
7095static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007096 struct btrfs_device *device)
7097{
Nikolay Borisov5495f192018-07-20 19:37:49 +03007098 struct btrfs_fs_info *fs_info = trans->fs_info;
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007099 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007100 struct btrfs_path *path;
7101 struct btrfs_key key;
7102 struct extent_buffer *eb;
7103 struct btrfs_dev_stats_item *ptr;
7104 int ret;
7105 int i;
7106
David Sterba242e2952016-01-25 17:51:31 +01007107 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7108 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007109 key.offset = device->devid;
7110
7111 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007112 if (!path)
7113 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007114 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7115 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007116 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007117 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007118 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007119 goto out;
7120 }
7121
7122 if (ret == 0 &&
7123 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7124 /* need to delete old one and insert a new one */
7125 ret = btrfs_del_item(trans, dev_root, path);
7126 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007127 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007128 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007129 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007130 goto out;
7131 }
7132 ret = 1;
7133 }
7134
7135 if (ret == 1) {
7136 /* need to insert a new item */
7137 btrfs_release_path(path);
7138 ret = btrfs_insert_empty_item(trans, dev_root, path,
7139 &key, sizeof(*ptr));
7140 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007141 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007142 "insert dev_stats item for device %s failed %d",
7143 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007144 goto out;
7145 }
7146 }
7147
7148 eb = path->nodes[0];
7149 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7150 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7151 btrfs_set_dev_stats_value(eb, ptr, i,
7152 btrfs_dev_stat_read(device, i));
7153 btrfs_mark_buffer_dirty(eb);
7154
7155out:
7156 btrfs_free_path(path);
7157 return ret;
7158}
7159
7160/*
7161 * called from commit_transaction. Writes all changed device stats to disk.
7162 */
7163int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7164 struct btrfs_fs_info *fs_info)
7165{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007166 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7167 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007168 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007169 int ret = 0;
7170
7171 mutex_lock(&fs_devices->device_list_mutex);
7172 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007173 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7174 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007175 continue;
7176
Nikolay Borisov9deae962017-10-24 13:47:37 +03007177
7178 /*
7179 * There is a LOAD-LOAD control dependency between the value of
7180 * dev_stats_ccnt and updating the on-disk values which requires
7181 * reading the in-memory counters. Such control dependencies
7182 * require explicit read memory barriers.
7183 *
7184 * This memory barriers pairs with smp_mb__before_atomic in
7185 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7186 * barrier implied by atomic_xchg in
7187 * btrfs_dev_stats_read_and_reset
7188 */
7189 smp_rmb();
7190
Nikolay Borisov5495f192018-07-20 19:37:49 +03007191 ret = update_dev_stat_item(trans, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007192 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007193 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007194 }
7195 mutex_unlock(&fs_devices->device_list_mutex);
7196
7197 return ret;
7198}
7199
Stefan Behrens442a4f62012-05-25 16:06:08 +02007200void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7201{
7202 btrfs_dev_stat_inc(dev, index);
7203 btrfs_dev_stat_print_on_error(dev);
7204}
7205
Eric Sandeen48a3b632013-04-25 20:41:01 +00007206static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007207{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007208 if (!dev->dev_stats_valid)
7209 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007210 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007211 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007212 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007213 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7214 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7215 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007216 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7217 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007218}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007219
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007220static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7221{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007222 int i;
7223
7224 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7225 if (btrfs_dev_stat_read(dev, i) != 0)
7226 break;
7227 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7228 return; /* all values == 0, suppress message */
7229
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007230 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007231 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007232 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007233 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7234 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7235 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7236 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7237 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7238}
7239
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007240int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007241 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007242{
7243 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007244 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007245 int i;
7246
7247 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007248 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007249 mutex_unlock(&fs_devices->device_list_mutex);
7250
7251 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007252 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007253 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007254 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007255 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007256 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007257 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007258 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7259 if (stats->nr_items > i)
7260 stats->values[i] =
7261 btrfs_dev_stat_read_and_reset(dev, i);
7262 else
7263 btrfs_dev_stat_reset(dev, i);
7264 }
7265 } else {
7266 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7267 if (stats->nr_items > i)
7268 stats->values[i] = btrfs_dev_stat_read(dev, i);
7269 }
7270 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7271 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7272 return 0;
7273}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007274
David Sterbada353f62017-02-14 17:55:53 +01007275void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007276{
7277 struct buffer_head *bh;
7278 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007279 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007280
Anand Jain12b1c262015-08-14 18:32:59 +08007281 if (!bdev)
7282 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007283
Anand Jain12b1c262015-08-14 18:32:59 +08007284 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7285 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007286
Anand Jain12b1c262015-08-14 18:32:59 +08007287 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7288 continue;
7289
7290 disk_super = (struct btrfs_super_block *)bh->b_data;
7291
7292 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7293 set_buffer_dirty(bh);
7294 sync_dirty_buffer(bh);
7295 brelse(bh);
7296 }
7297
7298 /* Notify udev that device has changed */
7299 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7300
7301 /* Update ctime/mtime for device path for libblkid */
7302 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007303}
Miao Xie935e5cc2014-09-03 21:35:33 +08007304
7305/*
7306 * Update the size of all devices, which is used for writing out the
7307 * super blocks.
7308 */
7309void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7310{
7311 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7312 struct btrfs_device *curr, *next;
7313
7314 if (list_empty(&fs_devices->resized_devices))
7315 return;
7316
7317 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007318 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007319 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7320 resized_list) {
7321 list_del_init(&curr->resized_list);
7322 curr->commit_total_bytes = curr->disk_total_bytes;
7323 }
David Sterba34441362016-10-04 19:34:27 +02007324 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007325 mutex_unlock(&fs_devices->device_list_mutex);
7326}
Miao Xiece7213c2014-09-03 21:35:34 +08007327
7328/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007329void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007330{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007331 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007332 struct extent_map *em;
7333 struct map_lookup *map;
7334 struct btrfs_device *dev;
7335 int i;
7336
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007337 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007338 return;
7339
7340 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007341 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007342 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007343 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007344
7345 for (i = 0; i < map->num_stripes; i++) {
7346 dev = map->stripes[i].dev;
7347 dev->commit_bytes_used = dev->bytes_used;
7348 }
7349 }
David Sterba34441362016-10-04 19:34:27 +02007350 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007351}
Anand Jain5a13f432015-03-10 06:38:31 +08007352
7353void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7354{
7355 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7356 while (fs_devices) {
7357 fs_devices->fs_info = fs_info;
7358 fs_devices = fs_devices->seed;
7359 }
7360}
7361
7362void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7363{
7364 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7365 while (fs_devices) {
7366 fs_devices->fs_info = NULL;
7367 fs_devices = fs_devices->seed;
7368 }
7369}
David Sterba46df06b2018-07-13 20:46:30 +02007370
7371/*
7372 * Multiplicity factor for simple profiles: DUP, RAID1-like and RAID10.
7373 */
7374int btrfs_bg_type_to_factor(u64 flags)
7375{
7376 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
7377 BTRFS_BLOCK_GROUP_RAID10))
7378 return 2;
7379 return 1;
7380}
Qu Wenruocf90d882018-08-01 10:37:19 +08007381
7382
7383static u64 calc_stripe_length(u64 type, u64 chunk_len, int num_stripes)
7384{
7385 int index = btrfs_bg_flags_to_raid_index(type);
7386 int ncopies = btrfs_raid_array[index].ncopies;
7387 int data_stripes;
7388
7389 switch (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
7390 case BTRFS_BLOCK_GROUP_RAID5:
7391 data_stripes = num_stripes - 1;
7392 break;
7393 case BTRFS_BLOCK_GROUP_RAID6:
7394 data_stripes = num_stripes - 2;
7395 break;
7396 default:
7397 data_stripes = num_stripes / ncopies;
7398 break;
7399 }
7400 return div_u64(chunk_len, data_stripes);
7401}
7402
7403static int verify_one_dev_extent(struct btrfs_fs_info *fs_info,
7404 u64 chunk_offset, u64 devid,
7405 u64 physical_offset, u64 physical_len)
7406{
7407 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7408 struct extent_map *em;
7409 struct map_lookup *map;
Qu Wenruo05a37c42018-10-05 17:45:55 +08007410 struct btrfs_device *dev;
Qu Wenruocf90d882018-08-01 10:37:19 +08007411 u64 stripe_len;
7412 bool found = false;
7413 int ret = 0;
7414 int i;
7415
7416 read_lock(&em_tree->lock);
7417 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
7418 read_unlock(&em_tree->lock);
7419
7420 if (!em) {
7421 btrfs_err(fs_info,
7422"dev extent physical offset %llu on devid %llu doesn't have corresponding chunk",
7423 physical_offset, devid);
7424 ret = -EUCLEAN;
7425 goto out;
7426 }
7427
7428 map = em->map_lookup;
7429 stripe_len = calc_stripe_length(map->type, em->len, map->num_stripes);
7430 if (physical_len != stripe_len) {
7431 btrfs_err(fs_info,
7432"dev extent physical offset %llu on devid %llu length doesn't match chunk %llu, have %llu expect %llu",
7433 physical_offset, devid, em->start, physical_len,
7434 stripe_len);
7435 ret = -EUCLEAN;
7436 goto out;
7437 }
7438
7439 for (i = 0; i < map->num_stripes; i++) {
7440 if (map->stripes[i].dev->devid == devid &&
7441 map->stripes[i].physical == physical_offset) {
7442 found = true;
7443 if (map->verified_stripes >= map->num_stripes) {
7444 btrfs_err(fs_info,
7445 "too many dev extents for chunk %llu found",
7446 em->start);
7447 ret = -EUCLEAN;
7448 goto out;
7449 }
7450 map->verified_stripes++;
7451 break;
7452 }
7453 }
7454 if (!found) {
7455 btrfs_err(fs_info,
7456 "dev extent physical offset %llu devid %llu has no corresponding chunk",
7457 physical_offset, devid);
7458 ret = -EUCLEAN;
7459 }
Qu Wenruo05a37c42018-10-05 17:45:55 +08007460
7461 /* Make sure no dev extent is beyond device bondary */
7462 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
7463 if (!dev) {
7464 btrfs_err(fs_info, "failed to find devid %llu", devid);
7465 ret = -EUCLEAN;
7466 goto out;
7467 }
7468 if (physical_offset + physical_len > dev->disk_total_bytes) {
7469 btrfs_err(fs_info,
7470"dev extent devid %llu physical offset %llu len %llu is beyond device boundary %llu",
7471 devid, physical_offset, physical_len,
7472 dev->disk_total_bytes);
7473 ret = -EUCLEAN;
7474 goto out;
7475 }
Qu Wenruocf90d882018-08-01 10:37:19 +08007476out:
7477 free_extent_map(em);
7478 return ret;
7479}
7480
7481static int verify_chunk_dev_extent_mapping(struct btrfs_fs_info *fs_info)
7482{
7483 struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree;
7484 struct extent_map *em;
7485 struct rb_node *node;
7486 int ret = 0;
7487
7488 read_lock(&em_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08007489 for (node = rb_first_cached(&em_tree->map); node; node = rb_next(node)) {
Qu Wenruocf90d882018-08-01 10:37:19 +08007490 em = rb_entry(node, struct extent_map, rb_node);
7491 if (em->map_lookup->num_stripes !=
7492 em->map_lookup->verified_stripes) {
7493 btrfs_err(fs_info,
7494 "chunk %llu has missing dev extent, have %d expect %d",
7495 em->start, em->map_lookup->verified_stripes,
7496 em->map_lookup->num_stripes);
7497 ret = -EUCLEAN;
7498 goto out;
7499 }
7500 }
7501out:
7502 read_unlock(&em_tree->lock);
7503 return ret;
7504}
7505
7506/*
7507 * Ensure that all dev extents are mapped to correct chunk, otherwise
7508 * later chunk allocation/free would cause unexpected behavior.
7509 *
7510 * NOTE: This will iterate through the whole device tree, which should be of
7511 * the same size level as the chunk tree. This slightly increases mount time.
7512 */
7513int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info)
7514{
7515 struct btrfs_path *path;
7516 struct btrfs_root *root = fs_info->dev_root;
7517 struct btrfs_key key;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007518 u64 prev_devid = 0;
7519 u64 prev_dev_ext_end = 0;
Qu Wenruocf90d882018-08-01 10:37:19 +08007520 int ret = 0;
7521
7522 key.objectid = 1;
7523 key.type = BTRFS_DEV_EXTENT_KEY;
7524 key.offset = 0;
7525
7526 path = btrfs_alloc_path();
7527 if (!path)
7528 return -ENOMEM;
7529
7530 path->reada = READA_FORWARD;
7531 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7532 if (ret < 0)
7533 goto out;
7534
7535 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
7536 ret = btrfs_next_item(root, path);
7537 if (ret < 0)
7538 goto out;
7539 /* No dev extents at all? Not good */
7540 if (ret > 0) {
7541 ret = -EUCLEAN;
7542 goto out;
7543 }
7544 }
7545 while (1) {
7546 struct extent_buffer *leaf = path->nodes[0];
7547 struct btrfs_dev_extent *dext;
7548 int slot = path->slots[0];
7549 u64 chunk_offset;
7550 u64 physical_offset;
7551 u64 physical_len;
7552 u64 devid;
7553
7554 btrfs_item_key_to_cpu(leaf, &key, slot);
7555 if (key.type != BTRFS_DEV_EXTENT_KEY)
7556 break;
7557 devid = key.objectid;
7558 physical_offset = key.offset;
7559
7560 dext = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
7561 chunk_offset = btrfs_dev_extent_chunk_offset(leaf, dext);
7562 physical_len = btrfs_dev_extent_length(leaf, dext);
7563
Qu Wenruo5eb19382018-10-05 17:45:54 +08007564 /* Check if this dev extent overlaps with the previous one */
7565 if (devid == prev_devid && physical_offset < prev_dev_ext_end) {
7566 btrfs_err(fs_info,
7567"dev extent devid %llu physical offset %llu overlap with previous dev extent end %llu",
7568 devid, physical_offset, prev_dev_ext_end);
7569 ret = -EUCLEAN;
7570 goto out;
7571 }
7572
Qu Wenruocf90d882018-08-01 10:37:19 +08007573 ret = verify_one_dev_extent(fs_info, chunk_offset, devid,
7574 physical_offset, physical_len);
7575 if (ret < 0)
7576 goto out;
Qu Wenruo5eb19382018-10-05 17:45:54 +08007577 prev_devid = devid;
7578 prev_dev_ext_end = physical_offset + physical_len;
7579
Qu Wenruocf90d882018-08-01 10:37:19 +08007580 ret = btrfs_next_item(root, path);
7581 if (ret < 0)
7582 goto out;
7583 if (ret > 0) {
7584 ret = 0;
7585 break;
7586 }
7587 }
7588
7589 /* Ensure all chunks have corresponding dev extents */
7590 ret = verify_chunk_dev_extent_mapping(fs_info);
7591out:
7592 btrfs_free_path(path);
7593 return ret;
7594}
Omar Sandovaleede2bf2016-11-03 10:28:12 -07007595
7596/*
7597 * Check whether the given block group or device is pinned by any inode being
7598 * used as a swapfile.
7599 */
7600bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr)
7601{
7602 struct btrfs_swapfile_pin *sp;
7603 struct rb_node *node;
7604
7605 spin_lock(&fs_info->swapfile_pins_lock);
7606 node = fs_info->swapfile_pins.rb_node;
7607 while (node) {
7608 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
7609 if (ptr < sp->ptr)
7610 node = node->rb_left;
7611 else if (ptr > sp->ptr)
7612 node = node->rb_right;
7613 else
7614 break;
7615 }
7616 spin_unlock(&fs_info->swapfile_pins_lock);
7617 return node != NULL;
7618}