blob: 1e72357bdfa8b44d41e523eddc8111e8ca9421a5 [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Masonb765ead2009-04-03 10:27:10 -040023#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000024#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020025#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020026#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050027#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020028#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070029#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080030#include <linux/list_sort.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050031#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "ctree.h"
33#include "extent_map.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "print-tree.h"
37#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050038#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040039#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010040#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040041#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060042#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010043#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080044#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045
Zhao Leiaf902042015-09-15 21:08:06 +080046const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
47 [BTRFS_RAID_RAID10] = {
48 .sub_stripes = 2,
49 .dev_stripes = 1,
50 .devs_max = 0, /* 0 == as many as possible */
51 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080052 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080053 .devs_increment = 2,
54 .ncopies = 2,
55 },
56 [BTRFS_RAID_RAID1] = {
57 .sub_stripes = 1,
58 .dev_stripes = 1,
59 .devs_max = 2,
60 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080061 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080062 .devs_increment = 2,
63 .ncopies = 2,
64 },
65 [BTRFS_RAID_DUP] = {
66 .sub_stripes = 1,
67 .dev_stripes = 2,
68 .devs_max = 1,
69 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080070 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080071 .devs_increment = 1,
72 .ncopies = 2,
73 },
74 [BTRFS_RAID_RAID0] = {
75 .sub_stripes = 1,
76 .dev_stripes = 1,
77 .devs_max = 0,
78 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080079 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080080 .devs_increment = 1,
81 .ncopies = 1,
82 },
83 [BTRFS_RAID_SINGLE] = {
84 .sub_stripes = 1,
85 .dev_stripes = 1,
86 .devs_max = 1,
87 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080088 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080089 .devs_increment = 1,
90 .ncopies = 1,
91 },
92 [BTRFS_RAID_RAID5] = {
93 .sub_stripes = 1,
94 .dev_stripes = 1,
95 .devs_max = 0,
96 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080097 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080098 .devs_increment = 1,
99 .ncopies = 2,
100 },
101 [BTRFS_RAID_RAID6] = {
102 .sub_stripes = 1,
103 .dev_stripes = 1,
104 .devs_max = 0,
105 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800106 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800107 .devs_increment = 1,
108 .ncopies = 3,
109 },
110};
111
Colin Ian Kingfb75d852016-01-19 00:05:28 +0000112const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
Zhao Leiaf902042015-09-15 21:08:06 +0800113 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
114 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
115 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
116 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
117 [BTRFS_RAID_SINGLE] = 0,
118 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
119 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
120};
121
David Sterba621292b2016-02-15 16:28:03 +0100122/*
123 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
124 * condition is not met. Zero means there's no corresponding
125 * BTRFS_ERROR_DEV_*_NOT_MET value.
126 */
127const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
128 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
129 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
130 [BTRFS_RAID_DUP] = 0,
131 [BTRFS_RAID_RAID0] = 0,
132 [BTRFS_RAID_SINGLE] = 0,
133 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
134 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
135};
136
Yan Zheng2b820322008-11-17 21:11:30 -0500137static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100138 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400139static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200140static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000141static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200142static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700143static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
144 enum btrfs_map_op op,
145 u64 logical, u64 *length,
146 struct btrfs_bio **bbio_ret,
147 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500148
David Sterba9c6b1c42017-06-16 22:30:00 +0200149/*
150 * Device locking
151 * ==============
152 *
153 * There are several mutexes that protect manipulation of devices and low-level
154 * structures like chunks but not block groups, extents or files
155 *
156 * uuid_mutex (global lock)
157 * ------------------------
158 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
159 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
160 * device) or requested by the device= mount option
161 *
162 * the mutex can be very coarse and can cover long-running operations
163 *
164 * protects: updates to fs_devices counters like missing devices, rw devices,
165 * seeding, structure cloning, openning/closing devices at mount/umount time
166 *
167 * global::fs_devs - add, remove, updates to the global list
168 *
169 * does not protect: manipulation of the fs_devices::devices list!
170 *
171 * btrfs_device::name - renames (write side), read is RCU
172 *
173 * fs_devices::device_list_mutex (per-fs, with RCU)
174 * ------------------------------------------------
175 * protects updates to fs_devices::devices, ie. adding and deleting
176 *
177 * simple list traversal with read-only actions can be done with RCU protection
178 *
179 * may be used to exclude some operations from running concurrently without any
180 * modifications to the list (see write_all_supers)
181 *
182 * volume_mutex
183 * ------------
184 * coarse lock owned by a mounted filesystem; used to exclude some operations
185 * that cannot run in parallel and affect the higher-level properties of the
186 * filesystem like: device add/deleting/resize/replace, or balance
187 *
188 * balance_mutex
189 * -------------
190 * protects balance structures (status, state) and context accessed from
191 * several places (internally, ioctl)
192 *
193 * chunk_mutex
194 * -----------
195 * protects chunks, adding or removing during allocation, trim or when a new
196 * device is added/removed
197 *
198 * cleaner_mutex
199 * -------------
200 * a big lock that is held by the cleaner thread and prevents running subvolume
201 * cleaning together with relocation or delayed iputs
202 *
203 *
204 * Lock nesting
205 * ============
206 *
207 * uuid_mutex
208 * volume_mutex
209 * device_list_mutex
210 * chunk_mutex
211 * balance_mutex
212 */
213
Miao Xie67a2c452014-09-03 21:35:43 +0800214DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400215static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800216struct list_head *btrfs_get_fs_uuids(void)
217{
218 return &fs_uuids;
219}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400220
David Sterba2dfeca92017-06-14 02:48:07 +0200221/*
222 * alloc_fs_devices - allocate struct btrfs_fs_devices
223 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
224 *
225 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
226 * The returned struct is not linked onto any lists and can be destroyed with
227 * kfree() right away.
228 */
229static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300230{
231 struct btrfs_fs_devices *fs_devs;
232
David Sterba78f2c9e2016-02-11 14:25:38 +0100233 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300234 if (!fs_devs)
235 return ERR_PTR(-ENOMEM);
236
237 mutex_init(&fs_devs->device_list_mutex);
238
239 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800240 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300241 INIT_LIST_HEAD(&fs_devs->alloc_list);
242 INIT_LIST_HEAD(&fs_devs->list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300243 if (fsid)
244 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300245
246 return fs_devs;
247}
248
David Sterba48dae9c2017-10-30 18:10:25 +0100249static void free_device(struct btrfs_device *device)
250{
251 rcu_string_free(device->name);
252 bio_put(device->flush_bio);
253 kfree(device);
254}
255
Yan Zhenge4404d62008-12-12 10:03:26 -0500256static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
257{
258 struct btrfs_device *device;
259 WARN_ON(fs_devices->opened);
260 while (!list_empty(&fs_devices->devices)) {
261 device = list_entry(fs_devices->devices.next,
262 struct btrfs_device, dev_list);
263 list_del(&device->dev_list);
David Sterba55de4802017-10-30 18:55:47 +0100264 free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500265 }
266 kfree(fs_devices);
267}
268
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000269static void btrfs_kobject_uevent(struct block_device *bdev,
270 enum kobject_action action)
271{
272 int ret;
273
274 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
275 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500276 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000277 action,
278 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
279 &disk_to_dev(bdev->bd_disk)->kobj);
280}
281
David Sterbaffc5a372018-02-19 17:24:15 +0100282void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400283{
284 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400285
Yan Zheng2b820322008-11-17 21:11:30 -0500286 while (!list_empty(&fs_uuids)) {
287 fs_devices = list_entry(fs_uuids.next,
288 struct btrfs_fs_devices, list);
289 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500290 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400291 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400292}
293
David Sterba48dae9c2017-10-30 18:10:25 +0100294/*
295 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
296 * Returned struct is not linked onto any lists and must be destroyed using
297 * free_device.
298 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300299static struct btrfs_device *__alloc_device(void)
300{
301 struct btrfs_device *dev;
302
David Sterba78f2c9e2016-02-11 14:25:38 +0100303 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300304 if (!dev)
305 return ERR_PTR(-ENOMEM);
306
David Sterbae0ae9992017-06-06 17:06:06 +0200307 /*
308 * Preallocate a bio that's always going to be used for flushing device
309 * barriers and matches the device lifespan
310 */
311 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
312 if (!dev->flush_bio) {
313 kfree(dev);
314 return ERR_PTR(-ENOMEM);
315 }
David Sterbae0ae9992017-06-06 17:06:06 +0200316
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300317 INIT_LIST_HEAD(&dev->dev_list);
318 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800319 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300320
321 spin_lock_init(&dev->io_lock);
322
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300323 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800324 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100325 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700326 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800327 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300328
329 return dev;
330}
331
David Sterba35c70102017-06-15 19:51:51 +0200332/*
333 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
334 * return NULL.
335 *
336 * If devid and uuid are both specified, the match must be exact, otherwise
337 * only devid is used.
338 */
339static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
340 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400341{
David Sterba35c70102017-06-15 19:51:51 +0200342 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400343 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400344
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500345 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400346 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400347 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400348 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400349 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400350 }
351 return NULL;
352}
353
Chris Masona1b32a52008-09-05 16:09:51 -0400354static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400355{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400356 struct btrfs_fs_devices *fs_devices;
357
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500358 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400359 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
360 return fs_devices;
361 }
362 return NULL;
363}
364
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100365static int
366btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
367 int flush, struct block_device **bdev,
368 struct buffer_head **bh)
369{
370 int ret;
371
372 *bdev = blkdev_get_by_path(device_path, flags, holder);
373
374 if (IS_ERR(*bdev)) {
375 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100376 goto error;
377 }
378
379 if (flush)
380 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200381 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100382 if (ret) {
383 blkdev_put(*bdev, flags);
384 goto error;
385 }
386 invalidate_bdev(*bdev);
387 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800388 if (IS_ERR(*bh)) {
389 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100390 blkdev_put(*bdev, flags);
391 goto error;
392 }
393
394 return 0;
395
396error:
397 *bdev = NULL;
398 *bh = NULL;
399 return ret;
400}
401
Chris Masonffbd5172009-04-20 15:50:09 -0400402static void requeue_list(struct btrfs_pending_bios *pending_bios,
403 struct bio *head, struct bio *tail)
404{
405
406 struct bio *old_head;
407
408 old_head = pending_bios->head;
409 pending_bios->head = head;
410 if (pending_bios->tail)
411 tail->bi_next = old_head;
412 else
413 pending_bios->tail = tail;
414}
415
Chris Mason8b712842008-06-11 16:50:36 -0400416/*
417 * we try to collect pending bios for a device so we don't get a large
418 * number of procs sending bios down to the same device. This greatly
419 * improves the schedulers ability to collect and merge the bios.
420 *
421 * But, it also turns into a long list of bios to process and that is sure
422 * to eventually make the worker thread block. The solution here is to
423 * make some progress and then put this work struct back at the end of
424 * the list if the block device is congested. This way, multiple devices
425 * can make progress from a single worker thread.
426 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100427static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400429 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400430 struct bio *pending;
431 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400432 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400433 struct bio *tail;
434 struct bio *cur;
435 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400436 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400437 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400438 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400439 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000440 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400441 struct blk_plug plug;
442
443 /*
444 * this function runs all the bios we've collected for
445 * a particular device. We don't want to wander off to
446 * another device without first sending all of these down.
447 * So, setup a plug here and finish it off before we return
448 */
449 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400450
Jan Karaefa7c9f2017-02-02 15:56:53 +0100451 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400452
Chris Mason8b712842008-06-11 16:50:36 -0400453loop:
454 spin_lock(&device->io_lock);
455
Chris Masona6837052009-02-04 09:19:41 -0500456loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400457 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400458
Chris Mason8b712842008-06-11 16:50:36 -0400459 /* take all the bios off the list at once and process them
460 * later on (without the lock held). But, remember the
461 * tail and other pointers so the bios can be properly reinserted
462 * into the list if we hit congestion
463 */
Chris Masond84275c2009-06-09 15:39:08 -0400464 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400465 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400466 force_reg = 1;
467 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400468 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400469 force_reg = 0;
470 }
Chris Masonffbd5172009-04-20 15:50:09 -0400471
472 pending = pending_bios->head;
473 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400474 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400475
476 /*
477 * if pending was null this time around, no bios need processing
478 * at all and we can stop. Otherwise it'll loop back up again
479 * and do an additional check so no bios are missed.
480 *
481 * device->running_pending is used to synchronize with the
482 * schedule_bio code.
483 */
Chris Masonffbd5172009-04-20 15:50:09 -0400484 if (device->pending_sync_bios.head == NULL &&
485 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400486 again = 0;
487 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400488 } else {
489 again = 1;
490 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400491 }
Chris Masonffbd5172009-04-20 15:50:09 -0400492
493 pending_bios->head = NULL;
494 pending_bios->tail = NULL;
495
Chris Mason8b712842008-06-11 16:50:36 -0400496 spin_unlock(&device->io_lock);
497
Chris Masond3977122009-01-05 21:25:51 -0500498 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400499
500 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400501 /* we want to work on both lists, but do more bios on the
502 * sync list than the regular list
503 */
504 if ((num_run > 32 &&
505 pending_bios != &device->pending_sync_bios &&
506 device->pending_sync_bios.head) ||
507 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
508 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400509 spin_lock(&device->io_lock);
510 requeue_list(pending_bios, pending, tail);
511 goto loop_lock;
512 }
513
Chris Mason8b712842008-06-11 16:50:36 -0400514 cur = pending;
515 pending = pending->bi_next;
516 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400517
Jens Axboedac56212015-04-17 16:23:59 -0600518 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400519
Chris Mason2ab1ba62011-08-04 14:28:36 -0400520 /*
521 * if we're doing the sync list, record that our
522 * plug has some sync requests on it
523 *
524 * If we're doing the regular list and there are
525 * sync requests sitting around, unplug before
526 * we add more
527 */
528 if (pending_bios == &device->pending_sync_bios) {
529 sync_pending = 1;
530 } else if (sync_pending) {
531 blk_finish_plug(&plug);
532 blk_start_plug(&plug);
533 sync_pending = 0;
534 }
535
Mike Christie4e49ea42016-06-05 14:31:41 -0500536 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400537 num_run++;
538 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100539
540 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400541
542 /*
543 * we made progress, there is more work to do and the bdi
544 * is now congested. Back off and let other work structs
545 * run instead
546 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400547 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500548 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400549 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400550
Chris Masonb765ead2009-04-03 10:27:10 -0400551 ioc = current->io_context;
552
553 /*
554 * the main goal here is that we don't want to
555 * block if we're going to be able to submit
556 * more requests without blocking.
557 *
558 * This code does two great things, it pokes into
559 * the elevator code from a filesystem _and_
560 * it makes assumptions about how batching works.
561 */
562 if (ioc && ioc->nr_batch_requests > 0 &&
563 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
564 (last_waited == 0 ||
565 ioc->last_waited == last_waited)) {
566 /*
567 * we want to go through our batch of
568 * requests and stop. So, we copy out
569 * the ioc->last_waited time and test
570 * against it before looping
571 */
572 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100573 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400574 continue;
575 }
Chris Mason8b712842008-06-11 16:50:36 -0400576 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400577 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500578 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400579
580 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800581 btrfs_queue_work(fs_info->submit_workers,
582 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400583 goto done;
584 }
585 }
Chris Masonffbd5172009-04-20 15:50:09 -0400586
Chris Mason51684082010-03-10 15:33:32 -0500587 cond_resched();
588 if (again)
589 goto loop;
590
591 spin_lock(&device->io_lock);
592 if (device->pending_bios.head || device->pending_sync_bios.head)
593 goto loop_lock;
594 spin_unlock(&device->io_lock);
595
Chris Mason8b712842008-06-11 16:50:36 -0400596done:
Chris Mason211588a2011-04-19 20:12:40 -0400597 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400598}
599
Christoph Hellwigb2950862008-12-02 09:54:17 -0500600static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400601{
602 struct btrfs_device *device;
603
604 device = container_of(work, struct btrfs_device, work);
605 run_scheduled_bios(device);
606}
607
Anand Jaind8367db2018-01-18 22:00:37 +0800608/*
609 * Search and remove all stale (devices which are not mounted) devices.
610 * When both inputs are NULL, it will search and release all stale devices.
611 * path: Optional. When provided will it release all unmounted devices
612 * matching this path only.
613 * skip_dev: Optional. Will skip this device when searching for the stale
614 * devices.
615 */
616static void btrfs_free_stale_devices(const char *path,
617 struct btrfs_device *skip_dev)
Anand Jain4fde46f2015-06-17 21:10:48 +0800618{
Anand Jain38cf6652018-01-18 22:00:34 +0800619 struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
620 struct btrfs_device *dev, *tmp_dev;
Anand Jain4fde46f2015-06-17 21:10:48 +0800621
Anand Jain38cf6652018-01-18 22:00:34 +0800622 list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800623
624 if (fs_devs->opened)
625 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800626
Anand Jain38cf6652018-01-18 22:00:34 +0800627 list_for_each_entry_safe(dev, tmp_dev,
628 &fs_devs->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800629 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800630
Anand Jaind8367db2018-01-18 22:00:37 +0800631 if (skip_dev && skip_dev == dev)
632 continue;
633 if (path && !dev->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800634 continue;
635
Anand Jain4fde46f2015-06-17 21:10:48 +0800636 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800637 if (path)
Anand Jain522f1b42018-01-18 22:00:35 +0800638 not_found = strcmp(rcu_str_deref(dev->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800639 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800640 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800641 if (not_found)
642 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800643
Anand Jain4fde46f2015-06-17 21:10:48 +0800644 /* delete the stale device */
645 if (fs_devs->num_devices == 1) {
646 btrfs_sysfs_remove_fsid(fs_devs);
647 list_del(&fs_devs->list);
648 free_fs_devices(fs_devs);
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200649 break;
Anand Jain4fde46f2015-06-17 21:10:48 +0800650 } else {
651 fs_devs->num_devices--;
652 list_del(&dev->dev_list);
David Sterba55de4802017-10-30 18:55:47 +0100653 free_device(dev);
Anand Jain4fde46f2015-06-17 21:10:48 +0800654 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800655 }
656 }
657}
658
Anand Jain0fb08bc2017-11-09 23:45:24 +0800659static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
660 struct btrfs_device *device, fmode_t flags,
661 void *holder)
662{
663 struct request_queue *q;
664 struct block_device *bdev;
665 struct buffer_head *bh;
666 struct btrfs_super_block *disk_super;
667 u64 devid;
668 int ret;
669
670 if (device->bdev)
671 return -EINVAL;
672 if (!device->name)
673 return -EINVAL;
674
675 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
676 &bdev, &bh);
677 if (ret)
678 return ret;
679
680 disk_super = (struct btrfs_super_block *)bh->b_data;
681 devid = btrfs_stack_device_id(&disk_super->dev_item);
682 if (devid != device->devid)
683 goto error_brelse;
684
685 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
686 goto error_brelse;
687
688 device->generation = btrfs_super_generation(disk_super);
689
690 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Anand Jainebbede42017-12-04 12:54:52 +0800691 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800692 fs_devices->seeding = 1;
693 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800694 if (bdev_read_only(bdev))
695 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
696 else
697 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800698 }
699
700 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800701 if (!blk_queue_nonrot(q))
702 fs_devices->rotating = 1;
703
704 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800705 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800706 device->mode = flags;
707
708 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800709 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
710 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800711 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800712 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800713 }
714 brelse(bh);
715
716 return 0;
717
718error_brelse:
719 brelse(bh);
720 blkdev_put(bdev, flags);
721
722 return -EINVAL;
723}
724
David Sterba60999ca2014-03-26 18:26:36 +0100725/*
726 * Add new device to list of registered devices
727 *
728 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800729 * device pointer which was just added or updated when successful
730 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100731 */
Anand Jaine124ece2018-01-18 22:02:35 +0800732static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain3acbcbf2018-01-18 22:02:36 +0800733 struct btrfs_super_block *disk_super)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400734{
735 struct btrfs_device *device;
736 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400737 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400738 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800739 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400740
741 fs_devices = find_fsid(disk_super->fsid);
742 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300743 fs_devices = alloc_fs_devices(disk_super->fsid);
744 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800745 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300746
Chris Mason8a4b83c2008-03-24 15:02:07 -0400747 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300748
Chris Mason8a4b83c2008-03-24 15:02:07 -0400749 device = NULL;
750 } else {
David Sterba35c70102017-06-15 19:51:51 +0200751 device = find_device(fs_devices, devid,
752 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400753 }
Miao Xie443f24f2014-07-24 11:37:15 +0800754
Chris Mason8a4b83c2008-03-24 15:02:07 -0400755 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500756 if (fs_devices->opened)
Anand Jaine124ece2018-01-18 22:02:35 +0800757 return ERR_PTR(-EBUSY);
Yan Zheng2b820322008-11-17 21:11:30 -0500758
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300759 device = btrfs_alloc_device(NULL, &devid,
760 disk_super->dev_item.uuid);
761 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400762 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800763 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764 }
Josef Bacik606686e2012-06-04 14:03:51 -0400765
766 name = rcu_string_strdup(path, GFP_NOFS);
767 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100768 free_device(device);
Anand Jaine124ece2018-01-18 22:02:35 +0800769 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400770 }
Josef Bacik606686e2012-06-04 14:03:51 -0400771 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200772
Chris Masone5e9a522009-06-10 15:17:02 -0400773 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000774 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100775 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400776 mutex_unlock(&fs_devices->device_list_mutex);
777
Yan Zheng2b820322008-11-17 21:11:30 -0500778 device->fs_devices = fs_devices;
Anand Jaind8367db2018-01-18 22:00:37 +0800779 btrfs_free_stale_devices(path, device);
Anand Jain327f18c2018-01-18 22:02:33 +0800780
781 if (disk_super->label[0])
782 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
783 disk_super->label, devid, found_transid, path);
784 else
785 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
786 disk_super->fsid, devid, found_transid, path);
787
Josef Bacik606686e2012-06-04 14:03:51 -0400788 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800789 /*
790 * When FS is already mounted.
791 * 1. If you are here and if the device->name is NULL that
792 * means this device was missing at time of FS mount.
793 * 2. If you are here and if the device->name is different
794 * from 'path' that means either
795 * a. The same device disappeared and reappeared with
796 * different name. or
797 * b. The missing-disk-which-was-replaced, has
798 * reappeared now.
799 *
800 * We must allow 1 and 2a above. But 2b would be a spurious
801 * and unintentional.
802 *
803 * Further in case of 1 and 2a above, the disk at 'path'
804 * would have missed some transaction when it was away and
805 * in case of 2a the stale bdev has to be updated as well.
806 * 2b must not be allowed at all time.
807 */
808
809 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700810 * For now, we do allow update to btrfs_fs_device through the
811 * btrfs dev scan cli after FS has been mounted. We're still
812 * tracking a problem where systems fail mount by subvolume id
813 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800814 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700815 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800816 /*
817 * That is if the FS is _not_ mounted and if you
818 * are here, that means there is more than one
819 * disk with same uuid and devid.We keep the one
820 * with larger generation number or the last-in if
821 * generation are equal.
822 */
Anand Jaine124ece2018-01-18 22:02:35 +0800823 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800824 }
Anand Jainb96de002014-07-03 18:22:05 +0800825
Josef Bacik606686e2012-06-04 14:03:51 -0400826 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000827 if (!name)
Anand Jaine124ece2018-01-18 22:02:35 +0800828 return ERR_PTR(-ENOMEM);
Josef Bacik606686e2012-06-04 14:03:51 -0400829 rcu_string_free(device->name);
830 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800831 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500832 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800833 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500834 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400835 }
836
Anand Jain77bdae42014-07-03 18:22:06 +0800837 /*
838 * Unmount does not free the btrfs_device struct but would zero
839 * generation along with most of the other members. So just update
840 * it back. We need it to pick the disk with largest generation
841 * (as above).
842 */
843 if (!fs_devices->opened)
844 device->generation = found_transid;
845
Anand Jainf2788d22018-01-18 22:02:34 +0800846 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
847
Anand Jaine124ece2018-01-18 22:02:35 +0800848 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400849}
850
Yan Zhenge4404d62008-12-12 10:03:26 -0500851static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
852{
853 struct btrfs_fs_devices *fs_devices;
854 struct btrfs_device *device;
855 struct btrfs_device *orig_dev;
856
Ilya Dryomov2208a372013-08-12 14:33:03 +0300857 fs_devices = alloc_fs_devices(orig->fsid);
858 if (IS_ERR(fs_devices))
859 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500860
Miao Xieadbbb862014-09-03 21:35:42 +0800861 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400862 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500863
Xiao Guangrong46224702011-04-20 10:08:47 +0000864 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500865 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400866 struct rcu_string *name;
867
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300868 device = btrfs_alloc_device(NULL, &orig_dev->devid,
869 orig_dev->uuid);
870 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500871 goto error;
872
Josef Bacik606686e2012-06-04 14:03:51 -0400873 /*
874 * This is ok to do without rcu read locked because we hold the
875 * uuid mutex so nothing we touch in here is going to disappear.
876 */
Anand Jaine755f782014-06-30 17:12:47 +0800877 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100878 name = rcu_string_strdup(orig_dev->name->str,
879 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800880 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100881 free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800882 goto error;
883 }
884 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400885 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500886
Yan Zhenge4404d62008-12-12 10:03:26 -0500887 list_add(&device->dev_list, &fs_devices->devices);
888 device->fs_devices = fs_devices;
889 fs_devices->num_devices++;
890 }
Miao Xieadbbb862014-09-03 21:35:42 +0800891 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500892 return fs_devices;
893error:
Miao Xieadbbb862014-09-03 21:35:42 +0800894 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500895 free_fs_devices(fs_devices);
896 return ERR_PTR(-ENOMEM);
897}
898
Anand Jain9b99b112018-02-27 12:41:59 +0800899/*
900 * After we have read the system tree and know devids belonging to
901 * this filesystem, remove the device which does not belong there.
902 */
903void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400904{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500905 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800906 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500907
Chris Masondfe25022008-05-13 13:46:40 -0400908 mutex_lock(&uuid_mutex);
909again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000910 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500911 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +0800912 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
913 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +0800914 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
915 &device->dev_state) &&
916 (!latest_dev ||
917 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +0800918 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500919 }
Yan Zheng2b820322008-11-17 21:11:30 -0500920 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500921 }
Yan Zheng2b820322008-11-17 21:11:30 -0500922
Stefan Behrens8dabb742012-11-06 13:15:27 +0100923 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
924 /*
925 * In the first step, keep the device which has
926 * the correct fsid and the devid that is used
927 * for the dev_replace procedure.
928 * In the second step, the dev_replace state is
929 * read from the device tree and it is known
930 * whether the procedure is really active or
931 * not, which means whether this device is
932 * used or whether it should be removed.
933 */
Anand Jain401e29c2017-12-04 12:54:55 +0800934 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
935 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +0100936 continue;
937 }
938 }
Yan Zheng2b820322008-11-17 21:11:30 -0500939 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100940 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500941 device->bdev = NULL;
942 fs_devices->open_devices--;
943 }
Anand Jainebbede42017-12-04 12:54:52 +0800944 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500945 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +0800946 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +0800947 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
948 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +0100949 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500950 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500951 list_del_init(&device->dev_list);
952 fs_devices->num_devices--;
David Sterba55de4802017-10-30 18:55:47 +0100953 free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400954 }
Yan Zheng2b820322008-11-17 21:11:30 -0500955
956 if (fs_devices->seed) {
957 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500958 goto again;
959 }
960
Miao Xie443f24f2014-07-24 11:37:15 +0800961 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500962
Chris Masondfe25022008-05-13 13:46:40 -0400963 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400964}
Chris Masona0af4692008-05-13 16:03:06 -0400965
David Sterbaf06c5962017-06-06 17:08:23 +0200966static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000967{
968 struct btrfs_device *device;
969
970 device = container_of(head, struct btrfs_device, rcu);
David Sterba55de4802017-10-30 18:55:47 +0100971 free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000972}
973
Anand Jain14238812016-07-22 06:04:53 +0800974static void btrfs_close_bdev(struct btrfs_device *device)
975{
David Sterba08ffcae2017-06-19 16:55:35 +0200976 if (!device->bdev)
977 return;
978
Anand Jainebbede42017-12-04 12:54:52 +0800979 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +0800980 sync_blockdev(device->bdev);
981 invalidate_bdev(device->bdev);
982 }
983
David Sterba08ffcae2017-06-19 16:55:35 +0200984 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +0800985}
986
Anand Jain0ccd0522016-09-22 12:56:13 +0800987static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +0800988{
989 struct btrfs_fs_devices *fs_devices = device->fs_devices;
990 struct btrfs_device *new_device;
991 struct rcu_string *name;
992
993 if (device->bdev)
994 fs_devices->open_devices--;
995
Anand Jainebbede42017-12-04 12:54:52 +0800996 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +0800997 device->devid != BTRFS_DEV_REPLACE_DEVID) {
998 list_del_init(&device->dev_alloc_list);
999 fs_devices->rw_devices--;
1000 }
1001
Anand Jaine6e674b2017-12-04 12:54:54 +08001002 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001003 fs_devices->missing_devices--;
1004
1005 new_device = btrfs_alloc_device(NULL, &device->devid,
1006 device->uuid);
1007 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1008
1009 /* Safe because we are under uuid_mutex */
1010 if (device->name) {
1011 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1012 BUG_ON(!name); /* -ENOMEM */
1013 rcu_assign_pointer(new_device->name, name);
1014 }
1015
1016 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1017 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001018}
1019
Yan Zheng2b820322008-11-17 21:11:30 -05001020static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001021{
Sasha Levin2037a092015-05-12 19:31:37 -04001022 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +08001023 struct list_head pending_put;
1024
1025 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -05001026
Yan Zheng2b820322008-11-17 21:11:30 -05001027 if (--fs_devices->opened > 0)
1028 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001029
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001030 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001031 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +08001032 btrfs_prepare_close_one_device(device);
1033 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001034 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001035 mutex_unlock(&fs_devices->device_list_mutex);
1036
Anand Jain0ccd0522016-09-22 12:56:13 +08001037 /*
1038 * btrfs_show_devname() is using the device_list_mutex,
1039 * sometimes call to blkdev_put() leads vfs calling
1040 * into this func. So do put outside of device_list_mutex,
1041 * as of now.
1042 */
1043 while (!list_empty(&pending_put)) {
1044 device = list_first_entry(&pending_put,
1045 struct btrfs_device, dev_list);
1046 list_del(&device->dev_list);
1047 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001048 call_rcu(&device->rcu, free_device_rcu);
Anand Jain0ccd0522016-09-22 12:56:13 +08001049 }
1050
Yan Zhenge4404d62008-12-12 10:03:26 -05001051 WARN_ON(fs_devices->open_devices);
1052 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001053 fs_devices->opened = 0;
1054 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001055
Chris Mason8a4b83c2008-03-24 15:02:07 -04001056 return 0;
1057}
1058
Yan Zheng2b820322008-11-17 21:11:30 -05001059int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1060{
Yan Zhenge4404d62008-12-12 10:03:26 -05001061 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001062 int ret;
1063
1064 mutex_lock(&uuid_mutex);
1065 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001066 if (!fs_devices->opened) {
1067 seed_devices = fs_devices->seed;
1068 fs_devices->seed = NULL;
1069 }
Yan Zheng2b820322008-11-17 21:11:30 -05001070 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001071
1072 while (seed_devices) {
1073 fs_devices = seed_devices;
1074 seed_devices = fs_devices->seed;
1075 __btrfs_close_devices(fs_devices);
1076 free_fs_devices(fs_devices);
1077 }
Yan Zheng2b820322008-11-17 21:11:30 -05001078 return ret;
1079}
1080
Yan Zhenge4404d62008-12-12 10:03:26 -05001081static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1082 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001083{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001084 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001085 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001086 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001087 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001088
Tejun Heod4d77622010-11-13 11:55:18 +01001089 flags |= FMODE_EXCL;
1090
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001091 list_for_each_entry(device, head, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001092 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001093 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001094 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001095
Anand Jain9f050db2017-11-09 23:45:25 +08001096 if (!latest_dev ||
1097 device->generation > latest_dev->generation)
1098 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001099 }
Chris Masona0af4692008-05-13 16:03:06 -04001100 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001101 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001102 goto out;
1103 }
Yan Zheng2b820322008-11-17 21:11:30 -05001104 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001105 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001106 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001107out:
Yan Zheng2b820322008-11-17 21:11:30 -05001108 return ret;
1109}
1110
Anand Jainf8e10cd2018-01-22 14:49:36 -08001111static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1112{
1113 struct btrfs_device *dev1, *dev2;
1114
1115 dev1 = list_entry(a, struct btrfs_device, dev_list);
1116 dev2 = list_entry(b, struct btrfs_device, dev_list);
1117
1118 if (dev1->devid < dev2->devid)
1119 return -1;
1120 else if (dev1->devid > dev2->devid)
1121 return 1;
1122 return 0;
1123}
1124
Yan Zheng2b820322008-11-17 21:11:30 -05001125int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001126 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001127{
1128 int ret;
1129
1130 mutex_lock(&uuid_mutex);
1131 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001132 fs_devices->opened++;
1133 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001134 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001135 list_sort(NULL, &fs_devices->devices, devid_cmp);
Chris Mason15916de2008-11-19 21:17:22 -05001136 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001137 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001138 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001139 return ret;
1140}
1141
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001142static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001143{
1144 kunmap(page);
1145 put_page(page);
1146}
1147
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001148static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1149 struct page **page,
1150 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001151{
1152 void *p;
1153 pgoff_t index;
1154
1155 /* make sure our super fits in the device */
1156 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1157 return 1;
1158
1159 /* make sure our super fits in the page */
1160 if (sizeof(**disk_super) > PAGE_SIZE)
1161 return 1;
1162
1163 /* make sure our super doesn't straddle pages on disk */
1164 index = bytenr >> PAGE_SHIFT;
1165 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1166 return 1;
1167
1168 /* pull in the page with our super */
1169 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1170 index, GFP_KERNEL);
1171
1172 if (IS_ERR_OR_NULL(*page))
1173 return 1;
1174
1175 p = kmap(*page);
1176
1177 /* align our pointer to the offset of the super block */
1178 *disk_super = p + (bytenr & ~PAGE_MASK);
1179
1180 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1181 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1182 btrfs_release_disk_super(*page);
1183 return 1;
1184 }
1185
1186 if ((*disk_super)->label[0] &&
1187 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1188 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1189
1190 return 0;
1191}
1192
David Sterba6f60cbd2013-02-15 11:31:02 -07001193/*
1194 * Look for a btrfs signature on a device. This may be called out of the mount path
1195 * and we are not allowed to call set_blocksize during the scan. The superblock
1196 * is read via pagecache
1197 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001198int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001199 struct btrfs_fs_devices **fs_devices_ret)
1200{
1201 struct btrfs_super_block *disk_super;
Anand Jaine124ece2018-01-18 22:02:35 +08001202 struct btrfs_device *device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001203 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001204 struct page *page;
Anand Jaine124ece2018-01-18 22:02:35 +08001205 int ret = 0;
David Sterba6f60cbd2013-02-15 11:31:02 -07001206 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001207
David Sterba6f60cbd2013-02-15 11:31:02 -07001208 /*
1209 * we would like to check all the supers, but that would make
1210 * a btrfs mount succeed after a mkfs from a different FS.
1211 * So, we need to add a special mount option to scan for
1212 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1213 */
1214 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001215 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001216 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001217
1218 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001219 if (IS_ERR(bdev)) {
1220 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001221 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001222 }
1223
Anand Jain05a5c552017-12-15 15:40:16 +08001224 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1225 ret = -EINVAL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001226 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001227 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001228
Anand Jain3acbcbf2018-01-18 22:02:36 +08001229 device = device_list_add(path, disk_super);
Anand Jaine124ece2018-01-18 22:02:35 +08001230 if (IS_ERR(device))
1231 ret = PTR_ERR(device);
1232 else
1233 *fs_devices_ret = device->fs_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001234
Anand Jain6cf86a002016-02-13 10:01:29 +08001235 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001236
1237error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001238 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001239error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001240 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001241 return ret;
1242}
Chris Mason0b86a832008-03-24 15:01:56 -04001243
Miao Xie6d07bce2011-01-05 10:07:31 +00001244/* helper to account the used device space in the range */
1245int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1246 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001247{
1248 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001249 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001250 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001251 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001252 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001253 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001254 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001255 struct extent_buffer *l;
1256
Miao Xie6d07bce2011-01-05 10:07:31 +00001257 *length = 0;
1258
Anand Jain401e29c2017-12-04 12:54:55 +08001259 if (start >= device->total_bytes ||
1260 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Miao Xie6d07bce2011-01-05 10:07:31 +00001261 return 0;
1262
Yan Zheng2b820322008-11-17 21:11:30 -05001263 path = btrfs_alloc_path();
1264 if (!path)
1265 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001266 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001267
Chris Mason0b86a832008-03-24 15:01:56 -04001268 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001269 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001270 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001271
1272 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001273 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001274 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001275 if (ret > 0) {
1276 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1277 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001278 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001279 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001280
Chris Mason0b86a832008-03-24 15:01:56 -04001281 while (1) {
1282 l = path->nodes[0];
1283 slot = path->slots[0];
1284 if (slot >= btrfs_header_nritems(l)) {
1285 ret = btrfs_next_leaf(root, path);
1286 if (ret == 0)
1287 continue;
1288 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001289 goto out;
1290
1291 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001292 }
1293 btrfs_item_key_to_cpu(l, &key, slot);
1294
1295 if (key.objectid < device->devid)
1296 goto next;
1297
1298 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001299 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001300
David Sterba962a2982014-06-04 18:41:45 +02001301 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001302 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001303
Chris Mason0b86a832008-03-24 15:01:56 -04001304 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001305 extent_end = key.offset + btrfs_dev_extent_length(l,
1306 dev_extent);
1307 if (key.offset <= start && extent_end > end) {
1308 *length = end - start + 1;
1309 break;
1310 } else if (key.offset <= start && extent_end > start)
1311 *length += extent_end - start;
1312 else if (key.offset > start && extent_end <= end)
1313 *length += extent_end - key.offset;
1314 else if (key.offset > start && key.offset <= end) {
1315 *length += end - key.offset + 1;
1316 break;
1317 } else if (key.offset > end)
1318 break;
1319
1320next:
1321 path->slots[0]++;
1322 }
1323 ret = 0;
1324out:
1325 btrfs_free_path(path);
1326 return ret;
1327}
1328
Jeff Mahoney499f3772015-06-15 09:41:17 -04001329static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001330 struct btrfs_device *device,
1331 u64 *start, u64 len)
1332{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001333 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001334 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001335 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001336 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001337 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001338
Jeff Mahoney499f3772015-06-15 09:41:17 -04001339 if (transaction)
1340 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001341again:
1342 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001343 struct map_lookup *map;
1344 int i;
1345
Jeff Mahoney95617d62015-06-03 10:55:48 -04001346 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001347 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001348 u64 end;
1349
Josef Bacik6df9a952013-06-27 13:22:46 -04001350 if (map->stripes[i].dev != device)
1351 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001352 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001353 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001354 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001355 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001356 /*
1357 * Make sure that while processing the pinned list we do
1358 * not override our *start with a lower value, because
1359 * we can have pinned chunks that fall within this
1360 * device hole and that have lower physical addresses
1361 * than the pending chunks we processed before. If we
1362 * do not take this special care we can end up getting
1363 * 2 pending chunks that start at the same physical
1364 * device offsets because the end offset of a pinned
1365 * chunk can be equal to the start offset of some
1366 * pending chunk.
1367 */
1368 end = map->stripes[i].physical + em->orig_block_len;
1369 if (end > *start) {
1370 *start = end;
1371 ret = 1;
1372 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 }
1374 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001375 if (search_list != &fs_info->pinned_chunks) {
1376 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001377 goto again;
1378 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001379
1380 return ret;
1381}
1382
1383
Chris Mason0b86a832008-03-24 15:01:56 -04001384/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001385 * find_free_dev_extent_start - find free space in the specified device
1386 * @device: the device which we search the free space in
1387 * @num_bytes: the size of the free space that we need
1388 * @search_start: the position from which to begin the search
1389 * @start: store the start of the free space.
1390 * @len: the size of the free space. that we find, or the size
1391 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001392 *
Chris Mason0b86a832008-03-24 15:01:56 -04001393 * this uses a pretty simple search, the expectation is that it is
1394 * called very infrequently and that a given device has a small number
1395 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001396 *
1397 * @start is used to store the start of the free space if we find. But if we
1398 * don't find suitable free space, it will be used to store the start position
1399 * of the max free space.
1400 *
1401 * @len is used to store the size of the free space that we find.
1402 * But if we don't find suitable free space, it is used to store the size of
1403 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001404 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001405int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1406 struct btrfs_device *device, u64 num_bytes,
1407 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 struct btrfs_fs_info *fs_info = device->fs_info;
1410 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001411 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001412 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001413 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001414 u64 hole_size;
1415 u64 max_hole_start;
1416 u64 max_hole_size;
1417 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001418 u64 search_end = device->total_bytes;
1419 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001420 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001422
1423 /*
1424 * We don't want to overwrite the superblock on the drive nor any area
1425 * used by the boot loader (grub for example), so we make sure to start
1426 * at an offset of at least 1MB.
1427 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001428 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001429
Josef Bacik6df9a952013-06-27 13:22:46 -04001430 path = btrfs_alloc_path();
1431 if (!path)
1432 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001433
Miao Xie7bfc8372011-01-05 10:07:26 +00001434 max_hole_start = search_start;
1435 max_hole_size = 0;
1436
Zhao Leif2ab7612015-02-16 18:52:17 +08001437again:
Anand Jain401e29c2017-12-04 12:54:55 +08001438 if (search_start >= search_end ||
1439 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001440 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001441 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001442 }
1443
David Sterbae4058b52015-11-27 16:31:35 +01001444 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001445 path->search_commit_root = 1;
1446 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001447
Chris Mason0b86a832008-03-24 15:01:56 -04001448 key.objectid = device->devid;
1449 key.offset = search_start;
1450 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001451
Li Zefan125ccb02011-12-08 15:07:24 +08001452 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001453 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001454 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001455 if (ret > 0) {
1456 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1457 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001458 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001459 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001460
Chris Mason0b86a832008-03-24 15:01:56 -04001461 while (1) {
1462 l = path->nodes[0];
1463 slot = path->slots[0];
1464 if (slot >= btrfs_header_nritems(l)) {
1465 ret = btrfs_next_leaf(root, path);
1466 if (ret == 0)
1467 continue;
1468 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001469 goto out;
1470
1471 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001472 }
1473 btrfs_item_key_to_cpu(l, &key, slot);
1474
1475 if (key.objectid < device->devid)
1476 goto next;
1477
1478 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001479 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001480
David Sterba962a2982014-06-04 18:41:45 +02001481 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001482 goto next;
1483
Miao Xie7bfc8372011-01-05 10:07:26 +00001484 if (key.offset > search_start) {
1485 hole_size = key.offset - search_start;
1486
Josef Bacik6df9a952013-06-27 13:22:46 -04001487 /*
1488 * Have to check before we set max_hole_start, otherwise
1489 * we could end up sending back this offset anyway.
1490 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001491 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001492 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001493 hole_size)) {
1494 if (key.offset >= search_start) {
1495 hole_size = key.offset - search_start;
1496 } else {
1497 WARN_ON_ONCE(1);
1498 hole_size = 0;
1499 }
1500 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001501
Miao Xie7bfc8372011-01-05 10:07:26 +00001502 if (hole_size > max_hole_size) {
1503 max_hole_start = search_start;
1504 max_hole_size = hole_size;
1505 }
1506
1507 /*
1508 * If this free space is greater than which we need,
1509 * it must be the max free space that we have found
1510 * until now, so max_hole_start must point to the start
1511 * of this free space and the length of this free space
1512 * is stored in max_hole_size. Thus, we return
1513 * max_hole_start and max_hole_size and go back to the
1514 * caller.
1515 */
1516 if (hole_size >= num_bytes) {
1517 ret = 0;
1518 goto out;
1519 }
1520 }
1521
Chris Mason0b86a832008-03-24 15:01:56 -04001522 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001523 extent_end = key.offset + btrfs_dev_extent_length(l,
1524 dev_extent);
1525 if (extent_end > search_start)
1526 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001527next:
1528 path->slots[0]++;
1529 cond_resched();
1530 }
Chris Mason0b86a832008-03-24 15:01:56 -04001531
liubo38c01b92011-08-02 02:39:03 +00001532 /*
1533 * At this point, search_start should be the end of
1534 * allocated dev extents, and when shrinking the device,
1535 * search_end may be smaller than search_start.
1536 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001537 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001538 hole_size = search_end - search_start;
1539
Jeff Mahoney499f3772015-06-15 09:41:17 -04001540 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001541 hole_size)) {
1542 btrfs_release_path(path);
1543 goto again;
1544 }
Chris Mason0b86a832008-03-24 15:01:56 -04001545
Zhao Leif2ab7612015-02-16 18:52:17 +08001546 if (hole_size > max_hole_size) {
1547 max_hole_start = search_start;
1548 max_hole_size = hole_size;
1549 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001550 }
1551
Miao Xie7bfc8372011-01-05 10:07:26 +00001552 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001553 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001554 ret = -ENOSPC;
1555 else
1556 ret = 0;
1557
1558out:
Yan Zheng2b820322008-11-17 21:11:30 -05001559 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001560 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001561 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001562 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001563 return ret;
1564}
1565
Jeff Mahoney499f3772015-06-15 09:41:17 -04001566int find_free_dev_extent(struct btrfs_trans_handle *trans,
1567 struct btrfs_device *device, u64 num_bytes,
1568 u64 *start, u64 *len)
1569{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001570 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001571 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001572 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001573}
1574
Christoph Hellwigb2950862008-12-02 09:54:17 -05001575static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001576 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001577 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001578{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001579 struct btrfs_fs_info *fs_info = device->fs_info;
1580 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001581 int ret;
1582 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001583 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001584 struct btrfs_key found_key;
1585 struct extent_buffer *leaf = NULL;
1586 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001587
1588 path = btrfs_alloc_path();
1589 if (!path)
1590 return -ENOMEM;
1591
1592 key.objectid = device->devid;
1593 key.offset = start;
1594 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001595again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001596 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001597 if (ret > 0) {
1598 ret = btrfs_previous_item(root, path, key.objectid,
1599 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001600 if (ret)
1601 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001602 leaf = path->nodes[0];
1603 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1604 extent = btrfs_item_ptr(leaf, path->slots[0],
1605 struct btrfs_dev_extent);
1606 BUG_ON(found_key.offset > start || found_key.offset +
1607 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001608 key = found_key;
1609 btrfs_release_path(path);
1610 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001611 } else if (ret == 0) {
1612 leaf = path->nodes[0];
1613 extent = btrfs_item_ptr(leaf, path->slots[0],
1614 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001615 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001616 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001617 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001618 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001619
Miao Xie2196d6e2014-09-03 21:35:41 +08001620 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1621
Chris Mason8f18cf12008-04-25 16:53:30 -04001622 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001623 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001624 btrfs_handle_fs_error(fs_info, ret,
1625 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001626 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001627 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001628 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001629out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001630 btrfs_free_path(path);
1631 return ret;
1632}
1633
Eric Sandeen48a3b632013-04-25 20:41:01 +00001634static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1635 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001636 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001637{
1638 int ret;
1639 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001640 struct btrfs_fs_info *fs_info = device->fs_info;
1641 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001642 struct btrfs_dev_extent *extent;
1643 struct extent_buffer *leaf;
1644 struct btrfs_key key;
1645
Anand Jaine12c9622017-12-04 12:54:53 +08001646 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001647 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001648 path = btrfs_alloc_path();
1649 if (!path)
1650 return -ENOMEM;
1651
Chris Mason0b86a832008-03-24 15:01:56 -04001652 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001653 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001654 key.type = BTRFS_DEV_EXTENT_KEY;
1655 ret = btrfs_insert_empty_item(trans, root, path, &key,
1656 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001657 if (ret)
1658 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001659
1660 leaf = path->nodes[0];
1661 extent = btrfs_item_ptr(leaf, path->slots[0],
1662 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001663 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1664 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001665 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1666 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001667 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1668
Chris Mason0b86a832008-03-24 15:01:56 -04001669 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1670 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001671out:
Chris Mason0b86a832008-03-24 15:01:56 -04001672 btrfs_free_path(path);
1673 return ret;
1674}
1675
Josef Bacik6df9a952013-06-27 13:22:46 -04001676static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001677{
Josef Bacik6df9a952013-06-27 13:22:46 -04001678 struct extent_map_tree *em_tree;
1679 struct extent_map *em;
1680 struct rb_node *n;
1681 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001682
Josef Bacik6df9a952013-06-27 13:22:46 -04001683 em_tree = &fs_info->mapping_tree.map_tree;
1684 read_lock(&em_tree->lock);
1685 n = rb_last(&em_tree->map);
1686 if (n) {
1687 em = rb_entry(n, struct extent_map, rb_node);
1688 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001689 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001690 read_unlock(&em_tree->lock);
1691
Chris Mason0b86a832008-03-24 15:01:56 -04001692 return ret;
1693}
1694
Ilya Dryomov53f10652013-08-12 14:33:01 +03001695static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1696 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001697{
1698 int ret;
1699 struct btrfs_key key;
1700 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001701 struct btrfs_path *path;
1702
Yan Zheng2b820322008-11-17 21:11:30 -05001703 path = btrfs_alloc_path();
1704 if (!path)
1705 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001706
1707 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1708 key.type = BTRFS_DEV_ITEM_KEY;
1709 key.offset = (u64)-1;
1710
Ilya Dryomov53f10652013-08-12 14:33:01 +03001711 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001712 if (ret < 0)
1713 goto error;
1714
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001715 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001716
Ilya Dryomov53f10652013-08-12 14:33:01 +03001717 ret = btrfs_previous_item(fs_info->chunk_root, path,
1718 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001719 BTRFS_DEV_ITEM_KEY);
1720 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001721 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001722 } else {
1723 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1724 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001725 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001726 }
1727 ret = 0;
1728error:
Yan Zheng2b820322008-11-17 21:11:30 -05001729 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001730 return ret;
1731}
1732
1733/*
1734 * the device information is stored in the chunk root
1735 * the btrfs_device struct should be fully filled in
1736 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001737static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001738 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001739 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001740{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001741 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001742 int ret;
1743 struct btrfs_path *path;
1744 struct btrfs_dev_item *dev_item;
1745 struct extent_buffer *leaf;
1746 struct btrfs_key key;
1747 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001748
Chris Mason0b86a832008-03-24 15:01:56 -04001749 path = btrfs_alloc_path();
1750 if (!path)
1751 return -ENOMEM;
1752
Chris Mason0b86a832008-03-24 15:01:56 -04001753 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1754 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001755 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001756
1757 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001758 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001759 if (ret)
1760 goto out;
1761
1762 leaf = path->nodes[0];
1763 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1764
1765 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001766 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001767 btrfs_set_device_type(leaf, dev_item, device->type);
1768 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1769 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1770 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001771 btrfs_set_device_total_bytes(leaf, dev_item,
1772 btrfs_device_get_disk_total_bytes(device));
1773 btrfs_set_device_bytes_used(leaf, dev_item,
1774 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001775 btrfs_set_device_group(leaf, dev_item, 0);
1776 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1777 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001778 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001779
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001780 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001781 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001782 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001783 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001784 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001785
Yan Zheng2b820322008-11-17 21:11:30 -05001786 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001787out:
1788 btrfs_free_path(path);
1789 return ret;
1790}
Chris Mason8f18cf12008-04-25 16:53:30 -04001791
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001792/*
1793 * Function to update ctime/mtime for a given device path.
1794 * Mainly used for ctime/mtime based probe like libblkid.
1795 */
David Sterbada353f62017-02-14 17:55:53 +01001796static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001797{
1798 struct file *filp;
1799
1800 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001801 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001802 return;
1803 file_update_time(filp);
1804 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001805}
1806
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001807static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001808 struct btrfs_device *device)
1809{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001810 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001811 int ret;
1812 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001813 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001814 struct btrfs_trans_handle *trans;
1815
Chris Masona061fc82008-05-07 11:43:44 -04001816 path = btrfs_alloc_path();
1817 if (!path)
1818 return -ENOMEM;
1819
Yan, Zhenga22285a2010-05-16 10:48:46 -04001820 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001821 if (IS_ERR(trans)) {
1822 btrfs_free_path(path);
1823 return PTR_ERR(trans);
1824 }
Chris Masona061fc82008-05-07 11:43:44 -04001825 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1826 key.type = BTRFS_DEV_ITEM_KEY;
1827 key.offset = device->devid;
1828
1829 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001830 if (ret) {
1831 if (ret > 0)
1832 ret = -ENOENT;
1833 btrfs_abort_transaction(trans, ret);
1834 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001835 goto out;
1836 }
1837
1838 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001839 if (ret) {
1840 btrfs_abort_transaction(trans, ret);
1841 btrfs_end_transaction(trans);
1842 }
1843
Chris Masona061fc82008-05-07 11:43:44 -04001844out:
1845 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001846 if (!ret)
1847 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001848 return ret;
1849}
1850
David Sterba3cc31a02016-02-15 16:00:26 +01001851/*
1852 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1853 * filesystem. It's up to the caller to adjust that number regarding eg. device
1854 * replace.
1855 */
1856static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1857 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001858{
Chris Masona061fc82008-05-07 11:43:44 -04001859 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001860 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001861 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001862
Miao Xiede98ced2013-01-29 10:13:12 +00001863 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001864 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001865
Anand Jainbd45ffb2016-02-13 10:01:34 +08001866 all_avail = fs_info->avail_data_alloc_bits |
1867 fs_info->avail_system_alloc_bits |
1868 fs_info->avail_metadata_alloc_bits;
1869 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001870
David Sterba418775a2016-02-15 16:28:14 +01001871 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1872 if (!(all_avail & btrfs_raid_group[i]))
1873 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001874
David Sterba418775a2016-02-15 16:28:14 +01001875 if (num_devices < btrfs_raid_array[i].devs_min) {
1876 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001877
David Sterba418775a2016-02-15 16:28:14 +01001878 if (ret)
1879 return ret;
1880 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001881 }
1882
1883 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001884}
1885
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001886static struct btrfs_device * btrfs_find_next_active_device(
1887 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001888{
1889 struct btrfs_device *next_device;
1890
1891 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1892 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001893 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1894 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001895 return next_device;
1896 }
1897
1898 return NULL;
1899}
1900
1901/*
1902 * Helper function to check if the given device is part of s_bdev / latest_bdev
1903 * and replace it with the provided or the next active device, in the context
1904 * where this function called, there should be always be another device (or
1905 * this_dev) which is active.
1906 */
1907void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1908 struct btrfs_device *device, struct btrfs_device *this_dev)
1909{
1910 struct btrfs_device *next_device;
1911
1912 if (this_dev)
1913 next_device = this_dev;
1914 else
1915 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1916 device);
1917 ASSERT(next_device);
1918
1919 if (fs_info->sb->s_bdev &&
1920 (fs_info->sb->s_bdev == device->bdev))
1921 fs_info->sb->s_bdev = next_device->bdev;
1922
1923 if (fs_info->fs_devices->latest_bdev == device->bdev)
1924 fs_info->fs_devices->latest_bdev = next_device->bdev;
1925}
1926
David Sterbada353f62017-02-14 17:55:53 +01001927int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1928 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001929{
1930 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001931 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001932 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001933 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001934
Anand Jain2c997382017-11-06 10:28:00 +08001935 mutex_lock(&fs_info->volume_mutex);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001936 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001937
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001938 num_devices = fs_info->fs_devices->num_devices;
1939 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1940 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001941 WARN_ON(num_devices < 1);
1942 num_devices--;
1943 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001945
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001946 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001947 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001948 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001949
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001950 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1951 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001952 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001953 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001954
Anand Jain401e29c2017-12-04 12:54:55 +08001955 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001956 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001957 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001958 }
1959
Anand Jainebbede42017-12-04 12:54:52 +08001960 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1961 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001962 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001963 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001964 }
1965
Anand Jainebbede42017-12-04 12:54:52 +08001966 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001967 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001968 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001969 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001970 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001971 }
Chris Masona061fc82008-05-07 11:43:44 -04001972
Carey Underwoodd7901552013-03-04 16:37:06 -06001973 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001974 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001975 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001976 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001977 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001978
Stefan Behrens63a212a2012-11-05 18:29:28 +01001979 /*
1980 * TODO: the superblock still includes this device in its num_devices
1981 * counter although write_all_supers() is not locked out. This
1982 * could give a filesystem state which requires a degraded mount.
1983 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001984 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001985 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001986 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001987
Anand Jaine12c9622017-12-04 12:54:53 +08001988 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001989 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001990
1991 /*
1992 * the device list mutex makes sure that we don't change
1993 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001994 * the device supers. Whoever is writing all supers, should
1995 * lock the device list mutex before getting the number of
1996 * devices in the super block (super_copy). Conversely,
1997 * whoever updates the number of devices in the super block
1998 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001999 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002000
2001 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002003 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002004
Yan Zhenge4404d62008-12-12 10:03:26 -05002005 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04002006 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002007
Anand Jaine6e674b2017-12-04 12:54:54 +08002008 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Miao Xie3a7d55c2014-07-16 18:38:01 +08002009 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002010
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002011 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002012
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002013 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002014 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002015 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002016 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002017 }
Anand Jain99994cd2014-06-03 11:36:00 +08002018
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002019 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2020 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2021 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002022
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002023 /*
2024 * at this point, the device is zero sized and detached from
2025 * the devices list. All that's left is to zero out the old
2026 * supers and free the device.
2027 */
Anand Jainebbede42017-12-04 12:54:52 +08002028 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002029 btrfs_scratch_superblocks(device->bdev, device->name->str);
2030
2031 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002032 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002033
Xiao Guangrong1f781602011-04-20 10:09:16 +00002034 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002035 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002036 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002037 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002038 if (fs_devices->seed == cur_devices) {
2039 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002040 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002041 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002042 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002043 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002044 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00002045 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002046 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002047 }
2048
Chris Masona061fc82008-05-07 11:43:44 -04002049out:
2050 mutex_unlock(&uuid_mutex);
Anand Jain2c997382017-11-06 10:28:00 +08002051 mutex_unlock(&fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002052 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002053
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002054error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002055 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002056 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002057 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002058 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002059 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002060 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002061 }
Anand Jain24fc5722016-02-13 10:01:36 +08002062 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002063}
2064
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002065void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2066 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002067{
Anand Jaind51908c2014-08-13 14:24:19 +08002068 struct btrfs_fs_devices *fs_devices;
2069
Stefan Behrense93c89c2012-11-05 17:33:06 +01002070 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03002071
Anand Jain25e8e912014-08-20 10:56:56 +08002072 /*
2073 * in case of fs with no seed, srcdev->fs_devices will point
2074 * to fs_devices of fs_info. However when the dev being replaced is
2075 * a seed dev it will point to the seed's local fs_devices. In short
2076 * srcdev will have its correct fs_devices in both the cases.
2077 */
2078 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002079
Stefan Behrense93c89c2012-11-05 17:33:06 +01002080 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002081 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002082 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002083 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002084 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002085
Anand Jainebbede42017-12-04 12:54:52 +08002086 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002087 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002088
Miao Xie82372bc2014-09-03 21:35:44 +08002089 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002090 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002091}
2092
2093void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2094 struct btrfs_device *srcdev)
2095{
2096 struct btrfs_fs_devices *fs_devices = srcdev->fs_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)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002099 /* zero out the old super if it is writable */
2100 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2101 }
Anand Jain14238812016-07-22 06:04:53 +08002102
2103 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002104 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002105
Anand Jain94d5f0c2014-08-13 14:24:22 +08002106 /* if this is no devs we rather delete the fs_devices */
2107 if (!fs_devices->num_devices) {
2108 struct btrfs_fs_devices *tmp_fs_devices;
2109
Anand Jain6dd38f82017-10-17 06:53:50 +08002110 /*
2111 * On a mounted FS, num_devices can't be zero unless it's a
2112 * seed. In case of a seed device being replaced, the replace
2113 * target added to the sprout FS, so there will be no more
2114 * device left under the seed FS.
2115 */
2116 ASSERT(fs_devices->seeding);
2117
Anand Jain94d5f0c2014-08-13 14:24:22 +08002118 tmp_fs_devices = fs_info->fs_devices;
2119 while (tmp_fs_devices) {
2120 if (tmp_fs_devices->seed == fs_devices) {
2121 tmp_fs_devices->seed = fs_devices->seed;
2122 break;
2123 }
2124 tmp_fs_devices = tmp_fs_devices->seed;
2125 }
2126 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002127 __btrfs_close_devices(fs_devices);
2128 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002129 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002130}
2131
2132void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2133 struct btrfs_device *tgtdev)
2134{
Miao Xie67a2c452014-09-03 21:35:43 +08002135 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002136 WARN_ON(!tgtdev);
2137 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002138
Anand Jain32576042015-08-14 18:32:49 +08002139 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002140
Anand Jain779bf3fe2016-04-18 16:51:23 +08002141 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002142 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002143
Stefan Behrense93c89c2012-11-05 17:33:06 +01002144 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002145
Anand Jain88acff62016-05-03 17:44:43 +08002146 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002147
Stefan Behrense93c89c2012-11-05 17:33:06 +01002148 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002149
2150 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002151 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002152
2153 /*
2154 * The update_dev_time() with in btrfs_scratch_superblocks()
2155 * may lead to a call to btrfs_show_devname() which will try
2156 * to hold device_list_mutex. And here this device
2157 * is already out of device list, so we don't have to hold
2158 * the device_list_mutex lock.
2159 */
2160 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002161
2162 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002163 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002164}
2165
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002166static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002167 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002168 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002169{
2170 int ret = 0;
2171 struct btrfs_super_block *disk_super;
2172 u64 devid;
2173 u8 *dev_uuid;
2174 struct block_device *bdev;
2175 struct buffer_head *bh;
2176
2177 *device = NULL;
2178 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002179 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002180 if (ret)
2181 return ret;
2182 disk_super = (struct btrfs_super_block *)bh->b_data;
2183 devid = btrfs_stack_device_id(&disk_super->dev_item);
2184 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002185 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002186 brelse(bh);
2187 if (!*device)
2188 ret = -ENOENT;
2189 blkdev_put(bdev, FMODE_READ);
2190 return ret;
2191}
2192
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002193int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002194 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002195 struct btrfs_device **device)
2196{
2197 *device = NULL;
2198 if (strcmp(device_path, "missing") == 0) {
2199 struct list_head *devices;
2200 struct btrfs_device *tmp;
2201
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002202 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002203 /*
2204 * It is safe to read the devices since the volume_mutex
2205 * is held by the caller.
2206 */
2207 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002208 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2209 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002210 *device = tmp;
2211 break;
2212 }
2213 }
2214
Anand Jaind74a6252015-08-14 18:32:56 +08002215 if (!*device)
2216 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002217
2218 return 0;
2219 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002220 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002221 }
2222}
2223
Yan Zheng2b820322008-11-17 21:11:30 -05002224/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002225 * Lookup a device given by device id, or the path if the id is 0.
2226 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002227int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002228 const char *devpath,
2229 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002230{
2231 int ret;
2232
David Sterba5c5c0df2016-02-15 16:39:55 +01002233 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002234 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002235 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002236 if (!*device)
2237 ret = -ENOENT;
2238 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002239 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002240 return -EINVAL;
2241
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002242 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002243 device);
2244 }
2245 return ret;
2246}
2247
Yan Zheng2b820322008-11-17 21:11:30 -05002248/*
2249 * does all the dirty work required for changing file system's UUID.
2250 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002251static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002252{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002253 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002254 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002255 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002256 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002257 struct btrfs_device *device;
2258 u64 super_flags;
2259
2260 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002261 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002262 return -EINVAL;
2263
David Sterba2dfeca92017-06-14 02:48:07 +02002264 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002265 if (IS_ERR(seed_devices))
2266 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002267
Yan Zhenge4404d62008-12-12 10:03:26 -05002268 old_devices = clone_fs_devices(fs_devices);
2269 if (IS_ERR(old_devices)) {
2270 kfree(seed_devices);
2271 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002272 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002273
Yan Zheng2b820322008-11-17 21:11:30 -05002274 list_add(&old_devices->list, &fs_uuids);
2275
Yan Zhenge4404d62008-12-12 10:03:26 -05002276 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2277 seed_devices->opened = 1;
2278 INIT_LIST_HEAD(&seed_devices->devices);
2279 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002280 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002281
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002282 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002283 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2284 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002285 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002286 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002287
David Sterba34441362016-10-04 19:34:27 +02002288 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002289 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002290 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002291
Yan Zheng2b820322008-11-17 21:11:30 -05002292 fs_devices->seeding = 0;
2293 fs_devices->num_devices = 0;
2294 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002295 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002296 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002297 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002298
2299 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002300 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002301 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002302 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002303
Yan Zheng2b820322008-11-17 21:11:30 -05002304 super_flags = btrfs_super_flags(disk_super) &
2305 ~BTRFS_SUPER_FLAG_SEEDING;
2306 btrfs_set_super_flags(disk_super, super_flags);
2307
2308 return 0;
2309}
2310
2311/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002312 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002313 */
2314static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002315 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002316{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002317 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002318 struct btrfs_path *path;
2319 struct extent_buffer *leaf;
2320 struct btrfs_dev_item *dev_item;
2321 struct btrfs_device *device;
2322 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002323 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002324 u8 dev_uuid[BTRFS_UUID_SIZE];
2325 u64 devid;
2326 int ret;
2327
2328 path = btrfs_alloc_path();
2329 if (!path)
2330 return -ENOMEM;
2331
Yan Zheng2b820322008-11-17 21:11:30 -05002332 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2333 key.offset = 0;
2334 key.type = BTRFS_DEV_ITEM_KEY;
2335
2336 while (1) {
2337 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2338 if (ret < 0)
2339 goto error;
2340
2341 leaf = path->nodes[0];
2342next_slot:
2343 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2344 ret = btrfs_next_leaf(root, path);
2345 if (ret > 0)
2346 break;
2347 if (ret < 0)
2348 goto error;
2349 leaf = path->nodes[0];
2350 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002351 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002352 continue;
2353 }
2354
2355 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2356 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2357 key.type != BTRFS_DEV_ITEM_KEY)
2358 break;
2359
2360 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2361 struct btrfs_dev_item);
2362 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002363 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002364 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002365 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002366 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002367 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002368 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002369
2370 if (device->fs_devices->seeding) {
2371 btrfs_set_device_generation(leaf, dev_item,
2372 device->generation);
2373 btrfs_mark_buffer_dirty(leaf);
2374 }
2375
2376 path->slots[0]++;
2377 goto next_slot;
2378 }
2379 ret = 0;
2380error:
2381 btrfs_free_path(path);
2382 return ret;
2383}
2384
David Sterbada353f62017-02-14 17:55:53 +01002385int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002386{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002387 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002388 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002389 struct btrfs_trans_handle *trans;
2390 struct btrfs_device *device;
2391 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002392 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002393 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002394 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002395 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002396 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002397 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002398 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002399
David Howellsbc98a422017-07-17 08:45:34 +01002400 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002401 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002402
Li Zefana5d16332011-12-07 20:08:40 -05002403 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002404 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002405 if (IS_ERR(bdev))
2406 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002408 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002409 seeding_dev = 1;
2410 down_write(&sb->s_umount);
2411 mutex_lock(&uuid_mutex);
2412 }
2413
Chris Mason8c8bee12008-09-29 11:19:10 -04002414 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002415
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002416 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002418 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002419 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002420 if (device->bdev == bdev) {
2421 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002422 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002424 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002425 }
2426 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002427 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002428
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002429 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002430 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002431 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002432 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002433 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002434 }
2435
David Sterba78f2c9e2016-02-11 14:25:38 +01002436 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002437 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002438 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002439 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002440 }
Josef Bacik606686e2012-06-04 14:03:51 -04002441 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002442
Yan, Zhenga22285a2010-05-16 10:48:46 -04002443 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002444 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002445 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002446 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002447 }
2448
Josef Bacikd5e20032011-08-04 14:52:27 +00002449 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002450 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002451 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002452 device->io_width = fs_info->sectorsize;
2453 device->io_align = fs_info->sectorsize;
2454 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002455 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2456 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002457 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002458 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002459 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002460 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002461 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002462 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002463 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002464 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002465 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002466
Yan Zheng2b820322008-11-17 21:11:30 -05002467 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002468 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002469 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002470 if (ret) {
2471 btrfs_abort_transaction(trans, ret);
2472 goto error_trans;
2473 }
Yan Zheng2b820322008-11-17 21:11:30 -05002474 }
2475
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002476 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002477
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002478 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002479 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002480 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002481 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002482 &fs_info->fs_devices->alloc_list);
2483 fs_info->fs_devices->num_devices++;
2484 fs_info->fs_devices->open_devices++;
2485 fs_info->fs_devices->rw_devices++;
2486 fs_info->fs_devices->total_devices++;
2487 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002488
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002489 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002490
Anand Jaine884f4f2017-04-04 18:40:19 +08002491 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002492 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002493
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2495 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002496 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002497
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002498 tmp = btrfs_super_num_devices(fs_info->super_copy);
2499 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002500
2501 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002503
Miao Xie2196d6e2014-09-03 21:35:41 +08002504 /*
2505 * we've got more storage, clear any full flags on the space
2506 * infos
2507 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002508 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002509
David Sterba34441362016-10-04 19:34:27 +02002510 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002511 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002512
Yan Zheng2b820322008-11-17 21:11:30 -05002513 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002514 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002515 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002516 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002517 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002518 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002519 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002520 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002521 }
2522
Anand Jainc74a0b02017-11-06 16:36:15 +08002523 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002524 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002525 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002526 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002527 }
2528
2529 if (seeding_dev) {
2530 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2531
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002532 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002533 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002534 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002535 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002536 }
Anand Jainb2373f22014-06-03 11:36:03 +08002537
2538 /* Sprouting would change fsid of the mounted root,
2539 * so rename the fsid on the sysfs
2540 */
2541 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002542 fs_info->fsid);
2543 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2544 btrfs_warn(fs_info,
2545 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002546 }
2547
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002548 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002549
2550 if (seeding_dev) {
2551 mutex_unlock(&uuid_mutex);
2552 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002553 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002554
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002555 if (ret) /* transaction commit */
2556 return ret;
2557
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002558 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002560 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002561 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002562 trans = btrfs_attach_transaction(root);
2563 if (IS_ERR(trans)) {
2564 if (PTR_ERR(trans) == -ENOENT)
2565 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002566 ret = PTR_ERR(trans);
2567 trans = NULL;
2568 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002569 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002570 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002571 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002572
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002573 /* Update ctime/mtime for libblkid */
2574 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002575 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002576
Anand Jaind31c32f2017-09-28 14:51:10 +08002577error_sysfs:
2578 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002579error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002580 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002581 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002582 if (trans)
2583 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002584error_free_device:
David Sterba55de4802017-10-30 18:55:47 +01002585 free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002586error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002587 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002588 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002589 mutex_unlock(&uuid_mutex);
2590 up_write(&sb->s_umount);
2591 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002592 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002593}
2594
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002595int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002596 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002597 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002598 struct btrfs_device **device_out)
2599{
Stefan Behrense93c89c2012-11-05 17:33:06 +01002600 struct btrfs_device *device;
2601 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002602 struct list_head *devices;
2603 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002604 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002605 int ret = 0;
2606
2607 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002608 if (fs_info->fs_devices->seeding) {
2609 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002610 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002611 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002612
2613 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2614 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002615 if (IS_ERR(bdev)) {
2616 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002617 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002618 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002619
2620 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2621
2622 devices = &fs_info->fs_devices->devices;
2623 list_for_each_entry(device, devices, dev_list) {
2624 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002625 btrfs_err(fs_info,
2626 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002627 ret = -EEXIST;
2628 goto error;
2629 }
2630 }
2631
Miao Xie1c433662014-09-03 21:35:32 +08002632
Miao Xie7cc8e582014-09-03 21:35:38 +08002633 if (i_size_read(bdev->bd_inode) <
2634 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002635 btrfs_err(fs_info,
2636 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002637 ret = -EINVAL;
2638 goto error;
2639 }
2640
2641
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002642 device = btrfs_alloc_device(NULL, &devid, NULL);
2643 if (IS_ERR(device)) {
2644 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002645 goto error;
2646 }
2647
David Sterba61655722017-06-15 17:16:43 +02002648 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002649 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +01002650 free_device(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002651 ret = -ENOMEM;
2652 goto error;
2653 }
2654 rcu_assign_pointer(device->name, name);
2655
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002656 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jainebbede42017-12-04 12:54:52 +08002657 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002658 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002659 device->io_width = fs_info->sectorsize;
2660 device->io_align = fs_info->sectorsize;
2661 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002662 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2663 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2664 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002665 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002666 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002667 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002668 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002669 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002670 set_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002671 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002672 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002673 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002674 device->fs_devices = fs_info->fs_devices;
2675 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2676 fs_info->fs_devices->num_devices++;
2677 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002678 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002679
2680 *device_out = device;
2681 return ret;
2682
2683error:
2684 blkdev_put(bdev, FMODE_EXCL);
2685 return ret;
2686}
2687
Chris Masond3977122009-01-05 21:25:51 -05002688static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2689 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002690{
2691 int ret;
2692 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002693 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002694 struct btrfs_dev_item *dev_item;
2695 struct extent_buffer *leaf;
2696 struct btrfs_key key;
2697
Chris Mason0b86a832008-03-24 15:01:56 -04002698 path = btrfs_alloc_path();
2699 if (!path)
2700 return -ENOMEM;
2701
2702 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2703 key.type = BTRFS_DEV_ITEM_KEY;
2704 key.offset = device->devid;
2705
2706 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2707 if (ret < 0)
2708 goto out;
2709
2710 if (ret > 0) {
2711 ret = -ENOENT;
2712 goto out;
2713 }
2714
2715 leaf = path->nodes[0];
2716 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2717
2718 btrfs_set_device_id(leaf, dev_item, device->devid);
2719 btrfs_set_device_type(leaf, dev_item, device->type);
2720 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2721 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2722 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002723 btrfs_set_device_total_bytes(leaf, dev_item,
2724 btrfs_device_get_disk_total_bytes(device));
2725 btrfs_set_device_bytes_used(leaf, dev_item,
2726 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002727 btrfs_mark_buffer_dirty(leaf);
2728
2729out:
2730 btrfs_free_path(path);
2731 return ret;
2732}
2733
Miao Xie2196d6e2014-09-03 21:35:41 +08002734int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002735 struct btrfs_device *device, u64 new_size)
2736{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002737 struct btrfs_fs_info *fs_info = device->fs_info;
2738 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002739 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002740 u64 old_total;
2741 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002742
Anand Jainebbede42017-12-04 12:54:52 +08002743 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002744 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002745
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002746 new_size = round_down(new_size, fs_info->sectorsize);
2747
David Sterba34441362016-10-04 19:34:27 +02002748 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002749 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002750 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002751
Stefan Behrens63a212a2012-11-05 18:29:28 +01002752 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002753 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002754 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002755 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002756 }
Yan Zheng2b820322008-11-17 21:11:30 -05002757
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002758 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002759
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002760 btrfs_set_super_total_bytes(super_copy,
2761 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002762 device->fs_devices->total_rw_bytes += diff;
2763
Miao Xie7cc8e582014-09-03 21:35:38 +08002764 btrfs_device_set_total_bytes(device, new_size);
2765 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002766 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002767 if (list_empty(&device->resized_list))
2768 list_add_tail(&device->resized_list,
2769 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002770 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002771
Chris Mason8f18cf12008-04-25 16:53:30 -04002772 return btrfs_update_device(trans, device);
2773}
2774
2775static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002776 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002777{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002778 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002779 int ret;
2780 struct btrfs_path *path;
2781 struct btrfs_key key;
2782
Chris Mason8f18cf12008-04-25 16:53:30 -04002783 path = btrfs_alloc_path();
2784 if (!path)
2785 return -ENOMEM;
2786
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002787 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002788 key.offset = chunk_offset;
2789 key.type = BTRFS_CHUNK_ITEM_KEY;
2790
2791 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002792 if (ret < 0)
2793 goto out;
2794 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002795 btrfs_handle_fs_error(fs_info, -ENOENT,
2796 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002797 ret = -ENOENT;
2798 goto out;
2799 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002800
2801 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002802 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002803 btrfs_handle_fs_error(fs_info, ret,
2804 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002805out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002806 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002807 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002808}
2809
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002810static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002811{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002812 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002813 struct btrfs_disk_key *disk_key;
2814 struct btrfs_chunk *chunk;
2815 u8 *ptr;
2816 int ret = 0;
2817 u32 num_stripes;
2818 u32 array_size;
2819 u32 len = 0;
2820 u32 cur;
2821 struct btrfs_key key;
2822
David Sterba34441362016-10-04 19:34:27 +02002823 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002824 array_size = btrfs_super_sys_array_size(super_copy);
2825
2826 ptr = super_copy->sys_chunk_array;
2827 cur = 0;
2828
2829 while (cur < array_size) {
2830 disk_key = (struct btrfs_disk_key *)ptr;
2831 btrfs_disk_key_to_cpu(&key, disk_key);
2832
2833 len = sizeof(*disk_key);
2834
2835 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2836 chunk = (struct btrfs_chunk *)(ptr + len);
2837 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2838 len += btrfs_chunk_item_size(num_stripes);
2839 } else {
2840 ret = -EIO;
2841 break;
2842 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002843 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002844 key.offset == chunk_offset) {
2845 memmove(ptr, ptr + len, array_size - (cur + len));
2846 array_size -= len;
2847 btrfs_set_super_sys_array_size(super_copy, array_size);
2848 } else {
2849 ptr += len;
2850 cur += len;
2851 }
2852 }
David Sterba34441362016-10-04 19:34:27 +02002853 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002854 return ret;
2855}
2856
Liu Bo592d92e2017-03-14 13:33:55 -07002857static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2858 u64 logical, u64 length)
2859{
2860 struct extent_map_tree *em_tree;
2861 struct extent_map *em;
2862
2863 em_tree = &fs_info->mapping_tree.map_tree;
2864 read_lock(&em_tree->lock);
2865 em = lookup_extent_mapping(em_tree, logical, length);
2866 read_unlock(&em_tree->lock);
2867
2868 if (!em) {
2869 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2870 logical, length);
2871 return ERR_PTR(-EINVAL);
2872 }
2873
2874 if (em->start > logical || em->start + em->len < logical) {
2875 btrfs_crit(fs_info,
2876 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2877 logical, length, em->start, em->start + em->len);
2878 free_extent_map(em);
2879 return ERR_PTR(-EINVAL);
2880 }
2881
2882 /* callers are responsible for dropping em's ref. */
2883 return em;
2884}
2885
Josef Bacik47ab2a62014-09-18 11:20:02 -04002886int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002887 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002889 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002890 struct map_lookup *map;
2891 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002892 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002893 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894
Liu Bo592d92e2017-03-14 13:33:55 -07002895 em = get_chunk_map(fs_info, chunk_offset, 1);
2896 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002897 /*
2898 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002899 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002900 * do anything we still error out.
2901 */
2902 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002903 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002904 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002905 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002906 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002907 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002908 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002910 /*
2911 * Take the device list mutex to prevent races with the final phase of
2912 * a device replace operation that replaces the device object associated
2913 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2914 */
2915 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002916 for (i = 0; i < map->num_stripes; i++) {
2917 struct btrfs_device *device = map->stripes[i].dev;
2918 ret = btrfs_free_dev_extent(trans, device,
2919 map->stripes[i].physical,
2920 &dev_extent_len);
2921 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002922 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002923 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002924 goto out;
2925 }
2926
2927 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002928 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002929 btrfs_device_set_bytes_used(device,
2930 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002931 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002932 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002933 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002934 }
2935
2936 if (map->stripes[i].dev) {
2937 ret = btrfs_update_device(trans, map->stripes[i].dev);
2938 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002939 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002940 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002941 goto out;
2942 }
2943 }
2944 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002945 mutex_unlock(&fs_devices->device_list_mutex);
2946
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002947 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002948 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002949 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002950 goto out;
2951 }
2952
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002953 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002954
2955 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002956 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002957 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002958 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002959 goto out;
2960 }
2961 }
2962
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002963 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002964 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002965 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002966 goto out;
2967 }
2968
Josef Bacik47ab2a62014-09-18 11:20:02 -04002969out:
2970 /* once for us */
2971 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002972 return ret;
2973}
2974
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002975static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002976{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002977 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002978 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002979 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002980
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002981 /*
2982 * Prevent races with automatic removal of unused block groups.
2983 * After we relocate and before we remove the chunk with offset
2984 * chunk_offset, automatic removal of the block group can kick in,
2985 * resulting in a failure when calling btrfs_remove_chunk() below.
2986 *
2987 * Make sure to acquire this mutex before doing a tree search (dev
2988 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2989 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2990 * we release the path used to search the chunk/dev tree and before
2991 * the current task acquires this mutex and calls us.
2992 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002993 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002994
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002995 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002996 if (ret)
2997 return -ENOSPC;
2998
Chris Mason8f18cf12008-04-25 16:53:30 -04002999 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003000 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003002 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003003 if (ret)
3004 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003005
Chris Mason19c4d2f2016-10-10 13:43:31 -07003006 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3007 chunk_offset);
3008 if (IS_ERR(trans)) {
3009 ret = PTR_ERR(trans);
3010 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3011 return ret;
3012 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003013
Chris Mason19c4d2f2016-10-10 13:43:31 -07003014 /*
3015 * step two, delete the device extents and the
3016 * chunk tree entries
3017 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003018 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003019 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003020 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003021}
3022
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003023static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003024{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003025 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003026 struct btrfs_path *path;
3027 struct extent_buffer *leaf;
3028 struct btrfs_chunk *chunk;
3029 struct btrfs_key key;
3030 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003031 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003032 bool retried = false;
3033 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003034 int ret;
3035
3036 path = btrfs_alloc_path();
3037 if (!path)
3038 return -ENOMEM;
3039
Josef Bacikba1bf482009-09-11 16:11:19 -04003040again:
Yan Zheng2b820322008-11-17 21:11:30 -05003041 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3042 key.offset = (u64)-1;
3043 key.type = BTRFS_CHUNK_ITEM_KEY;
3044
3045 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003046 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003047 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003048 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003049 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003050 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003051 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003052 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003053
3054 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3055 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003056 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003057 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003058 if (ret < 0)
3059 goto error;
3060 if (ret > 0)
3061 break;
3062
3063 leaf = path->nodes[0];
3064 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3065
3066 chunk = btrfs_item_ptr(leaf, path->slots[0],
3067 struct btrfs_chunk);
3068 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003069 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003070
3071 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003073 if (ret == -ENOSPC)
3074 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303075 else
3076 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003077 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003078 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003079
3080 if (found_key.offset == 0)
3081 break;
3082 key.offset = found_key.offset - 1;
3083 }
3084 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003085 if (failed && !retried) {
3086 failed = 0;
3087 retried = true;
3088 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303089 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003090 ret = -ENOSPC;
3091 }
Yan Zheng2b820322008-11-17 21:11:30 -05003092error:
3093 btrfs_free_path(path);
3094 return ret;
3095}
3096
Liu Boa6f93c72017-11-15 16:28:11 -07003097/*
3098 * return 1 : allocate a data chunk successfully,
3099 * return <0: errors during allocating a data chunk,
3100 * return 0 : no need to allocate a data chunk.
3101 */
3102static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3103 u64 chunk_offset)
3104{
3105 struct btrfs_block_group_cache *cache;
3106 u64 bytes_used;
3107 u64 chunk_type;
3108
3109 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3110 ASSERT(cache);
3111 chunk_type = cache->flags;
3112 btrfs_put_block_group(cache);
3113
3114 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3115 spin_lock(&fs_info->data_sinfo->lock);
3116 bytes_used = fs_info->data_sinfo->bytes_used;
3117 spin_unlock(&fs_info->data_sinfo->lock);
3118
3119 if (!bytes_used) {
3120 struct btrfs_trans_handle *trans;
3121 int ret;
3122
3123 trans = btrfs_join_transaction(fs_info->tree_root);
3124 if (IS_ERR(trans))
3125 return PTR_ERR(trans);
3126
3127 ret = btrfs_force_chunk_alloc(trans, fs_info,
3128 BTRFS_BLOCK_GROUP_DATA);
3129 btrfs_end_transaction(trans);
3130 if (ret < 0)
3131 return ret;
3132
3133 return 1;
3134 }
3135 }
3136 return 0;
3137}
3138
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003139static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003140 struct btrfs_balance_control *bctl)
3141{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003142 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003143 struct btrfs_trans_handle *trans;
3144 struct btrfs_balance_item *item;
3145 struct btrfs_disk_balance_args disk_bargs;
3146 struct btrfs_path *path;
3147 struct extent_buffer *leaf;
3148 struct btrfs_key key;
3149 int ret, err;
3150
3151 path = btrfs_alloc_path();
3152 if (!path)
3153 return -ENOMEM;
3154
3155 trans = btrfs_start_transaction(root, 0);
3156 if (IS_ERR(trans)) {
3157 btrfs_free_path(path);
3158 return PTR_ERR(trans);
3159 }
3160
3161 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003162 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003163 key.offset = 0;
3164
3165 ret = btrfs_insert_empty_item(trans, root, path, &key,
3166 sizeof(*item));
3167 if (ret)
3168 goto out;
3169
3170 leaf = path->nodes[0];
3171 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3172
David Sterbab159fa22016-11-08 18:09:03 +01003173 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003174
3175 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3176 btrfs_set_balance_data(leaf, item, &disk_bargs);
3177 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3178 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3179 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3180 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3181
3182 btrfs_set_balance_flags(leaf, item, bctl->flags);
3183
3184 btrfs_mark_buffer_dirty(leaf);
3185out:
3186 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003187 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003188 if (err && !ret)
3189 ret = err;
3190 return ret;
3191}
3192
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003193static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003194{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003195 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003196 struct btrfs_trans_handle *trans;
3197 struct btrfs_path *path;
3198 struct btrfs_key key;
3199 int ret, err;
3200
3201 path = btrfs_alloc_path();
3202 if (!path)
3203 return -ENOMEM;
3204
3205 trans = btrfs_start_transaction(root, 0);
3206 if (IS_ERR(trans)) {
3207 btrfs_free_path(path);
3208 return PTR_ERR(trans);
3209 }
3210
3211 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003212 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003213 key.offset = 0;
3214
3215 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3216 if (ret < 0)
3217 goto out;
3218 if (ret > 0) {
3219 ret = -ENOENT;
3220 goto out;
3221 }
3222
3223 ret = btrfs_del_item(trans, root, path);
3224out:
3225 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003226 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003227 if (err && !ret)
3228 ret = err;
3229 return ret;
3230}
3231
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003232/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003233 * This is a heuristic used to reduce the number of chunks balanced on
3234 * resume after balance was interrupted.
3235 */
3236static void update_balance_args(struct btrfs_balance_control *bctl)
3237{
3238 /*
3239 * Turn on soft mode for chunk types that were being converted.
3240 */
3241 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3242 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3243 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3244 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3245 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3246 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3247
3248 /*
3249 * Turn on usage filter if is not already used. The idea is
3250 * that chunks that we have already balanced should be
3251 * reasonably full. Don't do it for chunks that are being
3252 * converted - that will keep us from relocating unconverted
3253 * (albeit full) chunks.
3254 */
3255 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003256 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003257 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3258 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3259 bctl->data.usage = 90;
3260 }
3261 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003262 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003263 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3264 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3265 bctl->sys.usage = 90;
3266 }
3267 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003268 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003269 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3270 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3271 bctl->meta.usage = 90;
3272 }
3273}
3274
3275/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003276 * Should be called with both balance and volume mutexes held to
3277 * serialize other volume operations (add_dev/rm_dev/resize) with
3278 * restriper. Same goes for unset_balance_control.
3279 */
3280static void set_balance_control(struct btrfs_balance_control *bctl)
3281{
3282 struct btrfs_fs_info *fs_info = bctl->fs_info;
3283
3284 BUG_ON(fs_info->balance_ctl);
3285
3286 spin_lock(&fs_info->balance_lock);
3287 fs_info->balance_ctl = bctl;
3288 spin_unlock(&fs_info->balance_lock);
3289}
3290
3291static void unset_balance_control(struct btrfs_fs_info *fs_info)
3292{
3293 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3294
3295 BUG_ON(!fs_info->balance_ctl);
3296
3297 spin_lock(&fs_info->balance_lock);
3298 fs_info->balance_ctl = NULL;
3299 spin_unlock(&fs_info->balance_lock);
3300
3301 kfree(bctl);
3302}
3303
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003304/*
3305 * Balance filters. Return 1 if chunk should be filtered out
3306 * (should not be balanced).
3307 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003308static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003309 struct btrfs_balance_args *bargs)
3310{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003311 chunk_type = chunk_to_extended(chunk_type) &
3312 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003313
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003314 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003315 return 0;
3316
3317 return 1;
3318}
3319
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003320static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003321 struct btrfs_balance_args *bargs)
3322{
3323 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003324 u64 chunk_used;
3325 u64 user_thresh_min;
3326 u64 user_thresh_max;
3327 int ret = 1;
3328
3329 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3330 chunk_used = btrfs_block_group_used(&cache->item);
3331
3332 if (bargs->usage_min == 0)
3333 user_thresh_min = 0;
3334 else
3335 user_thresh_min = div_factor_fine(cache->key.offset,
3336 bargs->usage_min);
3337
3338 if (bargs->usage_max == 0)
3339 user_thresh_max = 1;
3340 else if (bargs->usage_max > 100)
3341 user_thresh_max = cache->key.offset;
3342 else
3343 user_thresh_max = div_factor_fine(cache->key.offset,
3344 bargs->usage_max);
3345
3346 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3347 ret = 0;
3348
3349 btrfs_put_block_group(cache);
3350 return ret;
3351}
3352
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003353static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003354 u64 chunk_offset, struct btrfs_balance_args *bargs)
3355{
3356 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003357 u64 chunk_used, user_thresh;
3358 int ret = 1;
3359
3360 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3361 chunk_used = btrfs_block_group_used(&cache->item);
3362
David Sterbabc309462015-10-20 18:22:13 +02003363 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003364 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003365 else if (bargs->usage > 100)
3366 user_thresh = cache->key.offset;
3367 else
3368 user_thresh = div_factor_fine(cache->key.offset,
3369 bargs->usage);
3370
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003371 if (chunk_used < user_thresh)
3372 ret = 0;
3373
3374 btrfs_put_block_group(cache);
3375 return ret;
3376}
3377
Ilya Dryomov409d4042012-01-16 22:04:47 +02003378static int chunk_devid_filter(struct extent_buffer *leaf,
3379 struct btrfs_chunk *chunk,
3380 struct btrfs_balance_args *bargs)
3381{
3382 struct btrfs_stripe *stripe;
3383 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3384 int i;
3385
3386 for (i = 0; i < num_stripes; i++) {
3387 stripe = btrfs_stripe_nr(chunk, i);
3388 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3389 return 0;
3390 }
3391
3392 return 1;
3393}
3394
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003395/* [pstart, pend) */
3396static int chunk_drange_filter(struct extent_buffer *leaf,
3397 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003398 struct btrfs_balance_args *bargs)
3399{
3400 struct btrfs_stripe *stripe;
3401 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3402 u64 stripe_offset;
3403 u64 stripe_length;
3404 int factor;
3405 int i;
3406
3407 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3408 return 0;
3409
3410 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003411 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3412 factor = num_stripes / 2;
3413 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3414 factor = num_stripes - 1;
3415 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3416 factor = num_stripes - 2;
3417 } else {
3418 factor = num_stripes;
3419 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003420
3421 for (i = 0; i < num_stripes; i++) {
3422 stripe = btrfs_stripe_nr(chunk, i);
3423 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3424 continue;
3425
3426 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3427 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003428 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003429
3430 if (stripe_offset < bargs->pend &&
3431 stripe_offset + stripe_length > bargs->pstart)
3432 return 0;
3433 }
3434
3435 return 1;
3436}
3437
Ilya Dryomovea671762012-01-16 22:04:48 +02003438/* [vstart, vend) */
3439static int chunk_vrange_filter(struct extent_buffer *leaf,
3440 struct btrfs_chunk *chunk,
3441 u64 chunk_offset,
3442 struct btrfs_balance_args *bargs)
3443{
3444 if (chunk_offset < bargs->vend &&
3445 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3446 /* at least part of the chunk is inside this vrange */
3447 return 0;
3448
3449 return 1;
3450}
3451
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003452static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3453 struct btrfs_chunk *chunk,
3454 struct btrfs_balance_args *bargs)
3455{
3456 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3457
3458 if (bargs->stripes_min <= num_stripes
3459 && num_stripes <= bargs->stripes_max)
3460 return 0;
3461
3462 return 1;
3463}
3464
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003465static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003466 struct btrfs_balance_args *bargs)
3467{
3468 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3469 return 0;
3470
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003471 chunk_type = chunk_to_extended(chunk_type) &
3472 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003473
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003474 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003475 return 1;
3476
3477 return 0;
3478}
3479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003480static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003481 struct extent_buffer *leaf,
3482 struct btrfs_chunk *chunk, u64 chunk_offset)
3483{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003484 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003485 struct btrfs_balance_args *bargs = NULL;
3486 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3487
3488 /* type filter */
3489 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3490 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3491 return 0;
3492 }
3493
3494 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3495 bargs = &bctl->data;
3496 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3497 bargs = &bctl->sys;
3498 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3499 bargs = &bctl->meta;
3500
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003501 /* profiles filter */
3502 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3503 chunk_profiles_filter(chunk_type, bargs)) {
3504 return 0;
3505 }
3506
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003507 /* usage filter */
3508 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003509 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003510 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003511 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003512 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003513 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003514 }
3515
Ilya Dryomov409d4042012-01-16 22:04:47 +02003516 /* devid filter */
3517 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3518 chunk_devid_filter(leaf, chunk, bargs)) {
3519 return 0;
3520 }
3521
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003522 /* drange filter, makes sense only with devid filter */
3523 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003524 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003525 return 0;
3526 }
3527
Ilya Dryomovea671762012-01-16 22:04:48 +02003528 /* vrange filter */
3529 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3530 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3531 return 0;
3532 }
3533
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003534 /* stripes filter */
3535 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3536 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3537 return 0;
3538 }
3539
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003540 /* soft profile changing mode */
3541 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3542 chunk_soft_convert_filter(chunk_type, bargs)) {
3543 return 0;
3544 }
3545
David Sterba7d824b62014-05-07 17:37:51 +02003546 /*
3547 * limited by count, must be the last filter
3548 */
3549 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3550 if (bargs->limit == 0)
3551 return 0;
3552 else
3553 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003554 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3555 /*
3556 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003557 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003558 * about the count of all chunks that satisfy the filters.
3559 */
3560 if (bargs->limit_max == 0)
3561 return 0;
3562 else
3563 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003564 }
3565
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003566 return 1;
3567}
3568
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003569static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003570{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003571 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003572 struct btrfs_root *chunk_root = fs_info->chunk_root;
3573 struct btrfs_root *dev_root = fs_info->dev_root;
3574 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003575 struct btrfs_device *device;
3576 u64 old_size;
3577 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003578 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003579 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003580 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003581 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003582 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003583 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003584 struct extent_buffer *leaf;
3585 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003586 int ret;
3587 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003588 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003589 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003590 u64 limit_data = bctl->data.limit;
3591 u64 limit_meta = bctl->meta.limit;
3592 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003593 u32 count_data = 0;
3594 u32 count_meta = 0;
3595 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003596 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003597
Chris Masonec44a352008-04-28 15:29:52 -04003598 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003599 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003600 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003601 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003602 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003603 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003604 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003605 btrfs_device_get_total_bytes(device) -
3606 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003607 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003608 continue;
3609
3610 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003611 if (ret == -ENOSPC)
3612 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003613 if (ret) {
3614 /* btrfs_shrink_device never returns ret > 0 */
3615 WARN_ON(ret > 0);
3616 goto error;
3617 }
Chris Masonec44a352008-04-28 15:29:52 -04003618
Yan, Zhenga22285a2010-05-16 10:48:46 -04003619 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003620 if (IS_ERR(trans)) {
3621 ret = PTR_ERR(trans);
3622 btrfs_info_in_rcu(fs_info,
3623 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3624 rcu_str_deref(device->name), ret,
3625 old_size, old_size - size_to_free);
3626 goto error;
3627 }
Chris Masonec44a352008-04-28 15:29:52 -04003628
3629 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003630 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003631 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003632 /* btrfs_grow_device never returns ret > 0 */
3633 WARN_ON(ret > 0);
3634 btrfs_info_in_rcu(fs_info,
3635 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3636 rcu_str_deref(device->name), ret,
3637 old_size, old_size - size_to_free);
3638 goto error;
3639 }
Chris Masonec44a352008-04-28 15:29:52 -04003640
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003641 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003642 }
3643
3644 /* step two, relocate all the chunks */
3645 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003646 if (!path) {
3647 ret = -ENOMEM;
3648 goto error;
3649 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003650
3651 /* zero out stat counters */
3652 spin_lock(&fs_info->balance_lock);
3653 memset(&bctl->stat, 0, sizeof(bctl->stat));
3654 spin_unlock(&fs_info->balance_lock);
3655again:
David Sterba7d824b62014-05-07 17:37:51 +02003656 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003657 /*
3658 * The single value limit and min/max limits use the same bytes
3659 * in the
3660 */
David Sterba7d824b62014-05-07 17:37:51 +02003661 bctl->data.limit = limit_data;
3662 bctl->meta.limit = limit_meta;
3663 bctl->sys.limit = limit_sys;
3664 }
Chris Masonec44a352008-04-28 15:29:52 -04003665 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3666 key.offset = (u64)-1;
3667 key.type = BTRFS_CHUNK_ITEM_KEY;
3668
Chris Masond3977122009-01-05 21:25:51 -05003669 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003670 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003671 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003672 ret = -ECANCELED;
3673 goto error;
3674 }
3675
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003676 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003677 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003678 if (ret < 0) {
3679 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003680 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003681 }
Chris Masonec44a352008-04-28 15:29:52 -04003682
3683 /*
3684 * this shouldn't happen, it means the last relocate
3685 * failed
3686 */
3687 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003688 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003689
3690 ret = btrfs_previous_item(chunk_root, path, 0,
3691 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003692 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003693 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003694 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003695 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003696 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003697
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003698 leaf = path->nodes[0];
3699 slot = path->slots[0];
3700 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3701
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003702 if (found_key.objectid != key.objectid) {
3703 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003704 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003705 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003706
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003707 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003708 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003709
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003710 if (!counting) {
3711 spin_lock(&fs_info->balance_lock);
3712 bctl->stat.considered++;
3713 spin_unlock(&fs_info->balance_lock);
3714 }
3715
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003716 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003717 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003718
David Sterbab3b4aa72011-04-21 01:20:15 +02003719 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003720 if (!ret) {
3721 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003722 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003723 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003724
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003725 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003726 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003727 spin_lock(&fs_info->balance_lock);
3728 bctl->stat.expected++;
3729 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003730
3731 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3732 count_data++;
3733 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3734 count_sys++;
3735 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3736 count_meta++;
3737
3738 goto loop;
3739 }
3740
3741 /*
3742 * Apply limit_min filter, no need to check if the LIMITS
3743 * filter is used, limit_min is 0 by default
3744 */
3745 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3746 count_data < bctl->data.limit_min)
3747 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3748 count_meta < bctl->meta.limit_min)
3749 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3750 count_sys < bctl->sys.limit_min)) {
3751 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003752 goto loop;
3753 }
3754
Liu Boa6f93c72017-11-15 16:28:11 -07003755 if (!chunk_reserved) {
3756 /*
3757 * We may be relocating the only data chunk we have,
3758 * which could potentially end up with losing data's
3759 * raid profile, so lets allocate an empty one in
3760 * advance.
3761 */
3762 ret = btrfs_may_alloc_data_chunk(fs_info,
3763 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003764 if (ret < 0) {
3765 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3766 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003767 } else if (ret == 1) {
3768 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003769 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003770 }
3771
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003772 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003773 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003774 if (ret && ret != -ENOSPC)
3775 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003776 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003777 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003778 } else {
3779 spin_lock(&fs_info->balance_lock);
3780 bctl->stat.completed++;
3781 spin_unlock(&fs_info->balance_lock);
3782 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003783loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003784 if (found_key.offset == 0)
3785 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003786 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003787 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003788
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003789 if (counting) {
3790 btrfs_release_path(path);
3791 counting = false;
3792 goto again;
3793 }
Chris Masonec44a352008-04-28 15:29:52 -04003794error:
3795 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003796 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003797 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003798 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003799 if (!ret)
3800 ret = -ENOSPC;
3801 }
3802
Chris Masonec44a352008-04-28 15:29:52 -04003803 return ret;
3804}
3805
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003806/**
3807 * alloc_profile_is_valid - see if a given profile is valid and reduced
3808 * @flags: profile to validate
3809 * @extended: if true @flags is treated as an extended profile
3810 */
3811static int alloc_profile_is_valid(u64 flags, int extended)
3812{
3813 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3814 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3815
3816 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3817
3818 /* 1) check that all other bits are zeroed */
3819 if (flags & ~mask)
3820 return 0;
3821
3822 /* 2) see if profile is reduced */
3823 if (flags == 0)
3824 return !extended; /* "0" is valid for usual profiles */
3825
3826 /* true if exactly one bit set */
3827 return (flags & (flags - 1)) == 0;
3828}
3829
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003830static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3831{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003832 /* cancel requested || normal exit path */
3833 return atomic_read(&fs_info->balance_cancel_req) ||
3834 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3835 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003836}
3837
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003838static void __cancel_balance(struct btrfs_fs_info *fs_info)
3839{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003840 int ret;
3841
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003842 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003843 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003844 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003845 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003846
David Sterba171938e2017-03-28 14:44:21 +02003847 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003848}
3849
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003850/* Non-zero return value signifies invalidity */
3851static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3852 u64 allowed)
3853{
3854 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3855 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3856 (bctl_arg->target & ~allowed)));
3857}
3858
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003859/*
3860 * Should be called with both balance and volume mutexes held
3861 */
3862int btrfs_balance(struct btrfs_balance_control *bctl,
3863 struct btrfs_ioctl_balance_args *bargs)
3864{
3865 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003866 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003867 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003868 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003869 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003870 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003871 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003872
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003873 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003874 atomic_read(&fs_info->balance_pause_req) ||
3875 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003876 ret = -EINVAL;
3877 goto out;
3878 }
3879
Ilya Dryomove4837f82012-03-27 17:09:17 +03003880 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3881 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3882 mixed = 1;
3883
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003884 /*
3885 * In case of mixed groups both data and meta should be picked,
3886 * and identical options should be given for both of them.
3887 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003888 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3889 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003890 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3891 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3892 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003893 btrfs_err(fs_info,
3894 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003895 ret = -EINVAL;
3896 goto out;
3897 }
3898 }
3899
Stefan Behrens8dabb742012-11-06 13:15:27 +01003900 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003901 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003902 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3903 BUG_ON(num_devices < 1);
3904 num_devices--;
3905 }
Liu Bo73beece2015-07-17 16:49:19 +08003906 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003907 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3908 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003909 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003910 if (num_devices > 2)
3911 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3912 if (num_devices > 3)
3913 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3914 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003915 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003916 btrfs_err(fs_info,
3917 "unable to start balance with target data profile %llu",
3918 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003919 ret = -EINVAL;
3920 goto out;
3921 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003922 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003923 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003924 "unable to start balance with target metadata profile %llu",
3925 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003926 ret = -EINVAL;
3927 goto out;
3928 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003929 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003930 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003931 "unable to start balance with target system profile %llu",
3932 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003933 ret = -EINVAL;
3934 goto out;
3935 }
3936
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003937 /* allow to reduce meta or sys integrity only if force set */
3938 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003939 BTRFS_BLOCK_GROUP_RAID10 |
3940 BTRFS_BLOCK_GROUP_RAID5 |
3941 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003942 do {
3943 seq = read_seqbegin(&fs_info->profiles_lock);
3944
3945 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3946 (fs_info->avail_system_alloc_bits & allowed) &&
3947 !(bctl->sys.target & allowed)) ||
3948 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3949 (fs_info->avail_metadata_alloc_bits & allowed) &&
3950 !(bctl->meta.target & allowed))) {
3951 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003952 btrfs_info(fs_info,
3953 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003954 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003955 btrfs_err(fs_info,
3956 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003957 ret = -EINVAL;
3958 goto out;
3959 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003960 }
Miao Xiede98ced2013-01-29 10:13:12 +00003961 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003962
Adam Borowski14506122017-03-07 23:34:44 +01003963 /* if we're not converting, the target field is uninitialized */
3964 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3965 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3966 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3967 bctl->data.target : fs_info->avail_data_alloc_bits;
3968 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3969 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003970 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003971 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003972 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003973 }
3974
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003975 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003976 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003977 goto out;
3978
Ilya Dryomov59641012012-01-16 22:04:48 +02003979 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3980 BUG_ON(ret == -EEXIST);
3981 set_balance_control(bctl);
3982 } else {
3983 BUG_ON(ret != -EEXIST);
3984 spin_lock(&fs_info->balance_lock);
3985 update_balance_args(bctl);
3986 spin_unlock(&fs_info->balance_lock);
3987 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003988
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003989 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003990 mutex_unlock(&fs_info->balance_mutex);
3991
3992 ret = __btrfs_balance(fs_info);
3993
3994 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003995 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003996
3997 if (bargs) {
3998 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003999 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004000 }
4001
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07004002 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
4003 balance_need_close(fs_info)) {
4004 __cancel_balance(fs_info);
4005 }
4006
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004007 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02004008
4009 return ret;
4010out:
Ilya Dryomov59641012012-01-16 22:04:48 +02004011 if (bctl->flags & BTRFS_BALANCE_RESUME)
4012 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004013 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02004014 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02004015 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004016 }
Ilya Dryomov59641012012-01-16 22:04:48 +02004017 return ret;
4018}
4019
4020static int balance_kthread(void *data)
4021{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004022 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004023 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02004024
4025 mutex_lock(&fs_info->volume_mutex);
4026 mutex_lock(&fs_info->balance_mutex);
4027
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004028 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004029 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004030 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004031 }
Ilya Dryomov59641012012-01-16 22:04:48 +02004032
4033 mutex_unlock(&fs_info->balance_mutex);
4034 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004035
Ilya Dryomov59641012012-01-16 22:04:48 +02004036 return ret;
4037}
4038
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004039int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4040{
4041 struct task_struct *tsk;
4042
4043 spin_lock(&fs_info->balance_lock);
4044 if (!fs_info->balance_ctl) {
4045 spin_unlock(&fs_info->balance_lock);
4046 return 0;
4047 }
4048 spin_unlock(&fs_info->balance_lock);
4049
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004050 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004051 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004052 return 0;
4053 }
4054
4055 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304056 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004057}
4058
Ilya Dryomov68310a52012-06-22 12:24:12 -06004059int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004060{
Ilya Dryomov59641012012-01-16 22:04:48 +02004061 struct btrfs_balance_control *bctl;
4062 struct btrfs_balance_item *item;
4063 struct btrfs_disk_balance_args disk_bargs;
4064 struct btrfs_path *path;
4065 struct extent_buffer *leaf;
4066 struct btrfs_key key;
4067 int ret;
4068
4069 path = btrfs_alloc_path();
4070 if (!path)
4071 return -ENOMEM;
4072
Ilya Dryomov68310a52012-06-22 12:24:12 -06004073 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004074 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004075 key.offset = 0;
4076
4077 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4078 if (ret < 0)
4079 goto out;
4080 if (ret > 0) { /* ret = -ENOENT; */
4081 ret = 0;
4082 goto out;
4083 }
4084
Ilya Dryomov59641012012-01-16 22:04:48 +02004085 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4086 if (!bctl) {
4087 ret = -ENOMEM;
4088 goto out;
4089 }
4090
Ilya Dryomov59641012012-01-16 22:04:48 +02004091 leaf = path->nodes[0];
4092 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4093
Ilya Dryomov68310a52012-06-22 12:24:12 -06004094 bctl->fs_info = fs_info;
4095 bctl->flags = btrfs_balance_flags(leaf, item);
4096 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004097
4098 btrfs_balance_data(leaf, item, &disk_bargs);
4099 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4100 btrfs_balance_meta(leaf, item, &disk_bargs);
4101 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4102 btrfs_balance_sys(leaf, item, &disk_bargs);
4103 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4104
David Sterba171938e2017-03-28 14:44:21 +02004105 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004106
Ilya Dryomov68310a52012-06-22 12:24:12 -06004107 mutex_lock(&fs_info->volume_mutex);
4108 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004109
Ilya Dryomov68310a52012-06-22 12:24:12 -06004110 set_balance_control(bctl);
4111
4112 mutex_unlock(&fs_info->balance_mutex);
4113 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004114out:
4115 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004116 return ret;
4117}
4118
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004119int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4120{
4121 int ret = 0;
4122
4123 mutex_lock(&fs_info->balance_mutex);
4124 if (!fs_info->balance_ctl) {
4125 mutex_unlock(&fs_info->balance_mutex);
4126 return -ENOTCONN;
4127 }
4128
4129 if (atomic_read(&fs_info->balance_running)) {
4130 atomic_inc(&fs_info->balance_pause_req);
4131 mutex_unlock(&fs_info->balance_mutex);
4132
4133 wait_event(fs_info->balance_wait_q,
4134 atomic_read(&fs_info->balance_running) == 0);
4135
4136 mutex_lock(&fs_info->balance_mutex);
4137 /* we are good with balance_ctl ripped off from under us */
4138 BUG_ON(atomic_read(&fs_info->balance_running));
4139 atomic_dec(&fs_info->balance_pause_req);
4140 } else {
4141 ret = -ENOTCONN;
4142 }
4143
4144 mutex_unlock(&fs_info->balance_mutex);
4145 return ret;
4146}
4147
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004148int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4149{
David Howellsbc98a422017-07-17 08:45:34 +01004150 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004151 return -EROFS;
4152
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004153 mutex_lock(&fs_info->balance_mutex);
4154 if (!fs_info->balance_ctl) {
4155 mutex_unlock(&fs_info->balance_mutex);
4156 return -ENOTCONN;
4157 }
4158
4159 atomic_inc(&fs_info->balance_cancel_req);
4160 /*
4161 * if we are running just wait and return, balance item is
4162 * deleted in btrfs_balance in this case
4163 */
4164 if (atomic_read(&fs_info->balance_running)) {
4165 mutex_unlock(&fs_info->balance_mutex);
4166 wait_event(fs_info->balance_wait_q,
4167 atomic_read(&fs_info->balance_running) == 0);
4168 mutex_lock(&fs_info->balance_mutex);
4169 } else {
4170 /* __cancel_balance needs volume_mutex */
4171 mutex_unlock(&fs_info->balance_mutex);
4172 mutex_lock(&fs_info->volume_mutex);
4173 mutex_lock(&fs_info->balance_mutex);
4174
4175 if (fs_info->balance_ctl)
4176 __cancel_balance(fs_info);
4177
4178 mutex_unlock(&fs_info->volume_mutex);
4179 }
4180
4181 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4182 atomic_dec(&fs_info->balance_cancel_req);
4183 mutex_unlock(&fs_info->balance_mutex);
4184 return 0;
4185}
4186
Stefan Behrens803b2f52013-08-15 17:11:21 +02004187static int btrfs_uuid_scan_kthread(void *data)
4188{
4189 struct btrfs_fs_info *fs_info = data;
4190 struct btrfs_root *root = fs_info->tree_root;
4191 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004192 struct btrfs_path *path = NULL;
4193 int ret = 0;
4194 struct extent_buffer *eb;
4195 int slot;
4196 struct btrfs_root_item root_item;
4197 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004198 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004199
4200 path = btrfs_alloc_path();
4201 if (!path) {
4202 ret = -ENOMEM;
4203 goto out;
4204 }
4205
4206 key.objectid = 0;
4207 key.type = BTRFS_ROOT_ITEM_KEY;
4208 key.offset = 0;
4209
Stefan Behrens803b2f52013-08-15 17:11:21 +02004210 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004211 ret = btrfs_search_forward(root, &key, path,
4212 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004213 if (ret) {
4214 if (ret > 0)
4215 ret = 0;
4216 break;
4217 }
4218
4219 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4220 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4221 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4222 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4223 goto skip;
4224
4225 eb = path->nodes[0];
4226 slot = path->slots[0];
4227 item_size = btrfs_item_size_nr(eb, slot);
4228 if (item_size < sizeof(root_item))
4229 goto skip;
4230
Stefan Behrens803b2f52013-08-15 17:11:21 +02004231 read_extent_buffer(eb, &root_item,
4232 btrfs_item_ptr_offset(eb, slot),
4233 (int)sizeof(root_item));
4234 if (btrfs_root_refs(&root_item) == 0)
4235 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004236
4237 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4238 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4239 if (trans)
4240 goto update_tree;
4241
4242 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004243 /*
4244 * 1 - subvol uuid item
4245 * 1 - received_subvol uuid item
4246 */
4247 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4248 if (IS_ERR(trans)) {
4249 ret = PTR_ERR(trans);
4250 break;
4251 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004252 continue;
4253 } else {
4254 goto skip;
4255 }
4256update_tree:
4257 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004258 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004259 root_item.uuid,
4260 BTRFS_UUID_KEY_SUBVOL,
4261 key.objectid);
4262 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004263 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004264 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004265 break;
4266 }
4267 }
4268
4269 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004270 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004271 root_item.received_uuid,
4272 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4273 key.objectid);
4274 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004275 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004276 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004277 break;
4278 }
4279 }
4280
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004281skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004282 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004283 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004284 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004285 if (ret)
4286 break;
4287 }
4288
Stefan Behrens803b2f52013-08-15 17:11:21 +02004289 btrfs_release_path(path);
4290 if (key.offset < (u64)-1) {
4291 key.offset++;
4292 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4293 key.offset = 0;
4294 key.type = BTRFS_ROOT_ITEM_KEY;
4295 } else if (key.objectid < (u64)-1) {
4296 key.offset = 0;
4297 key.type = BTRFS_ROOT_ITEM_KEY;
4298 key.objectid++;
4299 } else {
4300 break;
4301 }
4302 cond_resched();
4303 }
4304
4305out:
4306 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004307 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004308 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004309 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004310 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004311 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004312 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004313 up(&fs_info->uuid_tree_rescan_sem);
4314 return 0;
4315}
4316
Stefan Behrens70f80172013-08-15 17:11:23 +02004317/*
4318 * Callback for btrfs_uuid_tree_iterate().
4319 * returns:
4320 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004321 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004322 * > 0 if the check failed, which means the caller shall remove the entry.
4323 */
4324static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4325 u8 *uuid, u8 type, u64 subid)
4326{
4327 struct btrfs_key key;
4328 int ret = 0;
4329 struct btrfs_root *subvol_root;
4330
4331 if (type != BTRFS_UUID_KEY_SUBVOL &&
4332 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4333 goto out;
4334
4335 key.objectid = subid;
4336 key.type = BTRFS_ROOT_ITEM_KEY;
4337 key.offset = (u64)-1;
4338 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4339 if (IS_ERR(subvol_root)) {
4340 ret = PTR_ERR(subvol_root);
4341 if (ret == -ENOENT)
4342 ret = 1;
4343 goto out;
4344 }
4345
4346 switch (type) {
4347 case BTRFS_UUID_KEY_SUBVOL:
4348 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4349 ret = 1;
4350 break;
4351 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4352 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4353 BTRFS_UUID_SIZE))
4354 ret = 1;
4355 break;
4356 }
4357
4358out:
4359 return ret;
4360}
4361
4362static int btrfs_uuid_rescan_kthread(void *data)
4363{
4364 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4365 int ret;
4366
4367 /*
4368 * 1st step is to iterate through the existing UUID tree and
4369 * to delete all entries that contain outdated data.
4370 * 2nd step is to add all missing entries to the UUID tree.
4371 */
4372 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4373 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004374 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004375 up(&fs_info->uuid_tree_rescan_sem);
4376 return ret;
4377 }
4378 return btrfs_uuid_scan_kthread(data);
4379}
4380
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004381int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4382{
4383 struct btrfs_trans_handle *trans;
4384 struct btrfs_root *tree_root = fs_info->tree_root;
4385 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004386 struct task_struct *task;
4387 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004388
4389 /*
4390 * 1 - root node
4391 * 1 - root item
4392 */
4393 trans = btrfs_start_transaction(tree_root, 2);
4394 if (IS_ERR(trans))
4395 return PTR_ERR(trans);
4396
4397 uuid_root = btrfs_create_tree(trans, fs_info,
4398 BTRFS_UUID_TREE_OBJECTID);
4399 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004400 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004401 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004402 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004403 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004404 }
4405
4406 fs_info->uuid_root = uuid_root;
4407
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004408 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004409 if (ret)
4410 return ret;
4411
4412 down(&fs_info->uuid_tree_rescan_sem);
4413 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4414 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004415 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004416 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004417 up(&fs_info->uuid_tree_rescan_sem);
4418 return PTR_ERR(task);
4419 }
4420
4421 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004422}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004423
Stefan Behrens70f80172013-08-15 17:11:23 +02004424int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4425{
4426 struct task_struct *task;
4427
4428 down(&fs_info->uuid_tree_rescan_sem);
4429 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4430 if (IS_ERR(task)) {
4431 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004432 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004433 up(&fs_info->uuid_tree_rescan_sem);
4434 return PTR_ERR(task);
4435 }
4436
4437 return 0;
4438}
4439
Chris Mason8f18cf12008-04-25 16:53:30 -04004440/*
4441 * shrinking a device means finding all of the device extents past
4442 * the new size, and then following the back refs to the chunks.
4443 * The chunk relocation code actually frees the device extent
4444 */
4445int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004447 struct btrfs_fs_info *fs_info = device->fs_info;
4448 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004449 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004450 struct btrfs_dev_extent *dev_extent = NULL;
4451 struct btrfs_path *path;
4452 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004453 u64 chunk_offset;
4454 int ret;
4455 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004456 int failed = 0;
4457 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004458 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004459 struct extent_buffer *l;
4460 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004461 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004462 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004463 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004464 u64 diff;
4465
4466 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004467 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004468
Anand Jain401e29c2017-12-04 12:54:55 +08004469 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004470 return -EINVAL;
4471
Chris Mason8f18cf12008-04-25 16:53:30 -04004472 path = btrfs_alloc_path();
4473 if (!path)
4474 return -ENOMEM;
4475
David Sterbae4058b52015-11-27 16:31:35 +01004476 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004477
David Sterba34441362016-10-04 19:34:27 +02004478 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004479
Miao Xie7cc8e582014-09-03 21:35:38 +08004480 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004481 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004482 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004483 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004484 }
David Sterba34441362016-10-04 19:34:27 +02004485 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004486
Josef Bacikba1bf482009-09-11 16:11:19 -04004487again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004488 key.objectid = device->devid;
4489 key.offset = (u64)-1;
4490 key.type = BTRFS_DEV_EXTENT_KEY;
4491
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004492 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004493 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004495 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004497 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004498 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004499
4500 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004501 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004502 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004503 if (ret < 0)
4504 goto done;
4505 if (ret) {
4506 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004507 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004508 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004509 }
4510
4511 l = path->nodes[0];
4512 slot = path->slots[0];
4513 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4514
Josef Bacikba1bf482009-09-11 16:11:19 -04004515 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004516 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004517 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004518 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004519 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004520
4521 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4522 length = btrfs_dev_extent_length(l, dev_extent);
4523
Josef Bacikba1bf482009-09-11 16:11:19 -04004524 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004525 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004526 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004527 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004528 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004529
Chris Mason8f18cf12008-04-25 16:53:30 -04004530 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004531 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004532
Liu Boa6f93c72017-11-15 16:28:11 -07004533 /*
4534 * We may be relocating the only data chunk we have,
4535 * which could potentially end up with losing data's
4536 * raid profile, so lets allocate an empty one in
4537 * advance.
4538 */
4539 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4540 if (ret < 0) {
4541 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4542 goto done;
4543 }
4544
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004545 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4546 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004547 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004548 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004549 if (ret == -ENOSPC)
4550 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004551 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004552
4553 if (failed && !retried) {
4554 failed = 0;
4555 retried = true;
4556 goto again;
4557 } else if (failed && retried) {
4558 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004559 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004560 }
4561
Chris Balld6397ba2009-04-27 07:29:03 -04004562 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004563 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004564 if (IS_ERR(trans)) {
4565 ret = PTR_ERR(trans);
4566 goto done;
4567 }
4568
David Sterba34441362016-10-04 19:34:27 +02004569 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004570
4571 /*
4572 * We checked in the above loop all device extents that were already in
4573 * the device tree. However before we have updated the device's
4574 * total_bytes to the new size, we might have had chunk allocations that
4575 * have not complete yet (new block groups attached to transaction
4576 * handles), and therefore their device extents were not yet in the
4577 * device tree and we missed them in the loop above. So if we have any
4578 * pending chunk using a device extent that overlaps the device range
4579 * that we can not use anymore, commit the current transaction and
4580 * repeat the search on the device tree - this way we guarantee we will
4581 * not have chunks using device extents that end beyond 'new_size'.
4582 */
4583 if (!checked_pending_chunks) {
4584 u64 start = new_size;
4585 u64 len = old_size - new_size;
4586
Jeff Mahoney499f3772015-06-15 09:41:17 -04004587 if (contains_pending_extent(trans->transaction, device,
4588 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004589 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004590 checked_pending_chunks = true;
4591 failed = 0;
4592 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004593 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004594 if (ret)
4595 goto done;
4596 goto again;
4597 }
4598 }
4599
Miao Xie7cc8e582014-09-03 21:35:38 +08004600 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004601 if (list_empty(&device->resized_list))
4602 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004603 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004604
Chris Balld6397ba2009-04-27 07:29:03 -04004605 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004606 btrfs_set_super_total_bytes(super_copy,
4607 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004608 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004609
4610 /* Now btrfs_update_device() will change the on-disk size. */
4611 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004612 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004613done:
4614 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004615 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004616 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004617 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004618 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004619 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004620 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004621 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004622 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004623 return ret;
4624}
4625
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004626static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004627 struct btrfs_key *key,
4628 struct btrfs_chunk *chunk, int item_size)
4629{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004630 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004631 struct btrfs_disk_key disk_key;
4632 u32 array_size;
4633 u8 *ptr;
4634
David Sterba34441362016-10-04 19:34:27 +02004635 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004636 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004637 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004638 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004639 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004640 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004641 }
Chris Mason0b86a832008-03-24 15:01:56 -04004642
4643 ptr = super_copy->sys_chunk_array + array_size;
4644 btrfs_cpu_key_to_disk(&disk_key, key);
4645 memcpy(ptr, &disk_key, sizeof(disk_key));
4646 ptr += sizeof(disk_key);
4647 memcpy(ptr, chunk, item_size);
4648 item_size += sizeof(disk_key);
4649 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004650 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004651
Chris Mason0b86a832008-03-24 15:01:56 -04004652 return 0;
4653}
4654
Miao Xieb2117a32011-01-05 10:07:28 +00004655/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004656 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004657 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004658static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004659{
Arne Jansen73c5de02011-04-12 12:07:57 +02004660 const struct btrfs_device_info *di_a = a;
4661 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004662
Arne Jansen73c5de02011-04-12 12:07:57 +02004663 if (di_a->max_avail > di_b->max_avail)
4664 return -1;
4665 if (di_a->max_avail < di_b->max_avail)
4666 return 1;
4667 if (di_a->total_avail > di_b->total_avail)
4668 return -1;
4669 if (di_a->total_avail < di_b->total_avail)
4670 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004671 return 0;
4672}
4673
David Woodhouse53b381b2013-01-29 18:40:14 -05004674static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4675{
Zhao Leiffe2d202015-01-20 15:11:44 +08004676 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004677 return;
4678
Miao Xieceda0862013-04-11 10:30:16 +00004679 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004680}
4681
Qu Wenruo062d4d12018-01-30 18:20:46 +08004682#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004683 - sizeof(struct btrfs_chunk)) \
4684 / sizeof(struct btrfs_stripe) + 1)
4685
4686#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4687 - 2 * sizeof(struct btrfs_disk_key) \
4688 - 2 * sizeof(struct btrfs_chunk)) \
4689 / sizeof(struct btrfs_stripe) + 1)
4690
Miao Xieb2117a32011-01-05 10:07:28 +00004691static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004692 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004693{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004694 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004695 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004696 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004697 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004698 struct extent_map_tree *em_tree;
4699 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004700 struct btrfs_device_info *devices_info = NULL;
4701 u64 total_avail;
4702 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004703 int data_stripes; /* number of stripes that count for
4704 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004705 int sub_stripes; /* sub_stripes info for map */
4706 int dev_stripes; /* stripes per dev */
4707 int devs_max; /* max devs to use */
4708 int devs_min; /* min devs needed */
4709 int devs_increment; /* ndevs has to be a multiple of this */
4710 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004711 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004712 u64 max_stripe_size;
4713 u64 max_chunk_size;
4714 u64 stripe_size;
4715 u64 num_bytes;
4716 int ndevs;
4717 int i;
4718 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004719 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004720
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004721 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004722
Qu Wenruo4117f202018-01-22 13:50:54 +08004723 if (list_empty(&fs_devices->alloc_list)) {
4724 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4725 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004726 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004727 }
Miao Xieb2117a32011-01-05 10:07:28 +00004728
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004729 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004730
Liu Bo31e50222012-11-21 14:18:10 +00004731 sub_stripes = btrfs_raid_array[index].sub_stripes;
4732 dev_stripes = btrfs_raid_array[index].dev_stripes;
4733 devs_max = btrfs_raid_array[index].devs_max;
4734 devs_min = btrfs_raid_array[index].devs_min;
4735 devs_increment = btrfs_raid_array[index].devs_increment;
4736 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004737
4738 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004739 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004740 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004741 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004742 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004743 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004744 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004745 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4746 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004747 else
Byongho Leeee221842015-12-15 01:42:10 +09004748 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004749 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004750 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004751 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004752 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004753 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004754 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004755 if (!devs_max)
4756 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004757 } else {
David Sterba351fd352014-05-15 16:48:20 +02004758 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004759 type);
4760 BUG_ON(1);
4761 }
4762
4763 /* we don't want a chunk larger than 10% of writeable space */
4764 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4765 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004766
David Sterba31e818f2015-02-20 18:00:26 +01004767 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004768 GFP_NOFS);
4769 if (!devices_info)
4770 return -ENOMEM;
4771
Arne Jansen73c5de02011-04-12 12:07:57 +02004772 /*
4773 * in the first pass through the devices list, we gather information
4774 * about the available holes on each device.
4775 */
4776 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004777 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004778 u64 max_avail;
4779 u64 dev_offset;
4780
Anand Jainebbede42017-12-04 12:54:52 +08004781 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004782 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004783 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004784 continue;
4785 }
4786
Anand Jaine12c9622017-12-04 12:54:53 +08004787 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4788 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004789 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004790 continue;
4791
4792 if (device->total_bytes > device->bytes_used)
4793 total_avail = device->total_bytes - device->bytes_used;
4794 else
4795 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004796
4797 /* If there is no space on this device, skip it. */
4798 if (total_avail == 0)
4799 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004800
Josef Bacik6df9a952013-06-27 13:22:46 -04004801 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004802 max_stripe_size * dev_stripes,
4803 &dev_offset, &max_avail);
4804 if (ret && ret != -ENOSPC)
4805 goto error;
4806
4807 if (ret == 0)
4808 max_avail = max_stripe_size * dev_stripes;
4809
Qu Wenruo4117f202018-01-22 13:50:54 +08004810 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4811 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4812 btrfs_debug(info,
4813 "%s: devid %llu has no free space, have=%llu want=%u",
4814 __func__, device->devid, max_avail,
4815 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004816 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004817 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004818
Eric Sandeen063d0062013-01-31 00:55:01 +00004819 if (ndevs == fs_devices->rw_devices) {
4820 WARN(1, "%s: found more than %llu devices\n",
4821 __func__, fs_devices->rw_devices);
4822 break;
4823 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004824 devices_info[ndevs].dev_offset = dev_offset;
4825 devices_info[ndevs].max_avail = max_avail;
4826 devices_info[ndevs].total_avail = total_avail;
4827 devices_info[ndevs].dev = device;
4828 ++ndevs;
4829 }
4830
4831 /*
4832 * now sort the devices by hole size / available space
4833 */
4834 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4835 btrfs_cmp_device_info, NULL);
4836
4837 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004838 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004839
4840 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4841 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004842 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4843 btrfs_debug(info,
4844 "%s: not enough devices with free space: have=%d minimum required=%d",
4845 __func__, ndevs, min(devs_min,
4846 devs_increment * sub_stripes));
4847 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004848 goto error;
4849 }
4850
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004851 ndevs = min(ndevs, devs_max);
4852
Arne Jansen73c5de02011-04-12 12:07:57 +02004853 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004854 * The primary goal is to maximize the number of stripes, so use as
4855 * many devices as possible, even if the stripes are not maximum sized.
4856 *
4857 * The DUP profile stores more than one stripe per device, the
4858 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004859 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004860 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004861 num_stripes = ndevs * dev_stripes;
4862
David Woodhouse53b381b2013-01-29 18:40:14 -05004863 /*
4864 * this will have to be fixed for RAID1 and RAID10 over
4865 * more drives
4866 */
4867 data_stripes = num_stripes / ncopies;
4868
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004869 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004870 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004871
4872 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004873 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004874
4875 /*
4876 * Use the number of data stripes to figure out how big this chunk
4877 * is really going to be in terms of logical address space,
4878 * and compare that answer with the max chunk size
4879 */
4880 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004881 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004882
4883 /* bump the answer up to a 16MB boundary */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004884 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004885
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004886 /*
4887 * But don't go higher than the limits we found while searching
4888 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004889 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004890 stripe_size = min(devices_info[ndevs - 1].max_avail,
4891 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004892 }
4893
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004894 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004895 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004896
Miao Xieb2117a32011-01-05 10:07:28 +00004897 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4898 if (!map) {
4899 ret = -ENOMEM;
4900 goto error;
4901 }
4902 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004903
Arne Jansen73c5de02011-04-12 12:07:57 +02004904 for (i = 0; i < ndevs; ++i) {
4905 for (j = 0; j < dev_stripes; ++j) {
4906 int s = i * dev_stripes + j;
4907 map->stripes[s].dev = devices_info[i].dev;
4908 map->stripes[s].physical = devices_info[i].dev_offset +
4909 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004910 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004911 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004912 map->stripe_len = BTRFS_STRIPE_LEN;
4913 map->io_align = BTRFS_STRIPE_LEN;
4914 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004915 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004916 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004917
David Woodhouse53b381b2013-01-29 18:40:14 -05004918 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004919
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004920 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004921
David Sterba172ddd62011-04-21 00:48:27 +02004922 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004923 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004924 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004925 ret = -ENOMEM;
4926 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004927 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004928 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004929 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004930 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004931 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004932 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004933 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004934 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004935
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004936 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004937 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004938 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004939 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004940 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004941 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004942 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004943 }
Yan Zheng2b820322008-11-17 21:11:30 -05004944
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004945 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4946 refcount_inc(&em->refs);
4947 write_unlock(&em_tree->lock);
4948
Nikolay Borisov01744842017-07-27 14:22:11 +03004949 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004950 if (ret)
4951 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004952
Miao Xie7cc8e582014-09-03 21:35:38 +08004953 for (i = 0; i < map->num_stripes; i++) {
4954 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4955 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4956 }
Miao Xie43530c42014-09-03 21:35:36 +08004957
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004958 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004959
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004960 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004961 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004962
Miao Xieb2117a32011-01-05 10:07:28 +00004963 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004964 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004965
Josef Bacik6df9a952013-06-27 13:22:46 -04004966error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004967 write_lock(&em_tree->lock);
4968 remove_extent_mapping(em_tree, em);
4969 write_unlock(&em_tree->lock);
4970
4971 /* One for our allocation */
4972 free_extent_map(em);
4973 /* One for the tree reference */
4974 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004975 /* One for the pending_chunks list reference */
4976 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004977error:
Miao Xieb2117a32011-01-05 10:07:28 +00004978 kfree(devices_info);
4979 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004980}
4981
Josef Bacik6df9a952013-06-27 13:22:46 -04004982int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004983 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004984 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004985{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004986 struct btrfs_root *extent_root = fs_info->extent_root;
4987 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004988 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004989 struct btrfs_device *device;
4990 struct btrfs_chunk *chunk;
4991 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004992 struct extent_map *em;
4993 struct map_lookup *map;
4994 size_t item_size;
4995 u64 dev_offset;
4996 u64 stripe_size;
4997 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004998 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004999
Liu Bo592d92e2017-03-14 13:33:55 -07005000 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
5001 if (IS_ERR(em))
5002 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04005003
Jeff Mahoney95617d62015-06-03 10:55:48 -04005004 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04005005 item_size = btrfs_chunk_item_size(map->num_stripes);
5006 stripe_size = em->orig_block_len;
5007
5008 chunk = kzalloc(item_size, GFP_NOFS);
5009 if (!chunk) {
5010 ret = -ENOMEM;
5011 goto out;
5012 }
5013
Filipe Manana50460e32015-11-20 10:42:47 +00005014 /*
5015 * Take the device list mutex to prevent races with the final phase of
5016 * a device replace operation that replaces the device object associated
5017 * with the map's stripes, because the device object's id can change
5018 * at any time during that final phase of the device replace operation
5019 * (dev-replace.c:btrfs_dev_replace_finishing()).
5020 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005021 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04005022 for (i = 0; i < map->num_stripes; i++) {
5023 device = map->stripes[i].dev;
5024 dev_offset = map->stripes[i].physical;
5025
Yan Zheng2b820322008-11-17 21:11:30 -05005026 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07005027 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005028 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03005029 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
5030 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04005031 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00005032 break;
5033 }
5034 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00005036 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05005037 }
5038
Yan Zheng2b820322008-11-17 21:11:30 -05005039 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04005040 for (i = 0; i < map->num_stripes; i++) {
5041 device = map->stripes[i].dev;
5042 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05005043
5044 btrfs_set_stack_stripe_devid(stripe, device->devid);
5045 btrfs_set_stack_stripe_offset(stripe, dev_offset);
5046 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
5047 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05005048 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005049 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05005050
5051 btrfs_set_stack_chunk_length(chunk, chunk_size);
5052 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
5053 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
5054 btrfs_set_stack_chunk_type(chunk, map->type);
5055 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
5056 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5057 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005058 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005059 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5060
5061 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5062 key.type = BTRFS_CHUNK_ITEM_KEY;
5063 key.offset = chunk_offset;
5064
5065 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005066 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5067 /*
5068 * TODO: Cleanup of inserted chunk root in case of
5069 * failure.
5070 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005071 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005072 }
liubo1abe9b82011-03-24 11:18:59 +00005073
Josef Bacik6df9a952013-06-27 13:22:46 -04005074out:
Yan Zheng2b820322008-11-17 21:11:30 -05005075 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005076 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005077 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005078}
5079
5080/*
5081 * Chunk allocation falls into two parts. The first part does works
5082 * that make the new allocated chunk useable, but not do any operation
5083 * that modifies the chunk tree. The second part does the works that
5084 * require modifying the chunk tree. This division is important for the
5085 * bootstrap process of adding storage to a seed btrfs.
5086 */
5087int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005088 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005089{
5090 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005091
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005092 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
5093 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005094 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005095}
5096
Chris Masond3977122009-01-05 21:25:51 -05005097static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005098 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005099{
5100 u64 chunk_offset;
5101 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005102 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005103 int ret;
5104
Josef Bacik6df9a952013-06-27 13:22:46 -04005105 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005106 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005107 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005108 if (ret)
5109 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005110
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005111 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005112 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005113 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005114 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005115}
5116
Miao Xied20983b2014-07-03 18:22:13 +08005117static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5118{
5119 int max_errors;
5120
5121 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5122 BTRFS_BLOCK_GROUP_RAID10 |
5123 BTRFS_BLOCK_GROUP_RAID5 |
5124 BTRFS_BLOCK_GROUP_DUP)) {
5125 max_errors = 1;
5126 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5127 max_errors = 2;
5128 } else {
5129 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005130 }
5131
Miao Xied20983b2014-07-03 18:22:13 +08005132 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005133}
5134
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005135int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005136{
5137 struct extent_map *em;
5138 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005139 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005140 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005141 int i;
5142
Liu Bo592d92e2017-03-14 13:33:55 -07005143 em = get_chunk_map(fs_info, chunk_offset, 1);
5144 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005145 return 1;
5146
Jeff Mahoney95617d62015-06-03 10:55:48 -04005147 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005148 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005149 if (test_bit(BTRFS_DEV_STATE_MISSING,
5150 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005151 miss_ndevs++;
5152 continue;
5153 }
Anand Jainebbede42017-12-04 12:54:52 +08005154 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5155 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005156 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005157 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005158 }
5159 }
Miao Xied20983b2014-07-03 18:22:13 +08005160
5161 /*
5162 * If the number of missing devices is larger than max errors,
5163 * we can not write the data into that chunk successfully, so
5164 * set it readonly.
5165 */
5166 if (miss_ndevs > btrfs_chunk_max_errors(map))
5167 readonly = 1;
5168end:
Yan Zheng2b820322008-11-17 21:11:30 -05005169 free_extent_map(em);
5170 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005171}
5172
5173void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5174{
David Sterbaa8067e02011-04-21 00:34:43 +02005175 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005176}
5177
5178void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5179{
5180 struct extent_map *em;
5181
Chris Masond3977122009-01-05 21:25:51 -05005182 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005183 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005184 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5185 if (em)
5186 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005187 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005188 if (!em)
5189 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005190 /* once for us */
5191 free_extent_map(em);
5192 /* once for the tree */
5193 free_extent_map(em);
5194 }
5195}
5196
Stefan Behrens5d964052012-11-05 14:59:07 +01005197int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005198{
5199 struct extent_map *em;
5200 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005201 int ret;
5202
Liu Bo592d92e2017-03-14 13:33:55 -07005203 em = get_chunk_map(fs_info, logical, len);
5204 if (IS_ERR(em))
5205 /*
5206 * We could return errors for these cases, but that could get
5207 * ugly and we'd probably do the same thing which is just not do
5208 * anything else and exit, so return 1 so the callers don't try
5209 * to use other copies.
5210 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005211 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005212
Jeff Mahoney95617d62015-06-03 10:55:48 -04005213 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005214 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5215 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005216 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5217 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005218 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5219 ret = 2;
5220 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005221 /*
5222 * There could be two corrupted data stripes, we need
5223 * to loop retry in order to rebuild the correct data.
5224 *
5225 * Fail a stripe at a time on every retry except the
5226 * stripe under reconstruction.
5227 */
5228 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005229 else
5230 ret = 1;
5231 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005232
Liu Bo73beece2015-07-17 16:49:19 +08005233 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Liu Bo6fad8232017-03-14 13:33:59 -07005234 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5235 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005236 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005237 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005238
Chris Masonf1885912008-04-09 16:28:12 -04005239 return ret;
5240}
5241
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005242unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005243 u64 logical)
5244{
5245 struct extent_map *em;
5246 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005247 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005248
Liu Bo592d92e2017-03-14 13:33:55 -07005249 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005250
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005251 if (!WARN_ON(IS_ERR(em))) {
5252 map = em->map_lookup;
5253 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5254 len = map->stripe_len * nr_data_stripes(map);
5255 free_extent_map(em);
5256 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005257 return len;
5258}
5259
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005260int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005261{
5262 struct extent_map *em;
5263 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005264 int ret = 0;
5265
Liu Bo592d92e2017-03-14 13:33:55 -07005266 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005267
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005268 if(!WARN_ON(IS_ERR(em))) {
5269 map = em->map_lookup;
5270 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5271 ret = 1;
5272 free_extent_map(em);
5273 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005274 return ret;
5275}
5276
Stefan Behrens30d98612012-11-06 14:52:18 +01005277static int find_live_mirror(struct btrfs_fs_info *fs_info,
5278 struct map_lookup *map, int first, int num,
5279 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005280{
5281 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005282 int tolerance;
5283 struct btrfs_device *srcdev;
5284
5285 if (dev_replace_is_ongoing &&
5286 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5287 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5288 srcdev = fs_info->dev_replace.srcdev;
5289 else
5290 srcdev = NULL;
5291
5292 /*
5293 * try to avoid the drive that is the source drive for a
5294 * dev-replace procedure, only choose it if no other non-missing
5295 * mirror is available
5296 */
5297 for (tolerance = 0; tolerance < 2; tolerance++) {
5298 if (map->stripes[optimal].dev->bdev &&
5299 (tolerance || map->stripes[optimal].dev != srcdev))
5300 return optimal;
5301 for (i = first; i < first + num; i++) {
5302 if (map->stripes[i].dev->bdev &&
5303 (tolerance || map->stripes[i].dev != srcdev))
5304 return i;
5305 }
Chris Masondfe25022008-05-13 13:46:40 -04005306 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005307
Chris Masondfe25022008-05-13 13:46:40 -04005308 /* we couldn't find one that doesn't fail. Just return something
5309 * and the io error handling code will clean up eventually
5310 */
5311 return optimal;
5312}
5313
David Woodhouse53b381b2013-01-29 18:40:14 -05005314static inline int parity_smaller(u64 a, u64 b)
5315{
5316 return a > b;
5317}
5318
5319/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005320static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005321{
5322 struct btrfs_bio_stripe s;
5323 int i;
5324 u64 l;
5325 int again = 1;
5326
5327 while (again) {
5328 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005329 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005330 if (parity_smaller(bbio->raid_map[i],
5331 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005332 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005333 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005334 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005335 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005336 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005337 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005338
David Woodhouse53b381b2013-01-29 18:40:14 -05005339 again = 1;
5340 }
5341 }
5342 }
5343}
5344
Zhao Lei6e9606d2015-01-20 15:11:34 +08005345static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5346{
5347 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005348 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005349 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005350 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005351 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005352 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005353 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005354 /*
5355 * plus the raid_map, which includes both the tgt dev
5356 * and the stripes
5357 */
5358 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005359 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005360
5361 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005362 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005363
5364 return bbio;
5365}
5366
5367void btrfs_get_bbio(struct btrfs_bio *bbio)
5368{
Elena Reshetova140475a2017-03-03 10:55:10 +02005369 WARN_ON(!refcount_read(&bbio->refs));
5370 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005371}
5372
5373void btrfs_put_bbio(struct btrfs_bio *bbio)
5374{
5375 if (!bbio)
5376 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005377 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005378 kfree(bbio);
5379}
5380
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005381/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5382/*
5383 * Please note that, discard won't be sent to target device of device
5384 * replace.
5385 */
5386static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5387 u64 logical, u64 length,
5388 struct btrfs_bio **bbio_ret)
5389{
5390 struct extent_map *em;
5391 struct map_lookup *map;
5392 struct btrfs_bio *bbio;
5393 u64 offset;
5394 u64 stripe_nr;
5395 u64 stripe_nr_end;
5396 u64 stripe_end_offset;
5397 u64 stripe_cnt;
5398 u64 stripe_len;
5399 u64 stripe_offset;
5400 u64 num_stripes;
5401 u32 stripe_index;
5402 u32 factor = 0;
5403 u32 sub_stripes = 0;
5404 u64 stripes_per_dev = 0;
5405 u32 remaining_stripes = 0;
5406 u32 last_stripe = 0;
5407 int ret = 0;
5408 int i;
5409
5410 /* discard always return a bbio */
5411 ASSERT(bbio_ret);
5412
5413 em = get_chunk_map(fs_info, logical, length);
5414 if (IS_ERR(em))
5415 return PTR_ERR(em);
5416
5417 map = em->map_lookup;
5418 /* we don't discard raid56 yet */
5419 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5420 ret = -EOPNOTSUPP;
5421 goto out;
5422 }
5423
5424 offset = logical - em->start;
5425 length = min_t(u64, em->len - offset, length);
5426
5427 stripe_len = map->stripe_len;
5428 /*
5429 * stripe_nr counts the total number of stripes we have to stride
5430 * to get to this block
5431 */
5432 stripe_nr = div64_u64(offset, stripe_len);
5433
5434 /* stripe_offset is the offset of this block in its stripe */
5435 stripe_offset = offset - stripe_nr * stripe_len;
5436
5437 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005438 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005439 stripe_cnt = stripe_nr_end - stripe_nr;
5440 stripe_end_offset = stripe_nr_end * map->stripe_len -
5441 (offset + length);
5442 /*
5443 * after this, stripe_nr is the number of stripes on this
5444 * device we have to walk to find the data, and stripe_index is
5445 * the number of our device in the stripe array
5446 */
5447 num_stripes = 1;
5448 stripe_index = 0;
5449 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5450 BTRFS_BLOCK_GROUP_RAID10)) {
5451 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5452 sub_stripes = 1;
5453 else
5454 sub_stripes = map->sub_stripes;
5455
5456 factor = map->num_stripes / sub_stripes;
5457 num_stripes = min_t(u64, map->num_stripes,
5458 sub_stripes * stripe_cnt);
5459 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5460 stripe_index *= sub_stripes;
5461 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5462 &remaining_stripes);
5463 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5464 last_stripe *= sub_stripes;
5465 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5466 BTRFS_BLOCK_GROUP_DUP)) {
5467 num_stripes = map->num_stripes;
5468 } else {
5469 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5470 &stripe_index);
5471 }
5472
5473 bbio = alloc_btrfs_bio(num_stripes, 0);
5474 if (!bbio) {
5475 ret = -ENOMEM;
5476 goto out;
5477 }
5478
5479 for (i = 0; i < num_stripes; i++) {
5480 bbio->stripes[i].physical =
5481 map->stripes[stripe_index].physical +
5482 stripe_offset + stripe_nr * map->stripe_len;
5483 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5484
5485 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5486 BTRFS_BLOCK_GROUP_RAID10)) {
5487 bbio->stripes[i].length = stripes_per_dev *
5488 map->stripe_len;
5489
5490 if (i / sub_stripes < remaining_stripes)
5491 bbio->stripes[i].length +=
5492 map->stripe_len;
5493
5494 /*
5495 * Special for the first stripe and
5496 * the last stripe:
5497 *
5498 * |-------|...|-------|
5499 * |----------|
5500 * off end_off
5501 */
5502 if (i < sub_stripes)
5503 bbio->stripes[i].length -=
5504 stripe_offset;
5505
5506 if (stripe_index >= last_stripe &&
5507 stripe_index <= (last_stripe +
5508 sub_stripes - 1))
5509 bbio->stripes[i].length -=
5510 stripe_end_offset;
5511
5512 if (i == sub_stripes - 1)
5513 stripe_offset = 0;
5514 } else {
5515 bbio->stripes[i].length = length;
5516 }
5517
5518 stripe_index++;
5519 if (stripe_index == map->num_stripes) {
5520 stripe_index = 0;
5521 stripe_nr++;
5522 }
5523 }
5524
5525 *bbio_ret = bbio;
5526 bbio->map_type = map->type;
5527 bbio->num_stripes = num_stripes;
5528out:
5529 free_extent_map(em);
5530 return ret;
5531}
5532
Liu Bo5ab56092017-03-14 13:33:57 -07005533/*
5534 * In dev-replace case, for repair case (that's the only case where the mirror
5535 * is selected explicitly when calling btrfs_map_block), blocks left of the
5536 * left cursor can also be read from the target drive.
5537 *
5538 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5539 * array of stripes.
5540 * For READ, it also needs to be supported using the same mirror number.
5541 *
5542 * If the requested block is not left of the left cursor, EIO is returned. This
5543 * can happen because btrfs_num_copies() returns one more in the dev-replace
5544 * case.
5545 */
5546static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5547 u64 logical, u64 length,
5548 u64 srcdev_devid, int *mirror_num,
5549 u64 *physical)
5550{
5551 struct btrfs_bio *bbio = NULL;
5552 int num_stripes;
5553 int index_srcdev = 0;
5554 int found = 0;
5555 u64 physical_of_found = 0;
5556 int i;
5557 int ret = 0;
5558
5559 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5560 logical, &length, &bbio, 0, 0);
5561 if (ret) {
5562 ASSERT(bbio == NULL);
5563 return ret;
5564 }
5565
5566 num_stripes = bbio->num_stripes;
5567 if (*mirror_num > num_stripes) {
5568 /*
5569 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5570 * that means that the requested area is not left of the left
5571 * cursor
5572 */
5573 btrfs_put_bbio(bbio);
5574 return -EIO;
5575 }
5576
5577 /*
5578 * process the rest of the function using the mirror_num of the source
5579 * drive. Therefore look it up first. At the end, patch the device
5580 * pointer to the one of the target drive.
5581 */
5582 for (i = 0; i < num_stripes; i++) {
5583 if (bbio->stripes[i].dev->devid != srcdev_devid)
5584 continue;
5585
5586 /*
5587 * In case of DUP, in order to keep it simple, only add the
5588 * mirror with the lowest physical address
5589 */
5590 if (found &&
5591 physical_of_found <= bbio->stripes[i].physical)
5592 continue;
5593
5594 index_srcdev = i;
5595 found = 1;
5596 physical_of_found = bbio->stripes[i].physical;
5597 }
5598
5599 btrfs_put_bbio(bbio);
5600
5601 ASSERT(found);
5602 if (!found)
5603 return -EIO;
5604
5605 *mirror_num = index_srcdev + 1;
5606 *physical = physical_of_found;
5607 return ret;
5608}
5609
Liu Bo73c0f222017-03-14 13:33:58 -07005610static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5611 struct btrfs_bio **bbio_ret,
5612 struct btrfs_dev_replace *dev_replace,
5613 int *num_stripes_ret, int *max_errors_ret)
5614{
5615 struct btrfs_bio *bbio = *bbio_ret;
5616 u64 srcdev_devid = dev_replace->srcdev->devid;
5617 int tgtdev_indexes = 0;
5618 int num_stripes = *num_stripes_ret;
5619 int max_errors = *max_errors_ret;
5620 int i;
5621
5622 if (op == BTRFS_MAP_WRITE) {
5623 int index_where_to_add;
5624
5625 /*
5626 * duplicate the write operations while the dev replace
5627 * procedure is running. Since the copying of the old disk to
5628 * the new disk takes place at run time while the filesystem is
5629 * mounted writable, the regular write operations to the old
5630 * disk have to be duplicated to go to the new disk as well.
5631 *
5632 * Note that device->missing is handled by the caller, and that
5633 * the write to the old disk is already set up in the stripes
5634 * array.
5635 */
5636 index_where_to_add = num_stripes;
5637 for (i = 0; i < num_stripes; i++) {
5638 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5639 /* write to new disk, too */
5640 struct btrfs_bio_stripe *new =
5641 bbio->stripes + index_where_to_add;
5642 struct btrfs_bio_stripe *old =
5643 bbio->stripes + i;
5644
5645 new->physical = old->physical;
5646 new->length = old->length;
5647 new->dev = dev_replace->tgtdev;
5648 bbio->tgtdev_map[i] = index_where_to_add;
5649 index_where_to_add++;
5650 max_errors++;
5651 tgtdev_indexes++;
5652 }
5653 }
5654 num_stripes = index_where_to_add;
5655 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5656 int index_srcdev = 0;
5657 int found = 0;
5658 u64 physical_of_found = 0;
5659
5660 /*
5661 * During the dev-replace procedure, the target drive can also
5662 * be used to read data in case it is needed to repair a corrupt
5663 * block elsewhere. This is possible if the requested area is
5664 * left of the left cursor. In this area, the target drive is a
5665 * full copy of the source drive.
5666 */
5667 for (i = 0; i < num_stripes; i++) {
5668 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5669 /*
5670 * In case of DUP, in order to keep it simple,
5671 * only add the mirror with the lowest physical
5672 * address
5673 */
5674 if (found &&
5675 physical_of_found <=
5676 bbio->stripes[i].physical)
5677 continue;
5678 index_srcdev = i;
5679 found = 1;
5680 physical_of_found = bbio->stripes[i].physical;
5681 }
5682 }
5683 if (found) {
5684 struct btrfs_bio_stripe *tgtdev_stripe =
5685 bbio->stripes + num_stripes;
5686
5687 tgtdev_stripe->physical = physical_of_found;
5688 tgtdev_stripe->length =
5689 bbio->stripes[index_srcdev].length;
5690 tgtdev_stripe->dev = dev_replace->tgtdev;
5691 bbio->tgtdev_map[index_srcdev] = num_stripes;
5692
5693 tgtdev_indexes++;
5694 num_stripes++;
5695 }
5696 }
5697
5698 *num_stripes_ret = num_stripes;
5699 *max_errors_ret = max_errors;
5700 bbio->num_tgtdevs = tgtdev_indexes;
5701 *bbio_ret = bbio;
5702}
5703
Liu Bo2b19a1f2017-03-14 13:34:00 -07005704static bool need_full_stripe(enum btrfs_map_op op)
5705{
5706 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5707}
5708
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005709static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5710 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005711 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005712 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005713 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005714{
5715 struct extent_map *em;
5716 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005717 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005718 u64 stripe_offset;
5719 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005720 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005721 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005722 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005723 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005724 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005725 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005726 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005727 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005728 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5729 int dev_replace_is_ongoing = 0;
5730 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005731 int patch_the_first_stripe_for_dev_replace = 0;
5732 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005733 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005734
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005735 if (op == BTRFS_MAP_DISCARD)
5736 return __btrfs_map_block_for_discard(fs_info, logical,
5737 *length, bbio_ret);
5738
Liu Bo592d92e2017-03-14 13:33:55 -07005739 em = get_chunk_map(fs_info, logical, *length);
5740 if (IS_ERR(em))
5741 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005742
Jeff Mahoney95617d62015-06-03 10:55:48 -04005743 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005744 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005745
David Woodhouse53b381b2013-01-29 18:40:14 -05005746 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005747 stripe_nr = offset;
5748 /*
5749 * stripe_nr counts the total number of stripes we have to stride
5750 * to get to this block
5751 */
David Sterba47c57132015-02-20 18:43:47 +01005752 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005753
David Woodhouse53b381b2013-01-29 18:40:14 -05005754 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005755 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005756 btrfs_crit(fs_info,
5757 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005758 stripe_offset, offset, em->start, logical,
5759 stripe_len);
5760 free_extent_map(em);
5761 return -EINVAL;
5762 }
Chris Mason593060d2008-03-25 16:50:33 -04005763
5764 /* stripe_offset is the offset of this block in its stripe*/
5765 stripe_offset = offset - stripe_offset;
5766
David Woodhouse53b381b2013-01-29 18:40:14 -05005767 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005768 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005769 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5770 raid56_full_stripe_start = offset;
5771
5772 /* allow a write of a full stripe, but make sure we don't
5773 * allow straddling of stripes
5774 */
David Sterba47c57132015-02-20 18:43:47 +01005775 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5776 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005777 raid56_full_stripe_start *= full_stripe_len;
5778 }
5779
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005780 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005781 u64 max_len;
5782 /* For writes to RAID[56], allow a full stripeset across all disks.
5783 For other RAID types and for RAID[56] reads, just allow a single
5784 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005785 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005786 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005787 max_len = stripe_len * nr_data_stripes(map) -
5788 (offset - raid56_full_stripe_start);
5789 } else {
5790 /* we limit the length of each bio to what fits in a stripe */
5791 max_len = stripe_len - stripe_offset;
5792 }
5793 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005794 } else {
5795 *length = em->len - offset;
5796 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005797
David Woodhouse53b381b2013-01-29 18:40:14 -05005798 /* This is for when we're called from btrfs_merge_bio_hook() and all
5799 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005800 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005801 goto out;
5802
Liu Bo73beece2015-07-17 16:49:19 +08005803 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005804 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5805 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005806 btrfs_dev_replace_unlock(dev_replace, 0);
5807 else
5808 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005809
Stefan Behrensad6d6202012-11-06 15:06:47 +01005810 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005811 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005812 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5813 dev_replace->srcdev->devid,
5814 &mirror_num,
5815 &physical_to_patch_in_first_stripe);
5816 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005817 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005818 else
5819 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005820 } else if (mirror_num > map->num_stripes) {
5821 mirror_num = 0;
5822 }
5823
Chris Masonf2d8d742008-04-21 10:03:05 -04005824 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005825 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005826 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005827 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5828 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005829 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005830 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005831 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005832 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005833 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005834 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005835 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005836 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005837 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005838 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005839 current->pid % map->num_stripes,
5840 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005841 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005842 }
Chris Mason2fff7342008-04-29 14:12:09 -04005843
Chris Mason611f0e02008-04-03 16:29:03 -04005844 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005845 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005846 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005847 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005848 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005849 } else {
5850 mirror_num = 1;
5851 }
Chris Mason2fff7342008-04-29 14:12:09 -04005852
Chris Mason321aecc2008-04-16 10:49:51 -04005853 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005854 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005855
David Sterba47c57132015-02-20 18:43:47 +01005856 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005857 stripe_index *= map->sub_stripes;
5858
Anand Jainde483732017-10-12 16:43:00 +08005859 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005860 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005861 else if (mirror_num)
5862 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005863 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005864 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005865 stripe_index = find_live_mirror(fs_info, map,
5866 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005867 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005868 current->pid % map->sub_stripes,
5869 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005870 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005871 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005872
Zhao Leiffe2d202015-01-20 15:11:44 +08005873 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005874 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005875 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005876 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005877 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005878
5879 /* RAID[56] write or recovery. Return all stripes */
5880 num_stripes = map->num_stripes;
5881 max_errors = nr_parity_stripes(map);
5882
David Woodhouse53b381b2013-01-29 18:40:14 -05005883 *length = map->stripe_len;
5884 stripe_index = 0;
5885 stripe_offset = 0;
5886 } else {
5887 /*
5888 * Mirror #0 or #1 means the original data block.
5889 * Mirror #2 is RAID5 parity block.
5890 * Mirror #3 is RAID6 Q block.
5891 */
David Sterba47c57132015-02-20 18:43:47 +01005892 stripe_nr = div_u64_rem(stripe_nr,
5893 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005894 if (mirror_num > 1)
5895 stripe_index = nr_data_stripes(map) +
5896 mirror_num - 2;
5897
5898 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005899 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5900 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005901 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005902 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005903 }
Chris Mason8790d502008-04-03 16:29:03 -04005904 } else {
5905 /*
David Sterba47c57132015-02-20 18:43:47 +01005906 * after this, stripe_nr is the number of stripes on this
5907 * device we have to walk to find the data, and stripe_index is
5908 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005909 */
David Sterba47c57132015-02-20 18:43:47 +01005910 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5911 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005912 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005913 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005914 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005915 btrfs_crit(fs_info,
5916 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005917 stripe_index, map->num_stripes);
5918 ret = -EINVAL;
5919 goto out;
5920 }
Chris Mason593060d2008-03-25 16:50:33 -04005921
Stefan Behrens472262f2012-11-06 14:43:46 +01005922 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005923 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005924 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005925 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005926 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005927 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005928 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005929 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005930
Zhao Lei6e9606d2015-01-20 15:11:34 +08005931 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005932 if (!bbio) {
5933 ret = -ENOMEM;
5934 goto out;
5935 }
Liu Bo6fad8232017-03-14 13:33:59 -07005936 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005937 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005938
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005939 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005940 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5941 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005942 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005943 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005944
5945 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5946 sizeof(struct btrfs_bio_stripe) *
5947 num_alloc_stripes +
5948 sizeof(int) * tgtdev_indexes);
5949
5950 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005951 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005952
5953 /* Fill in the logical address of each stripe */
5954 tmp = stripe_nr * nr_data_stripes(map);
5955 for (i = 0; i < nr_data_stripes(map); i++)
5956 bbio->raid_map[(i+rot) % num_stripes] =
5957 em->start + (tmp + i) * map->stripe_len;
5958
5959 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5960 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5961 bbio->raid_map[(i+rot+1) % num_stripes] =
5962 RAID6_Q_STRIPE;
5963 }
5964
Li Zefanec9ef7a2011-12-01 14:06:42 +08005965
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005966 for (i = 0; i < num_stripes; i++) {
5967 bbio->stripes[i].physical =
5968 map->stripes[stripe_index].physical +
5969 stripe_offset +
5970 stripe_nr * map->stripe_len;
5971 bbio->stripes[i].dev =
5972 map->stripes[stripe_index].dev;
5973 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005974 }
Li Zefande11cc12011-12-01 12:55:47 +08005975
Liu Bo2b19a1f2017-03-14 13:34:00 -07005976 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005977 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005978
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005979 if (bbio->raid_map)
5980 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005981
Liu Bo73c0f222017-03-14 13:33:58 -07005982 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005983 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005984 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5985 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005986 }
5987
Li Zefande11cc12011-12-01 12:55:47 +08005988 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005989 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005990 bbio->num_stripes = num_stripes;
5991 bbio->max_errors = max_errors;
5992 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005993
5994 /*
5995 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5996 * mirror_num == num_stripes + 1 && dev_replace target drive is
5997 * available as a mirror
5998 */
5999 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
6000 WARN_ON(num_stripes > 1);
6001 bbio->stripes[0].dev = dev_replace->tgtdev;
6002 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
6003 bbio->mirror_num = map->num_stripes + 1;
6004 }
Chris Masoncea9e442008-04-09 16:28:12 -04006005out:
Liu Bo73beece2015-07-17 16:49:19 +08006006 if (dev_replace_is_ongoing) {
6007 btrfs_dev_replace_clear_lock_blocking(dev_replace);
6008 btrfs_dev_replace_unlock(dev_replace, 0);
6009 }
Chris Mason0b86a832008-03-24 15:01:56 -04006010 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08006011 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04006012}
6013
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006014int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04006015 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02006016 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04006017{
Mike Christieb3d3fa52016-06-05 14:31:53 -05006018 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006019 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04006020}
6021
Miao Xieaf8e2d12014-10-23 14:42:50 +08006022/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02006023int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08006024 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02006025 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08006026{
David Sterba825ad4c2017-03-28 14:45:22 +02006027 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08006028}
6029
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006030int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05006031 u64 chunk_start, u64 physical, u64 devid,
6032 u64 **logical, int *naddrs, int *stripe_len)
6033{
Yan Zhenga512bbf2008-12-08 16:46:26 -05006034 struct extent_map *em;
6035 struct map_lookup *map;
6036 u64 *buf;
6037 u64 bytenr;
6038 u64 length;
6039 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006040 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006041 int i, j, nr = 0;
6042
Liu Bo592d92e2017-03-14 13:33:55 -07006043 em = get_chunk_map(fs_info, chunk_start, 1);
6044 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04006045 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04006046
Jeff Mahoney95617d62015-06-03 10:55:48 -04006047 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006048 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05006049 rmap_len = map->stripe_len;
6050
Yan Zhenga512bbf2008-12-08 16:46:26 -05006051 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006052 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006053 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006054 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006055 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006056 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006057 rmap_len = map->stripe_len * nr_data_stripes(map);
6058 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006059
David Sterba31e818f2015-02-20 18:00:26 +01006060 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006061 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006062
6063 for (i = 0; i < map->num_stripes; i++) {
6064 if (devid && map->stripes[i].dev->devid != devid)
6065 continue;
6066 if (map->stripes[i].physical > physical ||
6067 map->stripes[i].physical + length <= physical)
6068 continue;
6069
6070 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006071 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006072
6073 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6074 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006075 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006076 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6077 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006078 } /* else if RAID[56], multiply by nr_data_stripes().
6079 * Alternatively, just use rmap_len below instead of
6080 * map->stripe_len */
6081
6082 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006083 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006084 for (j = 0; j < nr; j++) {
6085 if (buf[j] == bytenr)
6086 break;
6087 }
Chris Mason934d3752008-12-08 16:43:10 -05006088 if (j == nr) {
6089 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006090 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006091 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006092 }
6093
Yan Zhenga512bbf2008-12-08 16:46:26 -05006094 *logical = buf;
6095 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006096 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006097
6098 free_extent_map(em);
6099 return 0;
6100}
6101
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006102static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006103{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006104 bio->bi_private = bbio->private;
6105 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006106 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006107
Zhao Lei6e9606d2015-01-20 15:11:34 +08006108 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006109}
6110
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006111static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006112{
Chris Mason9be33952013-05-17 18:30:14 -04006113 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006114 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006115
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006116 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006117 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006118 if (bio->bi_status == BLK_STS_IOERR ||
6119 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006120 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006121 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006122 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006123
6124 BUG_ON(stripe_index >= bbio->num_stripes);
6125 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006126 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006127 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006128 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006129 BTRFS_DEV_STAT_WRITE_ERRS);
6130 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006131 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006132 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006133 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006134 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006135 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006136 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006137 }
6138 }
Chris Mason8790d502008-04-03 16:29:03 -04006139
Jan Schmidta1d3c472011-08-04 17:15:33 +02006140 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006141 is_orig_bio = 1;
6142
Miao Xiec404e0d2014-01-30 16:46:55 +08006143 btrfs_bio_counter_dec(bbio->fs_info);
6144
Jan Schmidta1d3c472011-08-04 17:15:33 +02006145 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006146 if (!is_orig_bio) {
6147 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006148 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006149 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006150
Chris Mason9be33952013-05-17 18:30:14 -04006151 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006152 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006153 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006154 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006155 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006156 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006157 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006158 /*
6159 * this bio is actually up to date, we didn't
6160 * go over the max number of errors
6161 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006162 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006163 }
Miao Xiec55f1392014-06-19 10:42:54 +08006164
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006165 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006166 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006167 bio_put(bio);
6168 }
Chris Mason8790d502008-04-03 16:29:03 -04006169}
6170
Chris Mason8b712842008-06-11 16:50:36 -04006171/*
6172 * see run_scheduled_bios for a description of why bios are collected for
6173 * async submit.
6174 *
6175 * This will add one bio to the pending list for a device and make sure
6176 * the work struct is scheduled.
6177 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006178static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006179 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006180{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006181 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006182 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006183 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006184
Anand Jaine6e674b2017-12-04 12:54:54 +08006185 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
6186 !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006187 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006188 return;
6189 }
6190
Chris Mason8b712842008-06-11 16:50:36 -04006191 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006192 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006193 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006194 return;
Chris Mason8b712842008-06-11 16:50:36 -04006195 }
6196
Chris Mason492bb6de2008-07-31 16:29:02 -04006197 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006198 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006199
6200 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006201 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006202 pending_bios = &device->pending_sync_bios;
6203 else
6204 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006205
Chris Masonffbd5172009-04-20 15:50:09 -04006206 if (pending_bios->tail)
6207 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006208
Chris Masonffbd5172009-04-20 15:50:09 -04006209 pending_bios->tail = bio;
6210 if (!pending_bios->head)
6211 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006212 if (device->running_pending)
6213 should_queue = 0;
6214
6215 spin_unlock(&device->io_lock);
6216
6217 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006218 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006219}
6220
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006221static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6222 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006223{
6224 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006225 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006226
6227 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006228 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006229 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006230 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006231#ifdef DEBUG
6232 {
6233 struct rcu_string *name;
6234
6235 rcu_read_lock();
6236 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006237 btrfs_debug(fs_info,
6238 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6239 bio_op(bio), bio->bi_opf,
6240 (u64)bio->bi_iter.bi_sector,
6241 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6242 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006243 rcu_read_unlock();
6244 }
6245#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006246 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006247
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006248 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006249
Josef Bacikde1ee922012-10-19 16:50:56 -04006250 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006251 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006252 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006253 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006254}
6255
Josef Bacikde1ee922012-10-19 16:50:56 -04006256static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6257{
6258 atomic_inc(&bbio->error);
6259 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006260 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006261 WARN_ON(bio != bbio->orig_bio);
6262
Chris Mason9be33952013-05-17 18:30:14 -04006263 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006264 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006265 if (atomic_read(&bbio->error) > bbio->max_errors)
6266 bio->bi_status = BLK_STS_IOERR;
6267 else
6268 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006269 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006270 }
6271}
6272
Omar Sandoval58efbc92017-08-22 23:45:59 -07006273blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6274 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006275{
Chris Mason0b86a832008-03-24 15:01:56 -04006276 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006277 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006278 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006279 u64 length = 0;
6280 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006281 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006282 int dev_nr;
6283 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006284 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006285
Kent Overstreet4f024f32013-10-11 15:44:27 -07006286 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006287 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006288
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006289 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006290 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006291 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006292 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006293 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006294 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006295 }
Chris Masoncea9e442008-04-09 16:28:12 -04006296
Jan Schmidta1d3c472011-08-04 17:15:33 +02006297 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006298 bbio->orig_bio = first_bio;
6299 bbio->private = first_bio->bi_private;
6300 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006301 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006302 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6303
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006304 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006305 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006306 /* In this case, map_length has been set to the length of
6307 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006308 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006309 ret = raid56_parity_write(fs_info, bio, bbio,
6310 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006311 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006312 ret = raid56_parity_recover(fs_info, bio, bbio,
6313 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006314 }
Miao Xie42452152014-11-25 16:39:28 +08006315
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006316 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006317 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006318 }
6319
Chris Masoncea9e442008-04-09 16:28:12 -04006320 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006321 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006322 "mapping failed logical %llu bio len %llu len %llu",
6323 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006324 BUG();
6325 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006326
Zhao Lei08da7572015-02-12 15:42:16 +08006327 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006328 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006329 if (!dev || !dev->bdev ||
Anand Jainebbede42017-12-04 12:54:52 +08006330 (bio_op(first_bio) == REQ_OP_WRITE &&
6331 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006332 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006333 continue;
6334 }
6335
David Sterba3aa8e072017-06-02 17:38:30 +02006336 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006337 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006338 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006339 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006340
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006341 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6342 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006343 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006344 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006345 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006346}
6347
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006348struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006349 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006350{
Yan Zheng2b820322008-11-17 21:11:30 -05006351 struct btrfs_device *device;
6352 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006353
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006354 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006355 while (cur_devices) {
6356 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006357 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006358 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006359 if (device)
6360 return device;
6361 }
6362 cur_devices = cur_devices->seed;
6363 }
6364 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006365}
6366
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006367static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006368 u64 devid, u8 *dev_uuid)
6369{
6370 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006371
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006372 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6373 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006374 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006375
6376 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006377 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006378 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006379
Anand Jaine6e674b2017-12-04 12:54:54 +08006380 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006381 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006382
Chris Masondfe25022008-05-13 13:46:40 -04006383 return device;
6384}
6385
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006386/**
6387 * btrfs_alloc_device - allocate struct btrfs_device
6388 * @fs_info: used only for generating a new devid, can be NULL if
6389 * devid is provided (i.e. @devid != NULL).
6390 * @devid: a pointer to devid for this device. If NULL a new devid
6391 * is generated.
6392 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6393 * is generated.
6394 *
6395 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006396 * on error. Returned struct is not linked onto any lists and must be
6397 * destroyed with free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006398 */
6399struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6400 const u64 *devid,
6401 const u8 *uuid)
6402{
6403 struct btrfs_device *dev;
6404 u64 tmp;
6405
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306406 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006407 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006408
6409 dev = __alloc_device();
6410 if (IS_ERR(dev))
6411 return dev;
6412
6413 if (devid)
6414 tmp = *devid;
6415 else {
6416 int ret;
6417
6418 ret = find_next_devid(fs_info, &tmp);
6419 if (ret) {
David Sterba55de4802017-10-30 18:55:47 +01006420 free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006421 return ERR_PTR(ret);
6422 }
6423 }
6424 dev->devid = tmp;
6425
6426 if (uuid)
6427 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6428 else
6429 generate_random_uuid(dev->uuid);
6430
Liu Bo9e0af232014-08-15 23:36:53 +08006431 btrfs_init_work(&dev->work, btrfs_submit_helper,
6432 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006433
6434 return dev;
6435}
6436
Liu Boe06cd3d2016-06-03 12:05:15 -07006437/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006438static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006439 struct extent_buffer *leaf,
6440 struct btrfs_chunk *chunk, u64 logical)
6441{
6442 u64 length;
6443 u64 stripe_len;
6444 u16 num_stripes;
6445 u16 sub_stripes;
6446 u64 type;
6447
6448 length = btrfs_chunk_length(leaf, chunk);
6449 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6450 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6451 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6452 type = btrfs_chunk_type(leaf, chunk);
6453
6454 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006455 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006456 num_stripes);
6457 return -EIO;
6458 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006459 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6460 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006461 return -EIO;
6462 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006463 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6464 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006465 btrfs_chunk_sector_size(leaf, chunk));
6466 return -EIO;
6467 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006468 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6469 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006470 return -EIO;
6471 }
6472 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006473 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006474 stripe_len);
6475 return -EIO;
6476 }
6477 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6478 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006479 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006480 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6481 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6482 btrfs_chunk_type(leaf, chunk));
6483 return -EIO;
6484 }
6485 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6486 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6487 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6488 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6489 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6490 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6491 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006492 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006493 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6494 num_stripes, sub_stripes,
6495 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6496 return -EIO;
6497 }
6498
6499 return 0;
6500}
6501
Anand Jain5a2b8e62017-10-09 11:07:45 +08006502static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006503 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006504{
Anand Jain2b902df2017-10-09 11:07:46 +08006505 if (error)
6506 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6507 devid, uuid);
6508 else
6509 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6510 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006511}
6512
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006513static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006514 struct extent_buffer *leaf,
6515 struct btrfs_chunk *chunk)
6516{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006517 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006518 struct map_lookup *map;
6519 struct extent_map *em;
6520 u64 logical;
6521 u64 length;
6522 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006523 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006524 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006525 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006526 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006527
Chris Masone17cade2008-04-15 15:41:47 -04006528 logical = key->offset;
6529 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006530 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006531
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006532 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006533 if (ret)
6534 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006535
Chris Mason890871b2009-09-02 16:24:52 -04006536 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006537 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006538 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006539
6540 /* already mapped? */
6541 if (em && em->start <= logical && em->start + em->len > logical) {
6542 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006543 return 0;
6544 } else if (em) {
6545 free_extent_map(em);
6546 }
Chris Mason0b86a832008-03-24 15:01:56 -04006547
David Sterba172ddd62011-04-21 00:48:27 +02006548 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006549 if (!em)
6550 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006551 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006552 if (!map) {
6553 free_extent_map(em);
6554 return -ENOMEM;
6555 }
6556
Wang Shilong298a8f92014-06-19 10:42:52 +08006557 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006558 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006559 em->start = logical;
6560 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006561 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006562 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006563 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006564
Chris Mason593060d2008-03-25 16:50:33 -04006565 map->num_stripes = num_stripes;
6566 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6567 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006568 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6569 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006570 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006571 for (i = 0; i < num_stripes; i++) {
6572 map->stripes[i].physical =
6573 btrfs_stripe_offset_nr(leaf, chunk, i);
6574 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006575 read_extent_buffer(leaf, uuid, (unsigned long)
6576 btrfs_stripe_dev_uuid_nr(chunk, i),
6577 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006578 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006579 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006580 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006581 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006582 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006583 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006584 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006585 }
Chris Masondfe25022008-05-13 13:46:40 -04006586 if (!map->stripes[i].dev) {
6587 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006588 add_missing_dev(fs_info->fs_devices, devid,
6589 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006590 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006591 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006592 btrfs_err(fs_info,
6593 "failed to init missing dev %llu: %ld",
6594 devid, PTR_ERR(map->stripes[i].dev));
6595 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006596 }
Anand Jain2b902df2017-10-09 11:07:46 +08006597 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006598 }
Anand Jaine12c9622017-12-04 12:54:53 +08006599 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6600 &(map->stripes[i].dev->dev_state));
6601
Chris Mason0b86a832008-03-24 15:01:56 -04006602 }
6603
Chris Mason890871b2009-09-02 16:24:52 -04006604 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006605 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006606 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006607 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006608 free_extent_map(em);
6609
6610 return 0;
6611}
6612
Jeff Mahoney143bede2012-03-01 14:56:26 +01006613static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006614 struct btrfs_dev_item *dev_item,
6615 struct btrfs_device *device)
6616{
6617 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006618
6619 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006620 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6621 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006622 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006623 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006624 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006625 device->type = btrfs_device_type(leaf, dev_item);
6626 device->io_align = btrfs_device_io_align(leaf, dev_item);
6627 device->io_width = btrfs_device_io_width(leaf, dev_item);
6628 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006629 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006630 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006631
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006632 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006633 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006634}
6635
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006636static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006637 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006638{
6639 struct btrfs_fs_devices *fs_devices;
6640 int ret;
6641
Li Zefanb367e472011-12-07 11:38:24 +08006642 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006643 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006644
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006645 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006646 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006647 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006648 return fs_devices;
6649
Yan Zheng2b820322008-11-17 21:11:30 -05006650 fs_devices = fs_devices->seed;
6651 }
6652
6653 fs_devices = find_fsid(fsid);
6654 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006655 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006656 return ERR_PTR(-ENOENT);
6657
6658 fs_devices = alloc_fs_devices(fsid);
6659 if (IS_ERR(fs_devices))
6660 return fs_devices;
6661
6662 fs_devices->seeding = 1;
6663 fs_devices->opened = 1;
6664 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006665 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006666
6667 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006668 if (IS_ERR(fs_devices))
6669 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006670
Christoph Hellwig97288f22008-12-02 06:36:09 -05006671 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006672 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006673 if (ret) {
6674 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006675 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006676 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006677 }
Yan Zheng2b820322008-11-17 21:11:30 -05006678
6679 if (!fs_devices->seeding) {
6680 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006681 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006682 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006683 goto out;
6684 }
6685
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006686 fs_devices->seed = fs_info->fs_devices->seed;
6687 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006688out:
Miao Xie5f375832014-09-03 21:35:46 +08006689 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006690}
6691
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006692static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006693 struct extent_buffer *leaf,
6694 struct btrfs_dev_item *dev_item)
6695{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006696 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006697 struct btrfs_device *device;
6698 u64 devid;
6699 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006700 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006701 u8 dev_uuid[BTRFS_UUID_SIZE];
6702
Chris Mason0b86a832008-03-24 15:01:56 -04006703 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006704 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006705 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006706 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006707 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006708
Anand Jain44880fd2017-07-29 17:50:09 +08006709 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006710 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006711 if (IS_ERR(fs_devices))
6712 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006713 }
6714
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006715 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006716 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006717 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006718 btrfs_report_missing_device(fs_info, devid,
6719 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006720 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006721 }
Yan Zheng2b820322008-11-17 21:11:30 -05006722
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006723 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006724 if (IS_ERR(device)) {
6725 btrfs_err(fs_info,
6726 "failed to add missing dev %llu: %ld",
6727 devid, PTR_ERR(device));
6728 return PTR_ERR(device);
6729 }
Anand Jain2b902df2017-10-09 11:07:46 +08006730 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006731 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006732 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006733 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6734 btrfs_report_missing_device(fs_info,
6735 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006736 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006737 }
6738 btrfs_report_missing_device(fs_info, devid,
6739 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006740 }
Miao Xie5f375832014-09-03 21:35:46 +08006741
Anand Jaine6e674b2017-12-04 12:54:54 +08006742 if (!device->bdev &&
6743 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006744 /*
6745 * this happens when a device that was properly setup
6746 * in the device info lists suddenly goes bad.
6747 * device->bdev is NULL, and so we have to set
6748 * device->missing to one here
6749 */
Miao Xie5f375832014-09-03 21:35:46 +08006750 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006751 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006752 }
Miao Xie5f375832014-09-03 21:35:46 +08006753
6754 /* Move the device to its own fs_devices */
6755 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006756 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6757 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006758
6759 list_move(&device->dev_list, &fs_devices->devices);
6760 device->fs_devices->num_devices--;
6761 fs_devices->num_devices++;
6762
6763 device->fs_devices->missing_devices--;
6764 fs_devices->missing_devices++;
6765
6766 device->fs_devices = fs_devices;
6767 }
Yan Zheng2b820322008-11-17 21:11:30 -05006768 }
6769
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006770 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006771 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006772 if (device->generation !=
6773 btrfs_device_generation(leaf, dev_item))
6774 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006775 }
Chris Mason0b86a832008-03-24 15:01:56 -04006776
6777 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006778 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006779 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006780 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006781 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006782 atomic64_add(device->total_bytes - device->bytes_used,
6783 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006784 }
Chris Mason0b86a832008-03-24 15:01:56 -04006785 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006786 return ret;
6787}
6788
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006789int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006790{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006791 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006792 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006793 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006794 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006795 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006796 u8 *array_ptr;
6797 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006798 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006799 u32 num_stripes;
6800 u32 array_size;
6801 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006802 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006803 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006804 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006805
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006806 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006807 /*
6808 * This will create extent buffer of nodesize, superblock size is
6809 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6810 * overallocate but we can keep it as-is, only the first page is used.
6811 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006812 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006813 if (IS_ERR(sb))
6814 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006815 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006816 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006817 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006818 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006819 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006820 * pages up-to-date when the page is larger: extent does not cover the
6821 * whole page and consequently check_page_uptodate does not find all
6822 * the page's extents up-to-date (the hole beyond sb),
6823 * write_extent_buffer then triggers a WARN_ON.
6824 *
6825 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6826 * but sb spans only this function. Add an explicit SetPageUptodate call
6827 * to silence the warning eg. on PowerPC 64.
6828 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006829 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006830 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006831
Chris Masona061fc82008-05-07 11:43:44 -04006832 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006833 array_size = btrfs_super_sys_array_size(super_copy);
6834
David Sterba1ffb22c2014-10-31 19:02:42 +01006835 array_ptr = super_copy->sys_chunk_array;
6836 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6837 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006838
David Sterba1ffb22c2014-10-31 19:02:42 +01006839 while (cur_offset < array_size) {
6840 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006841 len = sizeof(*disk_key);
6842 if (cur_offset + len > array_size)
6843 goto out_short_read;
6844
Chris Mason0b86a832008-03-24 15:01:56 -04006845 btrfs_disk_key_to_cpu(&key, disk_key);
6846
David Sterba1ffb22c2014-10-31 19:02:42 +01006847 array_ptr += len;
6848 sb_array_offset += len;
6849 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006850
Chris Mason0d81ba52008-03-24 15:02:07 -04006851 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006852 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006853 /*
6854 * At least one btrfs_chunk with one stripe must be
6855 * present, exact stripe count check comes afterwards
6856 */
6857 len = btrfs_chunk_item_size(1);
6858 if (cur_offset + len > array_size)
6859 goto out_short_read;
6860
6861 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006862 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006863 btrfs_err(fs_info,
6864 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006865 num_stripes, cur_offset);
6866 ret = -EIO;
6867 break;
6868 }
6869
Liu Boe06cd3d2016-06-03 12:05:15 -07006870 type = btrfs_chunk_type(sb, chunk);
6871 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006872 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006873 "invalid chunk type %llu in sys_array at offset %u",
6874 type, cur_offset);
6875 ret = -EIO;
6876 break;
6877 }
6878
David Sterbae3540ea2014-11-05 15:24:51 +01006879 len = btrfs_chunk_item_size(num_stripes);
6880 if (cur_offset + len > array_size)
6881 goto out_short_read;
6882
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006883 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006884 if (ret)
6885 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006886 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006887 btrfs_err(fs_info,
6888 "unexpected item type %u in sys_array at offset %u",
6889 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006890 ret = -EIO;
6891 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006892 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006893 array_ptr += len;
6894 sb_array_offset += len;
6895 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006896 }
Liu Bod8651772016-06-03 17:41:42 -07006897 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006898 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006899 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006900
6901out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006902 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006903 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006904 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006905 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006906 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006907}
6908
Qu Wenruo21634a12017-03-09 09:34:36 +08006909/*
6910 * Check if all chunks in the fs are OK for read-write degraded mount
6911 *
Anand Jain6528b992017-12-18 17:08:59 +08006912 * If the @failing_dev is specified, it's accounted as missing.
6913 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006914 * Return true if all chunks meet the minimal RW mount requirements.
6915 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6916 */
Anand Jain6528b992017-12-18 17:08:59 +08006917bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6918 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006919{
6920 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6921 struct extent_map *em;
6922 u64 next_start = 0;
6923 bool ret = true;
6924
6925 read_lock(&map_tree->map_tree.lock);
6926 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6927 read_unlock(&map_tree->map_tree.lock);
6928 /* No chunk at all? Return false anyway */
6929 if (!em) {
6930 ret = false;
6931 goto out;
6932 }
6933 while (em) {
6934 struct map_lookup *map;
6935 int missing = 0;
6936 int max_tolerated;
6937 int i;
6938
6939 map = em->map_lookup;
6940 max_tolerated =
6941 btrfs_get_num_tolerated_disk_barrier_failures(
6942 map->type);
6943 for (i = 0; i < map->num_stripes; i++) {
6944 struct btrfs_device *dev = map->stripes[i].dev;
6945
Anand Jaine6e674b2017-12-04 12:54:54 +08006946 if (!dev || !dev->bdev ||
6947 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006948 dev->last_flush_error)
6949 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006950 else if (failing_dev && failing_dev == dev)
6951 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006952 }
6953 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006954 if (!failing_dev)
6955 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006956 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6957 em->start, missing, max_tolerated);
6958 free_extent_map(em);
6959 ret = false;
6960 goto out;
6961 }
6962 next_start = extent_map_end(em);
6963 free_extent_map(em);
6964
6965 read_lock(&map_tree->map_tree.lock);
6966 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6967 (u64)(-1) - next_start);
6968 read_unlock(&map_tree->map_tree.lock);
6969 }
6970out:
6971 return ret;
6972}
6973
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006974int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006975{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006976 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006977 struct btrfs_path *path;
6978 struct extent_buffer *leaf;
6979 struct btrfs_key key;
6980 struct btrfs_key found_key;
6981 int ret;
6982 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006983 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006984
Chris Mason0b86a832008-03-24 15:01:56 -04006985 path = btrfs_alloc_path();
6986 if (!path)
6987 return -ENOMEM;
6988
Li Zefanb367e472011-12-07 11:38:24 +08006989 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006990 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006991
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006992 /*
6993 * Read all device items, and then all the chunk items. All
6994 * device items are found before any chunk item (their object id
6995 * is smaller than the lowest possible object id for a chunk
6996 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006997 */
6998 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6999 key.offset = 0;
7000 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04007001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00007002 if (ret < 0)
7003 goto error;
Chris Masond3977122009-01-05 21:25:51 -05007004 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04007005 leaf = path->nodes[0];
7006 slot = path->slots[0];
7007 if (slot >= btrfs_header_nritems(leaf)) {
7008 ret = btrfs_next_leaf(root, path);
7009 if (ret == 0)
7010 continue;
7011 if (ret < 0)
7012 goto error;
7013 break;
7014 }
7015 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007016 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
7017 struct btrfs_dev_item *dev_item;
7018 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04007019 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007020 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01007021 if (ret)
7022 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07007023 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04007024 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
7025 struct btrfs_chunk *chunk;
7026 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007027 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05007028 if (ret)
7029 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04007030 }
7031 path->slots[0]++;
7032 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07007033
7034 /*
7035 * After loading chunk tree, we've got all device information,
7036 * do another round of validation checks.
7037 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007038 if (total_dev != fs_info->fs_devices->total_devices) {
7039 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007040 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007041 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07007042 total_dev);
7043 ret = -EINVAL;
7044 goto error;
7045 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007046 if (btrfs_super_total_bytes(fs_info->super_copy) <
7047 fs_info->fs_devices->total_rw_bytes) {
7048 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007049 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007050 btrfs_super_total_bytes(fs_info->super_copy),
7051 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007052 ret = -EINVAL;
7053 goto error;
7054 }
Chris Mason0b86a832008-03-24 15:01:56 -04007055 ret = 0;
7056error:
David Sterba34441362016-10-04 19:34:27 +02007057 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007058 mutex_unlock(&uuid_mutex);
7059
Yan Zheng2b820322008-11-17 21:11:30 -05007060 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007061 return ret;
7062}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007063
Miao Xiecb517ea2013-05-15 07:48:19 +00007064void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7065{
7066 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7067 struct btrfs_device *device;
7068
Liu Bo29cc83f2014-05-11 23:14:59 +08007069 while (fs_devices) {
7070 mutex_lock(&fs_devices->device_list_mutex);
7071 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007072 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007073 mutex_unlock(&fs_devices->device_list_mutex);
7074
7075 fs_devices = fs_devices->seed;
7076 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007077}
7078
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007079static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7080{
7081 int i;
7082
7083 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7084 btrfs_dev_stat_reset(dev, i);
7085}
7086
7087int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7088{
7089 struct btrfs_key key;
7090 struct btrfs_key found_key;
7091 struct btrfs_root *dev_root = fs_info->dev_root;
7092 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7093 struct extent_buffer *eb;
7094 int slot;
7095 int ret = 0;
7096 struct btrfs_device *device;
7097 struct btrfs_path *path = NULL;
7098 int i;
7099
7100 path = btrfs_alloc_path();
7101 if (!path) {
7102 ret = -ENOMEM;
7103 goto out;
7104 }
7105
7106 mutex_lock(&fs_devices->device_list_mutex);
7107 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7108 int item_size;
7109 struct btrfs_dev_stats_item *ptr;
7110
David Sterba242e2952016-01-25 17:51:31 +01007111 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7112 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007113 key.offset = device->devid;
7114 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7115 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007116 __btrfs_reset_dev_stats(device);
7117 device->dev_stats_valid = 1;
7118 btrfs_release_path(path);
7119 continue;
7120 }
7121 slot = path->slots[0];
7122 eb = path->nodes[0];
7123 btrfs_item_key_to_cpu(eb, &found_key, slot);
7124 item_size = btrfs_item_size_nr(eb, slot);
7125
7126 ptr = btrfs_item_ptr(eb, slot,
7127 struct btrfs_dev_stats_item);
7128
7129 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7130 if (item_size >= (1 + i) * sizeof(__le64))
7131 btrfs_dev_stat_set(device, i,
7132 btrfs_dev_stats_value(eb, ptr, i));
7133 else
7134 btrfs_dev_stat_reset(device, i);
7135 }
7136
7137 device->dev_stats_valid = 1;
7138 btrfs_dev_stat_print_on_load(device);
7139 btrfs_release_path(path);
7140 }
7141 mutex_unlock(&fs_devices->device_list_mutex);
7142
7143out:
7144 btrfs_free_path(path);
7145 return ret < 0 ? ret : 0;
7146}
7147
7148static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007149 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007150 struct btrfs_device *device)
7151{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007152 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007153 struct btrfs_path *path;
7154 struct btrfs_key key;
7155 struct extent_buffer *eb;
7156 struct btrfs_dev_stats_item *ptr;
7157 int ret;
7158 int i;
7159
David Sterba242e2952016-01-25 17:51:31 +01007160 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7161 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007162 key.offset = device->devid;
7163
7164 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007165 if (!path)
7166 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007167 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7168 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007169 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007170 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007171 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007172 goto out;
7173 }
7174
7175 if (ret == 0 &&
7176 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7177 /* need to delete old one and insert a new one */
7178 ret = btrfs_del_item(trans, dev_root, path);
7179 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007180 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007181 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007182 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007183 goto out;
7184 }
7185 ret = 1;
7186 }
7187
7188 if (ret == 1) {
7189 /* need to insert a new item */
7190 btrfs_release_path(path);
7191 ret = btrfs_insert_empty_item(trans, dev_root, path,
7192 &key, sizeof(*ptr));
7193 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007194 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007195 "insert dev_stats item for device %s failed %d",
7196 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007197 goto out;
7198 }
7199 }
7200
7201 eb = path->nodes[0];
7202 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7203 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7204 btrfs_set_dev_stats_value(eb, ptr, i,
7205 btrfs_dev_stat_read(device, i));
7206 btrfs_mark_buffer_dirty(eb);
7207
7208out:
7209 btrfs_free_path(path);
7210 return ret;
7211}
7212
7213/*
7214 * called from commit_transaction. Writes all changed device stats to disk.
7215 */
7216int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7217 struct btrfs_fs_info *fs_info)
7218{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007219 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7220 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007221 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007222 int ret = 0;
7223
7224 mutex_lock(&fs_devices->device_list_mutex);
7225 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007226 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7227 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007228 continue;
7229
Nikolay Borisov9deae962017-10-24 13:47:37 +03007230
7231 /*
7232 * There is a LOAD-LOAD control dependency between the value of
7233 * dev_stats_ccnt and updating the on-disk values which requires
7234 * reading the in-memory counters. Such control dependencies
7235 * require explicit read memory barriers.
7236 *
7237 * This memory barriers pairs with smp_mb__before_atomic in
7238 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7239 * barrier implied by atomic_xchg in
7240 * btrfs_dev_stats_read_and_reset
7241 */
7242 smp_rmb();
7243
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007244 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007245 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007246 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007247 }
7248 mutex_unlock(&fs_devices->device_list_mutex);
7249
7250 return ret;
7251}
7252
Stefan Behrens442a4f62012-05-25 16:06:08 +02007253void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7254{
7255 btrfs_dev_stat_inc(dev, index);
7256 btrfs_dev_stat_print_on_error(dev);
7257}
7258
Eric Sandeen48a3b632013-04-25 20:41:01 +00007259static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007260{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007261 if (!dev->dev_stats_valid)
7262 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007263 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007264 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007265 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007266 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7267 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7268 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007269 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7270 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007271}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007272
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007273static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7274{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007275 int i;
7276
7277 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7278 if (btrfs_dev_stat_read(dev, i) != 0)
7279 break;
7280 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7281 return; /* all values == 0, suppress message */
7282
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007283 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007284 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007285 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007286 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7287 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7288 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7289 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7290 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7291}
7292
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007293int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007294 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007295{
7296 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007297 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007298 int i;
7299
7300 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007301 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007302 mutex_unlock(&fs_devices->device_list_mutex);
7303
7304 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007305 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007306 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007307 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007308 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007309 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007310 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007311 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7312 if (stats->nr_items > i)
7313 stats->values[i] =
7314 btrfs_dev_stat_read_and_reset(dev, i);
7315 else
7316 btrfs_dev_stat_reset(dev, i);
7317 }
7318 } else {
7319 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7320 if (stats->nr_items > i)
7321 stats->values[i] = btrfs_dev_stat_read(dev, i);
7322 }
7323 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7324 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7325 return 0;
7326}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007327
David Sterbada353f62017-02-14 17:55:53 +01007328void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007329{
7330 struct buffer_head *bh;
7331 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007332 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007333
Anand Jain12b1c262015-08-14 18:32:59 +08007334 if (!bdev)
7335 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007336
Anand Jain12b1c262015-08-14 18:32:59 +08007337 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7338 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007339
Anand Jain12b1c262015-08-14 18:32:59 +08007340 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7341 continue;
7342
7343 disk_super = (struct btrfs_super_block *)bh->b_data;
7344
7345 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7346 set_buffer_dirty(bh);
7347 sync_dirty_buffer(bh);
7348 brelse(bh);
7349 }
7350
7351 /* Notify udev that device has changed */
7352 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7353
7354 /* Update ctime/mtime for device path for libblkid */
7355 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007356}
Miao Xie935e5cc2014-09-03 21:35:33 +08007357
7358/*
7359 * Update the size of all devices, which is used for writing out the
7360 * super blocks.
7361 */
7362void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7363{
7364 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7365 struct btrfs_device *curr, *next;
7366
7367 if (list_empty(&fs_devices->resized_devices))
7368 return;
7369
7370 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007371 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007372 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7373 resized_list) {
7374 list_del_init(&curr->resized_list);
7375 curr->commit_total_bytes = curr->disk_total_bytes;
7376 }
David Sterba34441362016-10-04 19:34:27 +02007377 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007378 mutex_unlock(&fs_devices->device_list_mutex);
7379}
Miao Xiece7213c2014-09-03 21:35:34 +08007380
7381/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007382void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007383{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007384 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007385 struct extent_map *em;
7386 struct map_lookup *map;
7387 struct btrfs_device *dev;
7388 int i;
7389
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007390 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007391 return;
7392
7393 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007394 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007395 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007396 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007397
7398 for (i = 0; i < map->num_stripes; i++) {
7399 dev = map->stripes[i].dev;
7400 dev->commit_bytes_used = dev->bytes_used;
7401 }
7402 }
David Sterba34441362016-10-04 19:34:27 +02007403 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007404}
Anand Jain5a13f432015-03-10 06:38:31 +08007405
7406void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7407{
7408 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7409 while (fs_devices) {
7410 fs_devices->fs_info = fs_info;
7411 fs_devices = fs_devices->seed;
7412 }
7413}
7414
7415void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7416{
7417 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7418 while (fs_devices) {
7419 fs_devices->fs_info = NULL;
7420 fs_devices = fs_devices->seed;
7421 }
7422}