blob: 0216bc86f4762ee1c6baa05e06ef4390ecf2a73b [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>
Chris Masonb765ead2009-04-03 10:27:10 -040011#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000012#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020013#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020014#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050015#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020016#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070017#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080018#include <linux/list_sort.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050019#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040020#include "ctree.h"
21#include "extent_map.h"
22#include "disk-io.h"
23#include "transaction.h"
24#include "print-tree.h"
25#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050026#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040027#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010028#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040029#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060030#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010031#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080032#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033
Zhao Leiaf902042015-09-15 21:08:06 +080034const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
35 [BTRFS_RAID_RAID10] = {
36 .sub_stripes = 2,
37 .dev_stripes = 1,
38 .devs_max = 0, /* 0 == as many as possible */
39 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080040 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080041 .devs_increment = 2,
42 .ncopies = 2,
43 },
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,
52 },
53 [BTRFS_RAID_DUP] = {
54 .sub_stripes = 1,
55 .dev_stripes = 2,
56 .devs_max = 1,
57 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080058 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080059 .devs_increment = 1,
60 .ncopies = 2,
61 },
62 [BTRFS_RAID_RAID0] = {
63 .sub_stripes = 1,
64 .dev_stripes = 1,
65 .devs_max = 0,
66 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080067 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080068 .devs_increment = 1,
69 .ncopies = 1,
70 },
71 [BTRFS_RAID_SINGLE] = {
72 .sub_stripes = 1,
73 .dev_stripes = 1,
74 .devs_max = 1,
75 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080076 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080077 .devs_increment = 1,
78 .ncopies = 1,
79 },
80 [BTRFS_RAID_RAID5] = {
81 .sub_stripes = 1,
82 .dev_stripes = 1,
83 .devs_max = 0,
84 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080085 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080086 .devs_increment = 1,
87 .ncopies = 2,
88 },
89 [BTRFS_RAID_RAID6] = {
90 .sub_stripes = 1,
91 .dev_stripes = 1,
92 .devs_max = 0,
93 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +080094 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +080095 .devs_increment = 1,
96 .ncopies = 3,
97 },
98};
99
Colin Ian Kingfb75d852016-01-19 00:05:28 +0000100const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
Zhao Leiaf902042015-09-15 21:08:06 +0800101 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
102 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
103 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
104 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
105 [BTRFS_RAID_SINGLE] = 0,
106 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
107 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
108};
109
David Sterba621292b2016-02-15 16:28:03 +0100110/*
111 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
112 * condition is not met. Zero means there's no corresponding
113 * BTRFS_ERROR_DEV_*_NOT_MET value.
114 */
115const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
116 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
117 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
118 [BTRFS_RAID_DUP] = 0,
119 [BTRFS_RAID_RAID0] = 0,
120 [BTRFS_RAID_SINGLE] = 0,
121 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
122 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
123};
124
Yan Zheng2b820322008-11-17 21:11:30 -0500125static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100126 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400127static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200128static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000129static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200130static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700131static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
132 enum btrfs_map_op op,
133 u64 logical, u64 *length,
134 struct btrfs_bio **bbio_ret,
135 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500136
David Sterba9c6b1c42017-06-16 22:30:00 +0200137/*
138 * Device locking
139 * ==============
140 *
141 * There are several mutexes that protect manipulation of devices and low-level
142 * structures like chunks but not block groups, extents or files
143 *
144 * uuid_mutex (global lock)
145 * ------------------------
146 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
147 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
148 * device) or requested by the device= mount option
149 *
150 * the mutex can be very coarse and can cover long-running operations
151 *
152 * protects: updates to fs_devices counters like missing devices, rw devices,
153 * seeding, structure cloning, openning/closing devices at mount/umount time
154 *
155 * global::fs_devs - add, remove, updates to the global list
156 *
157 * does not protect: manipulation of the fs_devices::devices list!
158 *
159 * btrfs_device::name - renames (write side), read is RCU
160 *
161 * fs_devices::device_list_mutex (per-fs, with RCU)
162 * ------------------------------------------------
163 * protects updates to fs_devices::devices, ie. adding and deleting
164 *
165 * simple list traversal with read-only actions can be done with RCU protection
166 *
167 * may be used to exclude some operations from running concurrently without any
168 * modifications to the list (see write_all_supers)
169 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200170 * balance_mutex
171 * -------------
172 * protects balance structures (status, state) and context accessed from
173 * several places (internally, ioctl)
174 *
175 * chunk_mutex
176 * -----------
177 * protects chunks, adding or removing during allocation, trim or when a new
178 * device is added/removed
179 *
180 * cleaner_mutex
181 * -------------
182 * a big lock that is held by the cleaner thread and prevents running subvolume
183 * cleaning together with relocation or delayed iputs
184 *
185 *
186 * Lock nesting
187 * ============
188 *
189 * uuid_mutex
190 * volume_mutex
191 * device_list_mutex
192 * chunk_mutex
193 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800194 *
195 *
196 * Exclusive operations, BTRFS_FS_EXCL_OP
197 * ======================================
198 *
199 * Maintains the exclusivity of the following operations that apply to the
200 * whole filesystem and cannot run in parallel.
201 *
202 * - Balance (*)
203 * - Device add
204 * - Device remove
205 * - Device replace (*)
206 * - Resize
207 *
208 * The device operations (as above) can be in one of the following states:
209 *
210 * - Running state
211 * - Paused state
212 * - Completed state
213 *
214 * Only device operations marked with (*) can go into the Paused state for the
215 * following reasons:
216 *
217 * - ioctl (only Balance can be Paused through ioctl)
218 * - filesystem remounted as read-only
219 * - filesystem unmounted and mounted as read-only
220 * - system power-cycle and filesystem mounted as read-only
221 * - filesystem or device errors leading to forced read-only
222 *
223 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
224 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
225 * A device operation in Paused or Running state can be canceled or resumed
226 * either by ioctl (Balance only) or when remounted as read-write.
227 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
228 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200229 */
230
Miao Xie67a2c452014-09-03 21:35:43 +0800231DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400232static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800233struct list_head *btrfs_get_fs_uuids(void)
234{
235 return &fs_uuids;
236}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400237
David Sterba2dfeca92017-06-14 02:48:07 +0200238/*
239 * alloc_fs_devices - allocate struct btrfs_fs_devices
240 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
241 *
242 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
243 * The returned struct is not linked onto any lists and can be destroyed with
244 * kfree() right away.
245 */
246static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300247{
248 struct btrfs_fs_devices *fs_devs;
249
David Sterba78f2c9e2016-02-11 14:25:38 +0100250 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300251 if (!fs_devs)
252 return ERR_PTR(-ENOMEM);
253
254 mutex_init(&fs_devs->device_list_mutex);
255
256 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800257 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300258 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800259 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300260 if (fsid)
261 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300262
263 return fs_devs;
264}
265
David Sterbaa425f9d2018-03-20 15:47:33 +0100266void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100267{
268 rcu_string_free(device->name);
269 bio_put(device->flush_bio);
270 kfree(device);
271}
272
Yan Zhenge4404d62008-12-12 10:03:26 -0500273static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
274{
275 struct btrfs_device *device;
276 WARN_ON(fs_devices->opened);
277 while (!list_empty(&fs_devices->devices)) {
278 device = list_entry(fs_devices->devices.next,
279 struct btrfs_device, dev_list);
280 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100281 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500282 }
283 kfree(fs_devices);
284}
285
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000286static void btrfs_kobject_uevent(struct block_device *bdev,
287 enum kobject_action action)
288{
289 int ret;
290
291 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
292 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500293 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000294 action,
295 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
296 &disk_to_dev(bdev->bd_disk)->kobj);
297}
298
David Sterbaffc5a372018-02-19 17:24:15 +0100299void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400300{
301 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400302
Yan Zheng2b820322008-11-17 21:11:30 -0500303 while (!list_empty(&fs_uuids)) {
304 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800305 struct btrfs_fs_devices, fs_list);
306 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500307 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400308 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400309}
310
David Sterba48dae9c2017-10-30 18:10:25 +0100311/*
312 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
313 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100314 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100315 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300316static struct btrfs_device *__alloc_device(void)
317{
318 struct btrfs_device *dev;
319
David Sterba78f2c9e2016-02-11 14:25:38 +0100320 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300321 if (!dev)
322 return ERR_PTR(-ENOMEM);
323
David Sterbae0ae9992017-06-06 17:06:06 +0200324 /*
325 * Preallocate a bio that's always going to be used for flushing device
326 * barriers and matches the device lifespan
327 */
328 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
329 if (!dev->flush_bio) {
330 kfree(dev);
331 return ERR_PTR(-ENOMEM);
332 }
David Sterbae0ae9992017-06-06 17:06:06 +0200333
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300334 INIT_LIST_HEAD(&dev->dev_list);
335 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800336 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300337
338 spin_lock_init(&dev->io_lock);
339
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300340 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800341 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100342 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700343 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800344 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300345
346 return dev;
347}
348
David Sterba35c70102017-06-15 19:51:51 +0200349/*
350 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
351 * return NULL.
352 *
353 * If devid and uuid are both specified, the match must be exact, otherwise
354 * only devid is used.
355 */
356static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
357 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400358{
359 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400360
Anand Jain636d2c92018-04-12 10:29:29 +0800361 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400362 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400363 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400364 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400365 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400366 }
367 return NULL;
368}
369
Chris Masona1b32a52008-09-05 16:09:51 -0400370static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400371{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400372 struct btrfs_fs_devices *fs_devices;
373
Anand Jainc4babc52018-04-12 10:29:25 +0800374 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
376 return fs_devices;
377 }
378 return NULL;
379}
380
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100381static int
382btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
383 int flush, struct block_device **bdev,
384 struct buffer_head **bh)
385{
386 int ret;
387
388 *bdev = blkdev_get_by_path(device_path, flags, holder);
389
390 if (IS_ERR(*bdev)) {
391 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100392 goto error;
393 }
394
395 if (flush)
396 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200397 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100398 if (ret) {
399 blkdev_put(*bdev, flags);
400 goto error;
401 }
402 invalidate_bdev(*bdev);
403 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800404 if (IS_ERR(*bh)) {
405 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100406 blkdev_put(*bdev, flags);
407 goto error;
408 }
409
410 return 0;
411
412error:
413 *bdev = NULL;
414 *bh = NULL;
415 return ret;
416}
417
Chris Masonffbd5172009-04-20 15:50:09 -0400418static void requeue_list(struct btrfs_pending_bios *pending_bios,
419 struct bio *head, struct bio *tail)
420{
421
422 struct bio *old_head;
423
424 old_head = pending_bios->head;
425 pending_bios->head = head;
426 if (pending_bios->tail)
427 tail->bi_next = old_head;
428 else
429 pending_bios->tail = tail;
430}
431
Chris Mason8b712842008-06-11 16:50:36 -0400432/*
433 * we try to collect pending bios for a device so we don't get a large
434 * number of procs sending bios down to the same device. This greatly
435 * improves the schedulers ability to collect and merge the bios.
436 *
437 * But, it also turns into a long list of bios to process and that is sure
438 * to eventually make the worker thread block. The solution here is to
439 * make some progress and then put this work struct back at the end of
440 * the list if the block device is congested. This way, multiple devices
441 * can make progress from a single worker thread.
442 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100443static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400445 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400446 struct bio *pending;
447 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400448 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400449 struct bio *tail;
450 struct bio *cur;
451 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400452 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400453 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400454 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400455 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000456 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400457 struct blk_plug plug;
458
459 /*
460 * this function runs all the bios we've collected for
461 * a particular device. We don't want to wander off to
462 * another device without first sending all of these down.
463 * So, setup a plug here and finish it off before we return
464 */
465 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400466
Jan Karaefa7c9f2017-02-02 15:56:53 +0100467 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400468
Chris Mason8b712842008-06-11 16:50:36 -0400469loop:
470 spin_lock(&device->io_lock);
471
Chris Masona6837052009-02-04 09:19:41 -0500472loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400473 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400474
Chris Mason8b712842008-06-11 16:50:36 -0400475 /* take all the bios off the list at once and process them
476 * later on (without the lock held). But, remember the
477 * tail and other pointers so the bios can be properly reinserted
478 * into the list if we hit congestion
479 */
Chris Masond84275c2009-06-09 15:39:08 -0400480 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400481 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400482 force_reg = 1;
483 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400484 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400485 force_reg = 0;
486 }
Chris Masonffbd5172009-04-20 15:50:09 -0400487
488 pending = pending_bios->head;
489 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400490 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400491
492 /*
493 * if pending was null this time around, no bios need processing
494 * at all and we can stop. Otherwise it'll loop back up again
495 * and do an additional check so no bios are missed.
496 *
497 * device->running_pending is used to synchronize with the
498 * schedule_bio code.
499 */
Chris Masonffbd5172009-04-20 15:50:09 -0400500 if (device->pending_sync_bios.head == NULL &&
501 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400502 again = 0;
503 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400504 } else {
505 again = 1;
506 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400507 }
Chris Masonffbd5172009-04-20 15:50:09 -0400508
509 pending_bios->head = NULL;
510 pending_bios->tail = NULL;
511
Chris Mason8b712842008-06-11 16:50:36 -0400512 spin_unlock(&device->io_lock);
513
Chris Masond3977122009-01-05 21:25:51 -0500514 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400515
516 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400517 /* we want to work on both lists, but do more bios on the
518 * sync list than the regular list
519 */
520 if ((num_run > 32 &&
521 pending_bios != &device->pending_sync_bios &&
522 device->pending_sync_bios.head) ||
523 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
524 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400525 spin_lock(&device->io_lock);
526 requeue_list(pending_bios, pending, tail);
527 goto loop_lock;
528 }
529
Chris Mason8b712842008-06-11 16:50:36 -0400530 cur = pending;
531 pending = pending->bi_next;
532 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400533
Jens Axboedac56212015-04-17 16:23:59 -0600534 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400535
Chris Mason2ab1ba62011-08-04 14:28:36 -0400536 /*
537 * if we're doing the sync list, record that our
538 * plug has some sync requests on it
539 *
540 * If we're doing the regular list and there are
541 * sync requests sitting around, unplug before
542 * we add more
543 */
544 if (pending_bios == &device->pending_sync_bios) {
545 sync_pending = 1;
546 } else if (sync_pending) {
547 blk_finish_plug(&plug);
548 blk_start_plug(&plug);
549 sync_pending = 0;
550 }
551
Mike Christie4e49ea42016-06-05 14:31:41 -0500552 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400553 num_run++;
554 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100555
556 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400557
558 /*
559 * we made progress, there is more work to do and the bdi
560 * is now congested. Back off and let other work structs
561 * run instead
562 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400563 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500564 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400565 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400566
Chris Masonb765ead2009-04-03 10:27:10 -0400567 ioc = current->io_context;
568
569 /*
570 * the main goal here is that we don't want to
571 * block if we're going to be able to submit
572 * more requests without blocking.
573 *
574 * This code does two great things, it pokes into
575 * the elevator code from a filesystem _and_
576 * it makes assumptions about how batching works.
577 */
578 if (ioc && ioc->nr_batch_requests > 0 &&
579 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
580 (last_waited == 0 ||
581 ioc->last_waited == last_waited)) {
582 /*
583 * we want to go through our batch of
584 * requests and stop. So, we copy out
585 * the ioc->last_waited time and test
586 * against it before looping
587 */
588 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100589 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400590 continue;
591 }
Chris Mason8b712842008-06-11 16:50:36 -0400592 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400593 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500594 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400595
596 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800597 btrfs_queue_work(fs_info->submit_workers,
598 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400599 goto done;
600 }
601 }
Chris Masonffbd5172009-04-20 15:50:09 -0400602
Chris Mason51684082010-03-10 15:33:32 -0500603 cond_resched();
604 if (again)
605 goto loop;
606
607 spin_lock(&device->io_lock);
608 if (device->pending_bios.head || device->pending_sync_bios.head)
609 goto loop_lock;
610 spin_unlock(&device->io_lock);
611
Chris Mason8b712842008-06-11 16:50:36 -0400612done:
Chris Mason211588a2011-04-19 20:12:40 -0400613 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400614}
615
Christoph Hellwigb2950862008-12-02 09:54:17 -0500616static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400617{
618 struct btrfs_device *device;
619
620 device = container_of(work, struct btrfs_device, work);
621 run_scheduled_bios(device);
622}
623
Anand Jaind8367db2018-01-18 22:00:37 +0800624/*
625 * Search and remove all stale (devices which are not mounted) devices.
626 * When both inputs are NULL, it will search and release all stale devices.
627 * path: Optional. When provided will it release all unmounted devices
628 * matching this path only.
629 * skip_dev: Optional. Will skip this device when searching for the stale
630 * devices.
631 */
632static void btrfs_free_stale_devices(const char *path,
633 struct btrfs_device *skip_dev)
Anand Jain4fde46f2015-06-17 21:10:48 +0800634{
Anand Jain38cf6652018-01-18 22:00:34 +0800635 struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
636 struct btrfs_device *dev, *tmp_dev;
Anand Jain4fde46f2015-06-17 21:10:48 +0800637
Anand Jainc4babc52018-04-12 10:29:25 +0800638 list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800639
640 if (fs_devs->opened)
641 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800642
Anand Jain38cf6652018-01-18 22:00:34 +0800643 list_for_each_entry_safe(dev, tmp_dev,
644 &fs_devs->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800645 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800646
Anand Jaind8367db2018-01-18 22:00:37 +0800647 if (skip_dev && skip_dev == dev)
648 continue;
649 if (path && !dev->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800650 continue;
651
Anand Jain4fde46f2015-06-17 21:10:48 +0800652 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800653 if (path)
Anand Jain522f1b42018-01-18 22:00:35 +0800654 not_found = strcmp(rcu_str_deref(dev->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800655 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800656 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800657 if (not_found)
658 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800659
Anand Jain4fde46f2015-06-17 21:10:48 +0800660 /* delete the stale device */
661 if (fs_devs->num_devices == 1) {
662 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainc4babc52018-04-12 10:29:25 +0800663 list_del(&fs_devs->fs_list);
Anand Jain4fde46f2015-06-17 21:10:48 +0800664 free_fs_devices(fs_devs);
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200665 break;
Anand Jain4fde46f2015-06-17 21:10:48 +0800666 } else {
667 fs_devs->num_devices--;
668 list_del(&dev->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100669 btrfs_free_device(dev);
Anand Jain4fde46f2015-06-17 21:10:48 +0800670 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800671 }
672 }
673}
674
Anand Jain0fb08bc2017-11-09 23:45:24 +0800675static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
676 struct btrfs_device *device, fmode_t flags,
677 void *holder)
678{
679 struct request_queue *q;
680 struct block_device *bdev;
681 struct buffer_head *bh;
682 struct btrfs_super_block *disk_super;
683 u64 devid;
684 int ret;
685
686 if (device->bdev)
687 return -EINVAL;
688 if (!device->name)
689 return -EINVAL;
690
691 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
692 &bdev, &bh);
693 if (ret)
694 return ret;
695
696 disk_super = (struct btrfs_super_block *)bh->b_data;
697 devid = btrfs_stack_device_id(&disk_super->dev_item);
698 if (devid != device->devid)
699 goto error_brelse;
700
701 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
702 goto error_brelse;
703
704 device->generation = btrfs_super_generation(disk_super);
705
706 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Anand Jainebbede42017-12-04 12:54:52 +0800707 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800708 fs_devices->seeding = 1;
709 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800710 if (bdev_read_only(bdev))
711 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
712 else
713 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800714 }
715
716 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800717 if (!blk_queue_nonrot(q))
718 fs_devices->rotating = 1;
719
720 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800721 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800722 device->mode = flags;
723
724 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800725 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
726 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800727 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800728 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800729 }
730 brelse(bh);
731
732 return 0;
733
734error_brelse:
735 brelse(bh);
736 blkdev_put(bdev, flags);
737
738 return -EINVAL;
739}
740
David Sterba60999ca2014-03-26 18:26:36 +0100741/*
742 * Add new device to list of registered devices
743 *
744 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800745 * device pointer which was just added or updated when successful
746 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100747 */
Anand Jaine124ece2018-01-18 22:02:35 +0800748static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain3acbcbf2018-01-18 22:02:36 +0800749 struct btrfs_super_block *disk_super)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400750{
751 struct btrfs_device *device;
752 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400753 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800755 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756
757 fs_devices = find_fsid(disk_super->fsid);
758 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300759 fs_devices = alloc_fs_devices(disk_super->fsid);
760 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800761 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300762
Anand Jainc4babc52018-04-12 10:29:25 +0800763 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300764
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 device = NULL;
766 } else {
David Sterba35c70102017-06-15 19:51:51 +0200767 device = find_device(fs_devices, devid,
768 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400769 }
Miao Xie443f24f2014-07-24 11:37:15 +0800770
Chris Mason8a4b83c2008-03-24 15:02:07 -0400771 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500772 if (fs_devices->opened)
Anand Jaine124ece2018-01-18 22:02:35 +0800773 return ERR_PTR(-EBUSY);
Yan Zheng2b820322008-11-17 21:11:30 -0500774
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300775 device = btrfs_alloc_device(NULL, &devid,
776 disk_super->dev_item.uuid);
777 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400778 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800779 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400780 }
Josef Bacik606686e2012-06-04 14:03:51 -0400781
782 name = rcu_string_strdup(path, GFP_NOFS);
783 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100784 btrfs_free_device(device);
Anand Jaine124ece2018-01-18 22:02:35 +0800785 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400786 }
Josef Bacik606686e2012-06-04 14:03:51 -0400787 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200788
Chris Masone5e9a522009-06-10 15:17:02 -0400789 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000790 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100791 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400792 mutex_unlock(&fs_devices->device_list_mutex);
793
Yan Zheng2b820322008-11-17 21:11:30 -0500794 device->fs_devices = fs_devices;
Anand Jaind8367db2018-01-18 22:00:37 +0800795 btrfs_free_stale_devices(path, device);
Anand Jain327f18c2018-01-18 22:02:33 +0800796
797 if (disk_super->label[0])
798 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
799 disk_super->label, devid, found_transid, path);
800 else
801 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
802 disk_super->fsid, devid, found_transid, path);
803
Josef Bacik606686e2012-06-04 14:03:51 -0400804 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800805 /*
806 * When FS is already mounted.
807 * 1. If you are here and if the device->name is NULL that
808 * means this device was missing at time of FS mount.
809 * 2. If you are here and if the device->name is different
810 * from 'path' that means either
811 * a. The same device disappeared and reappeared with
812 * different name. or
813 * b. The missing-disk-which-was-replaced, has
814 * reappeared now.
815 *
816 * We must allow 1 and 2a above. But 2b would be a spurious
817 * and unintentional.
818 *
819 * Further in case of 1 and 2a above, the disk at 'path'
820 * would have missed some transaction when it was away and
821 * in case of 2a the stale bdev has to be updated as well.
822 * 2b must not be allowed at all time.
823 */
824
825 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700826 * For now, we do allow update to btrfs_fs_device through the
827 * btrfs dev scan cli after FS has been mounted. We're still
828 * tracking a problem where systems fail mount by subvolume id
829 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800830 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700831 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800832 /*
833 * That is if the FS is _not_ mounted and if you
834 * are here, that means there is more than one
835 * disk with same uuid and devid.We keep the one
836 * with larger generation number or the last-in if
837 * generation are equal.
838 */
Anand Jaine124ece2018-01-18 22:02:35 +0800839 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800840 }
Anand Jainb96de002014-07-03 18:22:05 +0800841
Josef Bacik606686e2012-06-04 14:03:51 -0400842 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000843 if (!name)
Anand Jaine124ece2018-01-18 22:02:35 +0800844 return ERR_PTR(-ENOMEM);
Josef Bacik606686e2012-06-04 14:03:51 -0400845 rcu_string_free(device->name);
846 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800847 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500848 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800849 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500850 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400851 }
852
Anand Jain77bdae42014-07-03 18:22:06 +0800853 /*
854 * Unmount does not free the btrfs_device struct but would zero
855 * generation along with most of the other members. So just update
856 * it back. We need it to pick the disk with largest generation
857 * (as above).
858 */
859 if (!fs_devices->opened)
860 device->generation = found_transid;
861
Anand Jainf2788d22018-01-18 22:02:34 +0800862 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
863
Anand Jaine124ece2018-01-18 22:02:35 +0800864 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400865}
866
Yan Zhenge4404d62008-12-12 10:03:26 -0500867static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
868{
869 struct btrfs_fs_devices *fs_devices;
870 struct btrfs_device *device;
871 struct btrfs_device *orig_dev;
872
Ilya Dryomov2208a372013-08-12 14:33:03 +0300873 fs_devices = alloc_fs_devices(orig->fsid);
874 if (IS_ERR(fs_devices))
875 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500876
Miao Xieadbbb862014-09-03 21:35:42 +0800877 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400878 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500879
Xiao Guangrong46224702011-04-20 10:08:47 +0000880 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500881 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400882 struct rcu_string *name;
883
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300884 device = btrfs_alloc_device(NULL, &orig_dev->devid,
885 orig_dev->uuid);
886 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500887 goto error;
888
Josef Bacik606686e2012-06-04 14:03:51 -0400889 /*
890 * This is ok to do without rcu read locked because we hold the
891 * uuid mutex so nothing we touch in here is going to disappear.
892 */
Anand Jaine755f782014-06-30 17:12:47 +0800893 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100894 name = rcu_string_strdup(orig_dev->name->str,
895 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800896 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100897 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800898 goto error;
899 }
900 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400901 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500902
Yan Zhenge4404d62008-12-12 10:03:26 -0500903 list_add(&device->dev_list, &fs_devices->devices);
904 device->fs_devices = fs_devices;
905 fs_devices->num_devices++;
906 }
Miao Xieadbbb862014-09-03 21:35:42 +0800907 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500908 return fs_devices;
909error:
Miao Xieadbbb862014-09-03 21:35:42 +0800910 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500911 free_fs_devices(fs_devices);
912 return ERR_PTR(-ENOMEM);
913}
914
Anand Jain9b99b112018-02-27 12:41:59 +0800915/*
916 * After we have read the system tree and know devids belonging to
917 * this filesystem, remove the device which does not belong there.
918 */
919void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400920{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500921 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800922 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500923
Chris Masondfe25022008-05-13 13:46:40 -0400924 mutex_lock(&uuid_mutex);
925again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000926 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500927 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +0800928 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
929 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +0800930 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
931 &device->dev_state) &&
932 (!latest_dev ||
933 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +0800934 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500935 }
Yan Zheng2b820322008-11-17 21:11:30 -0500936 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500937 }
Yan Zheng2b820322008-11-17 21:11:30 -0500938
Stefan Behrens8dabb742012-11-06 13:15:27 +0100939 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
940 /*
941 * In the first step, keep the device which has
942 * the correct fsid and the devid that is used
943 * for the dev_replace procedure.
944 * In the second step, the dev_replace state is
945 * read from the device tree and it is known
946 * whether the procedure is really active or
947 * not, which means whether this device is
948 * used or whether it should be removed.
949 */
Anand Jain401e29c2017-12-04 12:54:55 +0800950 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
951 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +0100952 continue;
953 }
954 }
Yan Zheng2b820322008-11-17 21:11:30 -0500955 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100956 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500957 device->bdev = NULL;
958 fs_devices->open_devices--;
959 }
Anand Jainebbede42017-12-04 12:54:52 +0800960 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500961 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +0800962 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +0800963 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
964 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +0100965 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500966 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500967 list_del_init(&device->dev_list);
968 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +0100969 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400970 }
Yan Zheng2b820322008-11-17 21:11:30 -0500971
972 if (fs_devices->seed) {
973 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500974 goto again;
975 }
976
Miao Xie443f24f2014-07-24 11:37:15 +0800977 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500978
Chris Masondfe25022008-05-13 13:46:40 -0400979 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400980}
Chris Masona0af4692008-05-13 16:03:06 -0400981
David Sterbaf06c5962017-06-06 17:08:23 +0200982static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000983{
984 struct btrfs_device *device;
985
986 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +0100987 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000988}
989
Anand Jain14238812016-07-22 06:04:53 +0800990static void btrfs_close_bdev(struct btrfs_device *device)
991{
David Sterba08ffcae2017-06-19 16:55:35 +0200992 if (!device->bdev)
993 return;
994
Anand Jainebbede42017-12-04 12:54:52 +0800995 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +0800996 sync_blockdev(device->bdev);
997 invalidate_bdev(device->bdev);
998 }
999
David Sterba08ffcae2017-06-19 16:55:35 +02001000 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001001}
1002
Anand Jain0ccd0522016-09-22 12:56:13 +08001003static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001004{
1005 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1006 struct btrfs_device *new_device;
1007 struct rcu_string *name;
1008
1009 if (device->bdev)
1010 fs_devices->open_devices--;
1011
Anand Jainebbede42017-12-04 12:54:52 +08001012 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001013 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1014 list_del_init(&device->dev_alloc_list);
1015 fs_devices->rw_devices--;
1016 }
1017
Anand Jaine6e674b2017-12-04 12:54:54 +08001018 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001019 fs_devices->missing_devices--;
1020
1021 new_device = btrfs_alloc_device(NULL, &device->devid,
1022 device->uuid);
1023 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1024
1025 /* Safe because we are under uuid_mutex */
1026 if (device->name) {
1027 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1028 BUG_ON(!name); /* -ENOMEM */
1029 rcu_assign_pointer(new_device->name, name);
1030 }
1031
1032 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1033 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001034}
1035
Anand Jain0226e0e2018-04-12 10:29:27 +08001036static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001037{
Sasha Levin2037a092015-05-12 19:31:37 -04001038 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +08001039 struct list_head pending_put;
1040
1041 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -05001042
Yan Zheng2b820322008-11-17 21:11:30 -05001043 if (--fs_devices->opened > 0)
1044 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001045
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001046 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001047 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +08001048 btrfs_prepare_close_one_device(device);
1049 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001050 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001051 mutex_unlock(&fs_devices->device_list_mutex);
1052
Anand Jain0ccd0522016-09-22 12:56:13 +08001053 /*
1054 * btrfs_show_devname() is using the device_list_mutex,
1055 * sometimes call to blkdev_put() leads vfs calling
1056 * into this func. So do put outside of device_list_mutex,
1057 * as of now.
1058 */
1059 while (!list_empty(&pending_put)) {
1060 device = list_first_entry(&pending_put,
1061 struct btrfs_device, dev_list);
1062 list_del(&device->dev_list);
1063 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001064 call_rcu(&device->rcu, free_device_rcu);
Anand Jain0ccd0522016-09-22 12:56:13 +08001065 }
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
1145 mutex_lock(&uuid_mutex);
1146 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001147 fs_devices->opened++;
1148 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001149 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001150 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001151 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001152 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001153 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001154 return ret;
1155}
1156
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001157static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001158{
1159 kunmap(page);
1160 put_page(page);
1161}
1162
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001163static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1164 struct page **page,
1165 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001166{
1167 void *p;
1168 pgoff_t index;
1169
1170 /* make sure our super fits in the device */
1171 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1172 return 1;
1173
1174 /* make sure our super fits in the page */
1175 if (sizeof(**disk_super) > PAGE_SIZE)
1176 return 1;
1177
1178 /* make sure our super doesn't straddle pages on disk */
1179 index = bytenr >> PAGE_SHIFT;
1180 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1181 return 1;
1182
1183 /* pull in the page with our super */
1184 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1185 index, GFP_KERNEL);
1186
1187 if (IS_ERR_OR_NULL(*page))
1188 return 1;
1189
1190 p = kmap(*page);
1191
1192 /* align our pointer to the offset of the super block */
1193 *disk_super = p + (bytenr & ~PAGE_MASK);
1194
1195 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1196 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1197 btrfs_release_disk_super(*page);
1198 return 1;
1199 }
1200
1201 if ((*disk_super)->label[0] &&
1202 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1203 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1204
1205 return 0;
1206}
1207
David Sterba6f60cbd2013-02-15 11:31:02 -07001208/*
1209 * Look for a btrfs signature on a device. This may be called out of the mount path
1210 * and we are not allowed to call set_blocksize during the scan. The superblock
1211 * is read via pagecache
1212 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001213int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001214 struct btrfs_fs_devices **fs_devices_ret)
1215{
1216 struct btrfs_super_block *disk_super;
Anand Jaine124ece2018-01-18 22:02:35 +08001217 struct btrfs_device *device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001218 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001219 struct page *page;
Anand Jaine124ece2018-01-18 22:02:35 +08001220 int ret = 0;
David Sterba6f60cbd2013-02-15 11:31:02 -07001221 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001222
David Sterba6f60cbd2013-02-15 11:31:02 -07001223 /*
1224 * we would like to check all the supers, but that would make
1225 * a btrfs mount succeed after a mkfs from a different FS.
1226 * So, we need to add a special mount option to scan for
1227 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1228 */
1229 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001230 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001231 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001232
1233 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001234 if (IS_ERR(bdev)) {
1235 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001236 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001237 }
1238
Anand Jain05a5c552017-12-15 15:40:16 +08001239 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1240 ret = -EINVAL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001241 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001242 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001243
Anand Jain3acbcbf2018-01-18 22:02:36 +08001244 device = device_list_add(path, disk_super);
Anand Jaine124ece2018-01-18 22:02:35 +08001245 if (IS_ERR(device))
1246 ret = PTR_ERR(device);
1247 else
1248 *fs_devices_ret = device->fs_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001249
Anand Jain6cf86a002016-02-13 10:01:29 +08001250 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001251
1252error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001253 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001254error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001255 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001256 return ret;
1257}
Chris Mason0b86a832008-03-24 15:01:56 -04001258
Miao Xie6d07bce2011-01-05 10:07:31 +00001259/* helper to account the used device space in the range */
1260int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1261 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001262{
1263 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001264 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001265 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001266 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001267 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001268 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001269 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001270 struct extent_buffer *l;
1271
Miao Xie6d07bce2011-01-05 10:07:31 +00001272 *length = 0;
1273
Anand Jain401e29c2017-12-04 12:54:55 +08001274 if (start >= device->total_bytes ||
1275 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Miao Xie6d07bce2011-01-05 10:07:31 +00001276 return 0;
1277
Yan Zheng2b820322008-11-17 21:11:30 -05001278 path = btrfs_alloc_path();
1279 if (!path)
1280 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001281 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001282
Chris Mason0b86a832008-03-24 15:01:56 -04001283 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001284 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001285 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001286
1287 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001288 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001289 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001290 if (ret > 0) {
1291 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1292 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001293 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001294 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001295
Chris Mason0b86a832008-03-24 15:01:56 -04001296 while (1) {
1297 l = path->nodes[0];
1298 slot = path->slots[0];
1299 if (slot >= btrfs_header_nritems(l)) {
1300 ret = btrfs_next_leaf(root, path);
1301 if (ret == 0)
1302 continue;
1303 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001304 goto out;
1305
1306 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001307 }
1308 btrfs_item_key_to_cpu(l, &key, slot);
1309
1310 if (key.objectid < device->devid)
1311 goto next;
1312
1313 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001314 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001315
David Sterba962a2982014-06-04 18:41:45 +02001316 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001317 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001318
Chris Mason0b86a832008-03-24 15:01:56 -04001319 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001320 extent_end = key.offset + btrfs_dev_extent_length(l,
1321 dev_extent);
1322 if (key.offset <= start && extent_end > end) {
1323 *length = end - start + 1;
1324 break;
1325 } else if (key.offset <= start && extent_end > start)
1326 *length += extent_end - start;
1327 else if (key.offset > start && extent_end <= end)
1328 *length += extent_end - key.offset;
1329 else if (key.offset > start && key.offset <= end) {
1330 *length += end - key.offset + 1;
1331 break;
1332 } else if (key.offset > end)
1333 break;
1334
1335next:
1336 path->slots[0]++;
1337 }
1338 ret = 0;
1339out:
1340 btrfs_free_path(path);
1341 return ret;
1342}
1343
Jeff Mahoney499f3772015-06-15 09:41:17 -04001344static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001345 struct btrfs_device *device,
1346 u64 *start, u64 len)
1347{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001348 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001349 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001350 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001351 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001352 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001353
Jeff Mahoney499f3772015-06-15 09:41:17 -04001354 if (transaction)
1355 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001356again:
1357 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001358 struct map_lookup *map;
1359 int i;
1360
Jeff Mahoney95617d62015-06-03 10:55:48 -04001361 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001362 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001363 u64 end;
1364
Josef Bacik6df9a952013-06-27 13:22:46 -04001365 if (map->stripes[i].dev != device)
1366 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001367 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001368 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001369 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001370 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001371 /*
1372 * Make sure that while processing the pinned list we do
1373 * not override our *start with a lower value, because
1374 * we can have pinned chunks that fall within this
1375 * device hole and that have lower physical addresses
1376 * than the pending chunks we processed before. If we
1377 * do not take this special care we can end up getting
1378 * 2 pending chunks that start at the same physical
1379 * device offsets because the end offset of a pinned
1380 * chunk can be equal to the start offset of some
1381 * pending chunk.
1382 */
1383 end = map->stripes[i].physical + em->orig_block_len;
1384 if (end > *start) {
1385 *start = end;
1386 ret = 1;
1387 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001388 }
1389 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001390 if (search_list != &fs_info->pinned_chunks) {
1391 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001392 goto again;
1393 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001394
1395 return ret;
1396}
1397
1398
Chris Mason0b86a832008-03-24 15:01:56 -04001399/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001400 * find_free_dev_extent_start - find free space in the specified device
1401 * @device: the device which we search the free space in
1402 * @num_bytes: the size of the free space that we need
1403 * @search_start: the position from which to begin the search
1404 * @start: store the start of the free space.
1405 * @len: the size of the free space. that we find, or the size
1406 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001407 *
Chris Mason0b86a832008-03-24 15:01:56 -04001408 * this uses a pretty simple search, the expectation is that it is
1409 * called very infrequently and that a given device has a small number
1410 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001411 *
1412 * @start is used to store the start of the free space if we find. But if we
1413 * don't find suitable free space, it will be used to store the start position
1414 * of the max free space.
1415 *
1416 * @len is used to store the size of the free space that we find.
1417 * But if we don't find suitable free space, it is used to store the size of
1418 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001419 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001420int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1421 struct btrfs_device *device, u64 num_bytes,
1422 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001423{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001424 struct btrfs_fs_info *fs_info = device->fs_info;
1425 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001426 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001427 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001428 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001429 u64 hole_size;
1430 u64 max_hole_start;
1431 u64 max_hole_size;
1432 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001433 u64 search_end = device->total_bytes;
1434 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001435 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001436 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001437
1438 /*
1439 * We don't want to overwrite the superblock on the drive nor any area
1440 * used by the boot loader (grub for example), so we make sure to start
1441 * at an offset of at least 1MB.
1442 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001443 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001444
Josef Bacik6df9a952013-06-27 13:22:46 -04001445 path = btrfs_alloc_path();
1446 if (!path)
1447 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001448
Miao Xie7bfc8372011-01-05 10:07:26 +00001449 max_hole_start = search_start;
1450 max_hole_size = 0;
1451
Zhao Leif2ab7612015-02-16 18:52:17 +08001452again:
Anand Jain401e29c2017-12-04 12:54:55 +08001453 if (search_start >= search_end ||
1454 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001455 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001456 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001457 }
1458
David Sterbae4058b52015-11-27 16:31:35 +01001459 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001460 path->search_commit_root = 1;
1461 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001462
Chris Mason0b86a832008-03-24 15:01:56 -04001463 key.objectid = device->devid;
1464 key.offset = search_start;
1465 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001466
Li Zefan125ccb02011-12-08 15:07:24 +08001467 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001468 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001469 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001470 if (ret > 0) {
1471 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1472 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001473 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001474 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001475
Chris Mason0b86a832008-03-24 15:01:56 -04001476 while (1) {
1477 l = path->nodes[0];
1478 slot = path->slots[0];
1479 if (slot >= btrfs_header_nritems(l)) {
1480 ret = btrfs_next_leaf(root, path);
1481 if (ret == 0)
1482 continue;
1483 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001484 goto out;
1485
1486 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001487 }
1488 btrfs_item_key_to_cpu(l, &key, slot);
1489
1490 if (key.objectid < device->devid)
1491 goto next;
1492
1493 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001494 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001495
David Sterba962a2982014-06-04 18:41:45 +02001496 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001497 goto next;
1498
Miao Xie7bfc8372011-01-05 10:07:26 +00001499 if (key.offset > search_start) {
1500 hole_size = key.offset - search_start;
1501
Josef Bacik6df9a952013-06-27 13:22:46 -04001502 /*
1503 * Have to check before we set max_hole_start, otherwise
1504 * we could end up sending back this offset anyway.
1505 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001506 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001507 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001508 hole_size)) {
1509 if (key.offset >= search_start) {
1510 hole_size = key.offset - search_start;
1511 } else {
1512 WARN_ON_ONCE(1);
1513 hole_size = 0;
1514 }
1515 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001516
Miao Xie7bfc8372011-01-05 10:07:26 +00001517 if (hole_size > max_hole_size) {
1518 max_hole_start = search_start;
1519 max_hole_size = hole_size;
1520 }
1521
1522 /*
1523 * If this free space is greater than which we need,
1524 * it must be the max free space that we have found
1525 * until now, so max_hole_start must point to the start
1526 * of this free space and the length of this free space
1527 * is stored in max_hole_size. Thus, we return
1528 * max_hole_start and max_hole_size and go back to the
1529 * caller.
1530 */
1531 if (hole_size >= num_bytes) {
1532 ret = 0;
1533 goto out;
1534 }
1535 }
1536
Chris Mason0b86a832008-03-24 15:01:56 -04001537 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001538 extent_end = key.offset + btrfs_dev_extent_length(l,
1539 dev_extent);
1540 if (extent_end > search_start)
1541 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001542next:
1543 path->slots[0]++;
1544 cond_resched();
1545 }
Chris Mason0b86a832008-03-24 15:01:56 -04001546
liubo38c01b92011-08-02 02:39:03 +00001547 /*
1548 * At this point, search_start should be the end of
1549 * allocated dev extents, and when shrinking the device,
1550 * search_end may be smaller than search_start.
1551 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001552 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001553 hole_size = search_end - search_start;
1554
Jeff Mahoney499f3772015-06-15 09:41:17 -04001555 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001556 hole_size)) {
1557 btrfs_release_path(path);
1558 goto again;
1559 }
Chris Mason0b86a832008-03-24 15:01:56 -04001560
Zhao Leif2ab7612015-02-16 18:52:17 +08001561 if (hole_size > max_hole_size) {
1562 max_hole_start = search_start;
1563 max_hole_size = hole_size;
1564 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001565 }
1566
Miao Xie7bfc8372011-01-05 10:07:26 +00001567 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001568 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001569 ret = -ENOSPC;
1570 else
1571 ret = 0;
1572
1573out:
Yan Zheng2b820322008-11-17 21:11:30 -05001574 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001575 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001576 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001577 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001578 return ret;
1579}
1580
Jeff Mahoney499f3772015-06-15 09:41:17 -04001581int find_free_dev_extent(struct btrfs_trans_handle *trans,
1582 struct btrfs_device *device, u64 num_bytes,
1583 u64 *start, u64 *len)
1584{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001585 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001586 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001587 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001588}
1589
Christoph Hellwigb2950862008-12-02 09:54:17 -05001590static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001591 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001592 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001594 struct btrfs_fs_info *fs_info = device->fs_info;
1595 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001596 int ret;
1597 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001598 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001599 struct btrfs_key found_key;
1600 struct extent_buffer *leaf = NULL;
1601 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001602
1603 path = btrfs_alloc_path();
1604 if (!path)
1605 return -ENOMEM;
1606
1607 key.objectid = device->devid;
1608 key.offset = start;
1609 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001610again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001611 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001612 if (ret > 0) {
1613 ret = btrfs_previous_item(root, path, key.objectid,
1614 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001615 if (ret)
1616 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001617 leaf = path->nodes[0];
1618 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1619 extent = btrfs_item_ptr(leaf, path->slots[0],
1620 struct btrfs_dev_extent);
1621 BUG_ON(found_key.offset > start || found_key.offset +
1622 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001623 key = found_key;
1624 btrfs_release_path(path);
1625 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001626 } else if (ret == 0) {
1627 leaf = path->nodes[0];
1628 extent = btrfs_item_ptr(leaf, path->slots[0],
1629 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001630 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001631 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001632 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001633 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001634
Miao Xie2196d6e2014-09-03 21:35:41 +08001635 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1636
Chris Mason8f18cf12008-04-25 16:53:30 -04001637 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001638 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001639 btrfs_handle_fs_error(fs_info, ret,
1640 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001641 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001642 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001643 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001644out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001645 btrfs_free_path(path);
1646 return ret;
1647}
1648
Eric Sandeen48a3b632013-04-25 20:41:01 +00001649static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1650 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001651 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001652{
1653 int ret;
1654 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001655 struct btrfs_fs_info *fs_info = device->fs_info;
1656 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001657 struct btrfs_dev_extent *extent;
1658 struct extent_buffer *leaf;
1659 struct btrfs_key key;
1660
Anand Jaine12c9622017-12-04 12:54:53 +08001661 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001662 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001663 path = btrfs_alloc_path();
1664 if (!path)
1665 return -ENOMEM;
1666
Chris Mason0b86a832008-03-24 15:01:56 -04001667 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001668 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001669 key.type = BTRFS_DEV_EXTENT_KEY;
1670 ret = btrfs_insert_empty_item(trans, root, path, &key,
1671 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001672 if (ret)
1673 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001674
1675 leaf = path->nodes[0];
1676 extent = btrfs_item_ptr(leaf, path->slots[0],
1677 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001678 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1679 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001680 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1681 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001682 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1683
Chris Mason0b86a832008-03-24 15:01:56 -04001684 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1685 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001686out:
Chris Mason0b86a832008-03-24 15:01:56 -04001687 btrfs_free_path(path);
1688 return ret;
1689}
1690
Josef Bacik6df9a952013-06-27 13:22:46 -04001691static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001692{
Josef Bacik6df9a952013-06-27 13:22:46 -04001693 struct extent_map_tree *em_tree;
1694 struct extent_map *em;
1695 struct rb_node *n;
1696 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001697
Josef Bacik6df9a952013-06-27 13:22:46 -04001698 em_tree = &fs_info->mapping_tree.map_tree;
1699 read_lock(&em_tree->lock);
1700 n = rb_last(&em_tree->map);
1701 if (n) {
1702 em = rb_entry(n, struct extent_map, rb_node);
1703 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001704 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001705 read_unlock(&em_tree->lock);
1706
Chris Mason0b86a832008-03-24 15:01:56 -04001707 return ret;
1708}
1709
Ilya Dryomov53f10652013-08-12 14:33:01 +03001710static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1711 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001712{
1713 int ret;
1714 struct btrfs_key key;
1715 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001716 struct btrfs_path *path;
1717
Yan Zheng2b820322008-11-17 21:11:30 -05001718 path = btrfs_alloc_path();
1719 if (!path)
1720 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001721
1722 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1723 key.type = BTRFS_DEV_ITEM_KEY;
1724 key.offset = (u64)-1;
1725
Ilya Dryomov53f10652013-08-12 14:33:01 +03001726 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001727 if (ret < 0)
1728 goto error;
1729
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001730 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001731
Ilya Dryomov53f10652013-08-12 14:33:01 +03001732 ret = btrfs_previous_item(fs_info->chunk_root, path,
1733 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001734 BTRFS_DEV_ITEM_KEY);
1735 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001736 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001737 } else {
1738 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1739 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001740 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001741 }
1742 ret = 0;
1743error:
Yan Zheng2b820322008-11-17 21:11:30 -05001744 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001745 return ret;
1746}
1747
1748/*
1749 * the device information is stored in the chunk root
1750 * the btrfs_device struct should be fully filled in
1751 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001752static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001753 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001754 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001755{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001756 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001757 int ret;
1758 struct btrfs_path *path;
1759 struct btrfs_dev_item *dev_item;
1760 struct extent_buffer *leaf;
1761 struct btrfs_key key;
1762 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001763
Chris Mason0b86a832008-03-24 15:01:56 -04001764 path = btrfs_alloc_path();
1765 if (!path)
1766 return -ENOMEM;
1767
Chris Mason0b86a832008-03-24 15:01:56 -04001768 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1769 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001770 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001771
1772 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001773 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001774 if (ret)
1775 goto out;
1776
1777 leaf = path->nodes[0];
1778 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1779
1780 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001781 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001782 btrfs_set_device_type(leaf, dev_item, device->type);
1783 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1784 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1785 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001786 btrfs_set_device_total_bytes(leaf, dev_item,
1787 btrfs_device_get_disk_total_bytes(device));
1788 btrfs_set_device_bytes_used(leaf, dev_item,
1789 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001790 btrfs_set_device_group(leaf, dev_item, 0);
1791 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1792 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001793 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001794
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001795 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001796 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001797 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001798 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001799 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001800
Yan Zheng2b820322008-11-17 21:11:30 -05001801 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001802out:
1803 btrfs_free_path(path);
1804 return ret;
1805}
Chris Mason8f18cf12008-04-25 16:53:30 -04001806
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001807/*
1808 * Function to update ctime/mtime for a given device path.
1809 * Mainly used for ctime/mtime based probe like libblkid.
1810 */
David Sterbada353f62017-02-14 17:55:53 +01001811static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001812{
1813 struct file *filp;
1814
1815 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001816 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001817 return;
1818 file_update_time(filp);
1819 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001820}
1821
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001822static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001823 struct btrfs_device *device)
1824{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001825 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001826 int ret;
1827 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001828 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001829 struct btrfs_trans_handle *trans;
1830
Chris Masona061fc82008-05-07 11:43:44 -04001831 path = btrfs_alloc_path();
1832 if (!path)
1833 return -ENOMEM;
1834
Yan, Zhenga22285a2010-05-16 10:48:46 -04001835 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001836 if (IS_ERR(trans)) {
1837 btrfs_free_path(path);
1838 return PTR_ERR(trans);
1839 }
Chris Masona061fc82008-05-07 11:43:44 -04001840 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1841 key.type = BTRFS_DEV_ITEM_KEY;
1842 key.offset = device->devid;
1843
1844 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001845 if (ret) {
1846 if (ret > 0)
1847 ret = -ENOENT;
1848 btrfs_abort_transaction(trans, ret);
1849 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001850 goto out;
1851 }
1852
1853 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001854 if (ret) {
1855 btrfs_abort_transaction(trans, ret);
1856 btrfs_end_transaction(trans);
1857 }
1858
Chris Masona061fc82008-05-07 11:43:44 -04001859out:
1860 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001861 if (!ret)
1862 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001863 return ret;
1864}
1865
David Sterba3cc31a02016-02-15 16:00:26 +01001866/*
1867 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1868 * filesystem. It's up to the caller to adjust that number regarding eg. device
1869 * replace.
1870 */
1871static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1872 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001873{
Chris Masona061fc82008-05-07 11:43:44 -04001874 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001875 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001876 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001877
Miao Xiede98ced2013-01-29 10:13:12 +00001878 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001879 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001880
Anand Jainbd45ffb2016-02-13 10:01:34 +08001881 all_avail = fs_info->avail_data_alloc_bits |
1882 fs_info->avail_system_alloc_bits |
1883 fs_info->avail_metadata_alloc_bits;
1884 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001885
David Sterba418775a2016-02-15 16:28:14 +01001886 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1887 if (!(all_avail & btrfs_raid_group[i]))
1888 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001889
David Sterba418775a2016-02-15 16:28:14 +01001890 if (num_devices < btrfs_raid_array[i].devs_min) {
1891 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001892
David Sterba418775a2016-02-15 16:28:14 +01001893 if (ret)
1894 return ret;
1895 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001896 }
1897
1898 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001899}
1900
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001901static struct btrfs_device * btrfs_find_next_active_device(
1902 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001903{
1904 struct btrfs_device *next_device;
1905
1906 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1907 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001908 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1909 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001910 return next_device;
1911 }
1912
1913 return NULL;
1914}
1915
1916/*
1917 * Helper function to check if the given device is part of s_bdev / latest_bdev
1918 * and replace it with the provided or the next active device, in the context
1919 * where this function called, there should be always be another device (or
1920 * this_dev) which is active.
1921 */
1922void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1923 struct btrfs_device *device, struct btrfs_device *this_dev)
1924{
1925 struct btrfs_device *next_device;
1926
1927 if (this_dev)
1928 next_device = this_dev;
1929 else
1930 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1931 device);
1932 ASSERT(next_device);
1933
1934 if (fs_info->sb->s_bdev &&
1935 (fs_info->sb->s_bdev == device->bdev))
1936 fs_info->sb->s_bdev = next_device->bdev;
1937
1938 if (fs_info->fs_devices->latest_bdev == device->bdev)
1939 fs_info->fs_devices->latest_bdev = next_device->bdev;
1940}
1941
David Sterbada353f62017-02-14 17:55:53 +01001942int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1943 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001944{
1945 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001946 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001947 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001948 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001949 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001950
1951 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001952
Anand Jainb5185192018-04-12 10:29:30 +08001953 num_devices = fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01001954 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001955 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001956 WARN_ON(num_devices < 1);
1957 num_devices--;
1958 }
David Sterba7e79cb82018-03-24 02:11:38 +01001959 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Chris Masona061fc82008-05-07 11:43:44 -04001960
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001962 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001963 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001964
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001965 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1966 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001967 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001968 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001969
Anand Jain401e29c2017-12-04 12:54:55 +08001970 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001971 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001972 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001973 }
1974
Anand Jainebbede42017-12-04 12:54:52 +08001975 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1976 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001977 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001978 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001979 }
1980
Anand Jainebbede42017-12-04 12:54:52 +08001981 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001982 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001983 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001984 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001985 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001986 }
Chris Masona061fc82008-05-07 11:43:44 -04001987
Carey Underwoodd7901552013-03-04 16:37:06 -06001988 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001989 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001990 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001991 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001992 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001993
Stefan Behrens63a212a2012-11-05 18:29:28 +01001994 /*
1995 * TODO: the superblock still includes this device in its num_devices
1996 * counter although write_all_supers() is not locked out. This
1997 * could give a filesystem state which requires a degraded mount.
1998 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001999 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002000 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002001 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002002
Anand Jaine12c9622017-12-04 12:54:53 +08002003 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002004 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002005
2006 /*
2007 * the device list mutex makes sure that we don't change
2008 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002009 * the device supers. Whoever is writing all supers, should
2010 * lock the device list mutex before getting the number of
2011 * devices in the super block (super_copy). Conversely,
2012 * whoever updates the number of devices in the super block
2013 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002014 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002015
2016 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002017 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002018 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002019
Yan Zhenge4404d62008-12-12 10:03:26 -05002020 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04002021 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002022
Anand Jaine6e674b2017-12-04 12:54:54 +08002023 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Miao Xie3a7d55c2014-07-16 18:38:01 +08002024 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002025
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002026 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002027
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002028 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002029 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002030 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002031 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002032 }
Anand Jain99994cd2014-06-03 11:36:00 +08002033
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002034 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2035 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002036 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002037
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002038 /*
2039 * at this point, the device is zero sized and detached from
2040 * the devices list. All that's left is to zero out the old
2041 * supers and free the device.
2042 */
Anand Jainebbede42017-12-04 12:54:52 +08002043 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002044 btrfs_scratch_superblocks(device->bdev, device->name->str);
2045
2046 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002047 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002048
Xiao Guangrong1f781602011-04-20 10:09:16 +00002049 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002050 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002051 if (fs_devices->seed == cur_devices) {
2052 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002053 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002054 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002055 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002056 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002057 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002058 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002059 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002060 }
2061
Chris Masona061fc82008-05-07 11:43:44 -04002062out:
2063 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002064 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002065
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002066error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002067 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002068 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002069 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002070 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002071 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002072 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002073 }
Anand Jain24fc5722016-02-13 10:01:36 +08002074 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002075}
2076
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002077void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2078 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002079{
Anand Jaind51908c2014-08-13 14:24:19 +08002080 struct btrfs_fs_devices *fs_devices;
2081
David Sterbaa32bf9a2018-03-16 02:21:22 +01002082 lockdep_assert_held(&fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002083
Anand Jain25e8e912014-08-20 10:56:56 +08002084 /*
2085 * in case of fs with no seed, srcdev->fs_devices will point
2086 * to fs_devices of fs_info. However when the dev being replaced is
2087 * a seed dev it will point to the seed's local fs_devices. In short
2088 * srcdev will have its correct fs_devices in both the cases.
2089 */
2090 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002091
Stefan Behrense93c89c2012-11-05 17:33:06 +01002092 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002093 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002094 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002095 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002096 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002097
Anand Jainebbede42017-12-04 12:54:52 +08002098 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002099 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002100
Miao Xie82372bc2014-09-03 21:35:44 +08002101 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002102 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002103}
2104
2105void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2106 struct btrfs_device *srcdev)
2107{
2108 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002109
Anand Jainebbede42017-12-04 12:54:52 +08002110 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002111 /* zero out the old super if it is writable */
2112 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2113 }
Anand Jain14238812016-07-22 06:04:53 +08002114
2115 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002116 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002117
Anand Jain94d5f0c2014-08-13 14:24:22 +08002118 /* if this is no devs we rather delete the fs_devices */
2119 if (!fs_devices->num_devices) {
2120 struct btrfs_fs_devices *tmp_fs_devices;
2121
Anand Jain6dd38f82017-10-17 06:53:50 +08002122 /*
2123 * On a mounted FS, num_devices can't be zero unless it's a
2124 * seed. In case of a seed device being replaced, the replace
2125 * target added to the sprout FS, so there will be no more
2126 * device left under the seed FS.
2127 */
2128 ASSERT(fs_devices->seeding);
2129
Anand Jain94d5f0c2014-08-13 14:24:22 +08002130 tmp_fs_devices = fs_info->fs_devices;
2131 while (tmp_fs_devices) {
2132 if (tmp_fs_devices->seed == fs_devices) {
2133 tmp_fs_devices->seed = fs_devices->seed;
2134 break;
2135 }
2136 tmp_fs_devices = tmp_fs_devices->seed;
2137 }
2138 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002139 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002140 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002141 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002142}
2143
2144void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2145 struct btrfs_device *tgtdev)
2146{
Miao Xie67a2c452014-09-03 21:35:43 +08002147 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002148 WARN_ON(!tgtdev);
2149 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002150
Anand Jain32576042015-08-14 18:32:49 +08002151 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002152
Anand Jain779bf3fe2016-04-18 16:51:23 +08002153 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002154 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002155
Stefan Behrense93c89c2012-11-05 17:33:06 +01002156 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002157
Anand Jain88acff62016-05-03 17:44:43 +08002158 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002159
Stefan Behrense93c89c2012-11-05 17:33:06 +01002160 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002161
2162 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002163 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002164
2165 /*
2166 * The update_dev_time() with in btrfs_scratch_superblocks()
2167 * may lead to a call to btrfs_show_devname() which will try
2168 * to hold device_list_mutex. And here this device
2169 * is already out of device list, so we don't have to hold
2170 * the device_list_mutex lock.
2171 */
2172 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002173
2174 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002175 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002176}
2177
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002178static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002179 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002180 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002181{
2182 int ret = 0;
2183 struct btrfs_super_block *disk_super;
2184 u64 devid;
2185 u8 *dev_uuid;
2186 struct block_device *bdev;
2187 struct buffer_head *bh;
2188
2189 *device = NULL;
2190 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002191 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002192 if (ret)
2193 return ret;
2194 disk_super = (struct btrfs_super_block *)bh->b_data;
2195 devid = btrfs_stack_device_id(&disk_super->dev_item);
2196 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002197 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002198 brelse(bh);
2199 if (!*device)
2200 ret = -ENOENT;
2201 blkdev_put(bdev, FMODE_READ);
2202 return ret;
2203}
2204
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002205int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002206 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002207 struct btrfs_device **device)
2208{
2209 *device = NULL;
2210 if (strcmp(device_path, "missing") == 0) {
2211 struct list_head *devices;
2212 struct btrfs_device *tmp;
2213
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002214 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002215 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002216 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2217 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002218 *device = tmp;
2219 break;
2220 }
2221 }
2222
Anand Jaind74a6252015-08-14 18:32:56 +08002223 if (!*device)
2224 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002225
2226 return 0;
2227 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002228 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002229 }
2230}
2231
Yan Zheng2b820322008-11-17 21:11:30 -05002232/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002233 * Lookup a device given by device id, or the path if the id is 0.
2234 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002235int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002236 const char *devpath,
2237 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002238{
2239 int ret;
2240
David Sterba5c5c0df2016-02-15 16:39:55 +01002241 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002242 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002243 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002244 if (!*device)
2245 ret = -ENOENT;
2246 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002247 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002248 return -EINVAL;
2249
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002250 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002251 device);
2252 }
2253 return ret;
2254}
2255
Yan Zheng2b820322008-11-17 21:11:30 -05002256/*
2257 * does all the dirty work required for changing file system's UUID.
2258 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002259static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002260{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002261 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002262 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002263 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002264 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002265 struct btrfs_device *device;
2266 u64 super_flags;
2267
David Sterbaa32bf9a2018-03-16 02:21:22 +01002268 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002269 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002270 return -EINVAL;
2271
David Sterba2dfeca92017-06-14 02:48:07 +02002272 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002273 if (IS_ERR(seed_devices))
2274 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002275
Yan Zhenge4404d62008-12-12 10:03:26 -05002276 old_devices = clone_fs_devices(fs_devices);
2277 if (IS_ERR(old_devices)) {
2278 kfree(seed_devices);
2279 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002280 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002281
Anand Jainc4babc52018-04-12 10:29:25 +08002282 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002283
Yan Zhenge4404d62008-12-12 10:03:26 -05002284 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2285 seed_devices->opened = 1;
2286 INIT_LIST_HEAD(&seed_devices->devices);
2287 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002288 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002289
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002290 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002291 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2292 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002293 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002294 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002295
David Sterba34441362016-10-04 19:34:27 +02002296 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002297 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002298 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002299
Yan Zheng2b820322008-11-17 21:11:30 -05002300 fs_devices->seeding = 0;
2301 fs_devices->num_devices = 0;
2302 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002303 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002304 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002305 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002306
2307 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002308 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002309 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002310 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002311
Yan Zheng2b820322008-11-17 21:11:30 -05002312 super_flags = btrfs_super_flags(disk_super) &
2313 ~BTRFS_SUPER_FLAG_SEEDING;
2314 btrfs_set_super_flags(disk_super, super_flags);
2315
2316 return 0;
2317}
2318
2319/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002320 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002321 */
2322static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002323 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002324{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002325 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002326 struct btrfs_path *path;
2327 struct extent_buffer *leaf;
2328 struct btrfs_dev_item *dev_item;
2329 struct btrfs_device *device;
2330 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002331 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002332 u8 dev_uuid[BTRFS_UUID_SIZE];
2333 u64 devid;
2334 int ret;
2335
2336 path = btrfs_alloc_path();
2337 if (!path)
2338 return -ENOMEM;
2339
Yan Zheng2b820322008-11-17 21:11:30 -05002340 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2341 key.offset = 0;
2342 key.type = BTRFS_DEV_ITEM_KEY;
2343
2344 while (1) {
2345 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2346 if (ret < 0)
2347 goto error;
2348
2349 leaf = path->nodes[0];
2350next_slot:
2351 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2352 ret = btrfs_next_leaf(root, path);
2353 if (ret > 0)
2354 break;
2355 if (ret < 0)
2356 goto error;
2357 leaf = path->nodes[0];
2358 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002359 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002360 continue;
2361 }
2362
2363 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2364 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2365 key.type != BTRFS_DEV_ITEM_KEY)
2366 break;
2367
2368 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2369 struct btrfs_dev_item);
2370 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002371 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002372 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002373 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002374 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002375 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002376 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002377
2378 if (device->fs_devices->seeding) {
2379 btrfs_set_device_generation(leaf, dev_item,
2380 device->generation);
2381 btrfs_mark_buffer_dirty(leaf);
2382 }
2383
2384 path->slots[0]++;
2385 goto next_slot;
2386 }
2387 ret = 0;
2388error:
2389 btrfs_free_path(path);
2390 return ret;
2391}
2392
David Sterbada353f62017-02-14 17:55:53 +01002393int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002394{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002395 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002396 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002397 struct btrfs_trans_handle *trans;
2398 struct btrfs_device *device;
2399 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002400 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002401 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002402 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002403 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002404 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002405 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002406 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002407
David Howellsbc98a422017-07-17 08:45:34 +01002408 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002409 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002410
Li Zefana5d16332011-12-07 20:08:40 -05002411 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002412 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002413 if (IS_ERR(bdev))
2414 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002416 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002417 seeding_dev = 1;
2418 down_write(&sb->s_umount);
2419 mutex_lock(&uuid_mutex);
2420 }
2421
Chris Mason8c8bee12008-09-29 11:19:10 -04002422 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002423
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002424 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002425
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002426 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002427 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002428 if (device->bdev == bdev) {
2429 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002430 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002431 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002432 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002433 }
2434 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002435 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002436
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002437 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002438 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002439 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002440 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002441 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002442 }
2443
David Sterba78f2c9e2016-02-11 14:25:38 +01002444 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002445 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002446 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002447 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002448 }
Josef Bacik606686e2012-06-04 14:03:51 -04002449 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002450
Yan, Zhenga22285a2010-05-16 10:48:46 -04002451 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002452 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002453 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002454 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002455 }
2456
Josef Bacikd5e20032011-08-04 14:52:27 +00002457 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002458 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002459 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002460 device->io_width = fs_info->sectorsize;
2461 device->io_align = fs_info->sectorsize;
2462 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002463 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2464 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002465 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002466 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002467 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002468 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002469 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002470 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002471 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002472 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002473 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002474
Yan Zheng2b820322008-11-17 21:11:30 -05002475 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002476 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002477 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002478 if (ret) {
2479 btrfs_abort_transaction(trans, ret);
2480 goto error_trans;
2481 }
Yan Zheng2b820322008-11-17 21:11:30 -05002482 }
2483
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002484 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002485
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002487 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002488 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002489 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 &fs_info->fs_devices->alloc_list);
2491 fs_info->fs_devices->num_devices++;
2492 fs_info->fs_devices->open_devices++;
2493 fs_info->fs_devices->rw_devices++;
2494 fs_info->fs_devices->total_devices++;
2495 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002496
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002497 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002498
Anand Jaine884f4f2017-04-04 18:40:19 +08002499 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002500 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002501
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2503 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002504 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002505
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002506 tmp = btrfs_super_num_devices(fs_info->super_copy);
2507 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002508
2509 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002510 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002511
Miao Xie2196d6e2014-09-03 21:35:41 +08002512 /*
2513 * we've got more storage, clear any full flags on the space
2514 * infos
2515 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002516 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002517
David Sterba34441362016-10-04 19:34:27 +02002518 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002519 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002520
Yan Zheng2b820322008-11-17 21:11:30 -05002521 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002522 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002523 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002524 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002525 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002526 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002527 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002528 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002529 }
2530
Anand Jainc74a0b02017-11-06 16:36:15 +08002531 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002532 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002533 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002534 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002535 }
2536
2537 if (seeding_dev) {
2538 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002540 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002541 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002542 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002543 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002544 }
Anand Jainb2373f22014-06-03 11:36:03 +08002545
2546 /* Sprouting would change fsid of the mounted root,
2547 * so rename the fsid on the sysfs
2548 */
2549 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002550 fs_info->fsid);
2551 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2552 btrfs_warn(fs_info,
2553 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002554 }
2555
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002556 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002557
2558 if (seeding_dev) {
2559 mutex_unlock(&uuid_mutex);
2560 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002561 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002562
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002563 if (ret) /* transaction commit */
2564 return ret;
2565
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002566 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002567 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002568 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002569 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002570 trans = btrfs_attach_transaction(root);
2571 if (IS_ERR(trans)) {
2572 if (PTR_ERR(trans) == -ENOENT)
2573 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002574 ret = PTR_ERR(trans);
2575 trans = NULL;
2576 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002577 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002578 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002579 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002580
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002581 /* Update ctime/mtime for libblkid */
2582 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002583 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002584
Anand Jaind31c32f2017-09-28 14:51:10 +08002585error_sysfs:
2586 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002587error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002588 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002589 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002590 if (trans)
2591 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002592error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002593 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002594error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002595 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002596 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002597 mutex_unlock(&uuid_mutex);
2598 up_write(&sb->s_umount);
2599 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002600 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002601}
2602
Chris Masond3977122009-01-05 21:25:51 -05002603static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2604 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002605{
2606 int ret;
2607 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002608 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002609 struct btrfs_dev_item *dev_item;
2610 struct extent_buffer *leaf;
2611 struct btrfs_key key;
2612
Chris Mason0b86a832008-03-24 15:01:56 -04002613 path = btrfs_alloc_path();
2614 if (!path)
2615 return -ENOMEM;
2616
2617 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2618 key.type = BTRFS_DEV_ITEM_KEY;
2619 key.offset = device->devid;
2620
2621 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2622 if (ret < 0)
2623 goto out;
2624
2625 if (ret > 0) {
2626 ret = -ENOENT;
2627 goto out;
2628 }
2629
2630 leaf = path->nodes[0];
2631 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2632
2633 btrfs_set_device_id(leaf, dev_item, device->devid);
2634 btrfs_set_device_type(leaf, dev_item, device->type);
2635 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2636 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2637 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002638 btrfs_set_device_total_bytes(leaf, dev_item,
2639 btrfs_device_get_disk_total_bytes(device));
2640 btrfs_set_device_bytes_used(leaf, dev_item,
2641 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002642 btrfs_mark_buffer_dirty(leaf);
2643
2644out:
2645 btrfs_free_path(path);
2646 return ret;
2647}
2648
Miao Xie2196d6e2014-09-03 21:35:41 +08002649int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002650 struct btrfs_device *device, u64 new_size)
2651{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002652 struct btrfs_fs_info *fs_info = device->fs_info;
2653 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002654 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002655 u64 old_total;
2656 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002657
Anand Jainebbede42017-12-04 12:54:52 +08002658 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002659 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002660
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002661 new_size = round_down(new_size, fs_info->sectorsize);
2662
David Sterba34441362016-10-04 19:34:27 +02002663 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002664 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002665 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002666
Stefan Behrens63a212a2012-11-05 18:29:28 +01002667 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002668 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002669 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002670 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002671 }
Yan Zheng2b820322008-11-17 21:11:30 -05002672
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002673 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002674
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002675 btrfs_set_super_total_bytes(super_copy,
2676 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002677 device->fs_devices->total_rw_bytes += diff;
2678
Miao Xie7cc8e582014-09-03 21:35:38 +08002679 btrfs_device_set_total_bytes(device, new_size);
2680 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002681 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002682 if (list_empty(&device->resized_list))
2683 list_add_tail(&device->resized_list,
2684 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002685 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002686
Chris Mason8f18cf12008-04-25 16:53:30 -04002687 return btrfs_update_device(trans, device);
2688}
2689
2690static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002691 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002692{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002693 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002694 int ret;
2695 struct btrfs_path *path;
2696 struct btrfs_key key;
2697
Chris Mason8f18cf12008-04-25 16:53:30 -04002698 path = btrfs_alloc_path();
2699 if (!path)
2700 return -ENOMEM;
2701
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002702 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002703 key.offset = chunk_offset;
2704 key.type = BTRFS_CHUNK_ITEM_KEY;
2705
2706 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002707 if (ret < 0)
2708 goto out;
2709 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002710 btrfs_handle_fs_error(fs_info, -ENOENT,
2711 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002712 ret = -ENOENT;
2713 goto out;
2714 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002715
2716 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002717 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002718 btrfs_handle_fs_error(fs_info, ret,
2719 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002721 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002722 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002723}
2724
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002725static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002726{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002727 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002728 struct btrfs_disk_key *disk_key;
2729 struct btrfs_chunk *chunk;
2730 u8 *ptr;
2731 int ret = 0;
2732 u32 num_stripes;
2733 u32 array_size;
2734 u32 len = 0;
2735 u32 cur;
2736 struct btrfs_key key;
2737
David Sterba34441362016-10-04 19:34:27 +02002738 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002739 array_size = btrfs_super_sys_array_size(super_copy);
2740
2741 ptr = super_copy->sys_chunk_array;
2742 cur = 0;
2743
2744 while (cur < array_size) {
2745 disk_key = (struct btrfs_disk_key *)ptr;
2746 btrfs_disk_key_to_cpu(&key, disk_key);
2747
2748 len = sizeof(*disk_key);
2749
2750 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2751 chunk = (struct btrfs_chunk *)(ptr + len);
2752 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2753 len += btrfs_chunk_item_size(num_stripes);
2754 } else {
2755 ret = -EIO;
2756 break;
2757 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002758 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002759 key.offset == chunk_offset) {
2760 memmove(ptr, ptr + len, array_size - (cur + len));
2761 array_size -= len;
2762 btrfs_set_super_sys_array_size(super_copy, array_size);
2763 } else {
2764 ptr += len;
2765 cur += len;
2766 }
2767 }
David Sterba34441362016-10-04 19:34:27 +02002768 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002769 return ret;
2770}
2771
Liu Bo592d92e2017-03-14 13:33:55 -07002772static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2773 u64 logical, u64 length)
2774{
2775 struct extent_map_tree *em_tree;
2776 struct extent_map *em;
2777
2778 em_tree = &fs_info->mapping_tree.map_tree;
2779 read_lock(&em_tree->lock);
2780 em = lookup_extent_mapping(em_tree, logical, length);
2781 read_unlock(&em_tree->lock);
2782
2783 if (!em) {
2784 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2785 logical, length);
2786 return ERR_PTR(-EINVAL);
2787 }
2788
2789 if (em->start > logical || em->start + em->len < logical) {
2790 btrfs_crit(fs_info,
2791 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2792 logical, length, em->start, em->start + em->len);
2793 free_extent_map(em);
2794 return ERR_PTR(-EINVAL);
2795 }
2796
2797 /* callers are responsible for dropping em's ref. */
2798 return em;
2799}
2800
Josef Bacik47ab2a62014-09-18 11:20:02 -04002801int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002802 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002803{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002804 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002805 struct map_lookup *map;
2806 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002807 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002809
Liu Bo592d92e2017-03-14 13:33:55 -07002810 em = get_chunk_map(fs_info, chunk_offset, 1);
2811 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002812 /*
2813 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002814 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002815 * do anything we still error out.
2816 */
2817 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002818 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002819 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002820 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002821 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002822 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002823 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002824
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002825 /*
2826 * Take the device list mutex to prevent races with the final phase of
2827 * a device replace operation that replaces the device object associated
2828 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2829 */
2830 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002831 for (i = 0; i < map->num_stripes; i++) {
2832 struct btrfs_device *device = map->stripes[i].dev;
2833 ret = btrfs_free_dev_extent(trans, device,
2834 map->stripes[i].physical,
2835 &dev_extent_len);
2836 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002837 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002838 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002839 goto out;
2840 }
2841
2842 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002843 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002844 btrfs_device_set_bytes_used(device,
2845 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002846 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002847 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002848 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002849 }
2850
2851 if (map->stripes[i].dev) {
2852 ret = btrfs_update_device(trans, map->stripes[i].dev);
2853 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002854 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002855 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002856 goto out;
2857 }
2858 }
2859 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002860 mutex_unlock(&fs_devices->device_list_mutex);
2861
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002862 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002863 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002864 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002865 goto out;
2866 }
2867
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002868 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002869
2870 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002871 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002872 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002873 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002874 goto out;
2875 }
2876 }
2877
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002878 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002879 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002880 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002881 goto out;
2882 }
2883
Josef Bacik47ab2a62014-09-18 11:20:02 -04002884out:
2885 /* once for us */
2886 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002887 return ret;
2888}
2889
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002890static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002891{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002892 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002893 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002894 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002895
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002896 /*
2897 * Prevent races with automatic removal of unused block groups.
2898 * After we relocate and before we remove the chunk with offset
2899 * chunk_offset, automatic removal of the block group can kick in,
2900 * resulting in a failure when calling btrfs_remove_chunk() below.
2901 *
2902 * Make sure to acquire this mutex before doing a tree search (dev
2903 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2904 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2905 * we release the path used to search the chunk/dev tree and before
2906 * the current task acquires this mutex and calls us.
2907 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002908 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002909
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002911 if (ret)
2912 return -ENOSPC;
2913
Chris Mason8f18cf12008-04-25 16:53:30 -04002914 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002915 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002916 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002917 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002918 if (ret)
2919 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002920
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002921 /*
2922 * We add the kobjects here (and after forcing data chunk creation)
2923 * since relocation is the only place we'll create chunks of a new
2924 * type at runtime. The only place where we'll remove the last
2925 * chunk of a type is the call immediately below this one. Even
2926 * so, we're protected against races with the cleaner thread since
2927 * we're covered by the delete_unused_bgs_mutex.
2928 */
2929 btrfs_add_raid_kobjects(fs_info);
2930
Chris Mason19c4d2f2016-10-10 13:43:31 -07002931 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2932 chunk_offset);
2933 if (IS_ERR(trans)) {
2934 ret = PTR_ERR(trans);
2935 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2936 return ret;
2937 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002938
Chris Mason19c4d2f2016-10-10 13:43:31 -07002939 /*
2940 * step two, delete the device extents and the
2941 * chunk tree entries
2942 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002943 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002944 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002945 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002946}
2947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002948static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002949{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002950 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002951 struct btrfs_path *path;
2952 struct extent_buffer *leaf;
2953 struct btrfs_chunk *chunk;
2954 struct btrfs_key key;
2955 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002956 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002957 bool retried = false;
2958 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002959 int ret;
2960
2961 path = btrfs_alloc_path();
2962 if (!path)
2963 return -ENOMEM;
2964
Josef Bacikba1bf482009-09-11 16:11:19 -04002965again:
Yan Zheng2b820322008-11-17 21:11:30 -05002966 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2967 key.offset = (u64)-1;
2968 key.type = BTRFS_CHUNK_ITEM_KEY;
2969
2970 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002971 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002972 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002973 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002974 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002975 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002976 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002977 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002978
2979 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2980 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002981 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002982 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002983 if (ret < 0)
2984 goto error;
2985 if (ret > 0)
2986 break;
2987
2988 leaf = path->nodes[0];
2989 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2990
2991 chunk = btrfs_item_ptr(leaf, path->slots[0],
2992 struct btrfs_chunk);
2993 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002994 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002995
2996 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002997 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002998 if (ret == -ENOSPC)
2999 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303000 else
3001 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003002 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003004
3005 if (found_key.offset == 0)
3006 break;
3007 key.offset = found_key.offset - 1;
3008 }
3009 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003010 if (failed && !retried) {
3011 failed = 0;
3012 retried = true;
3013 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303014 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003015 ret = -ENOSPC;
3016 }
Yan Zheng2b820322008-11-17 21:11:30 -05003017error:
3018 btrfs_free_path(path);
3019 return ret;
3020}
3021
Liu Boa6f93c72017-11-15 16:28:11 -07003022/*
3023 * return 1 : allocate a data chunk successfully,
3024 * return <0: errors during allocating a data chunk,
3025 * return 0 : no need to allocate a data chunk.
3026 */
3027static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3028 u64 chunk_offset)
3029{
3030 struct btrfs_block_group_cache *cache;
3031 u64 bytes_used;
3032 u64 chunk_type;
3033
3034 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3035 ASSERT(cache);
3036 chunk_type = cache->flags;
3037 btrfs_put_block_group(cache);
3038
3039 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3040 spin_lock(&fs_info->data_sinfo->lock);
3041 bytes_used = fs_info->data_sinfo->bytes_used;
3042 spin_unlock(&fs_info->data_sinfo->lock);
3043
3044 if (!bytes_used) {
3045 struct btrfs_trans_handle *trans;
3046 int ret;
3047
3048 trans = btrfs_join_transaction(fs_info->tree_root);
3049 if (IS_ERR(trans))
3050 return PTR_ERR(trans);
3051
3052 ret = btrfs_force_chunk_alloc(trans, fs_info,
3053 BTRFS_BLOCK_GROUP_DATA);
3054 btrfs_end_transaction(trans);
3055 if (ret < 0)
3056 return ret;
3057
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003058 btrfs_add_raid_kobjects(fs_info);
3059
Liu Boa6f93c72017-11-15 16:28:11 -07003060 return 1;
3061 }
3062 }
3063 return 0;
3064}
3065
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003066static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003067 struct btrfs_balance_control *bctl)
3068{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003069 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003070 struct btrfs_trans_handle *trans;
3071 struct btrfs_balance_item *item;
3072 struct btrfs_disk_balance_args disk_bargs;
3073 struct btrfs_path *path;
3074 struct extent_buffer *leaf;
3075 struct btrfs_key key;
3076 int ret, err;
3077
3078 path = btrfs_alloc_path();
3079 if (!path)
3080 return -ENOMEM;
3081
3082 trans = btrfs_start_transaction(root, 0);
3083 if (IS_ERR(trans)) {
3084 btrfs_free_path(path);
3085 return PTR_ERR(trans);
3086 }
3087
3088 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003089 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003090 key.offset = 0;
3091
3092 ret = btrfs_insert_empty_item(trans, root, path, &key,
3093 sizeof(*item));
3094 if (ret)
3095 goto out;
3096
3097 leaf = path->nodes[0];
3098 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3099
David Sterbab159fa22016-11-08 18:09:03 +01003100 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003101
3102 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3103 btrfs_set_balance_data(leaf, item, &disk_bargs);
3104 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3105 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3106 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3107 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3108
3109 btrfs_set_balance_flags(leaf, item, bctl->flags);
3110
3111 btrfs_mark_buffer_dirty(leaf);
3112out:
3113 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003114 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003115 if (err && !ret)
3116 ret = err;
3117 return ret;
3118}
3119
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003120static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003121{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003122 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003123 struct btrfs_trans_handle *trans;
3124 struct btrfs_path *path;
3125 struct btrfs_key key;
3126 int ret, err;
3127
3128 path = btrfs_alloc_path();
3129 if (!path)
3130 return -ENOMEM;
3131
3132 trans = btrfs_start_transaction(root, 0);
3133 if (IS_ERR(trans)) {
3134 btrfs_free_path(path);
3135 return PTR_ERR(trans);
3136 }
3137
3138 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003139 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003140 key.offset = 0;
3141
3142 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3143 if (ret < 0)
3144 goto out;
3145 if (ret > 0) {
3146 ret = -ENOENT;
3147 goto out;
3148 }
3149
3150 ret = btrfs_del_item(trans, root, path);
3151out:
3152 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003153 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003154 if (err && !ret)
3155 ret = err;
3156 return ret;
3157}
3158
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003159/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003160 * This is a heuristic used to reduce the number of chunks balanced on
3161 * resume after balance was interrupted.
3162 */
3163static void update_balance_args(struct btrfs_balance_control *bctl)
3164{
3165 /*
3166 * Turn on soft mode for chunk types that were being converted.
3167 */
3168 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3169 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3170 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3171 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3172 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3173 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3174
3175 /*
3176 * Turn on usage filter if is not already used. The idea is
3177 * that chunks that we have already balanced should be
3178 * reasonably full. Don't do it for chunks that are being
3179 * converted - that will keep us from relocating unconverted
3180 * (albeit full) chunks.
3181 */
3182 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003183 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003184 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3185 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3186 bctl->data.usage = 90;
3187 }
3188 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003189 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003190 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3191 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3192 bctl->sys.usage = 90;
3193 }
3194 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003195 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003196 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3197 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3198 bctl->meta.usage = 90;
3199 }
3200}
3201
3202/*
David Sterbadccdb072018-03-21 00:20:05 +01003203 * Should be called with balance mutex held to protect against checking the
3204 * balance status or progress. Same goes for reset_balance_state.
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003205 */
3206static void set_balance_control(struct btrfs_balance_control *bctl)
3207{
3208 struct btrfs_fs_info *fs_info = bctl->fs_info;
3209
3210 BUG_ON(fs_info->balance_ctl);
3211
3212 spin_lock(&fs_info->balance_lock);
3213 fs_info->balance_ctl = bctl;
3214 spin_unlock(&fs_info->balance_lock);
3215}
3216
David Sterba149196a2018-03-20 20:23:09 +01003217/*
3218 * Clear the balance status in fs_info and delete the balance item from disk.
3219 */
3220static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003221{
3222 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003223 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003224
3225 BUG_ON(!fs_info->balance_ctl);
3226
3227 spin_lock(&fs_info->balance_lock);
3228 fs_info->balance_ctl = NULL;
3229 spin_unlock(&fs_info->balance_lock);
3230
3231 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003232 ret = del_balance_item(fs_info);
3233 if (ret)
3234 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003235}
3236
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003237/*
3238 * Balance filters. Return 1 if chunk should be filtered out
3239 * (should not be balanced).
3240 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003241static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003242 struct btrfs_balance_args *bargs)
3243{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003244 chunk_type = chunk_to_extended(chunk_type) &
3245 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003246
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003247 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003248 return 0;
3249
3250 return 1;
3251}
3252
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003253static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003254 struct btrfs_balance_args *bargs)
3255{
3256 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003257 u64 chunk_used;
3258 u64 user_thresh_min;
3259 u64 user_thresh_max;
3260 int ret = 1;
3261
3262 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3263 chunk_used = btrfs_block_group_used(&cache->item);
3264
3265 if (bargs->usage_min == 0)
3266 user_thresh_min = 0;
3267 else
3268 user_thresh_min = div_factor_fine(cache->key.offset,
3269 bargs->usage_min);
3270
3271 if (bargs->usage_max == 0)
3272 user_thresh_max = 1;
3273 else if (bargs->usage_max > 100)
3274 user_thresh_max = cache->key.offset;
3275 else
3276 user_thresh_max = div_factor_fine(cache->key.offset,
3277 bargs->usage_max);
3278
3279 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3280 ret = 0;
3281
3282 btrfs_put_block_group(cache);
3283 return ret;
3284}
3285
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003286static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003287 u64 chunk_offset, struct btrfs_balance_args *bargs)
3288{
3289 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003290 u64 chunk_used, user_thresh;
3291 int ret = 1;
3292
3293 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3294 chunk_used = btrfs_block_group_used(&cache->item);
3295
David Sterbabc309462015-10-20 18:22:13 +02003296 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003297 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003298 else if (bargs->usage > 100)
3299 user_thresh = cache->key.offset;
3300 else
3301 user_thresh = div_factor_fine(cache->key.offset,
3302 bargs->usage);
3303
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003304 if (chunk_used < user_thresh)
3305 ret = 0;
3306
3307 btrfs_put_block_group(cache);
3308 return ret;
3309}
3310
Ilya Dryomov409d4042012-01-16 22:04:47 +02003311static int chunk_devid_filter(struct extent_buffer *leaf,
3312 struct btrfs_chunk *chunk,
3313 struct btrfs_balance_args *bargs)
3314{
3315 struct btrfs_stripe *stripe;
3316 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3317 int i;
3318
3319 for (i = 0; i < num_stripes; i++) {
3320 stripe = btrfs_stripe_nr(chunk, i);
3321 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3322 return 0;
3323 }
3324
3325 return 1;
3326}
3327
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003328/* [pstart, pend) */
3329static int chunk_drange_filter(struct extent_buffer *leaf,
3330 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003331 struct btrfs_balance_args *bargs)
3332{
3333 struct btrfs_stripe *stripe;
3334 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3335 u64 stripe_offset;
3336 u64 stripe_length;
3337 int factor;
3338 int i;
3339
3340 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3341 return 0;
3342
3343 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003344 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3345 factor = num_stripes / 2;
3346 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3347 factor = num_stripes - 1;
3348 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3349 factor = num_stripes - 2;
3350 } else {
3351 factor = num_stripes;
3352 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003353
3354 for (i = 0; i < num_stripes; i++) {
3355 stripe = btrfs_stripe_nr(chunk, i);
3356 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3357 continue;
3358
3359 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3360 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003361 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003362
3363 if (stripe_offset < bargs->pend &&
3364 stripe_offset + stripe_length > bargs->pstart)
3365 return 0;
3366 }
3367
3368 return 1;
3369}
3370
Ilya Dryomovea671762012-01-16 22:04:48 +02003371/* [vstart, vend) */
3372static int chunk_vrange_filter(struct extent_buffer *leaf,
3373 struct btrfs_chunk *chunk,
3374 u64 chunk_offset,
3375 struct btrfs_balance_args *bargs)
3376{
3377 if (chunk_offset < bargs->vend &&
3378 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3379 /* at least part of the chunk is inside this vrange */
3380 return 0;
3381
3382 return 1;
3383}
3384
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003385static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3386 struct btrfs_chunk *chunk,
3387 struct btrfs_balance_args *bargs)
3388{
3389 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3390
3391 if (bargs->stripes_min <= num_stripes
3392 && num_stripes <= bargs->stripes_max)
3393 return 0;
3394
3395 return 1;
3396}
3397
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003398static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003399 struct btrfs_balance_args *bargs)
3400{
3401 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3402 return 0;
3403
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003404 chunk_type = chunk_to_extended(chunk_type) &
3405 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003406
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003407 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003408 return 1;
3409
3410 return 0;
3411}
3412
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003413static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003414 struct extent_buffer *leaf,
3415 struct btrfs_chunk *chunk, u64 chunk_offset)
3416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003417 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003418 struct btrfs_balance_args *bargs = NULL;
3419 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3420
3421 /* type filter */
3422 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3423 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3424 return 0;
3425 }
3426
3427 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3428 bargs = &bctl->data;
3429 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3430 bargs = &bctl->sys;
3431 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3432 bargs = &bctl->meta;
3433
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003434 /* profiles filter */
3435 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3436 chunk_profiles_filter(chunk_type, bargs)) {
3437 return 0;
3438 }
3439
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003440 /* usage filter */
3441 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003443 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003444 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003445 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003446 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003447 }
3448
Ilya Dryomov409d4042012-01-16 22:04:47 +02003449 /* devid filter */
3450 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3451 chunk_devid_filter(leaf, chunk, bargs)) {
3452 return 0;
3453 }
3454
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003455 /* drange filter, makes sense only with devid filter */
3456 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003457 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003458 return 0;
3459 }
3460
Ilya Dryomovea671762012-01-16 22:04:48 +02003461 /* vrange filter */
3462 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3463 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3464 return 0;
3465 }
3466
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003467 /* stripes filter */
3468 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3469 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3470 return 0;
3471 }
3472
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003473 /* soft profile changing mode */
3474 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3475 chunk_soft_convert_filter(chunk_type, bargs)) {
3476 return 0;
3477 }
3478
David Sterba7d824b62014-05-07 17:37:51 +02003479 /*
3480 * limited by count, must be the last filter
3481 */
3482 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3483 if (bargs->limit == 0)
3484 return 0;
3485 else
3486 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003487 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3488 /*
3489 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003490 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003491 * about the count of all chunks that satisfy the filters.
3492 */
3493 if (bargs->limit_max == 0)
3494 return 0;
3495 else
3496 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003497 }
3498
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003499 return 1;
3500}
3501
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003502static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003503{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003504 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003505 struct btrfs_root *chunk_root = fs_info->chunk_root;
3506 struct btrfs_root *dev_root = fs_info->dev_root;
3507 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003508 struct btrfs_device *device;
3509 u64 old_size;
3510 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003511 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003512 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003513 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003514 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003515 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003516 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003517 struct extent_buffer *leaf;
3518 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003519 int ret;
3520 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003521 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003522 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003523 u64 limit_data = bctl->data.limit;
3524 u64 limit_meta = bctl->meta.limit;
3525 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003526 u32 count_data = 0;
3527 u32 count_meta = 0;
3528 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003529 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003530
Chris Masonec44a352008-04-28 15:29:52 -04003531 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003532 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003533 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003534 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003535 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003536 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003537 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003538 btrfs_device_get_total_bytes(device) -
3539 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003540 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003541 continue;
3542
3543 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003544 if (ret == -ENOSPC)
3545 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003546 if (ret) {
3547 /* btrfs_shrink_device never returns ret > 0 */
3548 WARN_ON(ret > 0);
3549 goto error;
3550 }
Chris Masonec44a352008-04-28 15:29:52 -04003551
Yan, Zhenga22285a2010-05-16 10:48:46 -04003552 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003553 if (IS_ERR(trans)) {
3554 ret = PTR_ERR(trans);
3555 btrfs_info_in_rcu(fs_info,
3556 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3557 rcu_str_deref(device->name), ret,
3558 old_size, old_size - size_to_free);
3559 goto error;
3560 }
Chris Masonec44a352008-04-28 15:29:52 -04003561
3562 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003563 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003564 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003565 /* btrfs_grow_device never returns ret > 0 */
3566 WARN_ON(ret > 0);
3567 btrfs_info_in_rcu(fs_info,
3568 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3569 rcu_str_deref(device->name), ret,
3570 old_size, old_size - size_to_free);
3571 goto error;
3572 }
Chris Masonec44a352008-04-28 15:29:52 -04003573
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003574 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003575 }
3576
3577 /* step two, relocate all the chunks */
3578 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003579 if (!path) {
3580 ret = -ENOMEM;
3581 goto error;
3582 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003583
3584 /* zero out stat counters */
3585 spin_lock(&fs_info->balance_lock);
3586 memset(&bctl->stat, 0, sizeof(bctl->stat));
3587 spin_unlock(&fs_info->balance_lock);
3588again:
David Sterba7d824b62014-05-07 17:37:51 +02003589 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003590 /*
3591 * The single value limit and min/max limits use the same bytes
3592 * in the
3593 */
David Sterba7d824b62014-05-07 17:37:51 +02003594 bctl->data.limit = limit_data;
3595 bctl->meta.limit = limit_meta;
3596 bctl->sys.limit = limit_sys;
3597 }
Chris Masonec44a352008-04-28 15:29:52 -04003598 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3599 key.offset = (u64)-1;
3600 key.type = BTRFS_CHUNK_ITEM_KEY;
3601
Chris Masond3977122009-01-05 21:25:51 -05003602 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003603 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003604 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003605 ret = -ECANCELED;
3606 goto error;
3607 }
3608
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003609 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003610 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003611 if (ret < 0) {
3612 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003613 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003614 }
Chris Masonec44a352008-04-28 15:29:52 -04003615
3616 /*
3617 * this shouldn't happen, it means the last relocate
3618 * failed
3619 */
3620 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003621 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003622
3623 ret = btrfs_previous_item(chunk_root, path, 0,
3624 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003625 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003626 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003627 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003628 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003629 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003630
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003631 leaf = path->nodes[0];
3632 slot = path->slots[0];
3633 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3634
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003635 if (found_key.objectid != key.objectid) {
3636 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003637 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003638 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003639
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003640 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003641 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003642
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003643 if (!counting) {
3644 spin_lock(&fs_info->balance_lock);
3645 bctl->stat.considered++;
3646 spin_unlock(&fs_info->balance_lock);
3647 }
3648
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003649 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003650 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003651
David Sterbab3b4aa72011-04-21 01:20:15 +02003652 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003653 if (!ret) {
3654 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003655 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003656 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003657
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003658 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003659 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003660 spin_lock(&fs_info->balance_lock);
3661 bctl->stat.expected++;
3662 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003663
3664 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3665 count_data++;
3666 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3667 count_sys++;
3668 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3669 count_meta++;
3670
3671 goto loop;
3672 }
3673
3674 /*
3675 * Apply limit_min filter, no need to check if the LIMITS
3676 * filter is used, limit_min is 0 by default
3677 */
3678 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3679 count_data < bctl->data.limit_min)
3680 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3681 count_meta < bctl->meta.limit_min)
3682 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3683 count_sys < bctl->sys.limit_min)) {
3684 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003685 goto loop;
3686 }
3687
Liu Boa6f93c72017-11-15 16:28:11 -07003688 if (!chunk_reserved) {
3689 /*
3690 * We may be relocating the only data chunk we have,
3691 * which could potentially end up with losing data's
3692 * raid profile, so lets allocate an empty one in
3693 * advance.
3694 */
3695 ret = btrfs_may_alloc_data_chunk(fs_info,
3696 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003697 if (ret < 0) {
3698 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3699 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003700 } else if (ret == 1) {
3701 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003702 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003703 }
3704
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003705 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003706 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003707 if (ret && ret != -ENOSPC)
3708 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003709 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003710 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003711 } else {
3712 spin_lock(&fs_info->balance_lock);
3713 bctl->stat.completed++;
3714 spin_unlock(&fs_info->balance_lock);
3715 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003716loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003717 if (found_key.offset == 0)
3718 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003719 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003720 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003721
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003722 if (counting) {
3723 btrfs_release_path(path);
3724 counting = false;
3725 goto again;
3726 }
Chris Masonec44a352008-04-28 15:29:52 -04003727error:
3728 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003729 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003730 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003731 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003732 if (!ret)
3733 ret = -ENOSPC;
3734 }
3735
Chris Masonec44a352008-04-28 15:29:52 -04003736 return ret;
3737}
3738
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003739/**
3740 * alloc_profile_is_valid - see if a given profile is valid and reduced
3741 * @flags: profile to validate
3742 * @extended: if true @flags is treated as an extended profile
3743 */
3744static int alloc_profile_is_valid(u64 flags, int extended)
3745{
3746 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3747 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3748
3749 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3750
3751 /* 1) check that all other bits are zeroed */
3752 if (flags & ~mask)
3753 return 0;
3754
3755 /* 2) see if profile is reduced */
3756 if (flags == 0)
3757 return !extended; /* "0" is valid for usual profiles */
3758
3759 /* true if exactly one bit set */
3760 return (flags & (flags - 1)) == 0;
3761}
3762
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003763static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3764{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003765 /* cancel requested || normal exit path */
3766 return atomic_read(&fs_info->balance_cancel_req) ||
3767 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3768 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003769}
3770
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003771/* Non-zero return value signifies invalidity */
3772static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3773 u64 allowed)
3774{
3775 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3776 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3777 (bctl_arg->target & ~allowed)));
3778}
3779
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003780/*
David Sterbadccdb072018-03-21 00:20:05 +01003781 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003782 */
3783int btrfs_balance(struct btrfs_balance_control *bctl,
3784 struct btrfs_ioctl_balance_args *bargs)
3785{
3786 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003787 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003788 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003789 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003790 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003791 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003792 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003793
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003794 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003795 atomic_read(&fs_info->balance_pause_req) ||
3796 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003797 ret = -EINVAL;
3798 goto out;
3799 }
3800
Ilya Dryomove4837f82012-03-27 17:09:17 +03003801 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3802 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3803 mixed = 1;
3804
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003805 /*
3806 * In case of mixed groups both data and meta should be picked,
3807 * and identical options should be given for both of them.
3808 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003809 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3810 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003811 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3812 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3813 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003814 btrfs_err(fs_info,
3815 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003816 ret = -EINVAL;
3817 goto out;
3818 }
3819 }
3820
Stefan Behrens8dabb742012-11-06 13:15:27 +01003821 num_devices = fs_info->fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01003822 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003823 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3824 BUG_ON(num_devices < 1);
3825 num_devices--;
3826 }
David Sterba7e79cb82018-03-24 02:11:38 +01003827 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003828 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3829 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003830 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003831 if (num_devices > 2)
3832 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3833 if (num_devices > 3)
3834 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3835 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003836 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003837 btrfs_err(fs_info,
3838 "unable to start balance with target data profile %llu",
3839 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003840 ret = -EINVAL;
3841 goto out;
3842 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003843 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003844 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003845 "unable to start balance with target metadata profile %llu",
3846 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003847 ret = -EINVAL;
3848 goto out;
3849 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003850 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003851 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003852 "unable to start balance with target system profile %llu",
3853 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003854 ret = -EINVAL;
3855 goto out;
3856 }
3857
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003858 /* allow to reduce meta or sys integrity only if force set */
3859 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003860 BTRFS_BLOCK_GROUP_RAID10 |
3861 BTRFS_BLOCK_GROUP_RAID5 |
3862 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003863 do {
3864 seq = read_seqbegin(&fs_info->profiles_lock);
3865
3866 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3867 (fs_info->avail_system_alloc_bits & allowed) &&
3868 !(bctl->sys.target & allowed)) ||
3869 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3870 (fs_info->avail_metadata_alloc_bits & allowed) &&
3871 !(bctl->meta.target & allowed))) {
3872 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003873 btrfs_info(fs_info,
3874 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003875 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003876 btrfs_err(fs_info,
3877 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003878 ret = -EINVAL;
3879 goto out;
3880 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003881 }
Miao Xiede98ced2013-01-29 10:13:12 +00003882 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003883
Adam Borowski14506122017-03-07 23:34:44 +01003884 /* if we're not converting, the target field is uninitialized */
3885 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3886 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3887 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3888 bctl->data.target : fs_info->avail_data_alloc_bits;
3889 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3890 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003891 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003892 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003893 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003894 }
3895
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003896 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003897 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003898 goto out;
3899
Ilya Dryomov59641012012-01-16 22:04:48 +02003900 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3901 BUG_ON(ret == -EEXIST);
3902 set_balance_control(bctl);
3903 } else {
3904 BUG_ON(ret != -EEXIST);
3905 spin_lock(&fs_info->balance_lock);
3906 update_balance_args(bctl);
3907 spin_unlock(&fs_info->balance_lock);
3908 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003909
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003910 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003911 mutex_unlock(&fs_info->balance_mutex);
3912
3913 ret = __btrfs_balance(fs_info);
3914
3915 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003916 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003917
3918 if (bargs) {
3919 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003920 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003921 }
3922
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003923 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3924 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01003925 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003926 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003927 }
3928
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003929 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003930
3931 return ret;
3932out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003933 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01003934 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003935 else
Ilya Dryomov59641012012-01-16 22:04:48 +02003936 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01003937 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3938
Ilya Dryomov59641012012-01-16 22:04:48 +02003939 return ret;
3940}
3941
3942static int balance_kthread(void *data)
3943{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003944 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003945 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003946
Ilya Dryomov59641012012-01-16 22:04:48 +02003947 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003948 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003949 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003950 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003951 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003952 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003953
Ilya Dryomov59641012012-01-16 22:04:48 +02003954 return ret;
3955}
3956
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003957int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3958{
3959 struct task_struct *tsk;
3960
3961 spin_lock(&fs_info->balance_lock);
3962 if (!fs_info->balance_ctl) {
3963 spin_unlock(&fs_info->balance_lock);
3964 return 0;
3965 }
3966 spin_unlock(&fs_info->balance_lock);
3967
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003968 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003969 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003970 return 0;
3971 }
3972
Anand Jain02ee6542018-05-17 15:16:51 +08003973 /*
3974 * A ro->rw remount sequence should continue with the paused balance
3975 * regardless of who pauses it, system or the user as of now, so set
3976 * the resume flag.
3977 */
3978 spin_lock(&fs_info->balance_lock);
3979 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
3980 spin_unlock(&fs_info->balance_lock);
3981
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003982 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303983 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003984}
3985
Ilya Dryomov68310a52012-06-22 12:24:12 -06003986int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003987{
Ilya Dryomov59641012012-01-16 22:04:48 +02003988 struct btrfs_balance_control *bctl;
3989 struct btrfs_balance_item *item;
3990 struct btrfs_disk_balance_args disk_bargs;
3991 struct btrfs_path *path;
3992 struct extent_buffer *leaf;
3993 struct btrfs_key key;
3994 int ret;
3995
3996 path = btrfs_alloc_path();
3997 if (!path)
3998 return -ENOMEM;
3999
Ilya Dryomov68310a52012-06-22 12:24:12 -06004000 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004001 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004002 key.offset = 0;
4003
4004 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4005 if (ret < 0)
4006 goto out;
4007 if (ret > 0) { /* ret = -ENOENT; */
4008 ret = 0;
4009 goto out;
4010 }
4011
Ilya Dryomov59641012012-01-16 22:04:48 +02004012 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4013 if (!bctl) {
4014 ret = -ENOMEM;
4015 goto out;
4016 }
4017
Ilya Dryomov59641012012-01-16 22:04:48 +02004018 leaf = path->nodes[0];
4019 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4020
Ilya Dryomov68310a52012-06-22 12:24:12 -06004021 bctl->fs_info = fs_info;
4022 bctl->flags = btrfs_balance_flags(leaf, item);
4023 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004024
4025 btrfs_balance_data(leaf, item, &disk_bargs);
4026 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4027 btrfs_balance_meta(leaf, item, &disk_bargs);
4028 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4029 btrfs_balance_sys(leaf, item, &disk_bargs);
4030 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4031
David Sterbaeee95e32018-03-20 20:07:58 +01004032 /*
4033 * This should never happen, as the paused balance state is recovered
4034 * during mount without any chance of other exclusive ops to collide.
4035 *
4036 * This gives the exclusive op status to balance and keeps in paused
4037 * state until user intervention (cancel or umount). If the ownership
4038 * cannot be assigned, show a message but do not fail. The balance
4039 * is in a paused state and must have fs_info::balance_ctl properly
4040 * set up.
4041 */
4042 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4043 btrfs_warn(fs_info,
4044 "cannot set exclusive op status to balance, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004045
Ilya Dryomov68310a52012-06-22 12:24:12 -06004046 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004047 set_balance_control(bctl);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004048 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004049out:
4050 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004051 return ret;
4052}
4053
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004054int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4055{
4056 int ret = 0;
4057
4058 mutex_lock(&fs_info->balance_mutex);
4059 if (!fs_info->balance_ctl) {
4060 mutex_unlock(&fs_info->balance_mutex);
4061 return -ENOTCONN;
4062 }
4063
4064 if (atomic_read(&fs_info->balance_running)) {
4065 atomic_inc(&fs_info->balance_pause_req);
4066 mutex_unlock(&fs_info->balance_mutex);
4067
4068 wait_event(fs_info->balance_wait_q,
4069 atomic_read(&fs_info->balance_running) == 0);
4070
4071 mutex_lock(&fs_info->balance_mutex);
4072 /* we are good with balance_ctl ripped off from under us */
4073 BUG_ON(atomic_read(&fs_info->balance_running));
4074 atomic_dec(&fs_info->balance_pause_req);
4075 } else {
4076 ret = -ENOTCONN;
4077 }
4078
4079 mutex_unlock(&fs_info->balance_mutex);
4080 return ret;
4081}
4082
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004083int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4084{
David Howellsbc98a422017-07-17 08:45:34 +01004085 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004086 return -EROFS;
4087
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004088 mutex_lock(&fs_info->balance_mutex);
4089 if (!fs_info->balance_ctl) {
4090 mutex_unlock(&fs_info->balance_mutex);
4091 return -ENOTCONN;
4092 }
4093
4094 atomic_inc(&fs_info->balance_cancel_req);
4095 /*
4096 * if we are running just wait and return, balance item is
4097 * deleted in btrfs_balance in this case
4098 */
4099 if (atomic_read(&fs_info->balance_running)) {
4100 mutex_unlock(&fs_info->balance_mutex);
4101 wait_event(fs_info->balance_wait_q,
4102 atomic_read(&fs_info->balance_running) == 0);
4103 mutex_lock(&fs_info->balance_mutex);
4104 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004105 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004106 /*
4107 * Lock released to allow other waiters to continue, we'll
4108 * reexamine the status again.
4109 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004110 mutex_lock(&fs_info->balance_mutex);
4111
David Sterbaa17c95d2018-03-20 17:28:05 +01004112 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004113 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004114 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4115 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004116 }
4117
4118 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4119 atomic_dec(&fs_info->balance_cancel_req);
4120 mutex_unlock(&fs_info->balance_mutex);
4121 return 0;
4122}
4123
Stefan Behrens803b2f52013-08-15 17:11:21 +02004124static int btrfs_uuid_scan_kthread(void *data)
4125{
4126 struct btrfs_fs_info *fs_info = data;
4127 struct btrfs_root *root = fs_info->tree_root;
4128 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004129 struct btrfs_path *path = NULL;
4130 int ret = 0;
4131 struct extent_buffer *eb;
4132 int slot;
4133 struct btrfs_root_item root_item;
4134 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004135 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004136
4137 path = btrfs_alloc_path();
4138 if (!path) {
4139 ret = -ENOMEM;
4140 goto out;
4141 }
4142
4143 key.objectid = 0;
4144 key.type = BTRFS_ROOT_ITEM_KEY;
4145 key.offset = 0;
4146
Stefan Behrens803b2f52013-08-15 17:11:21 +02004147 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004148 ret = btrfs_search_forward(root, &key, path,
4149 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004150 if (ret) {
4151 if (ret > 0)
4152 ret = 0;
4153 break;
4154 }
4155
4156 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4157 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4158 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4159 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4160 goto skip;
4161
4162 eb = path->nodes[0];
4163 slot = path->slots[0];
4164 item_size = btrfs_item_size_nr(eb, slot);
4165 if (item_size < sizeof(root_item))
4166 goto skip;
4167
Stefan Behrens803b2f52013-08-15 17:11:21 +02004168 read_extent_buffer(eb, &root_item,
4169 btrfs_item_ptr_offset(eb, slot),
4170 (int)sizeof(root_item));
4171 if (btrfs_root_refs(&root_item) == 0)
4172 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004173
4174 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4175 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4176 if (trans)
4177 goto update_tree;
4178
4179 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004180 /*
4181 * 1 - subvol uuid item
4182 * 1 - received_subvol uuid item
4183 */
4184 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4185 if (IS_ERR(trans)) {
4186 ret = PTR_ERR(trans);
4187 break;
4188 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004189 continue;
4190 } else {
4191 goto skip;
4192 }
4193update_tree:
4194 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004195 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004196 root_item.uuid,
4197 BTRFS_UUID_KEY_SUBVOL,
4198 key.objectid);
4199 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004200 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004201 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004202 break;
4203 }
4204 }
4205
4206 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004207 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004208 root_item.received_uuid,
4209 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4210 key.objectid);
4211 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004212 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004213 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004214 break;
4215 }
4216 }
4217
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004218skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004219 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004220 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004221 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004222 if (ret)
4223 break;
4224 }
4225
Stefan Behrens803b2f52013-08-15 17:11:21 +02004226 btrfs_release_path(path);
4227 if (key.offset < (u64)-1) {
4228 key.offset++;
4229 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4230 key.offset = 0;
4231 key.type = BTRFS_ROOT_ITEM_KEY;
4232 } else if (key.objectid < (u64)-1) {
4233 key.offset = 0;
4234 key.type = BTRFS_ROOT_ITEM_KEY;
4235 key.objectid++;
4236 } else {
4237 break;
4238 }
4239 cond_resched();
4240 }
4241
4242out:
4243 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004244 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004245 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004246 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004247 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004248 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004249 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004250 up(&fs_info->uuid_tree_rescan_sem);
4251 return 0;
4252}
4253
Stefan Behrens70f80172013-08-15 17:11:23 +02004254/*
4255 * Callback for btrfs_uuid_tree_iterate().
4256 * returns:
4257 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004258 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004259 * > 0 if the check failed, which means the caller shall remove the entry.
4260 */
4261static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4262 u8 *uuid, u8 type, u64 subid)
4263{
4264 struct btrfs_key key;
4265 int ret = 0;
4266 struct btrfs_root *subvol_root;
4267
4268 if (type != BTRFS_UUID_KEY_SUBVOL &&
4269 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4270 goto out;
4271
4272 key.objectid = subid;
4273 key.type = BTRFS_ROOT_ITEM_KEY;
4274 key.offset = (u64)-1;
4275 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4276 if (IS_ERR(subvol_root)) {
4277 ret = PTR_ERR(subvol_root);
4278 if (ret == -ENOENT)
4279 ret = 1;
4280 goto out;
4281 }
4282
4283 switch (type) {
4284 case BTRFS_UUID_KEY_SUBVOL:
4285 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4286 ret = 1;
4287 break;
4288 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4289 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4290 BTRFS_UUID_SIZE))
4291 ret = 1;
4292 break;
4293 }
4294
4295out:
4296 return ret;
4297}
4298
4299static int btrfs_uuid_rescan_kthread(void *data)
4300{
4301 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4302 int ret;
4303
4304 /*
4305 * 1st step is to iterate through the existing UUID tree and
4306 * to delete all entries that contain outdated data.
4307 * 2nd step is to add all missing entries to the UUID tree.
4308 */
4309 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4310 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004311 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004312 up(&fs_info->uuid_tree_rescan_sem);
4313 return ret;
4314 }
4315 return btrfs_uuid_scan_kthread(data);
4316}
4317
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004318int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4319{
4320 struct btrfs_trans_handle *trans;
4321 struct btrfs_root *tree_root = fs_info->tree_root;
4322 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004323 struct task_struct *task;
4324 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004325
4326 /*
4327 * 1 - root node
4328 * 1 - root item
4329 */
4330 trans = btrfs_start_transaction(tree_root, 2);
4331 if (IS_ERR(trans))
4332 return PTR_ERR(trans);
4333
4334 uuid_root = btrfs_create_tree(trans, fs_info,
4335 BTRFS_UUID_TREE_OBJECTID);
4336 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004337 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004338 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004339 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004340 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004341 }
4342
4343 fs_info->uuid_root = uuid_root;
4344
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004345 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004346 if (ret)
4347 return ret;
4348
4349 down(&fs_info->uuid_tree_rescan_sem);
4350 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4351 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004352 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004353 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004354 up(&fs_info->uuid_tree_rescan_sem);
4355 return PTR_ERR(task);
4356 }
4357
4358 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004359}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004360
Stefan Behrens70f80172013-08-15 17:11:23 +02004361int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4362{
4363 struct task_struct *task;
4364
4365 down(&fs_info->uuid_tree_rescan_sem);
4366 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4367 if (IS_ERR(task)) {
4368 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004369 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004370 up(&fs_info->uuid_tree_rescan_sem);
4371 return PTR_ERR(task);
4372 }
4373
4374 return 0;
4375}
4376
Chris Mason8f18cf12008-04-25 16:53:30 -04004377/*
4378 * shrinking a device means finding all of the device extents past
4379 * the new size, and then following the back refs to the chunks.
4380 * The chunk relocation code actually frees the device extent
4381 */
4382int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4383{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004384 struct btrfs_fs_info *fs_info = device->fs_info;
4385 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004386 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004387 struct btrfs_dev_extent *dev_extent = NULL;
4388 struct btrfs_path *path;
4389 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004390 u64 chunk_offset;
4391 int ret;
4392 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004393 int failed = 0;
4394 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004395 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004396 struct extent_buffer *l;
4397 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004398 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004399 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004400 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004401 u64 diff;
4402
4403 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004404 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004405
Anand Jain401e29c2017-12-04 12:54:55 +08004406 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004407 return -EINVAL;
4408
Chris Mason8f18cf12008-04-25 16:53:30 -04004409 path = btrfs_alloc_path();
4410 if (!path)
4411 return -ENOMEM;
4412
David Sterbae4058b52015-11-27 16:31:35 +01004413 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004414
David Sterba34441362016-10-04 19:34:27 +02004415 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004416
Miao Xie7cc8e582014-09-03 21:35:38 +08004417 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004418 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004419 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004420 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004421 }
David Sterba34441362016-10-04 19:34:27 +02004422 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004423
Josef Bacikba1bf482009-09-11 16:11:19 -04004424again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004425 key.objectid = device->devid;
4426 key.offset = (u64)-1;
4427 key.type = BTRFS_DEV_EXTENT_KEY;
4428
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004429 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004431 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004432 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004433 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004434 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004435 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004436
4437 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004438 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004439 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004440 if (ret < 0)
4441 goto done;
4442 if (ret) {
4443 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004444 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004445 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004446 }
4447
4448 l = path->nodes[0];
4449 slot = path->slots[0];
4450 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4451
Josef Bacikba1bf482009-09-11 16:11:19 -04004452 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004453 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004454 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004455 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004456 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004457
4458 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4459 length = btrfs_dev_extent_length(l, dev_extent);
4460
Josef Bacikba1bf482009-09-11 16:11:19 -04004461 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004462 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004463 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004464 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004465 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004466
Chris Mason8f18cf12008-04-25 16:53:30 -04004467 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004468 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004469
Liu Boa6f93c72017-11-15 16:28:11 -07004470 /*
4471 * We may be relocating the only data chunk we have,
4472 * which could potentially end up with losing data's
4473 * raid profile, so lets allocate an empty one in
4474 * advance.
4475 */
4476 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4477 if (ret < 0) {
4478 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4479 goto done;
4480 }
4481
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004482 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4483 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004484 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004485 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004486 if (ret == -ENOSPC)
4487 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004488 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004489
4490 if (failed && !retried) {
4491 failed = 0;
4492 retried = true;
4493 goto again;
4494 } else if (failed && retried) {
4495 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004496 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004497 }
4498
Chris Balld6397ba2009-04-27 07:29:03 -04004499 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004500 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004501 if (IS_ERR(trans)) {
4502 ret = PTR_ERR(trans);
4503 goto done;
4504 }
4505
David Sterba34441362016-10-04 19:34:27 +02004506 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004507
4508 /*
4509 * We checked in the above loop all device extents that were already in
4510 * the device tree. However before we have updated the device's
4511 * total_bytes to the new size, we might have had chunk allocations that
4512 * have not complete yet (new block groups attached to transaction
4513 * handles), and therefore their device extents were not yet in the
4514 * device tree and we missed them in the loop above. So if we have any
4515 * pending chunk using a device extent that overlaps the device range
4516 * that we can not use anymore, commit the current transaction and
4517 * repeat the search on the device tree - this way we guarantee we will
4518 * not have chunks using device extents that end beyond 'new_size'.
4519 */
4520 if (!checked_pending_chunks) {
4521 u64 start = new_size;
4522 u64 len = old_size - new_size;
4523
Jeff Mahoney499f3772015-06-15 09:41:17 -04004524 if (contains_pending_extent(trans->transaction, device,
4525 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004526 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004527 checked_pending_chunks = true;
4528 failed = 0;
4529 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004530 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004531 if (ret)
4532 goto done;
4533 goto again;
4534 }
4535 }
4536
Miao Xie7cc8e582014-09-03 21:35:38 +08004537 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004538 if (list_empty(&device->resized_list))
4539 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004540 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004541
Chris Balld6397ba2009-04-27 07:29:03 -04004542 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004543 btrfs_set_super_total_bytes(super_copy,
4544 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004545 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004546
4547 /* Now btrfs_update_device() will change the on-disk size. */
4548 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004549 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004550done:
4551 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004552 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004553 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004554 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004555 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004556 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004557 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004558 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004559 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004560 return ret;
4561}
4562
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004563static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004564 struct btrfs_key *key,
4565 struct btrfs_chunk *chunk, int item_size)
4566{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004567 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004568 struct btrfs_disk_key disk_key;
4569 u32 array_size;
4570 u8 *ptr;
4571
David Sterba34441362016-10-04 19:34:27 +02004572 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004573 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004574 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004575 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004576 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004577 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004578 }
Chris Mason0b86a832008-03-24 15:01:56 -04004579
4580 ptr = super_copy->sys_chunk_array + array_size;
4581 btrfs_cpu_key_to_disk(&disk_key, key);
4582 memcpy(ptr, &disk_key, sizeof(disk_key));
4583 ptr += sizeof(disk_key);
4584 memcpy(ptr, chunk, item_size);
4585 item_size += sizeof(disk_key);
4586 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004587 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004588
Chris Mason0b86a832008-03-24 15:01:56 -04004589 return 0;
4590}
4591
Miao Xieb2117a32011-01-05 10:07:28 +00004592/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004593 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004594 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004595static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004596{
Arne Jansen73c5de02011-04-12 12:07:57 +02004597 const struct btrfs_device_info *di_a = a;
4598 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004599
Arne Jansen73c5de02011-04-12 12:07:57 +02004600 if (di_a->max_avail > di_b->max_avail)
4601 return -1;
4602 if (di_a->max_avail < di_b->max_avail)
4603 return 1;
4604 if (di_a->total_avail > di_b->total_avail)
4605 return -1;
4606 if (di_a->total_avail < di_b->total_avail)
4607 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004608 return 0;
4609}
4610
David Woodhouse53b381b2013-01-29 18:40:14 -05004611static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4612{
Zhao Leiffe2d202015-01-20 15:11:44 +08004613 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004614 return;
4615
Miao Xieceda0862013-04-11 10:30:16 +00004616 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004617}
4618
Qu Wenruo062d4d12018-01-30 18:20:46 +08004619#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004620 - sizeof(struct btrfs_chunk)) \
4621 / sizeof(struct btrfs_stripe) + 1)
4622
4623#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4624 - 2 * sizeof(struct btrfs_disk_key) \
4625 - 2 * sizeof(struct btrfs_chunk)) \
4626 / sizeof(struct btrfs_stripe) + 1)
4627
Miao Xieb2117a32011-01-05 10:07:28 +00004628static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004629 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004630{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004631 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004632 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004633 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004634 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004635 struct extent_map_tree *em_tree;
4636 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004637 struct btrfs_device_info *devices_info = NULL;
4638 u64 total_avail;
4639 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004640 int data_stripes; /* number of stripes that count for
4641 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004642 int sub_stripes; /* sub_stripes info for map */
4643 int dev_stripes; /* stripes per dev */
4644 int devs_max; /* max devs to use */
4645 int devs_min; /* min devs needed */
4646 int devs_increment; /* ndevs has to be a multiple of this */
4647 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004648 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004649 u64 max_stripe_size;
4650 u64 max_chunk_size;
4651 u64 stripe_size;
4652 u64 num_bytes;
4653 int ndevs;
4654 int i;
4655 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004656 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004657
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004658 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004659
Qu Wenruo4117f202018-01-22 13:50:54 +08004660 if (list_empty(&fs_devices->alloc_list)) {
4661 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4662 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004663 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004664 }
Miao Xieb2117a32011-01-05 10:07:28 +00004665
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004666 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004667
Liu Bo31e50222012-11-21 14:18:10 +00004668 sub_stripes = btrfs_raid_array[index].sub_stripes;
4669 dev_stripes = btrfs_raid_array[index].dev_stripes;
4670 devs_max = btrfs_raid_array[index].devs_max;
4671 devs_min = btrfs_raid_array[index].devs_min;
4672 devs_increment = btrfs_raid_array[index].devs_increment;
4673 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004674
4675 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004676 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004677 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004678 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004679 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004680 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004681 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004682 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4683 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004684 else
Byongho Leeee221842015-12-15 01:42:10 +09004685 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004686 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004687 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004688 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004689 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004690 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004691 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004692 if (!devs_max)
4693 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004694 } else {
David Sterba351fd352014-05-15 16:48:20 +02004695 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004696 type);
4697 BUG_ON(1);
4698 }
4699
4700 /* we don't want a chunk larger than 10% of writeable space */
4701 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4702 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004703
David Sterba31e818f2015-02-20 18:00:26 +01004704 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004705 GFP_NOFS);
4706 if (!devices_info)
4707 return -ENOMEM;
4708
Arne Jansen73c5de02011-04-12 12:07:57 +02004709 /*
4710 * in the first pass through the devices list, we gather information
4711 * about the available holes on each device.
4712 */
4713 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004714 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004715 u64 max_avail;
4716 u64 dev_offset;
4717
Anand Jainebbede42017-12-04 12:54:52 +08004718 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004719 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004720 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004721 continue;
4722 }
4723
Anand Jaine12c9622017-12-04 12:54:53 +08004724 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4725 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004726 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004727 continue;
4728
4729 if (device->total_bytes > device->bytes_used)
4730 total_avail = device->total_bytes - device->bytes_used;
4731 else
4732 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004733
4734 /* If there is no space on this device, skip it. */
4735 if (total_avail == 0)
4736 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004737
Josef Bacik6df9a952013-06-27 13:22:46 -04004738 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004739 max_stripe_size * dev_stripes,
4740 &dev_offset, &max_avail);
4741 if (ret && ret != -ENOSPC)
4742 goto error;
4743
4744 if (ret == 0)
4745 max_avail = max_stripe_size * dev_stripes;
4746
Qu Wenruo4117f202018-01-22 13:50:54 +08004747 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4748 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4749 btrfs_debug(info,
4750 "%s: devid %llu has no free space, have=%llu want=%u",
4751 __func__, device->devid, max_avail,
4752 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004753 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004754 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004755
Eric Sandeen063d0062013-01-31 00:55:01 +00004756 if (ndevs == fs_devices->rw_devices) {
4757 WARN(1, "%s: found more than %llu devices\n",
4758 __func__, fs_devices->rw_devices);
4759 break;
4760 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004761 devices_info[ndevs].dev_offset = dev_offset;
4762 devices_info[ndevs].max_avail = max_avail;
4763 devices_info[ndevs].total_avail = total_avail;
4764 devices_info[ndevs].dev = device;
4765 ++ndevs;
4766 }
4767
4768 /*
4769 * now sort the devices by hole size / available space
4770 */
4771 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4772 btrfs_cmp_device_info, NULL);
4773
4774 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004775 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004776
Qu Wenruoba89b802018-01-31 13:56:15 +08004777 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004778 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004779 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4780 btrfs_debug(info,
4781 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004782 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004783 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004784 goto error;
4785 }
4786
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004787 ndevs = min(ndevs, devs_max);
4788
Arne Jansen73c5de02011-04-12 12:07:57 +02004789 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004790 * The primary goal is to maximize the number of stripes, so use as
4791 * many devices as possible, even if the stripes are not maximum sized.
4792 *
4793 * The DUP profile stores more than one stripe per device, the
4794 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004795 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004796 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004797 num_stripes = ndevs * dev_stripes;
4798
David Woodhouse53b381b2013-01-29 18:40:14 -05004799 /*
4800 * this will have to be fixed for RAID1 and RAID10 over
4801 * more drives
4802 */
4803 data_stripes = num_stripes / ncopies;
4804
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004805 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004806 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004807
4808 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004809 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004810
4811 /*
4812 * Use the number of data stripes to figure out how big this chunk
4813 * is really going to be in terms of logical address space,
4814 * and compare that answer with the max chunk size
4815 */
4816 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004817 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004818
4819 /* bump the answer up to a 16MB boundary */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004820 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004821
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004822 /*
4823 * But don't go higher than the limits we found while searching
4824 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004825 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004826 stripe_size = min(devices_info[ndevs - 1].max_avail,
4827 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004828 }
4829
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004830 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004831 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004832
Miao Xieb2117a32011-01-05 10:07:28 +00004833 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4834 if (!map) {
4835 ret = -ENOMEM;
4836 goto error;
4837 }
4838 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004839
Arne Jansen73c5de02011-04-12 12:07:57 +02004840 for (i = 0; i < ndevs; ++i) {
4841 for (j = 0; j < dev_stripes; ++j) {
4842 int s = i * dev_stripes + j;
4843 map->stripes[s].dev = devices_info[i].dev;
4844 map->stripes[s].physical = devices_info[i].dev_offset +
4845 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004846 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004847 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004848 map->stripe_len = BTRFS_STRIPE_LEN;
4849 map->io_align = BTRFS_STRIPE_LEN;
4850 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004851 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004852 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004853
David Woodhouse53b381b2013-01-29 18:40:14 -05004854 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004855
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004856 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004857
David Sterba172ddd62011-04-21 00:48:27 +02004858 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004859 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004860 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004861 ret = -ENOMEM;
4862 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004863 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004864 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004865 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004866 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004867 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004868 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004869 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004870 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004871
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004872 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004873 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004874 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004875 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004876 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004877 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004878 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004879 }
Yan Zheng2b820322008-11-17 21:11:30 -05004880
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004881 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4882 refcount_inc(&em->refs);
4883 write_unlock(&em_tree->lock);
4884
Nikolay Borisov01744842017-07-27 14:22:11 +03004885 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004886 if (ret)
4887 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004888
Miao Xie7cc8e582014-09-03 21:35:38 +08004889 for (i = 0; i < map->num_stripes; i++) {
4890 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4891 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4892 }
Miao Xie43530c42014-09-03 21:35:36 +08004893
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004894 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004895
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004896 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004897 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004898
Miao Xieb2117a32011-01-05 10:07:28 +00004899 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004900 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004901
Josef Bacik6df9a952013-06-27 13:22:46 -04004902error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004903 write_lock(&em_tree->lock);
4904 remove_extent_mapping(em_tree, em);
4905 write_unlock(&em_tree->lock);
4906
4907 /* One for our allocation */
4908 free_extent_map(em);
4909 /* One for the tree reference */
4910 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004911 /* One for the pending_chunks list reference */
4912 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004913error:
Miao Xieb2117a32011-01-05 10:07:28 +00004914 kfree(devices_info);
4915 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004916}
4917
Josef Bacik6df9a952013-06-27 13:22:46 -04004918int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004919 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004920 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004921{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004922 struct btrfs_root *extent_root = fs_info->extent_root;
4923 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004924 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004925 struct btrfs_device *device;
4926 struct btrfs_chunk *chunk;
4927 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004928 struct extent_map *em;
4929 struct map_lookup *map;
4930 size_t item_size;
4931 u64 dev_offset;
4932 u64 stripe_size;
4933 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004934 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004935
Liu Bo592d92e2017-03-14 13:33:55 -07004936 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4937 if (IS_ERR(em))
4938 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004939
Jeff Mahoney95617d62015-06-03 10:55:48 -04004940 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004941 item_size = btrfs_chunk_item_size(map->num_stripes);
4942 stripe_size = em->orig_block_len;
4943
4944 chunk = kzalloc(item_size, GFP_NOFS);
4945 if (!chunk) {
4946 ret = -ENOMEM;
4947 goto out;
4948 }
4949
Filipe Manana50460e32015-11-20 10:42:47 +00004950 /*
4951 * Take the device list mutex to prevent races with the final phase of
4952 * a device replace operation that replaces the device object associated
4953 * with the map's stripes, because the device object's id can change
4954 * at any time during that final phase of the device replace operation
4955 * (dev-replace.c:btrfs_dev_replace_finishing()).
4956 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004957 mutex_lock(&fs_info->fs_devices->device_list_mutex);
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;
4961
Yan Zheng2b820322008-11-17 21:11:30 -05004962 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004963 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004964 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004965 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4966 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004967 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004968 break;
4969 }
4970 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004971 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004972 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004973 }
4974
Yan Zheng2b820322008-11-17 21:11:30 -05004975 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004976 for (i = 0; i < map->num_stripes; i++) {
4977 device = map->stripes[i].dev;
4978 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004979
4980 btrfs_set_stack_stripe_devid(stripe, device->devid);
4981 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4982 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4983 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004984 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004985 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004986
4987 btrfs_set_stack_chunk_length(chunk, chunk_size);
4988 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4989 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4990 btrfs_set_stack_chunk_type(chunk, map->type);
4991 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4992 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4993 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004994 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05004995 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4996
4997 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4998 key.type = BTRFS_CHUNK_ITEM_KEY;
4999 key.offset = chunk_offset;
5000
5001 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005002 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5003 /*
5004 * TODO: Cleanup of inserted chunk root in case of
5005 * failure.
5006 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005007 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005008 }
liubo1abe9b82011-03-24 11:18:59 +00005009
Josef Bacik6df9a952013-06-27 13:22:46 -04005010out:
Yan Zheng2b820322008-11-17 21:11:30 -05005011 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005012 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005013 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005014}
5015
5016/*
5017 * Chunk allocation falls into two parts. The first part does works
5018 * that make the new allocated chunk useable, but not do any operation
5019 * that modifies the chunk tree. The second part does the works that
5020 * require modifying the chunk tree. This division is important for the
5021 * bootstrap process of adding storage to a seed btrfs.
5022 */
5023int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005024 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005025{
5026 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005027
David Sterbaa32bf9a2018-03-16 02:21:22 +01005028 lockdep_assert_held(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005029 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005030 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005031}
5032
Chris Masond3977122009-01-05 21:25:51 -05005033static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005034 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005035{
5036 u64 chunk_offset;
5037 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005038 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005039 int ret;
5040
Josef Bacik6df9a952013-06-27 13:22:46 -04005041 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005042 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005043 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005044 if (ret)
5045 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005046
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005047 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005048 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005049 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005050 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005051}
5052
Miao Xied20983b2014-07-03 18:22:13 +08005053static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5054{
5055 int max_errors;
5056
5057 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5058 BTRFS_BLOCK_GROUP_RAID10 |
5059 BTRFS_BLOCK_GROUP_RAID5 |
5060 BTRFS_BLOCK_GROUP_DUP)) {
5061 max_errors = 1;
5062 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5063 max_errors = 2;
5064 } else {
5065 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005066 }
5067
Miao Xied20983b2014-07-03 18:22:13 +08005068 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005069}
5070
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005071int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005072{
5073 struct extent_map *em;
5074 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005075 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005076 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005077 int i;
5078
Liu Bo592d92e2017-03-14 13:33:55 -07005079 em = get_chunk_map(fs_info, chunk_offset, 1);
5080 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005081 return 1;
5082
Jeff Mahoney95617d62015-06-03 10:55:48 -04005083 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005084 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005085 if (test_bit(BTRFS_DEV_STATE_MISSING,
5086 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005087 miss_ndevs++;
5088 continue;
5089 }
Anand Jainebbede42017-12-04 12:54:52 +08005090 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5091 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005092 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005093 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005094 }
5095 }
Miao Xied20983b2014-07-03 18:22:13 +08005096
5097 /*
5098 * If the number of missing devices is larger than max errors,
5099 * we can not write the data into that chunk successfully, so
5100 * set it readonly.
5101 */
5102 if (miss_ndevs > btrfs_chunk_max_errors(map))
5103 readonly = 1;
5104end:
Yan Zheng2b820322008-11-17 21:11:30 -05005105 free_extent_map(em);
5106 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005107}
5108
5109void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5110{
David Sterbaa8067e02011-04-21 00:34:43 +02005111 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005112}
5113
5114void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5115{
5116 struct extent_map *em;
5117
Chris Masond3977122009-01-05 21:25:51 -05005118 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005119 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005120 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5121 if (em)
5122 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005123 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005124 if (!em)
5125 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005126 /* once for us */
5127 free_extent_map(em);
5128 /* once for the tree */
5129 free_extent_map(em);
5130 }
5131}
5132
Stefan Behrens5d964052012-11-05 14:59:07 +01005133int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005134{
5135 struct extent_map *em;
5136 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005137 int ret;
5138
Liu Bo592d92e2017-03-14 13:33:55 -07005139 em = get_chunk_map(fs_info, logical, len);
5140 if (IS_ERR(em))
5141 /*
5142 * We could return errors for these cases, but that could get
5143 * ugly and we'd probably do the same thing which is just not do
5144 * anything else and exit, so return 1 so the callers don't try
5145 * to use other copies.
5146 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005147 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005148
Jeff Mahoney95617d62015-06-03 10:55:48 -04005149 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005150 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5151 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005152 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5153 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005154 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5155 ret = 2;
5156 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005157 /*
5158 * There could be two corrupted data stripes, we need
5159 * to loop retry in order to rebuild the correct data.
5160 *
5161 * Fail a stripe at a time on every retry except the
5162 * stripe under reconstruction.
5163 */
5164 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005165 else
5166 ret = 1;
5167 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005168
David Sterba7e79cb82018-03-24 02:11:38 +01005169 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005170 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5171 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005172 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005173 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005174
Chris Masonf1885912008-04-09 16:28:12 -04005175 return ret;
5176}
5177
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005178unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005179 u64 logical)
5180{
5181 struct extent_map *em;
5182 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005183 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005184
Liu Bo592d92e2017-03-14 13:33:55 -07005185 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005186
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005187 if (!WARN_ON(IS_ERR(em))) {
5188 map = em->map_lookup;
5189 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5190 len = map->stripe_len * nr_data_stripes(map);
5191 free_extent_map(em);
5192 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005193 return len;
5194}
5195
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005196int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005197{
5198 struct extent_map *em;
5199 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005200 int ret = 0;
5201
Liu Bo592d92e2017-03-14 13:33:55 -07005202 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005203
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005204 if(!WARN_ON(IS_ERR(em))) {
5205 map = em->map_lookup;
5206 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5207 ret = 1;
5208 free_extent_map(em);
5209 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005210 return ret;
5211}
5212
Stefan Behrens30d98612012-11-06 14:52:18 +01005213static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005214 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005215 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005216{
5217 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005218 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005219 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005220 int tolerance;
5221 struct btrfs_device *srcdev;
5222
Anand Jain99f92a72018-03-14 16:29:12 +08005223 ASSERT((map->type &
5224 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5225
5226 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5227 num_stripes = map->sub_stripes;
5228 else
5229 num_stripes = map->num_stripes;
5230
Anand Jain8ba0ae72018-03-14 16:29:13 +08005231 preferred_mirror = first + current->pid % num_stripes;
5232
Stefan Behrens30d98612012-11-06 14:52:18 +01005233 if (dev_replace_is_ongoing &&
5234 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5235 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5236 srcdev = fs_info->dev_replace.srcdev;
5237 else
5238 srcdev = NULL;
5239
5240 /*
5241 * try to avoid the drive that is the source drive for a
5242 * dev-replace procedure, only choose it if no other non-missing
5243 * mirror is available
5244 */
5245 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005246 if (map->stripes[preferred_mirror].dev->bdev &&
5247 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5248 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005249 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005250 if (map->stripes[i].dev->bdev &&
5251 (tolerance || map->stripes[i].dev != srcdev))
5252 return i;
5253 }
Chris Masondfe25022008-05-13 13:46:40 -04005254 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005255
Chris Masondfe25022008-05-13 13:46:40 -04005256 /* we couldn't find one that doesn't fail. Just return something
5257 * and the io error handling code will clean up eventually
5258 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005259 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005260}
5261
David Woodhouse53b381b2013-01-29 18:40:14 -05005262static inline int parity_smaller(u64 a, u64 b)
5263{
5264 return a > b;
5265}
5266
5267/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005268static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005269{
5270 struct btrfs_bio_stripe s;
5271 int i;
5272 u64 l;
5273 int again = 1;
5274
5275 while (again) {
5276 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005277 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005278 if (parity_smaller(bbio->raid_map[i],
5279 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005280 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005281 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005282 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005283 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005284 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005285 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005286
David Woodhouse53b381b2013-01-29 18:40:14 -05005287 again = 1;
5288 }
5289 }
5290 }
5291}
5292
Zhao Lei6e9606d2015-01-20 15:11:34 +08005293static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5294{
5295 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005296 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005297 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005298 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005299 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005300 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005301 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005302 /*
5303 * plus the raid_map, which includes both the tgt dev
5304 * and the stripes
5305 */
5306 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005307 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005308
5309 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005310 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005311
5312 return bbio;
5313}
5314
5315void btrfs_get_bbio(struct btrfs_bio *bbio)
5316{
Elena Reshetova140475a2017-03-03 10:55:10 +02005317 WARN_ON(!refcount_read(&bbio->refs));
5318 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005319}
5320
5321void btrfs_put_bbio(struct btrfs_bio *bbio)
5322{
5323 if (!bbio)
5324 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005325 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005326 kfree(bbio);
5327}
5328
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005329/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5330/*
5331 * Please note that, discard won't be sent to target device of device
5332 * replace.
5333 */
5334static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5335 u64 logical, u64 length,
5336 struct btrfs_bio **bbio_ret)
5337{
5338 struct extent_map *em;
5339 struct map_lookup *map;
5340 struct btrfs_bio *bbio;
5341 u64 offset;
5342 u64 stripe_nr;
5343 u64 stripe_nr_end;
5344 u64 stripe_end_offset;
5345 u64 stripe_cnt;
5346 u64 stripe_len;
5347 u64 stripe_offset;
5348 u64 num_stripes;
5349 u32 stripe_index;
5350 u32 factor = 0;
5351 u32 sub_stripes = 0;
5352 u64 stripes_per_dev = 0;
5353 u32 remaining_stripes = 0;
5354 u32 last_stripe = 0;
5355 int ret = 0;
5356 int i;
5357
5358 /* discard always return a bbio */
5359 ASSERT(bbio_ret);
5360
5361 em = get_chunk_map(fs_info, logical, length);
5362 if (IS_ERR(em))
5363 return PTR_ERR(em);
5364
5365 map = em->map_lookup;
5366 /* we don't discard raid56 yet */
5367 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5368 ret = -EOPNOTSUPP;
5369 goto out;
5370 }
5371
5372 offset = logical - em->start;
5373 length = min_t(u64, em->len - offset, length);
5374
5375 stripe_len = map->stripe_len;
5376 /*
5377 * stripe_nr counts the total number of stripes we have to stride
5378 * to get to this block
5379 */
5380 stripe_nr = div64_u64(offset, stripe_len);
5381
5382 /* stripe_offset is the offset of this block in its stripe */
5383 stripe_offset = offset - stripe_nr * stripe_len;
5384
5385 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005386 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005387 stripe_cnt = stripe_nr_end - stripe_nr;
5388 stripe_end_offset = stripe_nr_end * map->stripe_len -
5389 (offset + length);
5390 /*
5391 * after this, stripe_nr is the number of stripes on this
5392 * device we have to walk to find the data, and stripe_index is
5393 * the number of our device in the stripe array
5394 */
5395 num_stripes = 1;
5396 stripe_index = 0;
5397 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5398 BTRFS_BLOCK_GROUP_RAID10)) {
5399 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5400 sub_stripes = 1;
5401 else
5402 sub_stripes = map->sub_stripes;
5403
5404 factor = map->num_stripes / sub_stripes;
5405 num_stripes = min_t(u64, map->num_stripes,
5406 sub_stripes * stripe_cnt);
5407 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5408 stripe_index *= sub_stripes;
5409 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5410 &remaining_stripes);
5411 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5412 last_stripe *= sub_stripes;
5413 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5414 BTRFS_BLOCK_GROUP_DUP)) {
5415 num_stripes = map->num_stripes;
5416 } else {
5417 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5418 &stripe_index);
5419 }
5420
5421 bbio = alloc_btrfs_bio(num_stripes, 0);
5422 if (!bbio) {
5423 ret = -ENOMEM;
5424 goto out;
5425 }
5426
5427 for (i = 0; i < num_stripes; i++) {
5428 bbio->stripes[i].physical =
5429 map->stripes[stripe_index].physical +
5430 stripe_offset + stripe_nr * map->stripe_len;
5431 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5432
5433 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5434 BTRFS_BLOCK_GROUP_RAID10)) {
5435 bbio->stripes[i].length = stripes_per_dev *
5436 map->stripe_len;
5437
5438 if (i / sub_stripes < remaining_stripes)
5439 bbio->stripes[i].length +=
5440 map->stripe_len;
5441
5442 /*
5443 * Special for the first stripe and
5444 * the last stripe:
5445 *
5446 * |-------|...|-------|
5447 * |----------|
5448 * off end_off
5449 */
5450 if (i < sub_stripes)
5451 bbio->stripes[i].length -=
5452 stripe_offset;
5453
5454 if (stripe_index >= last_stripe &&
5455 stripe_index <= (last_stripe +
5456 sub_stripes - 1))
5457 bbio->stripes[i].length -=
5458 stripe_end_offset;
5459
5460 if (i == sub_stripes - 1)
5461 stripe_offset = 0;
5462 } else {
5463 bbio->stripes[i].length = length;
5464 }
5465
5466 stripe_index++;
5467 if (stripe_index == map->num_stripes) {
5468 stripe_index = 0;
5469 stripe_nr++;
5470 }
5471 }
5472
5473 *bbio_ret = bbio;
5474 bbio->map_type = map->type;
5475 bbio->num_stripes = num_stripes;
5476out:
5477 free_extent_map(em);
5478 return ret;
5479}
5480
Liu Bo5ab56092017-03-14 13:33:57 -07005481/*
5482 * In dev-replace case, for repair case (that's the only case where the mirror
5483 * is selected explicitly when calling btrfs_map_block), blocks left of the
5484 * left cursor can also be read from the target drive.
5485 *
5486 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5487 * array of stripes.
5488 * For READ, it also needs to be supported using the same mirror number.
5489 *
5490 * If the requested block is not left of the left cursor, EIO is returned. This
5491 * can happen because btrfs_num_copies() returns one more in the dev-replace
5492 * case.
5493 */
5494static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5495 u64 logical, u64 length,
5496 u64 srcdev_devid, int *mirror_num,
5497 u64 *physical)
5498{
5499 struct btrfs_bio *bbio = NULL;
5500 int num_stripes;
5501 int index_srcdev = 0;
5502 int found = 0;
5503 u64 physical_of_found = 0;
5504 int i;
5505 int ret = 0;
5506
5507 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5508 logical, &length, &bbio, 0, 0);
5509 if (ret) {
5510 ASSERT(bbio == NULL);
5511 return ret;
5512 }
5513
5514 num_stripes = bbio->num_stripes;
5515 if (*mirror_num > num_stripes) {
5516 /*
5517 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5518 * that means that the requested area is not left of the left
5519 * cursor
5520 */
5521 btrfs_put_bbio(bbio);
5522 return -EIO;
5523 }
5524
5525 /*
5526 * process the rest of the function using the mirror_num of the source
5527 * drive. Therefore look it up first. At the end, patch the device
5528 * pointer to the one of the target drive.
5529 */
5530 for (i = 0; i < num_stripes; i++) {
5531 if (bbio->stripes[i].dev->devid != srcdev_devid)
5532 continue;
5533
5534 /*
5535 * In case of DUP, in order to keep it simple, only add the
5536 * mirror with the lowest physical address
5537 */
5538 if (found &&
5539 physical_of_found <= bbio->stripes[i].physical)
5540 continue;
5541
5542 index_srcdev = i;
5543 found = 1;
5544 physical_of_found = bbio->stripes[i].physical;
5545 }
5546
5547 btrfs_put_bbio(bbio);
5548
5549 ASSERT(found);
5550 if (!found)
5551 return -EIO;
5552
5553 *mirror_num = index_srcdev + 1;
5554 *physical = physical_of_found;
5555 return ret;
5556}
5557
Liu Bo73c0f222017-03-14 13:33:58 -07005558static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5559 struct btrfs_bio **bbio_ret,
5560 struct btrfs_dev_replace *dev_replace,
5561 int *num_stripes_ret, int *max_errors_ret)
5562{
5563 struct btrfs_bio *bbio = *bbio_ret;
5564 u64 srcdev_devid = dev_replace->srcdev->devid;
5565 int tgtdev_indexes = 0;
5566 int num_stripes = *num_stripes_ret;
5567 int max_errors = *max_errors_ret;
5568 int i;
5569
5570 if (op == BTRFS_MAP_WRITE) {
5571 int index_where_to_add;
5572
5573 /*
5574 * duplicate the write operations while the dev replace
5575 * procedure is running. Since the copying of the old disk to
5576 * the new disk takes place at run time while the filesystem is
5577 * mounted writable, the regular write operations to the old
5578 * disk have to be duplicated to go to the new disk as well.
5579 *
5580 * Note that device->missing is handled by the caller, and that
5581 * the write to the old disk is already set up in the stripes
5582 * array.
5583 */
5584 index_where_to_add = num_stripes;
5585 for (i = 0; i < num_stripes; i++) {
5586 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5587 /* write to new disk, too */
5588 struct btrfs_bio_stripe *new =
5589 bbio->stripes + index_where_to_add;
5590 struct btrfs_bio_stripe *old =
5591 bbio->stripes + i;
5592
5593 new->physical = old->physical;
5594 new->length = old->length;
5595 new->dev = dev_replace->tgtdev;
5596 bbio->tgtdev_map[i] = index_where_to_add;
5597 index_where_to_add++;
5598 max_errors++;
5599 tgtdev_indexes++;
5600 }
5601 }
5602 num_stripes = index_where_to_add;
5603 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5604 int index_srcdev = 0;
5605 int found = 0;
5606 u64 physical_of_found = 0;
5607
5608 /*
5609 * During the dev-replace procedure, the target drive can also
5610 * be used to read data in case it is needed to repair a corrupt
5611 * block elsewhere. This is possible if the requested area is
5612 * left of the left cursor. In this area, the target drive is a
5613 * full copy of the source drive.
5614 */
5615 for (i = 0; i < num_stripes; i++) {
5616 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5617 /*
5618 * In case of DUP, in order to keep it simple,
5619 * only add the mirror with the lowest physical
5620 * address
5621 */
5622 if (found &&
5623 physical_of_found <=
5624 bbio->stripes[i].physical)
5625 continue;
5626 index_srcdev = i;
5627 found = 1;
5628 physical_of_found = bbio->stripes[i].physical;
5629 }
5630 }
5631 if (found) {
5632 struct btrfs_bio_stripe *tgtdev_stripe =
5633 bbio->stripes + num_stripes;
5634
5635 tgtdev_stripe->physical = physical_of_found;
5636 tgtdev_stripe->length =
5637 bbio->stripes[index_srcdev].length;
5638 tgtdev_stripe->dev = dev_replace->tgtdev;
5639 bbio->tgtdev_map[index_srcdev] = num_stripes;
5640
5641 tgtdev_indexes++;
5642 num_stripes++;
5643 }
5644 }
5645
5646 *num_stripes_ret = num_stripes;
5647 *max_errors_ret = max_errors;
5648 bbio->num_tgtdevs = tgtdev_indexes;
5649 *bbio_ret = bbio;
5650}
5651
Liu Bo2b19a1f2017-03-14 13:34:00 -07005652static bool need_full_stripe(enum btrfs_map_op op)
5653{
5654 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5655}
5656
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005657static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5658 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005659 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005660 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005661 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005662{
5663 struct extent_map *em;
5664 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005665 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005666 u64 stripe_offset;
5667 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005668 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005669 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005670 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005671 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005672 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005673 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005674 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005675 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005676 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5677 int dev_replace_is_ongoing = 0;
5678 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005679 int patch_the_first_stripe_for_dev_replace = 0;
5680 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005681 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005682
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005683 if (op == BTRFS_MAP_DISCARD)
5684 return __btrfs_map_block_for_discard(fs_info, logical,
5685 *length, bbio_ret);
5686
Liu Bo592d92e2017-03-14 13:33:55 -07005687 em = get_chunk_map(fs_info, logical, *length);
5688 if (IS_ERR(em))
5689 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005690
Jeff Mahoney95617d62015-06-03 10:55:48 -04005691 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005692 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005693
David Woodhouse53b381b2013-01-29 18:40:14 -05005694 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005695 stripe_nr = offset;
5696 /*
5697 * stripe_nr counts the total number of stripes we have to stride
5698 * to get to this block
5699 */
David Sterba47c57132015-02-20 18:43:47 +01005700 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005701
David Woodhouse53b381b2013-01-29 18:40:14 -05005702 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005703 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005704 btrfs_crit(fs_info,
5705 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005706 stripe_offset, offset, em->start, logical,
5707 stripe_len);
5708 free_extent_map(em);
5709 return -EINVAL;
5710 }
Chris Mason593060d2008-03-25 16:50:33 -04005711
5712 /* stripe_offset is the offset of this block in its stripe*/
5713 stripe_offset = offset - stripe_offset;
5714
David Woodhouse53b381b2013-01-29 18:40:14 -05005715 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005716 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005717 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5718 raid56_full_stripe_start = offset;
5719
5720 /* allow a write of a full stripe, but make sure we don't
5721 * allow straddling of stripes
5722 */
David Sterba47c57132015-02-20 18:43:47 +01005723 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5724 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005725 raid56_full_stripe_start *= full_stripe_len;
5726 }
5727
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005728 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005729 u64 max_len;
5730 /* For writes to RAID[56], allow a full stripeset across all disks.
5731 For other RAID types and for RAID[56] reads, just allow a single
5732 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005733 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005734 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005735 max_len = stripe_len * nr_data_stripes(map) -
5736 (offset - raid56_full_stripe_start);
5737 } else {
5738 /* we limit the length of each bio to what fits in a stripe */
5739 max_len = stripe_len - stripe_offset;
5740 }
5741 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005742 } else {
5743 *length = em->len - offset;
5744 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005745
David Woodhouse53b381b2013-01-29 18:40:14 -05005746 /* This is for when we're called from btrfs_merge_bio_hook() and all
5747 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005748 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005749 goto out;
5750
David Sterba7e79cb82018-03-24 02:11:38 +01005751 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005752 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5753 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01005754 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005755 else
5756 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005757
Stefan Behrensad6d6202012-11-06 15:06:47 +01005758 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005759 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005760 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5761 dev_replace->srcdev->devid,
5762 &mirror_num,
5763 &physical_to_patch_in_first_stripe);
5764 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005765 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005766 else
5767 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005768 } else if (mirror_num > map->num_stripes) {
5769 mirror_num = 0;
5770 }
5771
Chris Masonf2d8d742008-04-21 10:03:05 -04005772 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005773 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005774 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005775 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5776 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005777 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005778 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005779 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005780 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005781 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005782 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005783 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005784 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005785 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005786 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005787 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005788 }
Chris Mason2fff7342008-04-29 14:12:09 -04005789
Chris Mason611f0e02008-04-03 16:29:03 -04005790 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005791 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005792 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005793 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005794 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005795 } else {
5796 mirror_num = 1;
5797 }
Chris Mason2fff7342008-04-29 14:12:09 -04005798
Chris Mason321aecc2008-04-16 10:49:51 -04005799 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005800 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005801
David Sterba47c57132015-02-20 18:43:47 +01005802 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005803 stripe_index *= map->sub_stripes;
5804
Anand Jainde483732017-10-12 16:43:00 +08005805 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005806 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005807 else if (mirror_num)
5808 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005809 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005810 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005811 stripe_index = find_live_mirror(fs_info, map,
5812 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01005813 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005814 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005815 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005816
Zhao Leiffe2d202015-01-20 15:11:44 +08005817 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005818 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005819 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005820 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005821 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005822
5823 /* RAID[56] write or recovery. Return all stripes */
5824 num_stripes = map->num_stripes;
5825 max_errors = nr_parity_stripes(map);
5826
David Woodhouse53b381b2013-01-29 18:40:14 -05005827 *length = map->stripe_len;
5828 stripe_index = 0;
5829 stripe_offset = 0;
5830 } else {
5831 /*
5832 * Mirror #0 or #1 means the original data block.
5833 * Mirror #2 is RAID5 parity block.
5834 * Mirror #3 is RAID6 Q block.
5835 */
David Sterba47c57132015-02-20 18:43:47 +01005836 stripe_nr = div_u64_rem(stripe_nr,
5837 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005838 if (mirror_num > 1)
5839 stripe_index = nr_data_stripes(map) +
5840 mirror_num - 2;
5841
5842 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005843 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5844 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005845 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005846 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005847 }
Chris Mason8790d502008-04-03 16:29:03 -04005848 } else {
5849 /*
David Sterba47c57132015-02-20 18:43:47 +01005850 * after this, stripe_nr is the number of stripes on this
5851 * device we have to walk to find the data, and stripe_index is
5852 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005853 */
David Sterba47c57132015-02-20 18:43:47 +01005854 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5855 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005856 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005857 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005858 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005859 btrfs_crit(fs_info,
5860 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005861 stripe_index, map->num_stripes);
5862 ret = -EINVAL;
5863 goto out;
5864 }
Chris Mason593060d2008-03-25 16:50:33 -04005865
Stefan Behrens472262f2012-11-06 14:43:46 +01005866 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005867 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005868 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005869 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005870 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005871 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005872 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005873 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005874
Zhao Lei6e9606d2015-01-20 15:11:34 +08005875 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005876 if (!bbio) {
5877 ret = -ENOMEM;
5878 goto out;
5879 }
Liu Bo6fad8232017-03-14 13:33:59 -07005880 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005881 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005882
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005883 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005884 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5885 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005886 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005887 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005888
5889 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5890 sizeof(struct btrfs_bio_stripe) *
5891 num_alloc_stripes +
5892 sizeof(int) * tgtdev_indexes);
5893
5894 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005895 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005896
5897 /* Fill in the logical address of each stripe */
5898 tmp = stripe_nr * nr_data_stripes(map);
5899 for (i = 0; i < nr_data_stripes(map); i++)
5900 bbio->raid_map[(i+rot) % num_stripes] =
5901 em->start + (tmp + i) * map->stripe_len;
5902
5903 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5904 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5905 bbio->raid_map[(i+rot+1) % num_stripes] =
5906 RAID6_Q_STRIPE;
5907 }
5908
Li Zefanec9ef7a2011-12-01 14:06:42 +08005909
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005910 for (i = 0; i < num_stripes; i++) {
5911 bbio->stripes[i].physical =
5912 map->stripes[stripe_index].physical +
5913 stripe_offset +
5914 stripe_nr * map->stripe_len;
5915 bbio->stripes[i].dev =
5916 map->stripes[stripe_index].dev;
5917 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005918 }
Li Zefande11cc12011-12-01 12:55:47 +08005919
Liu Bo2b19a1f2017-03-14 13:34:00 -07005920 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005921 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005922
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005923 if (bbio->raid_map)
5924 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005925
Liu Bo73c0f222017-03-14 13:33:58 -07005926 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005927 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005928 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5929 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005930 }
5931
Li Zefande11cc12011-12-01 12:55:47 +08005932 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005933 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005934 bbio->num_stripes = num_stripes;
5935 bbio->max_errors = max_errors;
5936 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005937
5938 /*
5939 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5940 * mirror_num == num_stripes + 1 && dev_replace target drive is
5941 * available as a mirror
5942 */
5943 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5944 WARN_ON(num_stripes > 1);
5945 bbio->stripes[0].dev = dev_replace->tgtdev;
5946 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5947 bbio->mirror_num = map->num_stripes + 1;
5948 }
Chris Masoncea9e442008-04-09 16:28:12 -04005949out:
Liu Bo73beece2015-07-17 16:49:19 +08005950 if (dev_replace_is_ongoing) {
5951 btrfs_dev_replace_clear_lock_blocking(dev_replace);
David Sterba7e79cb82018-03-24 02:11:38 +01005952 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005953 }
Chris Mason0b86a832008-03-24 15:01:56 -04005954 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005955 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005956}
5957
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005958int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005959 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005960 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005961{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005962 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005963 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005964}
5965
Miao Xieaf8e2d12014-10-23 14:42:50 +08005966/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005967int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005968 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005969 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005970{
David Sterba825ad4c2017-03-28 14:45:22 +02005971 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005972}
5973
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005974int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05005975 u64 chunk_start, u64 physical, u64 devid,
5976 u64 **logical, int *naddrs, int *stripe_len)
5977{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005978 struct extent_map *em;
5979 struct map_lookup *map;
5980 u64 *buf;
5981 u64 bytenr;
5982 u64 length;
5983 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005984 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005985 int i, j, nr = 0;
5986
Liu Bo592d92e2017-03-14 13:33:55 -07005987 em = get_chunk_map(fs_info, chunk_start, 1);
5988 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005989 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005990
Jeff Mahoney95617d62015-06-03 10:55:48 -04005991 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005992 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005993 rmap_len = map->stripe_len;
5994
Yan Zhenga512bbf2008-12-08 16:46:26 -05005995 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005996 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005997 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005998 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005999 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006000 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006001 rmap_len = map->stripe_len * nr_data_stripes(map);
6002 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006003
David Sterba31e818f2015-02-20 18:00:26 +01006004 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006005 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006006
6007 for (i = 0; i < map->num_stripes; i++) {
6008 if (devid && map->stripes[i].dev->devid != devid)
6009 continue;
6010 if (map->stripes[i].physical > physical ||
6011 map->stripes[i].physical + length <= physical)
6012 continue;
6013
6014 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006015 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006016
6017 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6018 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006019 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006020 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6021 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006022 } /* else if RAID[56], multiply by nr_data_stripes().
6023 * Alternatively, just use rmap_len below instead of
6024 * map->stripe_len */
6025
6026 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006027 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006028 for (j = 0; j < nr; j++) {
6029 if (buf[j] == bytenr)
6030 break;
6031 }
Chris Mason934d3752008-12-08 16:43:10 -05006032 if (j == nr) {
6033 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006034 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006035 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006036 }
6037
Yan Zhenga512bbf2008-12-08 16:46:26 -05006038 *logical = buf;
6039 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006040 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006041
6042 free_extent_map(em);
6043 return 0;
6044}
6045
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006046static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006047{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006048 bio->bi_private = bbio->private;
6049 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006050 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006051
Zhao Lei6e9606d2015-01-20 15:11:34 +08006052 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006053}
6054
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006055static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006056{
Chris Mason9be33952013-05-17 18:30:14 -04006057 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006058 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006059
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006060 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006061 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006062 if (bio->bi_status == BLK_STS_IOERR ||
6063 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006064 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006065 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006066 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006067
6068 BUG_ON(stripe_index >= bbio->num_stripes);
6069 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006070 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006071 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006072 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006073 BTRFS_DEV_STAT_WRITE_ERRS);
6074 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006075 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006076 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006077 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006078 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006079 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006080 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006081 }
6082 }
Chris Mason8790d502008-04-03 16:29:03 -04006083
Jan Schmidta1d3c472011-08-04 17:15:33 +02006084 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006085 is_orig_bio = 1;
6086
Miao Xiec404e0d2014-01-30 16:46:55 +08006087 btrfs_bio_counter_dec(bbio->fs_info);
6088
Jan Schmidta1d3c472011-08-04 17:15:33 +02006089 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006090 if (!is_orig_bio) {
6091 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006092 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006093 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006094
Chris Mason9be33952013-05-17 18:30:14 -04006095 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006096 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006097 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006098 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006099 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006100 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006101 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006102 /*
6103 * this bio is actually up to date, we didn't
6104 * go over the max number of errors
6105 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006106 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006107 }
Miao Xiec55f1392014-06-19 10:42:54 +08006108
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006109 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006110 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006111 bio_put(bio);
6112 }
Chris Mason8790d502008-04-03 16:29:03 -04006113}
6114
Chris Mason8b712842008-06-11 16:50:36 -04006115/*
6116 * see run_scheduled_bios for a description of why bios are collected for
6117 * async submit.
6118 *
6119 * This will add one bio to the pending list for a device and make sure
6120 * the work struct is scheduled.
6121 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006122static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006123 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006125 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006126 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006127 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006128
Anand Jaine6e674b2017-12-04 12:54:54 +08006129 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
6130 !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006131 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006132 return;
6133 }
6134
Chris Mason8b712842008-06-11 16:50:36 -04006135 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006136 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006137 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006138 return;
Chris Mason8b712842008-06-11 16:50:36 -04006139 }
6140
Chris Mason492bb6de2008-07-31 16:29:02 -04006141 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006142 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006143
6144 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006145 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006146 pending_bios = &device->pending_sync_bios;
6147 else
6148 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006149
Chris Masonffbd5172009-04-20 15:50:09 -04006150 if (pending_bios->tail)
6151 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006152
Chris Masonffbd5172009-04-20 15:50:09 -04006153 pending_bios->tail = bio;
6154 if (!pending_bios->head)
6155 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006156 if (device->running_pending)
6157 should_queue = 0;
6158
6159 spin_unlock(&device->io_lock);
6160
6161 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006163}
6164
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006165static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6166 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006167{
6168 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006169 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006170
6171 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006172 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006173 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006174 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006175#ifdef DEBUG
6176 {
6177 struct rcu_string *name;
6178
6179 rcu_read_lock();
6180 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006181 btrfs_debug(fs_info,
6182 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6183 bio_op(bio), bio->bi_opf,
6184 (u64)bio->bi_iter.bi_sector,
6185 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6186 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006187 rcu_read_unlock();
6188 }
6189#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006190 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006191
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006192 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006193
Josef Bacikde1ee922012-10-19 16:50:56 -04006194 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006195 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006196 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006197 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006198}
6199
Josef Bacikde1ee922012-10-19 16:50:56 -04006200static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6201{
6202 atomic_inc(&bbio->error);
6203 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006204 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006205 WARN_ON(bio != bbio->orig_bio);
6206
Chris Mason9be33952013-05-17 18:30:14 -04006207 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006208 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006209 if (atomic_read(&bbio->error) > bbio->max_errors)
6210 bio->bi_status = BLK_STS_IOERR;
6211 else
6212 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006213 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006214 }
6215}
6216
Omar Sandoval58efbc92017-08-22 23:45:59 -07006217blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6218 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006219{
Chris Mason0b86a832008-03-24 15:01:56 -04006220 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006221 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006222 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006223 u64 length = 0;
6224 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006225 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006226 int dev_nr;
6227 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006228 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006229
Kent Overstreet4f024f32013-10-11 15:44:27 -07006230 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006231 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006232
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006233 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006234 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006235 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006236 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006237 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006238 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006239 }
Chris Masoncea9e442008-04-09 16:28:12 -04006240
Jan Schmidta1d3c472011-08-04 17:15:33 +02006241 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006242 bbio->orig_bio = first_bio;
6243 bbio->private = first_bio->bi_private;
6244 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006245 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006246 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6247
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006248 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006249 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006250 /* In this case, map_length has been set to the length of
6251 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006252 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006253 ret = raid56_parity_write(fs_info, bio, bbio,
6254 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006255 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006256 ret = raid56_parity_recover(fs_info, bio, bbio,
6257 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006258 }
Miao Xie42452152014-11-25 16:39:28 +08006259
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006260 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006261 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006262 }
6263
Chris Masoncea9e442008-04-09 16:28:12 -04006264 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006265 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006266 "mapping failed logical %llu bio len %llu len %llu",
6267 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006268 BUG();
6269 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006270
Zhao Lei08da7572015-02-12 15:42:16 +08006271 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006272 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006273 if (!dev || !dev->bdev ||
Anand Jainebbede42017-12-04 12:54:52 +08006274 (bio_op(first_bio) == REQ_OP_WRITE &&
6275 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006276 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006277 continue;
6278 }
6279
David Sterba3aa8e072017-06-02 17:38:30 +02006280 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006281 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006282 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006283 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006284
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006285 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6286 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006287 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006288 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006289 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006290}
6291
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006292struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006293 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006294{
Yan Zheng2b820322008-11-17 21:11:30 -05006295 struct btrfs_device *device;
6296 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006297
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006298 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006299 while (cur_devices) {
6300 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006301 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006302 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006303 if (device)
6304 return device;
6305 }
6306 cur_devices = cur_devices->seed;
6307 }
6308 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006309}
6310
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006311static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006312 u64 devid, u8 *dev_uuid)
6313{
6314 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006315
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006316 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6317 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006318 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006319
6320 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006321 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006322 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006323
Anand Jaine6e674b2017-12-04 12:54:54 +08006324 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006325 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006326
Chris Masondfe25022008-05-13 13:46:40 -04006327 return device;
6328}
6329
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006330/**
6331 * btrfs_alloc_device - allocate struct btrfs_device
6332 * @fs_info: used only for generating a new devid, can be NULL if
6333 * devid is provided (i.e. @devid != NULL).
6334 * @devid: a pointer to devid for this device. If NULL a new devid
6335 * is generated.
6336 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6337 * is generated.
6338 *
6339 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006340 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006341 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006342 */
6343struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6344 const u64 *devid,
6345 const u8 *uuid)
6346{
6347 struct btrfs_device *dev;
6348 u64 tmp;
6349
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306350 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006351 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006352
6353 dev = __alloc_device();
6354 if (IS_ERR(dev))
6355 return dev;
6356
6357 if (devid)
6358 tmp = *devid;
6359 else {
6360 int ret;
6361
6362 ret = find_next_devid(fs_info, &tmp);
6363 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006364 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006365 return ERR_PTR(ret);
6366 }
6367 }
6368 dev->devid = tmp;
6369
6370 if (uuid)
6371 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6372 else
6373 generate_random_uuid(dev->uuid);
6374
Liu Bo9e0af232014-08-15 23:36:53 +08006375 btrfs_init_work(&dev->work, btrfs_submit_helper,
6376 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006377
6378 return dev;
6379}
6380
Liu Boe06cd3d2016-06-03 12:05:15 -07006381/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006382static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006383 struct extent_buffer *leaf,
6384 struct btrfs_chunk *chunk, u64 logical)
6385{
6386 u64 length;
6387 u64 stripe_len;
6388 u16 num_stripes;
6389 u16 sub_stripes;
6390 u64 type;
6391
6392 length = btrfs_chunk_length(leaf, chunk);
6393 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6394 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6395 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6396 type = btrfs_chunk_type(leaf, chunk);
6397
6398 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006399 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006400 num_stripes);
6401 return -EIO;
6402 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006403 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6404 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006405 return -EIO;
6406 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006407 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6408 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006409 btrfs_chunk_sector_size(leaf, chunk));
6410 return -EIO;
6411 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006412 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6413 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006414 return -EIO;
6415 }
6416 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006417 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006418 stripe_len);
6419 return -EIO;
6420 }
6421 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6422 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006423 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006424 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6425 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6426 btrfs_chunk_type(leaf, chunk));
6427 return -EIO;
6428 }
6429 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6430 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6431 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6432 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6433 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6434 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6435 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006436 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006437 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6438 num_stripes, sub_stripes,
6439 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6440 return -EIO;
6441 }
6442
6443 return 0;
6444}
6445
Anand Jain5a2b8e62017-10-09 11:07:45 +08006446static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006447 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006448{
Anand Jain2b902df2017-10-09 11:07:46 +08006449 if (error)
6450 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6451 devid, uuid);
6452 else
6453 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6454 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006455}
6456
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006457static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006458 struct extent_buffer *leaf,
6459 struct btrfs_chunk *chunk)
6460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006461 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006462 struct map_lookup *map;
6463 struct extent_map *em;
6464 u64 logical;
6465 u64 length;
6466 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006467 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006468 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006469 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006470 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006471
Chris Masone17cade2008-04-15 15:41:47 -04006472 logical = key->offset;
6473 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006474 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006475
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006476 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006477 if (ret)
6478 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006479
Chris Mason890871b2009-09-02 16:24:52 -04006480 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006481 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006482 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006483
6484 /* already mapped? */
6485 if (em && em->start <= logical && em->start + em->len > logical) {
6486 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006487 return 0;
6488 } else if (em) {
6489 free_extent_map(em);
6490 }
Chris Mason0b86a832008-03-24 15:01:56 -04006491
David Sterba172ddd62011-04-21 00:48:27 +02006492 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006493 if (!em)
6494 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006495 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006496 if (!map) {
6497 free_extent_map(em);
6498 return -ENOMEM;
6499 }
6500
Wang Shilong298a8f92014-06-19 10:42:52 +08006501 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006502 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006503 em->start = logical;
6504 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006505 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006506 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006507 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006508
Chris Mason593060d2008-03-25 16:50:33 -04006509 map->num_stripes = num_stripes;
6510 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6511 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006512 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6513 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006514 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006515 for (i = 0; i < num_stripes; i++) {
6516 map->stripes[i].physical =
6517 btrfs_stripe_offset_nr(leaf, chunk, i);
6518 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006519 read_extent_buffer(leaf, uuid, (unsigned long)
6520 btrfs_stripe_dev_uuid_nr(chunk, i),
6521 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006522 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006523 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006524 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006525 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006526 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006527 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006528 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006529 }
Chris Masondfe25022008-05-13 13:46:40 -04006530 if (!map->stripes[i].dev) {
6531 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006532 add_missing_dev(fs_info->fs_devices, devid,
6533 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006534 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006535 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006536 btrfs_err(fs_info,
6537 "failed to init missing dev %llu: %ld",
6538 devid, PTR_ERR(map->stripes[i].dev));
6539 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006540 }
Anand Jain2b902df2017-10-09 11:07:46 +08006541 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006542 }
Anand Jaine12c9622017-12-04 12:54:53 +08006543 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6544 &(map->stripes[i].dev->dev_state));
6545
Chris Mason0b86a832008-03-24 15:01:56 -04006546 }
6547
Chris Mason890871b2009-09-02 16:24:52 -04006548 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006549 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006550 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006551 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006552 free_extent_map(em);
6553
6554 return 0;
6555}
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
Li Zefanb367e472011-12-07 11:38:24 +08006932 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006933 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006934
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006935 /*
6936 * Read all device items, and then all the chunk items. All
6937 * device items are found before any chunk item (their object id
6938 * is smaller than the lowest possible object id for a chunk
6939 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006940 */
6941 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6942 key.offset = 0;
6943 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006944 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006945 if (ret < 0)
6946 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006947 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006948 leaf = path->nodes[0];
6949 slot = path->slots[0];
6950 if (slot >= btrfs_header_nritems(leaf)) {
6951 ret = btrfs_next_leaf(root, path);
6952 if (ret == 0)
6953 continue;
6954 if (ret < 0)
6955 goto error;
6956 break;
6957 }
6958 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006959 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6960 struct btrfs_dev_item *dev_item;
6961 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006962 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006963 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006964 if (ret)
6965 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006966 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006967 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6968 struct btrfs_chunk *chunk;
6969 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006970 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006971 if (ret)
6972 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006973 }
6974 path->slots[0]++;
6975 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006976
6977 /*
6978 * After loading chunk tree, we've got all device information,
6979 * do another round of validation checks.
6980 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006981 if (total_dev != fs_info->fs_devices->total_devices) {
6982 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006983 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006984 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006985 total_dev);
6986 ret = -EINVAL;
6987 goto error;
6988 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006989 if (btrfs_super_total_bytes(fs_info->super_copy) <
6990 fs_info->fs_devices->total_rw_bytes) {
6991 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006992 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006993 btrfs_super_total_bytes(fs_info->super_copy),
6994 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006995 ret = -EINVAL;
6996 goto error;
6997 }
Chris Mason0b86a832008-03-24 15:01:56 -04006998 ret = 0;
6999error:
David Sterba34441362016-10-04 19:34:27 +02007000 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007001 mutex_unlock(&uuid_mutex);
7002
Yan Zheng2b820322008-11-17 21:11:30 -05007003 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007004 return ret;
7005}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007006
Miao Xiecb517ea2013-05-15 07:48:19 +00007007void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7008{
7009 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7010 struct btrfs_device *device;
7011
Liu Bo29cc83f2014-05-11 23:14:59 +08007012 while (fs_devices) {
7013 mutex_lock(&fs_devices->device_list_mutex);
7014 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007015 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007016 mutex_unlock(&fs_devices->device_list_mutex);
7017
7018 fs_devices = fs_devices->seed;
7019 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007020}
7021
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007022static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7023{
7024 int i;
7025
7026 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7027 btrfs_dev_stat_reset(dev, i);
7028}
7029
7030int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7031{
7032 struct btrfs_key key;
7033 struct btrfs_key found_key;
7034 struct btrfs_root *dev_root = fs_info->dev_root;
7035 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7036 struct extent_buffer *eb;
7037 int slot;
7038 int ret = 0;
7039 struct btrfs_device *device;
7040 struct btrfs_path *path = NULL;
7041 int i;
7042
7043 path = btrfs_alloc_path();
7044 if (!path) {
7045 ret = -ENOMEM;
7046 goto out;
7047 }
7048
7049 mutex_lock(&fs_devices->device_list_mutex);
7050 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7051 int item_size;
7052 struct btrfs_dev_stats_item *ptr;
7053
David Sterba242e2952016-01-25 17:51:31 +01007054 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7055 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007056 key.offset = device->devid;
7057 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7058 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007059 __btrfs_reset_dev_stats(device);
7060 device->dev_stats_valid = 1;
7061 btrfs_release_path(path);
7062 continue;
7063 }
7064 slot = path->slots[0];
7065 eb = path->nodes[0];
7066 btrfs_item_key_to_cpu(eb, &found_key, slot);
7067 item_size = btrfs_item_size_nr(eb, slot);
7068
7069 ptr = btrfs_item_ptr(eb, slot,
7070 struct btrfs_dev_stats_item);
7071
7072 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7073 if (item_size >= (1 + i) * sizeof(__le64))
7074 btrfs_dev_stat_set(device, i,
7075 btrfs_dev_stats_value(eb, ptr, i));
7076 else
7077 btrfs_dev_stat_reset(device, i);
7078 }
7079
7080 device->dev_stats_valid = 1;
7081 btrfs_dev_stat_print_on_load(device);
7082 btrfs_release_path(path);
7083 }
7084 mutex_unlock(&fs_devices->device_list_mutex);
7085
7086out:
7087 btrfs_free_path(path);
7088 return ret < 0 ? ret : 0;
7089}
7090
7091static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007092 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007093 struct btrfs_device *device)
7094{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007095 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007096 struct btrfs_path *path;
7097 struct btrfs_key key;
7098 struct extent_buffer *eb;
7099 struct btrfs_dev_stats_item *ptr;
7100 int ret;
7101 int i;
7102
David Sterba242e2952016-01-25 17:51:31 +01007103 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7104 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007105 key.offset = device->devid;
7106
7107 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007108 if (!path)
7109 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007110 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7111 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007112 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007113 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007114 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007115 goto out;
7116 }
7117
7118 if (ret == 0 &&
7119 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7120 /* need to delete old one and insert a new one */
7121 ret = btrfs_del_item(trans, dev_root, path);
7122 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007123 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007124 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007125 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007126 goto out;
7127 }
7128 ret = 1;
7129 }
7130
7131 if (ret == 1) {
7132 /* need to insert a new item */
7133 btrfs_release_path(path);
7134 ret = btrfs_insert_empty_item(trans, dev_root, path,
7135 &key, sizeof(*ptr));
7136 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007137 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007138 "insert dev_stats item for device %s failed %d",
7139 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007140 goto out;
7141 }
7142 }
7143
7144 eb = path->nodes[0];
7145 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7146 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7147 btrfs_set_dev_stats_value(eb, ptr, i,
7148 btrfs_dev_stat_read(device, i));
7149 btrfs_mark_buffer_dirty(eb);
7150
7151out:
7152 btrfs_free_path(path);
7153 return ret;
7154}
7155
7156/*
7157 * called from commit_transaction. Writes all changed device stats to disk.
7158 */
7159int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7160 struct btrfs_fs_info *fs_info)
7161{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007162 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7163 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007164 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007165 int ret = 0;
7166
7167 mutex_lock(&fs_devices->device_list_mutex);
7168 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007169 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7170 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007171 continue;
7172
Nikolay Borisov9deae962017-10-24 13:47:37 +03007173
7174 /*
7175 * There is a LOAD-LOAD control dependency between the value of
7176 * dev_stats_ccnt and updating the on-disk values which requires
7177 * reading the in-memory counters. Such control dependencies
7178 * require explicit read memory barriers.
7179 *
7180 * This memory barriers pairs with smp_mb__before_atomic in
7181 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7182 * barrier implied by atomic_xchg in
7183 * btrfs_dev_stats_read_and_reset
7184 */
7185 smp_rmb();
7186
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007187 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007188 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007189 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007190 }
7191 mutex_unlock(&fs_devices->device_list_mutex);
7192
7193 return ret;
7194}
7195
Stefan Behrens442a4f62012-05-25 16:06:08 +02007196void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7197{
7198 btrfs_dev_stat_inc(dev, index);
7199 btrfs_dev_stat_print_on_error(dev);
7200}
7201
Eric Sandeen48a3b632013-04-25 20:41:01 +00007202static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007203{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007204 if (!dev->dev_stats_valid)
7205 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007206 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007207 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007208 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007209 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7210 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7211 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007212 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7213 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007214}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007215
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007216static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7217{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007218 int i;
7219
7220 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7221 if (btrfs_dev_stat_read(dev, i) != 0)
7222 break;
7223 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7224 return; /* all values == 0, suppress message */
7225
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007226 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007227 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007228 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007229 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7230 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7231 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7232 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7233 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7234}
7235
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007236int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007237 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007238{
7239 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007240 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007241 int i;
7242
7243 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007244 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007245 mutex_unlock(&fs_devices->device_list_mutex);
7246
7247 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007248 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007249 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007250 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007251 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007252 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007253 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007254 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7255 if (stats->nr_items > i)
7256 stats->values[i] =
7257 btrfs_dev_stat_read_and_reset(dev, i);
7258 else
7259 btrfs_dev_stat_reset(dev, i);
7260 }
7261 } else {
7262 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7263 if (stats->nr_items > i)
7264 stats->values[i] = btrfs_dev_stat_read(dev, i);
7265 }
7266 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7267 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7268 return 0;
7269}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007270
David Sterbada353f62017-02-14 17:55:53 +01007271void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007272{
7273 struct buffer_head *bh;
7274 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007275 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007276
Anand Jain12b1c262015-08-14 18:32:59 +08007277 if (!bdev)
7278 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007279
Anand Jain12b1c262015-08-14 18:32:59 +08007280 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7281 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007282
Anand Jain12b1c262015-08-14 18:32:59 +08007283 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7284 continue;
7285
7286 disk_super = (struct btrfs_super_block *)bh->b_data;
7287
7288 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7289 set_buffer_dirty(bh);
7290 sync_dirty_buffer(bh);
7291 brelse(bh);
7292 }
7293
7294 /* Notify udev that device has changed */
7295 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7296
7297 /* Update ctime/mtime for device path for libblkid */
7298 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007299}
Miao Xie935e5cc2014-09-03 21:35:33 +08007300
7301/*
7302 * Update the size of all devices, which is used for writing out the
7303 * super blocks.
7304 */
7305void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7306{
7307 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7308 struct btrfs_device *curr, *next;
7309
7310 if (list_empty(&fs_devices->resized_devices))
7311 return;
7312
7313 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007314 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007315 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7316 resized_list) {
7317 list_del_init(&curr->resized_list);
7318 curr->commit_total_bytes = curr->disk_total_bytes;
7319 }
David Sterba34441362016-10-04 19:34:27 +02007320 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007321 mutex_unlock(&fs_devices->device_list_mutex);
7322}
Miao Xiece7213c2014-09-03 21:35:34 +08007323
7324/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007325void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007326{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007327 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007328 struct extent_map *em;
7329 struct map_lookup *map;
7330 struct btrfs_device *dev;
7331 int i;
7332
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007333 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007334 return;
7335
7336 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007337 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007338 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007339 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007340
7341 for (i = 0; i < map->num_stripes; i++) {
7342 dev = map->stripes[i].dev;
7343 dev->commit_bytes_used = dev->bytes_used;
7344 }
7345 }
David Sterba34441362016-10-04 19:34:27 +02007346 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007347}
Anand Jain5a13f432015-03-10 06:38:31 +08007348
7349void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7350{
7351 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7352 while (fs_devices) {
7353 fs_devices->fs_info = fs_info;
7354 fs_devices = fs_devices->seed;
7355 }
7356}
7357
7358void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7359{
7360 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7361 while (fs_devices) {
7362 fs_devices->fs_info = NULL;
7363 fs_devices = fs_devices->seed;
7364 }
7365}