blob: 6eec1e3f90833c894f1cfd0b7b54f4f7adafc5d8 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason0b86a832008-03-24 15:01:56 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason0b86a832008-03-24 15:01:56 -04004 */
David Sterbac1d7c512018-04-03 19:23:33 +02005
Chris Mason0b86a832008-03-24 15:01:56 -04006#include <linux/sched.h>
7#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -04009#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040010#include <linux/blkdev.h>
Chris Masonb765ead2009-04-03 10:27:10 -040011#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000012#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020013#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020014#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050015#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020016#include <linux/semaphore.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070017#include <linux/uuid.h>
Anand Jainf8e10cd2018-01-22 14:49:36 -080018#include <linux/list_sort.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050019#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040020#include "ctree.h"
21#include "extent_map.h"
22#include "disk-io.h"
23#include "transaction.h"
24#include "print-tree.h"
25#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050026#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040027#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010028#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040029#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060030#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010031#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080032#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040033
Zhao Leiaf902042015-09-15 21:08:06 +080034const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
35 [BTRFS_RAID_RAID10] = {
36 .sub_stripes = 2,
37 .dev_stripes = 1,
38 .devs_max = 0, /* 0 == as many as possible */
39 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080040 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080041 .devs_increment = 2,
42 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +080043 .raid_name = "raid10",
Anand Jain41a6e892018-04-25 19:01:43 +080044 .bg_flag = BTRFS_BLOCK_GROUP_RAID10,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080045 .mindev_error = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080046 },
47 [BTRFS_RAID_RAID1] = {
48 .sub_stripes = 1,
49 .dev_stripes = 1,
50 .devs_max = 2,
51 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080052 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080053 .devs_increment = 2,
54 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +080055 .raid_name = "raid1",
Anand Jain41a6e892018-04-25 19:01:43 +080056 .bg_flag = BTRFS_BLOCK_GROUP_RAID1,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080057 .mindev_error = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +080058 },
59 [BTRFS_RAID_DUP] = {
60 .sub_stripes = 1,
61 .dev_stripes = 2,
62 .devs_max = 1,
63 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080064 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080065 .devs_increment = 1,
66 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +080067 .raid_name = "dup",
Anand Jain41a6e892018-04-25 19:01:43 +080068 .bg_flag = BTRFS_BLOCK_GROUP_DUP,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080069 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080070 },
71 [BTRFS_RAID_RAID0] = {
72 .sub_stripes = 1,
73 .dev_stripes = 1,
74 .devs_max = 0,
75 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080076 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080077 .devs_increment = 1,
78 .ncopies = 1,
Anand Jained234672018-04-25 19:01:42 +080079 .raid_name = "raid0",
Anand Jain41a6e892018-04-25 19:01:43 +080080 .bg_flag = BTRFS_BLOCK_GROUP_RAID0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080081 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080082 },
83 [BTRFS_RAID_SINGLE] = {
84 .sub_stripes = 1,
85 .dev_stripes = 1,
86 .devs_max = 1,
87 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080088 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080089 .devs_increment = 1,
90 .ncopies = 1,
Anand Jained234672018-04-25 19:01:42 +080091 .raid_name = "single",
Anand Jain41a6e892018-04-25 19:01:43 +080092 .bg_flag = 0,
Anand Jainf9fbcaa2018-04-25 19:01:44 +080093 .mindev_error = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080094 },
95 [BTRFS_RAID_RAID5] = {
96 .sub_stripes = 1,
97 .dev_stripes = 1,
98 .devs_max = 0,
99 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800100 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +0800101 .devs_increment = 1,
102 .ncopies = 2,
Anand Jained234672018-04-25 19:01:42 +0800103 .raid_name = "raid5",
Anand Jain41a6e892018-04-25 19:01:43 +0800104 .bg_flag = BTRFS_BLOCK_GROUP_RAID5,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800105 .mindev_error = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800106 },
107 [BTRFS_RAID_RAID6] = {
108 .sub_stripes = 1,
109 .dev_stripes = 1,
110 .devs_max = 0,
111 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800112 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800113 .devs_increment = 1,
114 .ncopies = 3,
Anand Jained234672018-04-25 19:01:42 +0800115 .raid_name = "raid6",
Anand Jain41a6e892018-04-25 19:01:43 +0800116 .bg_flag = BTRFS_BLOCK_GROUP_RAID6,
Anand Jainf9fbcaa2018-04-25 19:01:44 +0800117 .mindev_error = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET,
Zhao Leiaf902042015-09-15 21:08:06 +0800118 },
119};
120
Anand Jained234672018-04-25 19:01:42 +0800121const char *get_raid_name(enum btrfs_raid_types type)
122{
123 if (type >= BTRFS_NR_RAID_TYPES)
124 return NULL;
125
126 return btrfs_raid_array[type].raid_name;
127}
128
Yan Zheng2b820322008-11-17 21:11:30 -0500129static int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +0100130 struct btrfs_fs_info *fs_info);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400131static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200132static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000133static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200134static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Liu Bo5ab56092017-03-14 13:33:57 -0700135static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
136 enum btrfs_map_op op,
137 u64 logical, u64 *length,
138 struct btrfs_bio **bbio_ret,
139 int mirror_num, int need_raid_map);
Yan Zheng2b820322008-11-17 21:11:30 -0500140
David Sterba9c6b1c42017-06-16 22:30:00 +0200141/*
142 * Device locking
143 * ==============
144 *
145 * There are several mutexes that protect manipulation of devices and low-level
146 * structures like chunks but not block groups, extents or files
147 *
148 * uuid_mutex (global lock)
149 * ------------------------
150 * protects the fs_uuids list that tracks all per-fs fs_devices, resulting from
151 * the SCAN_DEV ioctl registration or from mount either implicitly (the first
152 * device) or requested by the device= mount option
153 *
154 * the mutex can be very coarse and can cover long-running operations
155 *
156 * protects: updates to fs_devices counters like missing devices, rw devices,
157 * seeding, structure cloning, openning/closing devices at mount/umount time
158 *
159 * global::fs_devs - add, remove, updates to the global list
160 *
161 * does not protect: manipulation of the fs_devices::devices list!
162 *
163 * btrfs_device::name - renames (write side), read is RCU
164 *
165 * fs_devices::device_list_mutex (per-fs, with RCU)
166 * ------------------------------------------------
167 * protects updates to fs_devices::devices, ie. adding and deleting
168 *
169 * simple list traversal with read-only actions can be done with RCU protection
170 *
171 * may be used to exclude some operations from running concurrently without any
172 * modifications to the list (see write_all_supers)
173 *
David Sterba9c6b1c42017-06-16 22:30:00 +0200174 * balance_mutex
175 * -------------
176 * protects balance structures (status, state) and context accessed from
177 * several places (internally, ioctl)
178 *
179 * chunk_mutex
180 * -----------
181 * protects chunks, adding or removing during allocation, trim or when a new
182 * device is added/removed
183 *
184 * cleaner_mutex
185 * -------------
186 * a big lock that is held by the cleaner thread and prevents running subvolume
187 * cleaning together with relocation or delayed iputs
188 *
189 *
190 * Lock nesting
191 * ============
192 *
193 * uuid_mutex
194 * volume_mutex
195 * device_list_mutex
196 * chunk_mutex
197 * balance_mutex
Anand Jain89595e82018-04-18 14:59:25 +0800198 *
199 *
200 * Exclusive operations, BTRFS_FS_EXCL_OP
201 * ======================================
202 *
203 * Maintains the exclusivity of the following operations that apply to the
204 * whole filesystem and cannot run in parallel.
205 *
206 * - Balance (*)
207 * - Device add
208 * - Device remove
209 * - Device replace (*)
210 * - Resize
211 *
212 * The device operations (as above) can be in one of the following states:
213 *
214 * - Running state
215 * - Paused state
216 * - Completed state
217 *
218 * Only device operations marked with (*) can go into the Paused state for the
219 * following reasons:
220 *
221 * - ioctl (only Balance can be Paused through ioctl)
222 * - filesystem remounted as read-only
223 * - filesystem unmounted and mounted as read-only
224 * - system power-cycle and filesystem mounted as read-only
225 * - filesystem or device errors leading to forced read-only
226 *
227 * BTRFS_FS_EXCL_OP flag is set and cleared using atomic operations.
228 * During the course of Paused state, the BTRFS_FS_EXCL_OP remains set.
229 * A device operation in Paused or Running state can be canceled or resumed
230 * either by ioctl (Balance only) or when remounted as read-write.
231 * BTRFS_FS_EXCL_OP flag is cleared when the device operation is canceled or
232 * completed.
David Sterba9c6b1c42017-06-16 22:30:00 +0200233 */
234
Miao Xie67a2c452014-09-03 21:35:43 +0800235DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400236static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800237struct list_head *btrfs_get_fs_uuids(void)
238{
239 return &fs_uuids;
240}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400241
David Sterba2dfeca92017-06-14 02:48:07 +0200242/*
243 * alloc_fs_devices - allocate struct btrfs_fs_devices
244 * @fsid: if not NULL, copy the uuid to fs_devices::fsid
245 *
246 * Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
247 * The returned struct is not linked onto any lists and can be destroyed with
248 * kfree() right away.
249 */
250static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
Ilya Dryomov2208a372013-08-12 14:33:03 +0300251{
252 struct btrfs_fs_devices *fs_devs;
253
David Sterba78f2c9e2016-02-11 14:25:38 +0100254 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300255 if (!fs_devs)
256 return ERR_PTR(-ENOMEM);
257
258 mutex_init(&fs_devs->device_list_mutex);
259
260 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800261 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300262 INIT_LIST_HEAD(&fs_devs->alloc_list);
Anand Jainc4babc52018-04-12 10:29:25 +0800263 INIT_LIST_HEAD(&fs_devs->fs_list);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300264 if (fsid)
265 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300266
267 return fs_devs;
268}
269
David Sterbaa425f9d2018-03-20 15:47:33 +0100270void btrfs_free_device(struct btrfs_device *device)
David Sterba48dae9c2017-10-30 18:10:25 +0100271{
272 rcu_string_free(device->name);
273 bio_put(device->flush_bio);
274 kfree(device);
275}
276
Yan Zhenge4404d62008-12-12 10:03:26 -0500277static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
278{
279 struct btrfs_device *device;
280 WARN_ON(fs_devices->opened);
281 while (!list_empty(&fs_devices->devices)) {
282 device = list_entry(fs_devices->devices.next,
283 struct btrfs_device, dev_list);
284 list_del(&device->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100285 btrfs_free_device(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500286 }
287 kfree(fs_devices);
288}
289
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000290static void btrfs_kobject_uevent(struct block_device *bdev,
291 enum kobject_action action)
292{
293 int ret;
294
295 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
296 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500297 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000298 action,
299 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
300 &disk_to_dev(bdev->bd_disk)->kobj);
301}
302
David Sterbaffc5a372018-02-19 17:24:15 +0100303void __exit btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400304{
305 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400306
Yan Zheng2b820322008-11-17 21:11:30 -0500307 while (!list_empty(&fs_uuids)) {
308 fs_devices = list_entry(fs_uuids.next,
Anand Jainc4babc52018-04-12 10:29:25 +0800309 struct btrfs_fs_devices, fs_list);
310 list_del(&fs_devices->fs_list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500311 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400312 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400313}
314
David Sterba48dae9c2017-10-30 18:10:25 +0100315/*
316 * Returns a pointer to a new btrfs_device on success; ERR_PTR() on error.
317 * Returned struct is not linked onto any lists and must be destroyed using
David Sterbaa425f9d2018-03-20 15:47:33 +0100318 * btrfs_free_device.
David Sterba48dae9c2017-10-30 18:10:25 +0100319 */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300320static struct btrfs_device *__alloc_device(void)
321{
322 struct btrfs_device *dev;
323
David Sterba78f2c9e2016-02-11 14:25:38 +0100324 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300325 if (!dev)
326 return ERR_PTR(-ENOMEM);
327
David Sterbae0ae9992017-06-06 17:06:06 +0200328 /*
329 * Preallocate a bio that's always going to be used for flushing device
330 * barriers and matches the device lifespan
331 */
332 dev->flush_bio = bio_alloc_bioset(GFP_KERNEL, 0, NULL);
333 if (!dev->flush_bio) {
334 kfree(dev);
335 return ERR_PTR(-ENOMEM);
336 }
David Sterbae0ae9992017-06-06 17:06:06 +0200337
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300338 INIT_LIST_HEAD(&dev->dev_list);
339 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800340 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300341
342 spin_lock_init(&dev->io_lock);
343
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300344 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800345 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100346 btrfs_device_data_ordered_init(dev);
Chris Mason9bcaaea2017-05-04 16:08:08 -0700347 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Mel Gormand0164ad2015-11-06 16:28:21 -0800348 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300349
350 return dev;
351}
352
David Sterba35c70102017-06-15 19:51:51 +0200353/*
354 * Find a device specified by @devid or @uuid in the list of @fs_devices, or
355 * return NULL.
356 *
357 * If devid and uuid are both specified, the match must be exact, otherwise
358 * only devid is used.
359 */
360static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
361 u64 devid, const u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400362{
363 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400364
Anand Jain636d2c92018-04-12 10:29:29 +0800365 list_for_each_entry(dev, &fs_devices->devices, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400366 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400367 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400368 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400369 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400370 }
371 return NULL;
372}
373
Chris Masona1b32a52008-09-05 16:09:51 -0400374static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400375{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400376 struct btrfs_fs_devices *fs_devices;
377
Anand Jainc4babc52018-04-12 10:29:25 +0800378 list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400379 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
380 return fs_devices;
381 }
382 return NULL;
383}
384
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100385static int
386btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
387 int flush, struct block_device **bdev,
388 struct buffer_head **bh)
389{
390 int ret;
391
392 *bdev = blkdev_get_by_path(device_path, flags, holder);
393
394 if (IS_ERR(*bdev)) {
395 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100396 goto error;
397 }
398
399 if (flush)
400 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
David Sterba9f6d2512017-06-16 01:48:05 +0200401 ret = set_blocksize(*bdev, BTRFS_BDEV_BLOCKSIZE);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100402 if (ret) {
403 blkdev_put(*bdev, flags);
404 goto error;
405 }
406 invalidate_bdev(*bdev);
407 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800408 if (IS_ERR(*bh)) {
409 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100410 blkdev_put(*bdev, flags);
411 goto error;
412 }
413
414 return 0;
415
416error:
417 *bdev = NULL;
418 *bh = NULL;
419 return ret;
420}
421
Chris Masonffbd5172009-04-20 15:50:09 -0400422static void requeue_list(struct btrfs_pending_bios *pending_bios,
423 struct bio *head, struct bio *tail)
424{
425
426 struct bio *old_head;
427
428 old_head = pending_bios->head;
429 pending_bios->head = head;
430 if (pending_bios->tail)
431 tail->bi_next = old_head;
432 else
433 pending_bios->tail = tail;
434}
435
Chris Mason8b712842008-06-11 16:50:36 -0400436/*
437 * we try to collect pending bios for a device so we don't get a large
438 * number of procs sending bios down to the same device. This greatly
439 * improves the schedulers ability to collect and merge the bios.
440 *
441 * But, it also turns into a long list of bios to process and that is sure
442 * to eventually make the worker thread block. The solution here is to
443 * make some progress and then put this work struct back at the end of
444 * the list if the block device is congested. This way, multiple devices
445 * can make progress from a single worker thread.
446 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100447static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400448{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400449 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -0400450 struct bio *pending;
451 struct backing_dev_info *bdi;
Chris Masonffbd5172009-04-20 15:50:09 -0400452 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400453 struct bio *tail;
454 struct bio *cur;
455 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400456 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400457 unsigned long batch_run = 0;
Chris Masonb765ead2009-04-03 10:27:10 -0400458 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400459 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000460 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400461 struct blk_plug plug;
462
463 /*
464 * this function runs all the bios we've collected for
465 * a particular device. We don't want to wander off to
466 * another device without first sending all of these down.
467 * So, setup a plug here and finish it off before we return
468 */
469 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400470
Jan Karaefa7c9f2017-02-02 15:56:53 +0100471 bdi = device->bdev->bd_bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400472
Chris Mason8b712842008-06-11 16:50:36 -0400473loop:
474 spin_lock(&device->io_lock);
475
Chris Masona6837052009-02-04 09:19:41 -0500476loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400477 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400478
Chris Mason8b712842008-06-11 16:50:36 -0400479 /* take all the bios off the list at once and process them
480 * later on (without the lock held). But, remember the
481 * tail and other pointers so the bios can be properly reinserted
482 * into the list if we hit congestion
483 */
Chris Masond84275c2009-06-09 15:39:08 -0400484 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400485 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400486 force_reg = 1;
487 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400488 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400489 force_reg = 0;
490 }
Chris Masonffbd5172009-04-20 15:50:09 -0400491
492 pending = pending_bios->head;
493 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400494 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400495
496 /*
497 * if pending was null this time around, no bios need processing
498 * at all and we can stop. Otherwise it'll loop back up again
499 * and do an additional check so no bios are missed.
500 *
501 * device->running_pending is used to synchronize with the
502 * schedule_bio code.
503 */
Chris Masonffbd5172009-04-20 15:50:09 -0400504 if (device->pending_sync_bios.head == NULL &&
505 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400506 again = 0;
507 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400508 } else {
509 again = 1;
510 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400511 }
Chris Masonffbd5172009-04-20 15:50:09 -0400512
513 pending_bios->head = NULL;
514 pending_bios->tail = NULL;
515
Chris Mason8b712842008-06-11 16:50:36 -0400516 spin_unlock(&device->io_lock);
517
Chris Masond3977122009-01-05 21:25:51 -0500518 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400519
520 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400521 /* we want to work on both lists, but do more bios on the
522 * sync list than the regular list
523 */
524 if ((num_run > 32 &&
525 pending_bios != &device->pending_sync_bios &&
526 device->pending_sync_bios.head) ||
527 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
528 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400529 spin_lock(&device->io_lock);
530 requeue_list(pending_bios, pending, tail);
531 goto loop_lock;
532 }
533
Chris Mason8b712842008-06-11 16:50:36 -0400534 cur = pending;
535 pending = pending->bi_next;
536 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400537
Jens Axboedac56212015-04-17 16:23:59 -0600538 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400539
Chris Mason2ab1ba62011-08-04 14:28:36 -0400540 /*
541 * if we're doing the sync list, record that our
542 * plug has some sync requests on it
543 *
544 * If we're doing the regular list and there are
545 * sync requests sitting around, unplug before
546 * we add more
547 */
548 if (pending_bios == &device->pending_sync_bios) {
549 sync_pending = 1;
550 } else if (sync_pending) {
551 blk_finish_plug(&plug);
552 blk_start_plug(&plug);
553 sync_pending = 0;
554 }
555
Mike Christie4e49ea42016-06-05 14:31:41 -0500556 btrfsic_submit_bio(cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400557 num_run++;
558 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100559
560 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400561
562 /*
563 * we made progress, there is more work to do and the bdi
564 * is now congested. Back off and let other work structs
565 * run instead
566 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400567 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500568 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400569 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400570
Chris Masonb765ead2009-04-03 10:27:10 -0400571 ioc = current->io_context;
572
573 /*
574 * the main goal here is that we don't want to
575 * block if we're going to be able to submit
576 * more requests without blocking.
577 *
578 * This code does two great things, it pokes into
579 * the elevator code from a filesystem _and_
580 * it makes assumptions about how batching works.
581 */
582 if (ioc && ioc->nr_batch_requests > 0 &&
583 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
584 (last_waited == 0 ||
585 ioc->last_waited == last_waited)) {
586 /*
587 * we want to go through our batch of
588 * requests and stop. So, we copy out
589 * the ioc->last_waited time and test
590 * against it before looping
591 */
592 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100593 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400594 continue;
595 }
Chris Mason8b712842008-06-11 16:50:36 -0400596 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400597 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500598 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400599
600 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800601 btrfs_queue_work(fs_info->submit_workers,
602 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400603 goto done;
604 }
605 }
Chris Masonffbd5172009-04-20 15:50:09 -0400606
Chris Mason51684082010-03-10 15:33:32 -0500607 cond_resched();
608 if (again)
609 goto loop;
610
611 spin_lock(&device->io_lock);
612 if (device->pending_bios.head || device->pending_sync_bios.head)
613 goto loop_lock;
614 spin_unlock(&device->io_lock);
615
Chris Mason8b712842008-06-11 16:50:36 -0400616done:
Chris Mason211588a2011-04-19 20:12:40 -0400617 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400618}
619
Christoph Hellwigb2950862008-12-02 09:54:17 -0500620static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400621{
622 struct btrfs_device *device;
623
624 device = container_of(work, struct btrfs_device, work);
625 run_scheduled_bios(device);
626}
627
Anand Jaind8367db2018-01-18 22:00:37 +0800628/*
629 * Search and remove all stale (devices which are not mounted) devices.
630 * When both inputs are NULL, it will search and release all stale devices.
631 * path: Optional. When provided will it release all unmounted devices
632 * matching this path only.
633 * skip_dev: Optional. Will skip this device when searching for the stale
634 * devices.
635 */
636static void btrfs_free_stale_devices(const char *path,
637 struct btrfs_device *skip_dev)
Anand Jain4fde46f2015-06-17 21:10:48 +0800638{
Anand Jain38cf6652018-01-18 22:00:34 +0800639 struct btrfs_fs_devices *fs_devs, *tmp_fs_devs;
640 struct btrfs_device *dev, *tmp_dev;
Anand Jain4fde46f2015-06-17 21:10:48 +0800641
Anand Jainc4babc52018-04-12 10:29:25 +0800642 list_for_each_entry_safe(fs_devs, tmp_fs_devs, &fs_uuids, fs_list) {
Anand Jain4fde46f2015-06-17 21:10:48 +0800643
644 if (fs_devs->opened)
645 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800646
Anand Jain38cf6652018-01-18 22:00:34 +0800647 list_for_each_entry_safe(dev, tmp_dev,
648 &fs_devs->devices, dev_list) {
Anand Jain522f1b42018-01-18 22:00:35 +0800649 int not_found = 0;
Anand Jain4fde46f2015-06-17 21:10:48 +0800650
Anand Jaind8367db2018-01-18 22:00:37 +0800651 if (skip_dev && skip_dev == dev)
652 continue;
653 if (path && !dev->name)
Anand Jain4fde46f2015-06-17 21:10:48 +0800654 continue;
655
Anand Jain4fde46f2015-06-17 21:10:48 +0800656 rcu_read_lock();
Anand Jaind8367db2018-01-18 22:00:37 +0800657 if (path)
Anand Jain522f1b42018-01-18 22:00:35 +0800658 not_found = strcmp(rcu_str_deref(dev->name),
Anand Jaind8367db2018-01-18 22:00:37 +0800659 path);
Anand Jain4fde46f2015-06-17 21:10:48 +0800660 rcu_read_unlock();
Anand Jain38cf6652018-01-18 22:00:34 +0800661 if (not_found)
662 continue;
Anand Jain4fde46f2015-06-17 21:10:48 +0800663
Anand Jain4fde46f2015-06-17 21:10:48 +0800664 /* delete the stale device */
665 if (fs_devs->num_devices == 1) {
666 btrfs_sysfs_remove_fsid(fs_devs);
Anand Jainc4babc52018-04-12 10:29:25 +0800667 list_del(&fs_devs->fs_list);
Anand Jain4fde46f2015-06-17 21:10:48 +0800668 free_fs_devices(fs_devs);
Nikolay Borisovfd649f12018-01-30 16:07:37 +0200669 break;
Anand Jain4fde46f2015-06-17 21:10:48 +0800670 } else {
671 fs_devs->num_devices--;
672 list_del(&dev->dev_list);
David Sterbaa425f9d2018-03-20 15:47:33 +0100673 btrfs_free_device(dev);
Anand Jain4fde46f2015-06-17 21:10:48 +0800674 }
Anand Jain4fde46f2015-06-17 21:10:48 +0800675 }
676 }
677}
678
Anand Jain0fb08bc2017-11-09 23:45:24 +0800679static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
680 struct btrfs_device *device, fmode_t flags,
681 void *holder)
682{
683 struct request_queue *q;
684 struct block_device *bdev;
685 struct buffer_head *bh;
686 struct btrfs_super_block *disk_super;
687 u64 devid;
688 int ret;
689
690 if (device->bdev)
691 return -EINVAL;
692 if (!device->name)
693 return -EINVAL;
694
695 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
696 &bdev, &bh);
697 if (ret)
698 return ret;
699
700 disk_super = (struct btrfs_super_block *)bh->b_data;
701 devid = btrfs_stack_device_id(&disk_super->dev_item);
702 if (devid != device->devid)
703 goto error_brelse;
704
705 if (memcmp(device->uuid, disk_super->dev_item.uuid, BTRFS_UUID_SIZE))
706 goto error_brelse;
707
708 device->generation = btrfs_super_generation(disk_super);
709
710 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
Anand Jainebbede42017-12-04 12:54:52 +0800711 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800712 fs_devices->seeding = 1;
713 } else {
Anand Jainebbede42017-12-04 12:54:52 +0800714 if (bdev_read_only(bdev))
715 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
716 else
717 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800718 }
719
720 q = bdev_get_queue(bdev);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800721 if (!blk_queue_nonrot(q))
722 fs_devices->rotating = 1;
723
724 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +0800725 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800726 device->mode = flags;
727
728 fs_devices->open_devices++;
Anand Jainebbede42017-12-04 12:54:52 +0800729 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
730 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Anand Jain0fb08bc2017-11-09 23:45:24 +0800731 fs_devices->rw_devices++;
Anand Jainb1b8e382018-01-22 14:49:37 -0800732 list_add_tail(&device->dev_alloc_list, &fs_devices->alloc_list);
Anand Jain0fb08bc2017-11-09 23:45:24 +0800733 }
734 brelse(bh);
735
736 return 0;
737
738error_brelse:
739 brelse(bh);
740 blkdev_put(bdev, flags);
741
742 return -EINVAL;
743}
744
David Sterba60999ca2014-03-26 18:26:36 +0100745/*
746 * Add new device to list of registered devices
747 *
748 * Returns:
Anand Jaine124ece2018-01-18 22:02:35 +0800749 * device pointer which was just added or updated when successful
750 * error pointer when failed
David Sterba60999ca2014-03-26 18:26:36 +0100751 */
Anand Jaine124ece2018-01-18 22:02:35 +0800752static noinline struct btrfs_device *device_list_add(const char *path,
Anand Jain3acbcbf2018-01-18 22:02:36 +0800753 struct btrfs_super_block *disk_super)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400754{
755 struct btrfs_device *device;
756 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400757 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400758 u64 found_transid = btrfs_super_generation(disk_super);
Anand Jain3acbcbf2018-01-18 22:02:36 +0800759 u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400760
761 fs_devices = find_fsid(disk_super->fsid);
762 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300763 fs_devices = alloc_fs_devices(disk_super->fsid);
764 if (IS_ERR(fs_devices))
Anand Jaine124ece2018-01-18 22:02:35 +0800765 return ERR_CAST(fs_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300766
Anand Jainc4babc52018-04-12 10:29:25 +0800767 list_add(&fs_devices->fs_list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300768
Chris Mason8a4b83c2008-03-24 15:02:07 -0400769 device = NULL;
770 } else {
David Sterba35c70102017-06-15 19:51:51 +0200771 device = find_device(fs_devices, devid,
772 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400773 }
Miao Xie443f24f2014-07-24 11:37:15 +0800774
Chris Mason8a4b83c2008-03-24 15:02:07 -0400775 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500776 if (fs_devices->opened)
Anand Jaine124ece2018-01-18 22:02:35 +0800777 return ERR_PTR(-EBUSY);
Yan Zheng2b820322008-11-17 21:11:30 -0500778
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300779 device = btrfs_alloc_device(NULL, &devid,
780 disk_super->dev_item.uuid);
781 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400782 /* we can safely leave the fs_devices entry around */
Anand Jaine124ece2018-01-18 22:02:35 +0800783 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400784 }
Josef Bacik606686e2012-06-04 14:03:51 -0400785
786 name = rcu_string_strdup(path, GFP_NOFS);
787 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100788 btrfs_free_device(device);
Anand Jaine124ece2018-01-18 22:02:35 +0800789 return ERR_PTR(-ENOMEM);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400790 }
Josef Bacik606686e2012-06-04 14:03:51 -0400791 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200792
Chris Masone5e9a522009-06-10 15:17:02 -0400793 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000794 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100795 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400796 mutex_unlock(&fs_devices->device_list_mutex);
797
Yan Zheng2b820322008-11-17 21:11:30 -0500798 device->fs_devices = fs_devices;
Anand Jaind8367db2018-01-18 22:00:37 +0800799 btrfs_free_stale_devices(path, device);
Anand Jain327f18c2018-01-18 22:02:33 +0800800
801 if (disk_super->label[0])
802 pr_info("BTRFS: device label %s devid %llu transid %llu %s\n",
803 disk_super->label, devid, found_transid, path);
804 else
805 pr_info("BTRFS: device fsid %pU devid %llu transid %llu %s\n",
806 disk_super->fsid, devid, found_transid, path);
807
Josef Bacik606686e2012-06-04 14:03:51 -0400808 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800809 /*
810 * When FS is already mounted.
811 * 1. If you are here and if the device->name is NULL that
812 * means this device was missing at time of FS mount.
813 * 2. If you are here and if the device->name is different
814 * from 'path' that means either
815 * a. The same device disappeared and reappeared with
816 * different name. or
817 * b. The missing-disk-which-was-replaced, has
818 * reappeared now.
819 *
820 * We must allow 1 and 2a above. But 2b would be a spurious
821 * and unintentional.
822 *
823 * Further in case of 1 and 2a above, the disk at 'path'
824 * would have missed some transaction when it was away and
825 * in case of 2a the stale bdev has to be updated as well.
826 * 2b must not be allowed at all time.
827 */
828
829 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700830 * For now, we do allow update to btrfs_fs_device through the
831 * btrfs dev scan cli after FS has been mounted. We're still
832 * tracking a problem where systems fail mount by subvolume id
833 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800834 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700835 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800836 /*
837 * That is if the FS is _not_ mounted and if you
838 * are here, that means there is more than one
839 * disk with same uuid and devid.We keep the one
840 * with larger generation number or the last-in if
841 * generation are equal.
842 */
Anand Jaine124ece2018-01-18 22:02:35 +0800843 return ERR_PTR(-EEXIST);
Anand Jain77bdae42014-07-03 18:22:06 +0800844 }
Anand Jainb96de002014-07-03 18:22:05 +0800845
Josef Bacik606686e2012-06-04 14:03:51 -0400846 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000847 if (!name)
Anand Jaine124ece2018-01-18 22:02:35 +0800848 return ERR_PTR(-ENOMEM);
Josef Bacik606686e2012-06-04 14:03:51 -0400849 rcu_string_free(device->name);
850 rcu_assign_pointer(device->name, name);
Anand Jaine6e674b2017-12-04 12:54:54 +0800851 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -0500852 fs_devices->missing_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +0800853 clear_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -0500854 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400855 }
856
Anand Jain77bdae42014-07-03 18:22:06 +0800857 /*
858 * Unmount does not free the btrfs_device struct but would zero
859 * generation along with most of the other members. So just update
860 * it back. We need it to pick the disk with largest generation
861 * (as above).
862 */
863 if (!fs_devices->opened)
864 device->generation = found_transid;
865
Anand Jainf2788d22018-01-18 22:02:34 +0800866 fs_devices->total_devices = btrfs_super_num_devices(disk_super);
867
Anand Jaine124ece2018-01-18 22:02:35 +0800868 return device;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400869}
870
Yan Zhenge4404d62008-12-12 10:03:26 -0500871static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
872{
873 struct btrfs_fs_devices *fs_devices;
874 struct btrfs_device *device;
875 struct btrfs_device *orig_dev;
876
Ilya Dryomov2208a372013-08-12 14:33:03 +0300877 fs_devices = alloc_fs_devices(orig->fsid);
878 if (IS_ERR(fs_devices))
879 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500880
Miao Xieadbbb862014-09-03 21:35:42 +0800881 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400882 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500883
Xiao Guangrong46224702011-04-20 10:08:47 +0000884 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500885 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400886 struct rcu_string *name;
887
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300888 device = btrfs_alloc_device(NULL, &orig_dev->devid,
889 orig_dev->uuid);
890 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500891 goto error;
892
Josef Bacik606686e2012-06-04 14:03:51 -0400893 /*
894 * This is ok to do without rcu read locked because we hold the
895 * uuid mutex so nothing we touch in here is going to disappear.
896 */
Anand Jaine755f782014-06-30 17:12:47 +0800897 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100898 name = rcu_string_strdup(orig_dev->name->str,
899 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800900 if (!name) {
David Sterbaa425f9d2018-03-20 15:47:33 +0100901 btrfs_free_device(device);
Anand Jaine755f782014-06-30 17:12:47 +0800902 goto error;
903 }
904 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400905 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500906
Yan Zhenge4404d62008-12-12 10:03:26 -0500907 list_add(&device->dev_list, &fs_devices->devices);
908 device->fs_devices = fs_devices;
909 fs_devices->num_devices++;
910 }
Miao Xieadbbb862014-09-03 21:35:42 +0800911 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500912 return fs_devices;
913error:
Miao Xieadbbb862014-09-03 21:35:42 +0800914 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500915 free_fs_devices(fs_devices);
916 return ERR_PTR(-ENOMEM);
917}
918
Anand Jain9b99b112018-02-27 12:41:59 +0800919/*
920 * After we have read the system tree and know devids belonging to
921 * this filesystem, remove the device which does not belong there.
922 */
923void btrfs_free_extra_devids(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400924{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500925 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800926 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500927
Chris Masondfe25022008-05-13 13:46:40 -0400928 mutex_lock(&uuid_mutex);
929again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000930 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500931 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +0800932 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
933 &device->dev_state)) {
Anand Jain401e29c2017-12-04 12:54:55 +0800934 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
935 &device->dev_state) &&
936 (!latest_dev ||
937 device->generation > latest_dev->generation)) {
Miao Xie443f24f2014-07-24 11:37:15 +0800938 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500939 }
Yan Zheng2b820322008-11-17 21:11:30 -0500940 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500941 }
Yan Zheng2b820322008-11-17 21:11:30 -0500942
Stefan Behrens8dabb742012-11-06 13:15:27 +0100943 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
944 /*
945 * In the first step, keep the device which has
946 * the correct fsid and the devid that is used
947 * for the dev_replace procedure.
948 * In the second step, the dev_replace state is
949 * read from the device tree and it is known
950 * whether the procedure is really active or
951 * not, which means whether this device is
952 * used or whether it should be removed.
953 */
Anand Jain401e29c2017-12-04 12:54:55 +0800954 if (step == 0 || test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
955 &device->dev_state)) {
Stefan Behrens8dabb742012-11-06 13:15:27 +0100956 continue;
957 }
958 }
Yan Zheng2b820322008-11-17 21:11:30 -0500959 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100960 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500961 device->bdev = NULL;
962 fs_devices->open_devices--;
963 }
Anand Jainebbede42017-12-04 12:54:52 +0800964 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -0500965 list_del_init(&device->dev_alloc_list);
Anand Jainebbede42017-12-04 12:54:52 +0800966 clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +0800967 if (!test_bit(BTRFS_DEV_STATE_REPLACE_TGT,
968 &device->dev_state))
Stefan Behrens8dabb742012-11-06 13:15:27 +0100969 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500970 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500971 list_del_init(&device->dev_list);
972 fs_devices->num_devices--;
David Sterbaa425f9d2018-03-20 15:47:33 +0100973 btrfs_free_device(device);
Chris Masondfe25022008-05-13 13:46:40 -0400974 }
Yan Zheng2b820322008-11-17 21:11:30 -0500975
976 if (fs_devices->seed) {
977 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500978 goto again;
979 }
980
Miao Xie443f24f2014-07-24 11:37:15 +0800981 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500982
Chris Masondfe25022008-05-13 13:46:40 -0400983 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400984}
Chris Masona0af4692008-05-13 16:03:06 -0400985
David Sterbaf06c5962017-06-06 17:08:23 +0200986static void free_device_rcu(struct rcu_head *head)
Xiao Guangrong1f781602011-04-20 10:09:16 +0000987{
988 struct btrfs_device *device;
989
990 device = container_of(head, struct btrfs_device, rcu);
David Sterbaa425f9d2018-03-20 15:47:33 +0100991 btrfs_free_device(device);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000992}
993
Anand Jain14238812016-07-22 06:04:53 +0800994static void btrfs_close_bdev(struct btrfs_device *device)
995{
David Sterba08ffcae2017-06-19 16:55:35 +0200996 if (!device->bdev)
997 return;
998
Anand Jainebbede42017-12-04 12:54:52 +0800999 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Anand Jain14238812016-07-22 06:04:53 +08001000 sync_blockdev(device->bdev);
1001 invalidate_bdev(device->bdev);
1002 }
1003
David Sterba08ffcae2017-06-19 16:55:35 +02001004 blkdev_put(device->bdev, device->mode);
Anand Jain14238812016-07-22 06:04:53 +08001005}
1006
Anand Jain0ccd0522016-09-22 12:56:13 +08001007static void btrfs_prepare_close_one_device(struct btrfs_device *device)
Anand Jainf448341a2016-06-14 18:55:25 +08001008{
1009 struct btrfs_fs_devices *fs_devices = device->fs_devices;
1010 struct btrfs_device *new_device;
1011 struct rcu_string *name;
1012
1013 if (device->bdev)
1014 fs_devices->open_devices--;
1015
Anand Jainebbede42017-12-04 12:54:52 +08001016 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jainf448341a2016-06-14 18:55:25 +08001017 device->devid != BTRFS_DEV_REPLACE_DEVID) {
1018 list_del_init(&device->dev_alloc_list);
1019 fs_devices->rw_devices--;
1020 }
1021
Anand Jaine6e674b2017-12-04 12:54:54 +08001022 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jainf448341a2016-06-14 18:55:25 +08001023 fs_devices->missing_devices--;
1024
1025 new_device = btrfs_alloc_device(NULL, &device->devid,
1026 device->uuid);
1027 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
1028
1029 /* Safe because we are under uuid_mutex */
1030 if (device->name) {
1031 name = rcu_string_strdup(device->name->str, GFP_NOFS);
1032 BUG_ON(!name); /* -ENOMEM */
1033 rcu_assign_pointer(new_device->name, name);
1034 }
1035
1036 list_replace_rcu(&device->dev_list, &new_device->dev_list);
1037 new_device->fs_devices = device->fs_devices;
Anand Jainf448341a2016-06-14 18:55:25 +08001038}
1039
Anand Jain0226e0e2018-04-12 10:29:27 +08001040static int close_fs_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001041{
Sasha Levin2037a092015-05-12 19:31:37 -04001042 struct btrfs_device *device, *tmp;
Anand Jain0ccd0522016-09-22 12:56:13 +08001043 struct list_head pending_put;
1044
1045 INIT_LIST_HEAD(&pending_put);
Yan Zhenge4404d62008-12-12 10:03:26 -05001046
Yan Zheng2b820322008-11-17 21:11:30 -05001047 if (--fs_devices->opened > 0)
1048 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001049
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001050 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -04001051 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jain0ccd0522016-09-22 12:56:13 +08001052 btrfs_prepare_close_one_device(device);
1053 list_add(&device->dev_list, &pending_put);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001054 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001055 mutex_unlock(&fs_devices->device_list_mutex);
1056
Anand Jain0ccd0522016-09-22 12:56:13 +08001057 /*
1058 * btrfs_show_devname() is using the device_list_mutex,
1059 * sometimes call to blkdev_put() leads vfs calling
1060 * into this func. So do put outside of device_list_mutex,
1061 * as of now.
1062 */
1063 while (!list_empty(&pending_put)) {
1064 device = list_first_entry(&pending_put,
1065 struct btrfs_device, dev_list);
1066 list_del(&device->dev_list);
1067 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02001068 call_rcu(&device->rcu, free_device_rcu);
Anand Jain0ccd0522016-09-22 12:56:13 +08001069 }
1070
Yan Zhenge4404d62008-12-12 10:03:26 -05001071 WARN_ON(fs_devices->open_devices);
1072 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001073 fs_devices->opened = 0;
1074 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001075
Chris Mason8a4b83c2008-03-24 15:02:07 -04001076 return 0;
1077}
1078
Yan Zheng2b820322008-11-17 21:11:30 -05001079int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
1080{
Yan Zhenge4404d62008-12-12 10:03:26 -05001081 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -05001082 int ret;
1083
1084 mutex_lock(&uuid_mutex);
Anand Jain0226e0e2018-04-12 10:29:27 +08001085 ret = close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001086 if (!fs_devices->opened) {
1087 seed_devices = fs_devices->seed;
1088 fs_devices->seed = NULL;
1089 }
Yan Zheng2b820322008-11-17 21:11:30 -05001090 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001091
1092 while (seed_devices) {
1093 fs_devices = seed_devices;
1094 seed_devices = fs_devices->seed;
Anand Jain0226e0e2018-04-12 10:29:27 +08001095 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05001096 free_fs_devices(fs_devices);
1097 }
Yan Zheng2b820322008-11-17 21:11:30 -05001098 return ret;
1099}
1100
Anand Jain897fb572018-04-12 10:29:28 +08001101static int open_fs_devices(struct btrfs_fs_devices *fs_devices,
Yan Zhenge4404d62008-12-12 10:03:26 -05001102 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -04001103{
Chris Mason8a4b83c2008-03-24 15:02:07 -04001104 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +08001105 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -04001106 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001107
Tejun Heod4d77622010-11-13 11:55:18 +01001108 flags |= FMODE_EXCL;
1109
Anand Jainf117e292018-04-12 10:29:26 +08001110 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Eric Sandeenf63e0cc2013-04-04 20:45:08 +00001111 /* Just open everything we can; ignore failures here */
Anand Jain0fb08bc2017-11-09 23:45:24 +08001112 if (btrfs_open_one_device(fs_devices, device, flags, holder))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001113 continue;
Chris Masona0af4692008-05-13 16:03:06 -04001114
Anand Jain9f050db2017-11-09 23:45:25 +08001115 if (!latest_dev ||
1116 device->generation > latest_dev->generation)
1117 latest_dev = device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001118 }
Chris Masona0af4692008-05-13 16:03:06 -04001119 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +03001120 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -04001121 goto out;
1122 }
Yan Zheng2b820322008-11-17 21:11:30 -05001123 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +08001124 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -05001125 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -04001126out:
Yan Zheng2b820322008-11-17 21:11:30 -05001127 return ret;
1128}
1129
Anand Jainf8e10cd2018-01-22 14:49:36 -08001130static int devid_cmp(void *priv, struct list_head *a, struct list_head *b)
1131{
1132 struct btrfs_device *dev1, *dev2;
1133
1134 dev1 = list_entry(a, struct btrfs_device, dev_list);
1135 dev2 = list_entry(b, struct btrfs_device, dev_list);
1136
1137 if (dev1->devid < dev2->devid)
1138 return -1;
1139 else if (dev1->devid > dev2->devid)
1140 return 1;
1141 return 0;
1142}
1143
Yan Zheng2b820322008-11-17 21:11:30 -05001144int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001145 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001146{
1147 int ret;
1148
1149 mutex_lock(&uuid_mutex);
1150 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001151 fs_devices->opened++;
1152 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001153 } else {
Anand Jainf8e10cd2018-01-22 14:49:36 -08001154 list_sort(NULL, &fs_devices->devices, devid_cmp);
Anand Jain897fb572018-04-12 10:29:28 +08001155 ret = open_fs_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001156 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001157 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001158 return ret;
1159}
1160
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001161static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001162{
1163 kunmap(page);
1164 put_page(page);
1165}
1166
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001167static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1168 struct page **page,
1169 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001170{
1171 void *p;
1172 pgoff_t index;
1173
1174 /* make sure our super fits in the device */
1175 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1176 return 1;
1177
1178 /* make sure our super fits in the page */
1179 if (sizeof(**disk_super) > PAGE_SIZE)
1180 return 1;
1181
1182 /* make sure our super doesn't straddle pages on disk */
1183 index = bytenr >> PAGE_SHIFT;
1184 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1185 return 1;
1186
1187 /* pull in the page with our super */
1188 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1189 index, GFP_KERNEL);
1190
1191 if (IS_ERR_OR_NULL(*page))
1192 return 1;
1193
1194 p = kmap(*page);
1195
1196 /* align our pointer to the offset of the super block */
1197 *disk_super = p + (bytenr & ~PAGE_MASK);
1198
1199 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1200 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1201 btrfs_release_disk_super(*page);
1202 return 1;
1203 }
1204
1205 if ((*disk_super)->label[0] &&
1206 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1207 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1208
1209 return 0;
1210}
1211
David Sterba6f60cbd2013-02-15 11:31:02 -07001212/*
1213 * Look for a btrfs signature on a device. This may be called out of the mount path
1214 * and we are not allowed to call set_blocksize during the scan. The superblock
1215 * is read via pagecache
1216 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001217int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001218 struct btrfs_fs_devices **fs_devices_ret)
1219{
1220 struct btrfs_super_block *disk_super;
Anand Jaine124ece2018-01-18 22:02:35 +08001221 struct btrfs_device *device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001222 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001223 struct page *page;
Anand Jaine124ece2018-01-18 22:02:35 +08001224 int ret = 0;
David Sterba6f60cbd2013-02-15 11:31:02 -07001225 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001226
David Sterba6f60cbd2013-02-15 11:31:02 -07001227 /*
1228 * we would like to check all the supers, but that would make
1229 * a btrfs mount succeed after a mkfs from a different FS.
1230 * So, we need to add a special mount option to scan for
1231 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1232 */
1233 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001234 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001235
1236 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001237 if (IS_ERR(bdev))
1238 return PTR_ERR(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001239
Anand Jain05a5c552017-12-15 15:40:16 +08001240 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1241 ret = -EINVAL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001242 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001243 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001244
Anand Jainb6ed73b2018-04-12 10:29:24 +08001245 mutex_lock(&uuid_mutex);
Anand Jain3acbcbf2018-01-18 22:02:36 +08001246 device = device_list_add(path, disk_super);
Anand Jaine124ece2018-01-18 22:02:35 +08001247 if (IS_ERR(device))
1248 ret = PTR_ERR(device);
1249 else
1250 *fs_devices_ret = device->fs_devices;
Anand Jainb6ed73b2018-04-12 10:29:24 +08001251 mutex_unlock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001252
Anand Jain6cf86a002016-02-13 10:01:29 +08001253 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001254
1255error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001256 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001257
Chris Mason8a4b83c2008-03-24 15:02:07 -04001258 return ret;
1259}
Chris Mason0b86a832008-03-24 15:01:56 -04001260
Miao Xie6d07bce2011-01-05 10:07:31 +00001261/* helper to account the used device space in the range */
1262int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1263 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001264{
1265 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001266 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001267 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001268 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001269 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001270 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001271 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001272 struct extent_buffer *l;
1273
Miao Xie6d07bce2011-01-05 10:07:31 +00001274 *length = 0;
1275
Anand Jain401e29c2017-12-04 12:54:55 +08001276 if (start >= device->total_bytes ||
1277 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Miao Xie6d07bce2011-01-05 10:07:31 +00001278 return 0;
1279
Yan Zheng2b820322008-11-17 21:11:30 -05001280 path = btrfs_alloc_path();
1281 if (!path)
1282 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001283 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001284
Chris Mason0b86a832008-03-24 15:01:56 -04001285 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001286 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001287 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001288
1289 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001290 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001291 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001292 if (ret > 0) {
1293 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1294 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001295 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001296 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001297
Chris Mason0b86a832008-03-24 15:01:56 -04001298 while (1) {
1299 l = path->nodes[0];
1300 slot = path->slots[0];
1301 if (slot >= btrfs_header_nritems(l)) {
1302 ret = btrfs_next_leaf(root, path);
1303 if (ret == 0)
1304 continue;
1305 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001306 goto out;
1307
1308 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001309 }
1310 btrfs_item_key_to_cpu(l, &key, slot);
1311
1312 if (key.objectid < device->devid)
1313 goto next;
1314
1315 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001316 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001317
David Sterba962a2982014-06-04 18:41:45 +02001318 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001319 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001320
Chris Mason0b86a832008-03-24 15:01:56 -04001321 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001322 extent_end = key.offset + btrfs_dev_extent_length(l,
1323 dev_extent);
1324 if (key.offset <= start && extent_end > end) {
1325 *length = end - start + 1;
1326 break;
1327 } else if (key.offset <= start && extent_end > start)
1328 *length += extent_end - start;
1329 else if (key.offset > start && extent_end <= end)
1330 *length += extent_end - key.offset;
1331 else if (key.offset > start && key.offset <= end) {
1332 *length += end - key.offset + 1;
1333 break;
1334 } else if (key.offset > end)
1335 break;
1336
1337next:
1338 path->slots[0]++;
1339 }
1340 ret = 0;
1341out:
1342 btrfs_free_path(path);
1343 return ret;
1344}
1345
Jeff Mahoney499f3772015-06-15 09:41:17 -04001346static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001347 struct btrfs_device *device,
1348 u64 *start, u64 len)
1349{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001350 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001351 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001352 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001353 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001354 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001355
Jeff Mahoney499f3772015-06-15 09:41:17 -04001356 if (transaction)
1357 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001358again:
1359 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001360 struct map_lookup *map;
1361 int i;
1362
Jeff Mahoney95617d62015-06-03 10:55:48 -04001363 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001364 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001365 u64 end;
1366
Josef Bacik6df9a952013-06-27 13:22:46 -04001367 if (map->stripes[i].dev != device)
1368 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001369 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001370 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001371 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001372 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001373 /*
1374 * Make sure that while processing the pinned list we do
1375 * not override our *start with a lower value, because
1376 * we can have pinned chunks that fall within this
1377 * device hole and that have lower physical addresses
1378 * than the pending chunks we processed before. If we
1379 * do not take this special care we can end up getting
1380 * 2 pending chunks that start at the same physical
1381 * device offsets because the end offset of a pinned
1382 * chunk can be equal to the start offset of some
1383 * pending chunk.
1384 */
1385 end = map->stripes[i].physical + em->orig_block_len;
1386 if (end > *start) {
1387 *start = end;
1388 ret = 1;
1389 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001390 }
1391 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001392 if (search_list != &fs_info->pinned_chunks) {
1393 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001394 goto again;
1395 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001396
1397 return ret;
1398}
1399
1400
Chris Mason0b86a832008-03-24 15:01:56 -04001401/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001402 * find_free_dev_extent_start - find free space in the specified device
1403 * @device: the device which we search the free space in
1404 * @num_bytes: the size of the free space that we need
1405 * @search_start: the position from which to begin the search
1406 * @start: store the start of the free space.
1407 * @len: the size of the free space. that we find, or the size
1408 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001409 *
Chris Mason0b86a832008-03-24 15:01:56 -04001410 * this uses a pretty simple search, the expectation is that it is
1411 * called very infrequently and that a given device has a small number
1412 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001413 *
1414 * @start is used to store the start of the free space if we find. But if we
1415 * don't find suitable free space, it will be used to store the start position
1416 * of the max free space.
1417 *
1418 * @len is used to store the size of the free space that we find.
1419 * But if we don't find suitable free space, it is used to store the size of
1420 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001421 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001422int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1423 struct btrfs_device *device, u64 num_bytes,
1424 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001425{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001426 struct btrfs_fs_info *fs_info = device->fs_info;
1427 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001428 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001429 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001430 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001431 u64 hole_size;
1432 u64 max_hole_start;
1433 u64 max_hole_size;
1434 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001435 u64 search_end = device->total_bytes;
1436 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001437 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001438 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001439
1440 /*
1441 * We don't want to overwrite the superblock on the drive nor any area
1442 * used by the boot loader (grub for example), so we make sure to start
1443 * at an offset of at least 1MB.
1444 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001445 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001446
Josef Bacik6df9a952013-06-27 13:22:46 -04001447 path = btrfs_alloc_path();
1448 if (!path)
1449 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001450
Miao Xie7bfc8372011-01-05 10:07:26 +00001451 max_hole_start = search_start;
1452 max_hole_size = 0;
1453
Zhao Leif2ab7612015-02-16 18:52:17 +08001454again:
Anand Jain401e29c2017-12-04 12:54:55 +08001455 if (search_start >= search_end ||
1456 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001457 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001458 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001459 }
1460
David Sterbae4058b52015-11-27 16:31:35 +01001461 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001462 path->search_commit_root = 1;
1463 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001464
Chris Mason0b86a832008-03-24 15:01:56 -04001465 key.objectid = device->devid;
1466 key.offset = search_start;
1467 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001468
Li Zefan125ccb02011-12-08 15:07:24 +08001469 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001470 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001471 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001472 if (ret > 0) {
1473 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1474 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001475 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001476 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001477
Chris Mason0b86a832008-03-24 15:01:56 -04001478 while (1) {
1479 l = path->nodes[0];
1480 slot = path->slots[0];
1481 if (slot >= btrfs_header_nritems(l)) {
1482 ret = btrfs_next_leaf(root, path);
1483 if (ret == 0)
1484 continue;
1485 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001486 goto out;
1487
1488 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001489 }
1490 btrfs_item_key_to_cpu(l, &key, slot);
1491
1492 if (key.objectid < device->devid)
1493 goto next;
1494
1495 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001496 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001497
David Sterba962a2982014-06-04 18:41:45 +02001498 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001499 goto next;
1500
Miao Xie7bfc8372011-01-05 10:07:26 +00001501 if (key.offset > search_start) {
1502 hole_size = key.offset - search_start;
1503
Josef Bacik6df9a952013-06-27 13:22:46 -04001504 /*
1505 * Have to check before we set max_hole_start, otherwise
1506 * we could end up sending back this offset anyway.
1507 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001508 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001509 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001510 hole_size)) {
1511 if (key.offset >= search_start) {
1512 hole_size = key.offset - search_start;
1513 } else {
1514 WARN_ON_ONCE(1);
1515 hole_size = 0;
1516 }
1517 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001518
Miao Xie7bfc8372011-01-05 10:07:26 +00001519 if (hole_size > max_hole_size) {
1520 max_hole_start = search_start;
1521 max_hole_size = hole_size;
1522 }
1523
1524 /*
1525 * If this free space is greater than which we need,
1526 * it must be the max free space that we have found
1527 * until now, so max_hole_start must point to the start
1528 * of this free space and the length of this free space
1529 * is stored in max_hole_size. Thus, we return
1530 * max_hole_start and max_hole_size and go back to the
1531 * caller.
1532 */
1533 if (hole_size >= num_bytes) {
1534 ret = 0;
1535 goto out;
1536 }
1537 }
1538
Chris Mason0b86a832008-03-24 15:01:56 -04001539 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001540 extent_end = key.offset + btrfs_dev_extent_length(l,
1541 dev_extent);
1542 if (extent_end > search_start)
1543 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001544next:
1545 path->slots[0]++;
1546 cond_resched();
1547 }
Chris Mason0b86a832008-03-24 15:01:56 -04001548
liubo38c01b92011-08-02 02:39:03 +00001549 /*
1550 * At this point, search_start should be the end of
1551 * allocated dev extents, and when shrinking the device,
1552 * search_end may be smaller than search_start.
1553 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001554 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001555 hole_size = search_end - search_start;
1556
Jeff Mahoney499f3772015-06-15 09:41:17 -04001557 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001558 hole_size)) {
1559 btrfs_release_path(path);
1560 goto again;
1561 }
Chris Mason0b86a832008-03-24 15:01:56 -04001562
Zhao Leif2ab7612015-02-16 18:52:17 +08001563 if (hole_size > max_hole_size) {
1564 max_hole_start = search_start;
1565 max_hole_size = hole_size;
1566 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001567 }
1568
Miao Xie7bfc8372011-01-05 10:07:26 +00001569 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001570 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001571 ret = -ENOSPC;
1572 else
1573 ret = 0;
1574
1575out:
Yan Zheng2b820322008-11-17 21:11:30 -05001576 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001577 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001578 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001579 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001580 return ret;
1581}
1582
Jeff Mahoney499f3772015-06-15 09:41:17 -04001583int find_free_dev_extent(struct btrfs_trans_handle *trans,
1584 struct btrfs_device *device, u64 num_bytes,
1585 u64 *start, u64 *len)
1586{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001587 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001588 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001589 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001590}
1591
Christoph Hellwigb2950862008-12-02 09:54:17 -05001592static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001593 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001594 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001595{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001596 struct btrfs_fs_info *fs_info = device->fs_info;
1597 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001598 int ret;
1599 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001600 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001601 struct btrfs_key found_key;
1602 struct extent_buffer *leaf = NULL;
1603 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001604
1605 path = btrfs_alloc_path();
1606 if (!path)
1607 return -ENOMEM;
1608
1609 key.objectid = device->devid;
1610 key.offset = start;
1611 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001612again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001613 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001614 if (ret > 0) {
1615 ret = btrfs_previous_item(root, path, key.objectid,
1616 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001617 if (ret)
1618 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001619 leaf = path->nodes[0];
1620 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1621 extent = btrfs_item_ptr(leaf, path->slots[0],
1622 struct btrfs_dev_extent);
1623 BUG_ON(found_key.offset > start || found_key.offset +
1624 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001625 key = found_key;
1626 btrfs_release_path(path);
1627 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001628 } else if (ret == 0) {
1629 leaf = path->nodes[0];
1630 extent = btrfs_item_ptr(leaf, path->slots[0],
1631 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001632 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001633 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001634 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001635 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001636
Miao Xie2196d6e2014-09-03 21:35:41 +08001637 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1638
Chris Mason8f18cf12008-04-25 16:53:30 -04001639 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001640 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001641 btrfs_handle_fs_error(fs_info, ret,
1642 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001643 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001644 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001645 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001646out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001647 btrfs_free_path(path);
1648 return ret;
1649}
1650
Eric Sandeen48a3b632013-04-25 20:41:01 +00001651static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1652 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001653 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001654{
1655 int ret;
1656 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001657 struct btrfs_fs_info *fs_info = device->fs_info;
1658 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001659 struct btrfs_dev_extent *extent;
1660 struct extent_buffer *leaf;
1661 struct btrfs_key key;
1662
Anand Jaine12c9622017-12-04 12:54:53 +08001663 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001664 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001665 path = btrfs_alloc_path();
1666 if (!path)
1667 return -ENOMEM;
1668
Chris Mason0b86a832008-03-24 15:01:56 -04001669 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001670 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001671 key.type = BTRFS_DEV_EXTENT_KEY;
1672 ret = btrfs_insert_empty_item(trans, root, path, &key,
1673 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001674 if (ret)
1675 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001676
1677 leaf = path->nodes[0];
1678 extent = btrfs_item_ptr(leaf, path->slots[0],
1679 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001680 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1681 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001682 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1683 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001684 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1685
Chris Mason0b86a832008-03-24 15:01:56 -04001686 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1687 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001688out:
Chris Mason0b86a832008-03-24 15:01:56 -04001689 btrfs_free_path(path);
1690 return ret;
1691}
1692
Josef Bacik6df9a952013-06-27 13:22:46 -04001693static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001694{
Josef Bacik6df9a952013-06-27 13:22:46 -04001695 struct extent_map_tree *em_tree;
1696 struct extent_map *em;
1697 struct rb_node *n;
1698 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001699
Josef Bacik6df9a952013-06-27 13:22:46 -04001700 em_tree = &fs_info->mapping_tree.map_tree;
1701 read_lock(&em_tree->lock);
1702 n = rb_last(&em_tree->map);
1703 if (n) {
1704 em = rb_entry(n, struct extent_map, rb_node);
1705 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001706 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001707 read_unlock(&em_tree->lock);
1708
Chris Mason0b86a832008-03-24 15:01:56 -04001709 return ret;
1710}
1711
Ilya Dryomov53f10652013-08-12 14:33:01 +03001712static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1713 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001714{
1715 int ret;
1716 struct btrfs_key key;
1717 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001718 struct btrfs_path *path;
1719
Yan Zheng2b820322008-11-17 21:11:30 -05001720 path = btrfs_alloc_path();
1721 if (!path)
1722 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001723
1724 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1725 key.type = BTRFS_DEV_ITEM_KEY;
1726 key.offset = (u64)-1;
1727
Ilya Dryomov53f10652013-08-12 14:33:01 +03001728 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001729 if (ret < 0)
1730 goto error;
1731
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001732 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001733
Ilya Dryomov53f10652013-08-12 14:33:01 +03001734 ret = btrfs_previous_item(fs_info->chunk_root, path,
1735 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001736 BTRFS_DEV_ITEM_KEY);
1737 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001738 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001739 } else {
1740 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1741 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001742 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001743 }
1744 ret = 0;
1745error:
Yan Zheng2b820322008-11-17 21:11:30 -05001746 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001747 return ret;
1748}
1749
1750/*
1751 * the device information is stored in the chunk root
1752 * the btrfs_device struct should be fully filled in
1753 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001754static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001755 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001756 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001757{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001758 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001759 int ret;
1760 struct btrfs_path *path;
1761 struct btrfs_dev_item *dev_item;
1762 struct extent_buffer *leaf;
1763 struct btrfs_key key;
1764 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001765
Chris Mason0b86a832008-03-24 15:01:56 -04001766 path = btrfs_alloc_path();
1767 if (!path)
1768 return -ENOMEM;
1769
Chris Mason0b86a832008-03-24 15:01:56 -04001770 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1771 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001772 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001773
1774 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001775 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001776 if (ret)
1777 goto out;
1778
1779 leaf = path->nodes[0];
1780 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1781
1782 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001783 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001784 btrfs_set_device_type(leaf, dev_item, device->type);
1785 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1786 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1787 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001788 btrfs_set_device_total_bytes(leaf, dev_item,
1789 btrfs_device_get_disk_total_bytes(device));
1790 btrfs_set_device_bytes_used(leaf, dev_item,
1791 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001792 btrfs_set_device_group(leaf, dev_item, 0);
1793 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1794 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001795 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001796
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001797 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001798 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001799 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001800 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001801 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001802
Yan Zheng2b820322008-11-17 21:11:30 -05001803 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001804out:
1805 btrfs_free_path(path);
1806 return ret;
1807}
Chris Mason8f18cf12008-04-25 16:53:30 -04001808
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001809/*
1810 * Function to update ctime/mtime for a given device path.
1811 * Mainly used for ctime/mtime based probe like libblkid.
1812 */
David Sterbada353f62017-02-14 17:55:53 +01001813static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001814{
1815 struct file *filp;
1816
1817 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001818 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001819 return;
1820 file_update_time(filp);
1821 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001822}
1823
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001824static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001825 struct btrfs_device *device)
1826{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001827 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001828 int ret;
1829 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001830 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001831 struct btrfs_trans_handle *trans;
1832
Chris Masona061fc82008-05-07 11:43:44 -04001833 path = btrfs_alloc_path();
1834 if (!path)
1835 return -ENOMEM;
1836
Yan, Zhenga22285a2010-05-16 10:48:46 -04001837 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001838 if (IS_ERR(trans)) {
1839 btrfs_free_path(path);
1840 return PTR_ERR(trans);
1841 }
Chris Masona061fc82008-05-07 11:43:44 -04001842 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1843 key.type = BTRFS_DEV_ITEM_KEY;
1844 key.offset = device->devid;
1845
1846 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001847 if (ret) {
1848 if (ret > 0)
1849 ret = -ENOENT;
1850 btrfs_abort_transaction(trans, ret);
1851 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001852 goto out;
1853 }
1854
1855 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001856 if (ret) {
1857 btrfs_abort_transaction(trans, ret);
1858 btrfs_end_transaction(trans);
1859 }
1860
Chris Masona061fc82008-05-07 11:43:44 -04001861out:
1862 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001863 if (!ret)
1864 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001865 return ret;
1866}
1867
David Sterba3cc31a02016-02-15 16:00:26 +01001868/*
1869 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1870 * filesystem. It's up to the caller to adjust that number regarding eg. device
1871 * replace.
1872 */
1873static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1874 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001875{
Chris Masona061fc82008-05-07 11:43:44 -04001876 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001877 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001878 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001879
Miao Xiede98ced2013-01-29 10:13:12 +00001880 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001881 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001882
Anand Jainbd45ffb2016-02-13 10:01:34 +08001883 all_avail = fs_info->avail_data_alloc_bits |
1884 fs_info->avail_system_alloc_bits |
1885 fs_info->avail_metadata_alloc_bits;
1886 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001887
David Sterba418775a2016-02-15 16:28:14 +01001888 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001889 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001890 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001891
David Sterba418775a2016-02-15 16:28:14 +01001892 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001893 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001894
David Sterba418775a2016-02-15 16:28:14 +01001895 if (ret)
1896 return ret;
1897 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001898 }
1899
1900 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001901}
1902
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001903static struct btrfs_device * btrfs_find_next_active_device(
1904 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001905{
1906 struct btrfs_device *next_device;
1907
1908 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1909 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001910 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1911 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001912 return next_device;
1913 }
1914
1915 return NULL;
1916}
1917
1918/*
1919 * Helper function to check if the given device is part of s_bdev / latest_bdev
1920 * and replace it with the provided or the next active device, in the context
1921 * where this function called, there should be always be another device (or
1922 * this_dev) which is active.
1923 */
1924void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1925 struct btrfs_device *device, struct btrfs_device *this_dev)
1926{
1927 struct btrfs_device *next_device;
1928
1929 if (this_dev)
1930 next_device = this_dev;
1931 else
1932 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1933 device);
1934 ASSERT(next_device);
1935
1936 if (fs_info->sb->s_bdev &&
1937 (fs_info->sb->s_bdev == device->bdev))
1938 fs_info->sb->s_bdev = next_device->bdev;
1939
1940 if (fs_info->fs_devices->latest_bdev == device->bdev)
1941 fs_info->fs_devices->latest_bdev = next_device->bdev;
1942}
1943
David Sterbada353f62017-02-14 17:55:53 +01001944int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1945 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001946{
1947 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001948 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001949 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001950 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001951 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001952
1953 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001954
Anand Jainb5185192018-04-12 10:29:30 +08001955 num_devices = fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01001956 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001957 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001958 WARN_ON(num_devices < 1);
1959 num_devices--;
1960 }
David Sterba7e79cb82018-03-24 02:11:38 +01001961 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Chris Masona061fc82008-05-07 11:43:44 -04001962
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001963 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001964 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001965 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001966
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001967 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1968 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001969 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001970 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001971
Anand Jain401e29c2017-12-04 12:54:55 +08001972 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001973 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001974 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001975 }
1976
Anand Jainebbede42017-12-04 12:54:52 +08001977 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1978 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001979 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001980 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001981 }
1982
Anand Jainebbede42017-12-04 12:54:52 +08001983 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001984 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001985 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001986 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001987 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001988 }
Chris Masona061fc82008-05-07 11:43:44 -04001989
Carey Underwoodd7901552013-03-04 16:37:06 -06001990 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001991 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001992 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001993 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001994 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001995
Stefan Behrens63a212a2012-11-05 18:29:28 +01001996 /*
1997 * TODO: the superblock still includes this device in its num_devices
1998 * counter although write_all_supers() is not locked out. This
1999 * could give a filesystem state which requires a degraded mount.
2000 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002001 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002002 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002003 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002004
Anand Jaine12c9622017-12-04 12:54:53 +08002005 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002006 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002007
2008 /*
2009 * the device list mutex makes sure that we don't change
2010 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002011 * the device supers. Whoever is writing all supers, should
2012 * lock the device list mutex before getting the number of
2013 * devices in the super block (super_copy). Conversely,
2014 * whoever updates the number of devices in the super block
2015 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002016 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002017
Anand Jain41a52a02018-04-12 10:29:31 +08002018 /*
2019 * In normal cases the cur_devices == fs_devices. But in case
2020 * of deleting a seed device, the cur_devices should point to
2021 * its own fs_devices listed under the fs_devices->seed.
2022 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002023 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002024 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002025 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002026
Anand Jain41a52a02018-04-12 10:29:31 +08002027 cur_devices->num_devices--;
2028 cur_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002029
Anand Jaine6e674b2017-12-04 12:54:54 +08002030 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002031 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002032
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002033 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002034
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002035 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002036 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002037 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002038 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002039 }
Anand Jain99994cd2014-06-03 11:36:00 +08002040
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002041 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2042 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002043 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002044
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002045 /*
2046 * at this point, the device is zero sized and detached from
2047 * the devices list. All that's left is to zero out the old
2048 * supers and free the device.
2049 */
Anand Jainebbede42017-12-04 12:54:52 +08002050 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002051 btrfs_scratch_superblocks(device->bdev, device->name->str);
2052
2053 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002054 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002055
Xiao Guangrong1f781602011-04-20 10:09:16 +00002056 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002057 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002058 if (fs_devices->seed == cur_devices) {
2059 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002060 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002061 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002062 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002063 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002064 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002065 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002066 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002067 }
2068
Chris Masona061fc82008-05-07 11:43:44 -04002069out:
2070 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002071 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002072
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002073error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002074 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002075 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002076 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002077 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002078 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002079 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002080 }
Anand Jain24fc5722016-02-13 10:01:36 +08002081 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002082}
2083
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002084void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2085 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002086{
Anand Jaind51908c2014-08-13 14:24:19 +08002087 struct btrfs_fs_devices *fs_devices;
2088
David Sterbaa32bf9a2018-03-16 02:21:22 +01002089 lockdep_assert_held(&fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002090
Anand Jain25e8e912014-08-20 10:56:56 +08002091 /*
2092 * in case of fs with no seed, srcdev->fs_devices will point
2093 * to fs_devices of fs_info. However when the dev being replaced is
2094 * a seed dev it will point to the seed's local fs_devices. In short
2095 * srcdev will have its correct fs_devices in both the cases.
2096 */
2097 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002098
Stefan Behrense93c89c2012-11-05 17:33:06 +01002099 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002100 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002101 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002102 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002103 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002104
Anand Jainebbede42017-12-04 12:54:52 +08002105 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002106 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002107
Miao Xie82372bc2014-09-03 21:35:44 +08002108 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002109 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002110}
2111
2112void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2113 struct btrfs_device *srcdev)
2114{
2115 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002116
Anand Jainebbede42017-12-04 12:54:52 +08002117 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002118 /* zero out the old super if it is writable */
2119 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2120 }
Anand Jain14238812016-07-22 06:04:53 +08002121
2122 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002123 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002124
Anand Jain94d5f0c2014-08-13 14:24:22 +08002125 /* if this is no devs we rather delete the fs_devices */
2126 if (!fs_devices->num_devices) {
2127 struct btrfs_fs_devices *tmp_fs_devices;
2128
Anand Jain6dd38f82017-10-17 06:53:50 +08002129 /*
2130 * On a mounted FS, num_devices can't be zero unless it's a
2131 * seed. In case of a seed device being replaced, the replace
2132 * target added to the sprout FS, so there will be no more
2133 * device left under the seed FS.
2134 */
2135 ASSERT(fs_devices->seeding);
2136
Anand Jain94d5f0c2014-08-13 14:24:22 +08002137 tmp_fs_devices = fs_info->fs_devices;
2138 while (tmp_fs_devices) {
2139 if (tmp_fs_devices->seed == fs_devices) {
2140 tmp_fs_devices->seed = fs_devices->seed;
2141 break;
2142 }
2143 tmp_fs_devices = tmp_fs_devices->seed;
2144 }
2145 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002146 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002147 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002148 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002149}
2150
2151void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2152 struct btrfs_device *tgtdev)
2153{
Miao Xie67a2c452014-09-03 21:35:43 +08002154 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002155 WARN_ON(!tgtdev);
2156 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002157
Anand Jain32576042015-08-14 18:32:49 +08002158 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002159
Anand Jain779bf3fe2016-04-18 16:51:23 +08002160 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002161 fs_info->fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002162
Stefan Behrense93c89c2012-11-05 17:33:06 +01002163 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002164
Anand Jain88acff62016-05-03 17:44:43 +08002165 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002166
Stefan Behrense93c89c2012-11-05 17:33:06 +01002167 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002168
2169 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002170 mutex_unlock(&uuid_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002171
2172 /*
2173 * The update_dev_time() with in btrfs_scratch_superblocks()
2174 * may lead to a call to btrfs_show_devname() which will try
2175 * to hold device_list_mutex. And here this device
2176 * is already out of device list, so we don't have to hold
2177 * the device_list_mutex lock.
2178 */
2179 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002180
2181 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002182 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002183}
2184
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002185static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002186 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002187 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002188{
2189 int ret = 0;
2190 struct btrfs_super_block *disk_super;
2191 u64 devid;
2192 u8 *dev_uuid;
2193 struct block_device *bdev;
2194 struct buffer_head *bh;
2195
2196 *device = NULL;
2197 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002198 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002199 if (ret)
2200 return ret;
2201 disk_super = (struct btrfs_super_block *)bh->b_data;
2202 devid = btrfs_stack_device_id(&disk_super->dev_item);
2203 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002204 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002205 brelse(bh);
2206 if (!*device)
2207 ret = -ENOENT;
2208 blkdev_put(bdev, FMODE_READ);
2209 return ret;
2210}
2211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002212int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002213 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002214 struct btrfs_device **device)
2215{
2216 *device = NULL;
2217 if (strcmp(device_path, "missing") == 0) {
2218 struct list_head *devices;
2219 struct btrfs_device *tmp;
2220
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002221 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002222 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002223 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2224 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002225 *device = tmp;
2226 break;
2227 }
2228 }
2229
Anand Jaind74a6252015-08-14 18:32:56 +08002230 if (!*device)
2231 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002232
2233 return 0;
2234 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002235 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002236 }
2237}
2238
Yan Zheng2b820322008-11-17 21:11:30 -05002239/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002240 * Lookup a device given by device id, or the path if the id is 0.
2241 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002242int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002243 const char *devpath,
2244 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002245{
2246 int ret;
2247
David Sterba5c5c0df2016-02-15 16:39:55 +01002248 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002249 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002250 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002251 if (!*device)
2252 ret = -ENOENT;
2253 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002254 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002255 return -EINVAL;
2256
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002257 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002258 device);
2259 }
2260 return ret;
2261}
2262
Yan Zheng2b820322008-11-17 21:11:30 -05002263/*
2264 * does all the dirty work required for changing file system's UUID.
2265 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002266static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002267{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002268 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002269 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002270 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002271 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002272 struct btrfs_device *device;
2273 u64 super_flags;
2274
David Sterbaa32bf9a2018-03-16 02:21:22 +01002275 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002276 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002277 return -EINVAL;
2278
David Sterba2dfeca92017-06-14 02:48:07 +02002279 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002280 if (IS_ERR(seed_devices))
2281 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002282
Yan Zhenge4404d62008-12-12 10:03:26 -05002283 old_devices = clone_fs_devices(fs_devices);
2284 if (IS_ERR(old_devices)) {
2285 kfree(seed_devices);
2286 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002287 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002288
Anand Jainc4babc52018-04-12 10:29:25 +08002289 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002290
Yan Zhenge4404d62008-12-12 10:03:26 -05002291 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2292 seed_devices->opened = 1;
2293 INIT_LIST_HEAD(&seed_devices->devices);
2294 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002295 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002296
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002297 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002298 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2299 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002300 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002301 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002302
David Sterba34441362016-10-04 19:34:27 +02002303 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002304 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002305 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002306
Yan Zheng2b820322008-11-17 21:11:30 -05002307 fs_devices->seeding = 0;
2308 fs_devices->num_devices = 0;
2309 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002310 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002311 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002312 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002313
2314 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002315 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002316 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002317 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002318
Yan Zheng2b820322008-11-17 21:11:30 -05002319 super_flags = btrfs_super_flags(disk_super) &
2320 ~BTRFS_SUPER_FLAG_SEEDING;
2321 btrfs_set_super_flags(disk_super, super_flags);
2322
2323 return 0;
2324}
2325
2326/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002327 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002328 */
2329static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002330 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002331{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002332 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002333 struct btrfs_path *path;
2334 struct extent_buffer *leaf;
2335 struct btrfs_dev_item *dev_item;
2336 struct btrfs_device *device;
2337 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002338 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002339 u8 dev_uuid[BTRFS_UUID_SIZE];
2340 u64 devid;
2341 int ret;
2342
2343 path = btrfs_alloc_path();
2344 if (!path)
2345 return -ENOMEM;
2346
Yan Zheng2b820322008-11-17 21:11:30 -05002347 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2348 key.offset = 0;
2349 key.type = BTRFS_DEV_ITEM_KEY;
2350
2351 while (1) {
2352 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2353 if (ret < 0)
2354 goto error;
2355
2356 leaf = path->nodes[0];
2357next_slot:
2358 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2359 ret = btrfs_next_leaf(root, path);
2360 if (ret > 0)
2361 break;
2362 if (ret < 0)
2363 goto error;
2364 leaf = path->nodes[0];
2365 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002366 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002367 continue;
2368 }
2369
2370 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2371 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2372 key.type != BTRFS_DEV_ITEM_KEY)
2373 break;
2374
2375 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2376 struct btrfs_dev_item);
2377 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002378 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002379 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002380 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002381 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002382 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002383 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002384
2385 if (device->fs_devices->seeding) {
2386 btrfs_set_device_generation(leaf, dev_item,
2387 device->generation);
2388 btrfs_mark_buffer_dirty(leaf);
2389 }
2390
2391 path->slots[0]++;
2392 goto next_slot;
2393 }
2394 ret = 0;
2395error:
2396 btrfs_free_path(path);
2397 return ret;
2398}
2399
David Sterbada353f62017-02-14 17:55:53 +01002400int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002401{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002402 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002403 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002404 struct btrfs_trans_handle *trans;
2405 struct btrfs_device *device;
2406 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002407 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002408 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002409 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002410 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002411 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002412 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002413 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002414
David Howellsbc98a422017-07-17 08:45:34 +01002415 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002416 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002417
Li Zefana5d16332011-12-07 20:08:40 -05002418 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002419 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002420 if (IS_ERR(bdev))
2421 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002422
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002424 seeding_dev = 1;
2425 down_write(&sb->s_umount);
2426 mutex_lock(&uuid_mutex);
2427 }
2428
Chris Mason8c8bee12008-09-29 11:19:10 -04002429 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002430
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002431 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002432
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002433 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002434 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002435 if (device->bdev == bdev) {
2436 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002437 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002438 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002439 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002440 }
2441 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002442 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002443
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002444 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002445 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002446 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002447 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002448 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002449 }
2450
David Sterba78f2c9e2016-02-11 14:25:38 +01002451 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002452 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002453 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002454 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002455 }
Josef Bacik606686e2012-06-04 14:03:51 -04002456 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002457
Yan, Zhenga22285a2010-05-16 10:48:46 -04002458 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002459 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002460 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002461 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002462 }
2463
Josef Bacikd5e20032011-08-04 14:52:27 +00002464 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002465 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002466 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002467 device->io_width = fs_info->sectorsize;
2468 device->io_align = fs_info->sectorsize;
2469 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002470 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2471 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002472 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002473 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002474 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002475 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002476 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002477 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002478 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002479 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002480 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002481
Yan Zheng2b820322008-11-17 21:11:30 -05002482 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002483 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002484 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002485 if (ret) {
2486 btrfs_abort_transaction(trans, ret);
2487 goto error_trans;
2488 }
Yan Zheng2b820322008-11-17 21:11:30 -05002489 }
2490
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002491 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002492
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002493 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002494 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002495 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002496 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002497 &fs_info->fs_devices->alloc_list);
2498 fs_info->fs_devices->num_devices++;
2499 fs_info->fs_devices->open_devices++;
2500 fs_info->fs_devices->rw_devices++;
2501 fs_info->fs_devices->total_devices++;
2502 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002503
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002504 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002505
Anand Jaine884f4f2017-04-04 18:40:19 +08002506 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002508
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002509 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2510 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002511 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002512
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002513 tmp = btrfs_super_num_devices(fs_info->super_copy);
2514 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002515
2516 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002517 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002518
Miao Xie2196d6e2014-09-03 21:35:41 +08002519 /*
2520 * we've got more storage, clear any full flags on the space
2521 * infos
2522 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002523 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002524
David Sterba34441362016-10-04 19:34:27 +02002525 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002526 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002527
Yan Zheng2b820322008-11-17 21:11:30 -05002528 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002529 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002530 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002531 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002532 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002533 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002534 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002535 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002536 }
2537
Anand Jainc74a0b02017-11-06 16:36:15 +08002538 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002539 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002540 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002541 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002542 }
2543
2544 if (seeding_dev) {
2545 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2546
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002547 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002548 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002549 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002550 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002551 }
Anand Jainb2373f22014-06-03 11:36:03 +08002552
2553 /* Sprouting would change fsid of the mounted root,
2554 * so rename the fsid on the sysfs
2555 */
2556 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002557 fs_info->fsid);
2558 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2559 btrfs_warn(fs_info,
2560 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002561 }
2562
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002563 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002564
2565 if (seeding_dev) {
2566 mutex_unlock(&uuid_mutex);
2567 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002568 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002569
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002570 if (ret) /* transaction commit */
2571 return ret;
2572
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002573 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002574 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002575 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002576 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002577 trans = btrfs_attach_transaction(root);
2578 if (IS_ERR(trans)) {
2579 if (PTR_ERR(trans) == -ENOENT)
2580 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002581 ret = PTR_ERR(trans);
2582 trans = NULL;
2583 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002584 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002585 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002586 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002587
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002588 /* Update ctime/mtime for libblkid */
2589 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002590 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002591
Anand Jaind31c32f2017-09-28 14:51:10 +08002592error_sysfs:
2593 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002594error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002595 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002596 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002597 if (trans)
2598 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002599error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002600 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002601error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002602 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002603 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002604 mutex_unlock(&uuid_mutex);
2605 up_write(&sb->s_umount);
2606 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002607 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002608}
2609
Chris Masond3977122009-01-05 21:25:51 -05002610static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2611 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002612{
2613 int ret;
2614 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002615 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002616 struct btrfs_dev_item *dev_item;
2617 struct extent_buffer *leaf;
2618 struct btrfs_key key;
2619
Chris Mason0b86a832008-03-24 15:01:56 -04002620 path = btrfs_alloc_path();
2621 if (!path)
2622 return -ENOMEM;
2623
2624 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2625 key.type = BTRFS_DEV_ITEM_KEY;
2626 key.offset = device->devid;
2627
2628 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2629 if (ret < 0)
2630 goto out;
2631
2632 if (ret > 0) {
2633 ret = -ENOENT;
2634 goto out;
2635 }
2636
2637 leaf = path->nodes[0];
2638 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2639
2640 btrfs_set_device_id(leaf, dev_item, device->devid);
2641 btrfs_set_device_type(leaf, dev_item, device->type);
2642 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2643 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2644 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002645 btrfs_set_device_total_bytes(leaf, dev_item,
2646 btrfs_device_get_disk_total_bytes(device));
2647 btrfs_set_device_bytes_used(leaf, dev_item,
2648 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002649 btrfs_mark_buffer_dirty(leaf);
2650
2651out:
2652 btrfs_free_path(path);
2653 return ret;
2654}
2655
Miao Xie2196d6e2014-09-03 21:35:41 +08002656int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002657 struct btrfs_device *device, u64 new_size)
2658{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002659 struct btrfs_fs_info *fs_info = device->fs_info;
2660 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002661 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002662 u64 old_total;
2663 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002664
Anand Jainebbede42017-12-04 12:54:52 +08002665 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002666 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002667
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002668 new_size = round_down(new_size, fs_info->sectorsize);
2669
David Sterba34441362016-10-04 19:34:27 +02002670 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002671 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002672 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002673
Stefan Behrens63a212a2012-11-05 18:29:28 +01002674 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002675 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002676 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002677 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002678 }
Yan Zheng2b820322008-11-17 21:11:30 -05002679
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002680 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002681
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002682 btrfs_set_super_total_bytes(super_copy,
2683 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002684 device->fs_devices->total_rw_bytes += diff;
2685
Miao Xie7cc8e582014-09-03 21:35:38 +08002686 btrfs_device_set_total_bytes(device, new_size);
2687 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002688 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002689 if (list_empty(&device->resized_list))
2690 list_add_tail(&device->resized_list,
2691 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002692 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002693
Chris Mason8f18cf12008-04-25 16:53:30 -04002694 return btrfs_update_device(trans, device);
2695}
2696
2697static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002698 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002699{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002700 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002701 int ret;
2702 struct btrfs_path *path;
2703 struct btrfs_key key;
2704
Chris Mason8f18cf12008-04-25 16:53:30 -04002705 path = btrfs_alloc_path();
2706 if (!path)
2707 return -ENOMEM;
2708
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002709 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002710 key.offset = chunk_offset;
2711 key.type = BTRFS_CHUNK_ITEM_KEY;
2712
2713 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002714 if (ret < 0)
2715 goto out;
2716 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002717 btrfs_handle_fs_error(fs_info, -ENOENT,
2718 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002719 ret = -ENOENT;
2720 goto out;
2721 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002722
2723 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002724 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002725 btrfs_handle_fs_error(fs_info, ret,
2726 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002727out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002728 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002729 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002730}
2731
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002732static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002734 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002735 struct btrfs_disk_key *disk_key;
2736 struct btrfs_chunk *chunk;
2737 u8 *ptr;
2738 int ret = 0;
2739 u32 num_stripes;
2740 u32 array_size;
2741 u32 len = 0;
2742 u32 cur;
2743 struct btrfs_key key;
2744
David Sterba34441362016-10-04 19:34:27 +02002745 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002746 array_size = btrfs_super_sys_array_size(super_copy);
2747
2748 ptr = super_copy->sys_chunk_array;
2749 cur = 0;
2750
2751 while (cur < array_size) {
2752 disk_key = (struct btrfs_disk_key *)ptr;
2753 btrfs_disk_key_to_cpu(&key, disk_key);
2754
2755 len = sizeof(*disk_key);
2756
2757 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2758 chunk = (struct btrfs_chunk *)(ptr + len);
2759 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2760 len += btrfs_chunk_item_size(num_stripes);
2761 } else {
2762 ret = -EIO;
2763 break;
2764 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002765 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002766 key.offset == chunk_offset) {
2767 memmove(ptr, ptr + len, array_size - (cur + len));
2768 array_size -= len;
2769 btrfs_set_super_sys_array_size(super_copy, array_size);
2770 } else {
2771 ptr += len;
2772 cur += len;
2773 }
2774 }
David Sterba34441362016-10-04 19:34:27 +02002775 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002776 return ret;
2777}
2778
Liu Bo592d92e2017-03-14 13:33:55 -07002779static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2780 u64 logical, u64 length)
2781{
2782 struct extent_map_tree *em_tree;
2783 struct extent_map *em;
2784
2785 em_tree = &fs_info->mapping_tree.map_tree;
2786 read_lock(&em_tree->lock);
2787 em = lookup_extent_mapping(em_tree, logical, length);
2788 read_unlock(&em_tree->lock);
2789
2790 if (!em) {
2791 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2792 logical, length);
2793 return ERR_PTR(-EINVAL);
2794 }
2795
2796 if (em->start > logical || em->start + em->len < logical) {
2797 btrfs_crit(fs_info,
2798 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2799 logical, length, em->start, em->start + em->len);
2800 free_extent_map(em);
2801 return ERR_PTR(-EINVAL);
2802 }
2803
2804 /* callers are responsible for dropping em's ref. */
2805 return em;
2806}
2807
Josef Bacik47ab2a62014-09-18 11:20:02 -04002808int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002809 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002810{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002811 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002812 struct map_lookup *map;
2813 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002814 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002815 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002816
Liu Bo592d92e2017-03-14 13:33:55 -07002817 em = get_chunk_map(fs_info, chunk_offset, 1);
2818 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002819 /*
2820 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002821 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002822 * do anything we still error out.
2823 */
2824 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002825 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002826 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002827 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002828 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002829 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002830 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002831
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002832 /*
2833 * Take the device list mutex to prevent races with the final phase of
2834 * a device replace operation that replaces the device object associated
2835 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2836 */
2837 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002838 for (i = 0; i < map->num_stripes; i++) {
2839 struct btrfs_device *device = map->stripes[i].dev;
2840 ret = btrfs_free_dev_extent(trans, device,
2841 map->stripes[i].physical,
2842 &dev_extent_len);
2843 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002844 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002845 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002846 goto out;
2847 }
2848
2849 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002850 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002851 btrfs_device_set_bytes_used(device,
2852 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002853 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002854 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002855 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002856 }
2857
2858 if (map->stripes[i].dev) {
2859 ret = btrfs_update_device(trans, map->stripes[i].dev);
2860 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002861 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002862 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002863 goto out;
2864 }
2865 }
2866 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002867 mutex_unlock(&fs_devices->device_list_mutex);
2868
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002869 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002870 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002871 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002872 goto out;
2873 }
2874
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002875 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002876
2877 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002878 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002879 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002880 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002881 goto out;
2882 }
2883 }
2884
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002885 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002886 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002887 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002888 goto out;
2889 }
2890
Josef Bacik47ab2a62014-09-18 11:20:02 -04002891out:
2892 /* once for us */
2893 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002894 return ret;
2895}
2896
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002897static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002898{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002899 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002900 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002901 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002902
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002903 /*
2904 * Prevent races with automatic removal of unused block groups.
2905 * After we relocate and before we remove the chunk with offset
2906 * chunk_offset, automatic removal of the block group can kick in,
2907 * resulting in a failure when calling btrfs_remove_chunk() below.
2908 *
2909 * Make sure to acquire this mutex before doing a tree search (dev
2910 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2911 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2912 * we release the path used to search the chunk/dev tree and before
2913 * the current task acquires this mutex and calls us.
2914 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002915 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002916
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002917 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002918 if (ret)
2919 return -ENOSPC;
2920
Chris Mason8f18cf12008-04-25 16:53:30 -04002921 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002922 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002923 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002924 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002925 if (ret)
2926 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002927
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002928 /*
2929 * We add the kobjects here (and after forcing data chunk creation)
2930 * since relocation is the only place we'll create chunks of a new
2931 * type at runtime. The only place where we'll remove the last
2932 * chunk of a type is the call immediately below this one. Even
2933 * so, we're protected against races with the cleaner thread since
2934 * we're covered by the delete_unused_bgs_mutex.
2935 */
2936 btrfs_add_raid_kobjects(fs_info);
2937
Chris Mason19c4d2f2016-10-10 13:43:31 -07002938 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2939 chunk_offset);
2940 if (IS_ERR(trans)) {
2941 ret = PTR_ERR(trans);
2942 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2943 return ret;
2944 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002945
Chris Mason19c4d2f2016-10-10 13:43:31 -07002946 /*
2947 * step two, delete the device extents and the
2948 * chunk tree entries
2949 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002950 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002951 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002952 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002953}
2954
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002955static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002956{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002957 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002958 struct btrfs_path *path;
2959 struct extent_buffer *leaf;
2960 struct btrfs_chunk *chunk;
2961 struct btrfs_key key;
2962 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002963 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002964 bool retried = false;
2965 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002966 int ret;
2967
2968 path = btrfs_alloc_path();
2969 if (!path)
2970 return -ENOMEM;
2971
Josef Bacikba1bf482009-09-11 16:11:19 -04002972again:
Yan Zheng2b820322008-11-17 21:11:30 -05002973 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2974 key.offset = (u64)-1;
2975 key.type = BTRFS_CHUNK_ITEM_KEY;
2976
2977 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002978 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002979 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002980 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002981 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002982 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002983 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002984 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002985
2986 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2987 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002988 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002989 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002990 if (ret < 0)
2991 goto error;
2992 if (ret > 0)
2993 break;
2994
2995 leaf = path->nodes[0];
2996 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2997
2998 chunk = btrfs_item_ptr(leaf, path->slots[0],
2999 struct btrfs_chunk);
3000 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003001 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003002
3003 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003004 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003005 if (ret == -ENOSPC)
3006 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303007 else
3008 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003009 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003010 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003011
3012 if (found_key.offset == 0)
3013 break;
3014 key.offset = found_key.offset - 1;
3015 }
3016 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003017 if (failed && !retried) {
3018 failed = 0;
3019 retried = true;
3020 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303021 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003022 ret = -ENOSPC;
3023 }
Yan Zheng2b820322008-11-17 21:11:30 -05003024error:
3025 btrfs_free_path(path);
3026 return ret;
3027}
3028
Liu Boa6f93c72017-11-15 16:28:11 -07003029/*
3030 * return 1 : allocate a data chunk successfully,
3031 * return <0: errors during allocating a data chunk,
3032 * return 0 : no need to allocate a data chunk.
3033 */
3034static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3035 u64 chunk_offset)
3036{
3037 struct btrfs_block_group_cache *cache;
3038 u64 bytes_used;
3039 u64 chunk_type;
3040
3041 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3042 ASSERT(cache);
3043 chunk_type = cache->flags;
3044 btrfs_put_block_group(cache);
3045
3046 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3047 spin_lock(&fs_info->data_sinfo->lock);
3048 bytes_used = fs_info->data_sinfo->bytes_used;
3049 spin_unlock(&fs_info->data_sinfo->lock);
3050
3051 if (!bytes_used) {
3052 struct btrfs_trans_handle *trans;
3053 int ret;
3054
3055 trans = btrfs_join_transaction(fs_info->tree_root);
3056 if (IS_ERR(trans))
3057 return PTR_ERR(trans);
3058
3059 ret = btrfs_force_chunk_alloc(trans, fs_info,
3060 BTRFS_BLOCK_GROUP_DATA);
3061 btrfs_end_transaction(trans);
3062 if (ret < 0)
3063 return ret;
3064
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003065 btrfs_add_raid_kobjects(fs_info);
3066
Liu Boa6f93c72017-11-15 16:28:11 -07003067 return 1;
3068 }
3069 }
3070 return 0;
3071}
3072
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003073static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003074 struct btrfs_balance_control *bctl)
3075{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003076 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003077 struct btrfs_trans_handle *trans;
3078 struct btrfs_balance_item *item;
3079 struct btrfs_disk_balance_args disk_bargs;
3080 struct btrfs_path *path;
3081 struct extent_buffer *leaf;
3082 struct btrfs_key key;
3083 int ret, err;
3084
3085 path = btrfs_alloc_path();
3086 if (!path)
3087 return -ENOMEM;
3088
3089 trans = btrfs_start_transaction(root, 0);
3090 if (IS_ERR(trans)) {
3091 btrfs_free_path(path);
3092 return PTR_ERR(trans);
3093 }
3094
3095 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003096 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003097 key.offset = 0;
3098
3099 ret = btrfs_insert_empty_item(trans, root, path, &key,
3100 sizeof(*item));
3101 if (ret)
3102 goto out;
3103
3104 leaf = path->nodes[0];
3105 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3106
David Sterbab159fa22016-11-08 18:09:03 +01003107 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003108
3109 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3110 btrfs_set_balance_data(leaf, item, &disk_bargs);
3111 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3112 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3113 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3114 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3115
3116 btrfs_set_balance_flags(leaf, item, bctl->flags);
3117
3118 btrfs_mark_buffer_dirty(leaf);
3119out:
3120 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003121 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003122 if (err && !ret)
3123 ret = err;
3124 return ret;
3125}
3126
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003127static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003128{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003129 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003130 struct btrfs_trans_handle *trans;
3131 struct btrfs_path *path;
3132 struct btrfs_key key;
3133 int ret, err;
3134
3135 path = btrfs_alloc_path();
3136 if (!path)
3137 return -ENOMEM;
3138
3139 trans = btrfs_start_transaction(root, 0);
3140 if (IS_ERR(trans)) {
3141 btrfs_free_path(path);
3142 return PTR_ERR(trans);
3143 }
3144
3145 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003146 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003147 key.offset = 0;
3148
3149 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3150 if (ret < 0)
3151 goto out;
3152 if (ret > 0) {
3153 ret = -ENOENT;
3154 goto out;
3155 }
3156
3157 ret = btrfs_del_item(trans, root, path);
3158out:
3159 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003160 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003161 if (err && !ret)
3162 ret = err;
3163 return ret;
3164}
3165
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003166/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003167 * This is a heuristic used to reduce the number of chunks balanced on
3168 * resume after balance was interrupted.
3169 */
3170static void update_balance_args(struct btrfs_balance_control *bctl)
3171{
3172 /*
3173 * Turn on soft mode for chunk types that were being converted.
3174 */
3175 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3176 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3177 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3178 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3179 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3180 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3181
3182 /*
3183 * Turn on usage filter if is not already used. The idea is
3184 * that chunks that we have already balanced should be
3185 * reasonably full. Don't do it for chunks that are being
3186 * converted - that will keep us from relocating unconverted
3187 * (albeit full) chunks.
3188 */
3189 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003190 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003191 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3192 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3193 bctl->data.usage = 90;
3194 }
3195 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003196 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003197 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3198 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3199 bctl->sys.usage = 90;
3200 }
3201 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003202 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003203 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3204 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3205 bctl->meta.usage = 90;
3206 }
3207}
3208
3209/*
David Sterba149196a2018-03-20 20:23:09 +01003210 * Clear the balance status in fs_info and delete the balance item from disk.
3211 */
3212static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003213{
3214 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003215 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003216
3217 BUG_ON(!fs_info->balance_ctl);
3218
3219 spin_lock(&fs_info->balance_lock);
3220 fs_info->balance_ctl = NULL;
3221 spin_unlock(&fs_info->balance_lock);
3222
3223 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003224 ret = del_balance_item(fs_info);
3225 if (ret)
3226 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003227}
3228
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003229/*
3230 * Balance filters. Return 1 if chunk should be filtered out
3231 * (should not be balanced).
3232 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003233static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003234 struct btrfs_balance_args *bargs)
3235{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003236 chunk_type = chunk_to_extended(chunk_type) &
3237 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003238
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003239 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003240 return 0;
3241
3242 return 1;
3243}
3244
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003245static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003246 struct btrfs_balance_args *bargs)
3247{
3248 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003249 u64 chunk_used;
3250 u64 user_thresh_min;
3251 u64 user_thresh_max;
3252 int ret = 1;
3253
3254 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3255 chunk_used = btrfs_block_group_used(&cache->item);
3256
3257 if (bargs->usage_min == 0)
3258 user_thresh_min = 0;
3259 else
3260 user_thresh_min = div_factor_fine(cache->key.offset,
3261 bargs->usage_min);
3262
3263 if (bargs->usage_max == 0)
3264 user_thresh_max = 1;
3265 else if (bargs->usage_max > 100)
3266 user_thresh_max = cache->key.offset;
3267 else
3268 user_thresh_max = div_factor_fine(cache->key.offset,
3269 bargs->usage_max);
3270
3271 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3272 ret = 0;
3273
3274 btrfs_put_block_group(cache);
3275 return ret;
3276}
3277
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003278static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003279 u64 chunk_offset, struct btrfs_balance_args *bargs)
3280{
3281 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003282 u64 chunk_used, user_thresh;
3283 int ret = 1;
3284
3285 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3286 chunk_used = btrfs_block_group_used(&cache->item);
3287
David Sterbabc309462015-10-20 18:22:13 +02003288 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003289 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003290 else if (bargs->usage > 100)
3291 user_thresh = cache->key.offset;
3292 else
3293 user_thresh = div_factor_fine(cache->key.offset,
3294 bargs->usage);
3295
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003296 if (chunk_used < user_thresh)
3297 ret = 0;
3298
3299 btrfs_put_block_group(cache);
3300 return ret;
3301}
3302
Ilya Dryomov409d4042012-01-16 22:04:47 +02003303static int chunk_devid_filter(struct extent_buffer *leaf,
3304 struct btrfs_chunk *chunk,
3305 struct btrfs_balance_args *bargs)
3306{
3307 struct btrfs_stripe *stripe;
3308 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3309 int i;
3310
3311 for (i = 0; i < num_stripes; i++) {
3312 stripe = btrfs_stripe_nr(chunk, i);
3313 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3314 return 0;
3315 }
3316
3317 return 1;
3318}
3319
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003320/* [pstart, pend) */
3321static int chunk_drange_filter(struct extent_buffer *leaf,
3322 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003323 struct btrfs_balance_args *bargs)
3324{
3325 struct btrfs_stripe *stripe;
3326 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3327 u64 stripe_offset;
3328 u64 stripe_length;
3329 int factor;
3330 int i;
3331
3332 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3333 return 0;
3334
3335 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003336 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3337 factor = num_stripes / 2;
3338 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3339 factor = num_stripes - 1;
3340 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3341 factor = num_stripes - 2;
3342 } else {
3343 factor = num_stripes;
3344 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003345
3346 for (i = 0; i < num_stripes; i++) {
3347 stripe = btrfs_stripe_nr(chunk, i);
3348 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3349 continue;
3350
3351 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3352 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003353 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003354
3355 if (stripe_offset < bargs->pend &&
3356 stripe_offset + stripe_length > bargs->pstart)
3357 return 0;
3358 }
3359
3360 return 1;
3361}
3362
Ilya Dryomovea671762012-01-16 22:04:48 +02003363/* [vstart, vend) */
3364static int chunk_vrange_filter(struct extent_buffer *leaf,
3365 struct btrfs_chunk *chunk,
3366 u64 chunk_offset,
3367 struct btrfs_balance_args *bargs)
3368{
3369 if (chunk_offset < bargs->vend &&
3370 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3371 /* at least part of the chunk is inside this vrange */
3372 return 0;
3373
3374 return 1;
3375}
3376
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003377static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3378 struct btrfs_chunk *chunk,
3379 struct btrfs_balance_args *bargs)
3380{
3381 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3382
3383 if (bargs->stripes_min <= num_stripes
3384 && num_stripes <= bargs->stripes_max)
3385 return 0;
3386
3387 return 1;
3388}
3389
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003390static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003391 struct btrfs_balance_args *bargs)
3392{
3393 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3394 return 0;
3395
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003396 chunk_type = chunk_to_extended(chunk_type) &
3397 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003398
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003399 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003400 return 1;
3401
3402 return 0;
3403}
3404
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003405static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003406 struct extent_buffer *leaf,
3407 struct btrfs_chunk *chunk, u64 chunk_offset)
3408{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003409 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003410 struct btrfs_balance_args *bargs = NULL;
3411 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3412
3413 /* type filter */
3414 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3415 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3416 return 0;
3417 }
3418
3419 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3420 bargs = &bctl->data;
3421 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3422 bargs = &bctl->sys;
3423 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3424 bargs = &bctl->meta;
3425
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003426 /* profiles filter */
3427 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3428 chunk_profiles_filter(chunk_type, bargs)) {
3429 return 0;
3430 }
3431
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003432 /* usage filter */
3433 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003435 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003436 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003437 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003438 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003439 }
3440
Ilya Dryomov409d4042012-01-16 22:04:47 +02003441 /* devid filter */
3442 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3443 chunk_devid_filter(leaf, chunk, bargs)) {
3444 return 0;
3445 }
3446
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003447 /* drange filter, makes sense only with devid filter */
3448 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003449 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003450 return 0;
3451 }
3452
Ilya Dryomovea671762012-01-16 22:04:48 +02003453 /* vrange filter */
3454 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3455 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3456 return 0;
3457 }
3458
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003459 /* stripes filter */
3460 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3461 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3462 return 0;
3463 }
3464
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003465 /* soft profile changing mode */
3466 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3467 chunk_soft_convert_filter(chunk_type, bargs)) {
3468 return 0;
3469 }
3470
David Sterba7d824b62014-05-07 17:37:51 +02003471 /*
3472 * limited by count, must be the last filter
3473 */
3474 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3475 if (bargs->limit == 0)
3476 return 0;
3477 else
3478 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003479 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3480 /*
3481 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003482 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003483 * about the count of all chunks that satisfy the filters.
3484 */
3485 if (bargs->limit_max == 0)
3486 return 0;
3487 else
3488 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003489 }
3490
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003491 return 1;
3492}
3493
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003494static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003495{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003496 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003497 struct btrfs_root *chunk_root = fs_info->chunk_root;
3498 struct btrfs_root *dev_root = fs_info->dev_root;
3499 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003500 struct btrfs_device *device;
3501 u64 old_size;
3502 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003503 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003504 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003505 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003506 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003507 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003508 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003509 struct extent_buffer *leaf;
3510 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003511 int ret;
3512 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003513 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003514 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003515 u64 limit_data = bctl->data.limit;
3516 u64 limit_meta = bctl->meta.limit;
3517 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003518 u32 count_data = 0;
3519 u32 count_meta = 0;
3520 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003521 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003522
Chris Masonec44a352008-04-28 15:29:52 -04003523 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003524 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003525 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003526 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003527 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003528 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003529 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003530 btrfs_device_get_total_bytes(device) -
3531 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003532 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003533 continue;
3534
3535 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003536 if (ret == -ENOSPC)
3537 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003538 if (ret) {
3539 /* btrfs_shrink_device never returns ret > 0 */
3540 WARN_ON(ret > 0);
3541 goto error;
3542 }
Chris Masonec44a352008-04-28 15:29:52 -04003543
Yan, Zhenga22285a2010-05-16 10:48:46 -04003544 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003545 if (IS_ERR(trans)) {
3546 ret = PTR_ERR(trans);
3547 btrfs_info_in_rcu(fs_info,
3548 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3549 rcu_str_deref(device->name), ret,
3550 old_size, old_size - size_to_free);
3551 goto error;
3552 }
Chris Masonec44a352008-04-28 15:29:52 -04003553
3554 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003555 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003556 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003557 /* btrfs_grow_device never returns ret > 0 */
3558 WARN_ON(ret > 0);
3559 btrfs_info_in_rcu(fs_info,
3560 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3561 rcu_str_deref(device->name), ret,
3562 old_size, old_size - size_to_free);
3563 goto error;
3564 }
Chris Masonec44a352008-04-28 15:29:52 -04003565
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003566 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003567 }
3568
3569 /* step two, relocate all the chunks */
3570 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003571 if (!path) {
3572 ret = -ENOMEM;
3573 goto error;
3574 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003575
3576 /* zero out stat counters */
3577 spin_lock(&fs_info->balance_lock);
3578 memset(&bctl->stat, 0, sizeof(bctl->stat));
3579 spin_unlock(&fs_info->balance_lock);
3580again:
David Sterba7d824b62014-05-07 17:37:51 +02003581 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003582 /*
3583 * The single value limit and min/max limits use the same bytes
3584 * in the
3585 */
David Sterba7d824b62014-05-07 17:37:51 +02003586 bctl->data.limit = limit_data;
3587 bctl->meta.limit = limit_meta;
3588 bctl->sys.limit = limit_sys;
3589 }
Chris Masonec44a352008-04-28 15:29:52 -04003590 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3591 key.offset = (u64)-1;
3592 key.type = BTRFS_CHUNK_ITEM_KEY;
3593
Chris Masond3977122009-01-05 21:25:51 -05003594 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003595 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003596 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003597 ret = -ECANCELED;
3598 goto error;
3599 }
3600
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003601 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003602 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003603 if (ret < 0) {
3604 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003605 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003606 }
Chris Masonec44a352008-04-28 15:29:52 -04003607
3608 /*
3609 * this shouldn't happen, it means the last relocate
3610 * failed
3611 */
3612 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003613 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003614
3615 ret = btrfs_previous_item(chunk_root, path, 0,
3616 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003617 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003618 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003619 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003620 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003621 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003622
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003623 leaf = path->nodes[0];
3624 slot = path->slots[0];
3625 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3626
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003627 if (found_key.objectid != key.objectid) {
3628 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003629 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003630 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003631
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003632 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003633 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003634
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003635 if (!counting) {
3636 spin_lock(&fs_info->balance_lock);
3637 bctl->stat.considered++;
3638 spin_unlock(&fs_info->balance_lock);
3639 }
3640
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003641 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003642 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003643
David Sterbab3b4aa72011-04-21 01:20:15 +02003644 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003645 if (!ret) {
3646 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003647 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003648 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003649
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003650 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003651 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003652 spin_lock(&fs_info->balance_lock);
3653 bctl->stat.expected++;
3654 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003655
3656 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3657 count_data++;
3658 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3659 count_sys++;
3660 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3661 count_meta++;
3662
3663 goto loop;
3664 }
3665
3666 /*
3667 * Apply limit_min filter, no need to check if the LIMITS
3668 * filter is used, limit_min is 0 by default
3669 */
3670 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3671 count_data < bctl->data.limit_min)
3672 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3673 count_meta < bctl->meta.limit_min)
3674 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3675 count_sys < bctl->sys.limit_min)) {
3676 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003677 goto loop;
3678 }
3679
Liu Boa6f93c72017-11-15 16:28:11 -07003680 if (!chunk_reserved) {
3681 /*
3682 * We may be relocating the only data chunk we have,
3683 * which could potentially end up with losing data's
3684 * raid profile, so lets allocate an empty one in
3685 * advance.
3686 */
3687 ret = btrfs_may_alloc_data_chunk(fs_info,
3688 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003689 if (ret < 0) {
3690 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3691 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003692 } else if (ret == 1) {
3693 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003694 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003695 }
3696
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003697 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003698 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003699 if (ret && ret != -ENOSPC)
3700 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003701 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003702 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003703 } else {
3704 spin_lock(&fs_info->balance_lock);
3705 bctl->stat.completed++;
3706 spin_unlock(&fs_info->balance_lock);
3707 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003708loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003709 if (found_key.offset == 0)
3710 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003711 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003712 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003713
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003714 if (counting) {
3715 btrfs_release_path(path);
3716 counting = false;
3717 goto again;
3718 }
Chris Masonec44a352008-04-28 15:29:52 -04003719error:
3720 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003721 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003722 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003723 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003724 if (!ret)
3725 ret = -ENOSPC;
3726 }
3727
Chris Masonec44a352008-04-28 15:29:52 -04003728 return ret;
3729}
3730
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003731/**
3732 * alloc_profile_is_valid - see if a given profile is valid and reduced
3733 * @flags: profile to validate
3734 * @extended: if true @flags is treated as an extended profile
3735 */
3736static int alloc_profile_is_valid(u64 flags, int extended)
3737{
3738 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3739 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3740
3741 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3742
3743 /* 1) check that all other bits are zeroed */
3744 if (flags & ~mask)
3745 return 0;
3746
3747 /* 2) see if profile is reduced */
3748 if (flags == 0)
3749 return !extended; /* "0" is valid for usual profiles */
3750
3751 /* true if exactly one bit set */
3752 return (flags & (flags - 1)) == 0;
3753}
3754
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003755static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3756{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003757 /* cancel requested || normal exit path */
3758 return atomic_read(&fs_info->balance_cancel_req) ||
3759 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3760 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003761}
3762
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003763/* Non-zero return value signifies invalidity */
3764static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3765 u64 allowed)
3766{
3767 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3768 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3769 (bctl_arg->target & ~allowed)));
3770}
3771
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003772/*
David Sterbadccdb072018-03-21 00:20:05 +01003773 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003774 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003775int btrfs_balance(struct btrfs_fs_info *fs_info,
3776 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003777 struct btrfs_ioctl_balance_args *bargs)
3778{
Adam Borowski14506122017-03-07 23:34:44 +01003779 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003780 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003781 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003782 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003783 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003784 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003785
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003786 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003787 atomic_read(&fs_info->balance_pause_req) ||
3788 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003789 ret = -EINVAL;
3790 goto out;
3791 }
3792
Ilya Dryomove4837f82012-03-27 17:09:17 +03003793 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3794 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3795 mixed = 1;
3796
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003797 /*
3798 * In case of mixed groups both data and meta should be picked,
3799 * and identical options should be given for both of them.
3800 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003801 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3802 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003803 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3804 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3805 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003806 btrfs_err(fs_info,
3807 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003808 ret = -EINVAL;
3809 goto out;
3810 }
3811 }
3812
Stefan Behrens8dabb742012-11-06 13:15:27 +01003813 num_devices = fs_info->fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01003814 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003815 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3816 BUG_ON(num_devices < 1);
3817 num_devices--;
3818 }
David Sterba7e79cb82018-03-24 02:11:38 +01003819 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003820 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3821 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003822 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003823 if (num_devices > 2)
3824 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3825 if (num_devices > 3)
3826 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3827 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003828 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003829 btrfs_err(fs_info,
3830 "unable to start balance with target data profile %llu",
3831 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003832 ret = -EINVAL;
3833 goto out;
3834 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003835 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003836 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003837 "unable to start balance with target metadata profile %llu",
3838 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003839 ret = -EINVAL;
3840 goto out;
3841 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003842 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003843 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003844 "unable to start balance with target system profile %llu",
3845 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003846 ret = -EINVAL;
3847 goto out;
3848 }
3849
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003850 /* allow to reduce meta or sys integrity only if force set */
3851 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003852 BTRFS_BLOCK_GROUP_RAID10 |
3853 BTRFS_BLOCK_GROUP_RAID5 |
3854 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003855 do {
3856 seq = read_seqbegin(&fs_info->profiles_lock);
3857
3858 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3859 (fs_info->avail_system_alloc_bits & allowed) &&
3860 !(bctl->sys.target & allowed)) ||
3861 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3862 (fs_info->avail_metadata_alloc_bits & allowed) &&
3863 !(bctl->meta.target & allowed))) {
3864 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003865 btrfs_info(fs_info,
3866 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003867 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003868 btrfs_err(fs_info,
3869 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003870 ret = -EINVAL;
3871 goto out;
3872 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003873 }
Miao Xiede98ced2013-01-29 10:13:12 +00003874 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003875
Adam Borowski14506122017-03-07 23:34:44 +01003876 /* if we're not converting, the target field is uninitialized */
3877 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3878 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3879 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3880 bctl->data.target : fs_info->avail_data_alloc_bits;
3881 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3882 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003883 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003884 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003885 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003886 }
3887
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003888 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003889 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003890 goto out;
3891
Ilya Dryomov59641012012-01-16 22:04:48 +02003892 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3893 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01003894 BUG_ON(fs_info->balance_ctl);
3895 spin_lock(&fs_info->balance_lock);
3896 fs_info->balance_ctl = bctl;
3897 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02003898 } else {
3899 BUG_ON(ret != -EEXIST);
3900 spin_lock(&fs_info->balance_lock);
3901 update_balance_args(bctl);
3902 spin_unlock(&fs_info->balance_lock);
3903 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003904
David Sterba3009a622018-03-21 01:31:04 +01003905 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
3906 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003907 mutex_unlock(&fs_info->balance_mutex);
3908
3909 ret = __btrfs_balance(fs_info);
3910
3911 mutex_lock(&fs_info->balance_mutex);
David Sterba3009a622018-03-21 01:31:04 +01003912 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003913
3914 if (bargs) {
3915 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01003916 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003917 }
3918
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003919 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3920 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01003921 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003922 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003923 }
3924
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003925 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003926
3927 return ret;
3928out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003929 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01003930 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003931 else
Ilya Dryomov59641012012-01-16 22:04:48 +02003932 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01003933 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3934
Ilya Dryomov59641012012-01-16 22:04:48 +02003935 return ret;
3936}
3937
3938static int balance_kthread(void *data)
3939{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003940 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003941 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003942
Ilya Dryomov59641012012-01-16 22:04:48 +02003943 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003944 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003945 btrfs_info(fs_info, "continuing balance");
David Sterba6fcf6e22018-05-07 17:44:03 +02003946 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003947 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003948 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003949
Ilya Dryomov59641012012-01-16 22:04:48 +02003950 return ret;
3951}
3952
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003953int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3954{
3955 struct task_struct *tsk;
3956
David Sterba1354e1a2018-03-21 02:29:13 +01003957 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003958 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01003959 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003960 return 0;
3961 }
David Sterba1354e1a2018-03-21 02:29:13 +01003962 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003963
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003964 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003965 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003966 return 0;
3967 }
3968
Anand Jain02ee6542018-05-17 15:16:51 +08003969 /*
3970 * A ro->rw remount sequence should continue with the paused balance
3971 * regardless of who pauses it, system or the user as of now, so set
3972 * the resume flag.
3973 */
3974 spin_lock(&fs_info->balance_lock);
3975 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
3976 spin_unlock(&fs_info->balance_lock);
3977
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003978 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303979 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003980}
3981
Ilya Dryomov68310a52012-06-22 12:24:12 -06003982int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003983{
Ilya Dryomov59641012012-01-16 22:04:48 +02003984 struct btrfs_balance_control *bctl;
3985 struct btrfs_balance_item *item;
3986 struct btrfs_disk_balance_args disk_bargs;
3987 struct btrfs_path *path;
3988 struct extent_buffer *leaf;
3989 struct btrfs_key key;
3990 int ret;
3991
3992 path = btrfs_alloc_path();
3993 if (!path)
3994 return -ENOMEM;
3995
Ilya Dryomov68310a52012-06-22 12:24:12 -06003996 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003997 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003998 key.offset = 0;
3999
4000 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4001 if (ret < 0)
4002 goto out;
4003 if (ret > 0) { /* ret = -ENOENT; */
4004 ret = 0;
4005 goto out;
4006 }
4007
Ilya Dryomov59641012012-01-16 22:04:48 +02004008 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4009 if (!bctl) {
4010 ret = -ENOMEM;
4011 goto out;
4012 }
4013
Ilya Dryomov59641012012-01-16 22:04:48 +02004014 leaf = path->nodes[0];
4015 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4016
Ilya Dryomov68310a52012-06-22 12:24:12 -06004017 bctl->flags = btrfs_balance_flags(leaf, item);
4018 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004019
4020 btrfs_balance_data(leaf, item, &disk_bargs);
4021 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4022 btrfs_balance_meta(leaf, item, &disk_bargs);
4023 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4024 btrfs_balance_sys(leaf, item, &disk_bargs);
4025 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4026
David Sterbaeee95e32018-03-20 20:07:58 +01004027 /*
4028 * This should never happen, as the paused balance state is recovered
4029 * during mount without any chance of other exclusive ops to collide.
4030 *
4031 * This gives the exclusive op status to balance and keeps in paused
4032 * state until user intervention (cancel or umount). If the ownership
4033 * cannot be assigned, show a message but do not fail. The balance
4034 * is in a paused state and must have fs_info::balance_ctl properly
4035 * set up.
4036 */
4037 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4038 btrfs_warn(fs_info,
4039 "cannot set exclusive op status to balance, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004040
Ilya Dryomov68310a52012-06-22 12:24:12 -06004041 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004042 BUG_ON(fs_info->balance_ctl);
4043 spin_lock(&fs_info->balance_lock);
4044 fs_info->balance_ctl = bctl;
4045 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004046 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004047out:
4048 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004049 return ret;
4050}
4051
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004052int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4053{
4054 int ret = 0;
4055
4056 mutex_lock(&fs_info->balance_mutex);
4057 if (!fs_info->balance_ctl) {
4058 mutex_unlock(&fs_info->balance_mutex);
4059 return -ENOTCONN;
4060 }
4061
David Sterba3009a622018-03-21 01:31:04 +01004062 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004063 atomic_inc(&fs_info->balance_pause_req);
4064 mutex_unlock(&fs_info->balance_mutex);
4065
4066 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004067 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004068
4069 mutex_lock(&fs_info->balance_mutex);
4070 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004071 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004072 atomic_dec(&fs_info->balance_pause_req);
4073 } else {
4074 ret = -ENOTCONN;
4075 }
4076
4077 mutex_unlock(&fs_info->balance_mutex);
4078 return ret;
4079}
4080
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004081int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4082{
4083 mutex_lock(&fs_info->balance_mutex);
4084 if (!fs_info->balance_ctl) {
4085 mutex_unlock(&fs_info->balance_mutex);
4086 return -ENOTCONN;
4087 }
4088
David Sterbacf7d20f2018-03-21 01:45:32 +01004089 /*
4090 * A paused balance with the item stored on disk can be resumed at
4091 * mount time if the mount is read-write. Otherwise it's still paused
4092 * and we must not allow cancelling as it deletes the item.
4093 */
4094 if (sb_rdonly(fs_info->sb)) {
4095 mutex_unlock(&fs_info->balance_mutex);
4096 return -EROFS;
4097 }
4098
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004099 atomic_inc(&fs_info->balance_cancel_req);
4100 /*
4101 * if we are running just wait and return, balance item is
4102 * deleted in btrfs_balance in this case
4103 */
David Sterba3009a622018-03-21 01:31:04 +01004104 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004105 mutex_unlock(&fs_info->balance_mutex);
4106 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004107 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004108 mutex_lock(&fs_info->balance_mutex);
4109 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004110 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004111 /*
4112 * Lock released to allow other waiters to continue, we'll
4113 * reexamine the status again.
4114 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004115 mutex_lock(&fs_info->balance_mutex);
4116
David Sterbaa17c95d2018-03-20 17:28:05 +01004117 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004118 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004119 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4120 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004121 }
4122
David Sterba3009a622018-03-21 01:31:04 +01004123 BUG_ON(fs_info->balance_ctl ||
4124 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004125 atomic_dec(&fs_info->balance_cancel_req);
4126 mutex_unlock(&fs_info->balance_mutex);
4127 return 0;
4128}
4129
Stefan Behrens803b2f52013-08-15 17:11:21 +02004130static int btrfs_uuid_scan_kthread(void *data)
4131{
4132 struct btrfs_fs_info *fs_info = data;
4133 struct btrfs_root *root = fs_info->tree_root;
4134 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004135 struct btrfs_path *path = NULL;
4136 int ret = 0;
4137 struct extent_buffer *eb;
4138 int slot;
4139 struct btrfs_root_item root_item;
4140 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004141 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004142
4143 path = btrfs_alloc_path();
4144 if (!path) {
4145 ret = -ENOMEM;
4146 goto out;
4147 }
4148
4149 key.objectid = 0;
4150 key.type = BTRFS_ROOT_ITEM_KEY;
4151 key.offset = 0;
4152
Stefan Behrens803b2f52013-08-15 17:11:21 +02004153 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004154 ret = btrfs_search_forward(root, &key, path,
4155 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004156 if (ret) {
4157 if (ret > 0)
4158 ret = 0;
4159 break;
4160 }
4161
4162 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4163 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4164 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4165 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4166 goto skip;
4167
4168 eb = path->nodes[0];
4169 slot = path->slots[0];
4170 item_size = btrfs_item_size_nr(eb, slot);
4171 if (item_size < sizeof(root_item))
4172 goto skip;
4173
Stefan Behrens803b2f52013-08-15 17:11:21 +02004174 read_extent_buffer(eb, &root_item,
4175 btrfs_item_ptr_offset(eb, slot),
4176 (int)sizeof(root_item));
4177 if (btrfs_root_refs(&root_item) == 0)
4178 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004179
4180 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4181 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4182 if (trans)
4183 goto update_tree;
4184
4185 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004186 /*
4187 * 1 - subvol uuid item
4188 * 1 - received_subvol uuid item
4189 */
4190 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4191 if (IS_ERR(trans)) {
4192 ret = PTR_ERR(trans);
4193 break;
4194 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004195 continue;
4196 } else {
4197 goto skip;
4198 }
4199update_tree:
4200 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004201 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004202 root_item.uuid,
4203 BTRFS_UUID_KEY_SUBVOL,
4204 key.objectid);
4205 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004206 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004207 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004208 break;
4209 }
4210 }
4211
4212 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004213 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004214 root_item.received_uuid,
4215 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4216 key.objectid);
4217 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004218 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004219 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004220 break;
4221 }
4222 }
4223
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004224skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004225 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004226 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004227 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004228 if (ret)
4229 break;
4230 }
4231
Stefan Behrens803b2f52013-08-15 17:11:21 +02004232 btrfs_release_path(path);
4233 if (key.offset < (u64)-1) {
4234 key.offset++;
4235 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4236 key.offset = 0;
4237 key.type = BTRFS_ROOT_ITEM_KEY;
4238 } else if (key.objectid < (u64)-1) {
4239 key.offset = 0;
4240 key.type = BTRFS_ROOT_ITEM_KEY;
4241 key.objectid++;
4242 } else {
4243 break;
4244 }
4245 cond_resched();
4246 }
4247
4248out:
4249 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004250 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004251 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004252 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004253 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004254 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004255 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004256 up(&fs_info->uuid_tree_rescan_sem);
4257 return 0;
4258}
4259
Stefan Behrens70f80172013-08-15 17:11:23 +02004260/*
4261 * Callback for btrfs_uuid_tree_iterate().
4262 * returns:
4263 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004264 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004265 * > 0 if the check failed, which means the caller shall remove the entry.
4266 */
4267static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4268 u8 *uuid, u8 type, u64 subid)
4269{
4270 struct btrfs_key key;
4271 int ret = 0;
4272 struct btrfs_root *subvol_root;
4273
4274 if (type != BTRFS_UUID_KEY_SUBVOL &&
4275 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4276 goto out;
4277
4278 key.objectid = subid;
4279 key.type = BTRFS_ROOT_ITEM_KEY;
4280 key.offset = (u64)-1;
4281 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4282 if (IS_ERR(subvol_root)) {
4283 ret = PTR_ERR(subvol_root);
4284 if (ret == -ENOENT)
4285 ret = 1;
4286 goto out;
4287 }
4288
4289 switch (type) {
4290 case BTRFS_UUID_KEY_SUBVOL:
4291 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4292 ret = 1;
4293 break;
4294 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4295 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4296 BTRFS_UUID_SIZE))
4297 ret = 1;
4298 break;
4299 }
4300
4301out:
4302 return ret;
4303}
4304
4305static int btrfs_uuid_rescan_kthread(void *data)
4306{
4307 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4308 int ret;
4309
4310 /*
4311 * 1st step is to iterate through the existing UUID tree and
4312 * to delete all entries that contain outdated data.
4313 * 2nd step is to add all missing entries to the UUID tree.
4314 */
4315 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4316 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004317 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004318 up(&fs_info->uuid_tree_rescan_sem);
4319 return ret;
4320 }
4321 return btrfs_uuid_scan_kthread(data);
4322}
4323
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004324int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4325{
4326 struct btrfs_trans_handle *trans;
4327 struct btrfs_root *tree_root = fs_info->tree_root;
4328 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004329 struct task_struct *task;
4330 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004331
4332 /*
4333 * 1 - root node
4334 * 1 - root item
4335 */
4336 trans = btrfs_start_transaction(tree_root, 2);
4337 if (IS_ERR(trans))
4338 return PTR_ERR(trans);
4339
4340 uuid_root = btrfs_create_tree(trans, fs_info,
4341 BTRFS_UUID_TREE_OBJECTID);
4342 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004343 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004344 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004345 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004346 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004347 }
4348
4349 fs_info->uuid_root = uuid_root;
4350
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004351 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004352 if (ret)
4353 return ret;
4354
4355 down(&fs_info->uuid_tree_rescan_sem);
4356 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4357 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004358 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004359 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004360 up(&fs_info->uuid_tree_rescan_sem);
4361 return PTR_ERR(task);
4362 }
4363
4364 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004365}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004366
Stefan Behrens70f80172013-08-15 17:11:23 +02004367int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4368{
4369 struct task_struct *task;
4370
4371 down(&fs_info->uuid_tree_rescan_sem);
4372 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4373 if (IS_ERR(task)) {
4374 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004375 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004376 up(&fs_info->uuid_tree_rescan_sem);
4377 return PTR_ERR(task);
4378 }
4379
4380 return 0;
4381}
4382
Chris Mason8f18cf12008-04-25 16:53:30 -04004383/*
4384 * shrinking a device means finding all of the device extents past
4385 * the new size, and then following the back refs to the chunks.
4386 * The chunk relocation code actually frees the device extent
4387 */
4388int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004390 struct btrfs_fs_info *fs_info = device->fs_info;
4391 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004392 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004393 struct btrfs_dev_extent *dev_extent = NULL;
4394 struct btrfs_path *path;
4395 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004396 u64 chunk_offset;
4397 int ret;
4398 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004399 int failed = 0;
4400 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004401 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004402 struct extent_buffer *l;
4403 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004404 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004405 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004406 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004407 u64 diff;
4408
4409 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004410 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004411
Anand Jain401e29c2017-12-04 12:54:55 +08004412 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004413 return -EINVAL;
4414
Chris Mason8f18cf12008-04-25 16:53:30 -04004415 path = btrfs_alloc_path();
4416 if (!path)
4417 return -ENOMEM;
4418
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004419 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004420
David Sterba34441362016-10-04 19:34:27 +02004421 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004422
Miao Xie7cc8e582014-09-03 21:35:38 +08004423 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004424 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004425 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004426 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004427 }
David Sterba34441362016-10-04 19:34:27 +02004428 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004429
Josef Bacikba1bf482009-09-11 16:11:19 -04004430again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004431 key.objectid = device->devid;
4432 key.offset = (u64)-1;
4433 key.type = BTRFS_DEV_EXTENT_KEY;
4434
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004435 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004436 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004437 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004438 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004439 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004440 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004441 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004442
4443 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004444 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004445 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004446 if (ret < 0)
4447 goto done;
4448 if (ret) {
4449 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004450 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004451 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004452 }
4453
4454 l = path->nodes[0];
4455 slot = path->slots[0];
4456 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4457
Josef Bacikba1bf482009-09-11 16:11:19 -04004458 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004459 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004460 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004461 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004462 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004463
4464 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4465 length = btrfs_dev_extent_length(l, dev_extent);
4466
Josef Bacikba1bf482009-09-11 16:11:19 -04004467 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004468 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004469 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004470 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004471 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004472
Chris Mason8f18cf12008-04-25 16:53:30 -04004473 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004474 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004475
Liu Boa6f93c72017-11-15 16:28:11 -07004476 /*
4477 * We may be relocating the only data chunk we have,
4478 * which could potentially end up with losing data's
4479 * raid profile, so lets allocate an empty one in
4480 * advance.
4481 */
4482 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4483 if (ret < 0) {
4484 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4485 goto done;
4486 }
4487
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004488 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4489 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004490 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004491 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004492 if (ret == -ENOSPC)
4493 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004494 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004495
4496 if (failed && !retried) {
4497 failed = 0;
4498 retried = true;
4499 goto again;
4500 } else if (failed && retried) {
4501 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004502 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004503 }
4504
Chris Balld6397ba2009-04-27 07:29:03 -04004505 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004506 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004507 if (IS_ERR(trans)) {
4508 ret = PTR_ERR(trans);
4509 goto done;
4510 }
4511
David Sterba34441362016-10-04 19:34:27 +02004512 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004513
4514 /*
4515 * We checked in the above loop all device extents that were already in
4516 * the device tree. However before we have updated the device's
4517 * total_bytes to the new size, we might have had chunk allocations that
4518 * have not complete yet (new block groups attached to transaction
4519 * handles), and therefore their device extents were not yet in the
4520 * device tree and we missed them in the loop above. So if we have any
4521 * pending chunk using a device extent that overlaps the device range
4522 * that we can not use anymore, commit the current transaction and
4523 * repeat the search on the device tree - this way we guarantee we will
4524 * not have chunks using device extents that end beyond 'new_size'.
4525 */
4526 if (!checked_pending_chunks) {
4527 u64 start = new_size;
4528 u64 len = old_size - new_size;
4529
Jeff Mahoney499f3772015-06-15 09:41:17 -04004530 if (contains_pending_extent(trans->transaction, device,
4531 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004532 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004533 checked_pending_chunks = true;
4534 failed = 0;
4535 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004536 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004537 if (ret)
4538 goto done;
4539 goto again;
4540 }
4541 }
4542
Miao Xie7cc8e582014-09-03 21:35:38 +08004543 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004544 if (list_empty(&device->resized_list))
4545 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004546 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004547
Chris Balld6397ba2009-04-27 07:29:03 -04004548 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004549 btrfs_set_super_total_bytes(super_copy,
4550 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004551 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004552
4553 /* Now btrfs_update_device() will change the on-disk size. */
4554 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004555 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004556done:
4557 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004558 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004559 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004560 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004561 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004562 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004563 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004564 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004565 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004566 return ret;
4567}
4568
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004569static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004570 struct btrfs_key *key,
4571 struct btrfs_chunk *chunk, int item_size)
4572{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004573 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004574 struct btrfs_disk_key disk_key;
4575 u32 array_size;
4576 u8 *ptr;
4577
David Sterba34441362016-10-04 19:34:27 +02004578 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004579 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004580 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004581 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004582 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004583 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004584 }
Chris Mason0b86a832008-03-24 15:01:56 -04004585
4586 ptr = super_copy->sys_chunk_array + array_size;
4587 btrfs_cpu_key_to_disk(&disk_key, key);
4588 memcpy(ptr, &disk_key, sizeof(disk_key));
4589 ptr += sizeof(disk_key);
4590 memcpy(ptr, chunk, item_size);
4591 item_size += sizeof(disk_key);
4592 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004593 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004594
Chris Mason0b86a832008-03-24 15:01:56 -04004595 return 0;
4596}
4597
Miao Xieb2117a32011-01-05 10:07:28 +00004598/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004599 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004600 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004601static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004602{
Arne Jansen73c5de02011-04-12 12:07:57 +02004603 const struct btrfs_device_info *di_a = a;
4604 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004605
Arne Jansen73c5de02011-04-12 12:07:57 +02004606 if (di_a->max_avail > di_b->max_avail)
4607 return -1;
4608 if (di_a->max_avail < di_b->max_avail)
4609 return 1;
4610 if (di_a->total_avail > di_b->total_avail)
4611 return -1;
4612 if (di_a->total_avail < di_b->total_avail)
4613 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004614 return 0;
4615}
4616
David Woodhouse53b381b2013-01-29 18:40:14 -05004617static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4618{
Zhao Leiffe2d202015-01-20 15:11:44 +08004619 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004620 return;
4621
Miao Xieceda0862013-04-11 10:30:16 +00004622 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004623}
4624
Qu Wenruo062d4d12018-01-30 18:20:46 +08004625#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004626 - sizeof(struct btrfs_chunk)) \
4627 / sizeof(struct btrfs_stripe) + 1)
4628
4629#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4630 - 2 * sizeof(struct btrfs_disk_key) \
4631 - 2 * sizeof(struct btrfs_chunk)) \
4632 / sizeof(struct btrfs_stripe) + 1)
4633
Miao Xieb2117a32011-01-05 10:07:28 +00004634static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004635 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004636{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004637 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004638 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004639 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004640 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004641 struct extent_map_tree *em_tree;
4642 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004643 struct btrfs_device_info *devices_info = NULL;
4644 u64 total_avail;
4645 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004646 int data_stripes; /* number of stripes that count for
4647 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004648 int sub_stripes; /* sub_stripes info for map */
4649 int dev_stripes; /* stripes per dev */
4650 int devs_max; /* max devs to use */
4651 int devs_min; /* min devs needed */
4652 int devs_increment; /* ndevs has to be a multiple of this */
4653 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004654 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004655 u64 max_stripe_size;
4656 u64 max_chunk_size;
4657 u64 stripe_size;
4658 u64 num_bytes;
4659 int ndevs;
4660 int i;
4661 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004662 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004663
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004664 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004665
Qu Wenruo4117f202018-01-22 13:50:54 +08004666 if (list_empty(&fs_devices->alloc_list)) {
4667 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4668 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004669 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004670 }
Miao Xieb2117a32011-01-05 10:07:28 +00004671
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004672 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004673
Liu Bo31e50222012-11-21 14:18:10 +00004674 sub_stripes = btrfs_raid_array[index].sub_stripes;
4675 dev_stripes = btrfs_raid_array[index].dev_stripes;
4676 devs_max = btrfs_raid_array[index].devs_max;
4677 devs_min = btrfs_raid_array[index].devs_min;
4678 devs_increment = btrfs_raid_array[index].devs_increment;
4679 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004680
4681 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004682 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004683 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004684 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004685 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004686 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004687 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004688 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4689 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004690 else
Byongho Leeee221842015-12-15 01:42:10 +09004691 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004692 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004693 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004694 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004695 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004696 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004697 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004698 if (!devs_max)
4699 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004700 } else {
David Sterba351fd352014-05-15 16:48:20 +02004701 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004702 type);
4703 BUG_ON(1);
4704 }
4705
4706 /* we don't want a chunk larger than 10% of writeable space */
4707 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4708 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004709
David Sterba31e818f2015-02-20 18:00:26 +01004710 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004711 GFP_NOFS);
4712 if (!devices_info)
4713 return -ENOMEM;
4714
Arne Jansen73c5de02011-04-12 12:07:57 +02004715 /*
4716 * in the first pass through the devices list, we gather information
4717 * about the available holes on each device.
4718 */
4719 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004720 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004721 u64 max_avail;
4722 u64 dev_offset;
4723
Anand Jainebbede42017-12-04 12:54:52 +08004724 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004725 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004726 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004727 continue;
4728 }
4729
Anand Jaine12c9622017-12-04 12:54:53 +08004730 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4731 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004732 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004733 continue;
4734
4735 if (device->total_bytes > device->bytes_used)
4736 total_avail = device->total_bytes - device->bytes_used;
4737 else
4738 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004739
4740 /* If there is no space on this device, skip it. */
4741 if (total_avail == 0)
4742 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004743
Josef Bacik6df9a952013-06-27 13:22:46 -04004744 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004745 max_stripe_size * dev_stripes,
4746 &dev_offset, &max_avail);
4747 if (ret && ret != -ENOSPC)
4748 goto error;
4749
4750 if (ret == 0)
4751 max_avail = max_stripe_size * dev_stripes;
4752
Qu Wenruo4117f202018-01-22 13:50:54 +08004753 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4754 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4755 btrfs_debug(info,
4756 "%s: devid %llu has no free space, have=%llu want=%u",
4757 __func__, device->devid, max_avail,
4758 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004759 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004760 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004761
Eric Sandeen063d0062013-01-31 00:55:01 +00004762 if (ndevs == fs_devices->rw_devices) {
4763 WARN(1, "%s: found more than %llu devices\n",
4764 __func__, fs_devices->rw_devices);
4765 break;
4766 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004767 devices_info[ndevs].dev_offset = dev_offset;
4768 devices_info[ndevs].max_avail = max_avail;
4769 devices_info[ndevs].total_avail = total_avail;
4770 devices_info[ndevs].dev = device;
4771 ++ndevs;
4772 }
4773
4774 /*
4775 * now sort the devices by hole size / available space
4776 */
4777 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4778 btrfs_cmp_device_info, NULL);
4779
4780 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004781 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004782
Qu Wenruoba89b802018-01-31 13:56:15 +08004783 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004784 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004785 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4786 btrfs_debug(info,
4787 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004788 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004789 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004790 goto error;
4791 }
4792
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004793 ndevs = min(ndevs, devs_max);
4794
Arne Jansen73c5de02011-04-12 12:07:57 +02004795 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004796 * The primary goal is to maximize the number of stripes, so use as
4797 * many devices as possible, even if the stripes are not maximum sized.
4798 *
4799 * The DUP profile stores more than one stripe per device, the
4800 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004801 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004802 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004803 num_stripes = ndevs * dev_stripes;
4804
David Woodhouse53b381b2013-01-29 18:40:14 -05004805 /*
4806 * this will have to be fixed for RAID1 and RAID10 over
4807 * more drives
4808 */
4809 data_stripes = num_stripes / ncopies;
4810
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004811 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004812 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004813
4814 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004815 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004816
4817 /*
4818 * Use the number of data stripes to figure out how big this chunk
4819 * is really going to be in terms of logical address space,
4820 * and compare that answer with the max chunk size
4821 */
4822 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004823 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 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004826 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004827
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004828 /*
4829 * But don't go higher than the limits we found while searching
4830 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004831 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004832 stripe_size = min(devices_info[ndevs - 1].max_avail,
4833 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004834 }
4835
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004836 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004837 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004838
Miao Xieb2117a32011-01-05 10:07:28 +00004839 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4840 if (!map) {
4841 ret = -ENOMEM;
4842 goto error;
4843 }
4844 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004845
Arne Jansen73c5de02011-04-12 12:07:57 +02004846 for (i = 0; i < ndevs; ++i) {
4847 for (j = 0; j < dev_stripes; ++j) {
4848 int s = i * dev_stripes + j;
4849 map->stripes[s].dev = devices_info[i].dev;
4850 map->stripes[s].physical = devices_info[i].dev_offset +
4851 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004852 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004853 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004854 map->stripe_len = BTRFS_STRIPE_LEN;
4855 map->io_align = BTRFS_STRIPE_LEN;
4856 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004857 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004858 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004859
David Woodhouse53b381b2013-01-29 18:40:14 -05004860 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004861
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004862 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004863
David Sterba172ddd62011-04-21 00:48:27 +02004864 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004865 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004866 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004867 ret = -ENOMEM;
4868 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004869 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004870 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004871 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004872 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004873 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004874 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004875 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004876 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004877
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004878 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004879 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004880 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004881 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004882 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004883 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004884 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004885 }
Yan Zheng2b820322008-11-17 21:11:30 -05004886
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004887 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4888 refcount_inc(&em->refs);
4889 write_unlock(&em_tree->lock);
4890
Nikolay Borisov01744842017-07-27 14:22:11 +03004891 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004892 if (ret)
4893 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004894
Miao Xie7cc8e582014-09-03 21:35:38 +08004895 for (i = 0; i < map->num_stripes; i++) {
4896 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4897 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4898 }
Miao Xie43530c42014-09-03 21:35:36 +08004899
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004900 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004901
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004902 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004903 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004904
Miao Xieb2117a32011-01-05 10:07:28 +00004905 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004906 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004907
Josef Bacik6df9a952013-06-27 13:22:46 -04004908error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004909 write_lock(&em_tree->lock);
4910 remove_extent_mapping(em_tree, em);
4911 write_unlock(&em_tree->lock);
4912
4913 /* One for our allocation */
4914 free_extent_map(em);
4915 /* One for the tree reference */
4916 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004917 /* One for the pending_chunks list reference */
4918 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004919error:
Miao Xieb2117a32011-01-05 10:07:28 +00004920 kfree(devices_info);
4921 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004922}
4923
Josef Bacik6df9a952013-06-27 13:22:46 -04004924int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004925 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004926 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004927{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004928 struct btrfs_root *extent_root = fs_info->extent_root;
4929 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004930 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004931 struct btrfs_device *device;
4932 struct btrfs_chunk *chunk;
4933 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004934 struct extent_map *em;
4935 struct map_lookup *map;
4936 size_t item_size;
4937 u64 dev_offset;
4938 u64 stripe_size;
4939 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004940 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004941
Liu Bo592d92e2017-03-14 13:33:55 -07004942 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4943 if (IS_ERR(em))
4944 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004945
Jeff Mahoney95617d62015-06-03 10:55:48 -04004946 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004947 item_size = btrfs_chunk_item_size(map->num_stripes);
4948 stripe_size = em->orig_block_len;
4949
4950 chunk = kzalloc(item_size, GFP_NOFS);
4951 if (!chunk) {
4952 ret = -ENOMEM;
4953 goto out;
4954 }
4955
Filipe Manana50460e32015-11-20 10:42:47 +00004956 /*
4957 * Take the device list mutex to prevent races with the final phase of
4958 * a device replace operation that replaces the device object associated
4959 * with the map's stripes, because the device object's id can change
4960 * at any time during that final phase of the device replace operation
4961 * (dev-replace.c:btrfs_dev_replace_finishing()).
4962 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004963 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004964 for (i = 0; i < map->num_stripes; i++) {
4965 device = map->stripes[i].dev;
4966 dev_offset = map->stripes[i].physical;
4967
Yan Zheng2b820322008-11-17 21:11:30 -05004968 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004969 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004970 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004971 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4972 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004973 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004974 break;
4975 }
4976 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004977 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004978 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004979 }
4980
Yan Zheng2b820322008-11-17 21:11:30 -05004981 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004982 for (i = 0; i < map->num_stripes; i++) {
4983 device = map->stripes[i].dev;
4984 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004985
4986 btrfs_set_stack_stripe_devid(stripe, device->devid);
4987 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4988 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4989 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004990 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004991 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004992
4993 btrfs_set_stack_chunk_length(chunk, chunk_size);
4994 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4995 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4996 btrfs_set_stack_chunk_type(chunk, map->type);
4997 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4998 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4999 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005000 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005001 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5002
5003 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5004 key.type = BTRFS_CHUNK_ITEM_KEY;
5005 key.offset = chunk_offset;
5006
5007 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005008 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5009 /*
5010 * TODO: Cleanup of inserted chunk root in case of
5011 * failure.
5012 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005013 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005014 }
liubo1abe9b82011-03-24 11:18:59 +00005015
Josef Bacik6df9a952013-06-27 13:22:46 -04005016out:
Yan Zheng2b820322008-11-17 21:11:30 -05005017 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005018 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005019 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005020}
5021
5022/*
5023 * Chunk allocation falls into two parts. The first part does works
5024 * that make the new allocated chunk useable, but not do any operation
5025 * that modifies the chunk tree. The second part does the works that
5026 * require modifying the chunk tree. This division is important for the
5027 * bootstrap process of adding storage to a seed btrfs.
5028 */
5029int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005030 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005031{
5032 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005033
David Sterbaa32bf9a2018-03-16 02:21:22 +01005034 lockdep_assert_held(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005035 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005036 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005037}
5038
Chris Masond3977122009-01-05 21:25:51 -05005039static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005040 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005041{
5042 u64 chunk_offset;
5043 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005044 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005045 int ret;
5046
Josef Bacik6df9a952013-06-27 13:22:46 -04005047 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005048 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005049 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005050 if (ret)
5051 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005052
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005053 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005054 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005055 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005056 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005057}
5058
Miao Xied20983b2014-07-03 18:22:13 +08005059static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5060{
5061 int max_errors;
5062
5063 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5064 BTRFS_BLOCK_GROUP_RAID10 |
5065 BTRFS_BLOCK_GROUP_RAID5 |
5066 BTRFS_BLOCK_GROUP_DUP)) {
5067 max_errors = 1;
5068 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5069 max_errors = 2;
5070 } else {
5071 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005072 }
5073
Miao Xied20983b2014-07-03 18:22:13 +08005074 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005075}
5076
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005077int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005078{
5079 struct extent_map *em;
5080 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005081 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005082 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005083 int i;
5084
Liu Bo592d92e2017-03-14 13:33:55 -07005085 em = get_chunk_map(fs_info, chunk_offset, 1);
5086 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005087 return 1;
5088
Jeff Mahoney95617d62015-06-03 10:55:48 -04005089 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005090 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005091 if (test_bit(BTRFS_DEV_STATE_MISSING,
5092 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005093 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)
Liu Bo8810f752018-01-02 13:36:41 -07005163 /*
5164 * There could be two corrupted data stripes, we need
5165 * to loop retry in order to rebuild the correct data.
5166 *
5167 * Fail a stripe at a time on every retry except the
5168 * stripe under reconstruction.
5169 */
5170 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005171 else
5172 ret = 1;
5173 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005174
David Sterba7e79cb82018-03-24 02:11:38 +01005175 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005176 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5177 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005178 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005179 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005180
Chris Masonf1885912008-04-09 16:28:12 -04005181 return ret;
5182}
5183
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005184unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005185 u64 logical)
5186{
5187 struct extent_map *em;
5188 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005189 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005190
Liu Bo592d92e2017-03-14 13:33:55 -07005191 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005192
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005193 if (!WARN_ON(IS_ERR(em))) {
5194 map = em->map_lookup;
5195 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5196 len = map->stripe_len * nr_data_stripes(map);
5197 free_extent_map(em);
5198 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005199 return len;
5200}
5201
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005202int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005203{
5204 struct extent_map *em;
5205 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005206 int ret = 0;
5207
Liu Bo592d92e2017-03-14 13:33:55 -07005208 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005209
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005210 if(!WARN_ON(IS_ERR(em))) {
5211 map = em->map_lookup;
5212 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5213 ret = 1;
5214 free_extent_map(em);
5215 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005216 return ret;
5217}
5218
Stefan Behrens30d98612012-11-06 14:52:18 +01005219static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005220 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005221 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005222{
5223 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005224 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005225 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005226 int tolerance;
5227 struct btrfs_device *srcdev;
5228
Anand Jain99f92a72018-03-14 16:29:12 +08005229 ASSERT((map->type &
5230 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5231
5232 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5233 num_stripes = map->sub_stripes;
5234 else
5235 num_stripes = map->num_stripes;
5236
Anand Jain8ba0ae72018-03-14 16:29:13 +08005237 preferred_mirror = first + current->pid % num_stripes;
5238
Stefan Behrens30d98612012-11-06 14:52:18 +01005239 if (dev_replace_is_ongoing &&
5240 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5241 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5242 srcdev = fs_info->dev_replace.srcdev;
5243 else
5244 srcdev = NULL;
5245
5246 /*
5247 * try to avoid the drive that is the source drive for a
5248 * dev-replace procedure, only choose it if no other non-missing
5249 * mirror is available
5250 */
5251 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005252 if (map->stripes[preferred_mirror].dev->bdev &&
5253 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5254 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005255 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005256 if (map->stripes[i].dev->bdev &&
5257 (tolerance || map->stripes[i].dev != srcdev))
5258 return i;
5259 }
Chris Masondfe25022008-05-13 13:46:40 -04005260 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005261
Chris Masondfe25022008-05-13 13:46:40 -04005262 /* we couldn't find one that doesn't fail. Just return something
5263 * and the io error handling code will clean up eventually
5264 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005265 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005266}
5267
David Woodhouse53b381b2013-01-29 18:40:14 -05005268static inline int parity_smaller(u64 a, u64 b)
5269{
5270 return a > b;
5271}
5272
5273/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005274static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005275{
5276 struct btrfs_bio_stripe s;
5277 int i;
5278 u64 l;
5279 int again = 1;
5280
5281 while (again) {
5282 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005283 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005284 if (parity_smaller(bbio->raid_map[i],
5285 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005286 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005287 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005288 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005289 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005290 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005291 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005292
David Woodhouse53b381b2013-01-29 18:40:14 -05005293 again = 1;
5294 }
5295 }
5296 }
5297}
5298
Zhao Lei6e9606d2015-01-20 15:11:34 +08005299static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5300{
5301 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005302 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005303 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005304 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005305 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005306 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005307 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005308 /*
5309 * plus the raid_map, which includes both the tgt dev
5310 * and the stripes
5311 */
5312 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005313 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005314
5315 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005316 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005317
5318 return bbio;
5319}
5320
5321void btrfs_get_bbio(struct btrfs_bio *bbio)
5322{
Elena Reshetova140475a2017-03-03 10:55:10 +02005323 WARN_ON(!refcount_read(&bbio->refs));
5324 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005325}
5326
5327void btrfs_put_bbio(struct btrfs_bio *bbio)
5328{
5329 if (!bbio)
5330 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005331 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005332 kfree(bbio);
5333}
5334
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005335/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5336/*
5337 * Please note that, discard won't be sent to target device of device
5338 * replace.
5339 */
5340static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5341 u64 logical, u64 length,
5342 struct btrfs_bio **bbio_ret)
5343{
5344 struct extent_map *em;
5345 struct map_lookup *map;
5346 struct btrfs_bio *bbio;
5347 u64 offset;
5348 u64 stripe_nr;
5349 u64 stripe_nr_end;
5350 u64 stripe_end_offset;
5351 u64 stripe_cnt;
5352 u64 stripe_len;
5353 u64 stripe_offset;
5354 u64 num_stripes;
5355 u32 stripe_index;
5356 u32 factor = 0;
5357 u32 sub_stripes = 0;
5358 u64 stripes_per_dev = 0;
5359 u32 remaining_stripes = 0;
5360 u32 last_stripe = 0;
5361 int ret = 0;
5362 int i;
5363
5364 /* discard always return a bbio */
5365 ASSERT(bbio_ret);
5366
5367 em = get_chunk_map(fs_info, logical, length);
5368 if (IS_ERR(em))
5369 return PTR_ERR(em);
5370
5371 map = em->map_lookup;
5372 /* we don't discard raid56 yet */
5373 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5374 ret = -EOPNOTSUPP;
5375 goto out;
5376 }
5377
5378 offset = logical - em->start;
5379 length = min_t(u64, em->len - offset, length);
5380
5381 stripe_len = map->stripe_len;
5382 /*
5383 * stripe_nr counts the total number of stripes we have to stride
5384 * to get to this block
5385 */
5386 stripe_nr = div64_u64(offset, stripe_len);
5387
5388 /* stripe_offset is the offset of this block in its stripe */
5389 stripe_offset = offset - stripe_nr * stripe_len;
5390
5391 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005392 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005393 stripe_cnt = stripe_nr_end - stripe_nr;
5394 stripe_end_offset = stripe_nr_end * map->stripe_len -
5395 (offset + length);
5396 /*
5397 * after this, stripe_nr is the number of stripes on this
5398 * device we have to walk to find the data, and stripe_index is
5399 * the number of our device in the stripe array
5400 */
5401 num_stripes = 1;
5402 stripe_index = 0;
5403 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5404 BTRFS_BLOCK_GROUP_RAID10)) {
5405 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5406 sub_stripes = 1;
5407 else
5408 sub_stripes = map->sub_stripes;
5409
5410 factor = map->num_stripes / sub_stripes;
5411 num_stripes = min_t(u64, map->num_stripes,
5412 sub_stripes * stripe_cnt);
5413 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5414 stripe_index *= sub_stripes;
5415 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5416 &remaining_stripes);
5417 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5418 last_stripe *= sub_stripes;
5419 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5420 BTRFS_BLOCK_GROUP_DUP)) {
5421 num_stripes = map->num_stripes;
5422 } else {
5423 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5424 &stripe_index);
5425 }
5426
5427 bbio = alloc_btrfs_bio(num_stripes, 0);
5428 if (!bbio) {
5429 ret = -ENOMEM;
5430 goto out;
5431 }
5432
5433 for (i = 0; i < num_stripes; i++) {
5434 bbio->stripes[i].physical =
5435 map->stripes[stripe_index].physical +
5436 stripe_offset + stripe_nr * map->stripe_len;
5437 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5438
5439 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5440 BTRFS_BLOCK_GROUP_RAID10)) {
5441 bbio->stripes[i].length = stripes_per_dev *
5442 map->stripe_len;
5443
5444 if (i / sub_stripes < remaining_stripes)
5445 bbio->stripes[i].length +=
5446 map->stripe_len;
5447
5448 /*
5449 * Special for the first stripe and
5450 * the last stripe:
5451 *
5452 * |-------|...|-------|
5453 * |----------|
5454 * off end_off
5455 */
5456 if (i < sub_stripes)
5457 bbio->stripes[i].length -=
5458 stripe_offset;
5459
5460 if (stripe_index >= last_stripe &&
5461 stripe_index <= (last_stripe +
5462 sub_stripes - 1))
5463 bbio->stripes[i].length -=
5464 stripe_end_offset;
5465
5466 if (i == sub_stripes - 1)
5467 stripe_offset = 0;
5468 } else {
5469 bbio->stripes[i].length = length;
5470 }
5471
5472 stripe_index++;
5473 if (stripe_index == map->num_stripes) {
5474 stripe_index = 0;
5475 stripe_nr++;
5476 }
5477 }
5478
5479 *bbio_ret = bbio;
5480 bbio->map_type = map->type;
5481 bbio->num_stripes = num_stripes;
5482out:
5483 free_extent_map(em);
5484 return ret;
5485}
5486
Liu Bo5ab56092017-03-14 13:33:57 -07005487/*
5488 * In dev-replace case, for repair case (that's the only case where the mirror
5489 * is selected explicitly when calling btrfs_map_block), blocks left of the
5490 * left cursor can also be read from the target drive.
5491 *
5492 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5493 * array of stripes.
5494 * For READ, it also needs to be supported using the same mirror number.
5495 *
5496 * If the requested block is not left of the left cursor, EIO is returned. This
5497 * can happen because btrfs_num_copies() returns one more in the dev-replace
5498 * case.
5499 */
5500static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5501 u64 logical, u64 length,
5502 u64 srcdev_devid, int *mirror_num,
5503 u64 *physical)
5504{
5505 struct btrfs_bio *bbio = NULL;
5506 int num_stripes;
5507 int index_srcdev = 0;
5508 int found = 0;
5509 u64 physical_of_found = 0;
5510 int i;
5511 int ret = 0;
5512
5513 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5514 logical, &length, &bbio, 0, 0);
5515 if (ret) {
5516 ASSERT(bbio == NULL);
5517 return ret;
5518 }
5519
5520 num_stripes = bbio->num_stripes;
5521 if (*mirror_num > num_stripes) {
5522 /*
5523 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5524 * that means that the requested area is not left of the left
5525 * cursor
5526 */
5527 btrfs_put_bbio(bbio);
5528 return -EIO;
5529 }
5530
5531 /*
5532 * process the rest of the function using the mirror_num of the source
5533 * drive. Therefore look it up first. At the end, patch the device
5534 * pointer to the one of the target drive.
5535 */
5536 for (i = 0; i < num_stripes; i++) {
5537 if (bbio->stripes[i].dev->devid != srcdev_devid)
5538 continue;
5539
5540 /*
5541 * In case of DUP, in order to keep it simple, only add the
5542 * mirror with the lowest physical address
5543 */
5544 if (found &&
5545 physical_of_found <= bbio->stripes[i].physical)
5546 continue;
5547
5548 index_srcdev = i;
5549 found = 1;
5550 physical_of_found = bbio->stripes[i].physical;
5551 }
5552
5553 btrfs_put_bbio(bbio);
5554
5555 ASSERT(found);
5556 if (!found)
5557 return -EIO;
5558
5559 *mirror_num = index_srcdev + 1;
5560 *physical = physical_of_found;
5561 return ret;
5562}
5563
Liu Bo73c0f222017-03-14 13:33:58 -07005564static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5565 struct btrfs_bio **bbio_ret,
5566 struct btrfs_dev_replace *dev_replace,
5567 int *num_stripes_ret, int *max_errors_ret)
5568{
5569 struct btrfs_bio *bbio = *bbio_ret;
5570 u64 srcdev_devid = dev_replace->srcdev->devid;
5571 int tgtdev_indexes = 0;
5572 int num_stripes = *num_stripes_ret;
5573 int max_errors = *max_errors_ret;
5574 int i;
5575
5576 if (op == BTRFS_MAP_WRITE) {
5577 int index_where_to_add;
5578
5579 /*
5580 * duplicate the write operations while the dev replace
5581 * procedure is running. Since the copying of the old disk to
5582 * the new disk takes place at run time while the filesystem is
5583 * mounted writable, the regular write operations to the old
5584 * disk have to be duplicated to go to the new disk as well.
5585 *
5586 * Note that device->missing is handled by the caller, and that
5587 * the write to the old disk is already set up in the stripes
5588 * array.
5589 */
5590 index_where_to_add = num_stripes;
5591 for (i = 0; i < num_stripes; i++) {
5592 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5593 /* write to new disk, too */
5594 struct btrfs_bio_stripe *new =
5595 bbio->stripes + index_where_to_add;
5596 struct btrfs_bio_stripe *old =
5597 bbio->stripes + i;
5598
5599 new->physical = old->physical;
5600 new->length = old->length;
5601 new->dev = dev_replace->tgtdev;
5602 bbio->tgtdev_map[i] = index_where_to_add;
5603 index_where_to_add++;
5604 max_errors++;
5605 tgtdev_indexes++;
5606 }
5607 }
5608 num_stripes = index_where_to_add;
5609 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5610 int index_srcdev = 0;
5611 int found = 0;
5612 u64 physical_of_found = 0;
5613
5614 /*
5615 * During the dev-replace procedure, the target drive can also
5616 * be used to read data in case it is needed to repair a corrupt
5617 * block elsewhere. This is possible if the requested area is
5618 * left of the left cursor. In this area, the target drive is a
5619 * full copy of the source drive.
5620 */
5621 for (i = 0; i < num_stripes; i++) {
5622 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5623 /*
5624 * In case of DUP, in order to keep it simple,
5625 * only add the mirror with the lowest physical
5626 * address
5627 */
5628 if (found &&
5629 physical_of_found <=
5630 bbio->stripes[i].physical)
5631 continue;
5632 index_srcdev = i;
5633 found = 1;
5634 physical_of_found = bbio->stripes[i].physical;
5635 }
5636 }
5637 if (found) {
5638 struct btrfs_bio_stripe *tgtdev_stripe =
5639 bbio->stripes + num_stripes;
5640
5641 tgtdev_stripe->physical = physical_of_found;
5642 tgtdev_stripe->length =
5643 bbio->stripes[index_srcdev].length;
5644 tgtdev_stripe->dev = dev_replace->tgtdev;
5645 bbio->tgtdev_map[index_srcdev] = num_stripes;
5646
5647 tgtdev_indexes++;
5648 num_stripes++;
5649 }
5650 }
5651
5652 *num_stripes_ret = num_stripes;
5653 *max_errors_ret = max_errors;
5654 bbio->num_tgtdevs = tgtdev_indexes;
5655 *bbio_ret = bbio;
5656}
5657
Liu Bo2b19a1f2017-03-14 13:34:00 -07005658static bool need_full_stripe(enum btrfs_map_op op)
5659{
5660 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5661}
5662
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005663static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5664 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005665 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005666 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005667 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005668{
5669 struct extent_map *em;
5670 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005671 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005672 u64 stripe_offset;
5673 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005674 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005675 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005676 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005677 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005678 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005679 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005680 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005681 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005682 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5683 int dev_replace_is_ongoing = 0;
5684 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005685 int patch_the_first_stripe_for_dev_replace = 0;
5686 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005687 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005688
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005689 if (op == BTRFS_MAP_DISCARD)
5690 return __btrfs_map_block_for_discard(fs_info, logical,
5691 *length, bbio_ret);
5692
Liu Bo592d92e2017-03-14 13:33:55 -07005693 em = get_chunk_map(fs_info, logical, *length);
5694 if (IS_ERR(em))
5695 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005696
Jeff Mahoney95617d62015-06-03 10:55:48 -04005697 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005698 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005699
David Woodhouse53b381b2013-01-29 18:40:14 -05005700 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005701 stripe_nr = offset;
5702 /*
5703 * stripe_nr counts the total number of stripes we have to stride
5704 * to get to this block
5705 */
David Sterba47c57132015-02-20 18:43:47 +01005706 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005707
David Woodhouse53b381b2013-01-29 18:40:14 -05005708 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005709 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005710 btrfs_crit(fs_info,
5711 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005712 stripe_offset, offset, em->start, logical,
5713 stripe_len);
5714 free_extent_map(em);
5715 return -EINVAL;
5716 }
Chris Mason593060d2008-03-25 16:50:33 -04005717
5718 /* stripe_offset is the offset of this block in its stripe*/
5719 stripe_offset = offset - stripe_offset;
5720
David Woodhouse53b381b2013-01-29 18:40:14 -05005721 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005722 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005723 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5724 raid56_full_stripe_start = offset;
5725
5726 /* allow a write of a full stripe, but make sure we don't
5727 * allow straddling of stripes
5728 */
David Sterba47c57132015-02-20 18:43:47 +01005729 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5730 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005731 raid56_full_stripe_start *= full_stripe_len;
5732 }
5733
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005734 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005735 u64 max_len;
5736 /* For writes to RAID[56], allow a full stripeset across all disks.
5737 For other RAID types and for RAID[56] reads, just allow a single
5738 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005739 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005740 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005741 max_len = stripe_len * nr_data_stripes(map) -
5742 (offset - raid56_full_stripe_start);
5743 } else {
5744 /* we limit the length of each bio to what fits in a stripe */
5745 max_len = stripe_len - stripe_offset;
5746 }
5747 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005748 } else {
5749 *length = em->len - offset;
5750 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005751
David Woodhouse53b381b2013-01-29 18:40:14 -05005752 /* This is for when we're called from btrfs_merge_bio_hook() and all
5753 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005754 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005755 goto out;
5756
David Sterba7e79cb82018-03-24 02:11:38 +01005757 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005758 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5759 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01005760 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005761 else
5762 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005763
Stefan Behrensad6d6202012-11-06 15:06:47 +01005764 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005765 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005766 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5767 dev_replace->srcdev->devid,
5768 &mirror_num,
5769 &physical_to_patch_in_first_stripe);
5770 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005771 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005772 else
5773 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005774 } else if (mirror_num > map->num_stripes) {
5775 mirror_num = 0;
5776 }
5777
Chris Masonf2d8d742008-04-21 10:03:05 -04005778 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005779 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005780 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005781 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5782 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005783 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005784 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005785 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005786 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005787 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005788 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005789 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005790 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005791 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005792 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005793 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005794 }
Chris Mason2fff7342008-04-29 14:12:09 -04005795
Chris Mason611f0e02008-04-03 16:29:03 -04005796 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005797 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005798 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005799 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005800 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005801 } else {
5802 mirror_num = 1;
5803 }
Chris Mason2fff7342008-04-29 14:12:09 -04005804
Chris Mason321aecc2008-04-16 10:49:51 -04005805 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005806 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005807
David Sterba47c57132015-02-20 18:43:47 +01005808 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005809 stripe_index *= map->sub_stripes;
5810
Anand Jainde483732017-10-12 16:43:00 +08005811 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005812 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005813 else if (mirror_num)
5814 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005815 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005816 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005817 stripe_index = find_live_mirror(fs_info, map,
5818 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01005819 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005820 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005821 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005822
Zhao Leiffe2d202015-01-20 15:11:44 +08005823 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005824 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005825 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005826 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005827 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005828
5829 /* RAID[56] write or recovery. Return all stripes */
5830 num_stripes = map->num_stripes;
5831 max_errors = nr_parity_stripes(map);
5832
David Woodhouse53b381b2013-01-29 18:40:14 -05005833 *length = map->stripe_len;
5834 stripe_index = 0;
5835 stripe_offset = 0;
5836 } else {
5837 /*
5838 * Mirror #0 or #1 means the original data block.
5839 * Mirror #2 is RAID5 parity block.
5840 * Mirror #3 is RAID6 Q block.
5841 */
David Sterba47c57132015-02-20 18:43:47 +01005842 stripe_nr = div_u64_rem(stripe_nr,
5843 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005844 if (mirror_num > 1)
5845 stripe_index = nr_data_stripes(map) +
5846 mirror_num - 2;
5847
5848 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005849 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5850 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005851 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005852 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005853 }
Chris Mason8790d502008-04-03 16:29:03 -04005854 } else {
5855 /*
David Sterba47c57132015-02-20 18:43:47 +01005856 * after this, stripe_nr is the number of stripes on this
5857 * device we have to walk to find the data, and stripe_index is
5858 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005859 */
David Sterba47c57132015-02-20 18:43:47 +01005860 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5861 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005862 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005863 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005864 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005865 btrfs_crit(fs_info,
5866 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005867 stripe_index, map->num_stripes);
5868 ret = -EINVAL;
5869 goto out;
5870 }
Chris Mason593060d2008-03-25 16:50:33 -04005871
Stefan Behrens472262f2012-11-06 14:43:46 +01005872 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005873 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005874 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005875 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005876 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005877 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005878 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005879 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005880
Zhao Lei6e9606d2015-01-20 15:11:34 +08005881 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005882 if (!bbio) {
5883 ret = -ENOMEM;
5884 goto out;
5885 }
Liu Bo6fad8232017-03-14 13:33:59 -07005886 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005887 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005888
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005889 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005890 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5891 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005892 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005893 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005894
5895 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5896 sizeof(struct btrfs_bio_stripe) *
5897 num_alloc_stripes +
5898 sizeof(int) * tgtdev_indexes);
5899
5900 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005901 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005902
5903 /* Fill in the logical address of each stripe */
5904 tmp = stripe_nr * nr_data_stripes(map);
5905 for (i = 0; i < nr_data_stripes(map); i++)
5906 bbio->raid_map[(i+rot) % num_stripes] =
5907 em->start + (tmp + i) * map->stripe_len;
5908
5909 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5910 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5911 bbio->raid_map[(i+rot+1) % num_stripes] =
5912 RAID6_Q_STRIPE;
5913 }
5914
Li Zefanec9ef7a2011-12-01 14:06:42 +08005915
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005916 for (i = 0; i < num_stripes; i++) {
5917 bbio->stripes[i].physical =
5918 map->stripes[stripe_index].physical +
5919 stripe_offset +
5920 stripe_nr * map->stripe_len;
5921 bbio->stripes[i].dev =
5922 map->stripes[stripe_index].dev;
5923 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005924 }
Li Zefande11cc12011-12-01 12:55:47 +08005925
Liu Bo2b19a1f2017-03-14 13:34:00 -07005926 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005927 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005928
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005929 if (bbio->raid_map)
5930 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005931
Liu Bo73c0f222017-03-14 13:33:58 -07005932 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005933 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005934 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5935 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005936 }
5937
Li Zefande11cc12011-12-01 12:55:47 +08005938 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005939 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005940 bbio->num_stripes = num_stripes;
5941 bbio->max_errors = max_errors;
5942 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005943
5944 /*
5945 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5946 * mirror_num == num_stripes + 1 && dev_replace target drive is
5947 * available as a mirror
5948 */
5949 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5950 WARN_ON(num_stripes > 1);
5951 bbio->stripes[0].dev = dev_replace->tgtdev;
5952 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5953 bbio->mirror_num = map->num_stripes + 1;
5954 }
Chris Masoncea9e442008-04-09 16:28:12 -04005955out:
Liu Bo73beece2015-07-17 16:49:19 +08005956 if (dev_replace_is_ongoing) {
5957 btrfs_dev_replace_clear_lock_blocking(dev_replace);
David Sterba7e79cb82018-03-24 02:11:38 +01005958 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005959 }
Chris Mason0b86a832008-03-24 15:01:56 -04005960 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005961 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005962}
5963
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005964int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005965 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005966 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005967{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005968 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005969 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005970}
5971
Miao Xieaf8e2d12014-10-23 14:42:50 +08005972/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005973int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005974 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005975 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005976{
David Sterba825ad4c2017-03-28 14:45:22 +02005977 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005978}
5979
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03005980int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
5981 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05005982{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005983 struct extent_map *em;
5984 struct map_lookup *map;
5985 u64 *buf;
5986 u64 bytenr;
5987 u64 length;
5988 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005989 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005990 int i, j, nr = 0;
5991
Liu Bo592d92e2017-03-14 13:33:55 -07005992 em = get_chunk_map(fs_info, chunk_start, 1);
5993 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005994 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005995
Jeff Mahoney95617d62015-06-03 10:55:48 -04005996 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005997 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005998 rmap_len = map->stripe_len;
5999
Yan Zhenga512bbf2008-12-08 16:46:26 -05006000 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006001 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006002 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006003 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006004 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006005 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006006 rmap_len = map->stripe_len * nr_data_stripes(map);
6007 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006008
David Sterba31e818f2015-02-20 18:00:26 +01006009 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006010 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006011
6012 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006013 if (map->stripes[i].physical > physical ||
6014 map->stripes[i].physical + length <= physical)
6015 continue;
6016
6017 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006018 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006019
6020 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6021 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006022 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006023 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6024 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006025 } /* else if RAID[56], multiply by nr_data_stripes().
6026 * Alternatively, just use rmap_len below instead of
6027 * map->stripe_len */
6028
6029 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006030 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006031 for (j = 0; j < nr; j++) {
6032 if (buf[j] == bytenr)
6033 break;
6034 }
Chris Mason934d3752008-12-08 16:43:10 -05006035 if (j == nr) {
6036 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006037 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006038 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006039 }
6040
Yan Zhenga512bbf2008-12-08 16:46:26 -05006041 *logical = buf;
6042 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006043 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006044
6045 free_extent_map(em);
6046 return 0;
6047}
6048
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006049static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006050{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006051 bio->bi_private = bbio->private;
6052 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006053 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006054
Zhao Lei6e9606d2015-01-20 15:11:34 +08006055 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006056}
6057
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006058static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006059{
Chris Mason9be33952013-05-17 18:30:14 -04006060 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006061 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006062
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006063 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006064 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006065 if (bio->bi_status == BLK_STS_IOERR ||
6066 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006067 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006068 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006069 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006070
6071 BUG_ON(stripe_index >= bbio->num_stripes);
6072 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006073 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006074 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006075 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006076 BTRFS_DEV_STAT_WRITE_ERRS);
6077 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006078 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006079 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006080 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006081 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006082 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006083 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006084 }
6085 }
Chris Mason8790d502008-04-03 16:29:03 -04006086
Jan Schmidta1d3c472011-08-04 17:15:33 +02006087 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006088 is_orig_bio = 1;
6089
Miao Xiec404e0d2014-01-30 16:46:55 +08006090 btrfs_bio_counter_dec(bbio->fs_info);
6091
Jan Schmidta1d3c472011-08-04 17:15:33 +02006092 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006093 if (!is_orig_bio) {
6094 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006095 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006096 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006097
Chris Mason9be33952013-05-17 18:30:14 -04006098 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006099 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006100 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006101 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006102 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006103 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006104 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006105 /*
6106 * this bio is actually up to date, we didn't
6107 * go over the max number of errors
6108 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006109 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006110 }
Miao Xiec55f1392014-06-19 10:42:54 +08006111
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006112 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006113 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006114 bio_put(bio);
6115 }
Chris Mason8790d502008-04-03 16:29:03 -04006116}
6117
Chris Mason8b712842008-06-11 16:50:36 -04006118/*
6119 * see run_scheduled_bios for a description of why bios are collected for
6120 * async submit.
6121 *
6122 * This will add one bio to the pending list for a device and make sure
6123 * the work struct is scheduled.
6124 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006125static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006126 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006127{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006128 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006129 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006130 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006131
Anand Jaine6e674b2017-12-04 12:54:54 +08006132 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
6133 !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006134 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006135 return;
6136 }
6137
Chris Mason8b712842008-06-11 16:50:36 -04006138 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006139 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006140 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006141 return;
Chris Mason8b712842008-06-11 16:50:36 -04006142 }
6143
Chris Mason492bb6de2008-07-31 16:29:02 -04006144 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006145 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006146
6147 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006148 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006149 pending_bios = &device->pending_sync_bios;
6150 else
6151 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006152
Chris Masonffbd5172009-04-20 15:50:09 -04006153 if (pending_bios->tail)
6154 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006155
Chris Masonffbd5172009-04-20 15:50:09 -04006156 pending_bios->tail = bio;
6157 if (!pending_bios->head)
6158 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006159 if (device->running_pending)
6160 should_queue = 0;
6161
6162 spin_unlock(&device->io_lock);
6163
6164 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006165 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006166}
6167
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006168static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6169 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006170{
6171 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006172 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006173
6174 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006175 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006176 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006177 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006178#ifdef DEBUG
6179 {
6180 struct rcu_string *name;
6181
6182 rcu_read_lock();
6183 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006184 btrfs_debug(fs_info,
6185 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6186 bio_op(bio), bio->bi_opf,
6187 (u64)bio->bi_iter.bi_sector,
6188 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6189 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006190 rcu_read_unlock();
6191 }
6192#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006193 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006194
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006195 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006196
Josef Bacikde1ee922012-10-19 16:50:56 -04006197 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006198 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006199 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006200 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006201}
6202
Josef Bacikde1ee922012-10-19 16:50:56 -04006203static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6204{
6205 atomic_inc(&bbio->error);
6206 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006207 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006208 WARN_ON(bio != bbio->orig_bio);
6209
Chris Mason9be33952013-05-17 18:30:14 -04006210 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006211 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006212 if (atomic_read(&bbio->error) > bbio->max_errors)
6213 bio->bi_status = BLK_STS_IOERR;
6214 else
6215 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006216 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006217 }
6218}
6219
Omar Sandoval58efbc92017-08-22 23:45:59 -07006220blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6221 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006222{
Chris Mason0b86a832008-03-24 15:01:56 -04006223 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006224 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006225 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006226 u64 length = 0;
6227 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006228 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006229 int dev_nr;
6230 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006231 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006232
Kent Overstreet4f024f32013-10-11 15:44:27 -07006233 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006234 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006236 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006237 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006238 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006239 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006240 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006241 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006242 }
Chris Masoncea9e442008-04-09 16:28:12 -04006243
Jan Schmidta1d3c472011-08-04 17:15:33 +02006244 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006245 bbio->orig_bio = first_bio;
6246 bbio->private = first_bio->bi_private;
6247 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006248 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006249 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6250
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006251 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006252 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006253 /* In this case, map_length has been set to the length of
6254 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006255 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006256 ret = raid56_parity_write(fs_info, bio, bbio,
6257 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006258 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006259 ret = raid56_parity_recover(fs_info, bio, bbio,
6260 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006261 }
Miao Xie42452152014-11-25 16:39:28 +08006262
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006263 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006264 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006265 }
6266
Chris Masoncea9e442008-04-09 16:28:12 -04006267 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006268 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006269 "mapping failed logical %llu bio len %llu len %llu",
6270 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006271 BUG();
6272 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006273
Zhao Lei08da7572015-02-12 15:42:16 +08006274 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006275 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006276 if (!dev || !dev->bdev ||
Anand Jainebbede42017-12-04 12:54:52 +08006277 (bio_op(first_bio) == REQ_OP_WRITE &&
6278 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006279 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006280 continue;
6281 }
6282
David Sterba3aa8e072017-06-02 17:38:30 +02006283 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006284 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006285 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006286 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006287
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006288 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6289 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006290 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006291 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006292 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006293}
6294
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006295struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006296 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006297{
Yan Zheng2b820322008-11-17 21:11:30 -05006298 struct btrfs_device *device;
6299 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006300
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006301 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006302 while (cur_devices) {
6303 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006304 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006305 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006306 if (device)
6307 return device;
6308 }
6309 cur_devices = cur_devices->seed;
6310 }
6311 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006312}
6313
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006314static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006315 u64 devid, u8 *dev_uuid)
6316{
6317 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006318
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006319 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6320 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006321 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006322
6323 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006324 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006325 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006326
Anand Jaine6e674b2017-12-04 12:54:54 +08006327 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006328 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006329
Chris Masondfe25022008-05-13 13:46:40 -04006330 return device;
6331}
6332
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006333/**
6334 * btrfs_alloc_device - allocate struct btrfs_device
6335 * @fs_info: used only for generating a new devid, can be NULL if
6336 * devid is provided (i.e. @devid != NULL).
6337 * @devid: a pointer to devid for this device. If NULL a new devid
6338 * is generated.
6339 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6340 * is generated.
6341 *
6342 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006343 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006344 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006345 */
6346struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6347 const u64 *devid,
6348 const u8 *uuid)
6349{
6350 struct btrfs_device *dev;
6351 u64 tmp;
6352
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306353 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006354 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006355
6356 dev = __alloc_device();
6357 if (IS_ERR(dev))
6358 return dev;
6359
6360 if (devid)
6361 tmp = *devid;
6362 else {
6363 int ret;
6364
6365 ret = find_next_devid(fs_info, &tmp);
6366 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006367 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006368 return ERR_PTR(ret);
6369 }
6370 }
6371 dev->devid = tmp;
6372
6373 if (uuid)
6374 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6375 else
6376 generate_random_uuid(dev->uuid);
6377
Liu Bo9e0af232014-08-15 23:36:53 +08006378 btrfs_init_work(&dev->work, btrfs_submit_helper,
6379 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006380
6381 return dev;
6382}
6383
Liu Boe06cd3d2016-06-03 12:05:15 -07006384/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006385static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006386 struct extent_buffer *leaf,
6387 struct btrfs_chunk *chunk, u64 logical)
6388{
6389 u64 length;
6390 u64 stripe_len;
6391 u16 num_stripes;
6392 u16 sub_stripes;
6393 u64 type;
6394
6395 length = btrfs_chunk_length(leaf, chunk);
6396 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6397 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6398 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6399 type = btrfs_chunk_type(leaf, chunk);
6400
6401 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006402 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006403 num_stripes);
6404 return -EIO;
6405 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006406 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6407 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006408 return -EIO;
6409 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006410 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6411 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006412 btrfs_chunk_sector_size(leaf, chunk));
6413 return -EIO;
6414 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006415 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6416 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006417 return -EIO;
6418 }
6419 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006420 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006421 stripe_len);
6422 return -EIO;
6423 }
6424 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6425 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006426 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006427 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6428 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6429 btrfs_chunk_type(leaf, chunk));
6430 return -EIO;
6431 }
6432 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6433 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6434 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6435 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6436 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6437 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6438 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006439 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006440 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6441 num_stripes, sub_stripes,
6442 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6443 return -EIO;
6444 }
6445
6446 return 0;
6447}
6448
Anand Jain5a2b8e62017-10-09 11:07:45 +08006449static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006450 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006451{
Anand Jain2b902df2017-10-09 11:07:46 +08006452 if (error)
6453 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6454 devid, uuid);
6455 else
6456 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6457 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006458}
6459
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006460static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006461 struct extent_buffer *leaf,
6462 struct btrfs_chunk *chunk)
6463{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006464 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006465 struct map_lookup *map;
6466 struct extent_map *em;
6467 u64 logical;
6468 u64 length;
6469 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006470 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006471 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006472 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006473 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006474
Chris Masone17cade2008-04-15 15:41:47 -04006475 logical = key->offset;
6476 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006477 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006478
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006479 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006480 if (ret)
6481 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006482
Chris Mason890871b2009-09-02 16:24:52 -04006483 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006484 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006485 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006486
6487 /* already mapped? */
6488 if (em && em->start <= logical && em->start + em->len > logical) {
6489 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006490 return 0;
6491 } else if (em) {
6492 free_extent_map(em);
6493 }
Chris Mason0b86a832008-03-24 15:01:56 -04006494
David Sterba172ddd62011-04-21 00:48:27 +02006495 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006496 if (!em)
6497 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006498 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006499 if (!map) {
6500 free_extent_map(em);
6501 return -ENOMEM;
6502 }
6503
Wang Shilong298a8f92014-06-19 10:42:52 +08006504 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006505 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006506 em->start = logical;
6507 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006508 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006509 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006510 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006511
Chris Mason593060d2008-03-25 16:50:33 -04006512 map->num_stripes = num_stripes;
6513 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6514 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006515 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6516 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006517 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006518 for (i = 0; i < num_stripes; i++) {
6519 map->stripes[i].physical =
6520 btrfs_stripe_offset_nr(leaf, chunk, i);
6521 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006522 read_extent_buffer(leaf, uuid, (unsigned long)
6523 btrfs_stripe_dev_uuid_nr(chunk, i),
6524 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006525 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006526 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006527 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006528 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006529 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006530 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006531 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006532 }
Chris Masondfe25022008-05-13 13:46:40 -04006533 if (!map->stripes[i].dev) {
6534 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006535 add_missing_dev(fs_info->fs_devices, devid,
6536 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006537 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006538 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006539 btrfs_err(fs_info,
6540 "failed to init missing dev %llu: %ld",
6541 devid, PTR_ERR(map->stripes[i].dev));
6542 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006543 }
Anand Jain2b902df2017-10-09 11:07:46 +08006544 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006545 }
Anand Jaine12c9622017-12-04 12:54:53 +08006546 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6547 &(map->stripes[i].dev->dev_state));
6548
Chris Mason0b86a832008-03-24 15:01:56 -04006549 }
6550
Chris Mason890871b2009-09-02 16:24:52 -04006551 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006552 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006553 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006554 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006555 free_extent_map(em);
6556
6557 return 0;
6558}
6559
Jeff Mahoney143bede2012-03-01 14:56:26 +01006560static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006561 struct btrfs_dev_item *dev_item,
6562 struct btrfs_device *device)
6563{
6564 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006565
6566 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006567 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6568 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006569 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006570 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006571 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006572 device->type = btrfs_device_type(leaf, dev_item);
6573 device->io_align = btrfs_device_io_align(leaf, dev_item);
6574 device->io_width = btrfs_device_io_width(leaf, dev_item);
6575 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006576 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006577 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006578
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006579 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006580 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006581}
6582
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006583static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006584 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006585{
6586 struct btrfs_fs_devices *fs_devices;
6587 int ret;
6588
David Sterbaa32bf9a2018-03-16 02:21:22 +01006589 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006590 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006591
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006592 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006593 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006594 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006595 return fs_devices;
6596
Yan Zheng2b820322008-11-17 21:11:30 -05006597 fs_devices = fs_devices->seed;
6598 }
6599
6600 fs_devices = find_fsid(fsid);
6601 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006602 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006603 return ERR_PTR(-ENOENT);
6604
6605 fs_devices = alloc_fs_devices(fsid);
6606 if (IS_ERR(fs_devices))
6607 return fs_devices;
6608
6609 fs_devices->seeding = 1;
6610 fs_devices->opened = 1;
6611 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006612 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006613
6614 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006615 if (IS_ERR(fs_devices))
6616 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006617
Anand Jain897fb572018-04-12 10:29:28 +08006618 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006619 if (ret) {
6620 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006621 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006622 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006623 }
Yan Zheng2b820322008-11-17 21:11:30 -05006624
6625 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006626 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006627 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006628 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006629 goto out;
6630 }
6631
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006632 fs_devices->seed = fs_info->fs_devices->seed;
6633 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006634out:
Miao Xie5f375832014-09-03 21:35:46 +08006635 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006636}
6637
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006638static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006639 struct extent_buffer *leaf,
6640 struct btrfs_dev_item *dev_item)
6641{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006642 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006643 struct btrfs_device *device;
6644 u64 devid;
6645 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006646 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006647 u8 dev_uuid[BTRFS_UUID_SIZE];
6648
Chris Mason0b86a832008-03-24 15:01:56 -04006649 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006650 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006651 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006652 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006653 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006654
Anand Jain44880fd2017-07-29 17:50:09 +08006655 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006656 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006657 if (IS_ERR(fs_devices))
6658 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006659 }
6660
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006661 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006662 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006663 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006664 btrfs_report_missing_device(fs_info, devid,
6665 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006666 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006667 }
Yan Zheng2b820322008-11-17 21:11:30 -05006668
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006669 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006670 if (IS_ERR(device)) {
6671 btrfs_err(fs_info,
6672 "failed to add missing dev %llu: %ld",
6673 devid, PTR_ERR(device));
6674 return PTR_ERR(device);
6675 }
Anand Jain2b902df2017-10-09 11:07:46 +08006676 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006677 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006678 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006679 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6680 btrfs_report_missing_device(fs_info,
6681 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006682 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006683 }
6684 btrfs_report_missing_device(fs_info, devid,
6685 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006686 }
Miao Xie5f375832014-09-03 21:35:46 +08006687
Anand Jaine6e674b2017-12-04 12:54:54 +08006688 if (!device->bdev &&
6689 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006690 /*
6691 * this happens when a device that was properly setup
6692 * in the device info lists suddenly goes bad.
6693 * device->bdev is NULL, and so we have to set
6694 * device->missing to one here
6695 */
Miao Xie5f375832014-09-03 21:35:46 +08006696 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006697 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006698 }
Miao Xie5f375832014-09-03 21:35:46 +08006699
6700 /* Move the device to its own fs_devices */
6701 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006702 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6703 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006704
6705 list_move(&device->dev_list, &fs_devices->devices);
6706 device->fs_devices->num_devices--;
6707 fs_devices->num_devices++;
6708
6709 device->fs_devices->missing_devices--;
6710 fs_devices->missing_devices++;
6711
6712 device->fs_devices = fs_devices;
6713 }
Yan Zheng2b820322008-11-17 21:11:30 -05006714 }
6715
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006716 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006717 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006718 if (device->generation !=
6719 btrfs_device_generation(leaf, dev_item))
6720 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006721 }
Chris Mason0b86a832008-03-24 15:01:56 -04006722
6723 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006724 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006725 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006726 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006727 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006728 atomic64_add(device->total_bytes - device->bytes_used,
6729 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006730 }
Chris Mason0b86a832008-03-24 15:01:56 -04006731 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006732 return ret;
6733}
6734
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006735int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006736{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006737 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006738 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006739 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006740 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006741 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006742 u8 *array_ptr;
6743 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006744 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006745 u32 num_stripes;
6746 u32 array_size;
6747 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006748 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006749 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006750 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006751
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006752 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006753 /*
6754 * This will create extent buffer of nodesize, superblock size is
6755 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6756 * overallocate but we can keep it as-is, only the first page is used.
6757 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006758 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006759 if (IS_ERR(sb))
6760 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006761 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006762 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006763 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006764 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006765 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006766 * pages up-to-date when the page is larger: extent does not cover the
6767 * whole page and consequently check_page_uptodate does not find all
6768 * the page's extents up-to-date (the hole beyond sb),
6769 * write_extent_buffer then triggers a WARN_ON.
6770 *
6771 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6772 * but sb spans only this function. Add an explicit SetPageUptodate call
6773 * to silence the warning eg. on PowerPC 64.
6774 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006775 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006776 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006777
Chris Masona061fc82008-05-07 11:43:44 -04006778 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006779 array_size = btrfs_super_sys_array_size(super_copy);
6780
David Sterba1ffb22c2014-10-31 19:02:42 +01006781 array_ptr = super_copy->sys_chunk_array;
6782 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6783 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006784
David Sterba1ffb22c2014-10-31 19:02:42 +01006785 while (cur_offset < array_size) {
6786 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006787 len = sizeof(*disk_key);
6788 if (cur_offset + len > array_size)
6789 goto out_short_read;
6790
Chris Mason0b86a832008-03-24 15:01:56 -04006791 btrfs_disk_key_to_cpu(&key, disk_key);
6792
David Sterba1ffb22c2014-10-31 19:02:42 +01006793 array_ptr += len;
6794 sb_array_offset += len;
6795 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006796
Chris Mason0d81ba52008-03-24 15:02:07 -04006797 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006798 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006799 /*
6800 * At least one btrfs_chunk with one stripe must be
6801 * present, exact stripe count check comes afterwards
6802 */
6803 len = btrfs_chunk_item_size(1);
6804 if (cur_offset + len > array_size)
6805 goto out_short_read;
6806
6807 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006808 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006809 btrfs_err(fs_info,
6810 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006811 num_stripes, cur_offset);
6812 ret = -EIO;
6813 break;
6814 }
6815
Liu Boe06cd3d2016-06-03 12:05:15 -07006816 type = btrfs_chunk_type(sb, chunk);
6817 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006818 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006819 "invalid chunk type %llu in sys_array at offset %u",
6820 type, cur_offset);
6821 ret = -EIO;
6822 break;
6823 }
6824
David Sterbae3540ea2014-11-05 15:24:51 +01006825 len = btrfs_chunk_item_size(num_stripes);
6826 if (cur_offset + len > array_size)
6827 goto out_short_read;
6828
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006829 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006830 if (ret)
6831 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006832 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006833 btrfs_err(fs_info,
6834 "unexpected item type %u in sys_array at offset %u",
6835 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006836 ret = -EIO;
6837 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006838 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006839 array_ptr += len;
6840 sb_array_offset += len;
6841 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006842 }
Liu Bod8651772016-06-03 17:41:42 -07006843 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006844 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006845 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006846
6847out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006848 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006849 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006850 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006851 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006852 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006853}
6854
Qu Wenruo21634a12017-03-09 09:34:36 +08006855/*
6856 * Check if all chunks in the fs are OK for read-write degraded mount
6857 *
Anand Jain6528b992017-12-18 17:08:59 +08006858 * If the @failing_dev is specified, it's accounted as missing.
6859 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006860 * Return true if all chunks meet the minimal RW mount requirements.
6861 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6862 */
Anand Jain6528b992017-12-18 17:08:59 +08006863bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6864 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006865{
6866 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6867 struct extent_map *em;
6868 u64 next_start = 0;
6869 bool ret = true;
6870
6871 read_lock(&map_tree->map_tree.lock);
6872 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6873 read_unlock(&map_tree->map_tree.lock);
6874 /* No chunk at all? Return false anyway */
6875 if (!em) {
6876 ret = false;
6877 goto out;
6878 }
6879 while (em) {
6880 struct map_lookup *map;
6881 int missing = 0;
6882 int max_tolerated;
6883 int i;
6884
6885 map = em->map_lookup;
6886 max_tolerated =
6887 btrfs_get_num_tolerated_disk_barrier_failures(
6888 map->type);
6889 for (i = 0; i < map->num_stripes; i++) {
6890 struct btrfs_device *dev = map->stripes[i].dev;
6891
Anand Jaine6e674b2017-12-04 12:54:54 +08006892 if (!dev || !dev->bdev ||
6893 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006894 dev->last_flush_error)
6895 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006896 else if (failing_dev && failing_dev == dev)
6897 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006898 }
6899 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006900 if (!failing_dev)
6901 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006902 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6903 em->start, missing, max_tolerated);
6904 free_extent_map(em);
6905 ret = false;
6906 goto out;
6907 }
6908 next_start = extent_map_end(em);
6909 free_extent_map(em);
6910
6911 read_lock(&map_tree->map_tree.lock);
6912 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6913 (u64)(-1) - next_start);
6914 read_unlock(&map_tree->map_tree.lock);
6915 }
6916out:
6917 return ret;
6918}
6919
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006920int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006921{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006922 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006923 struct btrfs_path *path;
6924 struct extent_buffer *leaf;
6925 struct btrfs_key key;
6926 struct btrfs_key found_key;
6927 int ret;
6928 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006929 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006930
Chris Mason0b86a832008-03-24 15:01:56 -04006931 path = btrfs_alloc_path();
6932 if (!path)
6933 return -ENOMEM;
6934
Anand Jain3dd0f7a2018-04-12 10:29:32 +08006935 /*
6936 * uuid_mutex is needed only if we are mounting a sprout FS
6937 * otherwise we don't need it.
6938 */
Li Zefanb367e472011-12-07 11:38:24 +08006939 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006940 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006941
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006942 /*
6943 * Read all device items, and then all the chunk items. All
6944 * device items are found before any chunk item (their object id
6945 * is smaller than the lowest possible object id for a chunk
6946 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006947 */
6948 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6949 key.offset = 0;
6950 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006952 if (ret < 0)
6953 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006954 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006955 leaf = path->nodes[0];
6956 slot = path->slots[0];
6957 if (slot >= btrfs_header_nritems(leaf)) {
6958 ret = btrfs_next_leaf(root, path);
6959 if (ret == 0)
6960 continue;
6961 if (ret < 0)
6962 goto error;
6963 break;
6964 }
6965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006966 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6967 struct btrfs_dev_item *dev_item;
6968 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006969 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006970 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006971 if (ret)
6972 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006973 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006974 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6975 struct btrfs_chunk *chunk;
6976 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006977 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006978 if (ret)
6979 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006980 }
6981 path->slots[0]++;
6982 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006983
6984 /*
6985 * After loading chunk tree, we've got all device information,
6986 * do another round of validation checks.
6987 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006988 if (total_dev != fs_info->fs_devices->total_devices) {
6989 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006990 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006991 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006992 total_dev);
6993 ret = -EINVAL;
6994 goto error;
6995 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006996 if (btrfs_super_total_bytes(fs_info->super_copy) <
6997 fs_info->fs_devices->total_rw_bytes) {
6998 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006999 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007000 btrfs_super_total_bytes(fs_info->super_copy),
7001 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007002 ret = -EINVAL;
7003 goto error;
7004 }
Chris Mason0b86a832008-03-24 15:01:56 -04007005 ret = 0;
7006error:
David Sterba34441362016-10-04 19:34:27 +02007007 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007008 mutex_unlock(&uuid_mutex);
7009
Yan Zheng2b820322008-11-17 21:11:30 -05007010 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007011 return ret;
7012}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007013
Miao Xiecb517ea2013-05-15 07:48:19 +00007014void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7015{
7016 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7017 struct btrfs_device *device;
7018
Liu Bo29cc83f2014-05-11 23:14:59 +08007019 while (fs_devices) {
7020 mutex_lock(&fs_devices->device_list_mutex);
7021 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007022 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007023 mutex_unlock(&fs_devices->device_list_mutex);
7024
7025 fs_devices = fs_devices->seed;
7026 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007027}
7028
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007029static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7030{
7031 int i;
7032
7033 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7034 btrfs_dev_stat_reset(dev, i);
7035}
7036
7037int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7038{
7039 struct btrfs_key key;
7040 struct btrfs_key found_key;
7041 struct btrfs_root *dev_root = fs_info->dev_root;
7042 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7043 struct extent_buffer *eb;
7044 int slot;
7045 int ret = 0;
7046 struct btrfs_device *device;
7047 struct btrfs_path *path = NULL;
7048 int i;
7049
7050 path = btrfs_alloc_path();
7051 if (!path) {
7052 ret = -ENOMEM;
7053 goto out;
7054 }
7055
7056 mutex_lock(&fs_devices->device_list_mutex);
7057 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7058 int item_size;
7059 struct btrfs_dev_stats_item *ptr;
7060
David Sterba242e2952016-01-25 17:51:31 +01007061 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7062 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007063 key.offset = device->devid;
7064 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7065 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007066 __btrfs_reset_dev_stats(device);
7067 device->dev_stats_valid = 1;
7068 btrfs_release_path(path);
7069 continue;
7070 }
7071 slot = path->slots[0];
7072 eb = path->nodes[0];
7073 btrfs_item_key_to_cpu(eb, &found_key, slot);
7074 item_size = btrfs_item_size_nr(eb, slot);
7075
7076 ptr = btrfs_item_ptr(eb, slot,
7077 struct btrfs_dev_stats_item);
7078
7079 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7080 if (item_size >= (1 + i) * sizeof(__le64))
7081 btrfs_dev_stat_set(device, i,
7082 btrfs_dev_stats_value(eb, ptr, i));
7083 else
7084 btrfs_dev_stat_reset(device, i);
7085 }
7086
7087 device->dev_stats_valid = 1;
7088 btrfs_dev_stat_print_on_load(device);
7089 btrfs_release_path(path);
7090 }
7091 mutex_unlock(&fs_devices->device_list_mutex);
7092
7093out:
7094 btrfs_free_path(path);
7095 return ret < 0 ? ret : 0;
7096}
7097
7098static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007099 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007100 struct btrfs_device *device)
7101{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007102 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007103 struct btrfs_path *path;
7104 struct btrfs_key key;
7105 struct extent_buffer *eb;
7106 struct btrfs_dev_stats_item *ptr;
7107 int ret;
7108 int i;
7109
David Sterba242e2952016-01-25 17:51:31 +01007110 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7111 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007112 key.offset = device->devid;
7113
7114 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007115 if (!path)
7116 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007117 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7118 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007119 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007120 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007121 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007122 goto out;
7123 }
7124
7125 if (ret == 0 &&
7126 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7127 /* need to delete old one and insert a new one */
7128 ret = btrfs_del_item(trans, dev_root, path);
7129 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007130 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007131 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007132 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007133 goto out;
7134 }
7135 ret = 1;
7136 }
7137
7138 if (ret == 1) {
7139 /* need to insert a new item */
7140 btrfs_release_path(path);
7141 ret = btrfs_insert_empty_item(trans, dev_root, path,
7142 &key, sizeof(*ptr));
7143 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007144 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007145 "insert dev_stats item for device %s failed %d",
7146 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007147 goto out;
7148 }
7149 }
7150
7151 eb = path->nodes[0];
7152 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7153 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7154 btrfs_set_dev_stats_value(eb, ptr, i,
7155 btrfs_dev_stat_read(device, i));
7156 btrfs_mark_buffer_dirty(eb);
7157
7158out:
7159 btrfs_free_path(path);
7160 return ret;
7161}
7162
7163/*
7164 * called from commit_transaction. Writes all changed device stats to disk.
7165 */
7166int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7167 struct btrfs_fs_info *fs_info)
7168{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007169 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7170 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007171 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007172 int ret = 0;
7173
7174 mutex_lock(&fs_devices->device_list_mutex);
7175 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007176 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7177 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007178 continue;
7179
Nikolay Borisov9deae962017-10-24 13:47:37 +03007180
7181 /*
7182 * There is a LOAD-LOAD control dependency between the value of
7183 * dev_stats_ccnt and updating the on-disk values which requires
7184 * reading the in-memory counters. Such control dependencies
7185 * require explicit read memory barriers.
7186 *
7187 * This memory barriers pairs with smp_mb__before_atomic in
7188 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7189 * barrier implied by atomic_xchg in
7190 * btrfs_dev_stats_read_and_reset
7191 */
7192 smp_rmb();
7193
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007194 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007195 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007196 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007197 }
7198 mutex_unlock(&fs_devices->device_list_mutex);
7199
7200 return ret;
7201}
7202
Stefan Behrens442a4f62012-05-25 16:06:08 +02007203void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7204{
7205 btrfs_dev_stat_inc(dev, index);
7206 btrfs_dev_stat_print_on_error(dev);
7207}
7208
Eric Sandeen48a3b632013-04-25 20:41:01 +00007209static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007210{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007211 if (!dev->dev_stats_valid)
7212 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007213 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007214 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007215 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007216 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7217 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7218 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007219 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7220 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007221}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007222
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007223static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7224{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007225 int i;
7226
7227 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7228 if (btrfs_dev_stat_read(dev, i) != 0)
7229 break;
7230 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7231 return; /* all values == 0, suppress message */
7232
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007233 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007234 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007235 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007236 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7237 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7238 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7239 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7240 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7241}
7242
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007243int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007244 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007245{
7246 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007247 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007248 int i;
7249
7250 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007251 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007252 mutex_unlock(&fs_devices->device_list_mutex);
7253
7254 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007255 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007256 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007257 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007258 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007259 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007260 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007261 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7262 if (stats->nr_items > i)
7263 stats->values[i] =
7264 btrfs_dev_stat_read_and_reset(dev, i);
7265 else
7266 btrfs_dev_stat_reset(dev, i);
7267 }
7268 } else {
7269 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7270 if (stats->nr_items > i)
7271 stats->values[i] = btrfs_dev_stat_read(dev, i);
7272 }
7273 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7274 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7275 return 0;
7276}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007277
David Sterbada353f62017-02-14 17:55:53 +01007278void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007279{
7280 struct buffer_head *bh;
7281 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007282 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007283
Anand Jain12b1c262015-08-14 18:32:59 +08007284 if (!bdev)
7285 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007286
Anand Jain12b1c262015-08-14 18:32:59 +08007287 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7288 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007289
Anand Jain12b1c262015-08-14 18:32:59 +08007290 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7291 continue;
7292
7293 disk_super = (struct btrfs_super_block *)bh->b_data;
7294
7295 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7296 set_buffer_dirty(bh);
7297 sync_dirty_buffer(bh);
7298 brelse(bh);
7299 }
7300
7301 /* Notify udev that device has changed */
7302 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7303
7304 /* Update ctime/mtime for device path for libblkid */
7305 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007306}
Miao Xie935e5cc2014-09-03 21:35:33 +08007307
7308/*
7309 * Update the size of all devices, which is used for writing out the
7310 * super blocks.
7311 */
7312void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7313{
7314 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7315 struct btrfs_device *curr, *next;
7316
7317 if (list_empty(&fs_devices->resized_devices))
7318 return;
7319
7320 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007321 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007322 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7323 resized_list) {
7324 list_del_init(&curr->resized_list);
7325 curr->commit_total_bytes = curr->disk_total_bytes;
7326 }
David Sterba34441362016-10-04 19:34:27 +02007327 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007328 mutex_unlock(&fs_devices->device_list_mutex);
7329}
Miao Xiece7213c2014-09-03 21:35:34 +08007330
7331/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007332void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007333{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007334 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007335 struct extent_map *em;
7336 struct map_lookup *map;
7337 struct btrfs_device *dev;
7338 int i;
7339
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007340 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007341 return;
7342
7343 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007344 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007345 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007346 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007347
7348 for (i = 0; i < map->num_stripes; i++) {
7349 dev = map->stripes[i].dev;
7350 dev->commit_bytes_used = dev->bytes_used;
7351 }
7352 }
David Sterba34441362016-10-04 19:34:27 +02007353 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007354}
Anand Jain5a13f432015-03-10 06:38:31 +08007355
7356void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7357{
7358 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7359 while (fs_devices) {
7360 fs_devices->fs_info = fs_info;
7361 fs_devices = fs_devices->seed;
7362 }
7363}
7364
7365void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7366{
7367 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7368 while (fs_devices) {
7369 fs_devices->fs_info = NULL;
7370 fs_devices = fs_devices->seed;
7371 }
7372}