blob: 20039d6252106cef489300187889203b5fa16fc3 [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>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Zhao Leiaf902042015-09-15 21:08:06 +080045const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
46 [BTRFS_RAID_RAID10] = {
47 .sub_stripes = 2,
48 .dev_stripes = 1,
49 .devs_max = 0, /* 0 == as many as possible */
50 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080051 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080052 .devs_increment = 2,
53 .ncopies = 2,
54 },
55 [BTRFS_RAID_RAID1] = {
56 .sub_stripes = 1,
57 .dev_stripes = 1,
58 .devs_max = 2,
59 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080060 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080061 .devs_increment = 2,
62 .ncopies = 2,
63 },
64 [BTRFS_RAID_DUP] = {
65 .sub_stripes = 1,
66 .dev_stripes = 2,
67 .devs_max = 1,
68 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080069 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080070 .devs_increment = 1,
71 .ncopies = 2,
72 },
73 [BTRFS_RAID_RAID0] = {
74 .sub_stripes = 1,
75 .dev_stripes = 1,
76 .devs_max = 0,
77 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080078 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080079 .devs_increment = 1,
80 .ncopies = 1,
81 },
82 [BTRFS_RAID_SINGLE] = {
83 .sub_stripes = 1,
84 .dev_stripes = 1,
85 .devs_max = 1,
86 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080087 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080088 .devs_increment = 1,
89 .ncopies = 1,
90 },
91 [BTRFS_RAID_RAID5] = {
92 .sub_stripes = 1,
93 .dev_stripes = 1,
94 .devs_max = 0,
95 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080096 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080097 .devs_increment = 1,
98 .ncopies = 2,
99 },
100 [BTRFS_RAID_RAID6] = {
101 .sub_stripes = 1,
102 .dev_stripes = 1,
103 .devs_max = 0,
104 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800105 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800106 .devs_increment = 1,
107 .ncopies = 3,
108 },
109};
110
Colin Ian Kingfb75d852016-01-19 00:05:28 +0000111const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
Zhao Leiaf902042015-09-15 21:08:06 +0800112 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
113 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
114 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
115 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
116 [BTRFS_RAID_SINGLE] = 0,
117 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
118 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
119};
120
David Sterba621292b2016-02-15 16:28:03 +0100121/*
122 * Table to convert BTRFS_RAID_* to the error code if minimum number of devices
123 * condition is not met. Zero means there's no corresponding
124 * BTRFS_ERROR_DEV_*_NOT_MET value.
125 */
126const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
127 [BTRFS_RAID_RAID10] = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
128 [BTRFS_RAID_RAID1] = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
129 [BTRFS_RAID_DUP] = 0,
130 [BTRFS_RAID_RAID0] = 0,
131 [BTRFS_RAID_SINGLE] = 0,
132 [BTRFS_RAID_RAID5] = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
133 [BTRFS_RAID_RAID6] = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
134};
135
Yan Zheng2b820322008-11-17 21:11:30 -0500136static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100137 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400138static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200139static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000140static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200141static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700142static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
143 enum btrfs_map_op op,
144 u64 logical, u64 *length,
145 struct btrfs_bio **bbio_ret,
146 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500147
David Sterba9c6b1c42017-06-16 22:30:00 +0200148/*
149 * Device locking
150 * ==============
151 *
152 * There are several mutexes that protect manipulation of devices and low-level
153 * structures like chunks but not block groups, extents or files
154 *
155 * uuid_mutex (global lock)
156 * ------------------------
157 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
158 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
159 * device) or requested by the device= mount option
160 *
161 * the mutex can be very coarse and can cover long-running operations
162 *
163 * protects: updates to fs_devices counters like missing devices, rw devices,
164 * seeding, structure cloning, openning/closing devices at mount/umount time
165 *
166 * global::fs_devs - add, remove, updates to the global list
167 *
168 * does not protect: manipulation of the fs_devices::devices list!
169 *
170 * btrfs_device::name - renames (write side), read is RCU
171 *
172 * fs_devices::device_list_mutex (per-fs, with RCU)
173 * ------------------------------------------------
174 * protects updates to fs_devices::devices, ie. adding and deleting
175 *
176 * simple list traversal with read-only actions can be done with RCU protection
177 *
178 * may be used to exclude some operations from running concurrently without any
179 * modifications to the list (see write_all_supers)
180 *
181 * volume_mutex
182 * ------------
183 * coarse lock owned by a mounted filesystem; used to exclude some operations
184 * that cannot run in parallel and affect the higher-level properties of the
185 * filesystem like: device add/deleting/resize/replace, or balance
186 *
187 * balance_mutex
188 * -------------
189 * protects balance structures (status, state) and context accessed from
190 * several places (internally, ioctl)
191 *
192 * chunk_mutex
193 * -----------
194 * protects chunks, adding or removing during allocation, trim or when a new
195 * device is added/removed
196 *
197 * cleaner_mutex
198 * -------------
199 * a big lock that is held by the cleaner thread and prevents running subvolume
200 * cleaning together with relocation or delayed iputs
201 *
202 *
203 * Lock nesting
204 * ============
205 *
206 * uuid_mutex
207 * volume_mutex
208 * device_list_mutex
209 * chunk_mutex
210 * balance_mutex
211 */
212
Miao Xie67a2c452014-09-03 21:35:43 +0800213DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400214static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800215struct list_head *btrfs_get_fs_uuids(void)
216{
217 return &fs_uuids;
218}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400219
David Sterba2dfeca92017-06-14 02:48:07 +0200220/*
221 * alloc_fs_devices - allocate struct btrfs_fs_devices
222 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
223 *
224 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
225 * The returned struct is not linked onto any lists and can be destroyed with
226 * kfree() right away.
227 */
228static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300229{
230 struct btrfs_fs_devices *fs_devs;
231
David Sterba78f2c9e2016-02-11 14:25:38 +0100232 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300233 if (!fs_devs)
234 return ERR_PTR(-ENOMEM);
235
236 mutex_init(&fs_devs->device_list_mutex);
237
238 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800239 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300240 INIT_LIST_HEAD(&fs_devs->alloc_list);
241 INIT_LIST_HEAD(&fs_devs->list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300242 if (fsid)
243 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300244
245 return fs_devs;
246}
247
David Sterba48dae9c2017-10-30 18:10:25 +0100248static void free_device(struct btrfs_device *device)
249{
250 rcu_string_free(device->name);
251 bio_put(device->flush_bio);
252 kfree(device);
253}
254
Yan Zhenge4404d62008-12-12 10:03:26 -0500255static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
256{
257 struct btrfs_device *device;
258 WARN_ON(fs_devices->opened);
259 while (!list_empty(&fs_devices->devices)) {
260 device = list_entry(fs_devices->devices.next,
261 struct btrfs_device, dev_list);
262 list_del(&device->dev_list);
David Sterba55de4802017-10-30 18:55:47 +0100263 free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500264 }
265 kfree(fs_devices);
266}
267
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000268static void btrfs_kobject_uevent(struct block_device *bdev,
269 enum kobject_action action)
270{
271 int ret;
272
273 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
274 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500275 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000276 action,
277 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
278 &disk_to_dev(bdev->bd_disk)->kobj);
279}
280
Jeff Mahoney143bede2012-03-01 14:56:26 +0100281void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400282{
283 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400284
Yan Zheng2b820322008-11-17 21:11:30 -0500285 while (!list_empty(&fs_uuids)) {
286 fs_devices = list_entry(fs_uuids.next,
287 struct btrfs_fs_devices, list);
288 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500289 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400290 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400291}
292
David Sterba48dae9c2017-10-30 18:10:25 +0100293/*
294 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
295 * Returned struct is not linked onto any lists and must be destroyed using
296 * free_device.
297 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300298static struct btrfs_device *__alloc_device(void)
299{
300 struct btrfs_device *dev;
301
David Sterba78f2c9e2016-02-11 14:25:38 +0100302 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300303 if (!dev)
304 return ERR_PTR(-ENOMEM);
305
David Sterbae0ae9992017-06-06 17:06:06 +0200306 /*
307 * Preallocate a bio that's always going to be used for flushing device
308 * barriers and matches the device lifespan
309 */
310 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
311 if (!dev->flush_bio) {
312 kfree(dev);
313 return ERR_PTR(-ENOMEM);
314 }
David Sterbae0ae9992017-06-06 17:06:06 +0200315
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300316 INIT_LIST_HEAD(&dev->dev_list);
317 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800318 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300319
320 spin_lock_init(&dev->io_lock);
321
322 spin_lock_init(&dev->reada_lock);
323 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 Jain4fde46f2015-06-17 21:10:48 +0800608
Omar Sandovalc9162bd2017-08-22 23:46:04 -0700609static void btrfs_free_stale_device(struct btrfs_device *cur_dev)
Anand Jain4fde46f2015-06-17 21:10:48 +0800610{
611 struct btrfs_fs_devices *fs_devs;
612 struct btrfs_device *dev;
613
614 if (!cur_dev->name)
615 return;
616
617 list_for_each_entry(fs_devs, &fs_uuids, list) {
618 int del = 1;
619
620 if (fs_devs->opened)
621 continue;
622 if (fs_devs->seeding)
623 continue;
624
625 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
626
627 if (dev == cur_dev)
628 continue;
629 if (!dev->name)
630 continue;
631
632 /*
633 * Todo: This won't be enough. What if the same device
634 * comes back (with new uuid and) with its mapper path?
635 * But for now, this does help as mostly an admin will
636 * either use mapper or non mapper path throughout.
637 */
638 rcu_read_lock();
639 del = strcmp(rcu_str_deref(dev->name),
640 rcu_str_deref(cur_dev->name));
641 rcu_read_unlock();
642 if (!del)
643 break;
644 }
645
646 if (!del) {
647 /* delete the stale device */
648 if (fs_devs->num_devices == 1) {
649 btrfs_sysfs_remove_fsid(fs_devs);
650 list_del(&fs_devs->list);
651 free_fs_devices(fs_devs);
652 } else {
653 fs_devs->num_devices--;
654 list_del(&dev->dev_list);
David Sterba55de4802017-10-30 18:55:47 +0100655 free_device(dev);
Anand Jain4fde46f2015-06-17 21:10:48 +0800656 }
657 break;
658 }
659 }
660}
661
Anand Jain0fb08bc2017-11-09 23:45:24 +0800662static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
663 struct btrfs_device *device, fmode_t flags,
664 void *holder)
665{
666 struct request_queue *q;
667 struct block_device *bdev;
668 struct buffer_head *bh;
669 struct btrfs_super_block *disk_super;
670 u64 devid;
671 int ret;
672
673 if (device->bdev)
674 return -EINVAL;
675 if (!device->name)
676 return -EINVAL;
677
678 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
679 &bdev, &bh);
680 if (ret)
681 return ret;
682
683 disk_super = (struct btrfs_super_block *)bh->b_data;
684 devid = btrfs_stack_device_id(&disk_super->dev_item);
685 if (devid != device->devid)
686 goto error_brelse;
687
688 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
689 goto error_brelse;
690
691 device->generation = btrfs_super_generation(disk_super);
692
693 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
694 device->writeable = 0;
695 fs_devices->seeding = 1;
696 } else {
697 device->writeable = !bdev_read_only(bdev);
698 }
699
700 q = bdev_get_queue(bdev);
701 if (blk_queue_discard(q))
702 device->can_discard = 1;
703 if (!blk_queue_nonrot(q))
704 fs_devices->rotating = 1;
705
706 device->bdev = bdev;
707 device->in_fs_metadata = 0;
708 device->mode = flags;
709
710 fs_devices->open_devices++;
711 if (device->writeable && device->devid != BTRFS_DEV_REPLACE_DEVID) {
712 fs_devices->rw_devices++;
713 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
714 }
715 brelse(bh);
716
717 return 0;
718
719error_brelse:
720 brelse(bh);
721 blkdev_put(bdev, flags);
722
723 return -EINVAL;
724}
725
David Sterba60999ca2014-03-26 18:26:36 +0100726/*
727 * Add new device to list of registered devices
728 *
729 * Returns:
730 * 1 - first time device is seen
731 * 0 - device already known
732 * < 0 - error
733 */
Chris Masona1b32a52008-09-05 16:09:51 -0400734static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400735 struct btrfs_super_block *disk_super,
736 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
737{
738 struct btrfs_device *device;
739 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400740 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100741 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400742 u64 found_transid = btrfs_super_generation(disk_super);
743
744 fs_devices = find_fsid(disk_super->fsid);
745 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300746 fs_devices = alloc_fs_devices(disk_super->fsid);
747 if (IS_ERR(fs_devices))
748 return PTR_ERR(fs_devices);
749
Chris Mason8a4b83c2008-03-24 15:02:07 -0400750 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300751
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 device = NULL;
753 } else {
David Sterba35c70102017-06-15 19:51:51 +0200754 device = find_device(fs_devices, devid,
755 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400756 }
Miao Xie443f24f2014-07-24 11:37:15 +0800757
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500759 if (fs_devices->opened)
760 return -EBUSY;
761
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300762 device = btrfs_alloc_device(NULL, &devid,
763 disk_super->dev_item.uuid);
764 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400765 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300766 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400767 }
Josef Bacik606686e2012-06-04 14:03:51 -0400768
769 name = rcu_string_strdup(path, GFP_NOFS);
770 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100771 free_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400772 return -ENOMEM;
773 }
Josef Bacik606686e2012-06-04 14:03:51 -0400774 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200775
Chris Masone5e9a522009-06-10 15:17:02 -0400776 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000777 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100778 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400779 mutex_unlock(&fs_devices->device_list_mutex);
780
David Sterba60999ca2014-03-26 18:26:36 +0100781 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500782 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400783 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800784 /*
785 * When FS is already mounted.
786 * 1. If you are here and if the device->name is NULL that
787 * means this device was missing at time of FS mount.
788 * 2. If you are here and if the device->name is different
789 * from 'path' that means either
790 * a. The same device disappeared and reappeared with
791 * different name. or
792 * b. The missing-disk-which-was-replaced, has
793 * reappeared now.
794 *
795 * We must allow 1 and 2a above. But 2b would be a spurious
796 * and unintentional.
797 *
798 * Further in case of 1 and 2a above, the disk at 'path'
799 * would have missed some transaction when it was away and
800 * in case of 2a the stale bdev has to be updated as well.
801 * 2b must not be allowed at all time.
802 */
803
804 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700805 * For now, we do allow update to btrfs_fs_device through the
806 * btrfs dev scan cli after FS has been mounted. We're still
807 * tracking a problem where systems fail mount by subvolume id
808 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800809 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700810 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800811 /*
812 * That is if the FS is _not_ mounted and if you
813 * are here, that means there is more than one
814 * disk with same uuid and devid.We keep the one
815 * with larger generation number or the last-in if
816 * generation are equal.
817 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700818 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800819 }
Anand Jainb96de002014-07-03 18:22:05 +0800820
Josef Bacik606686e2012-06-04 14:03:51 -0400821 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000822 if (!name)
823 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400824 rcu_string_free(device->name);
825 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500826 if (device->missing) {
827 fs_devices->missing_devices--;
828 device->missing = 0;
829 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400830 }
831
Anand Jain77bdae42014-07-03 18:22:06 +0800832 /*
833 * Unmount does not free the btrfs_device struct but would zero
834 * generation along with most of the other members. So just update
835 * it back. We need it to pick the disk with largest generation
836 * (as above).
837 */
838 if (!fs_devices->opened)
839 device->generation = found_transid;
840
Anand Jain4fde46f2015-06-17 21:10:48 +0800841 /*
842 * if there is new btrfs on an already registered device,
843 * then remove the stale device entry.
844 */
Anand Jain02feae32016-02-13 10:01:40 +0800845 if (ret > 0)
846 btrfs_free_stale_device(device);
Anand Jain4fde46f2015-06-17 21:10:48 +0800847
Chris Mason8a4b83c2008-03-24 15:02:07 -0400848 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100849
850 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400851}
852
Yan Zhenge4404d62008-12-12 10:03:26 -0500853static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
854{
855 struct btrfs_fs_devices *fs_devices;
856 struct btrfs_device *device;
857 struct btrfs_device *orig_dev;
858
Ilya Dryomov2208a372013-08-12 14:33:03 +0300859 fs_devices = alloc_fs_devices(orig->fsid);
860 if (IS_ERR(fs_devices))
861 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500862
Miao Xieadbbb862014-09-03 21:35:42 +0800863 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400864 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500865
Xiao Guangrong46224702011-04-20 10:08:47 +0000866 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500867 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400868 struct rcu_string *name;
869
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300870 device = btrfs_alloc_device(NULL, &orig_dev->devid,
871 orig_dev->uuid);
872 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500873 goto error;
874
Josef Bacik606686e2012-06-04 14:03:51 -0400875 /*
876 * This is ok to do without rcu read locked because we hold the
877 * uuid mutex so nothing we touch in here is going to disappear.
878 */
Anand Jaine755f782014-06-30 17:12:47 +0800879 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100880 name = rcu_string_strdup(orig_dev->name->str,
881 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800882 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100883 free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800884 goto error;
885 }
886 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400887 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500888
Yan Zhenge4404d62008-12-12 10:03:26 -0500889 list_add(&device->dev_list, &fs_devices->devices);
890 device->fs_devices = fs_devices;
891 fs_devices->num_devices++;
892 }
Miao Xieadbbb862014-09-03 21:35:42 +0800893 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500894 return fs_devices;
895error:
Miao Xieadbbb862014-09-03 21:35:42 +0800896 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500897 free_fs_devices(fs_devices);
898 return ERR_PTR(-ENOMEM);
899}
900
Eric Sandeen9eaed212014-08-01 18:12:35 -0500901void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400902{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500903 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800904 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500905
Chris Masondfe25022008-05-13 13:46:40 -0400906 mutex_lock(&uuid_mutex);
907again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000908 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500909 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500910 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100911 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800912 (!latest_dev ||
913 device->generation > latest_dev->generation)) {
914 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500915 }
Yan Zheng2b820322008-11-17 21:11:30 -0500916 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500917 }
Yan Zheng2b820322008-11-17 21:11:30 -0500918
Stefan Behrens8dabb742012-11-06 13:15:27 +0100919 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
920 /*
921 * In the first step, keep the device which has
922 * the correct fsid and the devid that is used
923 * for the dev_replace procedure.
924 * In the second step, the dev_replace state is
925 * read from the device tree and it is known
926 * whether the procedure is really active or
927 * not, which means whether this device is
928 * used or whether it should be removed.
929 */
930 if (step == 0 || device->is_tgtdev_for_dev_replace) {
931 continue;
932 }
933 }
Yan Zheng2b820322008-11-17 21:11:30 -0500934 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100935 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500936 device->bdev = NULL;
937 fs_devices->open_devices--;
938 }
939 if (device->writeable) {
940 list_del_init(&device->dev_alloc_list);
941 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100942 if (!device->is_tgtdev_for_dev_replace)
943 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500944 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500945 list_del_init(&device->dev_list);
946 fs_devices->num_devices--;
David Sterba55de4802017-10-30 18:55:47 +0100947 free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400948 }
Yan Zheng2b820322008-11-17 21:11:30 -0500949
950 if (fs_devices->seed) {
951 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500952 goto again;
953 }
954
Miao Xie443f24f2014-07-24 11:37:15 +0800955 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500956
Chris Masondfe25022008-05-13 13:46:40 -0400957 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400958}
Chris Masona0af4692008-05-13 16:03:06 -0400959
David Sterbaf06c5962017-06-06 17:08:23 +0200960static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000961{
962 struct btrfs_device *device;
963
964 device = container_of(head, struct btrfs_device, rcu);
David Sterba55de4802017-10-30 18:55:47 +0100965 free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000966}
967
Anand Jain14238812016-07-22 06:04:53 +0800968static void btrfs_close_bdev(struct btrfs_device *device)
969{
David Sterba08ffcae2017-06-19 16:55:35 +0200970 if (!device->bdev)
971 return;
972
973 if (device->writeable) {
Anand Jain14238812016-07-22 06:04:53 +0800974 sync_blockdev(device->bdev);
975 invalidate_bdev(device->bdev);
976 }
977
David Sterba08ffcae2017-06-19 16:55:35 +0200978 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +0800979}
980
Anand Jain0ccd0522016-09-22 12:56:13 +0800981static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +0800982{
983 struct btrfs_fs_devices *fs_devices = device->fs_devices;
984 struct btrfs_device *new_device;
985 struct rcu_string *name;
986
987 if (device->bdev)
988 fs_devices->open_devices--;
989
990 if (device->writeable &&
991 device->devid != BTRFS_DEV_REPLACE_DEVID) {
992 list_del_init(&device->dev_alloc_list);
993 fs_devices->rw_devices--;
994 }
995
996 if (device->missing)
997 fs_devices->missing_devices--;
998
999 new_device = btrfs_alloc_device(NULL, &device->devid,
1000 device->uuid);
1001 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1002
1003 /* Safe because we are under uuid_mutex */
1004 if (device->name) {
1005 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1006 BUG_ON(!name); /* -ENOMEM */
1007 rcu_assign_pointer(new_device->name, name);
1008 }
1009
1010 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1011 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001012}
1013
Yan Zheng2b820322008-11-17 21:11:30 -05001014static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001015{
Sasha Levin2037a092015-05-12 19:31:37 -04001016 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +08001017 struct list_head pending_put;
1018
1019 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -05001020
Yan Zheng2b820322008-11-17 21:11:30 -05001021 if (--fs_devices->opened > 0)
1022 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001023
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001024 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001025 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +08001026 btrfs_prepare_close_one_device(device);
1027 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001028 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001029 mutex_unlock(&fs_devices->device_list_mutex);
1030
Anand Jain0ccd0522016-09-22 12:56:13 +08001031 /*
1032 * btrfs_show_devname() is using the device_list_mutex,
1033 * sometimes call to blkdev_put() leads vfs calling
1034 * into this func. So do put outside of device_list_mutex,
1035 * as of now.
1036 */
1037 while (!list_empty(&pending_put)) {
1038 device = list_first_entry(&pending_put,
1039 struct btrfs_device, dev_list);
1040 list_del(&device->dev_list);
1041 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001042 call_rcu(&device->rcu, free_device_rcu);
Anand Jain0ccd0522016-09-22 12:56:13 +08001043 }
1044
Yan Zhenge4404d62008-12-12 10:03:26 -05001045 WARN_ON(fs_devices->open_devices);
1046 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001047 fs_devices->opened = 0;
1048 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001049
Chris Mason8a4b83c2008-03-24 15:02:07 -04001050 return 0;
1051}
1052
Yan Zheng2b820322008-11-17 21:11:30 -05001053int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1054{
Yan Zhenge4404d62008-12-12 10:03:26 -05001055 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001056 int ret;
1057
1058 mutex_lock(&uuid_mutex);
1059 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001060 if (!fs_devices->opened) {
1061 seed_devices = fs_devices->seed;
1062 fs_devices->seed = NULL;
1063 }
Yan Zheng2b820322008-11-17 21:11:30 -05001064 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001065
1066 while (seed_devices) {
1067 fs_devices = seed_devices;
1068 seed_devices = fs_devices->seed;
1069 __btrfs_close_devices(fs_devices);
1070 free_fs_devices(fs_devices);
1071 }
Yan Zheng2b820322008-11-17 21:11:30 -05001072 return ret;
1073}
1074
Yan Zhenge4404d62008-12-12 10:03:26 -05001075static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1076 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001077{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001078 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001079 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001080 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001081 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001082
Tejun Heod4d77622010-11-13 11:55:18 +01001083 flags |= FMODE_EXCL;
1084
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001085 list_for_each_entry(device, head, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001086 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001087 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001088 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001089
Anand Jain9f050db2017-11-09 23:45:25 +08001090 if (!latest_dev ||
1091 device->generation > latest_dev->generation)
1092 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001093 }
Chris Masona0af4692008-05-13 16:03:06 -04001094 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001095 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001096 goto out;
1097 }
Yan Zheng2b820322008-11-17 21:11:30 -05001098 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001099 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001100 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001101out:
Yan Zheng2b820322008-11-17 21:11:30 -05001102 return ret;
1103}
1104
1105int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001106 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001107{
1108 int ret;
1109
1110 mutex_lock(&uuid_mutex);
1111 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001112 fs_devices->opened++;
1113 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001114 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001115 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001116 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001117 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001118 return ret;
1119}
1120
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001121static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001122{
1123 kunmap(page);
1124 put_page(page);
1125}
1126
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001127static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1128 struct page **page,
1129 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001130{
1131 void *p;
1132 pgoff_t index;
1133
1134 /* make sure our super fits in the device */
1135 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1136 return 1;
1137
1138 /* make sure our super fits in the page */
1139 if (sizeof(**disk_super) > PAGE_SIZE)
1140 return 1;
1141
1142 /* make sure our super doesn't straddle pages on disk */
1143 index = bytenr >> PAGE_SHIFT;
1144 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1145 return 1;
1146
1147 /* pull in the page with our super */
1148 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1149 index, GFP_KERNEL);
1150
1151 if (IS_ERR_OR_NULL(*page))
1152 return 1;
1153
1154 p = kmap(*page);
1155
1156 /* align our pointer to the offset of the super block */
1157 *disk_super = p + (bytenr & ~PAGE_MASK);
1158
1159 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1160 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1161 btrfs_release_disk_super(*page);
1162 return 1;
1163 }
1164
1165 if ((*disk_super)->label[0] &&
1166 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1167 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1168
1169 return 0;
1170}
1171
David Sterba6f60cbd2013-02-15 11:31:02 -07001172/*
1173 * Look for a btrfs signature on a device. This may be called out of the mount path
1174 * and we are not allowed to call set_blocksize during the scan. The superblock
1175 * is read via pagecache
1176 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001177int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001178 struct btrfs_fs_devices **fs_devices_ret)
1179{
1180 struct btrfs_super_block *disk_super;
1181 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001182 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001183 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001184 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001185 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001186 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001187 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001188
David Sterba6f60cbd2013-02-15 11:31:02 -07001189 /*
1190 * we would like to check all the supers, but that would make
1191 * a btrfs mount succeed after a mkfs from a different FS.
1192 * So, we need to add a special mount option to scan for
1193 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1194 */
1195 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001196 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001197 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001198
1199 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001200 if (IS_ERR(bdev)) {
1201 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001202 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001203 }
1204
Anand Jain6cf86a002016-02-13 10:01:29 +08001205 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
David Sterba6f60cbd2013-02-15 11:31:02 -07001206 goto error_bdev_put;
1207
Xiao Guangronga3438322010-01-06 11:48:18 +00001208 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001209 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001210 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001211
Chris Mason8a4b83c2008-03-24 15:02:07 -04001212 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001213 if (ret > 0) {
1214 if (disk_super->label[0]) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001215 pr_info("BTRFS: device label %s ", disk_super->label);
David Sterba60999ca2014-03-26 18:26:36 +01001216 } else {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001217 pr_info("BTRFS: device fsid %pU ", disk_super->fsid);
David Sterba60999ca2014-03-26 18:26:36 +01001218 }
1219
Jeff Mahoney62e85572016-09-20 10:05:01 -04001220 pr_cont("devid %llu transid %llu %s\n", devid, transid, path);
David Sterba60999ca2014-03-26 18:26:36 +01001221 ret = 0;
1222 }
Josef Bacik02db0842012-06-21 16:03:58 -04001223 if (!ret && fs_devices_ret)
1224 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001225
Anand Jain6cf86a002016-02-13 10:01:29 +08001226 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001227
1228error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001229 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001230error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001231 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001232 return ret;
1233}
Chris Mason0b86a832008-03-24 15:01:56 -04001234
Miao Xie6d07bce2011-01-05 10:07:31 +00001235/* helper to account the used device space in the range */
1236int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1237 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001238{
1239 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001240 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001241 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001242 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001243 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001244 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001245 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001246 struct extent_buffer *l;
1247
Miao Xie6d07bce2011-01-05 10:07:31 +00001248 *length = 0;
1249
Stefan Behrens63a212a2012-11-05 18:29:28 +01001250 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001251 return 0;
1252
Yan Zheng2b820322008-11-17 21:11:30 -05001253 path = btrfs_alloc_path();
1254 if (!path)
1255 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001256 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001257
Chris Mason0b86a832008-03-24 15:01:56 -04001258 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001259 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001260 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001261
1262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001263 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001264 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001265 if (ret > 0) {
1266 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1267 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001268 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001269 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001270
Chris Mason0b86a832008-03-24 15:01:56 -04001271 while (1) {
1272 l = path->nodes[0];
1273 slot = path->slots[0];
1274 if (slot >= btrfs_header_nritems(l)) {
1275 ret = btrfs_next_leaf(root, path);
1276 if (ret == 0)
1277 continue;
1278 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001279 goto out;
1280
1281 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001282 }
1283 btrfs_item_key_to_cpu(l, &key, slot);
1284
1285 if (key.objectid < device->devid)
1286 goto next;
1287
1288 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001289 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001290
David Sterba962a2982014-06-04 18:41:45 +02001291 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001292 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001293
Chris Mason0b86a832008-03-24 15:01:56 -04001294 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001295 extent_end = key.offset + btrfs_dev_extent_length(l,
1296 dev_extent);
1297 if (key.offset <= start && extent_end > end) {
1298 *length = end - start + 1;
1299 break;
1300 } else if (key.offset <= start && extent_end > start)
1301 *length += extent_end - start;
1302 else if (key.offset > start && extent_end <= end)
1303 *length += extent_end - key.offset;
1304 else if (key.offset > start && key.offset <= end) {
1305 *length += end - key.offset + 1;
1306 break;
1307 } else if (key.offset > end)
1308 break;
1309
1310next:
1311 path->slots[0]++;
1312 }
1313 ret = 0;
1314out:
1315 btrfs_free_path(path);
1316 return ret;
1317}
1318
Jeff Mahoney499f3772015-06-15 09:41:17 -04001319static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001320 struct btrfs_device *device,
1321 u64 *start, u64 len)
1322{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001323 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001324 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001325 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001326 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001327 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001328
Jeff Mahoney499f3772015-06-15 09:41:17 -04001329 if (transaction)
1330 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001331again:
1332 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001333 struct map_lookup *map;
1334 int i;
1335
Jeff Mahoney95617d62015-06-03 10:55:48 -04001336 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001337 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001338 u64 end;
1339
Josef Bacik6df9a952013-06-27 13:22:46 -04001340 if (map->stripes[i].dev != device)
1341 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001342 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001343 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001344 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001345 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001346 /*
1347 * Make sure that while processing the pinned list we do
1348 * not override our *start with a lower value, because
1349 * we can have pinned chunks that fall within this
1350 * device hole and that have lower physical addresses
1351 * than the pending chunks we processed before. If we
1352 * do not take this special care we can end up getting
1353 * 2 pending chunks that start at the same physical
1354 * device offsets because the end offset of a pinned
1355 * chunk can be equal to the start offset of some
1356 * pending chunk.
1357 */
1358 end = map->stripes[i].physical + em->orig_block_len;
1359 if (end > *start) {
1360 *start = end;
1361 ret = 1;
1362 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001363 }
1364 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001365 if (search_list != &fs_info->pinned_chunks) {
1366 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001367 goto again;
1368 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001369
1370 return ret;
1371}
1372
1373
Chris Mason0b86a832008-03-24 15:01:56 -04001374/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001375 * find_free_dev_extent_start - find free space in the specified device
1376 * @device: the device which we search the free space in
1377 * @num_bytes: the size of the free space that we need
1378 * @search_start: the position from which to begin the search
1379 * @start: store the start of the free space.
1380 * @len: the size of the free space. that we find, or the size
1381 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001382 *
Chris Mason0b86a832008-03-24 15:01:56 -04001383 * this uses a pretty simple search, the expectation is that it is
1384 * called very infrequently and that a given device has a small number
1385 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001386 *
1387 * @start is used to store the start of the free space if we find. But if we
1388 * don't find suitable free space, it will be used to store the start position
1389 * of the max free space.
1390 *
1391 * @len is used to store the size of the free space that we find.
1392 * But if we don't find suitable free space, it is used to store the size of
1393 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001394 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001395int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1396 struct btrfs_device *device, u64 num_bytes,
1397 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001398{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001399 struct btrfs_fs_info *fs_info = device->fs_info;
1400 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001401 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001402 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001403 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001404 u64 hole_size;
1405 u64 max_hole_start;
1406 u64 max_hole_size;
1407 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001408 u64 search_end = device->total_bytes;
1409 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001410 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001411 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001412
1413 /*
1414 * We don't want to overwrite the superblock on the drive nor any area
1415 * used by the boot loader (grub for example), so we make sure to start
1416 * at an offset of at least 1MB.
1417 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001418 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001419
Josef Bacik6df9a952013-06-27 13:22:46 -04001420 path = btrfs_alloc_path();
1421 if (!path)
1422 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001423
Miao Xie7bfc8372011-01-05 10:07:26 +00001424 max_hole_start = search_start;
1425 max_hole_size = 0;
1426
Zhao Leif2ab7612015-02-16 18:52:17 +08001427again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001428 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001429 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001430 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001431 }
1432
David Sterbae4058b52015-11-27 16:31:35 +01001433 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001434 path->search_commit_root = 1;
1435 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001436
Chris Mason0b86a832008-03-24 15:01:56 -04001437 key.objectid = device->devid;
1438 key.offset = search_start;
1439 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001440
Li Zefan125ccb02011-12-08 15:07:24 +08001441 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001442 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001443 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001444 if (ret > 0) {
1445 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1446 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001447 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001448 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001449
Chris Mason0b86a832008-03-24 15:01:56 -04001450 while (1) {
1451 l = path->nodes[0];
1452 slot = path->slots[0];
1453 if (slot >= btrfs_header_nritems(l)) {
1454 ret = btrfs_next_leaf(root, path);
1455 if (ret == 0)
1456 continue;
1457 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001458 goto out;
1459
1460 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001461 }
1462 btrfs_item_key_to_cpu(l, &key, slot);
1463
1464 if (key.objectid < device->devid)
1465 goto next;
1466
1467 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001468 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001469
David Sterba962a2982014-06-04 18:41:45 +02001470 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001471 goto next;
1472
Miao Xie7bfc8372011-01-05 10:07:26 +00001473 if (key.offset > search_start) {
1474 hole_size = key.offset - search_start;
1475
Josef Bacik6df9a952013-06-27 13:22:46 -04001476 /*
1477 * Have to check before we set max_hole_start, otherwise
1478 * we could end up sending back this offset anyway.
1479 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001480 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001481 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001482 hole_size)) {
1483 if (key.offset >= search_start) {
1484 hole_size = key.offset - search_start;
1485 } else {
1486 WARN_ON_ONCE(1);
1487 hole_size = 0;
1488 }
1489 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001490
Miao Xie7bfc8372011-01-05 10:07:26 +00001491 if (hole_size > max_hole_size) {
1492 max_hole_start = search_start;
1493 max_hole_size = hole_size;
1494 }
1495
1496 /*
1497 * If this free space is greater than which we need,
1498 * it must be the max free space that we have found
1499 * until now, so max_hole_start must point to the start
1500 * of this free space and the length of this free space
1501 * is stored in max_hole_size. Thus, we return
1502 * max_hole_start and max_hole_size and go back to the
1503 * caller.
1504 */
1505 if (hole_size >= num_bytes) {
1506 ret = 0;
1507 goto out;
1508 }
1509 }
1510
Chris Mason0b86a832008-03-24 15:01:56 -04001511 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001512 extent_end = key.offset + btrfs_dev_extent_length(l,
1513 dev_extent);
1514 if (extent_end > search_start)
1515 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001516next:
1517 path->slots[0]++;
1518 cond_resched();
1519 }
Chris Mason0b86a832008-03-24 15:01:56 -04001520
liubo38c01b92011-08-02 02:39:03 +00001521 /*
1522 * At this point, search_start should be the end of
1523 * allocated dev extents, and when shrinking the device,
1524 * search_end may be smaller than search_start.
1525 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001526 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001527 hole_size = search_end - search_start;
1528
Jeff Mahoney499f3772015-06-15 09:41:17 -04001529 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001530 hole_size)) {
1531 btrfs_release_path(path);
1532 goto again;
1533 }
Chris Mason0b86a832008-03-24 15:01:56 -04001534
Zhao Leif2ab7612015-02-16 18:52:17 +08001535 if (hole_size > max_hole_size) {
1536 max_hole_start = search_start;
1537 max_hole_size = hole_size;
1538 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001539 }
1540
Miao Xie7bfc8372011-01-05 10:07:26 +00001541 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001542 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001543 ret = -ENOSPC;
1544 else
1545 ret = 0;
1546
1547out:
Yan Zheng2b820322008-11-17 21:11:30 -05001548 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001549 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001550 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001551 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001552 return ret;
1553}
1554
Jeff Mahoney499f3772015-06-15 09:41:17 -04001555int find_free_dev_extent(struct btrfs_trans_handle *trans,
1556 struct btrfs_device *device, u64 num_bytes,
1557 u64 *start, u64 *len)
1558{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001559 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001560 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001561 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001562}
1563
Christoph Hellwigb2950862008-12-02 09:54:17 -05001564static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001565 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001566 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001567{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001568 struct btrfs_fs_info *fs_info = device->fs_info;
1569 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001570 int ret;
1571 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001572 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001573 struct btrfs_key found_key;
1574 struct extent_buffer *leaf = NULL;
1575 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001576
1577 path = btrfs_alloc_path();
1578 if (!path)
1579 return -ENOMEM;
1580
1581 key.objectid = device->devid;
1582 key.offset = start;
1583 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001584again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001585 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001586 if (ret > 0) {
1587 ret = btrfs_previous_item(root, path, key.objectid,
1588 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001589 if (ret)
1590 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001591 leaf = path->nodes[0];
1592 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1593 extent = btrfs_item_ptr(leaf, path->slots[0],
1594 struct btrfs_dev_extent);
1595 BUG_ON(found_key.offset > start || found_key.offset +
1596 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001597 key = found_key;
1598 btrfs_release_path(path);
1599 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001600 } else if (ret == 0) {
1601 leaf = path->nodes[0];
1602 extent = btrfs_item_ptr(leaf, path->slots[0],
1603 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001604 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001605 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001606 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001607 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001608
Miao Xie2196d6e2014-09-03 21:35:41 +08001609 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1610
Chris Mason8f18cf12008-04-25 16:53:30 -04001611 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001612 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001613 btrfs_handle_fs_error(fs_info, ret,
1614 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001615 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001616 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001617 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001618out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001619 btrfs_free_path(path);
1620 return ret;
1621}
1622
Eric Sandeen48a3b632013-04-25 20:41:01 +00001623static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1624 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001625 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001626{
1627 int ret;
1628 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001629 struct btrfs_fs_info *fs_info = device->fs_info;
1630 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001631 struct btrfs_dev_extent *extent;
1632 struct extent_buffer *leaf;
1633 struct btrfs_key key;
1634
Chris Masondfe25022008-05-13 13:46:40 -04001635 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001636 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001637 path = btrfs_alloc_path();
1638 if (!path)
1639 return -ENOMEM;
1640
Chris Mason0b86a832008-03-24 15:01:56 -04001641 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001642 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001643 key.type = BTRFS_DEV_EXTENT_KEY;
1644 ret = btrfs_insert_empty_item(trans, root, path, &key,
1645 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001646 if (ret)
1647 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001648
1649 leaf = path->nodes[0];
1650 extent = btrfs_item_ptr(leaf, path->slots[0],
1651 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001652 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1653 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001654 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1655 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001656 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1657
Chris Mason0b86a832008-03-24 15:01:56 -04001658 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1659 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001660out:
Chris Mason0b86a832008-03-24 15:01:56 -04001661 btrfs_free_path(path);
1662 return ret;
1663}
1664
Josef Bacik6df9a952013-06-27 13:22:46 -04001665static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001666{
Josef Bacik6df9a952013-06-27 13:22:46 -04001667 struct extent_map_tree *em_tree;
1668 struct extent_map *em;
1669 struct rb_node *n;
1670 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001671
Josef Bacik6df9a952013-06-27 13:22:46 -04001672 em_tree = &fs_info->mapping_tree.map_tree;
1673 read_lock(&em_tree->lock);
1674 n = rb_last(&em_tree->map);
1675 if (n) {
1676 em = rb_entry(n, struct extent_map, rb_node);
1677 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001678 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001679 read_unlock(&em_tree->lock);
1680
Chris Mason0b86a832008-03-24 15:01:56 -04001681 return ret;
1682}
1683
Ilya Dryomov53f10652013-08-12 14:33:01 +03001684static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1685 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001686{
1687 int ret;
1688 struct btrfs_key key;
1689 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001690 struct btrfs_path *path;
1691
Yan Zheng2b820322008-11-17 21:11:30 -05001692 path = btrfs_alloc_path();
1693 if (!path)
1694 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001695
1696 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1697 key.type = BTRFS_DEV_ITEM_KEY;
1698 key.offset = (u64)-1;
1699
Ilya Dryomov53f10652013-08-12 14:33:01 +03001700 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001701 if (ret < 0)
1702 goto error;
1703
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001704 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001705
Ilya Dryomov53f10652013-08-12 14:33:01 +03001706 ret = btrfs_previous_item(fs_info->chunk_root, path,
1707 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001708 BTRFS_DEV_ITEM_KEY);
1709 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001710 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001711 } else {
1712 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1713 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001714 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001715 }
1716 ret = 0;
1717error:
Yan Zheng2b820322008-11-17 21:11:30 -05001718 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001719 return ret;
1720}
1721
1722/*
1723 * the device information is stored in the chunk root
1724 * the btrfs_device struct should be fully filled in
1725 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001726static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001727 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001728 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001729{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001730 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001731 int ret;
1732 struct btrfs_path *path;
1733 struct btrfs_dev_item *dev_item;
1734 struct extent_buffer *leaf;
1735 struct btrfs_key key;
1736 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001737
Chris Mason0b86a832008-03-24 15:01:56 -04001738 path = btrfs_alloc_path();
1739 if (!path)
1740 return -ENOMEM;
1741
Chris Mason0b86a832008-03-24 15:01:56 -04001742 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1743 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001744 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001745
1746 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001747 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001748 if (ret)
1749 goto out;
1750
1751 leaf = path->nodes[0];
1752 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1753
1754 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001755 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001756 btrfs_set_device_type(leaf, dev_item, device->type);
1757 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1758 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1759 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001760 btrfs_set_device_total_bytes(leaf, dev_item,
1761 btrfs_device_get_disk_total_bytes(device));
1762 btrfs_set_device_bytes_used(leaf, dev_item,
1763 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001764 btrfs_set_device_group(leaf, dev_item, 0);
1765 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1766 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001767 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001768
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001769 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001770 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001771 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001772 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001773 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001774
Yan Zheng2b820322008-11-17 21:11:30 -05001775 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001776out:
1777 btrfs_free_path(path);
1778 return ret;
1779}
Chris Mason8f18cf12008-04-25 16:53:30 -04001780
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001781/*
1782 * Function to update ctime/mtime for a given device path.
1783 * Mainly used for ctime/mtime based probe like libblkid.
1784 */
David Sterbada353f62017-02-14 17:55:53 +01001785static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001786{
1787 struct file *filp;
1788
1789 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001790 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001791 return;
1792 file_update_time(filp);
1793 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001794}
1795
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001796static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001797 struct btrfs_device *device)
1798{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001799 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001800 int ret;
1801 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001802 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001803 struct btrfs_trans_handle *trans;
1804
Chris Masona061fc82008-05-07 11:43:44 -04001805 path = btrfs_alloc_path();
1806 if (!path)
1807 return -ENOMEM;
1808
Yan, Zhenga22285a2010-05-16 10:48:46 -04001809 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001810 if (IS_ERR(trans)) {
1811 btrfs_free_path(path);
1812 return PTR_ERR(trans);
1813 }
Chris Masona061fc82008-05-07 11:43:44 -04001814 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1815 key.type = BTRFS_DEV_ITEM_KEY;
1816 key.offset = device->devid;
1817
1818 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001819 if (ret) {
1820 if (ret > 0)
1821 ret = -ENOENT;
1822 btrfs_abort_transaction(trans, ret);
1823 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001824 goto out;
1825 }
1826
1827 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001828 if (ret) {
1829 btrfs_abort_transaction(trans, ret);
1830 btrfs_end_transaction(trans);
1831 }
1832
Chris Masona061fc82008-05-07 11:43:44 -04001833out:
1834 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001835 if (!ret)
1836 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001837 return ret;
1838}
1839
David Sterba3cc31a02016-02-15 16:00:26 +01001840/*
1841 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1842 * filesystem. It's up to the caller to adjust that number regarding eg. device
1843 * replace.
1844 */
1845static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1846 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001847{
Chris Masona061fc82008-05-07 11:43:44 -04001848 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001849 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001850 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001851
Miao Xiede98ced2013-01-29 10:13:12 +00001852 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001853 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001854
Anand Jainbd45ffb2016-02-13 10:01:34 +08001855 all_avail = fs_info->avail_data_alloc_bits |
1856 fs_info->avail_system_alloc_bits |
1857 fs_info->avail_metadata_alloc_bits;
1858 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001859
David Sterba418775a2016-02-15 16:28:14 +01001860 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1861 if (!(all_avail & btrfs_raid_group[i]))
1862 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001863
David Sterba418775a2016-02-15 16:28:14 +01001864 if (num_devices < btrfs_raid_array[i].devs_min) {
1865 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001866
David Sterba418775a2016-02-15 16:28:14 +01001867 if (ret)
1868 return ret;
1869 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001870 }
1871
1872 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001873}
1874
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001875static struct btrfs_device * btrfs_find_next_active_device(
1876 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001877{
1878 struct btrfs_device *next_device;
1879
1880 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1881 if (next_device != device &&
1882 !next_device->missing && next_device->bdev)
1883 return next_device;
1884 }
1885
1886 return NULL;
1887}
1888
1889/*
1890 * Helper function to check if the given device is part of s_bdev / latest_bdev
1891 * and replace it with the provided or the next active device, in the context
1892 * where this function called, there should be always be another device (or
1893 * this_dev) which is active.
1894 */
1895void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1896 struct btrfs_device *device, struct btrfs_device *this_dev)
1897{
1898 struct btrfs_device *next_device;
1899
1900 if (this_dev)
1901 next_device = this_dev;
1902 else
1903 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1904 device);
1905 ASSERT(next_device);
1906
1907 if (fs_info->sb->s_bdev &&
1908 (fs_info->sb->s_bdev == device->bdev))
1909 fs_info->sb->s_bdev = next_device->bdev;
1910
1911 if (fs_info->fs_devices->latest_bdev == device->bdev)
1912 fs_info->fs_devices->latest_bdev = next_device->bdev;
1913}
1914
David Sterbada353f62017-02-14 17:55:53 +01001915int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1916 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001917{
1918 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001919 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001920 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001921 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001922
Anand Jain2c997382017-11-06 10:28:00 +08001923 mutex_lock(&fs_info->volume_mutex);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001924 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001925
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001926 num_devices = fs_info->fs_devices->num_devices;
1927 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1928 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001929 WARN_ON(num_devices < 1);
1930 num_devices--;
1931 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001932 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001933
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001934 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001935 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001936 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001937
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001938 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1939 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001940 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001941 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001942
Stefan Behrens63a212a2012-11-05 18:29:28 +01001943 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001944 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001945 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001946 }
1947
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001948 if (device->writeable && fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001949 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001950 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001951 }
1952
1953 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02001954 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001955 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001956 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001957 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001958 }
Chris Masona061fc82008-05-07 11:43:44 -04001959
Carey Underwoodd7901552013-03-04 16:37:06 -06001960 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001961 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001962 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001963 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001964 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001965
Stefan Behrens63a212a2012-11-05 18:29:28 +01001966 /*
1967 * TODO: the superblock still includes this device in its num_devices
1968 * counter although write_all_supers() is not locked out. This
1969 * could give a filesystem state which requires a degraded mount.
1970 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001971 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001972 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001973 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001974
Yan Zheng2b820322008-11-17 21:11:30 -05001975 device->in_fs_metadata = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001976 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001977
1978 /*
1979 * the device list mutex makes sure that we don't change
1980 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001981 * the device supers. Whoever is writing all supers, should
1982 * lock the device list mutex before getting the number of
1983 * devices in the super block (super_copy). Conversely,
1984 * whoever updates the number of devices in the super block
1985 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001986 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001987
1988 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001989 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001990 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001991
Yan Zhenge4404d62008-12-12 10:03:26 -05001992 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001993 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001994
Chris Masoncd02dca2010-12-13 14:56:23 -05001995 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001996 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001997
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05001999
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002000 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002001 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002002 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002003 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002004 }
Anand Jain99994cd2014-06-03 11:36:00 +08002005
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002006 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2007 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2008 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002009
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002010 /*
2011 * at this point, the device is zero sized and detached from
2012 * the devices list. All that's left is to zero out the old
2013 * supers and free the device.
2014 */
2015 if (device->writeable)
2016 btrfs_scratch_superblocks(device->bdev, device->name->str);
2017
2018 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002019 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002020
Xiao Guangrong1f781602011-04-20 10:09:16 +00002021 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002022 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002023 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002024 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002025 if (fs_devices->seed == cur_devices) {
2026 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002027 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002028 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002029 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002030 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002031 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00002032 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002033 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002034 }
2035
Chris Masona061fc82008-05-07 11:43:44 -04002036out:
2037 mutex_unlock(&uuid_mutex);
Anand Jain2c997382017-11-06 10:28:00 +08002038 mutex_unlock(&fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002039 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002040
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002041error_undo:
2042 if (device->writeable) {
David Sterba34441362016-10-04 19:34:27 +02002043 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002044 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002045 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002046 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002047 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002048 }
Anand Jain24fc5722016-02-13 10:01:36 +08002049 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002050}
2051
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002052void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2053 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002054{
Anand Jaind51908c2014-08-13 14:24:19 +08002055 struct btrfs_fs_devices *fs_devices;
2056
Stefan Behrense93c89c2012-11-05 17:33:06 +01002057 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03002058
Anand Jain25e8e912014-08-20 10:56:56 +08002059 /*
2060 * in case of fs with no seed, srcdev->fs_devices will point
2061 * to fs_devices of fs_info. However when the dev being replaced is
2062 * a seed dev it will point to the seed's local fs_devices. In short
2063 * srcdev will have its correct fs_devices in both the cases.
2064 */
2065 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002066
Stefan Behrense93c89c2012-11-05 17:33:06 +01002067 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002068 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002069 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08002070 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08002071 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002072
Anand Jain48b3b9d2016-04-12 21:36:16 +08002073 if (srcdev->writeable)
Miao Xie82372bc2014-09-03 21:35:44 +08002074 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002075
Miao Xie82372bc2014-09-03 21:35:44 +08002076 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002077 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002078}
2079
2080void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2081 struct btrfs_device *srcdev)
2082{
2083 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002084
Anand Jain48b3b9d2016-04-12 21:36:16 +08002085 if (srcdev->writeable) {
2086 /* zero out the old super if it is writable */
2087 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2088 }
Anand Jain14238812016-07-22 06:04:53 +08002089
2090 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002091 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002092
Anand Jain94d5f0c2014-08-13 14:24:22 +08002093 /* if this is no devs we rather delete the fs_devices */
2094 if (!fs_devices->num_devices) {
2095 struct btrfs_fs_devices *tmp_fs_devices;
2096
Anand Jain6dd38f82017-10-17 06:53:50 +08002097 /*
2098 * On a mounted FS, num_devices can't be zero unless it's a
2099 * seed. In case of a seed device being replaced, the replace
2100 * target added to the sprout FS, so there will be no more
2101 * device left under the seed FS.
2102 */
2103 ASSERT(fs_devices->seeding);
2104
Anand Jain94d5f0c2014-08-13 14:24:22 +08002105 tmp_fs_devices = fs_info->fs_devices;
2106 while (tmp_fs_devices) {
2107 if (tmp_fs_devices->seed == fs_devices) {
2108 tmp_fs_devices->seed = fs_devices->seed;
2109 break;
2110 }
2111 tmp_fs_devices = tmp_fs_devices->seed;
2112 }
2113 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002114 __btrfs_close_devices(fs_devices);
2115 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002116 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002117}
2118
2119void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2120 struct btrfs_device *tgtdev)
2121{
Miao Xie67a2c452014-09-03 21:35:43 +08002122 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002123 WARN_ON(!tgtdev);
2124 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002125
Anand Jain32576042015-08-14 18:32:49 +08002126 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002127
Anand Jain779bf3fe2016-04-18 16:51:23 +08002128 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002129 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002130
Stefan Behrense93c89c2012-11-05 17:33:06 +01002131 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002132
Anand Jain88acff62016-05-03 17:44:43 +08002133 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002134
Stefan Behrense93c89c2012-11-05 17:33:06 +01002135 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002136
2137 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002138 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002139
2140 /*
2141 * The update_dev_time() with in btrfs_scratch_superblocks()
2142 * may lead to a call to btrfs_show_devname() which will try
2143 * to hold device_list_mutex. And here this device
2144 * is already out of device list, so we don't have to hold
2145 * the device_list_mutex lock.
2146 */
2147 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002148
2149 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002150 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002151}
2152
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002153static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002154 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002155 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002156{
2157 int ret = 0;
2158 struct btrfs_super_block *disk_super;
2159 u64 devid;
2160 u8 *dev_uuid;
2161 struct block_device *bdev;
2162 struct buffer_head *bh;
2163
2164 *device = NULL;
2165 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002166 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002167 if (ret)
2168 return ret;
2169 disk_super = (struct btrfs_super_block *)bh->b_data;
2170 devid = btrfs_stack_device_id(&disk_super->dev_item);
2171 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002172 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002173 brelse(bh);
2174 if (!*device)
2175 ret = -ENOENT;
2176 blkdev_put(bdev, FMODE_READ);
2177 return ret;
2178}
2179
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002180int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002181 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002182 struct btrfs_device **device)
2183{
2184 *device = NULL;
2185 if (strcmp(device_path, "missing") == 0) {
2186 struct list_head *devices;
2187 struct btrfs_device *tmp;
2188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002189 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002190 /*
2191 * It is safe to read the devices since the volume_mutex
2192 * is held by the caller.
2193 */
2194 list_for_each_entry(tmp, devices, dev_list) {
2195 if (tmp->in_fs_metadata && !tmp->bdev) {
2196 *device = tmp;
2197 break;
2198 }
2199 }
2200
Anand Jaind74a6252015-08-14 18:32:56 +08002201 if (!*device)
2202 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002203
2204 return 0;
2205 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002206 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002207 }
2208}
2209
Yan Zheng2b820322008-11-17 21:11:30 -05002210/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002211 * Lookup a device given by device id, or the path if the id is 0.
2212 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002213int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002214 const char *devpath,
2215 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002216{
2217 int ret;
2218
David Sterba5c5c0df2016-02-15 16:39:55 +01002219 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002220 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002221 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002222 if (!*device)
2223 ret = -ENOENT;
2224 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002225 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002226 return -EINVAL;
2227
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002228 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002229 device);
2230 }
2231 return ret;
2232}
2233
Yan Zheng2b820322008-11-17 21:11:30 -05002234/*
2235 * does all the dirty work required for changing file system's UUID.
2236 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002237static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002238{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002239 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002240 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002241 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002242 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002243 struct btrfs_device *device;
2244 u64 super_flags;
2245
2246 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002247 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002248 return -EINVAL;
2249
David Sterba2dfeca92017-06-14 02:48:07 +02002250 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002251 if (IS_ERR(seed_devices))
2252 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002253
Yan Zhenge4404d62008-12-12 10:03:26 -05002254 old_devices = clone_fs_devices(fs_devices);
2255 if (IS_ERR(old_devices)) {
2256 kfree(seed_devices);
2257 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002258 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002259
Yan Zheng2b820322008-11-17 21:11:30 -05002260 list_add(&old_devices->list, &fs_uuids);
2261
Yan Zhenge4404d62008-12-12 10:03:26 -05002262 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2263 seed_devices->opened = 1;
2264 INIT_LIST_HEAD(&seed_devices->devices);
2265 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002266 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002267
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002268 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002269 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2270 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002271 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002272 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002273
David Sterba34441362016-10-04 19:34:27 +02002274 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002275 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002276 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002277
Yan Zheng2b820322008-11-17 21:11:30 -05002278 fs_devices->seeding = 0;
2279 fs_devices->num_devices = 0;
2280 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002281 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002282 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002283 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002284
2285 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002286 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002287 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002288 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002289
Yan Zheng2b820322008-11-17 21:11:30 -05002290 super_flags = btrfs_super_flags(disk_super) &
2291 ~BTRFS_SUPER_FLAG_SEEDING;
2292 btrfs_set_super_flags(disk_super, super_flags);
2293
2294 return 0;
2295}
2296
2297/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002298 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002299 */
2300static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002301 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002302{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002303 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002304 struct btrfs_path *path;
2305 struct extent_buffer *leaf;
2306 struct btrfs_dev_item *dev_item;
2307 struct btrfs_device *device;
2308 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002309 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002310 u8 dev_uuid[BTRFS_UUID_SIZE];
2311 u64 devid;
2312 int ret;
2313
2314 path = btrfs_alloc_path();
2315 if (!path)
2316 return -ENOMEM;
2317
Yan Zheng2b820322008-11-17 21:11:30 -05002318 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2319 key.offset = 0;
2320 key.type = BTRFS_DEV_ITEM_KEY;
2321
2322 while (1) {
2323 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2324 if (ret < 0)
2325 goto error;
2326
2327 leaf = path->nodes[0];
2328next_slot:
2329 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2330 ret = btrfs_next_leaf(root, path);
2331 if (ret > 0)
2332 break;
2333 if (ret < 0)
2334 goto error;
2335 leaf = path->nodes[0];
2336 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002337 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002338 continue;
2339 }
2340
2341 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2342 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2343 key.type != BTRFS_DEV_ITEM_KEY)
2344 break;
2345
2346 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2347 struct btrfs_dev_item);
2348 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002349 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002350 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002351 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002352 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002353 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002354 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002355
2356 if (device->fs_devices->seeding) {
2357 btrfs_set_device_generation(leaf, dev_item,
2358 device->generation);
2359 btrfs_mark_buffer_dirty(leaf);
2360 }
2361
2362 path->slots[0]++;
2363 goto next_slot;
2364 }
2365 ret = 0;
2366error:
2367 btrfs_free_path(path);
2368 return ret;
2369}
2370
David Sterbada353f62017-02-14 17:55:53 +01002371int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002372{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002373 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002374 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002375 struct btrfs_trans_handle *trans;
2376 struct btrfs_device *device;
2377 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002378 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002379 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002380 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002381 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002382 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002383 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002384 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002385
David Howellsbc98a422017-07-17 08:45:34 +01002386 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002387 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002388
Li Zefana5d16332011-12-07 20:08:40 -05002389 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002391 if (IS_ERR(bdev))
2392 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002395 seeding_dev = 1;
2396 down_write(&sb->s_umount);
2397 mutex_lock(&uuid_mutex);
2398 }
2399
Chris Mason8c8bee12008-09-29 11:19:10 -04002400 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002401
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002402 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002403
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002404 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002405 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002406 if (device->bdev == bdev) {
2407 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002408 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002410 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002411 }
2412 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002413 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002415 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002416 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002417 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002418 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002419 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002420 }
2421
David Sterba78f2c9e2016-02-11 14:25:38 +01002422 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002423 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002424 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002425 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002426 }
Josef Bacik606686e2012-06-04 14:03:51 -04002427 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002428
Yan, Zhenga22285a2010-05-16 10:48:46 -04002429 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002430 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002431 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002432 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002433 }
2434
Josef Bacikd5e20032011-08-04 14:52:27 +00002435 q = bdev_get_queue(bdev);
2436 if (blk_queue_discard(q))
2437 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002438 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002439 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002440 device->io_width = fs_info->sectorsize;
2441 device->io_align = fs_info->sectorsize;
2442 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002443 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2444 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002445 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002446 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002447 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002448 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002449 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002450 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002451 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002452 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002453 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002454
Yan Zheng2b820322008-11-17 21:11:30 -05002455 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002456 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002457 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002458 if (ret) {
2459 btrfs_abort_transaction(trans, ret);
2460 goto error_trans;
2461 }
Yan Zheng2b820322008-11-17 21:11:30 -05002462 }
2463
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002464 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002465
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002466 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002467 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002468 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002469 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 &fs_info->fs_devices->alloc_list);
2471 fs_info->fs_devices->num_devices++;
2472 fs_info->fs_devices->open_devices++;
2473 fs_info->fs_devices->rw_devices++;
2474 fs_info->fs_devices->total_devices++;
2475 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002476
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002477 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002478
Anand Jaine884f4f2017-04-04 18:40:19 +08002479 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002480 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002481
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002482 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2483 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002484 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002485
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 tmp = btrfs_super_num_devices(fs_info->super_copy);
2487 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002488
2489 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002491
Miao Xie2196d6e2014-09-03 21:35:41 +08002492 /*
2493 * we've got more storage, clear any full flags on the space
2494 * infos
2495 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002496 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002497
David Sterba34441362016-10-04 19:34:27 +02002498 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002500
Yan Zheng2b820322008-11-17 21:11:30 -05002501 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002502 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002503 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002504 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002505 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002506 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002507 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002508 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002509 }
2510
Anand Jainc74a0b02017-11-06 16:36:15 +08002511 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002512 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002513 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002514 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002515 }
2516
2517 if (seeding_dev) {
2518 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2519
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002520 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002521 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002522 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002523 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002524 }
Anand Jainb2373f22014-06-03 11:36:03 +08002525
2526 /* Sprouting would change fsid of the mounted root,
2527 * so rename the fsid on the sysfs
2528 */
2529 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002530 fs_info->fsid);
2531 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2532 btrfs_warn(fs_info,
2533 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002534 }
2535
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002536 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002537
2538 if (seeding_dev) {
2539 mutex_unlock(&uuid_mutex);
2540 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002541 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002542
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002543 if (ret) /* transaction commit */
2544 return ret;
2545
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002546 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002547 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002548 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002549 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002550 trans = btrfs_attach_transaction(root);
2551 if (IS_ERR(trans)) {
2552 if (PTR_ERR(trans) == -ENOENT)
2553 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002554 ret = PTR_ERR(trans);
2555 trans = NULL;
2556 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002557 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002558 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002559 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002560
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002561 /* Update ctime/mtime for libblkid */
2562 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002563 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002564
Anand Jaind31c32f2017-09-28 14:51:10 +08002565error_sysfs:
2566 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002567error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002568 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002569 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002570 if (trans)
2571 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002572error_free_device:
David Sterba55de4802017-10-30 18:55:47 +01002573 free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002574error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002575 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002576 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002577 mutex_unlock(&uuid_mutex);
2578 up_write(&sb->s_umount);
2579 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002580 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002581}
2582
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002583int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002584 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002585 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002586 struct btrfs_device **device_out)
2587{
2588 struct request_queue *q;
2589 struct btrfs_device *device;
2590 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002591 struct list_head *devices;
2592 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002593 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002594 int ret = 0;
2595
2596 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002597 if (fs_info->fs_devices->seeding) {
2598 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002599 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002600 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002601
2602 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2603 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002604 if (IS_ERR(bdev)) {
2605 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002606 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002607 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002608
2609 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2610
2611 devices = &fs_info->fs_devices->devices;
2612 list_for_each_entry(device, devices, dev_list) {
2613 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002614 btrfs_err(fs_info,
2615 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002616 ret = -EEXIST;
2617 goto error;
2618 }
2619 }
2620
Miao Xie1c433662014-09-03 21:35:32 +08002621
Miao Xie7cc8e582014-09-03 21:35:38 +08002622 if (i_size_read(bdev->bd_inode) <
2623 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002624 btrfs_err(fs_info,
2625 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002626 ret = -EINVAL;
2627 goto error;
2628 }
2629
2630
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002631 device = btrfs_alloc_device(NULL, &devid, NULL);
2632 if (IS_ERR(device)) {
2633 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002634 goto error;
2635 }
2636
David Sterba61655722017-06-15 17:16:43 +02002637 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002638 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +01002639 free_device(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002640 ret = -ENOMEM;
2641 goto error;
2642 }
2643 rcu_assign_pointer(device->name, name);
2644
2645 q = bdev_get_queue(bdev);
2646 if (blk_queue_discard(q))
2647 device->can_discard = 1;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002648 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002649 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002650 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002651 device->io_width = fs_info->sectorsize;
2652 device->io_align = fs_info->sectorsize;
2653 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002654 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2655 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2656 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002657 ASSERT(list_empty(&srcdev->resized_list));
2658 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002659 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002660 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002661 device->bdev = bdev;
2662 device->in_fs_metadata = 1;
2663 device->is_tgtdev_for_dev_replace = 1;
2664 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002665 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002666 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002667 device->fs_devices = fs_info->fs_devices;
2668 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2669 fs_info->fs_devices->num_devices++;
2670 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002671 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002672
2673 *device_out = device;
2674 return ret;
2675
2676error:
2677 blkdev_put(bdev, FMODE_EXCL);
2678 return ret;
2679}
2680
2681void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2682 struct btrfs_device *tgtdev)
2683{
Jeff Mahoneyda170662016-06-15 09:22:56 -04002684 u32 sectorsize = fs_info->sectorsize;
2685
Stefan Behrense93c89c2012-11-05 17:33:06 +01002686 WARN_ON(fs_info->fs_devices->rw_devices == 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002687 tgtdev->io_width = sectorsize;
2688 tgtdev->io_align = sectorsize;
2689 tgtdev->sector_size = sectorsize;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002690 tgtdev->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002691 tgtdev->in_fs_metadata = 1;
2692}
2693
Chris Masond3977122009-01-05 21:25:51 -05002694static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2695 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002696{
2697 int ret;
2698 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002700 struct btrfs_dev_item *dev_item;
2701 struct extent_buffer *leaf;
2702 struct btrfs_key key;
2703
Chris Mason0b86a832008-03-24 15:01:56 -04002704 path = btrfs_alloc_path();
2705 if (!path)
2706 return -ENOMEM;
2707
2708 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2709 key.type = BTRFS_DEV_ITEM_KEY;
2710 key.offset = device->devid;
2711
2712 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2713 if (ret < 0)
2714 goto out;
2715
2716 if (ret > 0) {
2717 ret = -ENOENT;
2718 goto out;
2719 }
2720
2721 leaf = path->nodes[0];
2722 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2723
2724 btrfs_set_device_id(leaf, dev_item, device->devid);
2725 btrfs_set_device_type(leaf, dev_item, device->type);
2726 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2727 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2728 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002729 btrfs_set_device_total_bytes(leaf, dev_item,
2730 btrfs_device_get_disk_total_bytes(device));
2731 btrfs_set_device_bytes_used(leaf, dev_item,
2732 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002733 btrfs_mark_buffer_dirty(leaf);
2734
2735out:
2736 btrfs_free_path(path);
2737 return ret;
2738}
2739
Miao Xie2196d6e2014-09-03 21:35:41 +08002740int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002741 struct btrfs_device *device, u64 new_size)
2742{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002743 struct btrfs_fs_info *fs_info = device->fs_info;
2744 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002745 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002746 u64 old_total;
2747 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002748
Yan Zheng2b820322008-11-17 21:11:30 -05002749 if (!device->writeable)
2750 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002751
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002752 new_size = round_down(new_size, fs_info->sectorsize);
2753
David Sterba34441362016-10-04 19:34:27 +02002754 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002755 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002756 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002757
Stefan Behrens63a212a2012-11-05 18:29:28 +01002758 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002759 device->is_tgtdev_for_dev_replace) {
David Sterba34441362016-10-04 19:34:27 +02002760 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002761 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002762 }
Yan Zheng2b820322008-11-17 21:11:30 -05002763
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002764 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002765
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002766 btrfs_set_super_total_bytes(super_copy,
2767 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002768 device->fs_devices->total_rw_bytes += diff;
2769
Miao Xie7cc8e582014-09-03 21:35:38 +08002770 btrfs_device_set_total_bytes(device, new_size);
2771 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002772 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002773 if (list_empty(&device->resized_list))
2774 list_add_tail(&device->resized_list,
2775 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002776 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002777
Chris Mason8f18cf12008-04-25 16:53:30 -04002778 return btrfs_update_device(trans, device);
2779}
2780
2781static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002782 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002783{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002784 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002785 int ret;
2786 struct btrfs_path *path;
2787 struct btrfs_key key;
2788
Chris Mason8f18cf12008-04-25 16:53:30 -04002789 path = btrfs_alloc_path();
2790 if (!path)
2791 return -ENOMEM;
2792
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002793 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002794 key.offset = chunk_offset;
2795 key.type = BTRFS_CHUNK_ITEM_KEY;
2796
2797 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002798 if (ret < 0)
2799 goto out;
2800 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002801 btrfs_handle_fs_error(fs_info, -ENOENT,
2802 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002803 ret = -ENOENT;
2804 goto out;
2805 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002806
2807 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002808 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 btrfs_handle_fs_error(fs_info, ret,
2810 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002811out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002812 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002813 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002814}
2815
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002816static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002817{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002818 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002819 struct btrfs_disk_key *disk_key;
2820 struct btrfs_chunk *chunk;
2821 u8 *ptr;
2822 int ret = 0;
2823 u32 num_stripes;
2824 u32 array_size;
2825 u32 len = 0;
2826 u32 cur;
2827 struct btrfs_key key;
2828
David Sterba34441362016-10-04 19:34:27 +02002829 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002830 array_size = btrfs_super_sys_array_size(super_copy);
2831
2832 ptr = super_copy->sys_chunk_array;
2833 cur = 0;
2834
2835 while (cur < array_size) {
2836 disk_key = (struct btrfs_disk_key *)ptr;
2837 btrfs_disk_key_to_cpu(&key, disk_key);
2838
2839 len = sizeof(*disk_key);
2840
2841 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2842 chunk = (struct btrfs_chunk *)(ptr + len);
2843 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2844 len += btrfs_chunk_item_size(num_stripes);
2845 } else {
2846 ret = -EIO;
2847 break;
2848 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002849 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002850 key.offset == chunk_offset) {
2851 memmove(ptr, ptr + len, array_size - (cur + len));
2852 array_size -= len;
2853 btrfs_set_super_sys_array_size(super_copy, array_size);
2854 } else {
2855 ptr += len;
2856 cur += len;
2857 }
2858 }
David Sterba34441362016-10-04 19:34:27 +02002859 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002860 return ret;
2861}
2862
Liu Bo592d92e2017-03-14 13:33:55 -07002863static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2864 u64 logical, u64 length)
2865{
2866 struct extent_map_tree *em_tree;
2867 struct extent_map *em;
2868
2869 em_tree = &fs_info->mapping_tree.map_tree;
2870 read_lock(&em_tree->lock);
2871 em = lookup_extent_mapping(em_tree, logical, length);
2872 read_unlock(&em_tree->lock);
2873
2874 if (!em) {
2875 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2876 logical, length);
2877 return ERR_PTR(-EINVAL);
2878 }
2879
2880 if (em->start > logical || em->start + em->len < logical) {
2881 btrfs_crit(fs_info,
2882 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2883 logical, length, em->start, em->start + em->len);
2884 free_extent_map(em);
2885 return ERR_PTR(-EINVAL);
2886 }
2887
2888 /* callers are responsible for dropping em's ref. */
2889 return em;
2890}
2891
Josef Bacik47ab2a62014-09-18 11:20:02 -04002892int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002893 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002895 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002896 struct map_lookup *map;
2897 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002898 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002899 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002900
Liu Bo592d92e2017-03-14 13:33:55 -07002901 em = get_chunk_map(fs_info, chunk_offset, 1);
2902 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002903 /*
2904 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002905 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002906 * do anything we still error out.
2907 */
2908 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002909 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002910 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002911 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002912 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002913 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002914 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002915
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002916 /*
2917 * Take the device list mutex to prevent races with the final phase of
2918 * a device replace operation that replaces the device object associated
2919 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2920 */
2921 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002922 for (i = 0; i < map->num_stripes; i++) {
2923 struct btrfs_device *device = map->stripes[i].dev;
2924 ret = btrfs_free_dev_extent(trans, device,
2925 map->stripes[i].physical,
2926 &dev_extent_len);
2927 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002928 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002929 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002930 goto out;
2931 }
2932
2933 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002934 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002935 btrfs_device_set_bytes_used(device,
2936 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002937 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002938 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002939 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002940 }
2941
2942 if (map->stripes[i].dev) {
2943 ret = btrfs_update_device(trans, map->stripes[i].dev);
2944 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002945 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002946 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002947 goto out;
2948 }
2949 }
2950 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002951 mutex_unlock(&fs_devices->device_list_mutex);
2952
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002953 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002954 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002955 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002956 goto out;
2957 }
2958
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002959 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002960
2961 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002962 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002963 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002964 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002965 goto out;
2966 }
2967 }
2968
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002969 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002970 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002971 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002972 goto out;
2973 }
2974
Josef Bacik47ab2a62014-09-18 11:20:02 -04002975out:
2976 /* once for us */
2977 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002978 return ret;
2979}
2980
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002981static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002982{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002983 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002984 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002985 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002986
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002987 /*
2988 * Prevent races with automatic removal of unused block groups.
2989 * After we relocate and before we remove the chunk with offset
2990 * chunk_offset, automatic removal of the block group can kick in,
2991 * resulting in a failure when calling btrfs_remove_chunk() below.
2992 *
2993 * Make sure to acquire this mutex before doing a tree search (dev
2994 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2995 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2996 * we release the path used to search the chunk/dev tree and before
2997 * the current task acquires this mutex and calls us.
2998 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002999 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003000
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003001 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003002 if (ret)
3003 return -ENOSPC;
3004
Chris Mason8f18cf12008-04-25 16:53:30 -04003005 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003006 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003007 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003008 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003009 if (ret)
3010 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003011
Chris Mason19c4d2f2016-10-10 13:43:31 -07003012 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3013 chunk_offset);
3014 if (IS_ERR(trans)) {
3015 ret = PTR_ERR(trans);
3016 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3017 return ret;
3018 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003019
Chris Mason19c4d2f2016-10-10 13:43:31 -07003020 /*
3021 * step two, delete the device extents and the
3022 * chunk tree entries
3023 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003024 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003025 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003026 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003027}
3028
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003029static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003030{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003031 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003032 struct btrfs_path *path;
3033 struct extent_buffer *leaf;
3034 struct btrfs_chunk *chunk;
3035 struct btrfs_key key;
3036 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003037 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003038 bool retried = false;
3039 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003040 int ret;
3041
3042 path = btrfs_alloc_path();
3043 if (!path)
3044 return -ENOMEM;
3045
Josef Bacikba1bf482009-09-11 16:11:19 -04003046again:
Yan Zheng2b820322008-11-17 21:11:30 -05003047 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3048 key.offset = (u64)-1;
3049 key.type = BTRFS_CHUNK_ITEM_KEY;
3050
3051 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003053 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003054 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003056 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003057 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003058 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003059
3060 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3061 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003062 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003063 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003064 if (ret < 0)
3065 goto error;
3066 if (ret > 0)
3067 break;
3068
3069 leaf = path->nodes[0];
3070 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3071
3072 chunk = btrfs_item_ptr(leaf, path->slots[0],
3073 struct btrfs_chunk);
3074 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003075 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003076
3077 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003078 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003079 if (ret == -ENOSPC)
3080 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303081 else
3082 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003083 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003084 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003085
3086 if (found_key.offset == 0)
3087 break;
3088 key.offset = found_key.offset - 1;
3089 }
3090 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003091 if (failed && !retried) {
3092 failed = 0;
3093 retried = true;
3094 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303095 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003096 ret = -ENOSPC;
3097 }
Yan Zheng2b820322008-11-17 21:11:30 -05003098error:
3099 btrfs_free_path(path);
3100 return ret;
3101}
3102
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003103static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003104 struct btrfs_balance_control *bctl)
3105{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003106 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003107 struct btrfs_trans_handle *trans;
3108 struct btrfs_balance_item *item;
3109 struct btrfs_disk_balance_args disk_bargs;
3110 struct btrfs_path *path;
3111 struct extent_buffer *leaf;
3112 struct btrfs_key key;
3113 int ret, err;
3114
3115 path = btrfs_alloc_path();
3116 if (!path)
3117 return -ENOMEM;
3118
3119 trans = btrfs_start_transaction(root, 0);
3120 if (IS_ERR(trans)) {
3121 btrfs_free_path(path);
3122 return PTR_ERR(trans);
3123 }
3124
3125 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003126 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003127 key.offset = 0;
3128
3129 ret = btrfs_insert_empty_item(trans, root, path, &key,
3130 sizeof(*item));
3131 if (ret)
3132 goto out;
3133
3134 leaf = path->nodes[0];
3135 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3136
David Sterbab159fa22016-11-08 18:09:03 +01003137 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003138
3139 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3140 btrfs_set_balance_data(leaf, item, &disk_bargs);
3141 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3142 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3143 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3144 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3145
3146 btrfs_set_balance_flags(leaf, item, bctl->flags);
3147
3148 btrfs_mark_buffer_dirty(leaf);
3149out:
3150 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003151 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003152 if (err && !ret)
3153 ret = err;
3154 return ret;
3155}
3156
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003157static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003158{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003159 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003160 struct btrfs_trans_handle *trans;
3161 struct btrfs_path *path;
3162 struct btrfs_key key;
3163 int ret, err;
3164
3165 path = btrfs_alloc_path();
3166 if (!path)
3167 return -ENOMEM;
3168
3169 trans = btrfs_start_transaction(root, 0);
3170 if (IS_ERR(trans)) {
3171 btrfs_free_path(path);
3172 return PTR_ERR(trans);
3173 }
3174
3175 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003176 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003177 key.offset = 0;
3178
3179 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3180 if (ret < 0)
3181 goto out;
3182 if (ret > 0) {
3183 ret = -ENOENT;
3184 goto out;
3185 }
3186
3187 ret = btrfs_del_item(trans, root, path);
3188out:
3189 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003190 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003191 if (err && !ret)
3192 ret = err;
3193 return ret;
3194}
3195
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003196/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003197 * This is a heuristic used to reduce the number of chunks balanced on
3198 * resume after balance was interrupted.
3199 */
3200static void update_balance_args(struct btrfs_balance_control *bctl)
3201{
3202 /*
3203 * Turn on soft mode for chunk types that were being converted.
3204 */
3205 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3206 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3207 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3208 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3209 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3210 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3211
3212 /*
3213 * Turn on usage filter if is not already used. The idea is
3214 * that chunks that we have already balanced should be
3215 * reasonably full. Don't do it for chunks that are being
3216 * converted - that will keep us from relocating unconverted
3217 * (albeit full) chunks.
3218 */
3219 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003220 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003221 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3222 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3223 bctl->data.usage = 90;
3224 }
3225 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003226 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003227 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3228 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3229 bctl->sys.usage = 90;
3230 }
3231 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003232 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003233 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3234 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3235 bctl->meta.usage = 90;
3236 }
3237}
3238
3239/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003240 * Should be called with both balance and volume mutexes held to
3241 * serialize other volume operations (add_dev/rm_dev/resize) with
3242 * restriper. Same goes for unset_balance_control.
3243 */
3244static void set_balance_control(struct btrfs_balance_control *bctl)
3245{
3246 struct btrfs_fs_info *fs_info = bctl->fs_info;
3247
3248 BUG_ON(fs_info->balance_ctl);
3249
3250 spin_lock(&fs_info->balance_lock);
3251 fs_info->balance_ctl = bctl;
3252 spin_unlock(&fs_info->balance_lock);
3253}
3254
3255static void unset_balance_control(struct btrfs_fs_info *fs_info)
3256{
3257 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3258
3259 BUG_ON(!fs_info->balance_ctl);
3260
3261 spin_lock(&fs_info->balance_lock);
3262 fs_info->balance_ctl = NULL;
3263 spin_unlock(&fs_info->balance_lock);
3264
3265 kfree(bctl);
3266}
3267
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003268/*
3269 * Balance filters. Return 1 if chunk should be filtered out
3270 * (should not be balanced).
3271 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003272static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003273 struct btrfs_balance_args *bargs)
3274{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003275 chunk_type = chunk_to_extended(chunk_type) &
3276 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003277
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003278 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003279 return 0;
3280
3281 return 1;
3282}
3283
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003284static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003285 struct btrfs_balance_args *bargs)
3286{
3287 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003288 u64 chunk_used;
3289 u64 user_thresh_min;
3290 u64 user_thresh_max;
3291 int ret = 1;
3292
3293 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3294 chunk_used = btrfs_block_group_used(&cache->item);
3295
3296 if (bargs->usage_min == 0)
3297 user_thresh_min = 0;
3298 else
3299 user_thresh_min = div_factor_fine(cache->key.offset,
3300 bargs->usage_min);
3301
3302 if (bargs->usage_max == 0)
3303 user_thresh_max = 1;
3304 else if (bargs->usage_max > 100)
3305 user_thresh_max = cache->key.offset;
3306 else
3307 user_thresh_max = div_factor_fine(cache->key.offset,
3308 bargs->usage_max);
3309
3310 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3311 ret = 0;
3312
3313 btrfs_put_block_group(cache);
3314 return ret;
3315}
3316
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003317static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003318 u64 chunk_offset, struct btrfs_balance_args *bargs)
3319{
3320 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003321 u64 chunk_used, user_thresh;
3322 int ret = 1;
3323
3324 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3325 chunk_used = btrfs_block_group_used(&cache->item);
3326
David Sterbabc309462015-10-20 18:22:13 +02003327 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003328 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003329 else if (bargs->usage > 100)
3330 user_thresh = cache->key.offset;
3331 else
3332 user_thresh = div_factor_fine(cache->key.offset,
3333 bargs->usage);
3334
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003335 if (chunk_used < user_thresh)
3336 ret = 0;
3337
3338 btrfs_put_block_group(cache);
3339 return ret;
3340}
3341
Ilya Dryomov409d4042012-01-16 22:04:47 +02003342static int chunk_devid_filter(struct extent_buffer *leaf,
3343 struct btrfs_chunk *chunk,
3344 struct btrfs_balance_args *bargs)
3345{
3346 struct btrfs_stripe *stripe;
3347 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3348 int i;
3349
3350 for (i = 0; i < num_stripes; i++) {
3351 stripe = btrfs_stripe_nr(chunk, i);
3352 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3353 return 0;
3354 }
3355
3356 return 1;
3357}
3358
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003359/* [pstart, pend) */
3360static int chunk_drange_filter(struct extent_buffer *leaf,
3361 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003362 struct btrfs_balance_args *bargs)
3363{
3364 struct btrfs_stripe *stripe;
3365 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3366 u64 stripe_offset;
3367 u64 stripe_length;
3368 int factor;
3369 int i;
3370
3371 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3372 return 0;
3373
3374 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003375 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3376 factor = num_stripes / 2;
3377 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3378 factor = num_stripes - 1;
3379 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3380 factor = num_stripes - 2;
3381 } else {
3382 factor = num_stripes;
3383 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003384
3385 for (i = 0; i < num_stripes; i++) {
3386 stripe = btrfs_stripe_nr(chunk, i);
3387 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3388 continue;
3389
3390 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3391 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003392 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003393
3394 if (stripe_offset < bargs->pend &&
3395 stripe_offset + stripe_length > bargs->pstart)
3396 return 0;
3397 }
3398
3399 return 1;
3400}
3401
Ilya Dryomovea671762012-01-16 22:04:48 +02003402/* [vstart, vend) */
3403static int chunk_vrange_filter(struct extent_buffer *leaf,
3404 struct btrfs_chunk *chunk,
3405 u64 chunk_offset,
3406 struct btrfs_balance_args *bargs)
3407{
3408 if (chunk_offset < bargs->vend &&
3409 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3410 /* at least part of the chunk is inside this vrange */
3411 return 0;
3412
3413 return 1;
3414}
3415
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003416static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3417 struct btrfs_chunk *chunk,
3418 struct btrfs_balance_args *bargs)
3419{
3420 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3421
3422 if (bargs->stripes_min <= num_stripes
3423 && num_stripes <= bargs->stripes_max)
3424 return 0;
3425
3426 return 1;
3427}
3428
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003429static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003430 struct btrfs_balance_args *bargs)
3431{
3432 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3433 return 0;
3434
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003435 chunk_type = chunk_to_extended(chunk_type) &
3436 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003437
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003438 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003439 return 1;
3440
3441 return 0;
3442}
3443
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003444static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003445 struct extent_buffer *leaf,
3446 struct btrfs_chunk *chunk, u64 chunk_offset)
3447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003448 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003449 struct btrfs_balance_args *bargs = NULL;
3450 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3451
3452 /* type filter */
3453 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3454 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3455 return 0;
3456 }
3457
3458 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3459 bargs = &bctl->data;
3460 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3461 bargs = &bctl->sys;
3462 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3463 bargs = &bctl->meta;
3464
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003465 /* profiles filter */
3466 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3467 chunk_profiles_filter(chunk_type, bargs)) {
3468 return 0;
3469 }
3470
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003471 /* usage filter */
3472 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003473 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003474 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003475 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003476 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003477 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003478 }
3479
Ilya Dryomov409d4042012-01-16 22:04:47 +02003480 /* devid filter */
3481 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3482 chunk_devid_filter(leaf, chunk, bargs)) {
3483 return 0;
3484 }
3485
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003486 /* drange filter, makes sense only with devid filter */
3487 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003488 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003489 return 0;
3490 }
3491
Ilya Dryomovea671762012-01-16 22:04:48 +02003492 /* vrange filter */
3493 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3494 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3495 return 0;
3496 }
3497
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003498 /* stripes filter */
3499 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3500 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3501 return 0;
3502 }
3503
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003504 /* soft profile changing mode */
3505 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3506 chunk_soft_convert_filter(chunk_type, bargs)) {
3507 return 0;
3508 }
3509
David Sterba7d824b62014-05-07 17:37:51 +02003510 /*
3511 * limited by count, must be the last filter
3512 */
3513 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3514 if (bargs->limit == 0)
3515 return 0;
3516 else
3517 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003518 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3519 /*
3520 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003521 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003522 * about the count of all chunks that satisfy the filters.
3523 */
3524 if (bargs->limit_max == 0)
3525 return 0;
3526 else
3527 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003528 }
3529
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003530 return 1;
3531}
3532
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003533static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003534{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003535 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003536 struct btrfs_root *chunk_root = fs_info->chunk_root;
3537 struct btrfs_root *dev_root = fs_info->dev_root;
3538 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003539 struct btrfs_device *device;
3540 u64 old_size;
3541 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003542 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003543 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003544 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003545 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003546 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003547 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003548 struct extent_buffer *leaf;
3549 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003550 int ret;
3551 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003552 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003553 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003554 u64 limit_data = bctl->data.limit;
3555 u64 limit_meta = bctl->meta.limit;
3556 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003557 u32 count_data = 0;
3558 u32 count_meta = 0;
3559 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003560 int chunk_reserved = 0;
Liu Bocf25ce52015-12-14 18:29:32 -08003561 u64 bytes_used = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003562
Chris Masonec44a352008-04-28 15:29:52 -04003563 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003564 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003565 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003566 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003567 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003568 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003569 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003570 btrfs_device_get_total_bytes(device) -
3571 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003572 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003573 continue;
3574
3575 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003576 if (ret == -ENOSPC)
3577 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003578 if (ret) {
3579 /* btrfs_shrink_device never returns ret > 0 */
3580 WARN_ON(ret > 0);
3581 goto error;
3582 }
Chris Masonec44a352008-04-28 15:29:52 -04003583
Yan, Zhenga22285a2010-05-16 10:48:46 -04003584 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003585 if (IS_ERR(trans)) {
3586 ret = PTR_ERR(trans);
3587 btrfs_info_in_rcu(fs_info,
3588 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3589 rcu_str_deref(device->name), ret,
3590 old_size, old_size - size_to_free);
3591 goto error;
3592 }
Chris Masonec44a352008-04-28 15:29:52 -04003593
3594 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003595 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003596 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003597 /* btrfs_grow_device never returns ret > 0 */
3598 WARN_ON(ret > 0);
3599 btrfs_info_in_rcu(fs_info,
3600 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3601 rcu_str_deref(device->name), ret,
3602 old_size, old_size - size_to_free);
3603 goto error;
3604 }
Chris Masonec44a352008-04-28 15:29:52 -04003605
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003606 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003607 }
3608
3609 /* step two, relocate all the chunks */
3610 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003611 if (!path) {
3612 ret = -ENOMEM;
3613 goto error;
3614 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003615
3616 /* zero out stat counters */
3617 spin_lock(&fs_info->balance_lock);
3618 memset(&bctl->stat, 0, sizeof(bctl->stat));
3619 spin_unlock(&fs_info->balance_lock);
3620again:
David Sterba7d824b62014-05-07 17:37:51 +02003621 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003622 /*
3623 * The single value limit and min/max limits use the same bytes
3624 * in the
3625 */
David Sterba7d824b62014-05-07 17:37:51 +02003626 bctl->data.limit = limit_data;
3627 bctl->meta.limit = limit_meta;
3628 bctl->sys.limit = limit_sys;
3629 }
Chris Masonec44a352008-04-28 15:29:52 -04003630 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3631 key.offset = (u64)-1;
3632 key.type = BTRFS_CHUNK_ITEM_KEY;
3633
Chris Masond3977122009-01-05 21:25:51 -05003634 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003635 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003636 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003637 ret = -ECANCELED;
3638 goto error;
3639 }
3640
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003641 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003642 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003643 if (ret < 0) {
3644 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003645 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003646 }
Chris Masonec44a352008-04-28 15:29:52 -04003647
3648 /*
3649 * this shouldn't happen, it means the last relocate
3650 * failed
3651 */
3652 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003653 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003654
3655 ret = btrfs_previous_item(chunk_root, path, 0,
3656 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003657 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003658 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003659 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003660 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003661 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003662
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003663 leaf = path->nodes[0];
3664 slot = path->slots[0];
3665 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3666
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003667 if (found_key.objectid != key.objectid) {
3668 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003669 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003670 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003671
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003672 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003673 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003674
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003675 if (!counting) {
3676 spin_lock(&fs_info->balance_lock);
3677 bctl->stat.considered++;
3678 spin_unlock(&fs_info->balance_lock);
3679 }
3680
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003681 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003682 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003683
David Sterbab3b4aa72011-04-21 01:20:15 +02003684 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003685 if (!ret) {
3686 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003687 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003688 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003689
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003690 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003691 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003692 spin_lock(&fs_info->balance_lock);
3693 bctl->stat.expected++;
3694 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003695
3696 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3697 count_data++;
3698 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3699 count_sys++;
3700 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3701 count_meta++;
3702
3703 goto loop;
3704 }
3705
3706 /*
3707 * Apply limit_min filter, no need to check if the LIMITS
3708 * filter is used, limit_min is 0 by default
3709 */
3710 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3711 count_data < bctl->data.limit_min)
3712 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3713 count_meta < bctl->meta.limit_min)
3714 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3715 count_sys < bctl->sys.limit_min)) {
3716 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003717 goto loop;
3718 }
3719
Liu Bocf25ce52015-12-14 18:29:32 -08003720 ASSERT(fs_info->data_sinfo);
3721 spin_lock(&fs_info->data_sinfo->lock);
3722 bytes_used = fs_info->data_sinfo->bytes_used;
3723 spin_unlock(&fs_info->data_sinfo->lock);
3724
3725 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3726 !chunk_reserved && !bytes_used) {
Zhao Lei2c9fe832015-11-09 11:51:32 +08003727 trans = btrfs_start_transaction(chunk_root, 0);
3728 if (IS_ERR(trans)) {
3729 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3730 ret = PTR_ERR(trans);
3731 goto error;
3732 }
3733
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003734 ret = btrfs_force_chunk_alloc(trans, fs_info,
Zhao Lei2c9fe832015-11-09 11:51:32 +08003735 BTRFS_BLOCK_GROUP_DATA);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003736 btrfs_end_transaction(trans);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003737 if (ret < 0) {
3738 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3739 goto error;
3740 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003741 chunk_reserved = 1;
3742 }
3743
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003744 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003745 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003746 if (ret && ret != -ENOSPC)
3747 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003748 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003749 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003750 } else {
3751 spin_lock(&fs_info->balance_lock);
3752 bctl->stat.completed++;
3753 spin_unlock(&fs_info->balance_lock);
3754 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003755loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003756 if (found_key.offset == 0)
3757 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003758 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003759 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003760
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003761 if (counting) {
3762 btrfs_release_path(path);
3763 counting = false;
3764 goto again;
3765 }
Chris Masonec44a352008-04-28 15:29:52 -04003766error:
3767 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003768 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003769 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003770 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003771 if (!ret)
3772 ret = -ENOSPC;
3773 }
3774
Chris Masonec44a352008-04-28 15:29:52 -04003775 return ret;
3776}
3777
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003778/**
3779 * alloc_profile_is_valid - see if a given profile is valid and reduced
3780 * @flags: profile to validate
3781 * @extended: if true @flags is treated as an extended profile
3782 */
3783static int alloc_profile_is_valid(u64 flags, int extended)
3784{
3785 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3786 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3787
3788 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3789
3790 /* 1) check that all other bits are zeroed */
3791 if (flags & ~mask)
3792 return 0;
3793
3794 /* 2) see if profile is reduced */
3795 if (flags == 0)
3796 return !extended; /* "0" is valid for usual profiles */
3797
3798 /* true if exactly one bit set */
3799 return (flags & (flags - 1)) == 0;
3800}
3801
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003802static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3803{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003804 /* cancel requested || normal exit path */
3805 return atomic_read(&fs_info->balance_cancel_req) ||
3806 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3807 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003808}
3809
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003810static void __cancel_balance(struct btrfs_fs_info *fs_info)
3811{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003812 int ret;
3813
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003814 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003815 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003816 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003817 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003818
David Sterba171938e2017-03-28 14:44:21 +02003819 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003820}
3821
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003822/* Non-zero return value signifies invalidity */
3823static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3824 u64 allowed)
3825{
3826 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3827 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3828 (bctl_arg->target & ~allowed)));
3829}
3830
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003831/*
3832 * Should be called with both balance and volume mutexes held
3833 */
3834int btrfs_balance(struct btrfs_balance_control *bctl,
3835 struct btrfs_ioctl_balance_args *bargs)
3836{
3837 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003838 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003839 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003840 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003841 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003842 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003843 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003844
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003845 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003846 atomic_read(&fs_info->balance_pause_req) ||
3847 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003848 ret = -EINVAL;
3849 goto out;
3850 }
3851
Ilya Dryomove4837f82012-03-27 17:09:17 +03003852 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3853 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3854 mixed = 1;
3855
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003856 /*
3857 * In case of mixed groups both data and meta should be picked,
3858 * and identical options should be given for both of them.
3859 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003860 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3861 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003862 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3863 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3864 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003865 btrfs_err(fs_info,
3866 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003867 ret = -EINVAL;
3868 goto out;
3869 }
3870 }
3871
Stefan Behrens8dabb742012-11-06 13:15:27 +01003872 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003873 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003874 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3875 BUG_ON(num_devices < 1);
3876 num_devices--;
3877 }
Liu Bo73beece2015-07-17 16:49:19 +08003878 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003879 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3880 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003881 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003882 if (num_devices > 2)
3883 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3884 if (num_devices > 3)
3885 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3886 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003887 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003888 btrfs_err(fs_info,
3889 "unable to start balance with target data profile %llu",
3890 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003891 ret = -EINVAL;
3892 goto out;
3893 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003894 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003895 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003896 "unable to start balance with target metadata profile %llu",
3897 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003898 ret = -EINVAL;
3899 goto out;
3900 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003901 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003902 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003903 "unable to start balance with target system profile %llu",
3904 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003905 ret = -EINVAL;
3906 goto out;
3907 }
3908
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003909 /* allow to reduce meta or sys integrity only if force set */
3910 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003911 BTRFS_BLOCK_GROUP_RAID10 |
3912 BTRFS_BLOCK_GROUP_RAID5 |
3913 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003914 do {
3915 seq = read_seqbegin(&fs_info->profiles_lock);
3916
3917 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3918 (fs_info->avail_system_alloc_bits & allowed) &&
3919 !(bctl->sys.target & allowed)) ||
3920 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3921 (fs_info->avail_metadata_alloc_bits & allowed) &&
3922 !(bctl->meta.target & allowed))) {
3923 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003924 btrfs_info(fs_info,
3925 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003926 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003927 btrfs_err(fs_info,
3928 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003929 ret = -EINVAL;
3930 goto out;
3931 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003932 }
Miao Xiede98ced2013-01-29 10:13:12 +00003933 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003934
Adam Borowski14506122017-03-07 23:34:44 +01003935 /* if we're not converting, the target field is uninitialized */
3936 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3937 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3938 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3939 bctl->data.target : fs_info->avail_data_alloc_bits;
3940 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3941 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003942 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003943 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003944 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003945 }
3946
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003947 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003948 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003949 goto out;
3950
Ilya Dryomov59641012012-01-16 22:04:48 +02003951 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3952 BUG_ON(ret == -EEXIST);
3953 set_balance_control(bctl);
3954 } else {
3955 BUG_ON(ret != -EEXIST);
3956 spin_lock(&fs_info->balance_lock);
3957 update_balance_args(bctl);
3958 spin_unlock(&fs_info->balance_lock);
3959 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003960
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003961 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003962 mutex_unlock(&fs_info->balance_mutex);
3963
3964 ret = __btrfs_balance(fs_info);
3965
3966 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003967 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003968
3969 if (bargs) {
3970 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003971 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003972 }
3973
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003974 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3975 balance_need_close(fs_info)) {
3976 __cancel_balance(fs_info);
3977 }
3978
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003979 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003980
3981 return ret;
3982out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003983 if (bctl->flags & BTRFS_BALANCE_RESUME)
3984 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003985 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003986 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02003987 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003988 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003989 return ret;
3990}
3991
3992static int balance_kthread(void *data)
3993{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003994 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003995 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003996
3997 mutex_lock(&fs_info->volume_mutex);
3998 mutex_lock(&fs_info->balance_mutex);
3999
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004000 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004001 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004002 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004003 }
Ilya Dryomov59641012012-01-16 22:04:48 +02004004
4005 mutex_unlock(&fs_info->balance_mutex);
4006 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004007
Ilya Dryomov59641012012-01-16 22:04:48 +02004008 return ret;
4009}
4010
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004011int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4012{
4013 struct task_struct *tsk;
4014
4015 spin_lock(&fs_info->balance_lock);
4016 if (!fs_info->balance_ctl) {
4017 spin_unlock(&fs_info->balance_lock);
4018 return 0;
4019 }
4020 spin_unlock(&fs_info->balance_lock);
4021
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004022 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004023 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004024 return 0;
4025 }
4026
4027 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304028 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004029}
4030
Ilya Dryomov68310a52012-06-22 12:24:12 -06004031int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004032{
Ilya Dryomov59641012012-01-16 22:04:48 +02004033 struct btrfs_balance_control *bctl;
4034 struct btrfs_balance_item *item;
4035 struct btrfs_disk_balance_args disk_bargs;
4036 struct btrfs_path *path;
4037 struct extent_buffer *leaf;
4038 struct btrfs_key key;
4039 int ret;
4040
4041 path = btrfs_alloc_path();
4042 if (!path)
4043 return -ENOMEM;
4044
Ilya Dryomov68310a52012-06-22 12:24:12 -06004045 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004046 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004047 key.offset = 0;
4048
4049 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4050 if (ret < 0)
4051 goto out;
4052 if (ret > 0) { /* ret = -ENOENT; */
4053 ret = 0;
4054 goto out;
4055 }
4056
Ilya Dryomov59641012012-01-16 22:04:48 +02004057 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4058 if (!bctl) {
4059 ret = -ENOMEM;
4060 goto out;
4061 }
4062
Ilya Dryomov59641012012-01-16 22:04:48 +02004063 leaf = path->nodes[0];
4064 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4065
Ilya Dryomov68310a52012-06-22 12:24:12 -06004066 bctl->fs_info = fs_info;
4067 bctl->flags = btrfs_balance_flags(leaf, item);
4068 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004069
4070 btrfs_balance_data(leaf, item, &disk_bargs);
4071 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4072 btrfs_balance_meta(leaf, item, &disk_bargs);
4073 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4074 btrfs_balance_sys(leaf, item, &disk_bargs);
4075 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4076
David Sterba171938e2017-03-28 14:44:21 +02004077 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004078
Ilya Dryomov68310a52012-06-22 12:24:12 -06004079 mutex_lock(&fs_info->volume_mutex);
4080 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004081
Ilya Dryomov68310a52012-06-22 12:24:12 -06004082 set_balance_control(bctl);
4083
4084 mutex_unlock(&fs_info->balance_mutex);
4085 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004086out:
4087 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004088 return ret;
4089}
4090
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004091int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4092{
4093 int ret = 0;
4094
4095 mutex_lock(&fs_info->balance_mutex);
4096 if (!fs_info->balance_ctl) {
4097 mutex_unlock(&fs_info->balance_mutex);
4098 return -ENOTCONN;
4099 }
4100
4101 if (atomic_read(&fs_info->balance_running)) {
4102 atomic_inc(&fs_info->balance_pause_req);
4103 mutex_unlock(&fs_info->balance_mutex);
4104
4105 wait_event(fs_info->balance_wait_q,
4106 atomic_read(&fs_info->balance_running) == 0);
4107
4108 mutex_lock(&fs_info->balance_mutex);
4109 /* we are good with balance_ctl ripped off from under us */
4110 BUG_ON(atomic_read(&fs_info->balance_running));
4111 atomic_dec(&fs_info->balance_pause_req);
4112 } else {
4113 ret = -ENOTCONN;
4114 }
4115
4116 mutex_unlock(&fs_info->balance_mutex);
4117 return ret;
4118}
4119
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004120int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4121{
David Howellsbc98a422017-07-17 08:45:34 +01004122 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004123 return -EROFS;
4124
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004125 mutex_lock(&fs_info->balance_mutex);
4126 if (!fs_info->balance_ctl) {
4127 mutex_unlock(&fs_info->balance_mutex);
4128 return -ENOTCONN;
4129 }
4130
4131 atomic_inc(&fs_info->balance_cancel_req);
4132 /*
4133 * if we are running just wait and return, balance item is
4134 * deleted in btrfs_balance in this case
4135 */
4136 if (atomic_read(&fs_info->balance_running)) {
4137 mutex_unlock(&fs_info->balance_mutex);
4138 wait_event(fs_info->balance_wait_q,
4139 atomic_read(&fs_info->balance_running) == 0);
4140 mutex_lock(&fs_info->balance_mutex);
4141 } else {
4142 /* __cancel_balance needs volume_mutex */
4143 mutex_unlock(&fs_info->balance_mutex);
4144 mutex_lock(&fs_info->volume_mutex);
4145 mutex_lock(&fs_info->balance_mutex);
4146
4147 if (fs_info->balance_ctl)
4148 __cancel_balance(fs_info);
4149
4150 mutex_unlock(&fs_info->volume_mutex);
4151 }
4152
4153 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4154 atomic_dec(&fs_info->balance_cancel_req);
4155 mutex_unlock(&fs_info->balance_mutex);
4156 return 0;
4157}
4158
Stefan Behrens803b2f52013-08-15 17:11:21 +02004159static int btrfs_uuid_scan_kthread(void *data)
4160{
4161 struct btrfs_fs_info *fs_info = data;
4162 struct btrfs_root *root = fs_info->tree_root;
4163 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004164 struct btrfs_path *path = NULL;
4165 int ret = 0;
4166 struct extent_buffer *eb;
4167 int slot;
4168 struct btrfs_root_item root_item;
4169 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004170 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004171
4172 path = btrfs_alloc_path();
4173 if (!path) {
4174 ret = -ENOMEM;
4175 goto out;
4176 }
4177
4178 key.objectid = 0;
4179 key.type = BTRFS_ROOT_ITEM_KEY;
4180 key.offset = 0;
4181
Stefan Behrens803b2f52013-08-15 17:11:21 +02004182 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004183 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004184 if (ret) {
4185 if (ret > 0)
4186 ret = 0;
4187 break;
4188 }
4189
4190 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4191 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4192 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4193 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4194 goto skip;
4195
4196 eb = path->nodes[0];
4197 slot = path->slots[0];
4198 item_size = btrfs_item_size_nr(eb, slot);
4199 if (item_size < sizeof(root_item))
4200 goto skip;
4201
Stefan Behrens803b2f52013-08-15 17:11:21 +02004202 read_extent_buffer(eb, &root_item,
4203 btrfs_item_ptr_offset(eb, slot),
4204 (int)sizeof(root_item));
4205 if (btrfs_root_refs(&root_item) == 0)
4206 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004207
4208 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4209 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4210 if (trans)
4211 goto update_tree;
4212
4213 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004214 /*
4215 * 1 - subvol uuid item
4216 * 1 - received_subvol uuid item
4217 */
4218 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4219 if (IS_ERR(trans)) {
4220 ret = PTR_ERR(trans);
4221 break;
4222 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004223 continue;
4224 } else {
4225 goto skip;
4226 }
4227update_tree:
4228 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004229 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004230 root_item.uuid,
4231 BTRFS_UUID_KEY_SUBVOL,
4232 key.objectid);
4233 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004234 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004235 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004236 break;
4237 }
4238 }
4239
4240 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004241 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004242 root_item.received_uuid,
4243 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4244 key.objectid);
4245 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004246 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004247 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004248 break;
4249 }
4250 }
4251
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004252skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004253 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004254 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004255 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004256 if (ret)
4257 break;
4258 }
4259
Stefan Behrens803b2f52013-08-15 17:11:21 +02004260 btrfs_release_path(path);
4261 if (key.offset < (u64)-1) {
4262 key.offset++;
4263 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4264 key.offset = 0;
4265 key.type = BTRFS_ROOT_ITEM_KEY;
4266 } else if (key.objectid < (u64)-1) {
4267 key.offset = 0;
4268 key.type = BTRFS_ROOT_ITEM_KEY;
4269 key.objectid++;
4270 } else {
4271 break;
4272 }
4273 cond_resched();
4274 }
4275
4276out:
4277 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004278 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004279 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004280 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004281 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004282 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004283 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004284 up(&fs_info->uuid_tree_rescan_sem);
4285 return 0;
4286}
4287
Stefan Behrens70f80172013-08-15 17:11:23 +02004288/*
4289 * Callback for btrfs_uuid_tree_iterate().
4290 * returns:
4291 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004292 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004293 * > 0 if the check failed, which means the caller shall remove the entry.
4294 */
4295static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4296 u8 *uuid, u8 type, u64 subid)
4297{
4298 struct btrfs_key key;
4299 int ret = 0;
4300 struct btrfs_root *subvol_root;
4301
4302 if (type != BTRFS_UUID_KEY_SUBVOL &&
4303 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4304 goto out;
4305
4306 key.objectid = subid;
4307 key.type = BTRFS_ROOT_ITEM_KEY;
4308 key.offset = (u64)-1;
4309 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4310 if (IS_ERR(subvol_root)) {
4311 ret = PTR_ERR(subvol_root);
4312 if (ret == -ENOENT)
4313 ret = 1;
4314 goto out;
4315 }
4316
4317 switch (type) {
4318 case BTRFS_UUID_KEY_SUBVOL:
4319 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4320 ret = 1;
4321 break;
4322 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4323 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4324 BTRFS_UUID_SIZE))
4325 ret = 1;
4326 break;
4327 }
4328
4329out:
4330 return ret;
4331}
4332
4333static int btrfs_uuid_rescan_kthread(void *data)
4334{
4335 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4336 int ret;
4337
4338 /*
4339 * 1st step is to iterate through the existing UUID tree and
4340 * to delete all entries that contain outdated data.
4341 * 2nd step is to add all missing entries to the UUID tree.
4342 */
4343 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4344 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004345 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004346 up(&fs_info->uuid_tree_rescan_sem);
4347 return ret;
4348 }
4349 return btrfs_uuid_scan_kthread(data);
4350}
4351
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004352int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4353{
4354 struct btrfs_trans_handle *trans;
4355 struct btrfs_root *tree_root = fs_info->tree_root;
4356 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004357 struct task_struct *task;
4358 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004359
4360 /*
4361 * 1 - root node
4362 * 1 - root item
4363 */
4364 trans = btrfs_start_transaction(tree_root, 2);
4365 if (IS_ERR(trans))
4366 return PTR_ERR(trans);
4367
4368 uuid_root = btrfs_create_tree(trans, fs_info,
4369 BTRFS_UUID_TREE_OBJECTID);
4370 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004371 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004372 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004373 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004374 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004375 }
4376
4377 fs_info->uuid_root = uuid_root;
4378
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004379 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004380 if (ret)
4381 return ret;
4382
4383 down(&fs_info->uuid_tree_rescan_sem);
4384 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4385 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004386 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004387 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004388 up(&fs_info->uuid_tree_rescan_sem);
4389 return PTR_ERR(task);
4390 }
4391
4392 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004393}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004394
Stefan Behrens70f80172013-08-15 17:11:23 +02004395int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4396{
4397 struct task_struct *task;
4398
4399 down(&fs_info->uuid_tree_rescan_sem);
4400 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4401 if (IS_ERR(task)) {
4402 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004403 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004404 up(&fs_info->uuid_tree_rescan_sem);
4405 return PTR_ERR(task);
4406 }
4407
4408 return 0;
4409}
4410
Chris Mason8f18cf12008-04-25 16:53:30 -04004411/*
4412 * shrinking a device means finding all of the device extents past
4413 * the new size, and then following the back refs to the chunks.
4414 * The chunk relocation code actually frees the device extent
4415 */
4416int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4417{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004418 struct btrfs_fs_info *fs_info = device->fs_info;
4419 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004420 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004421 struct btrfs_dev_extent *dev_extent = NULL;
4422 struct btrfs_path *path;
4423 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004424 u64 chunk_offset;
4425 int ret;
4426 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004427 int failed = 0;
4428 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004429 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004430 struct extent_buffer *l;
4431 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004432 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004433 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004434 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004435 u64 diff;
4436
4437 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004438 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004439
Stefan Behrens63a212a2012-11-05 18:29:28 +01004440 if (device->is_tgtdev_for_dev_replace)
4441 return -EINVAL;
4442
Chris Mason8f18cf12008-04-25 16:53:30 -04004443 path = btrfs_alloc_path();
4444 if (!path)
4445 return -ENOMEM;
4446
David Sterbae4058b52015-11-27 16:31:35 +01004447 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004448
David Sterba34441362016-10-04 19:34:27 +02004449 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004450
Miao Xie7cc8e582014-09-03 21:35:38 +08004451 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004452 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004453 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004454 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004455 }
David Sterba34441362016-10-04 19:34:27 +02004456 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004457
Josef Bacikba1bf482009-09-11 16:11:19 -04004458again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004459 key.objectid = device->devid;
4460 key.offset = (u64)-1;
4461 key.type = BTRFS_DEV_EXTENT_KEY;
4462
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004463 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004464 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004465 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004466 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004467 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004468 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004469 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004470
4471 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004472 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004473 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004474 if (ret < 0)
4475 goto done;
4476 if (ret) {
4477 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004478 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004479 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004480 }
4481
4482 l = path->nodes[0];
4483 slot = path->slots[0];
4484 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4485
Josef Bacikba1bf482009-09-11 16:11:19 -04004486 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004487 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004488 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004489 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004490 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004491
4492 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4493 length = btrfs_dev_extent_length(l, dev_extent);
4494
Josef Bacikba1bf482009-09-11 16:11:19 -04004495 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004497 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004498 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004499 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004500
Chris Mason8f18cf12008-04-25 16:53:30 -04004501 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004502 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004503
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004504 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4505 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004506 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004507 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004508 if (ret == -ENOSPC)
4509 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004510 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004511
4512 if (failed && !retried) {
4513 failed = 0;
4514 retried = true;
4515 goto again;
4516 } else if (failed && retried) {
4517 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004518 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004519 }
4520
Chris Balld6397ba2009-04-27 07:29:03 -04004521 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004522 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004523 if (IS_ERR(trans)) {
4524 ret = PTR_ERR(trans);
4525 goto done;
4526 }
4527
David Sterba34441362016-10-04 19:34:27 +02004528 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004529
4530 /*
4531 * We checked in the above loop all device extents that were already in
4532 * the device tree. However before we have updated the device's
4533 * total_bytes to the new size, we might have had chunk allocations that
4534 * have not complete yet (new block groups attached to transaction
4535 * handles), and therefore their device extents were not yet in the
4536 * device tree and we missed them in the loop above. So if we have any
4537 * pending chunk using a device extent that overlaps the device range
4538 * that we can not use anymore, commit the current transaction and
4539 * repeat the search on the device tree - this way we guarantee we will
4540 * not have chunks using device extents that end beyond 'new_size'.
4541 */
4542 if (!checked_pending_chunks) {
4543 u64 start = new_size;
4544 u64 len = old_size - new_size;
4545
Jeff Mahoney499f3772015-06-15 09:41:17 -04004546 if (contains_pending_extent(trans->transaction, device,
4547 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004548 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004549 checked_pending_chunks = true;
4550 failed = 0;
4551 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004552 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004553 if (ret)
4554 goto done;
4555 goto again;
4556 }
4557 }
4558
Miao Xie7cc8e582014-09-03 21:35:38 +08004559 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004560 if (list_empty(&device->resized_list))
4561 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004562 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004563
Chris Balld6397ba2009-04-27 07:29:03 -04004564 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004565 btrfs_set_super_total_bytes(super_copy,
4566 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004567 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004568
4569 /* Now btrfs_update_device() will change the on-disk size. */
4570 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004571 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004572done:
4573 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004574 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004575 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004576 btrfs_device_set_total_bytes(device, old_size);
4577 if (device->writeable)
4578 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004579 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004580 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004581 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004582 return ret;
4583}
4584
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004585static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004586 struct btrfs_key *key,
4587 struct btrfs_chunk *chunk, int item_size)
4588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004589 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004590 struct btrfs_disk_key disk_key;
4591 u32 array_size;
4592 u8 *ptr;
4593
David Sterba34441362016-10-04 19:34:27 +02004594 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004595 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004596 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004597 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004598 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004599 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004600 }
Chris Mason0b86a832008-03-24 15:01:56 -04004601
4602 ptr = super_copy->sys_chunk_array + array_size;
4603 btrfs_cpu_key_to_disk(&disk_key, key);
4604 memcpy(ptr, &disk_key, sizeof(disk_key));
4605 ptr += sizeof(disk_key);
4606 memcpy(ptr, chunk, item_size);
4607 item_size += sizeof(disk_key);
4608 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004609 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004610
Chris Mason0b86a832008-03-24 15:01:56 -04004611 return 0;
4612}
4613
Miao Xieb2117a32011-01-05 10:07:28 +00004614/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004615 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004616 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004617static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004618{
Arne Jansen73c5de02011-04-12 12:07:57 +02004619 const struct btrfs_device_info *di_a = a;
4620 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004621
Arne Jansen73c5de02011-04-12 12:07:57 +02004622 if (di_a->max_avail > di_b->max_avail)
4623 return -1;
4624 if (di_a->max_avail < di_b->max_avail)
4625 return 1;
4626 if (di_a->total_avail > di_b->total_avail)
4627 return -1;
4628 if (di_a->total_avail < di_b->total_avail)
4629 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004630 return 0;
4631}
4632
David Woodhouse53b381b2013-01-29 18:40:14 -05004633static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4634{
Zhao Leiffe2d202015-01-20 15:11:44 +08004635 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004636 return;
4637
Miao Xieceda0862013-04-11 10:30:16 +00004638 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004639}
4640
Jeff Mahoneyda170662016-06-15 09:22:56 -04004641#define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004642 - sizeof(struct btrfs_chunk)) \
4643 / sizeof(struct btrfs_stripe) + 1)
4644
4645#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4646 - 2 * sizeof(struct btrfs_disk_key) \
4647 - 2 * sizeof(struct btrfs_chunk)) \
4648 / sizeof(struct btrfs_stripe) + 1)
4649
Miao Xieb2117a32011-01-05 10:07:28 +00004650static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004651 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004652{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004653 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004654 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004655 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004656 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004657 struct extent_map_tree *em_tree;
4658 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004659 struct btrfs_device_info *devices_info = NULL;
4660 u64 total_avail;
4661 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004662 int data_stripes; /* number of stripes that count for
4663 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004664 int sub_stripes; /* sub_stripes info for map */
4665 int dev_stripes; /* stripes per dev */
4666 int devs_max; /* max devs to use */
4667 int devs_min; /* min devs needed */
4668 int devs_increment; /* ndevs has to be a multiple of this */
4669 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004670 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004671 u64 max_stripe_size;
4672 u64 max_chunk_size;
4673 u64 stripe_size;
4674 u64 num_bytes;
4675 int ndevs;
4676 int i;
4677 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004678 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004679
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004680 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004681
Miao Xieb2117a32011-01-05 10:07:28 +00004682 if (list_empty(&fs_devices->alloc_list))
4683 return -ENOSPC;
4684
Liu Bo31e50222012-11-21 14:18:10 +00004685 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004686
Liu Bo31e50222012-11-21 14:18:10 +00004687 sub_stripes = btrfs_raid_array[index].sub_stripes;
4688 dev_stripes = btrfs_raid_array[index].dev_stripes;
4689 devs_max = btrfs_raid_array[index].devs_max;
4690 devs_min = btrfs_raid_array[index].devs_min;
4691 devs_increment = btrfs_raid_array[index].devs_increment;
4692 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004693
4694 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004695 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004696 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004697 if (!devs_max)
4698 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004699 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004700 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004701 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4702 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004703 else
Byongho Leeee221842015-12-15 01:42:10 +09004704 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004705 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004706 if (!devs_max)
4707 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004708 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004709 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004710 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004711 if (!devs_max)
4712 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004713 } else {
David Sterba351fd352014-05-15 16:48:20 +02004714 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004715 type);
4716 BUG_ON(1);
4717 }
4718
4719 /* we don't want a chunk larger than 10% of writeable space */
4720 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4721 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004722
David Sterba31e818f2015-02-20 18:00:26 +01004723 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004724 GFP_NOFS);
4725 if (!devices_info)
4726 return -ENOMEM;
4727
Arne Jansen73c5de02011-04-12 12:07:57 +02004728 /*
4729 * in the first pass through the devices list, we gather information
4730 * about the available holes on each device.
4731 */
4732 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004733 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004734 u64 max_avail;
4735 u64 dev_offset;
4736
Arne Jansen73c5de02011-04-12 12:07:57 +02004737 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004738 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004739 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004740 continue;
4741 }
4742
Stefan Behrens63a212a2012-11-05 18:29:28 +01004743 if (!device->in_fs_metadata ||
4744 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004745 continue;
4746
4747 if (device->total_bytes > device->bytes_used)
4748 total_avail = device->total_bytes - device->bytes_used;
4749 else
4750 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004751
4752 /* If there is no space on this device, skip it. */
4753 if (total_avail == 0)
4754 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004755
Josef Bacik6df9a952013-06-27 13:22:46 -04004756 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004757 max_stripe_size * dev_stripes,
4758 &dev_offset, &max_avail);
4759 if (ret && ret != -ENOSPC)
4760 goto error;
4761
4762 if (ret == 0)
4763 max_avail = max_stripe_size * dev_stripes;
4764
4765 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4766 continue;
4767
Eric Sandeen063d0062013-01-31 00:55:01 +00004768 if (ndevs == fs_devices->rw_devices) {
4769 WARN(1, "%s: found more than %llu devices\n",
4770 __func__, fs_devices->rw_devices);
4771 break;
4772 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004773 devices_info[ndevs].dev_offset = dev_offset;
4774 devices_info[ndevs].max_avail = max_avail;
4775 devices_info[ndevs].total_avail = total_avail;
4776 devices_info[ndevs].dev = device;
4777 ++ndevs;
4778 }
4779
4780 /*
4781 * now sort the devices by hole size / available space
4782 */
4783 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4784 btrfs_cmp_device_info, NULL);
4785
4786 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004787 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004788
4789 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4790 ret = -ENOSPC;
4791 goto error;
4792 }
4793
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004794 ndevs = min(ndevs, devs_max);
4795
Arne Jansen73c5de02011-04-12 12:07:57 +02004796 /*
4797 * the primary goal is to maximize the number of stripes, so use as many
4798 * devices as possible, even if the stripes are not maximum sized.
4799 */
4800 stripe_size = devices_info[ndevs-1].max_avail;
4801 num_stripes = ndevs * dev_stripes;
4802
David Woodhouse53b381b2013-01-29 18:40:14 -05004803 /*
4804 * this will have to be fixed for RAID1 and RAID10 over
4805 * more drives
4806 */
4807 data_stripes = num_stripes / ncopies;
4808
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004809 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004810 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004811
4812 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004813 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004814
4815 /*
4816 * Use the number of data stripes to figure out how big this chunk
4817 * is really going to be in terms of logical address space,
4818 * and compare that answer with the max chunk size
4819 */
4820 if (stripe_size * data_stripes > max_chunk_size) {
4821 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004822
4823 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004824
4825 /* bump the answer up to a 16MB boundary */
4826 stripe_size = (stripe_size + mask) & ~mask;
4827
4828 /* but don't go higher than the limits we found
4829 * while searching for free extents
4830 */
4831 if (stripe_size > devices_info[ndevs-1].max_avail)
4832 stripe_size = devices_info[ndevs-1].max_avail;
4833 }
4834
David Sterbab8b93ad2015-01-16 17:26:13 +01004835 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004836
4837 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004838 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004839
Miao Xieb2117a32011-01-05 10:07:28 +00004840 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4841 if (!map) {
4842 ret = -ENOMEM;
4843 goto error;
4844 }
4845 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004846
Arne Jansen73c5de02011-04-12 12:07:57 +02004847 for (i = 0; i < ndevs; ++i) {
4848 for (j = 0; j < dev_stripes; ++j) {
4849 int s = i * dev_stripes + j;
4850 map->stripes[s].dev = devices_info[i].dev;
4851 map->stripes[s].physical = devices_info[i].dev_offset +
4852 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004853 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004854 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004855 map->stripe_len = BTRFS_STRIPE_LEN;
4856 map->io_align = BTRFS_STRIPE_LEN;
4857 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004858 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004859 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004860
David Woodhouse53b381b2013-01-29 18:40:14 -05004861 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004862
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004863 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004864
David Sterba172ddd62011-04-21 00:48:27 +02004865 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004866 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004867 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004868 ret = -ENOMEM;
4869 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004870 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004871 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004872 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004873 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004874 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004875 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004876 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004877 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004878
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004880 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004881 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004882 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004883 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004884 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004885 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004886 }
Yan Zheng2b820322008-11-17 21:11:30 -05004887
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004888 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4889 refcount_inc(&em->refs);
4890 write_unlock(&em_tree->lock);
4891
Nikolay Borisov01744842017-07-27 14:22:11 +03004892 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004893 if (ret)
4894 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004895
Miao Xie7cc8e582014-09-03 21:35:38 +08004896 for (i = 0; i < map->num_stripes; i++) {
4897 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4898 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4899 }
Miao Xie43530c42014-09-03 21:35:36 +08004900
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004901 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004902
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004903 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004904 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004905
Miao Xieb2117a32011-01-05 10:07:28 +00004906 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004907 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004908
Josef Bacik6df9a952013-06-27 13:22:46 -04004909error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004910 write_lock(&em_tree->lock);
4911 remove_extent_mapping(em_tree, em);
4912 write_unlock(&em_tree->lock);
4913
4914 /* One for our allocation */
4915 free_extent_map(em);
4916 /* One for the tree reference */
4917 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004918 /* One for the pending_chunks list reference */
4919 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004920error:
Miao Xieb2117a32011-01-05 10:07:28 +00004921 kfree(devices_info);
4922 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004923}
4924
Josef Bacik6df9a952013-06-27 13:22:46 -04004925int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004926 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004927 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004928{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004929 struct btrfs_root *extent_root = fs_info->extent_root;
4930 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004931 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004932 struct btrfs_device *device;
4933 struct btrfs_chunk *chunk;
4934 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004935 struct extent_map *em;
4936 struct map_lookup *map;
4937 size_t item_size;
4938 u64 dev_offset;
4939 u64 stripe_size;
4940 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004941 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004942
Liu Bo592d92e2017-03-14 13:33:55 -07004943 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4944 if (IS_ERR(em))
4945 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004946
Jeff Mahoney95617d62015-06-03 10:55:48 -04004947 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004948 item_size = btrfs_chunk_item_size(map->num_stripes);
4949 stripe_size = em->orig_block_len;
4950
4951 chunk = kzalloc(item_size, GFP_NOFS);
4952 if (!chunk) {
4953 ret = -ENOMEM;
4954 goto out;
4955 }
4956
Filipe Manana50460e32015-11-20 10:42:47 +00004957 /*
4958 * Take the device list mutex to prevent races with the final phase of
4959 * a device replace operation that replaces the device object associated
4960 * with the map's stripes, because the device object's id can change
4961 * at any time during that final phase of the device replace operation
4962 * (dev-replace.c:btrfs_dev_replace_finishing()).
4963 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004964 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004965 for (i = 0; i < map->num_stripes; i++) {
4966 device = map->stripes[i].dev;
4967 dev_offset = map->stripes[i].physical;
4968
Yan Zheng2b820322008-11-17 21:11:30 -05004969 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004970 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004971 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004972 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4973 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004974 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004975 break;
4976 }
4977 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004978 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004979 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004980 }
4981
Yan Zheng2b820322008-11-17 21:11:30 -05004982 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004983 for (i = 0; i < map->num_stripes; i++) {
4984 device = map->stripes[i].dev;
4985 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004986
4987 btrfs_set_stack_stripe_devid(stripe, device->devid);
4988 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4989 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4990 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004991 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004992 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004993
4994 btrfs_set_stack_chunk_length(chunk, chunk_size);
4995 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4996 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4997 btrfs_set_stack_chunk_type(chunk, map->type);
4998 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4999 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5000 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005001 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005002 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5003
5004 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5005 key.type = BTRFS_CHUNK_ITEM_KEY;
5006 key.offset = chunk_offset;
5007
5008 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005009 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5010 /*
5011 * TODO: Cleanup of inserted chunk root in case of
5012 * failure.
5013 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005014 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005015 }
liubo1abe9b82011-03-24 11:18:59 +00005016
Josef Bacik6df9a952013-06-27 13:22:46 -04005017out:
Yan Zheng2b820322008-11-17 21:11:30 -05005018 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005019 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005020 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005021}
5022
5023/*
5024 * Chunk allocation falls into two parts. The first part does works
5025 * that make the new allocated chunk useable, but not do any operation
5026 * that modifies the chunk tree. The second part does the works that
5027 * require modifying the chunk tree. This division is important for the
5028 * bootstrap process of adding storage to a seed btrfs.
5029 */
5030int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005031 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005032{
5033 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005034
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
5036 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005037 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005038}
5039
Chris Masond3977122009-01-05 21:25:51 -05005040static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005041 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005042{
5043 u64 chunk_offset;
5044 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005045 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005046 int ret;
5047
Josef Bacik6df9a952013-06-27 13:22:46 -04005048 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005049 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005050 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005051 if (ret)
5052 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005053
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005055 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005056 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005057 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005058}
5059
Miao Xied20983b2014-07-03 18:22:13 +08005060static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5061{
5062 int max_errors;
5063
5064 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5065 BTRFS_BLOCK_GROUP_RAID10 |
5066 BTRFS_BLOCK_GROUP_RAID5 |
5067 BTRFS_BLOCK_GROUP_DUP)) {
5068 max_errors = 1;
5069 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5070 max_errors = 2;
5071 } else {
5072 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005073 }
5074
Miao Xied20983b2014-07-03 18:22:13 +08005075 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005076}
5077
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005078int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005079{
5080 struct extent_map *em;
5081 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005082 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005083 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005084 int i;
5085
Liu Bo592d92e2017-03-14 13:33:55 -07005086 em = get_chunk_map(fs_info, chunk_offset, 1);
5087 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005088 return 1;
5089
Jeff Mahoney95617d62015-06-03 10:55:48 -04005090 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005091 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08005092 if (map->stripes[i].dev->missing) {
5093 miss_ndevs++;
5094 continue;
5095 }
5096
Yan Zheng2b820322008-11-17 21:11:30 -05005097 if (!map->stripes[i].dev->writeable) {
5098 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005099 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005100 }
5101 }
Miao Xied20983b2014-07-03 18:22:13 +08005102
5103 /*
5104 * If the number of missing devices is larger than max errors,
5105 * we can not write the data into that chunk successfully, so
5106 * set it readonly.
5107 */
5108 if (miss_ndevs > btrfs_chunk_max_errors(map))
5109 readonly = 1;
5110end:
Yan Zheng2b820322008-11-17 21:11:30 -05005111 free_extent_map(em);
5112 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005113}
5114
5115void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5116{
David Sterbaa8067e02011-04-21 00:34:43 +02005117 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005118}
5119
5120void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5121{
5122 struct extent_map *em;
5123
Chris Masond3977122009-01-05 21:25:51 -05005124 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005125 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005126 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5127 if (em)
5128 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005129 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005130 if (!em)
5131 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005132 /* once for us */
5133 free_extent_map(em);
5134 /* once for the tree */
5135 free_extent_map(em);
5136 }
5137}
5138
Stefan Behrens5d964052012-11-05 14:59:07 +01005139int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005140{
5141 struct extent_map *em;
5142 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005143 int ret;
5144
Liu Bo592d92e2017-03-14 13:33:55 -07005145 em = get_chunk_map(fs_info, logical, len);
5146 if (IS_ERR(em))
5147 /*
5148 * We could return errors for these cases, but that could get
5149 * ugly and we'd probably do the same thing which is just not do
5150 * anything else and exit, so return 1 so the callers don't try
5151 * to use other copies.
5152 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005153 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005154
Jeff Mahoney95617d62015-06-03 10:55:48 -04005155 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005156 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5157 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005158 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5159 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005160 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5161 ret = 2;
5162 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5163 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005164 else
5165 ret = 1;
5166 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005167
Liu Bo73beece2015-07-17 16:49:19 +08005168 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Liu Bo6fad8232017-03-14 13:33:59 -07005169 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5170 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005171 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005172 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005173
Chris Masonf1885912008-04-09 16:28:12 -04005174 return ret;
5175}
5176
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005177unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005178 u64 logical)
5179{
5180 struct extent_map *em;
5181 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005182 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005183
Liu Bo592d92e2017-03-14 13:33:55 -07005184 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005185
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005186 if (!WARN_ON(IS_ERR(em))) {
5187 map = em->map_lookup;
5188 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5189 len = map->stripe_len * nr_data_stripes(map);
5190 free_extent_map(em);
5191 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005192 return len;
5193}
5194
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005195int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005196{
5197 struct extent_map *em;
5198 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005199 int ret = 0;
5200
Liu Bo592d92e2017-03-14 13:33:55 -07005201 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005202
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005203 if(!WARN_ON(IS_ERR(em))) {
5204 map = em->map_lookup;
5205 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5206 ret = 1;
5207 free_extent_map(em);
5208 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005209 return ret;
5210}
5211
Stefan Behrens30d98612012-11-06 14:52:18 +01005212static int find_live_mirror(struct btrfs_fs_info *fs_info,
5213 struct map_lookup *map, int first, int num,
5214 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005215{
5216 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005217 int tolerance;
5218 struct btrfs_device *srcdev;
5219
5220 if (dev_replace_is_ongoing &&
5221 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5222 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5223 srcdev = fs_info->dev_replace.srcdev;
5224 else
5225 srcdev = NULL;
5226
5227 /*
5228 * try to avoid the drive that is the source drive for a
5229 * dev-replace procedure, only choose it if no other non-missing
5230 * mirror is available
5231 */
5232 for (tolerance = 0; tolerance < 2; tolerance++) {
5233 if (map->stripes[optimal].dev->bdev &&
5234 (tolerance || map->stripes[optimal].dev != srcdev))
5235 return optimal;
5236 for (i = first; i < first + num; i++) {
5237 if (map->stripes[i].dev->bdev &&
5238 (tolerance || map->stripes[i].dev != srcdev))
5239 return i;
5240 }
Chris Masondfe25022008-05-13 13:46:40 -04005241 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005242
Chris Masondfe25022008-05-13 13:46:40 -04005243 /* we couldn't find one that doesn't fail. Just return something
5244 * and the io error handling code will clean up eventually
5245 */
5246 return optimal;
5247}
5248
David Woodhouse53b381b2013-01-29 18:40:14 -05005249static inline int parity_smaller(u64 a, u64 b)
5250{
5251 return a > b;
5252}
5253
5254/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005255static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005256{
5257 struct btrfs_bio_stripe s;
5258 int i;
5259 u64 l;
5260 int again = 1;
5261
5262 while (again) {
5263 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005264 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005265 if (parity_smaller(bbio->raid_map[i],
5266 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005267 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005268 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005269 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005270 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005271 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005272 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005273
David Woodhouse53b381b2013-01-29 18:40:14 -05005274 again = 1;
5275 }
5276 }
5277 }
5278}
5279
Zhao Lei6e9606d2015-01-20 15:11:34 +08005280static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5281{
5282 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005283 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005284 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005285 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005286 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005287 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005288 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005289 /*
5290 * plus the raid_map, which includes both the tgt dev
5291 * and the stripes
5292 */
5293 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005294 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005295
5296 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005297 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005298
5299 return bbio;
5300}
5301
5302void btrfs_get_bbio(struct btrfs_bio *bbio)
5303{
Elena Reshetova140475a2017-03-03 10:55:10 +02005304 WARN_ON(!refcount_read(&bbio->refs));
5305 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005306}
5307
5308void btrfs_put_bbio(struct btrfs_bio *bbio)
5309{
5310 if (!bbio)
5311 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005312 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005313 kfree(bbio);
5314}
5315
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005316/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5317/*
5318 * Please note that, discard won't be sent to target device of device
5319 * replace.
5320 */
5321static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5322 u64 logical, u64 length,
5323 struct btrfs_bio **bbio_ret)
5324{
5325 struct extent_map *em;
5326 struct map_lookup *map;
5327 struct btrfs_bio *bbio;
5328 u64 offset;
5329 u64 stripe_nr;
5330 u64 stripe_nr_end;
5331 u64 stripe_end_offset;
5332 u64 stripe_cnt;
5333 u64 stripe_len;
5334 u64 stripe_offset;
5335 u64 num_stripes;
5336 u32 stripe_index;
5337 u32 factor = 0;
5338 u32 sub_stripes = 0;
5339 u64 stripes_per_dev = 0;
5340 u32 remaining_stripes = 0;
5341 u32 last_stripe = 0;
5342 int ret = 0;
5343 int i;
5344
5345 /* discard always return a bbio */
5346 ASSERT(bbio_ret);
5347
5348 em = get_chunk_map(fs_info, logical, length);
5349 if (IS_ERR(em))
5350 return PTR_ERR(em);
5351
5352 map = em->map_lookup;
5353 /* we don't discard raid56 yet */
5354 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5355 ret = -EOPNOTSUPP;
5356 goto out;
5357 }
5358
5359 offset = logical - em->start;
5360 length = min_t(u64, em->len - offset, length);
5361
5362 stripe_len = map->stripe_len;
5363 /*
5364 * stripe_nr counts the total number of stripes we have to stride
5365 * to get to this block
5366 */
5367 stripe_nr = div64_u64(offset, stripe_len);
5368
5369 /* stripe_offset is the offset of this block in its stripe */
5370 stripe_offset = offset - stripe_nr * stripe_len;
5371
5372 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005373 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005374 stripe_cnt = stripe_nr_end - stripe_nr;
5375 stripe_end_offset = stripe_nr_end * map->stripe_len -
5376 (offset + length);
5377 /*
5378 * after this, stripe_nr is the number of stripes on this
5379 * device we have to walk to find the data, and stripe_index is
5380 * the number of our device in the stripe array
5381 */
5382 num_stripes = 1;
5383 stripe_index = 0;
5384 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5385 BTRFS_BLOCK_GROUP_RAID10)) {
5386 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5387 sub_stripes = 1;
5388 else
5389 sub_stripes = map->sub_stripes;
5390
5391 factor = map->num_stripes / sub_stripes;
5392 num_stripes = min_t(u64, map->num_stripes,
5393 sub_stripes * stripe_cnt);
5394 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5395 stripe_index *= sub_stripes;
5396 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5397 &remaining_stripes);
5398 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5399 last_stripe *= sub_stripes;
5400 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5401 BTRFS_BLOCK_GROUP_DUP)) {
5402 num_stripes = map->num_stripes;
5403 } else {
5404 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5405 &stripe_index);
5406 }
5407
5408 bbio = alloc_btrfs_bio(num_stripes, 0);
5409 if (!bbio) {
5410 ret = -ENOMEM;
5411 goto out;
5412 }
5413
5414 for (i = 0; i < num_stripes; i++) {
5415 bbio->stripes[i].physical =
5416 map->stripes[stripe_index].physical +
5417 stripe_offset + stripe_nr * map->stripe_len;
5418 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5419
5420 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5421 BTRFS_BLOCK_GROUP_RAID10)) {
5422 bbio->stripes[i].length = stripes_per_dev *
5423 map->stripe_len;
5424
5425 if (i / sub_stripes < remaining_stripes)
5426 bbio->stripes[i].length +=
5427 map->stripe_len;
5428
5429 /*
5430 * Special for the first stripe and
5431 * the last stripe:
5432 *
5433 * |-------|...|-------|
5434 * |----------|
5435 * off end_off
5436 */
5437 if (i < sub_stripes)
5438 bbio->stripes[i].length -=
5439 stripe_offset;
5440
5441 if (stripe_index >= last_stripe &&
5442 stripe_index <= (last_stripe +
5443 sub_stripes - 1))
5444 bbio->stripes[i].length -=
5445 stripe_end_offset;
5446
5447 if (i == sub_stripes - 1)
5448 stripe_offset = 0;
5449 } else {
5450 bbio->stripes[i].length = length;
5451 }
5452
5453 stripe_index++;
5454 if (stripe_index == map->num_stripes) {
5455 stripe_index = 0;
5456 stripe_nr++;
5457 }
5458 }
5459
5460 *bbio_ret = bbio;
5461 bbio->map_type = map->type;
5462 bbio->num_stripes = num_stripes;
5463out:
5464 free_extent_map(em);
5465 return ret;
5466}
5467
Liu Bo5ab56092017-03-14 13:33:57 -07005468/*
5469 * In dev-replace case, for repair case (that's the only case where the mirror
5470 * is selected explicitly when calling btrfs_map_block), blocks left of the
5471 * left cursor can also be read from the target drive.
5472 *
5473 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5474 * array of stripes.
5475 * For READ, it also needs to be supported using the same mirror number.
5476 *
5477 * If the requested block is not left of the left cursor, EIO is returned. This
5478 * can happen because btrfs_num_copies() returns one more in the dev-replace
5479 * case.
5480 */
5481static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5482 u64 logical, u64 length,
5483 u64 srcdev_devid, int *mirror_num,
5484 u64 *physical)
5485{
5486 struct btrfs_bio *bbio = NULL;
5487 int num_stripes;
5488 int index_srcdev = 0;
5489 int found = 0;
5490 u64 physical_of_found = 0;
5491 int i;
5492 int ret = 0;
5493
5494 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5495 logical, &length, &bbio, 0, 0);
5496 if (ret) {
5497 ASSERT(bbio == NULL);
5498 return ret;
5499 }
5500
5501 num_stripes = bbio->num_stripes;
5502 if (*mirror_num > num_stripes) {
5503 /*
5504 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5505 * that means that the requested area is not left of the left
5506 * cursor
5507 */
5508 btrfs_put_bbio(bbio);
5509 return -EIO;
5510 }
5511
5512 /*
5513 * process the rest of the function using the mirror_num of the source
5514 * drive. Therefore look it up first. At the end, patch the device
5515 * pointer to the one of the target drive.
5516 */
5517 for (i = 0; i < num_stripes; i++) {
5518 if (bbio->stripes[i].dev->devid != srcdev_devid)
5519 continue;
5520
5521 /*
5522 * In case of DUP, in order to keep it simple, only add the
5523 * mirror with the lowest physical address
5524 */
5525 if (found &&
5526 physical_of_found <= bbio->stripes[i].physical)
5527 continue;
5528
5529 index_srcdev = i;
5530 found = 1;
5531 physical_of_found = bbio->stripes[i].physical;
5532 }
5533
5534 btrfs_put_bbio(bbio);
5535
5536 ASSERT(found);
5537 if (!found)
5538 return -EIO;
5539
5540 *mirror_num = index_srcdev + 1;
5541 *physical = physical_of_found;
5542 return ret;
5543}
5544
Liu Bo73c0f222017-03-14 13:33:58 -07005545static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5546 struct btrfs_bio **bbio_ret,
5547 struct btrfs_dev_replace *dev_replace,
5548 int *num_stripes_ret, int *max_errors_ret)
5549{
5550 struct btrfs_bio *bbio = *bbio_ret;
5551 u64 srcdev_devid = dev_replace->srcdev->devid;
5552 int tgtdev_indexes = 0;
5553 int num_stripes = *num_stripes_ret;
5554 int max_errors = *max_errors_ret;
5555 int i;
5556
5557 if (op == BTRFS_MAP_WRITE) {
5558 int index_where_to_add;
5559
5560 /*
5561 * duplicate the write operations while the dev replace
5562 * procedure is running. Since the copying of the old disk to
5563 * the new disk takes place at run time while the filesystem is
5564 * mounted writable, the regular write operations to the old
5565 * disk have to be duplicated to go to the new disk as well.
5566 *
5567 * Note that device->missing is handled by the caller, and that
5568 * the write to the old disk is already set up in the stripes
5569 * array.
5570 */
5571 index_where_to_add = num_stripes;
5572 for (i = 0; i < num_stripes; i++) {
5573 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5574 /* write to new disk, too */
5575 struct btrfs_bio_stripe *new =
5576 bbio->stripes + index_where_to_add;
5577 struct btrfs_bio_stripe *old =
5578 bbio->stripes + i;
5579
5580 new->physical = old->physical;
5581 new->length = old->length;
5582 new->dev = dev_replace->tgtdev;
5583 bbio->tgtdev_map[i] = index_where_to_add;
5584 index_where_to_add++;
5585 max_errors++;
5586 tgtdev_indexes++;
5587 }
5588 }
5589 num_stripes = index_where_to_add;
5590 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5591 int index_srcdev = 0;
5592 int found = 0;
5593 u64 physical_of_found = 0;
5594
5595 /*
5596 * During the dev-replace procedure, the target drive can also
5597 * be used to read data in case it is needed to repair a corrupt
5598 * block elsewhere. This is possible if the requested area is
5599 * left of the left cursor. In this area, the target drive is a
5600 * full copy of the source drive.
5601 */
5602 for (i = 0; i < num_stripes; i++) {
5603 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5604 /*
5605 * In case of DUP, in order to keep it simple,
5606 * only add the mirror with the lowest physical
5607 * address
5608 */
5609 if (found &&
5610 physical_of_found <=
5611 bbio->stripes[i].physical)
5612 continue;
5613 index_srcdev = i;
5614 found = 1;
5615 physical_of_found = bbio->stripes[i].physical;
5616 }
5617 }
5618 if (found) {
5619 struct btrfs_bio_stripe *tgtdev_stripe =
5620 bbio->stripes + num_stripes;
5621
5622 tgtdev_stripe->physical = physical_of_found;
5623 tgtdev_stripe->length =
5624 bbio->stripes[index_srcdev].length;
5625 tgtdev_stripe->dev = dev_replace->tgtdev;
5626 bbio->tgtdev_map[index_srcdev] = num_stripes;
5627
5628 tgtdev_indexes++;
5629 num_stripes++;
5630 }
5631 }
5632
5633 *num_stripes_ret = num_stripes;
5634 *max_errors_ret = max_errors;
5635 bbio->num_tgtdevs = tgtdev_indexes;
5636 *bbio_ret = bbio;
5637}
5638
Liu Bo2b19a1f2017-03-14 13:34:00 -07005639static bool need_full_stripe(enum btrfs_map_op op)
5640{
5641 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5642}
5643
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005644static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5645 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005646 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005647 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005648 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005649{
5650 struct extent_map *em;
5651 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005652 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005653 u64 stripe_offset;
5654 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005655 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005656 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005657 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005658 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005659 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005660 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005661 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005662 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005663 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5664 int dev_replace_is_ongoing = 0;
5665 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005666 int patch_the_first_stripe_for_dev_replace = 0;
5667 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005668 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005669
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005670 if (op == BTRFS_MAP_DISCARD)
5671 return __btrfs_map_block_for_discard(fs_info, logical,
5672 *length, bbio_ret);
5673
Liu Bo592d92e2017-03-14 13:33:55 -07005674 em = get_chunk_map(fs_info, logical, *length);
5675 if (IS_ERR(em))
5676 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005677
Jeff Mahoney95617d62015-06-03 10:55:48 -04005678 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005679 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005680
David Woodhouse53b381b2013-01-29 18:40:14 -05005681 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005682 stripe_nr = offset;
5683 /*
5684 * stripe_nr counts the total number of stripes we have to stride
5685 * to get to this block
5686 */
David Sterba47c57132015-02-20 18:43:47 +01005687 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005688
David Woodhouse53b381b2013-01-29 18:40:14 -05005689 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005690 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005691 btrfs_crit(fs_info,
5692 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005693 stripe_offset, offset, em->start, logical,
5694 stripe_len);
5695 free_extent_map(em);
5696 return -EINVAL;
5697 }
Chris Mason593060d2008-03-25 16:50:33 -04005698
5699 /* stripe_offset is the offset of this block in its stripe*/
5700 stripe_offset = offset - stripe_offset;
5701
David Woodhouse53b381b2013-01-29 18:40:14 -05005702 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005703 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005704 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5705 raid56_full_stripe_start = offset;
5706
5707 /* allow a write of a full stripe, but make sure we don't
5708 * allow straddling of stripes
5709 */
David Sterba47c57132015-02-20 18:43:47 +01005710 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5711 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005712 raid56_full_stripe_start *= full_stripe_len;
5713 }
5714
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005715 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005716 u64 max_len;
5717 /* For writes to RAID[56], allow a full stripeset across all disks.
5718 For other RAID types and for RAID[56] reads, just allow a single
5719 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005720 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005721 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005722 max_len = stripe_len * nr_data_stripes(map) -
5723 (offset - raid56_full_stripe_start);
5724 } else {
5725 /* we limit the length of each bio to what fits in a stripe */
5726 max_len = stripe_len - stripe_offset;
5727 }
5728 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005729 } else {
5730 *length = em->len - offset;
5731 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005732
David Woodhouse53b381b2013-01-29 18:40:14 -05005733 /* This is for when we're called from btrfs_merge_bio_hook() and all
5734 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005735 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005736 goto out;
5737
Liu Bo73beece2015-07-17 16:49:19 +08005738 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005739 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5740 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005741 btrfs_dev_replace_unlock(dev_replace, 0);
5742 else
5743 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005744
Stefan Behrensad6d6202012-11-06 15:06:47 +01005745 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005746 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005747 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5748 dev_replace->srcdev->devid,
5749 &mirror_num,
5750 &physical_to_patch_in_first_stripe);
5751 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005752 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005753 else
5754 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005755 } else if (mirror_num > map->num_stripes) {
5756 mirror_num = 0;
5757 }
5758
Chris Masonf2d8d742008-04-21 10:03:05 -04005759 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005760 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005761 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005762 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5763 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005764 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005765 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005766 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005767 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005768 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005769 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005770 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005771 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005772 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005773 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005774 current->pid % map->num_stripes,
5775 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005776 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005777 }
Chris Mason2fff7342008-04-29 14:12:09 -04005778
Chris Mason611f0e02008-04-03 16:29:03 -04005779 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005780 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005781 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005782 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005783 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005784 } else {
5785 mirror_num = 1;
5786 }
Chris Mason2fff7342008-04-29 14:12:09 -04005787
Chris Mason321aecc2008-04-16 10:49:51 -04005788 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005789 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005790
David Sterba47c57132015-02-20 18:43:47 +01005791 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005792 stripe_index *= map->sub_stripes;
5793
Anand Jainde483732017-10-12 16:43:00 +08005794 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005795 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005796 else if (mirror_num)
5797 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005798 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005799 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005800 stripe_index = find_live_mirror(fs_info, map,
5801 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005802 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005803 current->pid % map->sub_stripes,
5804 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005805 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005806 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005807
Zhao Leiffe2d202015-01-20 15:11:44 +08005808 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005809 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005810 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005811 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005812 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005813
5814 /* RAID[56] write or recovery. Return all stripes */
5815 num_stripes = map->num_stripes;
5816 max_errors = nr_parity_stripes(map);
5817
David Woodhouse53b381b2013-01-29 18:40:14 -05005818 *length = map->stripe_len;
5819 stripe_index = 0;
5820 stripe_offset = 0;
5821 } else {
5822 /*
5823 * Mirror #0 or #1 means the original data block.
5824 * Mirror #2 is RAID5 parity block.
5825 * Mirror #3 is RAID6 Q block.
5826 */
David Sterba47c57132015-02-20 18:43:47 +01005827 stripe_nr = div_u64_rem(stripe_nr,
5828 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005829 if (mirror_num > 1)
5830 stripe_index = nr_data_stripes(map) +
5831 mirror_num - 2;
5832
5833 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005834 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5835 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005836 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005837 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005838 }
Chris Mason8790d502008-04-03 16:29:03 -04005839 } else {
5840 /*
David Sterba47c57132015-02-20 18:43:47 +01005841 * after this, stripe_nr is the number of stripes on this
5842 * device we have to walk to find the data, and stripe_index is
5843 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005844 */
David Sterba47c57132015-02-20 18:43:47 +01005845 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5846 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005847 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005848 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005849 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005850 btrfs_crit(fs_info,
5851 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005852 stripe_index, map->num_stripes);
5853 ret = -EINVAL;
5854 goto out;
5855 }
Chris Mason593060d2008-03-25 16:50:33 -04005856
Stefan Behrens472262f2012-11-06 14:43:46 +01005857 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005858 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005859 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005860 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005861 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005862 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005863 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005864 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005865
Zhao Lei6e9606d2015-01-20 15:11:34 +08005866 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005867 if (!bbio) {
5868 ret = -ENOMEM;
5869 goto out;
5870 }
Liu Bo6fad8232017-03-14 13:33:59 -07005871 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005872 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005873
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005874 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005875 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5876 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005877 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005878 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005879
5880 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5881 sizeof(struct btrfs_bio_stripe) *
5882 num_alloc_stripes +
5883 sizeof(int) * tgtdev_indexes);
5884
5885 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005886 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005887
5888 /* Fill in the logical address of each stripe */
5889 tmp = stripe_nr * nr_data_stripes(map);
5890 for (i = 0; i < nr_data_stripes(map); i++)
5891 bbio->raid_map[(i+rot) % num_stripes] =
5892 em->start + (tmp + i) * map->stripe_len;
5893
5894 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5895 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5896 bbio->raid_map[(i+rot+1) % num_stripes] =
5897 RAID6_Q_STRIPE;
5898 }
5899
Li Zefanec9ef7a2011-12-01 14:06:42 +08005900
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005901 for (i = 0; i < num_stripes; i++) {
5902 bbio->stripes[i].physical =
5903 map->stripes[stripe_index].physical +
5904 stripe_offset +
5905 stripe_nr * map->stripe_len;
5906 bbio->stripes[i].dev =
5907 map->stripes[stripe_index].dev;
5908 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005909 }
Li Zefande11cc12011-12-01 12:55:47 +08005910
Liu Bo2b19a1f2017-03-14 13:34:00 -07005911 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005912 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005913
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005914 if (bbio->raid_map)
5915 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005916
Liu Bo73c0f222017-03-14 13:33:58 -07005917 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005918 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005919 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5920 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005921 }
5922
Li Zefande11cc12011-12-01 12:55:47 +08005923 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005924 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005925 bbio->num_stripes = num_stripes;
5926 bbio->max_errors = max_errors;
5927 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005928
5929 /*
5930 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5931 * mirror_num == num_stripes + 1 && dev_replace target drive is
5932 * available as a mirror
5933 */
5934 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5935 WARN_ON(num_stripes > 1);
5936 bbio->stripes[0].dev = dev_replace->tgtdev;
5937 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5938 bbio->mirror_num = map->num_stripes + 1;
5939 }
Chris Masoncea9e442008-04-09 16:28:12 -04005940out:
Liu Bo73beece2015-07-17 16:49:19 +08005941 if (dev_replace_is_ongoing) {
5942 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5943 btrfs_dev_replace_unlock(dev_replace, 0);
5944 }
Chris Mason0b86a832008-03-24 15:01:56 -04005945 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005946 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005947}
5948
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005949int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005950 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005951 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005952{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005953 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005954 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005955}
5956
Miao Xieaf8e2d12014-10-23 14:42:50 +08005957/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005958int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005959 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005960 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005961{
David Sterba825ad4c2017-03-28 14:45:22 +02005962 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005963}
5964
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04005965int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
Yan Zhenga512bbf2008-12-08 16:46:26 -05005966 u64 chunk_start, u64 physical, u64 devid,
5967 u64 **logical, int *naddrs, int *stripe_len)
5968{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005969 struct extent_map *em;
5970 struct map_lookup *map;
5971 u64 *buf;
5972 u64 bytenr;
5973 u64 length;
5974 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005975 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005976 int i, j, nr = 0;
5977
Liu Bo592d92e2017-03-14 13:33:55 -07005978 em = get_chunk_map(fs_info, chunk_start, 1);
5979 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005980 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005981
Jeff Mahoney95617d62015-06-03 10:55:48 -04005982 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005983 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005984 rmap_len = map->stripe_len;
5985
Yan Zhenga512bbf2008-12-08 16:46:26 -05005986 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005987 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005988 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005989 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005990 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005991 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005992 rmap_len = map->stripe_len * nr_data_stripes(map);
5993 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005994
David Sterba31e818f2015-02-20 18:00:26 +01005995 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005996 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005997
5998 for (i = 0; i < map->num_stripes; i++) {
5999 if (devid && map->stripes[i].dev->devid != devid)
6000 continue;
6001 if (map->stripes[i].physical > physical ||
6002 map->stripes[i].physical + length <= physical)
6003 continue;
6004
6005 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006006 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006007
6008 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6009 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006010 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006011 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6012 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006013 } /* else if RAID[56], multiply by nr_data_stripes().
6014 * Alternatively, just use rmap_len below instead of
6015 * map->stripe_len */
6016
6017 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006018 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006019 for (j = 0; j < nr; j++) {
6020 if (buf[j] == bytenr)
6021 break;
6022 }
Chris Mason934d3752008-12-08 16:43:10 -05006023 if (j == nr) {
6024 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006025 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006026 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006027 }
6028
Yan Zhenga512bbf2008-12-08 16:46:26 -05006029 *logical = buf;
6030 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006031 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006032
6033 free_extent_map(em);
6034 return 0;
6035}
6036
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006037static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006038{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006039 bio->bi_private = bbio->private;
6040 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006041 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006042
Zhao Lei6e9606d2015-01-20 15:11:34 +08006043 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006044}
6045
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006046static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006047{
Chris Mason9be33952013-05-17 18:30:14 -04006048 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006049 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006050
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006051 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006052 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006053 if (bio->bi_status == BLK_STS_IOERR ||
6054 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006055 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006056 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006057 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006058
6059 BUG_ON(stripe_index >= bbio->num_stripes);
6060 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006061 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006062 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006063 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006064 BTRFS_DEV_STAT_WRITE_ERRS);
6065 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006066 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006067 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006068 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006069 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006070 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006071 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006072 }
6073 }
Chris Mason8790d502008-04-03 16:29:03 -04006074
Jan Schmidta1d3c472011-08-04 17:15:33 +02006075 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006076 is_orig_bio = 1;
6077
Miao Xiec404e0d2014-01-30 16:46:55 +08006078 btrfs_bio_counter_dec(bbio->fs_info);
6079
Jan Schmidta1d3c472011-08-04 17:15:33 +02006080 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006081 if (!is_orig_bio) {
6082 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006083 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006084 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006085
Chris Mason9be33952013-05-17 18:30:14 -04006086 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006087 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006088 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006089 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006090 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006091 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006092 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006093 /*
6094 * this bio is actually up to date, we didn't
6095 * go over the max number of errors
6096 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006097 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006098 }
Miao Xiec55f1392014-06-19 10:42:54 +08006099
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006100 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006101 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006102 bio_put(bio);
6103 }
Chris Mason8790d502008-04-03 16:29:03 -04006104}
6105
Chris Mason8b712842008-06-11 16:50:36 -04006106/*
6107 * see run_scheduled_bios for a description of why bios are collected for
6108 * async submit.
6109 *
6110 * This will add one bio to the pending list for a device and make sure
6111 * the work struct is scheduled.
6112 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006113static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006114 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006116 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006117 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006118 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006119
David Woodhouse53b381b2013-01-29 18:40:14 -05006120 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006121 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006122 return;
6123 }
6124
Chris Mason8b712842008-06-11 16:50:36 -04006125 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006126 if (bio_op(bio) == REQ_OP_READ) {
Chris Mason492bb6de2008-07-31 16:29:02 -04006127 bio_get(bio);
Mike Christie4e49ea42016-06-05 14:31:41 -05006128 btrfsic_submit_bio(bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04006129 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006130 return;
Chris Mason8b712842008-06-11 16:50:36 -04006131 }
6132
Chris Mason492bb6de2008-07-31 16:29:02 -04006133 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006134 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006135
6136 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006137 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006138 pending_bios = &device->pending_sync_bios;
6139 else
6140 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006141
Chris Masonffbd5172009-04-20 15:50:09 -04006142 if (pending_bios->tail)
6143 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006144
Chris Masonffbd5172009-04-20 15:50:09 -04006145 pending_bios->tail = bio;
6146 if (!pending_bios->head)
6147 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006148 if (device->running_pending)
6149 should_queue = 0;
6150
6151 spin_unlock(&device->io_lock);
6152
6153 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006154 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006155}
6156
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006157static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6158 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006159{
6160 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006161 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006162
6163 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006164 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006165 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006166 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006167#ifdef DEBUG
6168 {
6169 struct rcu_string *name;
6170
6171 rcu_read_lock();
6172 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006173 btrfs_debug(fs_info,
6174 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6175 bio_op(bio), bio->bi_opf,
6176 (u64)bio->bi_iter.bi_sector,
6177 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6178 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006179 rcu_read_unlock();
6180 }
6181#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006182 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006184 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006185
Josef Bacikde1ee922012-10-19 16:50:56 -04006186 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006187 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006188 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006189 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006190}
6191
Josef Bacikde1ee922012-10-19 16:50:56 -04006192static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6193{
6194 atomic_inc(&bbio->error);
6195 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006196 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006197 WARN_ON(bio != bbio->orig_bio);
6198
Chris Mason9be33952013-05-17 18:30:14 -04006199 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006200 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006201 if (atomic_read(&bbio->error) > bbio->max_errors)
6202 bio->bi_status = BLK_STS_IOERR;
6203 else
6204 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006205 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006206 }
6207}
6208
Omar Sandoval58efbc92017-08-22 23:45:59 -07006209blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6210 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006211{
Chris Mason0b86a832008-03-24 15:01:56 -04006212 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006213 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006214 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006215 u64 length = 0;
6216 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006217 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006218 int dev_nr;
6219 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006220 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006221
Kent Overstreet4f024f32013-10-11 15:44:27 -07006222 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006223 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006224
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006225 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006226 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006227 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006228 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006229 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006230 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006231 }
Chris Masoncea9e442008-04-09 16:28:12 -04006232
Jan Schmidta1d3c472011-08-04 17:15:33 +02006233 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006234 bbio->orig_bio = first_bio;
6235 bbio->private = first_bio->bi_private;
6236 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006237 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006238 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6239
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006240 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006241 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006242 /* In this case, map_length has been set to the length of
6243 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006244 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006245 ret = raid56_parity_write(fs_info, bio, bbio,
6246 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006247 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006248 ret = raid56_parity_recover(fs_info, bio, bbio,
6249 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006250 }
Miao Xie42452152014-11-25 16:39:28 +08006251
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006252 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006253 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006254 }
6255
Chris Masoncea9e442008-04-09 16:28:12 -04006256 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006257 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006258 "mapping failed logical %llu bio len %llu len %llu",
6259 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006260 BUG();
6261 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006262
Zhao Lei08da7572015-02-12 15:42:16 +08006263 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006264 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006265 if (!dev || !dev->bdev ||
Liu Boa967efb2017-04-10 12:36:26 -07006266 (bio_op(first_bio) == REQ_OP_WRITE && !dev->writeable)) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006267 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006268 continue;
6269 }
6270
David Sterba3aa8e072017-06-02 17:38:30 +02006271 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006272 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006273 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006274 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006275
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006276 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6277 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006278 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006279 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006280 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006281}
6282
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006283struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006284 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006285{
Yan Zheng2b820322008-11-17 21:11:30 -05006286 struct btrfs_device *device;
6287 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006288
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006289 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006290 while (cur_devices) {
6291 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006292 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006293 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006294 if (device)
6295 return device;
6296 }
6297 cur_devices = cur_devices->seed;
6298 }
6299 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006300}
6301
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006302static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006303 u64 devid, u8 *dev_uuid)
6304{
6305 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006306
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006307 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6308 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006309 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006310
6311 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006312 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006313 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006314
6315 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006316 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006317
Chris Masondfe25022008-05-13 13:46:40 -04006318 return device;
6319}
6320
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006321/**
6322 * btrfs_alloc_device - allocate struct btrfs_device
6323 * @fs_info: used only for generating a new devid, can be NULL if
6324 * devid is provided (i.e. @devid != NULL).
6325 * @devid: a pointer to devid for this device. If NULL a new devid
6326 * is generated.
6327 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6328 * is generated.
6329 *
6330 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006331 * on error. Returned struct is not linked onto any lists and must be
6332 * destroyed with free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006333 */
6334struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6335 const u64 *devid,
6336 const u8 *uuid)
6337{
6338 struct btrfs_device *dev;
6339 u64 tmp;
6340
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306341 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006342 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006343
6344 dev = __alloc_device();
6345 if (IS_ERR(dev))
6346 return dev;
6347
6348 if (devid)
6349 tmp = *devid;
6350 else {
6351 int ret;
6352
6353 ret = find_next_devid(fs_info, &tmp);
6354 if (ret) {
David Sterba55de4802017-10-30 18:55:47 +01006355 free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006356 return ERR_PTR(ret);
6357 }
6358 }
6359 dev->devid = tmp;
6360
6361 if (uuid)
6362 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6363 else
6364 generate_random_uuid(dev->uuid);
6365
Liu Bo9e0af232014-08-15 23:36:53 +08006366 btrfs_init_work(&dev->work, btrfs_submit_helper,
6367 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006368
6369 return dev;
6370}
6371
Liu Boe06cd3d2016-06-03 12:05:15 -07006372/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006373static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006374 struct extent_buffer *leaf,
6375 struct btrfs_chunk *chunk, u64 logical)
6376{
6377 u64 length;
6378 u64 stripe_len;
6379 u16 num_stripes;
6380 u16 sub_stripes;
6381 u64 type;
6382
6383 length = btrfs_chunk_length(leaf, chunk);
6384 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6385 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6386 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6387 type = btrfs_chunk_type(leaf, chunk);
6388
6389 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006390 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006391 num_stripes);
6392 return -EIO;
6393 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006394 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6395 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006396 return -EIO;
6397 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006398 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6399 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006400 btrfs_chunk_sector_size(leaf, chunk));
6401 return -EIO;
6402 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006403 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6404 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006405 return -EIO;
6406 }
6407 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006408 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006409 stripe_len);
6410 return -EIO;
6411 }
6412 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6413 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006414 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006415 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6416 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6417 btrfs_chunk_type(leaf, chunk));
6418 return -EIO;
6419 }
6420 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6421 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6422 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6423 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6424 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6425 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6426 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006427 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006428 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6429 num_stripes, sub_stripes,
6430 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6431 return -EIO;
6432 }
6433
6434 return 0;
6435}
6436
Anand Jain5a2b8e62017-10-09 11:07:45 +08006437static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006438 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006439{
Anand Jain2b902df2017-10-09 11:07:46 +08006440 if (error)
6441 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6442 devid, uuid);
6443 else
6444 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6445 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006446}
6447
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006448static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006449 struct extent_buffer *leaf,
6450 struct btrfs_chunk *chunk)
6451{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006452 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006453 struct map_lookup *map;
6454 struct extent_map *em;
6455 u64 logical;
6456 u64 length;
6457 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006458 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006459 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006460 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006461 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006462
Chris Masone17cade2008-04-15 15:41:47 -04006463 logical = key->offset;
6464 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006465 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006466
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006468 if (ret)
6469 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006470
Chris Mason890871b2009-09-02 16:24:52 -04006471 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006472 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006473 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006474
6475 /* already mapped? */
6476 if (em && em->start <= logical && em->start + em->len > logical) {
6477 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006478 return 0;
6479 } else if (em) {
6480 free_extent_map(em);
6481 }
Chris Mason0b86a832008-03-24 15:01:56 -04006482
David Sterba172ddd62011-04-21 00:48:27 +02006483 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006484 if (!em)
6485 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006486 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006487 if (!map) {
6488 free_extent_map(em);
6489 return -ENOMEM;
6490 }
6491
Wang Shilong298a8f92014-06-19 10:42:52 +08006492 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006493 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006494 em->start = logical;
6495 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006496 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006497 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006498 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006499
Chris Mason593060d2008-03-25 16:50:33 -04006500 map->num_stripes = num_stripes;
6501 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6502 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006503 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6504 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006505 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006506 for (i = 0; i < num_stripes; i++) {
6507 map->stripes[i].physical =
6508 btrfs_stripe_offset_nr(leaf, chunk, i);
6509 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006510 read_extent_buffer(leaf, uuid, (unsigned long)
6511 btrfs_stripe_dev_uuid_nr(chunk, i),
6512 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006513 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006514 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006515 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006516 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006517 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006518 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006519 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006520 }
Chris Masondfe25022008-05-13 13:46:40 -04006521 if (!map->stripes[i].dev) {
6522 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006523 add_missing_dev(fs_info->fs_devices, devid,
6524 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006525 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006526 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006527 btrfs_err(fs_info,
6528 "failed to init missing dev %llu: %ld",
6529 devid, PTR_ERR(map->stripes[i].dev));
6530 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006531 }
Anand Jain2b902df2017-10-09 11:07:46 +08006532 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006533 }
6534 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006535 }
6536
Chris Mason890871b2009-09-02 16:24:52 -04006537 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006538 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006539 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006540 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006541 free_extent_map(em);
6542
6543 return 0;
6544}
6545
Jeff Mahoney143bede2012-03-01 14:56:26 +01006546static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006547 struct btrfs_dev_item *dev_item,
6548 struct btrfs_device *device)
6549{
6550 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006551
6552 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006553 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6554 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006555 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006556 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006557 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006558 device->type = btrfs_device_type(leaf, dev_item);
6559 device->io_align = btrfs_device_io_align(leaf, dev_item);
6560 device->io_width = btrfs_device_io_width(leaf, dev_item);
6561 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006562 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006563 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006564
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006565 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006566 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006567}
6568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006569static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006570 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006571{
6572 struct btrfs_fs_devices *fs_devices;
6573 int ret;
6574
Li Zefanb367e472011-12-07 11:38:24 +08006575 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006576 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006577
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006578 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006579 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006580 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006581 return fs_devices;
6582
Yan Zheng2b820322008-11-17 21:11:30 -05006583 fs_devices = fs_devices->seed;
6584 }
6585
6586 fs_devices = find_fsid(fsid);
6587 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006588 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006589 return ERR_PTR(-ENOENT);
6590
6591 fs_devices = alloc_fs_devices(fsid);
6592 if (IS_ERR(fs_devices))
6593 return fs_devices;
6594
6595 fs_devices->seeding = 1;
6596 fs_devices->opened = 1;
6597 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006598 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006599
6600 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006601 if (IS_ERR(fs_devices))
6602 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006603
Christoph Hellwig97288f22008-12-02 06:36:09 -05006604 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006605 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006606 if (ret) {
6607 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006608 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006609 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006610 }
Yan Zheng2b820322008-11-17 21:11:30 -05006611
6612 if (!fs_devices->seeding) {
6613 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006614 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006615 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006616 goto out;
6617 }
6618
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006619 fs_devices->seed = fs_info->fs_devices->seed;
6620 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006621out:
Miao Xie5f375832014-09-03 21:35:46 +08006622 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006623}
6624
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006625static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006626 struct extent_buffer *leaf,
6627 struct btrfs_dev_item *dev_item)
6628{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006629 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006630 struct btrfs_device *device;
6631 u64 devid;
6632 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006633 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006634 u8 dev_uuid[BTRFS_UUID_SIZE];
6635
Chris Mason0b86a832008-03-24 15:01:56 -04006636 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006637 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006638 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006639 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006640 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006641
Anand Jain44880fd2017-07-29 17:50:09 +08006642 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006643 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006644 if (IS_ERR(fs_devices))
6645 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006646 }
6647
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006648 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006649 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006650 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006651 btrfs_report_missing_device(fs_info, devid,
6652 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006653 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006654 }
Yan Zheng2b820322008-11-17 21:11:30 -05006655
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006656 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006657 if (IS_ERR(device)) {
6658 btrfs_err(fs_info,
6659 "failed to add missing dev %llu: %ld",
6660 devid, PTR_ERR(device));
6661 return PTR_ERR(device);
6662 }
Anand Jain2b902df2017-10-09 11:07:46 +08006663 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006664 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006665 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006666 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6667 btrfs_report_missing_device(fs_info,
6668 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006669 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006670 }
6671 btrfs_report_missing_device(fs_info, devid,
6672 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006673 }
Miao Xie5f375832014-09-03 21:35:46 +08006674
6675 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006676 /*
6677 * this happens when a device that was properly setup
6678 * in the device info lists suddenly goes bad.
6679 * device->bdev is NULL, and so we have to set
6680 * device->missing to one here
6681 */
Miao Xie5f375832014-09-03 21:35:46 +08006682 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006683 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006684 }
Miao Xie5f375832014-09-03 21:35:46 +08006685
6686 /* Move the device to its own fs_devices */
6687 if (device->fs_devices != fs_devices) {
6688 ASSERT(device->missing);
6689
6690 list_move(&device->dev_list, &fs_devices->devices);
6691 device->fs_devices->num_devices--;
6692 fs_devices->num_devices++;
6693
6694 device->fs_devices->missing_devices--;
6695 fs_devices->missing_devices++;
6696
6697 device->fs_devices = fs_devices;
6698 }
Yan Zheng2b820322008-11-17 21:11:30 -05006699 }
6700
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006701 if (device->fs_devices != fs_info->fs_devices) {
Yan Zheng2b820322008-11-17 21:11:30 -05006702 BUG_ON(device->writeable);
6703 if (device->generation !=
6704 btrfs_device_generation(leaf, dev_item))
6705 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006706 }
Chris Mason0b86a832008-03-24 15:01:56 -04006707
6708 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006709 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006710 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006711 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006712 atomic64_add(device->total_bytes - device->bytes_used,
6713 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006714 }
Chris Mason0b86a832008-03-24 15:01:56 -04006715 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006716 return ret;
6717}
6718
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006719int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006720{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006721 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006722 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006723 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006724 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006725 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006726 u8 *array_ptr;
6727 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006728 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006729 u32 num_stripes;
6730 u32 array_size;
6731 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006732 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006733 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006734 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006735
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006736 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006737 /*
6738 * This will create extent buffer of nodesize, superblock size is
6739 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6740 * overallocate but we can keep it as-is, only the first page is used.
6741 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006742 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006743 if (IS_ERR(sb))
6744 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006745 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006746 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006747 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006748 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006749 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006750 * pages up-to-date when the page is larger: extent does not cover the
6751 * whole page and consequently check_page_uptodate does not find all
6752 * the page's extents up-to-date (the hole beyond sb),
6753 * write_extent_buffer then triggers a WARN_ON.
6754 *
6755 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6756 * but sb spans only this function. Add an explicit SetPageUptodate call
6757 * to silence the warning eg. on PowerPC 64.
6758 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006759 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006760 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006761
Chris Masona061fc82008-05-07 11:43:44 -04006762 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006763 array_size = btrfs_super_sys_array_size(super_copy);
6764
David Sterba1ffb22c2014-10-31 19:02:42 +01006765 array_ptr = super_copy->sys_chunk_array;
6766 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6767 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006768
David Sterba1ffb22c2014-10-31 19:02:42 +01006769 while (cur_offset < array_size) {
6770 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006771 len = sizeof(*disk_key);
6772 if (cur_offset + len > array_size)
6773 goto out_short_read;
6774
Chris Mason0b86a832008-03-24 15:01:56 -04006775 btrfs_disk_key_to_cpu(&key, disk_key);
6776
David Sterba1ffb22c2014-10-31 19:02:42 +01006777 array_ptr += len;
6778 sb_array_offset += len;
6779 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006780
Chris Mason0d81ba52008-03-24 15:02:07 -04006781 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006782 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006783 /*
6784 * At least one btrfs_chunk with one stripe must be
6785 * present, exact stripe count check comes afterwards
6786 */
6787 len = btrfs_chunk_item_size(1);
6788 if (cur_offset + len > array_size)
6789 goto out_short_read;
6790
6791 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006792 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006793 btrfs_err(fs_info,
6794 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006795 num_stripes, cur_offset);
6796 ret = -EIO;
6797 break;
6798 }
6799
Liu Boe06cd3d2016-06-03 12:05:15 -07006800 type = btrfs_chunk_type(sb, chunk);
6801 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006802 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006803 "invalid chunk type %llu in sys_array at offset %u",
6804 type, cur_offset);
6805 ret = -EIO;
6806 break;
6807 }
6808
David Sterbae3540ea2014-11-05 15:24:51 +01006809 len = btrfs_chunk_item_size(num_stripes);
6810 if (cur_offset + len > array_size)
6811 goto out_short_read;
6812
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006813 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006814 if (ret)
6815 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006816 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006817 btrfs_err(fs_info,
6818 "unexpected item type %u in sys_array at offset %u",
6819 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006820 ret = -EIO;
6821 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006822 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006823 array_ptr += len;
6824 sb_array_offset += len;
6825 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006826 }
Liu Bod8651772016-06-03 17:41:42 -07006827 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006828 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006829 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006830
6831out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006832 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006833 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006834 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006835 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006836 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006837}
6838
Qu Wenruo21634a12017-03-09 09:34:36 +08006839/*
6840 * Check if all chunks in the fs are OK for read-write degraded mount
6841 *
6842 * Return true if all chunks meet the minimal RW mount requirements.
6843 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6844 */
6845bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
6846{
6847 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6848 struct extent_map *em;
6849 u64 next_start = 0;
6850 bool ret = true;
6851
6852 read_lock(&map_tree->map_tree.lock);
6853 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6854 read_unlock(&map_tree->map_tree.lock);
6855 /* No chunk at all? Return false anyway */
6856 if (!em) {
6857 ret = false;
6858 goto out;
6859 }
6860 while (em) {
6861 struct map_lookup *map;
6862 int missing = 0;
6863 int max_tolerated;
6864 int i;
6865
6866 map = em->map_lookup;
6867 max_tolerated =
6868 btrfs_get_num_tolerated_disk_barrier_failures(
6869 map->type);
6870 for (i = 0; i < map->num_stripes; i++) {
6871 struct btrfs_device *dev = map->stripes[i].dev;
6872
6873 if (!dev || !dev->bdev || dev->missing ||
6874 dev->last_flush_error)
6875 missing++;
6876 }
6877 if (missing > max_tolerated) {
6878 btrfs_warn(fs_info,
6879 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6880 em->start, missing, max_tolerated);
6881 free_extent_map(em);
6882 ret = false;
6883 goto out;
6884 }
6885 next_start = extent_map_end(em);
6886 free_extent_map(em);
6887
6888 read_lock(&map_tree->map_tree.lock);
6889 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6890 (u64)(-1) - next_start);
6891 read_unlock(&map_tree->map_tree.lock);
6892 }
6893out:
6894 return ret;
6895}
6896
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006897int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006898{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006899 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006900 struct btrfs_path *path;
6901 struct extent_buffer *leaf;
6902 struct btrfs_key key;
6903 struct btrfs_key found_key;
6904 int ret;
6905 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006906 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006907
Chris Mason0b86a832008-03-24 15:01:56 -04006908 path = btrfs_alloc_path();
6909 if (!path)
6910 return -ENOMEM;
6911
Li Zefanb367e472011-12-07 11:38:24 +08006912 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006913 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006914
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006915 /*
6916 * Read all device items, and then all the chunk items. All
6917 * device items are found before any chunk item (their object id
6918 * is smaller than the lowest possible object id for a chunk
6919 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006920 */
6921 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6922 key.offset = 0;
6923 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006924 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006925 if (ret < 0)
6926 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006927 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006928 leaf = path->nodes[0];
6929 slot = path->slots[0];
6930 if (slot >= btrfs_header_nritems(leaf)) {
6931 ret = btrfs_next_leaf(root, path);
6932 if (ret == 0)
6933 continue;
6934 if (ret < 0)
6935 goto error;
6936 break;
6937 }
6938 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006939 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6940 struct btrfs_dev_item *dev_item;
6941 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006942 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006943 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006944 if (ret)
6945 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006946 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006947 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6948 struct btrfs_chunk *chunk;
6949 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006950 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006951 if (ret)
6952 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006953 }
6954 path->slots[0]++;
6955 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006956
6957 /*
6958 * After loading chunk tree, we've got all device information,
6959 * do another round of validation checks.
6960 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006961 if (total_dev != fs_info->fs_devices->total_devices) {
6962 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006963 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006964 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006965 total_dev);
6966 ret = -EINVAL;
6967 goto error;
6968 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006969 if (btrfs_super_total_bytes(fs_info->super_copy) <
6970 fs_info->fs_devices->total_rw_bytes) {
6971 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006972 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006973 btrfs_super_total_bytes(fs_info->super_copy),
6974 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006975 ret = -EINVAL;
6976 goto error;
6977 }
Chris Mason0b86a832008-03-24 15:01:56 -04006978 ret = 0;
6979error:
David Sterba34441362016-10-04 19:34:27 +02006980 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006981 mutex_unlock(&uuid_mutex);
6982
Yan Zheng2b820322008-11-17 21:11:30 -05006983 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006984 return ret;
6985}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006986
Miao Xiecb517ea2013-05-15 07:48:19 +00006987void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6988{
6989 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6990 struct btrfs_device *device;
6991
Liu Bo29cc83f2014-05-11 23:14:59 +08006992 while (fs_devices) {
6993 mutex_lock(&fs_devices->device_list_mutex);
6994 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04006995 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08006996 mutex_unlock(&fs_devices->device_list_mutex);
6997
6998 fs_devices = fs_devices->seed;
6999 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007000}
7001
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007002static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7003{
7004 int i;
7005
7006 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7007 btrfs_dev_stat_reset(dev, i);
7008}
7009
7010int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7011{
7012 struct btrfs_key key;
7013 struct btrfs_key found_key;
7014 struct btrfs_root *dev_root = fs_info->dev_root;
7015 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7016 struct extent_buffer *eb;
7017 int slot;
7018 int ret = 0;
7019 struct btrfs_device *device;
7020 struct btrfs_path *path = NULL;
7021 int i;
7022
7023 path = btrfs_alloc_path();
7024 if (!path) {
7025 ret = -ENOMEM;
7026 goto out;
7027 }
7028
7029 mutex_lock(&fs_devices->device_list_mutex);
7030 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7031 int item_size;
7032 struct btrfs_dev_stats_item *ptr;
7033
David Sterba242e2952016-01-25 17:51:31 +01007034 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7035 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007036 key.offset = device->devid;
7037 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7038 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007039 __btrfs_reset_dev_stats(device);
7040 device->dev_stats_valid = 1;
7041 btrfs_release_path(path);
7042 continue;
7043 }
7044 slot = path->slots[0];
7045 eb = path->nodes[0];
7046 btrfs_item_key_to_cpu(eb, &found_key, slot);
7047 item_size = btrfs_item_size_nr(eb, slot);
7048
7049 ptr = btrfs_item_ptr(eb, slot,
7050 struct btrfs_dev_stats_item);
7051
7052 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7053 if (item_size >= (1 + i) * sizeof(__le64))
7054 btrfs_dev_stat_set(device, i,
7055 btrfs_dev_stats_value(eb, ptr, i));
7056 else
7057 btrfs_dev_stat_reset(device, i);
7058 }
7059
7060 device->dev_stats_valid = 1;
7061 btrfs_dev_stat_print_on_load(device);
7062 btrfs_release_path(path);
7063 }
7064 mutex_unlock(&fs_devices->device_list_mutex);
7065
7066out:
7067 btrfs_free_path(path);
7068 return ret < 0 ? ret : 0;
7069}
7070
7071static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007072 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007073 struct btrfs_device *device)
7074{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007075 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007076 struct btrfs_path *path;
7077 struct btrfs_key key;
7078 struct extent_buffer *eb;
7079 struct btrfs_dev_stats_item *ptr;
7080 int ret;
7081 int i;
7082
David Sterba242e2952016-01-25 17:51:31 +01007083 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7084 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007085 key.offset = device->devid;
7086
7087 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007088 if (!path)
7089 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007090 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7091 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007092 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007093 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007094 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007095 goto out;
7096 }
7097
7098 if (ret == 0 &&
7099 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7100 /* need to delete old one and insert a new one */
7101 ret = btrfs_del_item(trans, dev_root, path);
7102 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007103 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007104 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007105 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007106 goto out;
7107 }
7108 ret = 1;
7109 }
7110
7111 if (ret == 1) {
7112 /* need to insert a new item */
7113 btrfs_release_path(path);
7114 ret = btrfs_insert_empty_item(trans, dev_root, path,
7115 &key, sizeof(*ptr));
7116 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007117 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007118 "insert dev_stats item for device %s failed %d",
7119 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007120 goto out;
7121 }
7122 }
7123
7124 eb = path->nodes[0];
7125 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7126 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7127 btrfs_set_dev_stats_value(eb, ptr, i,
7128 btrfs_dev_stat_read(device, i));
7129 btrfs_mark_buffer_dirty(eb);
7130
7131out:
7132 btrfs_free_path(path);
7133 return ret;
7134}
7135
7136/*
7137 * called from commit_transaction. Writes all changed device stats to disk.
7138 */
7139int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7140 struct btrfs_fs_info *fs_info)
7141{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007142 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7143 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007144 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007145 int ret = 0;
7146
7147 mutex_lock(&fs_devices->device_list_mutex);
7148 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007149 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7150 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007151 continue;
7152
Nikolay Borisov9deae962017-10-24 13:47:37 +03007153
7154 /*
7155 * There is a LOAD-LOAD control dependency between the value of
7156 * dev_stats_ccnt and updating the on-disk values which requires
7157 * reading the in-memory counters. Such control dependencies
7158 * require explicit read memory barriers.
7159 *
7160 * This memory barriers pairs with smp_mb__before_atomic in
7161 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7162 * barrier implied by atomic_xchg in
7163 * btrfs_dev_stats_read_and_reset
7164 */
7165 smp_rmb();
7166
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007167 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007168 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007169 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007170 }
7171 mutex_unlock(&fs_devices->device_list_mutex);
7172
7173 return ret;
7174}
7175
Stefan Behrens442a4f62012-05-25 16:06:08 +02007176void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7177{
7178 btrfs_dev_stat_inc(dev, index);
7179 btrfs_dev_stat_print_on_error(dev);
7180}
7181
Eric Sandeen48a3b632013-04-25 20:41:01 +00007182static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007183{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007184 if (!dev->dev_stats_valid)
7185 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007186 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007187 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007188 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007189 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7190 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7191 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007192 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7193 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007194}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007195
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007196static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7197{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007198 int i;
7199
7200 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7201 if (btrfs_dev_stat_read(dev, i) != 0)
7202 break;
7203 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7204 return; /* all values == 0, suppress message */
7205
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007206 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007207 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007208 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007209 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7210 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7211 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7212 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7213 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7214}
7215
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007216int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007217 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007218{
7219 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007220 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007221 int i;
7222
7223 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007224 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007225 mutex_unlock(&fs_devices->device_list_mutex);
7226
7227 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007228 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007229 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007230 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007231 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007232 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007233 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007234 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7235 if (stats->nr_items > i)
7236 stats->values[i] =
7237 btrfs_dev_stat_read_and_reset(dev, i);
7238 else
7239 btrfs_dev_stat_reset(dev, i);
7240 }
7241 } else {
7242 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7243 if (stats->nr_items > i)
7244 stats->values[i] = btrfs_dev_stat_read(dev, i);
7245 }
7246 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7247 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7248 return 0;
7249}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007250
David Sterbada353f62017-02-14 17:55:53 +01007251void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007252{
7253 struct buffer_head *bh;
7254 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007255 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007256
Anand Jain12b1c262015-08-14 18:32:59 +08007257 if (!bdev)
7258 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007259
Anand Jain12b1c262015-08-14 18:32:59 +08007260 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7261 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007262
Anand Jain12b1c262015-08-14 18:32:59 +08007263 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7264 continue;
7265
7266 disk_super = (struct btrfs_super_block *)bh->b_data;
7267
7268 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7269 set_buffer_dirty(bh);
7270 sync_dirty_buffer(bh);
7271 brelse(bh);
7272 }
7273
7274 /* Notify udev that device has changed */
7275 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7276
7277 /* Update ctime/mtime for device path for libblkid */
7278 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007279}
Miao Xie935e5cc2014-09-03 21:35:33 +08007280
7281/*
7282 * Update the size of all devices, which is used for writing out the
7283 * super blocks.
7284 */
7285void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7286{
7287 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7288 struct btrfs_device *curr, *next;
7289
7290 if (list_empty(&fs_devices->resized_devices))
7291 return;
7292
7293 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007294 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007295 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7296 resized_list) {
7297 list_del_init(&curr->resized_list);
7298 curr->commit_total_bytes = curr->disk_total_bytes;
7299 }
David Sterba34441362016-10-04 19:34:27 +02007300 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007301 mutex_unlock(&fs_devices->device_list_mutex);
7302}
Miao Xiece7213c2014-09-03 21:35:34 +08007303
7304/* Must be invoked during the transaction commit */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007305void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
Miao Xiece7213c2014-09-03 21:35:34 +08007306 struct btrfs_transaction *transaction)
7307{
7308 struct extent_map *em;
7309 struct map_lookup *map;
7310 struct btrfs_device *dev;
7311 int i;
7312
7313 if (list_empty(&transaction->pending_chunks))
7314 return;
7315
7316 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007317 mutex_lock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007318 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007319 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007320
7321 for (i = 0; i < map->num_stripes; i++) {
7322 dev = map->stripes[i].dev;
7323 dev->commit_bytes_used = dev->bytes_used;
7324 }
7325 }
David Sterba34441362016-10-04 19:34:27 +02007326 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007327}
Anand Jain5a13f432015-03-10 06:38:31 +08007328
7329void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7330{
7331 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7332 while (fs_devices) {
7333 fs_devices->fs_info = fs_info;
7334 fs_devices = fs_devices->seed;
7335 }
7336}
7337
7338void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7339{
7340 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7341 while (fs_devices) {
7342 fs_devices->fs_info = NULL;
7343 fs_devices = fs_devices->seed;
7344 }
7345}