blob: e782f685375761ccae65d8adbf5b0d7f7576bac5 [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) {
Anand Jainebbede42017-12-04 12:54:52 +0800694 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800695 fs_devices->seeding = 1;
696 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800697 if (bdev_read_only(bdev))
698 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
699 else
700 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800701 }
702
703 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800704 if (!blk_queue_nonrot(q))
705 fs_devices->rotating = 1;
706
707 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800708 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800709 device->mode = flags;
710
711 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800712 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
713 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800714 fs_devices->rw_devices++;
715 list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
716 }
717 brelse(bh);
718
719 return 0;
720
721error_brelse:
722 brelse(bh);
723 blkdev_put(bdev, flags);
724
725 return -EINVAL;
726}
727
David Sterba60999ca2014-03-26 18:26:36 +0100728/*
729 * Add new device to list of registered devices
730 *
731 * Returns:
732 * 1 - first time device is seen
733 * 0 - device already known
734 * < 0 - error
735 */
Chris Masona1b32a52008-09-05 16:09:51 -0400736static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400737 struct btrfs_super_block *disk_super,
738 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
739{
740 struct btrfs_device *device;
741 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400742 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100743 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400744 u64 found_transid = btrfs_super_generation(disk_super);
745
746 fs_devices = find_fsid(disk_super->fsid);
747 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300748 fs_devices = alloc_fs_devices(disk_super->fsid);
749 if (IS_ERR(fs_devices))
750 return PTR_ERR(fs_devices);
751
Chris Mason8a4b83c2008-03-24 15:02:07 -0400752 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300753
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754 device = NULL;
755 } else {
David Sterba35c70102017-06-15 19:51:51 +0200756 device = find_device(fs_devices, devid,
757 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 }
Miao Xie443f24f2014-07-24 11:37:15 +0800759
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500761 if (fs_devices->opened)
762 return -EBUSY;
763
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300764 device = btrfs_alloc_device(NULL, &devid,
765 disk_super->dev_item.uuid);
766 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400767 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300768 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400769 }
Josef Bacik606686e2012-06-04 14:03:51 -0400770
771 name = rcu_string_strdup(path, GFP_NOFS);
772 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100773 free_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400774 return -ENOMEM;
775 }
Josef Bacik606686e2012-06-04 14:03:51 -0400776 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200777
Chris Masone5e9a522009-06-10 15:17:02 -0400778 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000779 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100780 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400781 mutex_unlock(&fs_devices->device_list_mutex);
782
David Sterba60999ca2014-03-26 18:26:36 +0100783 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500784 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400785 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800786 /*
787 * When FS is already mounted.
788 * 1. If you are here and if the device->name is NULL that
789 * means this device was missing at time of FS mount.
790 * 2. If you are here and if the device->name is different
791 * from 'path' that means either
792 * a. The same device disappeared and reappeared with
793 * different name. or
794 * b. The missing-disk-which-was-replaced, has
795 * reappeared now.
796 *
797 * We must allow 1 and 2a above. But 2b would be a spurious
798 * and unintentional.
799 *
800 * Further in case of 1 and 2a above, the disk at 'path'
801 * would have missed some transaction when it was away and
802 * in case of 2a the stale bdev has to be updated as well.
803 * 2b must not be allowed at all time.
804 */
805
806 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700807 * For now, we do allow update to btrfs_fs_device through the
808 * btrfs dev scan cli after FS has been mounted. We're still
809 * tracking a problem where systems fail mount by subvolume id
810 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800811 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700812 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800813 /*
814 * That is if the FS is _not_ mounted and if you
815 * are here, that means there is more than one
816 * disk with same uuid and devid.We keep the one
817 * with larger generation number or the last-in if
818 * generation are equal.
819 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700820 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800821 }
Anand Jainb96de002014-07-03 18:22:05 +0800822
Josef Bacik606686e2012-06-04 14:03:51 -0400823 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000824 if (!name)
825 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400826 rcu_string_free(device->name);
827 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500828 if (device->missing) {
829 fs_devices->missing_devices--;
830 device->missing = 0;
831 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400832 }
833
Anand Jain77bdae42014-07-03 18:22:06 +0800834 /*
835 * Unmount does not free the btrfs_device struct but would zero
836 * generation along with most of the other members. So just update
837 * it back. We need it to pick the disk with largest generation
838 * (as above).
839 */
840 if (!fs_devices->opened)
841 device->generation = found_transid;
842
Anand Jain4fde46f2015-06-17 21:10:48 +0800843 /*
844 * if there is new btrfs on an already registered device,
845 * then remove the stale device entry.
846 */
Anand Jain02feae32016-02-13 10:01:40 +0800847 if (ret > 0)
848 btrfs_free_stale_device(device);
Anand Jain4fde46f2015-06-17 21:10:48 +0800849
Chris Mason8a4b83c2008-03-24 15:02:07 -0400850 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100851
852 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400853}
854
Yan Zhenge4404d62008-12-12 10:03:26 -0500855static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
856{
857 struct btrfs_fs_devices *fs_devices;
858 struct btrfs_device *device;
859 struct btrfs_device *orig_dev;
860
Ilya Dryomov2208a372013-08-12 14:33:03 +0300861 fs_devices = alloc_fs_devices(orig->fsid);
862 if (IS_ERR(fs_devices))
863 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500864
Miao Xieadbbb862014-09-03 21:35:42 +0800865 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400866 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500867
Xiao Guangrong46224702011-04-20 10:08:47 +0000868 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500869 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400870 struct rcu_string *name;
871
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300872 device = btrfs_alloc_device(NULL, &orig_dev->devid,
873 orig_dev->uuid);
874 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500875 goto error;
876
Josef Bacik606686e2012-06-04 14:03:51 -0400877 /*
878 * This is ok to do without rcu read locked because we hold the
879 * uuid mutex so nothing we touch in here is going to disappear.
880 */
Anand Jaine755f782014-06-30 17:12:47 +0800881 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100882 name = rcu_string_strdup(orig_dev->name->str,
883 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800884 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +0100885 free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800886 goto error;
887 }
888 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400889 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500890
Yan Zhenge4404d62008-12-12 10:03:26 -0500891 list_add(&device->dev_list, &fs_devices->devices);
892 device->fs_devices = fs_devices;
893 fs_devices->num_devices++;
894 }
Miao Xieadbbb862014-09-03 21:35:42 +0800895 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500896 return fs_devices;
897error:
Miao Xieadbbb862014-09-03 21:35:42 +0800898 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500899 free_fs_devices(fs_devices);
900 return ERR_PTR(-ENOMEM);
901}
902
Eric Sandeen9eaed212014-08-01 18:12:35 -0500903void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400904{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500905 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800906 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500907
Chris Masondfe25022008-05-13 13:46:40 -0400908 mutex_lock(&uuid_mutex);
909again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000910 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500911 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +0800912 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
913 &device->dev_state)) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100914 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800915 (!latest_dev ||
916 device->generation > latest_dev->generation)) {
917 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500918 }
Yan Zheng2b820322008-11-17 21:11:30 -0500919 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500920 }
Yan Zheng2b820322008-11-17 21:11:30 -0500921
Stefan Behrens8dabb742012-11-06 13:15:27 +0100922 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
923 /*
924 * In the first step, keep the device which has
925 * the correct fsid and the devid that is used
926 * for the dev_replace procedure.
927 * In the second step, the dev_replace state is
928 * read from the device tree and it is known
929 * whether the procedure is really active or
930 * not, which means whether this device is
931 * used or whether it should be removed.
932 */
933 if (step == 0 || device->is_tgtdev_for_dev_replace) {
934 continue;
935 }
936 }
Yan Zheng2b820322008-11-17 21:11:30 -0500937 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100938 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500939 device->bdev = NULL;
940 fs_devices->open_devices--;
941 }
Anand Jainebbede42017-12-04 12:54:52 +0800942 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500943 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +0800944 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Stefan Behrens8dabb742012-11-06 13:15:27 +0100945 if (!device->is_tgtdev_for_dev_replace)
946 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500947 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500948 list_del_init(&device->dev_list);
949 fs_devices->num_devices--;
David Sterba55de4802017-10-30 18:55:47 +0100950 free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400951 }
Yan Zheng2b820322008-11-17 21:11:30 -0500952
953 if (fs_devices->seed) {
954 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500955 goto again;
956 }
957
Miao Xie443f24f2014-07-24 11:37:15 +0800958 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500959
Chris Masondfe25022008-05-13 13:46:40 -0400960 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400961}
Chris Masona0af4692008-05-13 16:03:06 -0400962
David Sterbaf06c5962017-06-06 17:08:23 +0200963static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000964{
965 struct btrfs_device *device;
966
967 device = container_of(head, struct btrfs_device, rcu);
David Sterba55de4802017-10-30 18:55:47 +0100968 free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000969}
970
Anand Jain14238812016-07-22 06:04:53 +0800971static void btrfs_close_bdev(struct btrfs_device *device)
972{
David Sterba08ffcae2017-06-19 16:55:35 +0200973 if (!device->bdev)
974 return;
975
Anand Jainebbede42017-12-04 12:54:52 +0800976 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +0800977 sync_blockdev(device->bdev);
978 invalidate_bdev(device->bdev);
979 }
980
David Sterba08ffcae2017-06-19 16:55:35 +0200981 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +0800982}
983
Anand Jain0ccd0522016-09-22 12:56:13 +0800984static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +0800985{
986 struct btrfs_fs_devices *fs_devices = device->fs_devices;
987 struct btrfs_device *new_device;
988 struct rcu_string *name;
989
990 if (device->bdev)
991 fs_devices->open_devices--;
992
Anand Jainebbede42017-12-04 12:54:52 +0800993 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +0800994 device->devid != BTRFS_DEV_REPLACE_DEVID) {
995 list_del_init(&device->dev_alloc_list);
996 fs_devices->rw_devices--;
997 }
998
999 if (device->missing)
1000 fs_devices->missing_devices--;
1001
1002 new_device = btrfs_alloc_device(NULL, &device->devid,
1003 device->uuid);
1004 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1005
1006 /* Safe because we are under uuid_mutex */
1007 if (device->name) {
1008 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1009 BUG_ON(!name); /* -ENOMEM */
1010 rcu_assign_pointer(new_device->name, name);
1011 }
1012
1013 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1014 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001015}
1016
Yan Zheng2b820322008-11-17 21:11:30 -05001017static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001018{
Sasha Levin2037a092015-05-12 19:31:37 -04001019 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +08001020 struct list_head pending_put;
1021
1022 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -05001023
Yan Zheng2b820322008-11-17 21:11:30 -05001024 if (--fs_devices->opened > 0)
1025 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001026
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001027 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001028 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +08001029 btrfs_prepare_close_one_device(device);
1030 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001031 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001032 mutex_unlock(&fs_devices->device_list_mutex);
1033
Anand Jain0ccd0522016-09-22 12:56:13 +08001034 /*
1035 * btrfs_show_devname() is using the device_list_mutex,
1036 * sometimes call to blkdev_put() leads vfs calling
1037 * into this func. So do put outside of device_list_mutex,
1038 * as of now.
1039 */
1040 while (!list_empty(&pending_put)) {
1041 device = list_first_entry(&pending_put,
1042 struct btrfs_device, dev_list);
1043 list_del(&device->dev_list);
1044 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001045 call_rcu(&device->rcu, free_device_rcu);
Anand Jain0ccd0522016-09-22 12:56:13 +08001046 }
1047
Yan Zhenge4404d62008-12-12 10:03:26 -05001048 WARN_ON(fs_devices->open_devices);
1049 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001050 fs_devices->opened = 0;
1051 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001052
Chris Mason8a4b83c2008-03-24 15:02:07 -04001053 return 0;
1054}
1055
Yan Zheng2b820322008-11-17 21:11:30 -05001056int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1057{
Yan Zhenge4404d62008-12-12 10:03:26 -05001058 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001059 int ret;
1060
1061 mutex_lock(&uuid_mutex);
1062 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001063 if (!fs_devices->opened) {
1064 seed_devices = fs_devices->seed;
1065 fs_devices->seed = NULL;
1066 }
Yan Zheng2b820322008-11-17 21:11:30 -05001067 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001068
1069 while (seed_devices) {
1070 fs_devices = seed_devices;
1071 seed_devices = fs_devices->seed;
1072 __btrfs_close_devices(fs_devices);
1073 free_fs_devices(fs_devices);
1074 }
Yan Zheng2b820322008-11-17 21:11:30 -05001075 return ret;
1076}
1077
Yan Zhenge4404d62008-12-12 10:03:26 -05001078static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
1079 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001080{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001081 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001082 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001083 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001084 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001085
Tejun Heod4d77622010-11-13 11:55:18 +01001086 flags |= FMODE_EXCL;
1087
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001088 list_for_each_entry(device, head, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001089 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001090 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001091 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001092
Anand Jain9f050db2017-11-09 23:45:25 +08001093 if (!latest_dev ||
1094 device->generation > latest_dev->generation)
1095 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001096 }
Chris Masona0af4692008-05-13 16:03:06 -04001097 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001098 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001099 goto out;
1100 }
Yan Zheng2b820322008-11-17 21:11:30 -05001101 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001102 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001103 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001104out:
Yan Zheng2b820322008-11-17 21:11:30 -05001105 return ret;
1106}
1107
1108int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001109 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001110{
1111 int ret;
1112
1113 mutex_lock(&uuid_mutex);
1114 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001115 fs_devices->opened++;
1116 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001117 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001118 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001119 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001120 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001121 return ret;
1122}
1123
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001124static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001125{
1126 kunmap(page);
1127 put_page(page);
1128}
1129
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001130static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1131 struct page **page,
1132 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001133{
1134 void *p;
1135 pgoff_t index;
1136
1137 /* make sure our super fits in the device */
1138 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1139 return 1;
1140
1141 /* make sure our super fits in the page */
1142 if (sizeof(**disk_super) > PAGE_SIZE)
1143 return 1;
1144
1145 /* make sure our super doesn't straddle pages on disk */
1146 index = bytenr >> PAGE_SHIFT;
1147 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1148 return 1;
1149
1150 /* pull in the page with our super */
1151 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1152 index, GFP_KERNEL);
1153
1154 if (IS_ERR_OR_NULL(*page))
1155 return 1;
1156
1157 p = kmap(*page);
1158
1159 /* align our pointer to the offset of the super block */
1160 *disk_super = p + (bytenr & ~PAGE_MASK);
1161
1162 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1163 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1164 btrfs_release_disk_super(*page);
1165 return 1;
1166 }
1167
1168 if ((*disk_super)->label[0] &&
1169 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1170 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1171
1172 return 0;
1173}
1174
David Sterba6f60cbd2013-02-15 11:31:02 -07001175/*
1176 * Look for a btrfs signature on a device. This may be called out of the mount path
1177 * and we are not allowed to call set_blocksize during the scan. The superblock
1178 * is read via pagecache
1179 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001180int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001181 struct btrfs_fs_devices **fs_devices_ret)
1182{
1183 struct btrfs_super_block *disk_super;
1184 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001185 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001186 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001187 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001188 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001189 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001190 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001191
David Sterba6f60cbd2013-02-15 11:31:02 -07001192 /*
1193 * we would like to check all the supers, but that would make
1194 * a btrfs mount succeed after a mkfs from a different FS.
1195 * So, we need to add a special mount option to scan for
1196 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1197 */
1198 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001199 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001200 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001201
1202 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001203 if (IS_ERR(bdev)) {
1204 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001205 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001206 }
1207
Anand Jain6cf86a002016-02-13 10:01:29 +08001208 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
David Sterba6f60cbd2013-02-15 11:31:02 -07001209 goto error_bdev_put;
1210
Xiao Guangronga3438322010-01-06 11:48:18 +00001211 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001212 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001213 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001214
Chris Mason8a4b83c2008-03-24 15:02:07 -04001215 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001216 if (ret > 0) {
1217 if (disk_super->label[0]) {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001218 pr_info("BTRFS: device label %s ", disk_super->label);
David Sterba60999ca2014-03-26 18:26:36 +01001219 } else {
Jeff Mahoney62e85572016-09-20 10:05:01 -04001220 pr_info("BTRFS: device fsid %pU ", disk_super->fsid);
David Sterba60999ca2014-03-26 18:26:36 +01001221 }
1222
Jeff Mahoney62e85572016-09-20 10:05:01 -04001223 pr_cont("devid %llu transid %llu %s\n", devid, transid, path);
David Sterba60999ca2014-03-26 18:26:36 +01001224 ret = 0;
1225 }
Josef Bacik02db0842012-06-21 16:03:58 -04001226 if (!ret && fs_devices_ret)
1227 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001228
Anand Jain6cf86a002016-02-13 10:01:29 +08001229 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001230
1231error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001232 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001233error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001234 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001235 return ret;
1236}
Chris Mason0b86a832008-03-24 15:01:56 -04001237
Miao Xie6d07bce2011-01-05 10:07:31 +00001238/* helper to account the used device space in the range */
1239int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1240 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001241{
1242 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001243 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001244 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001245 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001246 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001247 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001248 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001249 struct extent_buffer *l;
1250
Miao Xie6d07bce2011-01-05 10:07:31 +00001251 *length = 0;
1252
Stefan Behrens63a212a2012-11-05 18:29:28 +01001253 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001254 return 0;
1255
Yan Zheng2b820322008-11-17 21:11:30 -05001256 path = btrfs_alloc_path();
1257 if (!path)
1258 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001259 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001260
Chris Mason0b86a832008-03-24 15:01:56 -04001261 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001262 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001263 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001264
1265 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001266 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001267 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001268 if (ret > 0) {
1269 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1270 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001271 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001272 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001273
Chris Mason0b86a832008-03-24 15:01:56 -04001274 while (1) {
1275 l = path->nodes[0];
1276 slot = path->slots[0];
1277 if (slot >= btrfs_header_nritems(l)) {
1278 ret = btrfs_next_leaf(root, path);
1279 if (ret == 0)
1280 continue;
1281 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001282 goto out;
1283
1284 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001285 }
1286 btrfs_item_key_to_cpu(l, &key, slot);
1287
1288 if (key.objectid < device->devid)
1289 goto next;
1290
1291 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001292 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001293
David Sterba962a2982014-06-04 18:41:45 +02001294 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001295 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001296
Chris Mason0b86a832008-03-24 15:01:56 -04001297 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001298 extent_end = key.offset + btrfs_dev_extent_length(l,
1299 dev_extent);
1300 if (key.offset <= start && extent_end > end) {
1301 *length = end - start + 1;
1302 break;
1303 } else if (key.offset <= start && extent_end > start)
1304 *length += extent_end - start;
1305 else if (key.offset > start && extent_end <= end)
1306 *length += extent_end - key.offset;
1307 else if (key.offset > start && key.offset <= end) {
1308 *length += end - key.offset + 1;
1309 break;
1310 } else if (key.offset > end)
1311 break;
1312
1313next:
1314 path->slots[0]++;
1315 }
1316 ret = 0;
1317out:
1318 btrfs_free_path(path);
1319 return ret;
1320}
1321
Jeff Mahoney499f3772015-06-15 09:41:17 -04001322static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001323 struct btrfs_device *device,
1324 u64 *start, u64 len)
1325{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001326 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001327 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001328 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001329 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001330 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001331
Jeff Mahoney499f3772015-06-15 09:41:17 -04001332 if (transaction)
1333 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001334again:
1335 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001336 struct map_lookup *map;
1337 int i;
1338
Jeff Mahoney95617d62015-06-03 10:55:48 -04001339 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001340 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001341 u64 end;
1342
Josef Bacik6df9a952013-06-27 13:22:46 -04001343 if (map->stripes[i].dev != device)
1344 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001345 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001346 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001347 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001348 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001349 /*
1350 * Make sure that while processing the pinned list we do
1351 * not override our *start with a lower value, because
1352 * we can have pinned chunks that fall within this
1353 * device hole and that have lower physical addresses
1354 * than the pending chunks we processed before. If we
1355 * do not take this special care we can end up getting
1356 * 2 pending chunks that start at the same physical
1357 * device offsets because the end offset of a pinned
1358 * chunk can be equal to the start offset of some
1359 * pending chunk.
1360 */
1361 end = map->stripes[i].physical + em->orig_block_len;
1362 if (end > *start) {
1363 *start = end;
1364 ret = 1;
1365 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001366 }
1367 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001368 if (search_list != &fs_info->pinned_chunks) {
1369 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001370 goto again;
1371 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001372
1373 return ret;
1374}
1375
1376
Chris Mason0b86a832008-03-24 15:01:56 -04001377/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001378 * find_free_dev_extent_start - find free space in the specified device
1379 * @device: the device which we search the free space in
1380 * @num_bytes: the size of the free space that we need
1381 * @search_start: the position from which to begin the search
1382 * @start: store the start of the free space.
1383 * @len: the size of the free space. that we find, or the size
1384 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001385 *
Chris Mason0b86a832008-03-24 15:01:56 -04001386 * this uses a pretty simple search, the expectation is that it is
1387 * called very infrequently and that a given device has a small number
1388 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001389 *
1390 * @start is used to store the start of the free space if we find. But if we
1391 * don't find suitable free space, it will be used to store the start position
1392 * of the max free space.
1393 *
1394 * @len is used to store the size of the free space that we find.
1395 * But if we don't find suitable free space, it is used to store the size of
1396 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001397 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001398int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1399 struct btrfs_device *device, u64 num_bytes,
1400 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001401{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001402 struct btrfs_fs_info *fs_info = device->fs_info;
1403 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001404 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001405 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001406 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001407 u64 hole_size;
1408 u64 max_hole_start;
1409 u64 max_hole_size;
1410 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001411 u64 search_end = device->total_bytes;
1412 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001413 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001414 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001415
1416 /*
1417 * We don't want to overwrite the superblock on the drive nor any area
1418 * used by the boot loader (grub for example), so we make sure to start
1419 * at an offset of at least 1MB.
1420 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001421 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001422
Josef Bacik6df9a952013-06-27 13:22:46 -04001423 path = btrfs_alloc_path();
1424 if (!path)
1425 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001426
Miao Xie7bfc8372011-01-05 10:07:26 +00001427 max_hole_start = search_start;
1428 max_hole_size = 0;
1429
Zhao Leif2ab7612015-02-16 18:52:17 +08001430again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001431 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001432 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001433 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001434 }
1435
David Sterbae4058b52015-11-27 16:31:35 +01001436 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001437 path->search_commit_root = 1;
1438 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001439
Chris Mason0b86a832008-03-24 15:01:56 -04001440 key.objectid = device->devid;
1441 key.offset = search_start;
1442 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001443
Li Zefan125ccb02011-12-08 15:07:24 +08001444 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001445 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001446 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001447 if (ret > 0) {
1448 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1449 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001450 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001451 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001452
Chris Mason0b86a832008-03-24 15:01:56 -04001453 while (1) {
1454 l = path->nodes[0];
1455 slot = path->slots[0];
1456 if (slot >= btrfs_header_nritems(l)) {
1457 ret = btrfs_next_leaf(root, path);
1458 if (ret == 0)
1459 continue;
1460 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001461 goto out;
1462
1463 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001464 }
1465 btrfs_item_key_to_cpu(l, &key, slot);
1466
1467 if (key.objectid < device->devid)
1468 goto next;
1469
1470 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001471 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001472
David Sterba962a2982014-06-04 18:41:45 +02001473 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001474 goto next;
1475
Miao Xie7bfc8372011-01-05 10:07:26 +00001476 if (key.offset > search_start) {
1477 hole_size = key.offset - search_start;
1478
Josef Bacik6df9a952013-06-27 13:22:46 -04001479 /*
1480 * Have to check before we set max_hole_start, otherwise
1481 * we could end up sending back this offset anyway.
1482 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001483 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001484 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001485 hole_size)) {
1486 if (key.offset >= search_start) {
1487 hole_size = key.offset - search_start;
1488 } else {
1489 WARN_ON_ONCE(1);
1490 hole_size = 0;
1491 }
1492 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001493
Miao Xie7bfc8372011-01-05 10:07:26 +00001494 if (hole_size > max_hole_size) {
1495 max_hole_start = search_start;
1496 max_hole_size = hole_size;
1497 }
1498
1499 /*
1500 * If this free space is greater than which we need,
1501 * it must be the max free space that we have found
1502 * until now, so max_hole_start must point to the start
1503 * of this free space and the length of this free space
1504 * is stored in max_hole_size. Thus, we return
1505 * max_hole_start and max_hole_size and go back to the
1506 * caller.
1507 */
1508 if (hole_size >= num_bytes) {
1509 ret = 0;
1510 goto out;
1511 }
1512 }
1513
Chris Mason0b86a832008-03-24 15:01:56 -04001514 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001515 extent_end = key.offset + btrfs_dev_extent_length(l,
1516 dev_extent);
1517 if (extent_end > search_start)
1518 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001519next:
1520 path->slots[0]++;
1521 cond_resched();
1522 }
Chris Mason0b86a832008-03-24 15:01:56 -04001523
liubo38c01b92011-08-02 02:39:03 +00001524 /*
1525 * At this point, search_start should be the end of
1526 * allocated dev extents, and when shrinking the device,
1527 * search_end may be smaller than search_start.
1528 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001529 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001530 hole_size = search_end - search_start;
1531
Jeff Mahoney499f3772015-06-15 09:41:17 -04001532 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001533 hole_size)) {
1534 btrfs_release_path(path);
1535 goto again;
1536 }
Chris Mason0b86a832008-03-24 15:01:56 -04001537
Zhao Leif2ab7612015-02-16 18:52:17 +08001538 if (hole_size > max_hole_size) {
1539 max_hole_start = search_start;
1540 max_hole_size = hole_size;
1541 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001542 }
1543
Miao Xie7bfc8372011-01-05 10:07:26 +00001544 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001545 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001546 ret = -ENOSPC;
1547 else
1548 ret = 0;
1549
1550out:
Yan Zheng2b820322008-11-17 21:11:30 -05001551 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001552 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001553 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001554 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001555 return ret;
1556}
1557
Jeff Mahoney499f3772015-06-15 09:41:17 -04001558int find_free_dev_extent(struct btrfs_trans_handle *trans,
1559 struct btrfs_device *device, u64 num_bytes,
1560 u64 *start, u64 *len)
1561{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001562 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001563 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001564 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001565}
1566
Christoph Hellwigb2950862008-12-02 09:54:17 -05001567static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001568 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001569 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001570{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001571 struct btrfs_fs_info *fs_info = device->fs_info;
1572 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001573 int ret;
1574 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001575 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001576 struct btrfs_key found_key;
1577 struct extent_buffer *leaf = NULL;
1578 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001579
1580 path = btrfs_alloc_path();
1581 if (!path)
1582 return -ENOMEM;
1583
1584 key.objectid = device->devid;
1585 key.offset = start;
1586 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001587again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001588 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001589 if (ret > 0) {
1590 ret = btrfs_previous_item(root, path, key.objectid,
1591 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001592 if (ret)
1593 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001594 leaf = path->nodes[0];
1595 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1596 extent = btrfs_item_ptr(leaf, path->slots[0],
1597 struct btrfs_dev_extent);
1598 BUG_ON(found_key.offset > start || found_key.offset +
1599 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001600 key = found_key;
1601 btrfs_release_path(path);
1602 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001603 } else if (ret == 0) {
1604 leaf = path->nodes[0];
1605 extent = btrfs_item_ptr(leaf, path->slots[0],
1606 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001607 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001609 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001610 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001611
Miao Xie2196d6e2014-09-03 21:35:41 +08001612 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1613
Chris Mason8f18cf12008-04-25 16:53:30 -04001614 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001615 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001616 btrfs_handle_fs_error(fs_info, ret,
1617 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001618 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001619 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001620 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001621out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001622 btrfs_free_path(path);
1623 return ret;
1624}
1625
Eric Sandeen48a3b632013-04-25 20:41:01 +00001626static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1627 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001628 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001629{
1630 int ret;
1631 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001632 struct btrfs_fs_info *fs_info = device->fs_info;
1633 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001634 struct btrfs_dev_extent *extent;
1635 struct extent_buffer *leaf;
1636 struct btrfs_key key;
1637
Anand Jaine12c9622017-12-04 12:54:53 +08001638 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Stefan Behrens63a212a2012-11-05 18:29:28 +01001639 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001640 path = btrfs_alloc_path();
1641 if (!path)
1642 return -ENOMEM;
1643
Chris Mason0b86a832008-03-24 15:01:56 -04001644 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001645 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001646 key.type = BTRFS_DEV_EXTENT_KEY;
1647 ret = btrfs_insert_empty_item(trans, root, path, &key,
1648 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001649 if (ret)
1650 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001651
1652 leaf = path->nodes[0];
1653 extent = btrfs_item_ptr(leaf, path->slots[0],
1654 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001655 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1656 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001657 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1658 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001659 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1660
Chris Mason0b86a832008-03-24 15:01:56 -04001661 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1662 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001663out:
Chris Mason0b86a832008-03-24 15:01:56 -04001664 btrfs_free_path(path);
1665 return ret;
1666}
1667
Josef Bacik6df9a952013-06-27 13:22:46 -04001668static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001669{
Josef Bacik6df9a952013-06-27 13:22:46 -04001670 struct extent_map_tree *em_tree;
1671 struct extent_map *em;
1672 struct rb_node *n;
1673 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001674
Josef Bacik6df9a952013-06-27 13:22:46 -04001675 em_tree = &fs_info->mapping_tree.map_tree;
1676 read_lock(&em_tree->lock);
1677 n = rb_last(&em_tree->map);
1678 if (n) {
1679 em = rb_entry(n, struct extent_map, rb_node);
1680 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001681 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001682 read_unlock(&em_tree->lock);
1683
Chris Mason0b86a832008-03-24 15:01:56 -04001684 return ret;
1685}
1686
Ilya Dryomov53f10652013-08-12 14:33:01 +03001687static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1688 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001689{
1690 int ret;
1691 struct btrfs_key key;
1692 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001693 struct btrfs_path *path;
1694
Yan Zheng2b820322008-11-17 21:11:30 -05001695 path = btrfs_alloc_path();
1696 if (!path)
1697 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001698
1699 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1700 key.type = BTRFS_DEV_ITEM_KEY;
1701 key.offset = (u64)-1;
1702
Ilya Dryomov53f10652013-08-12 14:33:01 +03001703 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001704 if (ret < 0)
1705 goto error;
1706
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001707 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001708
Ilya Dryomov53f10652013-08-12 14:33:01 +03001709 ret = btrfs_previous_item(fs_info->chunk_root, path,
1710 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001711 BTRFS_DEV_ITEM_KEY);
1712 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001713 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001714 } else {
1715 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1716 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001717 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001718 }
1719 ret = 0;
1720error:
Yan Zheng2b820322008-11-17 21:11:30 -05001721 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001722 return ret;
1723}
1724
1725/*
1726 * the device information is stored in the chunk root
1727 * the btrfs_device struct should be fully filled in
1728 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001729static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001730 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001731 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001732{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001733 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001734 int ret;
1735 struct btrfs_path *path;
1736 struct btrfs_dev_item *dev_item;
1737 struct extent_buffer *leaf;
1738 struct btrfs_key key;
1739 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001740
Chris Mason0b86a832008-03-24 15:01:56 -04001741 path = btrfs_alloc_path();
1742 if (!path)
1743 return -ENOMEM;
1744
Chris Mason0b86a832008-03-24 15:01:56 -04001745 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1746 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001747 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001748
1749 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001750 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001751 if (ret)
1752 goto out;
1753
1754 leaf = path->nodes[0];
1755 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1756
1757 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001758 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001759 btrfs_set_device_type(leaf, dev_item, device->type);
1760 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1761 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1762 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001763 btrfs_set_device_total_bytes(leaf, dev_item,
1764 btrfs_device_get_disk_total_bytes(device));
1765 btrfs_set_device_bytes_used(leaf, dev_item,
1766 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001767 btrfs_set_device_group(leaf, dev_item, 0);
1768 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1769 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001770 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001771
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001772 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001773 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001774 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001775 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001776 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001777
Yan Zheng2b820322008-11-17 21:11:30 -05001778 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001779out:
1780 btrfs_free_path(path);
1781 return ret;
1782}
Chris Mason8f18cf12008-04-25 16:53:30 -04001783
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001784/*
1785 * Function to update ctime/mtime for a given device path.
1786 * Mainly used for ctime/mtime based probe like libblkid.
1787 */
David Sterbada353f62017-02-14 17:55:53 +01001788static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001789{
1790 struct file *filp;
1791
1792 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001793 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001794 return;
1795 file_update_time(filp);
1796 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001797}
1798
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001799static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001800 struct btrfs_device *device)
1801{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001802 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001803 int ret;
1804 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001805 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001806 struct btrfs_trans_handle *trans;
1807
Chris Masona061fc82008-05-07 11:43:44 -04001808 path = btrfs_alloc_path();
1809 if (!path)
1810 return -ENOMEM;
1811
Yan, Zhenga22285a2010-05-16 10:48:46 -04001812 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001813 if (IS_ERR(trans)) {
1814 btrfs_free_path(path);
1815 return PTR_ERR(trans);
1816 }
Chris Masona061fc82008-05-07 11:43:44 -04001817 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1818 key.type = BTRFS_DEV_ITEM_KEY;
1819 key.offset = device->devid;
1820
1821 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001822 if (ret) {
1823 if (ret > 0)
1824 ret = -ENOENT;
1825 btrfs_abort_transaction(trans, ret);
1826 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001827 goto out;
1828 }
1829
1830 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001831 if (ret) {
1832 btrfs_abort_transaction(trans, ret);
1833 btrfs_end_transaction(trans);
1834 }
1835
Chris Masona061fc82008-05-07 11:43:44 -04001836out:
1837 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001838 if (!ret)
1839 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001840 return ret;
1841}
1842
David Sterba3cc31a02016-02-15 16:00:26 +01001843/*
1844 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1845 * filesystem. It's up to the caller to adjust that number regarding eg. device
1846 * replace.
1847 */
1848static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1849 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001850{
Chris Masona061fc82008-05-07 11:43:44 -04001851 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001852 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001853 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001854
Miao Xiede98ced2013-01-29 10:13:12 +00001855 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001856 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001857
Anand Jainbd45ffb2016-02-13 10:01:34 +08001858 all_avail = fs_info->avail_data_alloc_bits |
1859 fs_info->avail_system_alloc_bits |
1860 fs_info->avail_metadata_alloc_bits;
1861 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001862
David Sterba418775a2016-02-15 16:28:14 +01001863 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
1864 if (!(all_avail & btrfs_raid_group[i]))
1865 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001866
David Sterba418775a2016-02-15 16:28:14 +01001867 if (num_devices < btrfs_raid_array[i].devs_min) {
1868 int ret = btrfs_raid_mindev_error[i];
Chris Masona061fc82008-05-07 11:43:44 -04001869
David Sterba418775a2016-02-15 16:28:14 +01001870 if (ret)
1871 return ret;
1872 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001873 }
1874
1875 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001876}
1877
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001878static struct btrfs_device * btrfs_find_next_active_device(
1879 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001880{
1881 struct btrfs_device *next_device;
1882
1883 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1884 if (next_device != device &&
1885 !next_device->missing && next_device->bdev)
1886 return next_device;
1887 }
1888
1889 return NULL;
1890}
1891
1892/*
1893 * Helper function to check if the given device is part of s_bdev / latest_bdev
1894 * and replace it with the provided or the next active device, in the context
1895 * where this function called, there should be always be another device (or
1896 * this_dev) which is active.
1897 */
1898void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1899 struct btrfs_device *device, struct btrfs_device *this_dev)
1900{
1901 struct btrfs_device *next_device;
1902
1903 if (this_dev)
1904 next_device = this_dev;
1905 else
1906 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1907 device);
1908 ASSERT(next_device);
1909
1910 if (fs_info->sb->s_bdev &&
1911 (fs_info->sb->s_bdev == device->bdev))
1912 fs_info->sb->s_bdev = next_device->bdev;
1913
1914 if (fs_info->fs_devices->latest_bdev == device->bdev)
1915 fs_info->fs_devices->latest_bdev = next_device->bdev;
1916}
1917
David Sterbada353f62017-02-14 17:55:53 +01001918int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1919 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001920{
1921 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001922 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001923 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001924 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001925
Anand Jain2c997382017-11-06 10:28:00 +08001926 mutex_lock(&fs_info->volume_mutex);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001927 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001928
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001929 num_devices = fs_info->fs_devices->num_devices;
1930 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1931 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001932 WARN_ON(num_devices < 1);
1933 num_devices--;
1934 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001935 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Chris Masona061fc82008-05-07 11:43:44 -04001936
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001937 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001938 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001939 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001940
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001941 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1942 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001943 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001944 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001945
Stefan Behrens63a212a2012-11-05 18:29:28 +01001946 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001947 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001948 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001949 }
1950
Anand Jainebbede42017-12-04 12:54:52 +08001951 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1952 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001953 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001954 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001955 }
1956
Anand Jainebbede42017-12-04 12:54:52 +08001957 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001958 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001959 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001960 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001961 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001962 }
Chris Masona061fc82008-05-07 11:43:44 -04001963
Carey Underwoodd7901552013-03-04 16:37:06 -06001964 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001965 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001966 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001967 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001968 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001969
Stefan Behrens63a212a2012-11-05 18:29:28 +01001970 /*
1971 * TODO: the superblock still includes this device in its num_devices
1972 * counter although write_all_supers() is not locked out. This
1973 * could give a filesystem state which requires a degraded mount.
1974 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001975 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04001976 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001977 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001978
Anand Jaine12c9622017-12-04 12:54:53 +08001979 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001980 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001981
1982 /*
1983 * the device list mutex makes sure that we don't change
1984 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001985 * the device supers. Whoever is writing all supers, should
1986 * lock the device list mutex before getting the number of
1987 * devices in the super block (super_copy). Conversely,
1988 * whoever updates the number of devices in the super block
1989 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001990 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001991
1992 cur_devices = device->fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001993 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001994 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001995
Yan Zhenge4404d62008-12-12 10:03:26 -05001996 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001997 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001998
Chris Masoncd02dca2010-12-13 14:56:23 -05001999 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08002000 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002001
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002003
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002004 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002005 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002006 /* remove sysfs entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002007 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002008 }
Anand Jain99994cd2014-06-03 11:36:00 +08002009
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002010 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2011 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
2012 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002013
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002014 /*
2015 * at this point, the device is zero sized and detached from
2016 * the devices list. All that's left is to zero out the old
2017 * supers and free the device.
2018 */
Anand Jainebbede42017-12-04 12:54:52 +08002019 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002020 btrfs_scratch_superblocks(device->bdev, device->name->str);
2021
2022 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002023 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002024
Xiao Guangrong1f781602011-04-20 10:09:16 +00002025 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002026 struct btrfs_fs_devices *fs_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002027 fs_devices = fs_info->fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002028 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002029 if (fs_devices->seed == cur_devices) {
2030 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002031 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002032 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002033 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002034 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002035 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00002036 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002037 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002038 }
2039
Chris Masona061fc82008-05-07 11:43:44 -04002040out:
2041 mutex_unlock(&uuid_mutex);
Anand Jain2c997382017-11-06 10:28:00 +08002042 mutex_unlock(&fs_info->volume_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002043 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002044
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002045error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002046 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002047 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002048 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002049 &fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002050 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002051 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002052 }
Anand Jain24fc5722016-02-13 10:01:36 +08002053 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002054}
2055
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002056void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2057 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002058{
Anand Jaind51908c2014-08-13 14:24:19 +08002059 struct btrfs_fs_devices *fs_devices;
2060
Stefan Behrense93c89c2012-11-05 17:33:06 +01002061 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03002062
Anand Jain25e8e912014-08-20 10:56:56 +08002063 /*
2064 * in case of fs with no seed, srcdev->fs_devices will point
2065 * to fs_devices of fs_info. However when the dev being replaced is
2066 * a seed dev it will point to the seed's local fs_devices. In short
2067 * srcdev will have its correct fs_devices in both the cases.
2068 */
2069 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002070
Stefan Behrense93c89c2012-11-05 17:33:06 +01002071 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002072 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002073 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08002074 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08002075 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002076
Anand Jainebbede42017-12-04 12:54:52 +08002077 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002078 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002079
Miao Xie82372bc2014-09-03 21:35:44 +08002080 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002081 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002082}
2083
2084void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2085 struct btrfs_device *srcdev)
2086{
2087 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002088
Anand Jainebbede42017-12-04 12:54:52 +08002089 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002090 /* zero out the old super if it is writable */
2091 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2092 }
Anand Jain14238812016-07-22 06:04:53 +08002093
2094 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002095 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002096
Anand Jain94d5f0c2014-08-13 14:24:22 +08002097 /* if this is no devs we rather delete the fs_devices */
2098 if (!fs_devices->num_devices) {
2099 struct btrfs_fs_devices *tmp_fs_devices;
2100
Anand Jain6dd38f82017-10-17 06:53:50 +08002101 /*
2102 * On a mounted FS, num_devices can't be zero unless it's a
2103 * seed. In case of a seed device being replaced, the replace
2104 * target added to the sprout FS, so there will be no more
2105 * device left under the seed FS.
2106 */
2107 ASSERT(fs_devices->seeding);
2108
Anand Jain94d5f0c2014-08-13 14:24:22 +08002109 tmp_fs_devices = fs_info->fs_devices;
2110 while (tmp_fs_devices) {
2111 if (tmp_fs_devices->seed == fs_devices) {
2112 tmp_fs_devices->seed = fs_devices->seed;
2113 break;
2114 }
2115 tmp_fs_devices = tmp_fs_devices->seed;
2116 }
2117 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002118 __btrfs_close_devices(fs_devices);
2119 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002120 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002121}
2122
2123void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2124 struct btrfs_device *tgtdev)
2125{
Miao Xie67a2c452014-09-03 21:35:43 +08002126 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002127 WARN_ON(!tgtdev);
2128 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002129
Anand Jain32576042015-08-14 18:32:49 +08002130 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002131
Anand Jain779bf3fe2016-04-18 16:51:23 +08002132 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002133 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002134
Stefan Behrense93c89c2012-11-05 17:33:06 +01002135 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002136
Anand Jain88acff62016-05-03 17:44:43 +08002137 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002138
Stefan Behrense93c89c2012-11-05 17:33:06 +01002139 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002140
2141 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002142 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002143
2144 /*
2145 * The update_dev_time() with in btrfs_scratch_superblocks()
2146 * may lead to a call to btrfs_show_devname() which will try
2147 * to hold device_list_mutex. And here this device
2148 * is already out of device list, so we don't have to hold
2149 * the device_list_mutex lock.
2150 */
2151 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002152
2153 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002154 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002155}
2156
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002157static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002158 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002159 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002160{
2161 int ret = 0;
2162 struct btrfs_super_block *disk_super;
2163 u64 devid;
2164 u8 *dev_uuid;
2165 struct block_device *bdev;
2166 struct buffer_head *bh;
2167
2168 *device = NULL;
2169 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002170 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002171 if (ret)
2172 return ret;
2173 disk_super = (struct btrfs_super_block *)bh->b_data;
2174 devid = btrfs_stack_device_id(&disk_super->dev_item);
2175 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002177 brelse(bh);
2178 if (!*device)
2179 ret = -ENOENT;
2180 blkdev_put(bdev, FMODE_READ);
2181 return ret;
2182}
2183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002184int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002185 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002186 struct btrfs_device **device)
2187{
2188 *device = NULL;
2189 if (strcmp(device_path, "missing") == 0) {
2190 struct list_head *devices;
2191 struct btrfs_device *tmp;
2192
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002193 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002194 /*
2195 * It is safe to read the devices since the volume_mutex
2196 * is held by the caller.
2197 */
2198 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002199 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2200 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002201 *device = tmp;
2202 break;
2203 }
2204 }
2205
Anand Jaind74a6252015-08-14 18:32:56 +08002206 if (!*device)
2207 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002208
2209 return 0;
2210 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002211 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002212 }
2213}
2214
Yan Zheng2b820322008-11-17 21:11:30 -05002215/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002216 * Lookup a device given by device id, or the path if the id is 0.
2217 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002218int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002219 const char *devpath,
2220 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002221{
2222 int ret;
2223
David Sterba5c5c0df2016-02-15 16:39:55 +01002224 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002225 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002226 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002227 if (!*device)
2228 ret = -ENOENT;
2229 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002230 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002231 return -EINVAL;
2232
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002233 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002234 device);
2235 }
2236 return ret;
2237}
2238
Yan Zheng2b820322008-11-17 21:11:30 -05002239/*
2240 * does all the dirty work required for changing file system's UUID.
2241 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002242static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002243{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002244 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002245 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002246 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002247 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002248 struct btrfs_device *device;
2249 u64 super_flags;
2250
2251 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002252 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002253 return -EINVAL;
2254
David Sterba2dfeca92017-06-14 02:48:07 +02002255 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002256 if (IS_ERR(seed_devices))
2257 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002258
Yan Zhenge4404d62008-12-12 10:03:26 -05002259 old_devices = clone_fs_devices(fs_devices);
2260 if (IS_ERR(old_devices)) {
2261 kfree(seed_devices);
2262 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002263 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002264
Yan Zheng2b820322008-11-17 21:11:30 -05002265 list_add(&old_devices->list, &fs_uuids);
2266
Yan Zhenge4404d62008-12-12 10:03:26 -05002267 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2268 seed_devices->opened = 1;
2269 INIT_LIST_HEAD(&seed_devices->devices);
2270 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002271 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002272
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002273 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002274 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2275 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002276 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002277 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002278
David Sterba34441362016-10-04 19:34:27 +02002279 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002280 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002281 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002282
Yan Zheng2b820322008-11-17 21:11:30 -05002283 fs_devices->seeding = 0;
2284 fs_devices->num_devices = 0;
2285 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002286 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002287 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002288 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002289
2290 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002291 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002292 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002293 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002294
Yan Zheng2b820322008-11-17 21:11:30 -05002295 super_flags = btrfs_super_flags(disk_super) &
2296 ~BTRFS_SUPER_FLAG_SEEDING;
2297 btrfs_set_super_flags(disk_super, super_flags);
2298
2299 return 0;
2300}
2301
2302/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002303 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002304 */
2305static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002306 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002307{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002308 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002309 struct btrfs_path *path;
2310 struct extent_buffer *leaf;
2311 struct btrfs_dev_item *dev_item;
2312 struct btrfs_device *device;
2313 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002314 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002315 u8 dev_uuid[BTRFS_UUID_SIZE];
2316 u64 devid;
2317 int ret;
2318
2319 path = btrfs_alloc_path();
2320 if (!path)
2321 return -ENOMEM;
2322
Yan Zheng2b820322008-11-17 21:11:30 -05002323 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2324 key.offset = 0;
2325 key.type = BTRFS_DEV_ITEM_KEY;
2326
2327 while (1) {
2328 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2329 if (ret < 0)
2330 goto error;
2331
2332 leaf = path->nodes[0];
2333next_slot:
2334 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2335 ret = btrfs_next_leaf(root, path);
2336 if (ret > 0)
2337 break;
2338 if (ret < 0)
2339 goto error;
2340 leaf = path->nodes[0];
2341 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002342 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002343 continue;
2344 }
2345
2346 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2347 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2348 key.type != BTRFS_DEV_ITEM_KEY)
2349 break;
2350
2351 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2352 struct btrfs_dev_item);
2353 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002354 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002355 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002356 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002357 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002358 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002359 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002360
2361 if (device->fs_devices->seeding) {
2362 btrfs_set_device_generation(leaf, dev_item,
2363 device->generation);
2364 btrfs_mark_buffer_dirty(leaf);
2365 }
2366
2367 path->slots[0]++;
2368 goto next_slot;
2369 }
2370 ret = 0;
2371error:
2372 btrfs_free_path(path);
2373 return ret;
2374}
2375
David Sterbada353f62017-02-14 17:55:53 +01002376int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002377{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002378 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002379 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002380 struct btrfs_trans_handle *trans;
2381 struct btrfs_device *device;
2382 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002383 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002384 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002385 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002386 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002387 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002388 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002389 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002390
David Howellsbc98a422017-07-17 08:45:34 +01002391 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002392 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002393
Li Zefana5d163332011-12-07 20:08:40 -05002394 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002395 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002396 if (IS_ERR(bdev))
2397 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002398
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002399 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002400 seeding_dev = 1;
2401 down_write(&sb->s_umount);
2402 mutex_lock(&uuid_mutex);
2403 }
2404
Chris Mason8c8bee12008-09-29 11:19:10 -04002405 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002406
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002407 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002408
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002410 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002411 if (device->bdev == bdev) {
2412 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002413 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002414 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002415 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002416 }
2417 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002418 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002419
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002420 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002421 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002422 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002423 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002424 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002425 }
2426
David Sterba78f2c9e2016-02-11 14:25:38 +01002427 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002428 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002429 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002430 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002431 }
Josef Bacik606686e2012-06-04 14:03:51 -04002432 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002433
Yan, Zhenga22285a2010-05-16 10:48:46 -04002434 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002435 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002436 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002437 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002438 }
2439
Josef Bacikd5e20032011-08-04 14:52:27 +00002440 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002441 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002442 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002443 device->io_width = fs_info->sectorsize;
2444 device->io_align = fs_info->sectorsize;
2445 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002446 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2447 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002448 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002449 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002450 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002451 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002452 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Stefan Behrens63a212a2012-11-05 18:29:28 +01002453 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002454 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002455 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002456 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002457
Yan Zheng2b820322008-11-17 21:11:30 -05002458 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002459 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002460 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002461 if (ret) {
2462 btrfs_abort_transaction(trans, ret);
2463 goto error_trans;
2464 }
Yan Zheng2b820322008-11-17 21:11:30 -05002465 }
2466
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002467 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002468
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002469 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002470 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002471 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002472 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002473 &fs_info->fs_devices->alloc_list);
2474 fs_info->fs_devices->num_devices++;
2475 fs_info->fs_devices->open_devices++;
2476 fs_info->fs_devices->rw_devices++;
2477 fs_info->fs_devices->total_devices++;
2478 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002479
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002480 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002481
Anand Jaine884f4f2017-04-04 18:40:19 +08002482 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002483 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002484
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002485 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2486 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002487 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002488
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002489 tmp = btrfs_super_num_devices(fs_info->super_copy);
2490 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002491
2492 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002494
Miao Xie2196d6e2014-09-03 21:35:41 +08002495 /*
2496 * we've got more storage, clear any full flags on the space
2497 * infos
2498 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002500
David Sterba34441362016-10-04 19:34:27 +02002501 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002503
Yan Zheng2b820322008-11-17 21:11:30 -05002504 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002505 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002506 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002507 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002508 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002509 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002510 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002511 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002512 }
2513
Anand Jainc74a0b02017-11-06 16:36:15 +08002514 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002515 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002516 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002517 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002518 }
2519
2520 if (seeding_dev) {
2521 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2522
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002523 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002524 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002525 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002526 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002527 }
Anand Jainb2373f22014-06-03 11:36:03 +08002528
2529 /* Sprouting would change fsid of the mounted root,
2530 * so rename the fsid on the sysfs
2531 */
2532 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002533 fs_info->fsid);
2534 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2535 btrfs_warn(fs_info,
2536 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002537 }
2538
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002539 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002540
2541 if (seeding_dev) {
2542 mutex_unlock(&uuid_mutex);
2543 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002544 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002545
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002546 if (ret) /* transaction commit */
2547 return ret;
2548
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002549 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002550 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002551 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002552 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002553 trans = btrfs_attach_transaction(root);
2554 if (IS_ERR(trans)) {
2555 if (PTR_ERR(trans) == -ENOENT)
2556 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002557 ret = PTR_ERR(trans);
2558 trans = NULL;
2559 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002560 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002561 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002562 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002563
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002564 /* Update ctime/mtime for libblkid */
2565 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002566 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002567
Anand Jaind31c32f2017-09-28 14:51:10 +08002568error_sysfs:
2569 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002570error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002571 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002572 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002573 if (trans)
2574 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002575error_free_device:
David Sterba55de4802017-10-30 18:55:47 +01002576 free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002577error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002578 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002579 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002580 mutex_unlock(&uuid_mutex);
2581 up_write(&sb->s_umount);
2582 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002583 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002584}
2585
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002586int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002587 const char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002588 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002589 struct btrfs_device **device_out)
2590{
Stefan Behrense93c89c2012-11-05 17:33:06 +01002591 struct btrfs_device *device;
2592 struct block_device *bdev;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002593 struct list_head *devices;
2594 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002595 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002596 int ret = 0;
2597
2598 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002599 if (fs_info->fs_devices->seeding) {
2600 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002601 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002602 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002603
2604 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2605 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002606 if (IS_ERR(bdev)) {
2607 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002608 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002609 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002610
2611 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2612
2613 devices = &fs_info->fs_devices->devices;
2614 list_for_each_entry(device, devices, dev_list) {
2615 if (device->bdev == bdev) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002616 btrfs_err(fs_info,
2617 "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002618 ret = -EEXIST;
2619 goto error;
2620 }
2621 }
2622
Miao Xie1c433662014-09-03 21:35:32 +08002623
Miao Xie7cc8e582014-09-03 21:35:38 +08002624 if (i_size_read(bdev->bd_inode) <
2625 btrfs_device_get_total_bytes(srcdev)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002626 btrfs_err(fs_info,
2627 "target device is smaller than source device!");
Miao Xie1c433662014-09-03 21:35:32 +08002628 ret = -EINVAL;
2629 goto error;
2630 }
2631
2632
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002633 device = btrfs_alloc_device(NULL, &devid, NULL);
2634 if (IS_ERR(device)) {
2635 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002636 goto error;
2637 }
2638
David Sterba61655722017-06-15 17:16:43 +02002639 name = rcu_string_strdup(device_path, GFP_KERNEL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002640 if (!name) {
David Sterba55de4802017-10-30 18:55:47 +01002641 free_device(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002642 ret = -ENOMEM;
2643 goto error;
2644 }
2645 rcu_assign_pointer(device->name, name);
2646
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002647 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jainebbede42017-12-04 12:54:52 +08002648 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002649 device->generation = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002650 device->io_width = fs_info->sectorsize;
2651 device->io_align = fs_info->sectorsize;
2652 device->sector_size = fs_info->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002653 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2654 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2655 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002656 ASSERT(list_empty(&srcdev->resized_list));
2657 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002658 device->commit_bytes_used = device->bytes_used;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002659 device->fs_info = fs_info;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002660 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002661 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002662 device->is_tgtdev_for_dev_replace = 1;
2663 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002664 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002665 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002666 device->fs_devices = fs_info->fs_devices;
2667 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2668 fs_info->fs_devices->num_devices++;
2669 fs_info->fs_devices->open_devices++;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002670 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002671
2672 *device_out = device;
2673 return ret;
2674
2675error:
2676 blkdev_put(bdev, FMODE_EXCL);
2677 return ret;
2678}
2679
2680void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2681 struct btrfs_device *tgtdev)
2682{
Jeff Mahoneyda170662016-06-15 09:22:56 -04002683 u32 sectorsize = fs_info->sectorsize;
2684
Stefan Behrense93c89c2012-11-05 17:33:06 +01002685 WARN_ON(fs_info->fs_devices->rw_devices == 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04002686 tgtdev->io_width = sectorsize;
2687 tgtdev->io_align = sectorsize;
2688 tgtdev->sector_size = sectorsize;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002689 tgtdev->fs_info = fs_info;
Anand Jaine12c9622017-12-04 12:54:53 +08002690 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &tgtdev->dev_state);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002691}
2692
Chris Masond3977122009-01-05 21:25:51 -05002693static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2694 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002695{
2696 int ret;
2697 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002698 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002699 struct btrfs_dev_item *dev_item;
2700 struct extent_buffer *leaf;
2701 struct btrfs_key key;
2702
Chris Mason0b86a832008-03-24 15:01:56 -04002703 path = btrfs_alloc_path();
2704 if (!path)
2705 return -ENOMEM;
2706
2707 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2708 key.type = BTRFS_DEV_ITEM_KEY;
2709 key.offset = device->devid;
2710
2711 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2712 if (ret < 0)
2713 goto out;
2714
2715 if (ret > 0) {
2716 ret = -ENOENT;
2717 goto out;
2718 }
2719
2720 leaf = path->nodes[0];
2721 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2722
2723 btrfs_set_device_id(leaf, dev_item, device->devid);
2724 btrfs_set_device_type(leaf, dev_item, device->type);
2725 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2726 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2727 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002728 btrfs_set_device_total_bytes(leaf, dev_item,
2729 btrfs_device_get_disk_total_bytes(device));
2730 btrfs_set_device_bytes_used(leaf, dev_item,
2731 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002732 btrfs_mark_buffer_dirty(leaf);
2733
2734out:
2735 btrfs_free_path(path);
2736 return ret;
2737}
2738
Miao Xie2196d6e2014-09-03 21:35:41 +08002739int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002740 struct btrfs_device *device, u64 new_size)
2741{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002742 struct btrfs_fs_info *fs_info = device->fs_info;
2743 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002744 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002745 u64 old_total;
2746 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002747
Anand Jainebbede42017-12-04 12:54:52 +08002748 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002749 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002750
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002751 new_size = round_down(new_size, fs_info->sectorsize);
2752
David Sterba34441362016-10-04 19:34:27 +02002753 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002754 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002755 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002756
Stefan Behrens63a212a2012-11-05 18:29:28 +01002757 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002758 device->is_tgtdev_for_dev_replace) {
David Sterba34441362016-10-04 19:34:27 +02002759 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002760 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002761 }
Yan Zheng2b820322008-11-17 21:11:30 -05002762
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002763 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002764
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002765 btrfs_set_super_total_bytes(super_copy,
2766 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002767 device->fs_devices->total_rw_bytes += diff;
2768
Miao Xie7cc8e582014-09-03 21:35:38 +08002769 btrfs_device_set_total_bytes(device, new_size);
2770 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002771 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002772 if (list_empty(&device->resized_list))
2773 list_add_tail(&device->resized_list,
2774 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002775 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002776
Chris Mason8f18cf12008-04-25 16:53:30 -04002777 return btrfs_update_device(trans, device);
2778}
2779
2780static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002781 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002782{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002783 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002784 int ret;
2785 struct btrfs_path *path;
2786 struct btrfs_key key;
2787
Chris Mason8f18cf12008-04-25 16:53:30 -04002788 path = btrfs_alloc_path();
2789 if (!path)
2790 return -ENOMEM;
2791
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002792 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002793 key.offset = chunk_offset;
2794 key.type = BTRFS_CHUNK_ITEM_KEY;
2795
2796 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002797 if (ret < 0)
2798 goto out;
2799 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002800 btrfs_handle_fs_error(fs_info, -ENOENT,
2801 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002802 ret = -ENOENT;
2803 goto out;
2804 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002805
2806 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002807 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 btrfs_handle_fs_error(fs_info, ret,
2809 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002810out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002811 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002812 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002813}
2814
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002815static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002816{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002817 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002818 struct btrfs_disk_key *disk_key;
2819 struct btrfs_chunk *chunk;
2820 u8 *ptr;
2821 int ret = 0;
2822 u32 num_stripes;
2823 u32 array_size;
2824 u32 len = 0;
2825 u32 cur;
2826 struct btrfs_key key;
2827
David Sterba34441362016-10-04 19:34:27 +02002828 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002829 array_size = btrfs_super_sys_array_size(super_copy);
2830
2831 ptr = super_copy->sys_chunk_array;
2832 cur = 0;
2833
2834 while (cur < array_size) {
2835 disk_key = (struct btrfs_disk_key *)ptr;
2836 btrfs_disk_key_to_cpu(&key, disk_key);
2837
2838 len = sizeof(*disk_key);
2839
2840 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2841 chunk = (struct btrfs_chunk *)(ptr + len);
2842 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2843 len += btrfs_chunk_item_size(num_stripes);
2844 } else {
2845 ret = -EIO;
2846 break;
2847 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002848 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002849 key.offset == chunk_offset) {
2850 memmove(ptr, ptr + len, array_size - (cur + len));
2851 array_size -= len;
2852 btrfs_set_super_sys_array_size(super_copy, array_size);
2853 } else {
2854 ptr += len;
2855 cur += len;
2856 }
2857 }
David Sterba34441362016-10-04 19:34:27 +02002858 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002859 return ret;
2860}
2861
Liu Bo592d92e2017-03-14 13:33:55 -07002862static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2863 u64 logical, u64 length)
2864{
2865 struct extent_map_tree *em_tree;
2866 struct extent_map *em;
2867
2868 em_tree = &fs_info->mapping_tree.map_tree;
2869 read_lock(&em_tree->lock);
2870 em = lookup_extent_mapping(em_tree, logical, length);
2871 read_unlock(&em_tree->lock);
2872
2873 if (!em) {
2874 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2875 logical, length);
2876 return ERR_PTR(-EINVAL);
2877 }
2878
2879 if (em->start > logical || em->start + em->len < logical) {
2880 btrfs_crit(fs_info,
2881 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2882 logical, length, em->start, em->start + em->len);
2883 free_extent_map(em);
2884 return ERR_PTR(-EINVAL);
2885 }
2886
2887 /* callers are responsible for dropping em's ref. */
2888 return em;
2889}
2890
Josef Bacik47ab2a62014-09-18 11:20:02 -04002891int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002892 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002893{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002894 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002895 struct map_lookup *map;
2896 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002897 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002898 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002899
Liu Bo592d92e2017-03-14 13:33:55 -07002900 em = get_chunk_map(fs_info, chunk_offset, 1);
2901 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002902 /*
2903 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002904 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002905 * do anything we still error out.
2906 */
2907 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002908 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002909 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002910 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002911 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002912 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002913 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002914
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002915 /*
2916 * Take the device list mutex to prevent races with the final phase of
2917 * a device replace operation that replaces the device object associated
2918 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2919 */
2920 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002921 for (i = 0; i < map->num_stripes; i++) {
2922 struct btrfs_device *device = map->stripes[i].dev;
2923 ret = btrfs_free_dev_extent(trans, device,
2924 map->stripes[i].physical,
2925 &dev_extent_len);
2926 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002927 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002928 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002929 goto out;
2930 }
2931
2932 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002933 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002934 btrfs_device_set_bytes_used(device,
2935 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002936 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002937 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002938 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002939 }
2940
2941 if (map->stripes[i].dev) {
2942 ret = btrfs_update_device(trans, map->stripes[i].dev);
2943 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002944 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002945 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002946 goto out;
2947 }
2948 }
2949 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002950 mutex_unlock(&fs_devices->device_list_mutex);
2951
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002952 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002953 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002954 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002955 goto out;
2956 }
2957
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002958 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002959
2960 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002961 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002962 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002963 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002964 goto out;
2965 }
2966 }
2967
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002968 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002969 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002970 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002971 goto out;
2972 }
2973
Josef Bacik47ab2a62014-09-18 11:20:02 -04002974out:
2975 /* once for us */
2976 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002977 return ret;
2978}
2979
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002980static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002981{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002982 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002983 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002984 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002985
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002986 /*
2987 * Prevent races with automatic removal of unused block groups.
2988 * After we relocate and before we remove the chunk with offset
2989 * chunk_offset, automatic removal of the block group can kick in,
2990 * resulting in a failure when calling btrfs_remove_chunk() below.
2991 *
2992 * Make sure to acquire this mutex before doing a tree search (dev
2993 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2994 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2995 * we release the path used to search the chunk/dev tree and before
2996 * the current task acquires this mutex and calls us.
2997 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002998 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002999
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003000 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003001 if (ret)
3002 return -ENOSPC;
3003
Chris Mason8f18cf12008-04-25 16:53:30 -04003004 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003005 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003006 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003007 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003008 if (ret)
3009 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003010
Chris Mason19c4d2f2016-10-10 13:43:31 -07003011 trans = btrfs_start_trans_remove_block_group(root->fs_info,
3012 chunk_offset);
3013 if (IS_ERR(trans)) {
3014 ret = PTR_ERR(trans);
3015 btrfs_handle_fs_error(root->fs_info, ret, NULL);
3016 return ret;
3017 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09003018
Chris Mason19c4d2f2016-10-10 13:43:31 -07003019 /*
3020 * step two, delete the device extents and the
3021 * chunk tree entries
3022 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003023 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003024 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07003025 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04003026}
3027
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003028static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05003029{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003030 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05003031 struct btrfs_path *path;
3032 struct extent_buffer *leaf;
3033 struct btrfs_chunk *chunk;
3034 struct btrfs_key key;
3035 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05003036 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04003037 bool retried = false;
3038 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05003039 int ret;
3040
3041 path = btrfs_alloc_path();
3042 if (!path)
3043 return -ENOMEM;
3044
Josef Bacikba1bf482009-09-11 16:11:19 -04003045again:
Yan Zheng2b820322008-11-17 21:11:30 -05003046 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3047 key.offset = (u64)-1;
3048 key.type = BTRFS_CHUNK_ITEM_KEY;
3049
3050 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003051 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003052 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003053 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003055 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003056 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003057 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05003058
3059 ret = btrfs_previous_item(chunk_root, path, key.objectid,
3060 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003061 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003062 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003063 if (ret < 0)
3064 goto error;
3065 if (ret > 0)
3066 break;
3067
3068 leaf = path->nodes[0];
3069 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3070
3071 chunk = btrfs_item_ptr(leaf, path->slots[0],
3072 struct btrfs_chunk);
3073 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003074 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003075
3076 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003077 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003078 if (ret == -ENOSPC)
3079 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303080 else
3081 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003082 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003083 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003084
3085 if (found_key.offset == 0)
3086 break;
3087 key.offset = found_key.offset - 1;
3088 }
3089 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003090 if (failed && !retried) {
3091 failed = 0;
3092 retried = true;
3093 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303094 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003095 ret = -ENOSPC;
3096 }
Yan Zheng2b820322008-11-17 21:11:30 -05003097error:
3098 btrfs_free_path(path);
3099 return ret;
3100}
3101
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003102static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003103 struct btrfs_balance_control *bctl)
3104{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003105 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003106 struct btrfs_trans_handle *trans;
3107 struct btrfs_balance_item *item;
3108 struct btrfs_disk_balance_args disk_bargs;
3109 struct btrfs_path *path;
3110 struct extent_buffer *leaf;
3111 struct btrfs_key key;
3112 int ret, err;
3113
3114 path = btrfs_alloc_path();
3115 if (!path)
3116 return -ENOMEM;
3117
3118 trans = btrfs_start_transaction(root, 0);
3119 if (IS_ERR(trans)) {
3120 btrfs_free_path(path);
3121 return PTR_ERR(trans);
3122 }
3123
3124 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003125 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003126 key.offset = 0;
3127
3128 ret = btrfs_insert_empty_item(trans, root, path, &key,
3129 sizeof(*item));
3130 if (ret)
3131 goto out;
3132
3133 leaf = path->nodes[0];
3134 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3135
David Sterbab159fa22016-11-08 18:09:03 +01003136 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003137
3138 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3139 btrfs_set_balance_data(leaf, item, &disk_bargs);
3140 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3141 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3142 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3143 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3144
3145 btrfs_set_balance_flags(leaf, item, bctl->flags);
3146
3147 btrfs_mark_buffer_dirty(leaf);
3148out:
3149 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003150 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003151 if (err && !ret)
3152 ret = err;
3153 return ret;
3154}
3155
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003156static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003157{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003158 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003159 struct btrfs_trans_handle *trans;
3160 struct btrfs_path *path;
3161 struct btrfs_key key;
3162 int ret, err;
3163
3164 path = btrfs_alloc_path();
3165 if (!path)
3166 return -ENOMEM;
3167
3168 trans = btrfs_start_transaction(root, 0);
3169 if (IS_ERR(trans)) {
3170 btrfs_free_path(path);
3171 return PTR_ERR(trans);
3172 }
3173
3174 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003175 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003176 key.offset = 0;
3177
3178 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3179 if (ret < 0)
3180 goto out;
3181 if (ret > 0) {
3182 ret = -ENOENT;
3183 goto out;
3184 }
3185
3186 ret = btrfs_del_item(trans, root, path);
3187out:
3188 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003189 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003190 if (err && !ret)
3191 ret = err;
3192 return ret;
3193}
3194
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003195/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003196 * This is a heuristic used to reduce the number of chunks balanced on
3197 * resume after balance was interrupted.
3198 */
3199static void update_balance_args(struct btrfs_balance_control *bctl)
3200{
3201 /*
3202 * Turn on soft mode for chunk types that were being converted.
3203 */
3204 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3205 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3206 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3207 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3208 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3209 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3210
3211 /*
3212 * Turn on usage filter if is not already used. The idea is
3213 * that chunks that we have already balanced should be
3214 * reasonably full. Don't do it for chunks that are being
3215 * converted - that will keep us from relocating unconverted
3216 * (albeit full) chunks.
3217 */
3218 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003219 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003220 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3221 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3222 bctl->data.usage = 90;
3223 }
3224 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003225 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003226 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3227 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3228 bctl->sys.usage = 90;
3229 }
3230 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003231 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003232 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3233 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3234 bctl->meta.usage = 90;
3235 }
3236}
3237
3238/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003239 * Should be called with both balance and volume mutexes held to
3240 * serialize other volume operations (add_dev/rm_dev/resize) with
3241 * restriper. Same goes for unset_balance_control.
3242 */
3243static void set_balance_control(struct btrfs_balance_control *bctl)
3244{
3245 struct btrfs_fs_info *fs_info = bctl->fs_info;
3246
3247 BUG_ON(fs_info->balance_ctl);
3248
3249 spin_lock(&fs_info->balance_lock);
3250 fs_info->balance_ctl = bctl;
3251 spin_unlock(&fs_info->balance_lock);
3252}
3253
3254static void unset_balance_control(struct btrfs_fs_info *fs_info)
3255{
3256 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3257
3258 BUG_ON(!fs_info->balance_ctl);
3259
3260 spin_lock(&fs_info->balance_lock);
3261 fs_info->balance_ctl = NULL;
3262 spin_unlock(&fs_info->balance_lock);
3263
3264 kfree(bctl);
3265}
3266
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003267/*
3268 * Balance filters. Return 1 if chunk should be filtered out
3269 * (should not be balanced).
3270 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003271static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003272 struct btrfs_balance_args *bargs)
3273{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003274 chunk_type = chunk_to_extended(chunk_type) &
3275 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003276
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003277 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003278 return 0;
3279
3280 return 1;
3281}
3282
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003283static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003284 struct btrfs_balance_args *bargs)
3285{
3286 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003287 u64 chunk_used;
3288 u64 user_thresh_min;
3289 u64 user_thresh_max;
3290 int ret = 1;
3291
3292 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3293 chunk_used = btrfs_block_group_used(&cache->item);
3294
3295 if (bargs->usage_min == 0)
3296 user_thresh_min = 0;
3297 else
3298 user_thresh_min = div_factor_fine(cache->key.offset,
3299 bargs->usage_min);
3300
3301 if (bargs->usage_max == 0)
3302 user_thresh_max = 1;
3303 else if (bargs->usage_max > 100)
3304 user_thresh_max = cache->key.offset;
3305 else
3306 user_thresh_max = div_factor_fine(cache->key.offset,
3307 bargs->usage_max);
3308
3309 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3310 ret = 0;
3311
3312 btrfs_put_block_group(cache);
3313 return ret;
3314}
3315
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003316static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003317 u64 chunk_offset, struct btrfs_balance_args *bargs)
3318{
3319 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003320 u64 chunk_used, user_thresh;
3321 int ret = 1;
3322
3323 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3324 chunk_used = btrfs_block_group_used(&cache->item);
3325
David Sterbabc309462015-10-20 18:22:13 +02003326 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003327 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003328 else if (bargs->usage > 100)
3329 user_thresh = cache->key.offset;
3330 else
3331 user_thresh = div_factor_fine(cache->key.offset,
3332 bargs->usage);
3333
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003334 if (chunk_used < user_thresh)
3335 ret = 0;
3336
3337 btrfs_put_block_group(cache);
3338 return ret;
3339}
3340
Ilya Dryomov409d4042012-01-16 22:04:47 +02003341static int chunk_devid_filter(struct extent_buffer *leaf,
3342 struct btrfs_chunk *chunk,
3343 struct btrfs_balance_args *bargs)
3344{
3345 struct btrfs_stripe *stripe;
3346 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3347 int i;
3348
3349 for (i = 0; i < num_stripes; i++) {
3350 stripe = btrfs_stripe_nr(chunk, i);
3351 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3352 return 0;
3353 }
3354
3355 return 1;
3356}
3357
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003358/* [pstart, pend) */
3359static int chunk_drange_filter(struct extent_buffer *leaf,
3360 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003361 struct btrfs_balance_args *bargs)
3362{
3363 struct btrfs_stripe *stripe;
3364 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3365 u64 stripe_offset;
3366 u64 stripe_length;
3367 int factor;
3368 int i;
3369
3370 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3371 return 0;
3372
3373 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003374 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3375 factor = num_stripes / 2;
3376 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3377 factor = num_stripes - 1;
3378 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3379 factor = num_stripes - 2;
3380 } else {
3381 factor = num_stripes;
3382 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003383
3384 for (i = 0; i < num_stripes; i++) {
3385 stripe = btrfs_stripe_nr(chunk, i);
3386 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3387 continue;
3388
3389 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3390 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003391 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003392
3393 if (stripe_offset < bargs->pend &&
3394 stripe_offset + stripe_length > bargs->pstart)
3395 return 0;
3396 }
3397
3398 return 1;
3399}
3400
Ilya Dryomovea671762012-01-16 22:04:48 +02003401/* [vstart, vend) */
3402static int chunk_vrange_filter(struct extent_buffer *leaf,
3403 struct btrfs_chunk *chunk,
3404 u64 chunk_offset,
3405 struct btrfs_balance_args *bargs)
3406{
3407 if (chunk_offset < bargs->vend &&
3408 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3409 /* at least part of the chunk is inside this vrange */
3410 return 0;
3411
3412 return 1;
3413}
3414
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003415static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3416 struct btrfs_chunk *chunk,
3417 struct btrfs_balance_args *bargs)
3418{
3419 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3420
3421 if (bargs->stripes_min <= num_stripes
3422 && num_stripes <= bargs->stripes_max)
3423 return 0;
3424
3425 return 1;
3426}
3427
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003428static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003429 struct btrfs_balance_args *bargs)
3430{
3431 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3432 return 0;
3433
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003434 chunk_type = chunk_to_extended(chunk_type) &
3435 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003436
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003437 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003438 return 1;
3439
3440 return 0;
3441}
3442
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003443static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003444 struct extent_buffer *leaf,
3445 struct btrfs_chunk *chunk, u64 chunk_offset)
3446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003447 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003448 struct btrfs_balance_args *bargs = NULL;
3449 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3450
3451 /* type filter */
3452 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3453 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3454 return 0;
3455 }
3456
3457 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3458 bargs = &bctl->data;
3459 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3460 bargs = &bctl->sys;
3461 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3462 bargs = &bctl->meta;
3463
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003464 /* profiles filter */
3465 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3466 chunk_profiles_filter(chunk_type, bargs)) {
3467 return 0;
3468 }
3469
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003470 /* usage filter */
3471 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003472 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003473 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003474 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003475 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003476 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003477 }
3478
Ilya Dryomov409d4042012-01-16 22:04:47 +02003479 /* devid filter */
3480 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3481 chunk_devid_filter(leaf, chunk, bargs)) {
3482 return 0;
3483 }
3484
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003485 /* drange filter, makes sense only with devid filter */
3486 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003487 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003488 return 0;
3489 }
3490
Ilya Dryomovea671762012-01-16 22:04:48 +02003491 /* vrange filter */
3492 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3493 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3494 return 0;
3495 }
3496
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003497 /* stripes filter */
3498 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3499 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3500 return 0;
3501 }
3502
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003503 /* soft profile changing mode */
3504 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3505 chunk_soft_convert_filter(chunk_type, bargs)) {
3506 return 0;
3507 }
3508
David Sterba7d824b62014-05-07 17:37:51 +02003509 /*
3510 * limited by count, must be the last filter
3511 */
3512 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3513 if (bargs->limit == 0)
3514 return 0;
3515 else
3516 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003517 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3518 /*
3519 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003520 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003521 * about the count of all chunks that satisfy the filters.
3522 */
3523 if (bargs->limit_max == 0)
3524 return 0;
3525 else
3526 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003527 }
3528
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003529 return 1;
3530}
3531
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003532static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003533{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003534 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003535 struct btrfs_root *chunk_root = fs_info->chunk_root;
3536 struct btrfs_root *dev_root = fs_info->dev_root;
3537 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003538 struct btrfs_device *device;
3539 u64 old_size;
3540 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003541 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003542 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003543 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003544 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003545 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003546 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003547 struct extent_buffer *leaf;
3548 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003549 int ret;
3550 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003551 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003552 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003553 u64 limit_data = bctl->data.limit;
3554 u64 limit_meta = bctl->meta.limit;
3555 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003556 u32 count_data = 0;
3557 u32 count_meta = 0;
3558 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003559 int chunk_reserved = 0;
Liu Bocf25ce52015-12-14 18:29:32 -08003560 u64 bytes_used = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003561
Chris Masonec44a352008-04-28 15:29:52 -04003562 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003563 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003564 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003565 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003566 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003567 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003568 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003569 btrfs_device_get_total_bytes(device) -
3570 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003571 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003572 continue;
3573
3574 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003575 if (ret == -ENOSPC)
3576 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003577 if (ret) {
3578 /* btrfs_shrink_device never returns ret > 0 */
3579 WARN_ON(ret > 0);
3580 goto error;
3581 }
Chris Masonec44a352008-04-28 15:29:52 -04003582
Yan, Zhenga22285a2010-05-16 10:48:46 -04003583 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003584 if (IS_ERR(trans)) {
3585 ret = PTR_ERR(trans);
3586 btrfs_info_in_rcu(fs_info,
3587 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3588 rcu_str_deref(device->name), ret,
3589 old_size, old_size - size_to_free);
3590 goto error;
3591 }
Chris Masonec44a352008-04-28 15:29:52 -04003592
3593 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003594 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003595 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003596 /* btrfs_grow_device never returns ret > 0 */
3597 WARN_ON(ret > 0);
3598 btrfs_info_in_rcu(fs_info,
3599 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3600 rcu_str_deref(device->name), ret,
3601 old_size, old_size - size_to_free);
3602 goto error;
3603 }
Chris Masonec44a352008-04-28 15:29:52 -04003604
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003605 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003606 }
3607
3608 /* step two, relocate all the chunks */
3609 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003610 if (!path) {
3611 ret = -ENOMEM;
3612 goto error;
3613 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003614
3615 /* zero out stat counters */
3616 spin_lock(&fs_info->balance_lock);
3617 memset(&bctl->stat, 0, sizeof(bctl->stat));
3618 spin_unlock(&fs_info->balance_lock);
3619again:
David Sterba7d824b62014-05-07 17:37:51 +02003620 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003621 /*
3622 * The single value limit and min/max limits use the same bytes
3623 * in the
3624 */
David Sterba7d824b62014-05-07 17:37:51 +02003625 bctl->data.limit = limit_data;
3626 bctl->meta.limit = limit_meta;
3627 bctl->sys.limit = limit_sys;
3628 }
Chris Masonec44a352008-04-28 15:29:52 -04003629 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3630 key.offset = (u64)-1;
3631 key.type = BTRFS_CHUNK_ITEM_KEY;
3632
Chris Masond3977122009-01-05 21:25:51 -05003633 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003634 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003635 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003636 ret = -ECANCELED;
3637 goto error;
3638 }
3639
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003640 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003641 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003642 if (ret < 0) {
3643 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003644 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003645 }
Chris Masonec44a352008-04-28 15:29:52 -04003646
3647 /*
3648 * this shouldn't happen, it means the last relocate
3649 * failed
3650 */
3651 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003652 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003653
3654 ret = btrfs_previous_item(chunk_root, path, 0,
3655 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003656 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003657 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003658 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003659 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003660 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003661
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003662 leaf = path->nodes[0];
3663 slot = path->slots[0];
3664 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3665
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003666 if (found_key.objectid != key.objectid) {
3667 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003668 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003669 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003670
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003671 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003672 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003673
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003674 if (!counting) {
3675 spin_lock(&fs_info->balance_lock);
3676 bctl->stat.considered++;
3677 spin_unlock(&fs_info->balance_lock);
3678 }
3679
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003680 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003681 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003682
David Sterbab3b4aa72011-04-21 01:20:15 +02003683 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003684 if (!ret) {
3685 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003686 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003687 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003688
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003689 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003690 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003691 spin_lock(&fs_info->balance_lock);
3692 bctl->stat.expected++;
3693 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003694
3695 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3696 count_data++;
3697 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3698 count_sys++;
3699 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3700 count_meta++;
3701
3702 goto loop;
3703 }
3704
3705 /*
3706 * Apply limit_min filter, no need to check if the LIMITS
3707 * filter is used, limit_min is 0 by default
3708 */
3709 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3710 count_data < bctl->data.limit_min)
3711 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3712 count_meta < bctl->meta.limit_min)
3713 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3714 count_sys < bctl->sys.limit_min)) {
3715 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003716 goto loop;
3717 }
3718
Liu Bocf25ce52015-12-14 18:29:32 -08003719 ASSERT(fs_info->data_sinfo);
3720 spin_lock(&fs_info->data_sinfo->lock);
3721 bytes_used = fs_info->data_sinfo->bytes_used;
3722 spin_unlock(&fs_info->data_sinfo->lock);
3723
3724 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3725 !chunk_reserved && !bytes_used) {
Zhao Lei2c9fe832015-11-09 11:51:32 +08003726 trans = btrfs_start_transaction(chunk_root, 0);
3727 if (IS_ERR(trans)) {
3728 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3729 ret = PTR_ERR(trans);
3730 goto error;
3731 }
3732
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003733 ret = btrfs_force_chunk_alloc(trans, fs_info,
Zhao Lei2c9fe832015-11-09 11:51:32 +08003734 BTRFS_BLOCK_GROUP_DATA);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003735 btrfs_end_transaction(trans);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003736 if (ret < 0) {
3737 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3738 goto error;
3739 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003740 chunk_reserved = 1;
3741 }
3742
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003743 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003744 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003745 if (ret && ret != -ENOSPC)
3746 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003747 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003748 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003749 } else {
3750 spin_lock(&fs_info->balance_lock);
3751 bctl->stat.completed++;
3752 spin_unlock(&fs_info->balance_lock);
3753 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003754loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003755 if (found_key.offset == 0)
3756 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003757 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003758 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003759
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003760 if (counting) {
3761 btrfs_release_path(path);
3762 counting = false;
3763 goto again;
3764 }
Chris Masonec44a352008-04-28 15:29:52 -04003765error:
3766 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003767 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003768 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003769 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003770 if (!ret)
3771 ret = -ENOSPC;
3772 }
3773
Chris Masonec44a352008-04-28 15:29:52 -04003774 return ret;
3775}
3776
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003777/**
3778 * alloc_profile_is_valid - see if a given profile is valid and reduced
3779 * @flags: profile to validate
3780 * @extended: if true @flags is treated as an extended profile
3781 */
3782static int alloc_profile_is_valid(u64 flags, int extended)
3783{
3784 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3785 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3786
3787 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3788
3789 /* 1) check that all other bits are zeroed */
3790 if (flags & ~mask)
3791 return 0;
3792
3793 /* 2) see if profile is reduced */
3794 if (flags == 0)
3795 return !extended; /* "0" is valid for usual profiles */
3796
3797 /* true if exactly one bit set */
3798 return (flags & (flags - 1)) == 0;
3799}
3800
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003801static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3802{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003803 /* cancel requested || normal exit path */
3804 return atomic_read(&fs_info->balance_cancel_req) ||
3805 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3806 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003807}
3808
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003809static void __cancel_balance(struct btrfs_fs_info *fs_info)
3810{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003811 int ret;
3812
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003813 unset_balance_control(fs_info);
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003814 ret = del_balance_item(fs_info);
Liu Bo0f788c52013-03-04 16:25:40 +00003815 if (ret)
Anand Jain34d97002016-03-16 16:43:06 +08003816 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003817
David Sterba171938e2017-03-28 14:44:21 +02003818 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003819}
3820
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003821/* Non-zero return value signifies invalidity */
3822static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3823 u64 allowed)
3824{
3825 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3826 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3827 (bctl_arg->target & ~allowed)));
3828}
3829
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003830/*
3831 * Should be called with both balance and volume mutexes held
3832 */
3833int btrfs_balance(struct btrfs_balance_control *bctl,
3834 struct btrfs_ioctl_balance_args *bargs)
3835{
3836 struct btrfs_fs_info *fs_info = bctl->fs_info;
Adam Borowski14506122017-03-07 23:34:44 +01003837 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003838 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003839 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003840 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003841 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003842 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003843
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003844 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003845 atomic_read(&fs_info->balance_pause_req) ||
3846 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003847 ret = -EINVAL;
3848 goto out;
3849 }
3850
Ilya Dryomove4837f82012-03-27 17:09:17 +03003851 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3852 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3853 mixed = 1;
3854
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003855 /*
3856 * In case of mixed groups both data and meta should be picked,
3857 * and identical options should be given for both of them.
3858 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003859 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3860 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003861 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3862 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3863 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003864 btrfs_err(fs_info,
3865 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003866 ret = -EINVAL;
3867 goto out;
3868 }
3869 }
3870
Stefan Behrens8dabb742012-11-06 13:15:27 +01003871 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003872 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003873 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3874 BUG_ON(num_devices < 1);
3875 num_devices--;
3876 }
Liu Bo73beece2015-07-17 16:49:19 +08003877 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003878 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3879 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003880 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003881 if (num_devices > 2)
3882 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3883 if (num_devices > 3)
3884 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3885 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003886 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003887 btrfs_err(fs_info,
3888 "unable to start balance with target data profile %llu",
3889 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003890 ret = -EINVAL;
3891 goto out;
3892 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003893 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003894 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003895 "unable to start balance with target metadata profile %llu",
3896 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003897 ret = -EINVAL;
3898 goto out;
3899 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003900 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003901 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003902 "unable to start balance with target system profile %llu",
3903 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003904 ret = -EINVAL;
3905 goto out;
3906 }
3907
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003908 /* allow to reduce meta or sys integrity only if force set */
3909 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003910 BTRFS_BLOCK_GROUP_RAID10 |
3911 BTRFS_BLOCK_GROUP_RAID5 |
3912 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003913 do {
3914 seq = read_seqbegin(&fs_info->profiles_lock);
3915
3916 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3917 (fs_info->avail_system_alloc_bits & allowed) &&
3918 !(bctl->sys.target & allowed)) ||
3919 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3920 (fs_info->avail_metadata_alloc_bits & allowed) &&
3921 !(bctl->meta.target & allowed))) {
3922 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003923 btrfs_info(fs_info,
3924 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003925 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003926 btrfs_err(fs_info,
3927 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003928 ret = -EINVAL;
3929 goto out;
3930 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003931 }
Miao Xiede98ced2013-01-29 10:13:12 +00003932 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003933
Adam Borowski14506122017-03-07 23:34:44 +01003934 /* if we're not converting, the target field is uninitialized */
3935 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3936 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3937 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3938 bctl->data.target : fs_info->avail_data_alloc_bits;
3939 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3940 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003941 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003942 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003943 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003944 }
3945
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003946 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003947 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003948 goto out;
3949
Ilya Dryomov59641012012-01-16 22:04:48 +02003950 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3951 BUG_ON(ret == -EEXIST);
3952 set_balance_control(bctl);
3953 } else {
3954 BUG_ON(ret != -EEXIST);
3955 spin_lock(&fs_info->balance_lock);
3956 update_balance_args(bctl);
3957 spin_unlock(&fs_info->balance_lock);
3958 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003959
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003960 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003961 mutex_unlock(&fs_info->balance_mutex);
3962
3963 ret = __btrfs_balance(fs_info);
3964
3965 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003966 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003967
3968 if (bargs) {
3969 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003970 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003971 }
3972
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003973 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3974 balance_need_close(fs_info)) {
3975 __cancel_balance(fs_info);
3976 }
3977
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003978 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003979
3980 return ret;
3981out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003982 if (bctl->flags & BTRFS_BALANCE_RESUME)
3983 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003984 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003985 kfree(bctl);
David Sterba171938e2017-03-28 14:44:21 +02003986 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003987 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003988 return ret;
3989}
3990
3991static int balance_kthread(void *data)
3992{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003993 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003994 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003995
3996 mutex_lock(&fs_info->volume_mutex);
3997 mutex_lock(&fs_info->balance_mutex);
3998
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003999 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004000 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004001 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02004002 }
Ilya Dryomov59641012012-01-16 22:04:48 +02004003
4004 mutex_unlock(&fs_info->balance_mutex);
4005 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004006
Ilya Dryomov59641012012-01-16 22:04:48 +02004007 return ret;
4008}
4009
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004010int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
4011{
4012 struct task_struct *tsk;
4013
4014 spin_lock(&fs_info->balance_lock);
4015 if (!fs_info->balance_ctl) {
4016 spin_unlock(&fs_info->balance_lock);
4017 return 0;
4018 }
4019 spin_unlock(&fs_info->balance_lock);
4020
Jeff Mahoney3cdde222016-06-09 21:38:35 -04004021 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004022 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004023 return 0;
4024 }
4025
4026 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05304027 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06004028}
4029
Ilya Dryomov68310a52012-06-22 12:24:12 -06004030int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02004031{
Ilya Dryomov59641012012-01-16 22:04:48 +02004032 struct btrfs_balance_control *bctl;
4033 struct btrfs_balance_item *item;
4034 struct btrfs_disk_balance_args disk_bargs;
4035 struct btrfs_path *path;
4036 struct extent_buffer *leaf;
4037 struct btrfs_key key;
4038 int ret;
4039
4040 path = btrfs_alloc_path();
4041 if (!path)
4042 return -ENOMEM;
4043
Ilya Dryomov68310a52012-06-22 12:24:12 -06004044 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01004045 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06004046 key.offset = 0;
4047
4048 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4049 if (ret < 0)
4050 goto out;
4051 if (ret > 0) { /* ret = -ENOENT; */
4052 ret = 0;
4053 goto out;
4054 }
4055
Ilya Dryomov59641012012-01-16 22:04:48 +02004056 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4057 if (!bctl) {
4058 ret = -ENOMEM;
4059 goto out;
4060 }
4061
Ilya Dryomov59641012012-01-16 22:04:48 +02004062 leaf = path->nodes[0];
4063 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4064
Ilya Dryomov68310a52012-06-22 12:24:12 -06004065 bctl->fs_info = fs_info;
4066 bctl->flags = btrfs_balance_flags(leaf, item);
4067 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004068
4069 btrfs_balance_data(leaf, item, &disk_bargs);
4070 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4071 btrfs_balance_meta(leaf, item, &disk_bargs);
4072 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4073 btrfs_balance_sys(leaf, item, &disk_bargs);
4074 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4075
David Sterba171938e2017-03-28 14:44:21 +02004076 WARN_ON(test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags));
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004077
Ilya Dryomov68310a52012-06-22 12:24:12 -06004078 mutex_lock(&fs_info->volume_mutex);
4079 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004080
Ilya Dryomov68310a52012-06-22 12:24:12 -06004081 set_balance_control(bctl);
4082
4083 mutex_unlock(&fs_info->balance_mutex);
4084 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004085out:
4086 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004087 return ret;
4088}
4089
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004090int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4091{
4092 int ret = 0;
4093
4094 mutex_lock(&fs_info->balance_mutex);
4095 if (!fs_info->balance_ctl) {
4096 mutex_unlock(&fs_info->balance_mutex);
4097 return -ENOTCONN;
4098 }
4099
4100 if (atomic_read(&fs_info->balance_running)) {
4101 atomic_inc(&fs_info->balance_pause_req);
4102 mutex_unlock(&fs_info->balance_mutex);
4103
4104 wait_event(fs_info->balance_wait_q,
4105 atomic_read(&fs_info->balance_running) == 0);
4106
4107 mutex_lock(&fs_info->balance_mutex);
4108 /* we are good with balance_ctl ripped off from under us */
4109 BUG_ON(atomic_read(&fs_info->balance_running));
4110 atomic_dec(&fs_info->balance_pause_req);
4111 } else {
4112 ret = -ENOTCONN;
4113 }
4114
4115 mutex_unlock(&fs_info->balance_mutex);
4116 return ret;
4117}
4118
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004119int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4120{
David Howellsbc98a422017-07-17 08:45:34 +01004121 if (sb_rdonly(fs_info->sb))
Ilya Dryomove649e582013-10-10 20:40:21 +03004122 return -EROFS;
4123
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004124 mutex_lock(&fs_info->balance_mutex);
4125 if (!fs_info->balance_ctl) {
4126 mutex_unlock(&fs_info->balance_mutex);
4127 return -ENOTCONN;
4128 }
4129
4130 atomic_inc(&fs_info->balance_cancel_req);
4131 /*
4132 * if we are running just wait and return, balance item is
4133 * deleted in btrfs_balance in this case
4134 */
4135 if (atomic_read(&fs_info->balance_running)) {
4136 mutex_unlock(&fs_info->balance_mutex);
4137 wait_event(fs_info->balance_wait_q,
4138 atomic_read(&fs_info->balance_running) == 0);
4139 mutex_lock(&fs_info->balance_mutex);
4140 } else {
4141 /* __cancel_balance needs volume_mutex */
4142 mutex_unlock(&fs_info->balance_mutex);
4143 mutex_lock(&fs_info->volume_mutex);
4144 mutex_lock(&fs_info->balance_mutex);
4145
4146 if (fs_info->balance_ctl)
4147 __cancel_balance(fs_info);
4148
4149 mutex_unlock(&fs_info->volume_mutex);
4150 }
4151
4152 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
4153 atomic_dec(&fs_info->balance_cancel_req);
4154 mutex_unlock(&fs_info->balance_mutex);
4155 return 0;
4156}
4157
Stefan Behrens803b2f52013-08-15 17:11:21 +02004158static int btrfs_uuid_scan_kthread(void *data)
4159{
4160 struct btrfs_fs_info *fs_info = data;
4161 struct btrfs_root *root = fs_info->tree_root;
4162 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004163 struct btrfs_path *path = NULL;
4164 int ret = 0;
4165 struct extent_buffer *eb;
4166 int slot;
4167 struct btrfs_root_item root_item;
4168 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004169 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004170
4171 path = btrfs_alloc_path();
4172 if (!path) {
4173 ret = -ENOMEM;
4174 goto out;
4175 }
4176
4177 key.objectid = 0;
4178 key.type = BTRFS_ROOT_ITEM_KEY;
4179 key.offset = 0;
4180
Stefan Behrens803b2f52013-08-15 17:11:21 +02004181 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004182 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004183 if (ret) {
4184 if (ret > 0)
4185 ret = 0;
4186 break;
4187 }
4188
4189 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4190 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4191 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4192 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4193 goto skip;
4194
4195 eb = path->nodes[0];
4196 slot = path->slots[0];
4197 item_size = btrfs_item_size_nr(eb, slot);
4198 if (item_size < sizeof(root_item))
4199 goto skip;
4200
Stefan Behrens803b2f52013-08-15 17:11:21 +02004201 read_extent_buffer(eb, &root_item,
4202 btrfs_item_ptr_offset(eb, slot),
4203 (int)sizeof(root_item));
4204 if (btrfs_root_refs(&root_item) == 0)
4205 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004206
4207 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4208 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4209 if (trans)
4210 goto update_tree;
4211
4212 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004213 /*
4214 * 1 - subvol uuid item
4215 * 1 - received_subvol uuid item
4216 */
4217 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4218 if (IS_ERR(trans)) {
4219 ret = PTR_ERR(trans);
4220 break;
4221 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004222 continue;
4223 } else {
4224 goto skip;
4225 }
4226update_tree:
4227 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004228 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004229 root_item.uuid,
4230 BTRFS_UUID_KEY_SUBVOL,
4231 key.objectid);
4232 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004233 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004234 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004235 break;
4236 }
4237 }
4238
4239 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004240 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004241 root_item.received_uuid,
4242 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4243 key.objectid);
4244 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004245 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004246 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004247 break;
4248 }
4249 }
4250
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004251skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004252 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004253 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004254 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004255 if (ret)
4256 break;
4257 }
4258
Stefan Behrens803b2f52013-08-15 17:11:21 +02004259 btrfs_release_path(path);
4260 if (key.offset < (u64)-1) {
4261 key.offset++;
4262 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4263 key.offset = 0;
4264 key.type = BTRFS_ROOT_ITEM_KEY;
4265 } else if (key.objectid < (u64)-1) {
4266 key.offset = 0;
4267 key.type = BTRFS_ROOT_ITEM_KEY;
4268 key.objectid++;
4269 } else {
4270 break;
4271 }
4272 cond_resched();
4273 }
4274
4275out:
4276 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004277 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004278 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004279 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004280 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004281 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004282 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004283 up(&fs_info->uuid_tree_rescan_sem);
4284 return 0;
4285}
4286
Stefan Behrens70f80172013-08-15 17:11:23 +02004287/*
4288 * Callback for btrfs_uuid_tree_iterate().
4289 * returns:
4290 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004291 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004292 * > 0 if the check failed, which means the caller shall remove the entry.
4293 */
4294static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4295 u8 *uuid, u8 type, u64 subid)
4296{
4297 struct btrfs_key key;
4298 int ret = 0;
4299 struct btrfs_root *subvol_root;
4300
4301 if (type != BTRFS_UUID_KEY_SUBVOL &&
4302 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4303 goto out;
4304
4305 key.objectid = subid;
4306 key.type = BTRFS_ROOT_ITEM_KEY;
4307 key.offset = (u64)-1;
4308 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4309 if (IS_ERR(subvol_root)) {
4310 ret = PTR_ERR(subvol_root);
4311 if (ret == -ENOENT)
4312 ret = 1;
4313 goto out;
4314 }
4315
4316 switch (type) {
4317 case BTRFS_UUID_KEY_SUBVOL:
4318 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4319 ret = 1;
4320 break;
4321 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4322 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4323 BTRFS_UUID_SIZE))
4324 ret = 1;
4325 break;
4326 }
4327
4328out:
4329 return ret;
4330}
4331
4332static int btrfs_uuid_rescan_kthread(void *data)
4333{
4334 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4335 int ret;
4336
4337 /*
4338 * 1st step is to iterate through the existing UUID tree and
4339 * to delete all entries that contain outdated data.
4340 * 2nd step is to add all missing entries to the UUID tree.
4341 */
4342 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4343 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004344 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004345 up(&fs_info->uuid_tree_rescan_sem);
4346 return ret;
4347 }
4348 return btrfs_uuid_scan_kthread(data);
4349}
4350
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004351int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4352{
4353 struct btrfs_trans_handle *trans;
4354 struct btrfs_root *tree_root = fs_info->tree_root;
4355 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004356 struct task_struct *task;
4357 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004358
4359 /*
4360 * 1 - root node
4361 * 1 - root item
4362 */
4363 trans = btrfs_start_transaction(tree_root, 2);
4364 if (IS_ERR(trans))
4365 return PTR_ERR(trans);
4366
4367 uuid_root = btrfs_create_tree(trans, fs_info,
4368 BTRFS_UUID_TREE_OBJECTID);
4369 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004370 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004371 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004372 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004373 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004374 }
4375
4376 fs_info->uuid_root = uuid_root;
4377
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004378 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004379 if (ret)
4380 return ret;
4381
4382 down(&fs_info->uuid_tree_rescan_sem);
4383 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4384 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004385 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004386 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004387 up(&fs_info->uuid_tree_rescan_sem);
4388 return PTR_ERR(task);
4389 }
4390
4391 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004392}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004393
Stefan Behrens70f80172013-08-15 17:11:23 +02004394int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4395{
4396 struct task_struct *task;
4397
4398 down(&fs_info->uuid_tree_rescan_sem);
4399 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4400 if (IS_ERR(task)) {
4401 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004402 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004403 up(&fs_info->uuid_tree_rescan_sem);
4404 return PTR_ERR(task);
4405 }
4406
4407 return 0;
4408}
4409
Chris Mason8f18cf12008-04-25 16:53:30 -04004410/*
4411 * shrinking a device means finding all of the device extents past
4412 * the new size, and then following the back refs to the chunks.
4413 * The chunk relocation code actually frees the device extent
4414 */
4415int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004417 struct btrfs_fs_info *fs_info = device->fs_info;
4418 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004419 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004420 struct btrfs_dev_extent *dev_extent = NULL;
4421 struct btrfs_path *path;
4422 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004423 u64 chunk_offset;
4424 int ret;
4425 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004426 int failed = 0;
4427 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004428 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004429 struct extent_buffer *l;
4430 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004432 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004433 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004434 u64 diff;
4435
4436 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004437 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004438
Stefan Behrens63a212a2012-11-05 18:29:28 +01004439 if (device->is_tgtdev_for_dev_replace)
4440 return -EINVAL;
4441
Chris Mason8f18cf12008-04-25 16:53:30 -04004442 path = btrfs_alloc_path();
4443 if (!path)
4444 return -ENOMEM;
4445
David Sterbae4058b52015-11-27 16:31:35 +01004446 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004447
David Sterba34441362016-10-04 19:34:27 +02004448 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004449
Miao Xie7cc8e582014-09-03 21:35:38 +08004450 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004451 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004452 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004453 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004454 }
David Sterba34441362016-10-04 19:34:27 +02004455 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004456
Josef Bacikba1bf482009-09-11 16:11:19 -04004457again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004458 key.objectid = device->devid;
4459 key.offset = (u64)-1;
4460 key.type = BTRFS_DEV_EXTENT_KEY;
4461
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004462 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004463 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004464 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004465 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004466 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004467 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004468 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004469
4470 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004471 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004472 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004473 if (ret < 0)
4474 goto done;
4475 if (ret) {
4476 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004477 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004478 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004479 }
4480
4481 l = path->nodes[0];
4482 slot = path->slots[0];
4483 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4484
Josef Bacikba1bf482009-09-11 16:11:19 -04004485 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004486 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004487 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004488 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004489 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004490
4491 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4492 length = btrfs_dev_extent_length(l, dev_extent);
4493
Josef Bacikba1bf482009-09-11 16:11:19 -04004494 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004495 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004496 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004497 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004498 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004499
Chris Mason8f18cf12008-04-25 16:53:30 -04004500 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004501 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004502
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004503 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4504 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004505 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004506 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004507 if (ret == -ENOSPC)
4508 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004509 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004510
4511 if (failed && !retried) {
4512 failed = 0;
4513 retried = true;
4514 goto again;
4515 } else if (failed && retried) {
4516 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004517 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004518 }
4519
Chris Balld6397ba2009-04-27 07:29:03 -04004520 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004521 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004522 if (IS_ERR(trans)) {
4523 ret = PTR_ERR(trans);
4524 goto done;
4525 }
4526
David Sterba34441362016-10-04 19:34:27 +02004527 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004528
4529 /*
4530 * We checked in the above loop all device extents that were already in
4531 * the device tree. However before we have updated the device's
4532 * total_bytes to the new size, we might have had chunk allocations that
4533 * have not complete yet (new block groups attached to transaction
4534 * handles), and therefore their device extents were not yet in the
4535 * device tree and we missed them in the loop above. So if we have any
4536 * pending chunk using a device extent that overlaps the device range
4537 * that we can not use anymore, commit the current transaction and
4538 * repeat the search on the device tree - this way we guarantee we will
4539 * not have chunks using device extents that end beyond 'new_size'.
4540 */
4541 if (!checked_pending_chunks) {
4542 u64 start = new_size;
4543 u64 len = old_size - new_size;
4544
Jeff Mahoney499f3772015-06-15 09:41:17 -04004545 if (contains_pending_extent(trans->transaction, device,
4546 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004547 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004548 checked_pending_chunks = true;
4549 failed = 0;
4550 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004551 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004552 if (ret)
4553 goto done;
4554 goto again;
4555 }
4556 }
4557
Miao Xie7cc8e582014-09-03 21:35:38 +08004558 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004559 if (list_empty(&device->resized_list))
4560 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004561 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004562
Chris Balld6397ba2009-04-27 07:29:03 -04004563 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004564 btrfs_set_super_total_bytes(super_copy,
4565 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004566 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004567
4568 /* Now btrfs_update_device() will change the on-disk size. */
4569 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004570 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004571done:
4572 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004573 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004574 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004575 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004576 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004577 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004578 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004579 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004580 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004581 return ret;
4582}
4583
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004584static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004585 struct btrfs_key *key,
4586 struct btrfs_chunk *chunk, int item_size)
4587{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004588 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004589 struct btrfs_disk_key disk_key;
4590 u32 array_size;
4591 u8 *ptr;
4592
David Sterba34441362016-10-04 19:34:27 +02004593 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004594 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004595 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004596 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004597 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004598 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004599 }
Chris Mason0b86a832008-03-24 15:01:56 -04004600
4601 ptr = super_copy->sys_chunk_array + array_size;
4602 btrfs_cpu_key_to_disk(&disk_key, key);
4603 memcpy(ptr, &disk_key, sizeof(disk_key));
4604 ptr += sizeof(disk_key);
4605 memcpy(ptr, chunk, item_size);
4606 item_size += sizeof(disk_key);
4607 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004608 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004609
Chris Mason0b86a832008-03-24 15:01:56 -04004610 return 0;
4611}
4612
Miao Xieb2117a32011-01-05 10:07:28 +00004613/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004614 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004615 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004616static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004617{
Arne Jansen73c5de02011-04-12 12:07:57 +02004618 const struct btrfs_device_info *di_a = a;
4619 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004620
Arne Jansen73c5de02011-04-12 12:07:57 +02004621 if (di_a->max_avail > di_b->max_avail)
4622 return -1;
4623 if (di_a->max_avail < di_b->max_avail)
4624 return 1;
4625 if (di_a->total_avail > di_b->total_avail)
4626 return -1;
4627 if (di_a->total_avail < di_b->total_avail)
4628 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004629 return 0;
4630}
4631
David Woodhouse53b381b2013-01-29 18:40:14 -05004632static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4633{
Zhao Leiffe2d202015-01-20 15:11:44 +08004634 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004635 return;
4636
Miao Xieceda0862013-04-11 10:30:16 +00004637 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004638}
4639
Jeff Mahoneyda170662016-06-15 09:22:56 -04004640#define BTRFS_MAX_DEVS(r) ((BTRFS_MAX_ITEM_SIZE(r->fs_info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004641 - sizeof(struct btrfs_chunk)) \
4642 / sizeof(struct btrfs_stripe) + 1)
4643
4644#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4645 - 2 * sizeof(struct btrfs_disk_key) \
4646 - 2 * sizeof(struct btrfs_chunk)) \
4647 / sizeof(struct btrfs_stripe) + 1)
4648
Miao Xieb2117a32011-01-05 10:07:28 +00004649static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004650 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004651{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004652 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004653 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004654 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004655 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004656 struct extent_map_tree *em_tree;
4657 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004658 struct btrfs_device_info *devices_info = NULL;
4659 u64 total_avail;
4660 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004661 int data_stripes; /* number of stripes that count for
4662 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004663 int sub_stripes; /* sub_stripes info for map */
4664 int dev_stripes; /* stripes per dev */
4665 int devs_max; /* max devs to use */
4666 int devs_min; /* min devs needed */
4667 int devs_increment; /* ndevs has to be a multiple of this */
4668 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004669 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004670 u64 max_stripe_size;
4671 u64 max_chunk_size;
4672 u64 stripe_size;
4673 u64 num_bytes;
4674 int ndevs;
4675 int i;
4676 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004677 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004678
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004679 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004680
Miao Xieb2117a32011-01-05 10:07:28 +00004681 if (list_empty(&fs_devices->alloc_list))
4682 return -ENOSPC;
4683
Liu Bo31e50222012-11-21 14:18:10 +00004684 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004685
Liu Bo31e50222012-11-21 14:18:10 +00004686 sub_stripes = btrfs_raid_array[index].sub_stripes;
4687 dev_stripes = btrfs_raid_array[index].dev_stripes;
4688 devs_max = btrfs_raid_array[index].devs_max;
4689 devs_min = btrfs_raid_array[index].devs_min;
4690 devs_increment = btrfs_raid_array[index].devs_increment;
4691 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004692
4693 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004694 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004695 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004696 if (!devs_max)
4697 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004698 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004699 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004700 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4701 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004702 else
Byongho Leeee221842015-12-15 01:42:10 +09004703 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004704 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004705 if (!devs_max)
4706 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004707 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004708 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004709 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004710 if (!devs_max)
4711 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004712 } else {
David Sterba351fd352014-05-15 16:48:20 +02004713 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004714 type);
4715 BUG_ON(1);
4716 }
4717
4718 /* we don't want a chunk larger than 10% of writeable space */
4719 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4720 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004721
David Sterba31e818f2015-02-20 18:00:26 +01004722 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004723 GFP_NOFS);
4724 if (!devices_info)
4725 return -ENOMEM;
4726
Arne Jansen73c5de02011-04-12 12:07:57 +02004727 /*
4728 * in the first pass through the devices list, we gather information
4729 * about the available holes on each device.
4730 */
4731 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004732 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004733 u64 max_avail;
4734 u64 dev_offset;
4735
Anand Jainebbede42017-12-04 12:54:52 +08004736 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004737 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004738 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004739 continue;
4740 }
4741
Anand Jaine12c9622017-12-04 12:54:53 +08004742 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4743 &device->dev_state) ||
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 }
Anand Jainebbede42017-12-04 12:54:52 +08005096 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5097 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005098 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 ||
Anand Jainebbede42017-12-04 12:54:52 +08006266 (bio_op(first_bio) == REQ_OP_WRITE &&
6267 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006268 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006269 continue;
6270 }
6271
David Sterba3aa8e072017-06-02 17:38:30 +02006272 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006273 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006274 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006275 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006276
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006277 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6278 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006279 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006280 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006281 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006282}
6283
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006284struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006285 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006286{
Yan Zheng2b820322008-11-17 21:11:30 -05006287 struct btrfs_device *device;
6288 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006289
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006290 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006291 while (cur_devices) {
6292 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006293 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006294 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006295 if (device)
6296 return device;
6297 }
6298 cur_devices = cur_devices->seed;
6299 }
6300 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006301}
6302
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006303static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006304 u64 devid, u8 *dev_uuid)
6305{
6306 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006307
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006308 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6309 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006310 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006311
6312 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006313 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006314 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006315
6316 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006317 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006318
Chris Masondfe25022008-05-13 13:46:40 -04006319 return device;
6320}
6321
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006322/**
6323 * btrfs_alloc_device - allocate struct btrfs_device
6324 * @fs_info: used only for generating a new devid, can be NULL if
6325 * devid is provided (i.e. @devid != NULL).
6326 * @devid: a pointer to devid for this device. If NULL a new devid
6327 * is generated.
6328 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6329 * is generated.
6330 *
6331 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006332 * on error. Returned struct is not linked onto any lists and must be
6333 * destroyed with free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006334 */
6335struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6336 const u64 *devid,
6337 const u8 *uuid)
6338{
6339 struct btrfs_device *dev;
6340 u64 tmp;
6341
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306342 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006343 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006344
6345 dev = __alloc_device();
6346 if (IS_ERR(dev))
6347 return dev;
6348
6349 if (devid)
6350 tmp = *devid;
6351 else {
6352 int ret;
6353
6354 ret = find_next_devid(fs_info, &tmp);
6355 if (ret) {
David Sterba55de4802017-10-30 18:55:47 +01006356 free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006357 return ERR_PTR(ret);
6358 }
6359 }
6360 dev->devid = tmp;
6361
6362 if (uuid)
6363 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6364 else
6365 generate_random_uuid(dev->uuid);
6366
Liu Bo9e0af232014-08-15 23:36:53 +08006367 btrfs_init_work(&dev->work, btrfs_submit_helper,
6368 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006369
6370 return dev;
6371}
6372
Liu Boe06cd3d2016-06-03 12:05:15 -07006373/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006374static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006375 struct extent_buffer *leaf,
6376 struct btrfs_chunk *chunk, u64 logical)
6377{
6378 u64 length;
6379 u64 stripe_len;
6380 u16 num_stripes;
6381 u16 sub_stripes;
6382 u64 type;
6383
6384 length = btrfs_chunk_length(leaf, chunk);
6385 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6386 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6387 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6388 type = btrfs_chunk_type(leaf, chunk);
6389
6390 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006391 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006392 num_stripes);
6393 return -EIO;
6394 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006395 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6396 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006397 return -EIO;
6398 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006399 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6400 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006401 btrfs_chunk_sector_size(leaf, chunk));
6402 return -EIO;
6403 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006404 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6405 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006406 return -EIO;
6407 }
6408 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006409 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006410 stripe_len);
6411 return -EIO;
6412 }
6413 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6414 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006415 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006416 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6417 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6418 btrfs_chunk_type(leaf, chunk));
6419 return -EIO;
6420 }
6421 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6422 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6423 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6424 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6425 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6426 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6427 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006428 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006429 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6430 num_stripes, sub_stripes,
6431 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6432 return -EIO;
6433 }
6434
6435 return 0;
6436}
6437
Anand Jain5a2b8e62017-10-09 11:07:45 +08006438static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006439 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006440{
Anand Jain2b902df2017-10-09 11:07:46 +08006441 if (error)
6442 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6443 devid, uuid);
6444 else
6445 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6446 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006447}
6448
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006449static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006450 struct extent_buffer *leaf,
6451 struct btrfs_chunk *chunk)
6452{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006453 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006454 struct map_lookup *map;
6455 struct extent_map *em;
6456 u64 logical;
6457 u64 length;
6458 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006459 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006460 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006461 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006462 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006463
Chris Masone17cade2008-04-15 15:41:47 -04006464 logical = key->offset;
6465 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006466 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006467
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006468 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006469 if (ret)
6470 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006471
Chris Mason890871b2009-09-02 16:24:52 -04006472 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006473 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006474 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006475
6476 /* already mapped? */
6477 if (em && em->start <= logical && em->start + em->len > logical) {
6478 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006479 return 0;
6480 } else if (em) {
6481 free_extent_map(em);
6482 }
Chris Mason0b86a832008-03-24 15:01:56 -04006483
David Sterba172ddd62011-04-21 00:48:27 +02006484 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006485 if (!em)
6486 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006487 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006488 if (!map) {
6489 free_extent_map(em);
6490 return -ENOMEM;
6491 }
6492
Wang Shilong298a8f92014-06-19 10:42:52 +08006493 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006494 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006495 em->start = logical;
6496 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006497 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006498 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006499 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006500
Chris Mason593060d2008-03-25 16:50:33 -04006501 map->num_stripes = num_stripes;
6502 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6503 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006504 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6505 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006506 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006507 for (i = 0; i < num_stripes; i++) {
6508 map->stripes[i].physical =
6509 btrfs_stripe_offset_nr(leaf, chunk, i);
6510 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006511 read_extent_buffer(leaf, uuid, (unsigned long)
6512 btrfs_stripe_dev_uuid_nr(chunk, i),
6513 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006514 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006515 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006516 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006517 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006518 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006519 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006520 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006521 }
Chris Masondfe25022008-05-13 13:46:40 -04006522 if (!map->stripes[i].dev) {
6523 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006524 add_missing_dev(fs_info->fs_devices, devid,
6525 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006526 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006527 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006528 btrfs_err(fs_info,
6529 "failed to init missing dev %llu: %ld",
6530 devid, PTR_ERR(map->stripes[i].dev));
6531 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006532 }
Anand Jain2b902df2017-10-09 11:07:46 +08006533 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006534 }
Anand Jaine12c9622017-12-04 12:54:53 +08006535 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6536 &(map->stripes[i].dev->dev_state));
6537
Chris Mason0b86a832008-03-24 15:01:56 -04006538 }
6539
Chris Mason890871b2009-09-02 16:24:52 -04006540 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006541 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006542 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006543 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006544 free_extent_map(em);
6545
6546 return 0;
6547}
6548
Jeff Mahoney143bede2012-03-01 14:56:26 +01006549static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006550 struct btrfs_dev_item *dev_item,
6551 struct btrfs_device *device)
6552{
6553 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006554
6555 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006556 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6557 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006558 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006559 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006560 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006561 device->type = btrfs_device_type(leaf, dev_item);
6562 device->io_align = btrfs_device_io_align(leaf, dev_item);
6563 device->io_width = btrfs_device_io_width(leaf, dev_item);
6564 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006565 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006566 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006567
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006568 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006569 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006570}
6571
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006572static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006573 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006574{
6575 struct btrfs_fs_devices *fs_devices;
6576 int ret;
6577
Li Zefanb367e472011-12-07 11:38:24 +08006578 BUG_ON(!mutex_is_locked(&uuid_mutex));
David Sterba2dfeca92017-06-14 02:48:07 +02006579 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006580
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006581 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006582 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006583 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006584 return fs_devices;
6585
Yan Zheng2b820322008-11-17 21:11:30 -05006586 fs_devices = fs_devices->seed;
6587 }
6588
6589 fs_devices = find_fsid(fsid);
6590 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006591 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006592 return ERR_PTR(-ENOENT);
6593
6594 fs_devices = alloc_fs_devices(fsid);
6595 if (IS_ERR(fs_devices))
6596 return fs_devices;
6597
6598 fs_devices->seeding = 1;
6599 fs_devices->opened = 1;
6600 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006601 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006602
6603 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006604 if (IS_ERR(fs_devices))
6605 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006606
Christoph Hellwig97288f22008-12-02 06:36:09 -05006607 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006608 fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006609 if (ret) {
6610 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006611 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006612 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006613 }
Yan Zheng2b820322008-11-17 21:11:30 -05006614
6615 if (!fs_devices->seeding) {
6616 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006617 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006618 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006619 goto out;
6620 }
6621
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006622 fs_devices->seed = fs_info->fs_devices->seed;
6623 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006624out:
Miao Xie5f375832014-09-03 21:35:46 +08006625 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006626}
6627
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006628static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006629 struct extent_buffer *leaf,
6630 struct btrfs_dev_item *dev_item)
6631{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006632 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006633 struct btrfs_device *device;
6634 u64 devid;
6635 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006636 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006637 u8 dev_uuid[BTRFS_UUID_SIZE];
6638
Chris Mason0b86a832008-03-24 15:01:56 -04006639 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006640 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006641 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006642 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006643 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006644
Anand Jain44880fd2017-07-29 17:50:09 +08006645 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006646 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006647 if (IS_ERR(fs_devices))
6648 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006649 }
6650
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006651 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006652 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006653 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006654 btrfs_report_missing_device(fs_info, devid,
6655 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006656 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006657 }
Yan Zheng2b820322008-11-17 21:11:30 -05006658
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006659 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006660 if (IS_ERR(device)) {
6661 btrfs_err(fs_info,
6662 "failed to add missing dev %llu: %ld",
6663 devid, PTR_ERR(device));
6664 return PTR_ERR(device);
6665 }
Anand Jain2b902df2017-10-09 11:07:46 +08006666 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006667 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006668 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006669 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6670 btrfs_report_missing_device(fs_info,
6671 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006672 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006673 }
6674 btrfs_report_missing_device(fs_info, devid,
6675 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006676 }
Miao Xie5f375832014-09-03 21:35:46 +08006677
6678 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006679 /*
6680 * this happens when a device that was properly setup
6681 * in the device info lists suddenly goes bad.
6682 * device->bdev is NULL, and so we have to set
6683 * device->missing to one here
6684 */
Miao Xie5f375832014-09-03 21:35:46 +08006685 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006686 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006687 }
Miao Xie5f375832014-09-03 21:35:46 +08006688
6689 /* Move the device to its own fs_devices */
6690 if (device->fs_devices != fs_devices) {
6691 ASSERT(device->missing);
6692
6693 list_move(&device->dev_list, &fs_devices->devices);
6694 device->fs_devices->num_devices--;
6695 fs_devices->num_devices++;
6696
6697 device->fs_devices->missing_devices--;
6698 fs_devices->missing_devices++;
6699
6700 device->fs_devices = fs_devices;
6701 }
Yan Zheng2b820322008-11-17 21:11:30 -05006702 }
6703
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006704 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006705 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006706 if (device->generation !=
6707 btrfs_device_generation(leaf, dev_item))
6708 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006709 }
Chris Mason0b86a832008-03-24 15:01:56 -04006710
6711 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006712 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006713 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
6714 !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006715 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006716 atomic64_add(device->total_bytes - device->bytes_used,
6717 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006718 }
Chris Mason0b86a832008-03-24 15:01:56 -04006719 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006720 return ret;
6721}
6722
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006723int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006724{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006725 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006726 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006727 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006728 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006729 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006730 u8 *array_ptr;
6731 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006732 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006733 u32 num_stripes;
6734 u32 array_size;
6735 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006736 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006737 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006738 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006739
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006740 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006741 /*
6742 * This will create extent buffer of nodesize, superblock size is
6743 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6744 * overallocate but we can keep it as-is, only the first page is used.
6745 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006746 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006747 if (IS_ERR(sb))
6748 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006749 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006750 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006751 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006752 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006753 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006754 * pages up-to-date when the page is larger: extent does not cover the
6755 * whole page and consequently check_page_uptodate does not find all
6756 * the page's extents up-to-date (the hole beyond sb),
6757 * write_extent_buffer then triggers a WARN_ON.
6758 *
6759 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6760 * but sb spans only this function. Add an explicit SetPageUptodate call
6761 * to silence the warning eg. on PowerPC 64.
6762 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006763 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006764 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006765
Chris Masona061fc82008-05-07 11:43:44 -04006766 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006767 array_size = btrfs_super_sys_array_size(super_copy);
6768
David Sterba1ffb22c2014-10-31 19:02:42 +01006769 array_ptr = super_copy->sys_chunk_array;
6770 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6771 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006772
David Sterba1ffb22c2014-10-31 19:02:42 +01006773 while (cur_offset < array_size) {
6774 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006775 len = sizeof(*disk_key);
6776 if (cur_offset + len > array_size)
6777 goto out_short_read;
6778
Chris Mason0b86a832008-03-24 15:01:56 -04006779 btrfs_disk_key_to_cpu(&key, disk_key);
6780
David Sterba1ffb22c2014-10-31 19:02:42 +01006781 array_ptr += len;
6782 sb_array_offset += len;
6783 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006784
Chris Mason0d81ba52008-03-24 15:02:07 -04006785 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006786 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006787 /*
6788 * At least one btrfs_chunk with one stripe must be
6789 * present, exact stripe count check comes afterwards
6790 */
6791 len = btrfs_chunk_item_size(1);
6792 if (cur_offset + len > array_size)
6793 goto out_short_read;
6794
6795 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006796 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006797 btrfs_err(fs_info,
6798 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006799 num_stripes, cur_offset);
6800 ret = -EIO;
6801 break;
6802 }
6803
Liu Boe06cd3d2016-06-03 12:05:15 -07006804 type = btrfs_chunk_type(sb, chunk);
6805 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006806 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006807 "invalid chunk type %llu in sys_array at offset %u",
6808 type, cur_offset);
6809 ret = -EIO;
6810 break;
6811 }
6812
David Sterbae3540ea2014-11-05 15:24:51 +01006813 len = btrfs_chunk_item_size(num_stripes);
6814 if (cur_offset + len > array_size)
6815 goto out_short_read;
6816
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006817 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006818 if (ret)
6819 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006820 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006821 btrfs_err(fs_info,
6822 "unexpected item type %u in sys_array at offset %u",
6823 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006824 ret = -EIO;
6825 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006826 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006827 array_ptr += len;
6828 sb_array_offset += len;
6829 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006830 }
Liu Bod8651772016-06-03 17:41:42 -07006831 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006832 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006833 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006834
6835out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006836 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006837 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006838 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006839 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006840 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006841}
6842
Qu Wenruo21634a12017-03-09 09:34:36 +08006843/*
6844 * Check if all chunks in the fs are OK for read-write degraded mount
6845 *
6846 * Return true if all chunks meet the minimal RW mount requirements.
6847 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6848 */
6849bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info)
6850{
6851 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6852 struct extent_map *em;
6853 u64 next_start = 0;
6854 bool ret = true;
6855
6856 read_lock(&map_tree->map_tree.lock);
6857 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6858 read_unlock(&map_tree->map_tree.lock);
6859 /* No chunk at all? Return false anyway */
6860 if (!em) {
6861 ret = false;
6862 goto out;
6863 }
6864 while (em) {
6865 struct map_lookup *map;
6866 int missing = 0;
6867 int max_tolerated;
6868 int i;
6869
6870 map = em->map_lookup;
6871 max_tolerated =
6872 btrfs_get_num_tolerated_disk_barrier_failures(
6873 map->type);
6874 for (i = 0; i < map->num_stripes; i++) {
6875 struct btrfs_device *dev = map->stripes[i].dev;
6876
6877 if (!dev || !dev->bdev || dev->missing ||
6878 dev->last_flush_error)
6879 missing++;
6880 }
6881 if (missing > max_tolerated) {
6882 btrfs_warn(fs_info,
6883 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6884 em->start, missing, max_tolerated);
6885 free_extent_map(em);
6886 ret = false;
6887 goto out;
6888 }
6889 next_start = extent_map_end(em);
6890 free_extent_map(em);
6891
6892 read_lock(&map_tree->map_tree.lock);
6893 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6894 (u64)(-1) - next_start);
6895 read_unlock(&map_tree->map_tree.lock);
6896 }
6897out:
6898 return ret;
6899}
6900
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006901int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006902{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006903 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006904 struct btrfs_path *path;
6905 struct extent_buffer *leaf;
6906 struct btrfs_key key;
6907 struct btrfs_key found_key;
6908 int ret;
6909 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006910 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006911
Chris Mason0b86a832008-03-24 15:01:56 -04006912 path = btrfs_alloc_path();
6913 if (!path)
6914 return -ENOMEM;
6915
Li Zefanb367e472011-12-07 11:38:24 +08006916 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006917 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006918
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006919 /*
6920 * Read all device items, and then all the chunk items. All
6921 * device items are found before any chunk item (their object id
6922 * is smaller than the lowest possible object id for a chunk
6923 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006924 */
6925 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6926 key.offset = 0;
6927 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006928 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006929 if (ret < 0)
6930 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006931 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006932 leaf = path->nodes[0];
6933 slot = path->slots[0];
6934 if (slot >= btrfs_header_nritems(leaf)) {
6935 ret = btrfs_next_leaf(root, path);
6936 if (ret == 0)
6937 continue;
6938 if (ret < 0)
6939 goto error;
6940 break;
6941 }
6942 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006943 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6944 struct btrfs_dev_item *dev_item;
6945 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006946 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006947 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006948 if (ret)
6949 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006950 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006951 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6952 struct btrfs_chunk *chunk;
6953 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006954 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006955 if (ret)
6956 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006957 }
6958 path->slots[0]++;
6959 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006960
6961 /*
6962 * After loading chunk tree, we've got all device information,
6963 * do another round of validation checks.
6964 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006965 if (total_dev != fs_info->fs_devices->total_devices) {
6966 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006967 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006968 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006969 total_dev);
6970 ret = -EINVAL;
6971 goto error;
6972 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006973 if (btrfs_super_total_bytes(fs_info->super_copy) <
6974 fs_info->fs_devices->total_rw_bytes) {
6975 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006976 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006977 btrfs_super_total_bytes(fs_info->super_copy),
6978 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07006979 ret = -EINVAL;
6980 goto error;
6981 }
Chris Mason0b86a832008-03-24 15:01:56 -04006982 ret = 0;
6983error:
David Sterba34441362016-10-04 19:34:27 +02006984 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006985 mutex_unlock(&uuid_mutex);
6986
Yan Zheng2b820322008-11-17 21:11:30 -05006987 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006988 return ret;
6989}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006990
Miao Xiecb517ea2013-05-15 07:48:19 +00006991void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6992{
6993 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6994 struct btrfs_device *device;
6995
Liu Bo29cc83f2014-05-11 23:14:59 +08006996 while (fs_devices) {
6997 mutex_lock(&fs_devices->device_list_mutex);
6998 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04006999 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007000 mutex_unlock(&fs_devices->device_list_mutex);
7001
7002 fs_devices = fs_devices->seed;
7003 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007004}
7005
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007006static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7007{
7008 int i;
7009
7010 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7011 btrfs_dev_stat_reset(dev, i);
7012}
7013
7014int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7015{
7016 struct btrfs_key key;
7017 struct btrfs_key found_key;
7018 struct btrfs_root *dev_root = fs_info->dev_root;
7019 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7020 struct extent_buffer *eb;
7021 int slot;
7022 int ret = 0;
7023 struct btrfs_device *device;
7024 struct btrfs_path *path = NULL;
7025 int i;
7026
7027 path = btrfs_alloc_path();
7028 if (!path) {
7029 ret = -ENOMEM;
7030 goto out;
7031 }
7032
7033 mutex_lock(&fs_devices->device_list_mutex);
7034 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7035 int item_size;
7036 struct btrfs_dev_stats_item *ptr;
7037
David Sterba242e2952016-01-25 17:51:31 +01007038 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7039 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007040 key.offset = device->devid;
7041 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7042 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007043 __btrfs_reset_dev_stats(device);
7044 device->dev_stats_valid = 1;
7045 btrfs_release_path(path);
7046 continue;
7047 }
7048 slot = path->slots[0];
7049 eb = path->nodes[0];
7050 btrfs_item_key_to_cpu(eb, &found_key, slot);
7051 item_size = btrfs_item_size_nr(eb, slot);
7052
7053 ptr = btrfs_item_ptr(eb, slot,
7054 struct btrfs_dev_stats_item);
7055
7056 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7057 if (item_size >= (1 + i) * sizeof(__le64))
7058 btrfs_dev_stat_set(device, i,
7059 btrfs_dev_stats_value(eb, ptr, i));
7060 else
7061 btrfs_dev_stat_reset(device, i);
7062 }
7063
7064 device->dev_stats_valid = 1;
7065 btrfs_dev_stat_print_on_load(device);
7066 btrfs_release_path(path);
7067 }
7068 mutex_unlock(&fs_devices->device_list_mutex);
7069
7070out:
7071 btrfs_free_path(path);
7072 return ret < 0 ? ret : 0;
7073}
7074
7075static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007076 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007077 struct btrfs_device *device)
7078{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007079 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007080 struct btrfs_path *path;
7081 struct btrfs_key key;
7082 struct extent_buffer *eb;
7083 struct btrfs_dev_stats_item *ptr;
7084 int ret;
7085 int i;
7086
David Sterba242e2952016-01-25 17:51:31 +01007087 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7088 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007089 key.offset = device->devid;
7090
7091 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007092 if (!path)
7093 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007094 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7095 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007096 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007097 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007098 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007099 goto out;
7100 }
7101
7102 if (ret == 0 &&
7103 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7104 /* need to delete old one and insert a new one */
7105 ret = btrfs_del_item(trans, dev_root, path);
7106 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007107 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007108 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007109 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007110 goto out;
7111 }
7112 ret = 1;
7113 }
7114
7115 if (ret == 1) {
7116 /* need to insert a new item */
7117 btrfs_release_path(path);
7118 ret = btrfs_insert_empty_item(trans, dev_root, path,
7119 &key, sizeof(*ptr));
7120 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007121 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007122 "insert dev_stats item for device %s failed %d",
7123 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007124 goto out;
7125 }
7126 }
7127
7128 eb = path->nodes[0];
7129 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7130 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7131 btrfs_set_dev_stats_value(eb, ptr, i,
7132 btrfs_dev_stat_read(device, i));
7133 btrfs_mark_buffer_dirty(eb);
7134
7135out:
7136 btrfs_free_path(path);
7137 return ret;
7138}
7139
7140/*
7141 * called from commit_transaction. Writes all changed device stats to disk.
7142 */
7143int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7144 struct btrfs_fs_info *fs_info)
7145{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007146 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7147 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007148 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007149 int ret = 0;
7150
7151 mutex_lock(&fs_devices->device_list_mutex);
7152 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007153 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7154 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007155 continue;
7156
Nikolay Borisov9deae962017-10-24 13:47:37 +03007157
7158 /*
7159 * There is a LOAD-LOAD control dependency between the value of
7160 * dev_stats_ccnt and updating the on-disk values which requires
7161 * reading the in-memory counters. Such control dependencies
7162 * require explicit read memory barriers.
7163 *
7164 * This memory barriers pairs with smp_mb__before_atomic in
7165 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7166 * barrier implied by atomic_xchg in
7167 * btrfs_dev_stats_read_and_reset
7168 */
7169 smp_rmb();
7170
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007171 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007172 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007173 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007174 }
7175 mutex_unlock(&fs_devices->device_list_mutex);
7176
7177 return ret;
7178}
7179
Stefan Behrens442a4f62012-05-25 16:06:08 +02007180void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7181{
7182 btrfs_dev_stat_inc(dev, index);
7183 btrfs_dev_stat_print_on_error(dev);
7184}
7185
Eric Sandeen48a3b632013-04-25 20:41:01 +00007186static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007187{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007188 if (!dev->dev_stats_valid)
7189 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007190 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007191 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007192 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007193 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7194 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7195 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007196 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7197 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007198}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007199
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007200static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7201{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007202 int i;
7203
7204 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7205 if (btrfs_dev_stat_read(dev, i) != 0)
7206 break;
7207 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7208 return; /* all values == 0, suppress message */
7209
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007210 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007211 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007212 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007213 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7214 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7215 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7216 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7217 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7218}
7219
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007220int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007221 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007222{
7223 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007224 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007225 int i;
7226
7227 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007228 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007229 mutex_unlock(&fs_devices->device_list_mutex);
7230
7231 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007232 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007233 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007234 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007235 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007236 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007237 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007238 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7239 if (stats->nr_items > i)
7240 stats->values[i] =
7241 btrfs_dev_stat_read_and_reset(dev, i);
7242 else
7243 btrfs_dev_stat_reset(dev, i);
7244 }
7245 } else {
7246 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7247 if (stats->nr_items > i)
7248 stats->values[i] = btrfs_dev_stat_read(dev, i);
7249 }
7250 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7251 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7252 return 0;
7253}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007254
David Sterbada353f62017-02-14 17:55:53 +01007255void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007256{
7257 struct buffer_head *bh;
7258 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007259 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007260
Anand Jain12b1c262015-08-14 18:32:59 +08007261 if (!bdev)
7262 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007263
Anand Jain12b1c262015-08-14 18:32:59 +08007264 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7265 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007266
Anand Jain12b1c262015-08-14 18:32:59 +08007267 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7268 continue;
7269
7270 disk_super = (struct btrfs_super_block *)bh->b_data;
7271
7272 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7273 set_buffer_dirty(bh);
7274 sync_dirty_buffer(bh);
7275 brelse(bh);
7276 }
7277
7278 /* Notify udev that device has changed */
7279 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7280
7281 /* Update ctime/mtime for device path for libblkid */
7282 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007283}
Miao Xie935e5cc2014-09-03 21:35:33 +08007284
7285/*
7286 * Update the size of all devices, which is used for writing out the
7287 * super blocks.
7288 */
7289void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7290{
7291 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7292 struct btrfs_device *curr, *next;
7293
7294 if (list_empty(&fs_devices->resized_devices))
7295 return;
7296
7297 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007298 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007299 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7300 resized_list) {
7301 list_del_init(&curr->resized_list);
7302 curr->commit_total_bytes = curr->disk_total_bytes;
7303 }
David Sterba34441362016-10-04 19:34:27 +02007304 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007305 mutex_unlock(&fs_devices->device_list_mutex);
7306}
Miao Xiece7213c2014-09-03 21:35:34 +08007307
7308/* Must be invoked during the transaction commit */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007309void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
Miao Xiece7213c2014-09-03 21:35:34 +08007310 struct btrfs_transaction *transaction)
7311{
7312 struct extent_map *em;
7313 struct map_lookup *map;
7314 struct btrfs_device *dev;
7315 int i;
7316
7317 if (list_empty(&transaction->pending_chunks))
7318 return;
7319
7320 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007321 mutex_lock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007322 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007323 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007324
7325 for (i = 0; i < map->num_stripes; i++) {
7326 dev = map->stripes[i].dev;
7327 dev->commit_bytes_used = dev->bytes_used;
7328 }
7329 }
David Sterba34441362016-10-04 19:34:27 +02007330 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007331}
Anand Jain5a13f432015-03-10 06:38:31 +08007332
7333void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7334{
7335 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7336 while (fs_devices) {
7337 fs_devices->fs_info = fs_info;
7338 fs_devices = fs_devices->seed;
7339 }
7340}
7341
7342void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7343{
7344 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7345 while (fs_devices) {
7346 fs_devices->fs_info = NULL;
7347 fs_devices = fs_devices->seed;
7348 }
7349}