blob: 8ed90107b727192da47ab4e8c017ecaa5f4204a0 [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
Anand Jain542c5902018-04-12 10:29:34 +08001149 mutex_lock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001150 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 }
Anand Jain542c5902018-04-12 10:29:34 +08001157 mutex_unlock(&fs_devices->device_list_mutex);
1158
Chris Mason8a4b83c2008-03-24 15:02:07 -04001159 return ret;
1160}
1161
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001162static void btrfs_release_disk_super(struct page *page)
Anand Jain6cf86a002016-02-13 10:01:29 +08001163{
1164 kunmap(page);
1165 put_page(page);
1166}
1167
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001168static int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
1169 struct page **page,
1170 struct btrfs_super_block **disk_super)
Anand Jain6cf86a002016-02-13 10:01:29 +08001171{
1172 void *p;
1173 pgoff_t index;
1174
1175 /* make sure our super fits in the device */
1176 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1177 return 1;
1178
1179 /* make sure our super fits in the page */
1180 if (sizeof(**disk_super) > PAGE_SIZE)
1181 return 1;
1182
1183 /* make sure our super doesn't straddle pages on disk */
1184 index = bytenr >> PAGE_SHIFT;
1185 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1186 return 1;
1187
1188 /* pull in the page with our super */
1189 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1190 index, GFP_KERNEL);
1191
1192 if (IS_ERR_OR_NULL(*page))
1193 return 1;
1194
1195 p = kmap(*page);
1196
1197 /* align our pointer to the offset of the super block */
1198 *disk_super = p + (bytenr & ~PAGE_MASK);
1199
1200 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1201 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1202 btrfs_release_disk_super(*page);
1203 return 1;
1204 }
1205
1206 if ((*disk_super)->label[0] &&
1207 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1208 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1209
1210 return 0;
1211}
1212
David Sterba6f60cbd2013-02-15 11:31:02 -07001213/*
1214 * Look for a btrfs signature on a device. This may be called out of the mount path
1215 * and we are not allowed to call set_blocksize during the scan. The superblock
1216 * is read via pagecache
1217 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001218int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001219 struct btrfs_fs_devices **fs_devices_ret)
1220{
1221 struct btrfs_super_block *disk_super;
Anand Jaine124ece2018-01-18 22:02:35 +08001222 struct btrfs_device *device;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001223 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001224 struct page *page;
Anand Jaine124ece2018-01-18 22:02:35 +08001225 int ret = 0;
David Sterba6f60cbd2013-02-15 11:31:02 -07001226 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001227
David Sterba6f60cbd2013-02-15 11:31:02 -07001228 /*
1229 * we would like to check all the supers, but that would make
1230 * a btrfs mount succeed after a mkfs from a different FS.
1231 * So, we need to add a special mount option to scan for
1232 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1233 */
1234 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001235 flags |= FMODE_EXCL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001236
1237 bdev = blkdev_get_by_path(path, flags, holder);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001238 if (IS_ERR(bdev))
1239 return PTR_ERR(bdev);
David Sterba6f60cbd2013-02-15 11:31:02 -07001240
Anand Jain05a5c552017-12-15 15:40:16 +08001241 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super)) {
1242 ret = -EINVAL;
David Sterba6f60cbd2013-02-15 11:31:02 -07001243 goto error_bdev_put;
Anand Jain05a5c552017-12-15 15:40:16 +08001244 }
David Sterba6f60cbd2013-02-15 11:31:02 -07001245
Anand Jainb6ed73b2018-04-12 10:29:24 +08001246 mutex_lock(&uuid_mutex);
Anand Jain3acbcbf2018-01-18 22:02:36 +08001247 device = device_list_add(path, disk_super);
Anand Jaine124ece2018-01-18 22:02:35 +08001248 if (IS_ERR(device))
1249 ret = PTR_ERR(device);
1250 else
1251 *fs_devices_ret = device->fs_devices;
Anand Jainb6ed73b2018-04-12 10:29:24 +08001252 mutex_unlock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001253
Anand Jain6cf86a002016-02-13 10:01:29 +08001254 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001255
1256error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001257 blkdev_put(bdev, flags);
Anand Jainb6ed73b2018-04-12 10:29:24 +08001258
Chris Mason8a4b83c2008-03-24 15:02:07 -04001259 return ret;
1260}
Chris Mason0b86a832008-03-24 15:01:56 -04001261
Miao Xie6d07bce2011-01-05 10:07:31 +00001262/* helper to account the used device space in the range */
1263int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1264 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001265{
1266 struct btrfs_key key;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001267 struct btrfs_root *root = device->fs_info->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001268 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001269 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001270 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001271 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001272 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001273 struct extent_buffer *l;
1274
Miao Xie6d07bce2011-01-05 10:07:31 +00001275 *length = 0;
1276
Anand Jain401e29c2017-12-04 12:54:55 +08001277 if (start >= device->total_bytes ||
1278 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Miao Xie6d07bce2011-01-05 10:07:31 +00001279 return 0;
1280
Yan Zheng2b820322008-11-17 21:11:30 -05001281 path = btrfs_alloc_path();
1282 if (!path)
1283 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001284 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001285
Chris Mason0b86a832008-03-24 15:01:56 -04001286 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001287 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001288 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001289
1290 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001291 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001292 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001293 if (ret > 0) {
1294 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1295 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001296 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001297 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001298
Chris Mason0b86a832008-03-24 15:01:56 -04001299 while (1) {
1300 l = path->nodes[0];
1301 slot = path->slots[0];
1302 if (slot >= btrfs_header_nritems(l)) {
1303 ret = btrfs_next_leaf(root, path);
1304 if (ret == 0)
1305 continue;
1306 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001307 goto out;
1308
1309 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001310 }
1311 btrfs_item_key_to_cpu(l, &key, slot);
1312
1313 if (key.objectid < device->devid)
1314 goto next;
1315
1316 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001317 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001318
David Sterba962a2982014-06-04 18:41:45 +02001319 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001320 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001321
Chris Mason0b86a832008-03-24 15:01:56 -04001322 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001323 extent_end = key.offset + btrfs_dev_extent_length(l,
1324 dev_extent);
1325 if (key.offset <= start && extent_end > end) {
1326 *length = end - start + 1;
1327 break;
1328 } else if (key.offset <= start && extent_end > start)
1329 *length += extent_end - start;
1330 else if (key.offset > start && extent_end <= end)
1331 *length += extent_end - key.offset;
1332 else if (key.offset > start && key.offset <= end) {
1333 *length += end - key.offset + 1;
1334 break;
1335 } else if (key.offset > end)
1336 break;
1337
1338next:
1339 path->slots[0]++;
1340 }
1341 ret = 0;
1342out:
1343 btrfs_free_path(path);
1344 return ret;
1345}
1346
Jeff Mahoney499f3772015-06-15 09:41:17 -04001347static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001348 struct btrfs_device *device,
1349 u64 *start, u64 len)
1350{
Jeff Mahoneyfb456252016-06-22 18:54:56 -04001351 struct btrfs_fs_info *fs_info = device->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001352 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001353 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001354 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001355 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001356
Jeff Mahoney499f3772015-06-15 09:41:17 -04001357 if (transaction)
1358 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001359again:
1360 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001361 struct map_lookup *map;
1362 int i;
1363
Jeff Mahoney95617d62015-06-03 10:55:48 -04001364 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001365 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001366 u64 end;
1367
Josef Bacik6df9a952013-06-27 13:22:46 -04001368 if (map->stripes[i].dev != device)
1369 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001370 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001371 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001372 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001374 /*
1375 * Make sure that while processing the pinned list we do
1376 * not override our *start with a lower value, because
1377 * we can have pinned chunks that fall within this
1378 * device hole and that have lower physical addresses
1379 * than the pending chunks we processed before. If we
1380 * do not take this special care we can end up getting
1381 * 2 pending chunks that start at the same physical
1382 * device offsets because the end offset of a pinned
1383 * chunk can be equal to the start offset of some
1384 * pending chunk.
1385 */
1386 end = map->stripes[i].physical + em->orig_block_len;
1387 if (end > *start) {
1388 *start = end;
1389 ret = 1;
1390 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001391 }
1392 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001393 if (search_list != &fs_info->pinned_chunks) {
1394 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001395 goto again;
1396 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001397
1398 return ret;
1399}
1400
1401
Chris Mason0b86a832008-03-24 15:01:56 -04001402/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001403 * find_free_dev_extent_start - find free space in the specified device
1404 * @device: the device which we search the free space in
1405 * @num_bytes: the size of the free space that we need
1406 * @search_start: the position from which to begin the search
1407 * @start: store the start of the free space.
1408 * @len: the size of the free space. that we find, or the size
1409 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001410 *
Chris Mason0b86a832008-03-24 15:01:56 -04001411 * this uses a pretty simple search, the expectation is that it is
1412 * called very infrequently and that a given device has a small number
1413 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001414 *
1415 * @start is used to store the start of the free space if we find. But if we
1416 * don't find suitable free space, it will be used to store the start position
1417 * of the max free space.
1418 *
1419 * @len is used to store the size of the free space that we find.
1420 * But if we don't find suitable free space, it is used to store the size of
1421 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001422 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001423int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1424 struct btrfs_device *device, u64 num_bytes,
1425 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001426{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 struct btrfs_fs_info *fs_info = device->fs_info;
1428 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001429 struct btrfs_key key;
Miao Xie7bfc8372011-01-05 10:07:26 +00001430 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001431 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001432 u64 hole_size;
1433 u64 max_hole_start;
1434 u64 max_hole_size;
1435 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001436 u64 search_end = device->total_bytes;
1437 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001438 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001439 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001440
1441 /*
1442 * We don't want to overwrite the superblock on the drive nor any area
1443 * used by the boot loader (grub for example), so we make sure to start
1444 * at an offset of at least 1MB.
1445 */
David Sterba0d0c71b2017-06-15 01:30:06 +02001446 search_start = max_t(u64, search_start, SZ_1M);
Chris Mason0b86a832008-03-24 15:01:56 -04001447
Josef Bacik6df9a952013-06-27 13:22:46 -04001448 path = btrfs_alloc_path();
1449 if (!path)
1450 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001451
Miao Xie7bfc8372011-01-05 10:07:26 +00001452 max_hole_start = search_start;
1453 max_hole_size = 0;
1454
Zhao Leif2ab7612015-02-16 18:52:17 +08001455again:
Anand Jain401e29c2017-12-04 12:54:55 +08001456 if (search_start >= search_end ||
1457 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001458 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001459 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001460 }
1461
David Sterbae4058b52015-11-27 16:31:35 +01001462 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001463 path->search_commit_root = 1;
1464 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001465
Chris Mason0b86a832008-03-24 15:01:56 -04001466 key.objectid = device->devid;
1467 key.offset = search_start;
1468 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001469
Li Zefan125ccb02011-12-08 15:07:24 +08001470 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001471 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001472 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001473 if (ret > 0) {
1474 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1475 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001476 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001477 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001478
Chris Mason0b86a832008-03-24 15:01:56 -04001479 while (1) {
1480 l = path->nodes[0];
1481 slot = path->slots[0];
1482 if (slot >= btrfs_header_nritems(l)) {
1483 ret = btrfs_next_leaf(root, path);
1484 if (ret == 0)
1485 continue;
1486 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001487 goto out;
1488
1489 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001490 }
1491 btrfs_item_key_to_cpu(l, &key, slot);
1492
1493 if (key.objectid < device->devid)
1494 goto next;
1495
1496 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001497 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001498
David Sterba962a2982014-06-04 18:41:45 +02001499 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001500 goto next;
1501
Miao Xie7bfc8372011-01-05 10:07:26 +00001502 if (key.offset > search_start) {
1503 hole_size = key.offset - search_start;
1504
Josef Bacik6df9a952013-06-27 13:22:46 -04001505 /*
1506 * Have to check before we set max_hole_start, otherwise
1507 * we could end up sending back this offset anyway.
1508 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001509 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001510 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001511 hole_size)) {
1512 if (key.offset >= search_start) {
1513 hole_size = key.offset - search_start;
1514 } else {
1515 WARN_ON_ONCE(1);
1516 hole_size = 0;
1517 }
1518 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001519
Miao Xie7bfc8372011-01-05 10:07:26 +00001520 if (hole_size > max_hole_size) {
1521 max_hole_start = search_start;
1522 max_hole_size = hole_size;
1523 }
1524
1525 /*
1526 * If this free space is greater than which we need,
1527 * it must be the max free space that we have found
1528 * until now, so max_hole_start must point to the start
1529 * of this free space and the length of this free space
1530 * is stored in max_hole_size. Thus, we return
1531 * max_hole_start and max_hole_size and go back to the
1532 * caller.
1533 */
1534 if (hole_size >= num_bytes) {
1535 ret = 0;
1536 goto out;
1537 }
1538 }
1539
Chris Mason0b86a832008-03-24 15:01:56 -04001540 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001541 extent_end = key.offset + btrfs_dev_extent_length(l,
1542 dev_extent);
1543 if (extent_end > search_start)
1544 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001545next:
1546 path->slots[0]++;
1547 cond_resched();
1548 }
Chris Mason0b86a832008-03-24 15:01:56 -04001549
liubo38c01b92011-08-02 02:39:03 +00001550 /*
1551 * At this point, search_start should be the end of
1552 * allocated dev extents, and when shrinking the device,
1553 * search_end may be smaller than search_start.
1554 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001555 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001556 hole_size = search_end - search_start;
1557
Jeff Mahoney499f3772015-06-15 09:41:17 -04001558 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001559 hole_size)) {
1560 btrfs_release_path(path);
1561 goto again;
1562 }
Chris Mason0b86a832008-03-24 15:01:56 -04001563
Zhao Leif2ab7612015-02-16 18:52:17 +08001564 if (hole_size > max_hole_size) {
1565 max_hole_start = search_start;
1566 max_hole_size = hole_size;
1567 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001568 }
1569
Miao Xie7bfc8372011-01-05 10:07:26 +00001570 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001571 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001572 ret = -ENOSPC;
1573 else
1574 ret = 0;
1575
1576out:
Yan Zheng2b820322008-11-17 21:11:30 -05001577 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001578 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001579 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001580 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001581 return ret;
1582}
1583
Jeff Mahoney499f3772015-06-15 09:41:17 -04001584int find_free_dev_extent(struct btrfs_trans_handle *trans,
1585 struct btrfs_device *device, u64 num_bytes,
1586 u64 *start, u64 *len)
1587{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001588 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001589 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001590 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001591}
1592
Christoph Hellwigb2950862008-12-02 09:54:17 -05001593static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001594 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001595 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001597 struct btrfs_fs_info *fs_info = device->fs_info;
1598 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04001599 int ret;
1600 struct btrfs_path *path;
Chris Mason8f18cf12008-04-25 16:53:30 -04001601 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001602 struct btrfs_key found_key;
1603 struct extent_buffer *leaf = NULL;
1604 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001605
1606 path = btrfs_alloc_path();
1607 if (!path)
1608 return -ENOMEM;
1609
1610 key.objectid = device->devid;
1611 key.offset = start;
1612 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001613again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001614 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001615 if (ret > 0) {
1616 ret = btrfs_previous_item(root, path, key.objectid,
1617 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001618 if (ret)
1619 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001620 leaf = path->nodes[0];
1621 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1622 extent = btrfs_item_ptr(leaf, path->slots[0],
1623 struct btrfs_dev_extent);
1624 BUG_ON(found_key.offset > start || found_key.offset +
1625 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001626 key = found_key;
1627 btrfs_release_path(path);
1628 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001629 } else if (ret == 0) {
1630 leaf = path->nodes[0];
1631 extent = btrfs_item_ptr(leaf, path->slots[0],
1632 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001633 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001634 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001635 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001636 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001637
Miao Xie2196d6e2014-09-03 21:35:41 +08001638 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1639
Chris Mason8f18cf12008-04-25 16:53:30 -04001640 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001641 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001642 btrfs_handle_fs_error(fs_info, ret,
1643 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001644 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001645 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001646 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001647out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001648 btrfs_free_path(path);
1649 return ret;
1650}
1651
Eric Sandeen48a3b632013-04-25 20:41:01 +00001652static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1653 struct btrfs_device *device,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001654 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001655{
1656 int ret;
1657 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001658 struct btrfs_fs_info *fs_info = device->fs_info;
1659 struct btrfs_root *root = fs_info->dev_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001660 struct btrfs_dev_extent *extent;
1661 struct extent_buffer *leaf;
1662 struct btrfs_key key;
1663
Anand Jaine12c9622017-12-04 12:54:53 +08001664 WARN_ON(!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state));
Anand Jain401e29c2017-12-04 12:54:55 +08001665 WARN_ON(test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state));
Chris Mason0b86a832008-03-24 15:01:56 -04001666 path = btrfs_alloc_path();
1667 if (!path)
1668 return -ENOMEM;
1669
Chris Mason0b86a832008-03-24 15:01:56 -04001670 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001671 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001672 key.type = BTRFS_DEV_EXTENT_KEY;
1673 ret = btrfs_insert_empty_item(trans, root, path, &key,
1674 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001675 if (ret)
1676 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001677
1678 leaf = path->nodes[0];
1679 extent = btrfs_item_ptr(leaf, path->slots[0],
1680 struct btrfs_dev_extent);
Nikolay Borisovb5d90712017-08-18 17:58:23 +03001681 btrfs_set_dev_extent_chunk_tree(leaf, extent,
1682 BTRFS_CHUNK_TREE_OBJECTID);
Nikolay Borisov0ca00af2017-08-18 17:58:22 +03001683 btrfs_set_dev_extent_chunk_objectid(leaf, extent,
1684 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
Chris Masone17cade2008-04-15 15:41:47 -04001685 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1686
Chris Mason0b86a832008-03-24 15:01:56 -04001687 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1688 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001689out:
Chris Mason0b86a832008-03-24 15:01:56 -04001690 btrfs_free_path(path);
1691 return ret;
1692}
1693
Josef Bacik6df9a952013-06-27 13:22:46 -04001694static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001695{
Josef Bacik6df9a952013-06-27 13:22:46 -04001696 struct extent_map_tree *em_tree;
1697 struct extent_map *em;
1698 struct rb_node *n;
1699 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001700
Josef Bacik6df9a952013-06-27 13:22:46 -04001701 em_tree = &fs_info->mapping_tree.map_tree;
1702 read_lock(&em_tree->lock);
1703 n = rb_last(&em_tree->map);
1704 if (n) {
1705 em = rb_entry(n, struct extent_map, rb_node);
1706 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001707 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001708 read_unlock(&em_tree->lock);
1709
Chris Mason0b86a832008-03-24 15:01:56 -04001710 return ret;
1711}
1712
Ilya Dryomov53f10652013-08-12 14:33:01 +03001713static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1714 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001715{
1716 int ret;
1717 struct btrfs_key key;
1718 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001719 struct btrfs_path *path;
1720
Yan Zheng2b820322008-11-17 21:11:30 -05001721 path = btrfs_alloc_path();
1722 if (!path)
1723 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001724
1725 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1726 key.type = BTRFS_DEV_ITEM_KEY;
1727 key.offset = (u64)-1;
1728
Ilya Dryomov53f10652013-08-12 14:33:01 +03001729 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001730 if (ret < 0)
1731 goto error;
1732
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001733 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001734
Ilya Dryomov53f10652013-08-12 14:33:01 +03001735 ret = btrfs_previous_item(fs_info->chunk_root, path,
1736 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001737 BTRFS_DEV_ITEM_KEY);
1738 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001739 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001740 } else {
1741 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1742 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001743 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001744 }
1745 ret = 0;
1746error:
Yan Zheng2b820322008-11-17 21:11:30 -05001747 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001748 return ret;
1749}
1750
1751/*
1752 * the device information is stored in the chunk root
1753 * the btrfs_device struct should be fully filled in
1754 */
Anand Jainc74a0b02017-11-06 16:36:15 +08001755static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001756 struct btrfs_fs_info *fs_info,
Eric Sandeen48a3b632013-04-25 20:41:01 +00001757 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001758{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001759 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04001760 int ret;
1761 struct btrfs_path *path;
1762 struct btrfs_dev_item *dev_item;
1763 struct extent_buffer *leaf;
1764 struct btrfs_key key;
1765 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001766
Chris Mason0b86a832008-03-24 15:01:56 -04001767 path = btrfs_alloc_path();
1768 if (!path)
1769 return -ENOMEM;
1770
Chris Mason0b86a832008-03-24 15:01:56 -04001771 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1772 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001773 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001774
1775 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001776 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001777 if (ret)
1778 goto out;
1779
1780 leaf = path->nodes[0];
1781 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1782
1783 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001784 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001785 btrfs_set_device_type(leaf, dev_item, device->type);
1786 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1787 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1788 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001789 btrfs_set_device_total_bytes(leaf, dev_item,
1790 btrfs_device_get_disk_total_bytes(device));
1791 btrfs_set_device_bytes_used(leaf, dev_item,
1792 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001793 btrfs_set_device_group(leaf, dev_item, 0);
1794 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1795 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001796 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001797
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001798 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001799 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001800 ptr = btrfs_device_fsid(dev_item);
Anand Jain44880fd2017-07-29 17:50:09 +08001801 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_FSID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001802 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001803
Yan Zheng2b820322008-11-17 21:11:30 -05001804 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001805out:
1806 btrfs_free_path(path);
1807 return ret;
1808}
Chris Mason8f18cf12008-04-25 16:53:30 -04001809
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001810/*
1811 * Function to update ctime/mtime for a given device path.
1812 * Mainly used for ctime/mtime based probe like libblkid.
1813 */
David Sterbada353f62017-02-14 17:55:53 +01001814static void update_dev_time(const char *path_name)
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001815{
1816 struct file *filp;
1817
1818 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001819 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001820 return;
1821 file_update_time(filp);
1822 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001823}
1824
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001825static int btrfs_rm_dev_item(struct btrfs_fs_info *fs_info,
Chris Masona061fc82008-05-07 11:43:44 -04001826 struct btrfs_device *device)
1827{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04001828 struct btrfs_root *root = fs_info->chunk_root;
Chris Masona061fc82008-05-07 11:43:44 -04001829 int ret;
1830 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001831 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001832 struct btrfs_trans_handle *trans;
1833
Chris Masona061fc82008-05-07 11:43:44 -04001834 path = btrfs_alloc_path();
1835 if (!path)
1836 return -ENOMEM;
1837
Yan, Zhenga22285a2010-05-16 10:48:46 -04001838 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001839 if (IS_ERR(trans)) {
1840 btrfs_free_path(path);
1841 return PTR_ERR(trans);
1842 }
Chris Masona061fc82008-05-07 11:43:44 -04001843 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1844 key.type = BTRFS_DEV_ITEM_KEY;
1845 key.offset = device->devid;
1846
1847 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001848 if (ret) {
1849 if (ret > 0)
1850 ret = -ENOENT;
1851 btrfs_abort_transaction(trans, ret);
1852 btrfs_end_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001853 goto out;
1854 }
1855
1856 ret = btrfs_del_item(trans, root, path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001857 if (ret) {
1858 btrfs_abort_transaction(trans, ret);
1859 btrfs_end_transaction(trans);
1860 }
1861
Chris Masona061fc82008-05-07 11:43:44 -04001862out:
1863 btrfs_free_path(path);
Nikolay Borisov5e9f2ad2017-10-23 09:58:46 +03001864 if (!ret)
1865 ret = btrfs_commit_transaction(trans);
Chris Masona061fc82008-05-07 11:43:44 -04001866 return ret;
1867}
1868
David Sterba3cc31a02016-02-15 16:00:26 +01001869/*
1870 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1871 * filesystem. It's up to the caller to adjust that number regarding eg. device
1872 * replace.
1873 */
1874static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1875 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001876{
Chris Masona061fc82008-05-07 11:43:44 -04001877 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001878 unsigned seq;
David Sterba418775a2016-02-15 16:28:14 +01001879 int i;
Chris Masona061fc82008-05-07 11:43:44 -04001880
Miao Xiede98ced2013-01-29 10:13:12 +00001881 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001882 seq = read_seqbegin(&fs_info->profiles_lock);
Miao Xiede98ced2013-01-29 10:13:12 +00001883
Anand Jainbd45ffb2016-02-13 10:01:34 +08001884 all_avail = fs_info->avail_data_alloc_bits |
1885 fs_info->avail_system_alloc_bits |
1886 fs_info->avail_metadata_alloc_bits;
1887 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001888
David Sterba418775a2016-02-15 16:28:14 +01001889 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Anand Jain41a6e892018-04-25 19:01:43 +08001890 if (!(all_avail & btrfs_raid_array[i].bg_flag))
David Sterba418775a2016-02-15 16:28:14 +01001891 continue;
Chris Masona061fc82008-05-07 11:43:44 -04001892
David Sterba418775a2016-02-15 16:28:14 +01001893 if (num_devices < btrfs_raid_array[i].devs_min) {
Anand Jainf9fbcaa2018-04-25 19:01:44 +08001894 int ret = btrfs_raid_array[i].mindev_error;
Chris Masona061fc82008-05-07 11:43:44 -04001895
David Sterba418775a2016-02-15 16:28:14 +01001896 if (ret)
1897 return ret;
1898 }
Anand Jainbd45ffb2016-02-13 10:01:34 +08001899 }
1900
1901 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001902}
1903
Omar Sandovalc9162bd2017-08-22 23:46:04 -07001904static struct btrfs_device * btrfs_find_next_active_device(
1905 struct btrfs_fs_devices *fs_devs, struct btrfs_device *device)
Anand Jain88acff62016-05-03 17:44:43 +08001906{
1907 struct btrfs_device *next_device;
1908
1909 list_for_each_entry(next_device, &fs_devs->devices, dev_list) {
1910 if (next_device != device &&
Anand Jaine6e674b2017-12-04 12:54:54 +08001911 !test_bit(BTRFS_DEV_STATE_MISSING, &next_device->dev_state)
1912 && next_device->bdev)
Anand Jain88acff62016-05-03 17:44:43 +08001913 return next_device;
1914 }
1915
1916 return NULL;
1917}
1918
1919/*
1920 * Helper function to check if the given device is part of s_bdev / latest_bdev
1921 * and replace it with the provided or the next active device, in the context
1922 * where this function called, there should be always be another device (or
1923 * this_dev) which is active.
1924 */
1925void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1926 struct btrfs_device *device, struct btrfs_device *this_dev)
1927{
1928 struct btrfs_device *next_device;
1929
1930 if (this_dev)
1931 next_device = this_dev;
1932 else
1933 next_device = btrfs_find_next_active_device(fs_info->fs_devices,
1934 device);
1935 ASSERT(next_device);
1936
1937 if (fs_info->sb->s_bdev &&
1938 (fs_info->sb->s_bdev == device->bdev))
1939 fs_info->sb->s_bdev = next_device->bdev;
1940
1941 if (fs_info->fs_devices->latest_bdev == device->bdev)
1942 fs_info->fs_devices->latest_bdev = next_device->bdev;
1943}
1944
David Sterbada353f62017-02-14 17:55:53 +01001945int btrfs_rm_device(struct btrfs_fs_info *fs_info, const char *device_path,
1946 u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001947{
1948 struct btrfs_device *device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001949 struct btrfs_fs_devices *cur_devices;
Anand Jainb5185192018-04-12 10:29:30 +08001950 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001951 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001952 int ret = 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001953
1954 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001955
Anand Jainb5185192018-04-12 10:29:30 +08001956 num_devices = fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01001957 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001958 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
Chris Masona061fc82008-05-07 11:43:44 -04001959 WARN_ON(num_devices < 1);
1960 num_devices--;
1961 }
David Sterba7e79cb82018-03-24 02:11:38 +01001962 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Chris Masona061fc82008-05-07 11:43:44 -04001963
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001964 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001965 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001966 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001967
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001968 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1969 &device);
Anand Jain24fc5722016-02-13 10:01:36 +08001970 if (ret)
Chris Masona061fc82008-05-07 11:43:44 -04001971 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001972
Anand Jain401e29c2017-12-04 12:54:55 +08001973 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Anand Jain183860f2013-05-17 10:52:45 +00001974 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001975 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001976 }
1977
Anand Jainebbede42017-12-04 12:54:52 +08001978 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
1979 fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001980 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001981 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001982 }
1983
Anand Jainebbede42017-12-04 12:54:52 +08001984 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02001985 mutex_lock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001986 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001987 device->fs_devices->rw_devices--;
David Sterba34441362016-10-04 19:34:27 +02001988 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001989 }
Chris Masona061fc82008-05-07 11:43:44 -04001990
Carey Underwoodd7901552013-03-04 16:37:06 -06001991 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001992 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001993 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001994 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001995 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001996
Stefan Behrens63a212a2012-11-05 18:29:28 +01001997 /*
1998 * TODO: the superblock still includes this device in its num_devices
1999 * counter although write_all_supers() is not locked out. This
2000 * could give a filesystem state which requires a degraded mount.
2001 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 ret = btrfs_rm_dev_item(fs_info, device);
Chris Masona061fc82008-05-07 11:43:44 -04002003 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002004 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04002005
Anand Jaine12c9622017-12-04 12:54:53 +08002006 clear_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002007 btrfs_scrub_cancel_dev(fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04002008
2009 /*
2010 * the device list mutex makes sure that we don't change
2011 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01002012 * the device supers. Whoever is writing all supers, should
2013 * lock the device list mutex before getting the number of
2014 * devices in the super block (super_copy). Conversely,
2015 * whoever updates the number of devices in the super block
2016 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04002017 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002018
Anand Jain41a52a02018-04-12 10:29:31 +08002019 /*
2020 * In normal cases the cur_devices == fs_devices. But in case
2021 * of deleting a seed device, the cur_devices should point to
2022 * its own fs_devices listed under the fs_devices->seed.
2023 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00002024 cur_devices = device->fs_devices;
Anand Jainb5185192018-04-12 10:29:30 +08002025 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002026 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002027
Anand Jain41a52a02018-04-12 10:29:31 +08002028 cur_devices->num_devices--;
2029 cur_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05002030
Anand Jaine6e674b2017-12-04 12:54:54 +08002031 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
Anand Jain41a52a02018-04-12 10:29:31 +08002032 cur_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05002033
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002034 btrfs_assign_next_active_device(fs_info, device, NULL);
Yan Zheng2b820322008-11-17 21:11:30 -05002035
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002036 if (device->bdev) {
Anand Jain41a52a02018-04-12 10:29:31 +08002037 cur_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002038 /* remove sysfs entry */
Anand Jainb5185192018-04-12 10:29:30 +08002039 btrfs_sysfs_rm_device_link(fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05002040 }
Anand Jain99994cd2014-06-03 11:36:00 +08002041
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002042 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
2043 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
Anand Jainb5185192018-04-12 10:29:30 +08002044 mutex_unlock(&fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002045
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002046 /*
2047 * at this point, the device is zero sized and detached from
2048 * the devices list. All that's left is to zero out the old
2049 * supers and free the device.
2050 */
Anand Jainebbede42017-12-04 12:54:52 +08002051 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002052 btrfs_scratch_superblocks(device->bdev, device->name->str);
2053
2054 btrfs_close_bdev(device);
David Sterbaf06c5962017-06-06 17:08:23 +02002055 call_rcu(&device->rcu, free_device_rcu);
Jeff Mahoneycea67ab2016-09-20 08:50:21 -04002056
Xiao Guangrong1f781602011-04-20 10:09:16 +00002057 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05002058 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002059 if (fs_devices->seed == cur_devices) {
2060 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05002061 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02002062 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002063 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05002064 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00002065 cur_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002066 close_fs_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002067 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002068 }
2069
Chris Masona061fc82008-05-07 11:43:44 -04002070out:
2071 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04002072 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08002073
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002074error_undo:
Anand Jainebbede42017-12-04 12:54:52 +08002075 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002076 mutex_lock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002077 list_add(&device->dev_alloc_list,
Anand Jainb5185192018-04-12 10:29:30 +08002078 &fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08002079 device->fs_devices->rw_devices++;
David Sterba34441362016-10-04 19:34:27 +02002080 mutex_unlock(&fs_info->chunk_mutex);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00002081 }
Anand Jain24fc5722016-02-13 10:01:36 +08002082 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04002083}
2084
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002085void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
2086 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002087{
Anand Jaind51908c2014-08-13 14:24:19 +08002088 struct btrfs_fs_devices *fs_devices;
2089
David Sterbaa32bf9a2018-03-16 02:21:22 +01002090 lockdep_assert_held(&fs_info->fs_devices->device_list_mutex);
Ilya Dryomov13572722013-10-02 20:41:01 +03002091
Anand Jain25e8e912014-08-20 10:56:56 +08002092 /*
2093 * in case of fs with no seed, srcdev->fs_devices will point
2094 * to fs_devices of fs_info. However when the dev being replaced is
2095 * a seed dev it will point to the seed's local fs_devices. In short
2096 * srcdev will have its correct fs_devices in both the cases.
2097 */
2098 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08002099
Stefan Behrense93c89c2012-11-05 17:33:06 +01002100 list_del_rcu(&srcdev->dev_list);
David Sterba619c47f2017-06-19 14:14:22 +02002101 list_del(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08002102 fs_devices->num_devices--;
Anand Jaine6e674b2017-12-04 12:54:54 +08002103 if (test_bit(BTRFS_DEV_STATE_MISSING, &srcdev->dev_state))
Anand Jaind51908c2014-08-13 14:24:19 +08002104 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002105
Anand Jainebbede42017-12-04 12:54:52 +08002106 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state))
Miao Xie82372bc2014-09-03 21:35:44 +08002107 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03002108
Miao Xie82372bc2014-09-03 21:35:44 +08002109 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002110 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002111}
2112
2113void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2114 struct btrfs_device *srcdev)
2115{
2116 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002117
Anand Jainebbede42017-12-04 12:54:52 +08002118 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &srcdev->dev_state)) {
Anand Jain48b3b9d2016-04-12 21:36:16 +08002119 /* zero out the old super if it is writable */
2120 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
2121 }
Anand Jain14238812016-07-22 06:04:53 +08002122
2123 btrfs_close_bdev(srcdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002124 call_rcu(&srcdev->rcu, free_device_rcu);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002125
Anand Jain94d5f0c2014-08-13 14:24:22 +08002126 /* if this is no devs we rather delete the fs_devices */
2127 if (!fs_devices->num_devices) {
2128 struct btrfs_fs_devices *tmp_fs_devices;
2129
Anand Jain6dd38f82017-10-17 06:53:50 +08002130 /*
2131 * On a mounted FS, num_devices can't be zero unless it's a
2132 * seed. In case of a seed device being replaced, the replace
2133 * target added to the sprout FS, so there will be no more
2134 * device left under the seed FS.
2135 */
2136 ASSERT(fs_devices->seeding);
2137
Anand Jain94d5f0c2014-08-13 14:24:22 +08002138 tmp_fs_devices = fs_info->fs_devices;
2139 while (tmp_fs_devices) {
2140 if (tmp_fs_devices->seed == fs_devices) {
2141 tmp_fs_devices->seed = fs_devices->seed;
2142 break;
2143 }
2144 tmp_fs_devices = tmp_fs_devices->seed;
2145 }
2146 fs_devices->seed = NULL;
Anand Jain0226e0e2018-04-12 10:29:27 +08002147 close_fs_devices(fs_devices);
Anand Jain8bef8402014-08-13 14:24:23 +08002148 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002149 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002150}
2151
2152void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2153 struct btrfs_device *tgtdev)
2154{
Anand Jaind9a071f2018-04-12 10:29:38 +08002155 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Anand Jaind2ff1b22015-03-10 06:38:42 +08002156
Anand Jaind9a071f2018-04-12 10:29:38 +08002157 WARN_ON(!tgtdev);
2158 mutex_lock(&fs_devices->device_list_mutex);
2159
2160 btrfs_sysfs_rm_device_link(fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002161
Anand Jain779bf3fe2016-04-18 16:51:23 +08002162 if (tgtdev->bdev)
Anand Jaind9a071f2018-04-12 10:29:38 +08002163 fs_devices->open_devices--;
Anand Jain779bf3fe2016-04-18 16:51:23 +08002164
Anand Jaind9a071f2018-04-12 10:29:38 +08002165 fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002166
Anand Jain88acff62016-05-03 17:44:43 +08002167 btrfs_assign_next_active_device(fs_info, tgtdev, NULL);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002168
Stefan Behrense93c89c2012-11-05 17:33:06 +01002169 list_del_rcu(&tgtdev->dev_list);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002170
Anand Jaind9a071f2018-04-12 10:29:38 +08002171 mutex_unlock(&fs_devices->device_list_mutex);
Anand Jain779bf3fe2016-04-18 16:51:23 +08002172
2173 /*
2174 * The update_dev_time() with in btrfs_scratch_superblocks()
2175 * may lead to a call to btrfs_show_devname() which will try
2176 * to hold device_list_mutex. And here this device
2177 * is already out of device list, so we don't have to hold
2178 * the device_list_mutex lock.
2179 */
2180 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Anand Jain14238812016-07-22 06:04:53 +08002181
2182 btrfs_close_bdev(tgtdev);
David Sterbaf06c5962017-06-06 17:08:23 +02002183 call_rcu(&tgtdev->rcu, free_device_rcu);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002184}
2185
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002186static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002187 const char *device_path,
Eric Sandeen48a3b632013-04-25 20:41:01 +00002188 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002189{
2190 int ret = 0;
2191 struct btrfs_super_block *disk_super;
2192 u64 devid;
2193 u8 *dev_uuid;
2194 struct block_device *bdev;
2195 struct buffer_head *bh;
2196
2197 *device = NULL;
2198 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002199 fs_info->bdev_holder, 0, &bdev, &bh);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002200 if (ret)
2201 return ret;
2202 disk_super = (struct btrfs_super_block *)bh->b_data;
2203 devid = btrfs_stack_device_id(&disk_super->dev_item);
2204 dev_uuid = disk_super->dev_item.uuid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002206 brelse(bh);
2207 if (!*device)
2208 ret = -ENOENT;
2209 blkdev_put(bdev, FMODE_READ);
2210 return ret;
2211}
2212
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002213int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
David Sterbada353f62017-02-14 17:55:53 +01002214 const char *device_path,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002215 struct btrfs_device **device)
2216{
2217 *device = NULL;
2218 if (strcmp(device_path, "missing") == 0) {
2219 struct list_head *devices;
2220 struct btrfs_device *tmp;
2221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002222 devices = &fs_info->fs_devices->devices;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002223 list_for_each_entry(tmp, devices, dev_list) {
Anand Jaine12c9622017-12-04 12:54:53 +08002224 if (test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
2225 &tmp->dev_state) && !tmp->bdev) {
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002226 *device = tmp;
2227 break;
2228 }
2229 }
2230
Anand Jaind74a6252015-08-14 18:32:56 +08002231 if (!*device)
2232 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002233
2234 return 0;
2235 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002236 return btrfs_find_device_by_path(fs_info, device_path, device);
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002237 }
2238}
2239
Yan Zheng2b820322008-11-17 21:11:30 -05002240/*
David Sterba5c5c0df2016-02-15 16:39:55 +01002241 * Lookup a device given by device id, or the path if the id is 0.
2242 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002243int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
David Sterbada353f62017-02-14 17:55:53 +01002244 const char *devpath,
2245 struct btrfs_device **device)
Anand Jain24e04742016-02-13 10:01:35 +08002246{
2247 int ret;
2248
David Sterba5c5c0df2016-02-15 16:39:55 +01002249 if (devid) {
Anand Jain24e04742016-02-13 10:01:35 +08002250 ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002251 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
Anand Jain24e04742016-02-13 10:01:35 +08002252 if (!*device)
2253 ret = -ENOENT;
2254 } else {
David Sterba5c5c0df2016-02-15 16:39:55 +01002255 if (!devpath || !devpath[0])
Anand Jainb3d1b152016-02-13 10:01:37 +08002256 return -EINVAL;
2257
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002258 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
Anand Jain24e04742016-02-13 10:01:35 +08002259 device);
2260 }
2261 return ret;
2262}
2263
Yan Zheng2b820322008-11-17 21:11:30 -05002264/*
2265 * does all the dirty work required for changing file system's UUID.
2266 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002267static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002268{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002269 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002270 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002271 struct btrfs_fs_devices *seed_devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002272 struct btrfs_super_block *disk_super = fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002273 struct btrfs_device *device;
2274 u64 super_flags;
2275
David Sterbaa32bf9a2018-03-16 02:21:22 +01002276 lockdep_assert_held(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002277 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002278 return -EINVAL;
2279
David Sterba2dfeca92017-06-14 02:48:07 +02002280 seed_devices = alloc_fs_devices(NULL);
Ilya Dryomov2208a372013-08-12 14:33:03 +03002281 if (IS_ERR(seed_devices))
2282 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002283
Yan Zhenge4404d62008-12-12 10:03:26 -05002284 old_devices = clone_fs_devices(fs_devices);
2285 if (IS_ERR(old_devices)) {
2286 kfree(seed_devices);
2287 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002288 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002289
Anand Jainc4babc52018-04-12 10:29:25 +08002290 list_add(&old_devices->fs_list, &fs_uuids);
Yan Zheng2b820322008-11-17 21:11:30 -05002291
Yan Zhenge4404d62008-12-12 10:03:26 -05002292 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2293 seed_devices->opened = 1;
2294 INIT_LIST_HEAD(&seed_devices->devices);
2295 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002296 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002297
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002298 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002299 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2300 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002301 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002302 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002303
David Sterba34441362016-10-04 19:34:27 +02002304 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002305 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
David Sterba34441362016-10-04 19:34:27 +02002306 mutex_unlock(&fs_info->chunk_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05002307
Yan Zheng2b820322008-11-17 21:11:30 -05002308 fs_devices->seeding = 0;
2309 fs_devices->num_devices = 0;
2310 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002311 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002312 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002313 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002314
2315 generate_random_uuid(fs_devices->fsid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002316 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05002317 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002318 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002319
Yan Zheng2b820322008-11-17 21:11:30 -05002320 super_flags = btrfs_super_flags(disk_super) &
2321 ~BTRFS_SUPER_FLAG_SEEDING;
2322 btrfs_set_super_flags(disk_super, super_flags);
2323
2324 return 0;
2325}
2326
2327/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002328 * Store the expected generation for seed devices in device items.
Yan Zheng2b820322008-11-17 21:11:30 -05002329 */
2330static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002331 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002332{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002333 struct btrfs_root *root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002334 struct btrfs_path *path;
2335 struct extent_buffer *leaf;
2336 struct btrfs_dev_item *dev_item;
2337 struct btrfs_device *device;
2338 struct btrfs_key key;
Anand Jain44880fd2017-07-29 17:50:09 +08002339 u8 fs_uuid[BTRFS_FSID_SIZE];
Yan Zheng2b820322008-11-17 21:11:30 -05002340 u8 dev_uuid[BTRFS_UUID_SIZE];
2341 u64 devid;
2342 int ret;
2343
2344 path = btrfs_alloc_path();
2345 if (!path)
2346 return -ENOMEM;
2347
Yan Zheng2b820322008-11-17 21:11:30 -05002348 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2349 key.offset = 0;
2350 key.type = BTRFS_DEV_ITEM_KEY;
2351
2352 while (1) {
2353 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2354 if (ret < 0)
2355 goto error;
2356
2357 leaf = path->nodes[0];
2358next_slot:
2359 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2360 ret = btrfs_next_leaf(root, path);
2361 if (ret > 0)
2362 break;
2363 if (ret < 0)
2364 goto error;
2365 leaf = path->nodes[0];
2366 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002367 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002368 continue;
2369 }
2370
2371 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2372 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2373 key.type != BTRFS_DEV_ITEM_KEY)
2374 break;
2375
2376 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2377 struct btrfs_dev_item);
2378 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002379 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002380 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002381 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08002382 BTRFS_FSID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002383 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002384 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002385
2386 if (device->fs_devices->seeding) {
2387 btrfs_set_device_generation(leaf, dev_item,
2388 device->generation);
2389 btrfs_mark_buffer_dirty(leaf);
2390 }
2391
2392 path->slots[0]++;
2393 goto next_slot;
2394 }
2395 ret = 0;
2396error:
2397 btrfs_free_path(path);
2398 return ret;
2399}
2400
David Sterbada353f62017-02-14 17:55:53 +01002401int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
Chris Mason788f20e2008-04-28 15:29:42 -04002402{
Jeff Mahoney5112feb2016-06-21 20:16:08 -04002403 struct btrfs_root *root = fs_info->dev_root;
Josef Bacikd5e20032011-08-04 14:52:27 +00002404 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002405 struct btrfs_trans_handle *trans;
2406 struct btrfs_device *device;
2407 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002408 struct list_head *devices;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 struct super_block *sb = fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002410 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002411 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002412 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002413 int ret = 0;
Anand Jain7132a262017-09-28 14:51:11 +08002414 bool unlocked = false;
Chris Mason788f20e2008-04-28 15:29:42 -04002415
David Howellsbc98a422017-07-17 08:45:34 +01002416 if (sb_rdonly(sb) && !fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002417 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002418
Li Zefana5d16332011-12-07 20:08:40 -05002419 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002420 fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002421 if (IS_ERR(bdev))
2422 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002423
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002424 if (fs_info->fs_devices->seeding) {
Yan Zheng2b820322008-11-17 21:11:30 -05002425 seeding_dev = 1;
2426 down_write(&sb->s_umount);
2427 mutex_lock(&uuid_mutex);
2428 }
2429
Chris Mason8c8bee12008-09-29 11:19:10 -04002430 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002431
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002432 devices = &fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002434 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002435 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002436 if (device->bdev == bdev) {
2437 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002438 mutex_unlock(
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002439 &fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002440 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002441 }
2442 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002443 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002444
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002445 device = btrfs_alloc_device(fs_info, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002446 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002447 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002448 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002449 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002450 }
2451
David Sterba78f2c9e2016-02-11 14:25:38 +01002452 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002453 if (!name) {
Yan Zheng2b820322008-11-17 21:11:30 -05002454 ret = -ENOMEM;
David Sterba5c4cf6c2017-10-30 19:29:46 +01002455 goto error_free_device;
Chris Mason788f20e2008-04-28 15:29:42 -04002456 }
Josef Bacik606686e2012-06-04 14:03:51 -04002457 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002458
Yan, Zhenga22285a2010-05-16 10:48:46 -04002459 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002460 if (IS_ERR(trans)) {
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002461 ret = PTR_ERR(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002462 goto error_free_device;
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002463 }
2464
Josef Bacikd5e20032011-08-04 14:52:27 +00002465 q = bdev_get_queue(bdev);
Anand Jainebbede42017-12-04 12:54:52 +08002466 set_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05002467 device->generation = trans->transid;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002468 device->io_width = fs_info->sectorsize;
2469 device->io_align = fs_info->sectorsize;
2470 device->sector_size = fs_info->sectorsize;
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002471 device->total_bytes = round_down(i_size_read(bdev->bd_inode),
2472 fs_info->sectorsize);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002473 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002474 device->commit_total_bytes = device->total_bytes;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002475 device->fs_info = fs_info;
Chris Mason788f20e2008-04-28 15:29:42 -04002476 device->bdev = bdev;
Anand Jaine12c9622017-12-04 12:54:53 +08002477 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jain401e29c2017-12-04 12:54:55 +08002478 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002479 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002480 device->dev_stats_valid = 1;
David Sterba9f6d2512017-06-16 01:48:05 +02002481 set_blocksize(device->bdev, BTRFS_BDEV_BLOCKSIZE);
Zheng Yan325cd4b2008-09-05 16:43:54 -04002482
Yan Zheng2b820322008-11-17 21:11:30 -05002483 if (seeding_dev) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002484 sb->s_flags &= ~SB_RDONLY;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002485 ret = btrfs_prepare_sprout(fs_info);
Anand Jaind31c32f2017-09-28 14:51:10 +08002486 if (ret) {
2487 btrfs_abort_transaction(trans, ret);
2488 goto error_trans;
2489 }
Yan Zheng2b820322008-11-17 21:11:30 -05002490 }
2491
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002492 device->fs_devices = fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002493
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 mutex_lock(&fs_info->fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02002495 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002496 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002497 list_add(&device->dev_alloc_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002498 &fs_info->fs_devices->alloc_list);
2499 fs_info->fs_devices->num_devices++;
2500 fs_info->fs_devices->open_devices++;
2501 fs_info->fs_devices->rw_devices++;
2502 fs_info->fs_devices->total_devices++;
2503 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05002504
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002505 atomic64_add(device->total_bytes, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04002506
Anand Jaine884f4f2017-04-04 18:40:19 +08002507 if (!blk_queue_nonrot(q))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002508 fs_info->fs_devices->rotating = 1;
Chris Masonc2898112009-06-10 09:51:32 -04002509
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002510 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2511 btrfs_set_super_total_bytes(fs_info->super_copy,
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002512 round_down(tmp + device->total_bytes, fs_info->sectorsize));
Chris Mason788f20e2008-04-28 15:29:42 -04002513
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002514 tmp = btrfs_super_num_devices(fs_info->super_copy);
2515 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002516
2517 /* add sysfs device entry */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002518 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002519
Miao Xie2196d6e2014-09-03 21:35:41 +08002520 /*
2521 * we've got more storage, clear any full flags on the space
2522 * infos
2523 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002524 btrfs_clear_space_info_full(fs_info);
Miao Xie2196d6e2014-09-03 21:35:41 +08002525
David Sterba34441362016-10-04 19:34:27 +02002526 mutex_unlock(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002527 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002528
Yan Zheng2b820322008-11-17 21:11:30 -05002529 if (seeding_dev) {
David Sterba34441362016-10-04 19:34:27 +02002530 mutex_lock(&fs_info->chunk_mutex);
David Sterbae4a4dce2017-02-10 19:49:01 +01002531 ret = init_first_rw_device(trans, fs_info);
David Sterba34441362016-10-04 19:34:27 +02002532 mutex_unlock(&fs_info->chunk_mutex);
David Sterba005d6422012-09-18 07:52:32 -06002533 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002534 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002535 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002536 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002537 }
2538
Anand Jainc74a0b02017-11-06 16:36:15 +08002539 ret = btrfs_add_dev_item(trans, fs_info, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002540 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002541 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002542 goto error_sysfs;
Miao Xie2196d6e2014-09-03 21:35:41 +08002543 }
2544
2545 if (seeding_dev) {
2546 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2547
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002548 ret = btrfs_finish_sprout(trans, fs_info);
David Sterba005d6422012-09-18 07:52:32 -06002549 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002550 btrfs_abort_transaction(trans, ret);
Anand Jaind31c32f2017-09-28 14:51:10 +08002551 goto error_sysfs;
David Sterba005d6422012-09-18 07:52:32 -06002552 }
Anand Jainb2373f22014-06-03 11:36:03 +08002553
2554 /* Sprouting would change fsid of the mounted root,
2555 * so rename the fsid on the sysfs
2556 */
2557 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002558 fs_info->fsid);
2559 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2560 btrfs_warn(fs_info,
2561 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002562 }
2563
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002564 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002565
2566 if (seeding_dev) {
2567 mutex_unlock(&uuid_mutex);
2568 up_write(&sb->s_umount);
Anand Jain7132a262017-09-28 14:51:11 +08002569 unlocked = true;
Yan Zheng2b820322008-11-17 21:11:30 -05002570
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002571 if (ret) /* transaction commit */
2572 return ret;
2573
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002574 ret = btrfs_relocate_sys_chunks(fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002575 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002576 btrfs_handle_fs_error(fs_info, ret,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04002577 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002578 trans = btrfs_attach_transaction(root);
2579 if (IS_ERR(trans)) {
2580 if (PTR_ERR(trans) == -ENOENT)
2581 return 0;
Anand Jain7132a262017-09-28 14:51:11 +08002582 ret = PTR_ERR(trans);
2583 trans = NULL;
2584 goto error_sysfs;
Miao Xie671415b2012-10-16 11:26:46 +00002585 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002586 ret = btrfs_commit_transaction(trans);
Yan Zheng2b820322008-11-17 21:11:30 -05002587 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002588
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002589 /* Update ctime/mtime for libblkid */
2590 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002591 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002592
Anand Jaind31c32f2017-09-28 14:51:10 +08002593error_sysfs:
2594 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002595error_trans:
Anand Jain0af2c4b2017-09-28 14:51:09 +08002596 if (seeding_dev)
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002597 sb->s_flags |= SB_RDONLY;
Anand Jain7132a262017-09-28 14:51:11 +08002598 if (trans)
2599 btrfs_end_transaction(trans);
David Sterba5c4cf6c2017-10-30 19:29:46 +01002600error_free_device:
David Sterbaa425f9d2018-03-20 15:47:33 +01002601 btrfs_free_device(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002602error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002603 blkdev_put(bdev, FMODE_EXCL);
Anand Jain7132a262017-09-28 14:51:11 +08002604 if (seeding_dev && !unlocked) {
Yan Zheng2b820322008-11-17 21:11:30 -05002605 mutex_unlock(&uuid_mutex);
2606 up_write(&sb->s_umount);
2607 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002608 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002609}
2610
Chris Masond3977122009-01-05 21:25:51 -05002611static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2612 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002613{
2614 int ret;
2615 struct btrfs_path *path;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002616 struct btrfs_root *root = device->fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04002617 struct btrfs_dev_item *dev_item;
2618 struct extent_buffer *leaf;
2619 struct btrfs_key key;
2620
Chris Mason0b86a832008-03-24 15:01:56 -04002621 path = btrfs_alloc_path();
2622 if (!path)
2623 return -ENOMEM;
2624
2625 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2626 key.type = BTRFS_DEV_ITEM_KEY;
2627 key.offset = device->devid;
2628
2629 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2630 if (ret < 0)
2631 goto out;
2632
2633 if (ret > 0) {
2634 ret = -ENOENT;
2635 goto out;
2636 }
2637
2638 leaf = path->nodes[0];
2639 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2640
2641 btrfs_set_device_id(leaf, dev_item, device->devid);
2642 btrfs_set_device_type(leaf, dev_item, device->type);
2643 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2644 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2645 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002646 btrfs_set_device_total_bytes(leaf, dev_item,
2647 btrfs_device_get_disk_total_bytes(device));
2648 btrfs_set_device_bytes_used(leaf, dev_item,
2649 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002650 btrfs_mark_buffer_dirty(leaf);
2651
2652out:
2653 btrfs_free_path(path);
2654 return ret;
2655}
2656
Miao Xie2196d6e2014-09-03 21:35:41 +08002657int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002658 struct btrfs_device *device, u64 new_size)
2659{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002660 struct btrfs_fs_info *fs_info = device->fs_info;
2661 struct btrfs_super_block *super_copy = fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002662 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002663 u64 old_total;
2664 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002665
Anand Jainebbede42017-12-04 12:54:52 +08002666 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Yan Zheng2b820322008-11-17 21:11:30 -05002667 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002668
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002669 new_size = round_down(new_size, fs_info->sectorsize);
2670
David Sterba34441362016-10-04 19:34:27 +02002671 mutex_lock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002672 old_total = btrfs_super_total_bytes(super_copy);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03002673 diff = round_down(new_size - device->total_bytes, fs_info->sectorsize);
Miao Xie2196d6e2014-09-03 21:35:41 +08002674
Stefan Behrens63a212a2012-11-05 18:29:28 +01002675 if (new_size <= device->total_bytes ||
Anand Jain401e29c2017-12-04 12:54:55 +08002676 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
David Sterba34441362016-10-04 19:34:27 +02002677 mutex_unlock(&fs_info->chunk_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002678 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002679 }
Yan Zheng2b820322008-11-17 21:11:30 -05002680
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002681 fs_devices = fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002682
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03002683 btrfs_set_super_total_bytes(super_copy,
2684 round_down(old_total + diff, fs_info->sectorsize));
Yan Zheng2b820322008-11-17 21:11:30 -05002685 device->fs_devices->total_rw_bytes += diff;
2686
Miao Xie7cc8e582014-09-03 21:35:38 +08002687 btrfs_device_set_total_bytes(device, new_size);
2688 btrfs_device_set_disk_total_bytes(device, new_size);
Jeff Mahoneyfb456252016-06-22 18:54:56 -04002689 btrfs_clear_space_info_full(device->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002690 if (list_empty(&device->resized_list))
2691 list_add_tail(&device->resized_list,
2692 &fs_devices->resized_devices);
David Sterba34441362016-10-04 19:34:27 +02002693 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason4184ea72009-03-10 12:39:20 -04002694
Chris Mason8f18cf12008-04-25 16:53:30 -04002695 return btrfs_update_device(trans, device);
2696}
2697
2698static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002699 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002700{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002701 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002702 int ret;
2703 struct btrfs_path *path;
2704 struct btrfs_key key;
2705
Chris Mason8f18cf12008-04-25 16:53:30 -04002706 path = btrfs_alloc_path();
2707 if (!path)
2708 return -ENOMEM;
2709
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002710 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Chris Mason8f18cf12008-04-25 16:53:30 -04002711 key.offset = chunk_offset;
2712 key.type = BTRFS_CHUNK_ITEM_KEY;
2713
2714 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002715 if (ret < 0)
2716 goto out;
2717 else if (ret > 0) { /* Logic error or corruption */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002718 btrfs_handle_fs_error(fs_info, -ENOENT,
2719 "Failed lookup while freeing chunk.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720 ret = -ENOENT;
2721 goto out;
2722 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002723
2724 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002725 if (ret < 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002726 btrfs_handle_fs_error(fs_info, ret,
2727 "Failed to delete chunk item.");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002728out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002729 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002730 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002731}
2732
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002733static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002734{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002735 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002736 struct btrfs_disk_key *disk_key;
2737 struct btrfs_chunk *chunk;
2738 u8 *ptr;
2739 int ret = 0;
2740 u32 num_stripes;
2741 u32 array_size;
2742 u32 len = 0;
2743 u32 cur;
2744 struct btrfs_key key;
2745
David Sterba34441362016-10-04 19:34:27 +02002746 mutex_lock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002747 array_size = btrfs_super_sys_array_size(super_copy);
2748
2749 ptr = super_copy->sys_chunk_array;
2750 cur = 0;
2751
2752 while (cur < array_size) {
2753 disk_key = (struct btrfs_disk_key *)ptr;
2754 btrfs_disk_key_to_cpu(&key, disk_key);
2755
2756 len = sizeof(*disk_key);
2757
2758 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2759 chunk = (struct btrfs_chunk *)(ptr + len);
2760 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2761 len += btrfs_chunk_item_size(num_stripes);
2762 } else {
2763 ret = -EIO;
2764 break;
2765 }
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002766 if (key.objectid == BTRFS_FIRST_CHUNK_TREE_OBJECTID &&
Chris Mason8f18cf12008-04-25 16:53:30 -04002767 key.offset == chunk_offset) {
2768 memmove(ptr, ptr + len, array_size - (cur + len));
2769 array_size -= len;
2770 btrfs_set_super_sys_array_size(super_copy, array_size);
2771 } else {
2772 ptr += len;
2773 cur += len;
2774 }
2775 }
David Sterba34441362016-10-04 19:34:27 +02002776 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04002777 return ret;
2778}
2779
Liu Bo592d92e2017-03-14 13:33:55 -07002780static struct extent_map *get_chunk_map(struct btrfs_fs_info *fs_info,
2781 u64 logical, u64 length)
2782{
2783 struct extent_map_tree *em_tree;
2784 struct extent_map *em;
2785
2786 em_tree = &fs_info->mapping_tree.map_tree;
2787 read_lock(&em_tree->lock);
2788 em = lookup_extent_mapping(em_tree, logical, length);
2789 read_unlock(&em_tree->lock);
2790
2791 if (!em) {
2792 btrfs_crit(fs_info, "unable to find logical %llu length %llu",
2793 logical, length);
2794 return ERR_PTR(-EINVAL);
2795 }
2796
2797 if (em->start > logical || em->start + em->len < logical) {
2798 btrfs_crit(fs_info,
2799 "found a bad mapping, wanted %llu-%llu, found %llu-%llu",
2800 logical, length, em->start, em->start + em->len);
2801 free_extent_map(em);
2802 return ERR_PTR(-EINVAL);
2803 }
2804
2805 /* callers are responsible for dropping em's ref. */
2806 return em;
2807}
2808
Josef Bacik47ab2a62014-09-18 11:20:02 -04002809int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002810 struct btrfs_fs_info *fs_info, u64 chunk_offset)
Josef Bacik47ab2a62014-09-18 11:20:02 -04002811{
Josef Bacik47ab2a62014-09-18 11:20:02 -04002812 struct extent_map *em;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002813 struct map_lookup *map;
2814 u64 dev_extent_len = 0;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002815 int i, ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002816 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002817
Liu Bo592d92e2017-03-14 13:33:55 -07002818 em = get_chunk_map(fs_info, chunk_offset, 1);
2819 if (IS_ERR(em)) {
Josef Bacik47ab2a62014-09-18 11:20:02 -04002820 /*
2821 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002822 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002823 * do anything we still error out.
2824 */
2825 ASSERT(0);
Liu Bo592d92e2017-03-14 13:33:55 -07002826 return PTR_ERR(em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002827 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002828 map = em->map_lookup;
David Sterba34441362016-10-04 19:34:27 +02002829 mutex_lock(&fs_info->chunk_mutex);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002830 check_system_chunk(trans, fs_info, map->type);
David Sterba34441362016-10-04 19:34:27 +02002831 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002832
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002833 /*
2834 * Take the device list mutex to prevent races with the final phase of
2835 * a device replace operation that replaces the device object associated
2836 * with map stripes (dev-replace.c:btrfs_dev_replace_finishing()).
2837 */
2838 mutex_lock(&fs_devices->device_list_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002839 for (i = 0; i < map->num_stripes; i++) {
2840 struct btrfs_device *device = map->stripes[i].dev;
2841 ret = btrfs_free_dev_extent(trans, device,
2842 map->stripes[i].physical,
2843 &dev_extent_len);
2844 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002845 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002846 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002847 goto out;
2848 }
2849
2850 if (device->bytes_used > 0) {
David Sterba34441362016-10-04 19:34:27 +02002851 mutex_lock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002852 btrfs_device_set_bytes_used(device,
2853 device->bytes_used - dev_extent_len);
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03002854 atomic64_add(dev_extent_len, &fs_info->free_chunk_space);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002855 btrfs_clear_space_info_full(fs_info);
David Sterba34441362016-10-04 19:34:27 +02002856 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002857 }
2858
2859 if (map->stripes[i].dev) {
2860 ret = btrfs_update_device(trans, map->stripes[i].dev);
2861 if (ret) {
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002862 mutex_unlock(&fs_devices->device_list_mutex);
Jeff Mahoney66642832016-06-10 18:19:25 -04002863 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002864 goto out;
2865 }
2866 }
2867 }
Filipe Manana57ba4cb2016-05-20 04:34:23 +01002868 mutex_unlock(&fs_devices->device_list_mutex);
2869
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002870 ret = btrfs_free_chunk(trans, fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002871 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002872 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002873 goto out;
2874 }
2875
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002876 trace_btrfs_chunk_free(fs_info, map, chunk_offset, em->len);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002877
2878 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
Nikolay Borisov408fbf12017-07-27 14:37:29 +03002879 ret = btrfs_del_sys_chunk(fs_info, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002880 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002881 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002882 goto out;
2883 }
2884 }
2885
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04002886 ret = btrfs_remove_block_group(trans, fs_info, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002887 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002888 btrfs_abort_transaction(trans, ret);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002889 goto out;
2890 }
2891
Josef Bacik47ab2a62014-09-18 11:20:02 -04002892out:
2893 /* once for us */
2894 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002895 return ret;
2896}
2897
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002898static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002899{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002900 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason19c4d2f2016-10-10 13:43:31 -07002901 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002902 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002903
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002904 /*
2905 * Prevent races with automatic removal of unused block groups.
2906 * After we relocate and before we remove the chunk with offset
2907 * chunk_offset, automatic removal of the block group can kick in,
2908 * resulting in a failure when calling btrfs_remove_chunk() below.
2909 *
2910 * Make sure to acquire this mutex before doing a tree search (dev
2911 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2912 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2913 * we release the path used to search the chunk/dev tree and before
2914 * the current task acquires this mutex and calls us.
2915 */
David Sterbaa32bf9a2018-03-16 02:21:22 +01002916 lockdep_assert_held(&fs_info->delete_unused_bgs_mutex);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002917
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002918 ret = btrfs_can_relocate(fs_info, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002919 if (ret)
2920 return -ENOSPC;
2921
Chris Mason8f18cf12008-04-25 16:53:30 -04002922 /* step one, relocate all the extents inside this chunk */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002923 btrfs_scrub_pause(fs_info);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002924 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002925 btrfs_scrub_continue(fs_info);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002926 if (ret)
2927 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002928
Jeff Mahoney75cb3792018-03-20 15:25:26 -04002929 /*
2930 * We add the kobjects here (and after forcing data chunk creation)
2931 * since relocation is the only place we'll create chunks of a new
2932 * type at runtime. The only place where we'll remove the last
2933 * chunk of a type is the call immediately below this one. Even
2934 * so, we're protected against races with the cleaner thread since
2935 * we're covered by the delete_unused_bgs_mutex.
2936 */
2937 btrfs_add_raid_kobjects(fs_info);
2938
Chris Mason19c4d2f2016-10-10 13:43:31 -07002939 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2940 chunk_offset);
2941 if (IS_ERR(trans)) {
2942 ret = PTR_ERR(trans);
2943 btrfs_handle_fs_error(root->fs_info, ret, NULL);
2944 return ret;
2945 }
Naohiro Aota5d8eb6f2016-09-02 16:46:32 +09002946
Chris Mason19c4d2f2016-10-10 13:43:31 -07002947 /*
2948 * step two, delete the device extents and the
2949 * chunk tree entries
2950 */
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04002951 ret = btrfs_remove_chunk(trans, fs_info, chunk_offset);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002952 btrfs_end_transaction(trans);
Chris Mason19c4d2f2016-10-10 13:43:31 -07002953 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002954}
2955
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002956static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05002957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002958 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05002959 struct btrfs_path *path;
2960 struct extent_buffer *leaf;
2961 struct btrfs_chunk *chunk;
2962 struct btrfs_key key;
2963 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002964 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002965 bool retried = false;
2966 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002967 int ret;
2968
2969 path = btrfs_alloc_path();
2970 if (!path)
2971 return -ENOMEM;
2972
Josef Bacikba1bf482009-09-11 16:11:19 -04002973again:
Yan Zheng2b820322008-11-17 21:11:30 -05002974 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2975 key.offset = (u64)-1;
2976 key.type = BTRFS_CHUNK_ITEM_KEY;
2977
2978 while (1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002979 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002980 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002981 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002982 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002983 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002984 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002985 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002986
2987 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2988 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002989 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002990 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002991 if (ret < 0)
2992 goto error;
2993 if (ret > 0)
2994 break;
2995
2996 leaf = path->nodes[0];
2997 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2998
2999 chunk = btrfs_item_ptr(leaf, path->slots[0],
3000 struct btrfs_chunk);
3001 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02003002 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05003003
3004 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003005 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003006 if (ret == -ENOSPC)
3007 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05303008 else
3009 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05003010 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003011 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05003012
3013 if (found_key.offset == 0)
3014 break;
3015 key.offset = found_key.offset - 1;
3016 }
3017 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04003018 if (failed && !retried) {
3019 failed = 0;
3020 retried = true;
3021 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303022 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04003023 ret = -ENOSPC;
3024 }
Yan Zheng2b820322008-11-17 21:11:30 -05003025error:
3026 btrfs_free_path(path);
3027 return ret;
3028}
3029
Liu Boa6f93c72017-11-15 16:28:11 -07003030/*
3031 * return 1 : allocate a data chunk successfully,
3032 * return <0: errors during allocating a data chunk,
3033 * return 0 : no need to allocate a data chunk.
3034 */
3035static int btrfs_may_alloc_data_chunk(struct btrfs_fs_info *fs_info,
3036 u64 chunk_offset)
3037{
3038 struct btrfs_block_group_cache *cache;
3039 u64 bytes_used;
3040 u64 chunk_type;
3041
3042 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3043 ASSERT(cache);
3044 chunk_type = cache->flags;
3045 btrfs_put_block_group(cache);
3046
3047 if (chunk_type & BTRFS_BLOCK_GROUP_DATA) {
3048 spin_lock(&fs_info->data_sinfo->lock);
3049 bytes_used = fs_info->data_sinfo->bytes_used;
3050 spin_unlock(&fs_info->data_sinfo->lock);
3051
3052 if (!bytes_used) {
3053 struct btrfs_trans_handle *trans;
3054 int ret;
3055
3056 trans = btrfs_join_transaction(fs_info->tree_root);
3057 if (IS_ERR(trans))
3058 return PTR_ERR(trans);
3059
3060 ret = btrfs_force_chunk_alloc(trans, fs_info,
3061 BTRFS_BLOCK_GROUP_DATA);
3062 btrfs_end_transaction(trans);
3063 if (ret < 0)
3064 return ret;
3065
Jeff Mahoney75cb3792018-03-20 15:25:26 -04003066 btrfs_add_raid_kobjects(fs_info);
3067
Liu Boa6f93c72017-11-15 16:28:11 -07003068 return 1;
3069 }
3070 }
3071 return 0;
3072}
3073
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003074static int insert_balance_item(struct btrfs_fs_info *fs_info,
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003075 struct btrfs_balance_control *bctl)
3076{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003077 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003078 struct btrfs_trans_handle *trans;
3079 struct btrfs_balance_item *item;
3080 struct btrfs_disk_balance_args disk_bargs;
3081 struct btrfs_path *path;
3082 struct extent_buffer *leaf;
3083 struct btrfs_key key;
3084 int ret, err;
3085
3086 path = btrfs_alloc_path();
3087 if (!path)
3088 return -ENOMEM;
3089
3090 trans = btrfs_start_transaction(root, 0);
3091 if (IS_ERR(trans)) {
3092 btrfs_free_path(path);
3093 return PTR_ERR(trans);
3094 }
3095
3096 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003097 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003098 key.offset = 0;
3099
3100 ret = btrfs_insert_empty_item(trans, root, path, &key,
3101 sizeof(*item));
3102 if (ret)
3103 goto out;
3104
3105 leaf = path->nodes[0];
3106 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3107
David Sterbab159fa22016-11-08 18:09:03 +01003108 memzero_extent_buffer(leaf, (unsigned long)item, sizeof(*item));
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003109
3110 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3111 btrfs_set_balance_data(leaf, item, &disk_bargs);
3112 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3113 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3114 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3115 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3116
3117 btrfs_set_balance_flags(leaf, item, bctl->flags);
3118
3119 btrfs_mark_buffer_dirty(leaf);
3120out:
3121 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003122 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003123 if (err && !ret)
3124 ret = err;
3125 return ret;
3126}
3127
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003128static int del_balance_item(struct btrfs_fs_info *fs_info)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003129{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003130 struct btrfs_root *root = fs_info->tree_root;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003131 struct btrfs_trans_handle *trans;
3132 struct btrfs_path *path;
3133 struct btrfs_key key;
3134 int ret, err;
3135
3136 path = btrfs_alloc_path();
3137 if (!path)
3138 return -ENOMEM;
3139
3140 trans = btrfs_start_transaction(root, 0);
3141 if (IS_ERR(trans)) {
3142 btrfs_free_path(path);
3143 return PTR_ERR(trans);
3144 }
3145
3146 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003147 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003148 key.offset = 0;
3149
3150 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3151 if (ret < 0)
3152 goto out;
3153 if (ret > 0) {
3154 ret = -ENOENT;
3155 goto out;
3156 }
3157
3158 ret = btrfs_del_item(trans, root, path);
3159out:
3160 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003161 err = btrfs_commit_transaction(trans);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003162 if (err && !ret)
3163 ret = err;
3164 return ret;
3165}
3166
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003167/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003168 * This is a heuristic used to reduce the number of chunks balanced on
3169 * resume after balance was interrupted.
3170 */
3171static void update_balance_args(struct btrfs_balance_control *bctl)
3172{
3173 /*
3174 * Turn on soft mode for chunk types that were being converted.
3175 */
3176 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3177 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3178 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3179 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3180 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3181 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3182
3183 /*
3184 * Turn on usage filter if is not already used. The idea is
3185 * that chunks that we have already balanced should be
3186 * reasonably full. Don't do it for chunks that are being
3187 * converted - that will keep us from relocating unconverted
3188 * (albeit full) chunks.
3189 */
3190 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003191 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003192 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3193 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3194 bctl->data.usage = 90;
3195 }
3196 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003197 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003198 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3199 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3200 bctl->sys.usage = 90;
3201 }
3202 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003203 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003204 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3205 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3206 bctl->meta.usage = 90;
3207 }
3208}
3209
3210/*
David Sterba149196a2018-03-20 20:23:09 +01003211 * Clear the balance status in fs_info and delete the balance item from disk.
3212 */
3213static void reset_balance_state(struct btrfs_fs_info *fs_info)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003214{
3215 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
David Sterba149196a2018-03-20 20:23:09 +01003216 int ret;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003217
3218 BUG_ON(!fs_info->balance_ctl);
3219
3220 spin_lock(&fs_info->balance_lock);
3221 fs_info->balance_ctl = NULL;
3222 spin_unlock(&fs_info->balance_lock);
3223
3224 kfree(bctl);
David Sterba149196a2018-03-20 20:23:09 +01003225 ret = del_balance_item(fs_info);
3226 if (ret)
3227 btrfs_handle_fs_error(fs_info, ret, NULL);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003228}
3229
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003230/*
3231 * Balance filters. Return 1 if chunk should be filtered out
3232 * (should not be balanced).
3233 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003234static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003235 struct btrfs_balance_args *bargs)
3236{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003237 chunk_type = chunk_to_extended(chunk_type) &
3238 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003239
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003240 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003241 return 0;
3242
3243 return 1;
3244}
3245
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003246static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003247 struct btrfs_balance_args *bargs)
3248{
3249 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003250 u64 chunk_used;
3251 u64 user_thresh_min;
3252 u64 user_thresh_max;
3253 int ret = 1;
3254
3255 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3256 chunk_used = btrfs_block_group_used(&cache->item);
3257
3258 if (bargs->usage_min == 0)
3259 user_thresh_min = 0;
3260 else
3261 user_thresh_min = div_factor_fine(cache->key.offset,
3262 bargs->usage_min);
3263
3264 if (bargs->usage_max == 0)
3265 user_thresh_max = 1;
3266 else if (bargs->usage_max > 100)
3267 user_thresh_max = cache->key.offset;
3268 else
3269 user_thresh_max = div_factor_fine(cache->key.offset,
3270 bargs->usage_max);
3271
3272 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3273 ret = 0;
3274
3275 btrfs_put_block_group(cache);
3276 return ret;
3277}
3278
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003279static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003280 u64 chunk_offset, struct btrfs_balance_args *bargs)
3281{
3282 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003283 u64 chunk_used, user_thresh;
3284 int ret = 1;
3285
3286 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3287 chunk_used = btrfs_block_group_used(&cache->item);
3288
David Sterbabc309462015-10-20 18:22:13 +02003289 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003290 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003291 else if (bargs->usage > 100)
3292 user_thresh = cache->key.offset;
3293 else
3294 user_thresh = div_factor_fine(cache->key.offset,
3295 bargs->usage);
3296
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003297 if (chunk_used < user_thresh)
3298 ret = 0;
3299
3300 btrfs_put_block_group(cache);
3301 return ret;
3302}
3303
Ilya Dryomov409d4042012-01-16 22:04:47 +02003304static int chunk_devid_filter(struct extent_buffer *leaf,
3305 struct btrfs_chunk *chunk,
3306 struct btrfs_balance_args *bargs)
3307{
3308 struct btrfs_stripe *stripe;
3309 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3310 int i;
3311
3312 for (i = 0; i < num_stripes; i++) {
3313 stripe = btrfs_stripe_nr(chunk, i);
3314 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3315 return 0;
3316 }
3317
3318 return 1;
3319}
3320
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003321/* [pstart, pend) */
3322static int chunk_drange_filter(struct extent_buffer *leaf,
3323 struct btrfs_chunk *chunk,
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003324 struct btrfs_balance_args *bargs)
3325{
3326 struct btrfs_stripe *stripe;
3327 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3328 u64 stripe_offset;
3329 u64 stripe_length;
3330 int factor;
3331 int i;
3332
3333 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3334 return 0;
3335
3336 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003337 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3338 factor = num_stripes / 2;
3339 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3340 factor = num_stripes - 1;
3341 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3342 factor = num_stripes - 2;
3343 } else {
3344 factor = num_stripes;
3345 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003346
3347 for (i = 0; i < num_stripes; i++) {
3348 stripe = btrfs_stripe_nr(chunk, i);
3349 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3350 continue;
3351
3352 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3353 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003354 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003355
3356 if (stripe_offset < bargs->pend &&
3357 stripe_offset + stripe_length > bargs->pstart)
3358 return 0;
3359 }
3360
3361 return 1;
3362}
3363
Ilya Dryomovea671762012-01-16 22:04:48 +02003364/* [vstart, vend) */
3365static int chunk_vrange_filter(struct extent_buffer *leaf,
3366 struct btrfs_chunk *chunk,
3367 u64 chunk_offset,
3368 struct btrfs_balance_args *bargs)
3369{
3370 if (chunk_offset < bargs->vend &&
3371 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3372 /* at least part of the chunk is inside this vrange */
3373 return 0;
3374
3375 return 1;
3376}
3377
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003378static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3379 struct btrfs_chunk *chunk,
3380 struct btrfs_balance_args *bargs)
3381{
3382 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3383
3384 if (bargs->stripes_min <= num_stripes
3385 && num_stripes <= bargs->stripes_max)
3386 return 0;
3387
3388 return 1;
3389}
3390
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003391static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003392 struct btrfs_balance_args *bargs)
3393{
3394 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3395 return 0;
3396
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003397 chunk_type = chunk_to_extended(chunk_type) &
3398 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003399
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003400 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003401 return 1;
3402
3403 return 0;
3404}
3405
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003406static int should_balance_chunk(struct btrfs_fs_info *fs_info,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003407 struct extent_buffer *leaf,
3408 struct btrfs_chunk *chunk, u64 chunk_offset)
3409{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003410 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003411 struct btrfs_balance_args *bargs = NULL;
3412 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3413
3414 /* type filter */
3415 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3416 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3417 return 0;
3418 }
3419
3420 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3421 bargs = &bctl->data;
3422 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3423 bargs = &bctl->sys;
3424 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3425 bargs = &bctl->meta;
3426
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003427 /* profiles filter */
3428 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3429 chunk_profiles_filter(chunk_type, bargs)) {
3430 return 0;
3431 }
3432
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003433 /* usage filter */
3434 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003436 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003437 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003438 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
David Sterbabc309462015-10-20 18:22:13 +02003439 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003440 }
3441
Ilya Dryomov409d4042012-01-16 22:04:47 +02003442 /* devid filter */
3443 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3444 chunk_devid_filter(leaf, chunk, bargs)) {
3445 return 0;
3446 }
3447
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003448 /* drange filter, makes sense only with devid filter */
3449 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03003450 chunk_drange_filter(leaf, chunk, bargs)) {
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003451 return 0;
3452 }
3453
Ilya Dryomovea671762012-01-16 22:04:48 +02003454 /* vrange filter */
3455 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3456 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3457 return 0;
3458 }
3459
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003460 /* stripes filter */
3461 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3462 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3463 return 0;
3464 }
3465
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003466 /* soft profile changing mode */
3467 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3468 chunk_soft_convert_filter(chunk_type, bargs)) {
3469 return 0;
3470 }
3471
David Sterba7d824b62014-05-07 17:37:51 +02003472 /*
3473 * limited by count, must be the last filter
3474 */
3475 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3476 if (bargs->limit == 0)
3477 return 0;
3478 else
3479 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003480 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3481 /*
3482 * Same logic as the 'limit' filter; the minimum cannot be
Nicholas D Steeves01327612016-05-19 21:18:45 -04003483 * determined here because we do not have the global information
David Sterba12907fc2015-10-10 17:16:50 +02003484 * about the count of all chunks that satisfy the filters.
3485 */
3486 if (bargs->limit_max == 0)
3487 return 0;
3488 else
3489 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003490 }
3491
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003492 return 1;
3493}
3494
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003495static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003496{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003497 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003498 struct btrfs_root *chunk_root = fs_info->chunk_root;
3499 struct btrfs_root *dev_root = fs_info->dev_root;
3500 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003501 struct btrfs_device *device;
3502 u64 old_size;
3503 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003504 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003505 struct btrfs_chunk *chunk;
Liu Bo5a488b92016-07-12 11:24:21 -07003506 struct btrfs_path *path = NULL;
Chris Masonec44a352008-04-28 15:29:52 -04003507 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003508 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003509 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003510 struct extent_buffer *leaf;
3511 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003512 int ret;
3513 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003514 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003515 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003516 u64 limit_data = bctl->data.limit;
3517 u64 limit_meta = bctl->meta.limit;
3518 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003519 u32 count_data = 0;
3520 u32 count_meta = 0;
3521 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003522 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003523
Chris Masonec44a352008-04-28 15:29:52 -04003524 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003525 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003526 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003527 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003528 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003529 size_to_free = min_t(u64, size_to_free, SZ_1M);
Anand Jainebbede42017-12-04 12:54:52 +08003530 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003531 btrfs_device_get_total_bytes(device) -
3532 btrfs_device_get_bytes_used(device) > size_to_free ||
Anand Jain401e29c2017-12-04 12:54:55 +08003533 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Chris Masonec44a352008-04-28 15:29:52 -04003534 continue;
3535
3536 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003537 if (ret == -ENOSPC)
3538 break;
Liu Bo5a488b92016-07-12 11:24:21 -07003539 if (ret) {
3540 /* btrfs_shrink_device never returns ret > 0 */
3541 WARN_ON(ret > 0);
3542 goto error;
3543 }
Chris Masonec44a352008-04-28 15:29:52 -04003544
Yan, Zhenga22285a2010-05-16 10:48:46 -04003545 trans = btrfs_start_transaction(dev_root, 0);
Liu Bo5a488b92016-07-12 11:24:21 -07003546 if (IS_ERR(trans)) {
3547 ret = PTR_ERR(trans);
3548 btrfs_info_in_rcu(fs_info,
3549 "resize: unable to start transaction after shrinking device %s (error %d), old size %llu, new size %llu",
3550 rcu_str_deref(device->name), ret,
3551 old_size, old_size - size_to_free);
3552 goto error;
3553 }
Chris Masonec44a352008-04-28 15:29:52 -04003554
3555 ret = btrfs_grow_device(trans, device, old_size);
Liu Bo5a488b92016-07-12 11:24:21 -07003556 if (ret) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003557 btrfs_end_transaction(trans);
Liu Bo5a488b92016-07-12 11:24:21 -07003558 /* btrfs_grow_device never returns ret > 0 */
3559 WARN_ON(ret > 0);
3560 btrfs_info_in_rcu(fs_info,
3561 "resize: unable to grow device after shrinking device %s (error %d), old size %llu, new size %llu",
3562 rcu_str_deref(device->name), ret,
3563 old_size, old_size - size_to_free);
3564 goto error;
3565 }
Chris Masonec44a352008-04-28 15:29:52 -04003566
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003567 btrfs_end_transaction(trans);
Chris Masonec44a352008-04-28 15:29:52 -04003568 }
3569
3570 /* step two, relocate all the chunks */
3571 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003572 if (!path) {
3573 ret = -ENOMEM;
3574 goto error;
3575 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003576
3577 /* zero out stat counters */
3578 spin_lock(&fs_info->balance_lock);
3579 memset(&bctl->stat, 0, sizeof(bctl->stat));
3580 spin_unlock(&fs_info->balance_lock);
3581again:
David Sterba7d824b62014-05-07 17:37:51 +02003582 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003583 /*
3584 * The single value limit and min/max limits use the same bytes
3585 * in the
3586 */
David Sterba7d824b62014-05-07 17:37:51 +02003587 bctl->data.limit = limit_data;
3588 bctl->meta.limit = limit_meta;
3589 bctl->sys.limit = limit_sys;
3590 }
Chris Masonec44a352008-04-28 15:29:52 -04003591 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3592 key.offset = (u64)-1;
3593 key.type = BTRFS_CHUNK_ITEM_KEY;
3594
Chris Masond3977122009-01-05 21:25:51 -05003595 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003596 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003597 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003598 ret = -ECANCELED;
3599 goto error;
3600 }
3601
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003602 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003603 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003604 if (ret < 0) {
3605 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003606 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003607 }
Chris Masonec44a352008-04-28 15:29:52 -04003608
3609 /*
3610 * this shouldn't happen, it means the last relocate
3611 * failed
3612 */
3613 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003614 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003615
3616 ret = btrfs_previous_item(chunk_root, path, 0,
3617 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003618 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003619 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003620 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003621 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003622 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003623
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003624 leaf = path->nodes[0];
3625 slot = path->slots[0];
3626 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3627
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003628 if (found_key.objectid != key.objectid) {
3629 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003630 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003631 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003632
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003633 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003634 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003635
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003636 if (!counting) {
3637 spin_lock(&fs_info->balance_lock);
3638 bctl->stat.considered++;
3639 spin_unlock(&fs_info->balance_lock);
3640 }
3641
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003642 ret = should_balance_chunk(fs_info, leaf, chunk,
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003643 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003644
David Sterbab3b4aa72011-04-21 01:20:15 +02003645 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003646 if (!ret) {
3647 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003648 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003649 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003650
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003651 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003652 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003653 spin_lock(&fs_info->balance_lock);
3654 bctl->stat.expected++;
3655 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003656
3657 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3658 count_data++;
3659 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3660 count_sys++;
3661 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3662 count_meta++;
3663
3664 goto loop;
3665 }
3666
3667 /*
3668 * Apply limit_min filter, no need to check if the LIMITS
3669 * filter is used, limit_min is 0 by default
3670 */
3671 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3672 count_data < bctl->data.limit_min)
3673 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3674 count_meta < bctl->meta.limit_min)
3675 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3676 count_sys < bctl->sys.limit_min)) {
3677 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003678 goto loop;
3679 }
3680
Liu Boa6f93c72017-11-15 16:28:11 -07003681 if (!chunk_reserved) {
3682 /*
3683 * We may be relocating the only data chunk we have,
3684 * which could potentially end up with losing data's
3685 * raid profile, so lets allocate an empty one in
3686 * advance.
3687 */
3688 ret = btrfs_may_alloc_data_chunk(fs_info,
3689 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003690 if (ret < 0) {
3691 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3692 goto error;
Liu Boa6f93c72017-11-15 16:28:11 -07003693 } else if (ret == 1) {
3694 chunk_reserved = 1;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003695 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003696 }
3697
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04003698 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003699 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003700 if (ret && ret != -ENOSPC)
3701 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003702 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003703 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003704 } else {
3705 spin_lock(&fs_info->balance_lock);
3706 bctl->stat.completed++;
3707 spin_unlock(&fs_info->balance_lock);
3708 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003709loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003710 if (found_key.offset == 0)
3711 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003712 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003713 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003714
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003715 if (counting) {
3716 btrfs_release_path(path);
3717 counting = false;
3718 goto again;
3719 }
Chris Masonec44a352008-04-28 15:29:52 -04003720error:
3721 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003722 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003723 btrfs_info(fs_info, "%d enospc errors during balance",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003724 enospc_errors);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003725 if (!ret)
3726 ret = -ENOSPC;
3727 }
3728
Chris Masonec44a352008-04-28 15:29:52 -04003729 return ret;
3730}
3731
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003732/**
3733 * alloc_profile_is_valid - see if a given profile is valid and reduced
3734 * @flags: profile to validate
3735 * @extended: if true @flags is treated as an extended profile
3736 */
3737static int alloc_profile_is_valid(u64 flags, int extended)
3738{
3739 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3740 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3741
3742 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3743
3744 /* 1) check that all other bits are zeroed */
3745 if (flags & ~mask)
3746 return 0;
3747
3748 /* 2) see if profile is reduced */
3749 if (flags == 0)
3750 return !extended; /* "0" is valid for usual profiles */
3751
3752 /* true if exactly one bit set */
3753 return (flags & (flags - 1)) == 0;
3754}
3755
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003756static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3757{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003758 /* cancel requested || normal exit path */
3759 return atomic_read(&fs_info->balance_cancel_req) ||
3760 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3761 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003762}
3763
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003764/* Non-zero return value signifies invalidity */
3765static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3766 u64 allowed)
3767{
3768 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3769 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3770 (bctl_arg->target & ~allowed)));
3771}
3772
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003773/*
David Sterbadccdb072018-03-21 00:20:05 +01003774 * Should be called with balance mutexe held
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003775 */
David Sterba6fcf6e22018-05-07 17:44:03 +02003776int btrfs_balance(struct btrfs_fs_info *fs_info,
3777 struct btrfs_balance_control *bctl,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003778 struct btrfs_ioctl_balance_args *bargs)
3779{
Adam Borowski14506122017-03-07 23:34:44 +01003780 u64 meta_target, data_target;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003781 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003782 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003783 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003784 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003785 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003786
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003787 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003788 atomic_read(&fs_info->balance_pause_req) ||
3789 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003790 ret = -EINVAL;
3791 goto out;
3792 }
3793
Ilya Dryomove4837f82012-03-27 17:09:17 +03003794 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3795 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3796 mixed = 1;
3797
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003798 /*
3799 * In case of mixed groups both data and meta should be picked,
3800 * and identical options should be given for both of them.
3801 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003802 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3803 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003804 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3805 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3806 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003807 btrfs_err(fs_info,
3808 "with mixed groups data and metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003809 ret = -EINVAL;
3810 goto out;
3811 }
3812 }
3813
Stefan Behrens8dabb742012-11-06 13:15:27 +01003814 num_devices = fs_info->fs_devices->num_devices;
David Sterba7e79cb82018-03-24 02:11:38 +01003815 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003816 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3817 BUG_ON(num_devices < 1);
3818 num_devices--;
3819 }
David Sterba7e79cb82018-03-24 02:11:38 +01003820 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003821 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3822 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003823 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003824 if (num_devices > 2)
3825 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3826 if (num_devices > 3)
3827 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3828 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003829 if (validate_convert_profile(&bctl->data, allowed)) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003830 btrfs_err(fs_info,
3831 "unable to start balance with target data profile %llu",
3832 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003833 ret = -EINVAL;
3834 goto out;
3835 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003836 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003837 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003838 "unable to start balance with target metadata profile %llu",
3839 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003840 ret = -EINVAL;
3841 goto out;
3842 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003843 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003844 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003845 "unable to start balance with target system profile %llu",
3846 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003847 ret = -EINVAL;
3848 goto out;
3849 }
3850
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003851 /* allow to reduce meta or sys integrity only if force set */
3852 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003853 BTRFS_BLOCK_GROUP_RAID10 |
3854 BTRFS_BLOCK_GROUP_RAID5 |
3855 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003856 do {
3857 seq = read_seqbegin(&fs_info->profiles_lock);
3858
3859 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3860 (fs_info->avail_system_alloc_bits & allowed) &&
3861 !(bctl->sys.target & allowed)) ||
3862 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3863 (fs_info->avail_metadata_alloc_bits & allowed) &&
3864 !(bctl->meta.target & allowed))) {
3865 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003866 btrfs_info(fs_info,
3867 "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003868 } else {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003869 btrfs_err(fs_info,
3870 "balance will reduce metadata integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003871 ret = -EINVAL;
3872 goto out;
3873 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003874 }
Miao Xiede98ced2013-01-29 10:13:12 +00003875 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003876
Adam Borowski14506122017-03-07 23:34:44 +01003877 /* if we're not converting, the target field is uninitialized */
3878 meta_target = (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3879 bctl->meta.target : fs_info->avail_metadata_alloc_bits;
3880 data_target = (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) ?
3881 bctl->data.target : fs_info->avail_data_alloc_bits;
3882 if (btrfs_get_num_tolerated_disk_barrier_failures(meta_target) <
3883 btrfs_get_num_tolerated_disk_barrier_failures(data_target)) {
Sam Tygieree592d02016-01-06 08:46:12 +00003884 btrfs_warn(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003885 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Adam Borowski14506122017-03-07 23:34:44 +01003886 meta_target, data_target);
Sam Tygieree592d02016-01-06 08:46:12 +00003887 }
3888
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04003889 ret = insert_balance_item(fs_info, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003890 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003891 goto out;
3892
Ilya Dryomov59641012012-01-16 22:04:48 +02003893 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3894 BUG_ON(ret == -EEXIST);
David Sterba833aae12018-03-21 02:41:30 +01003895 BUG_ON(fs_info->balance_ctl);
3896 spin_lock(&fs_info->balance_lock);
3897 fs_info->balance_ctl = bctl;
3898 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov59641012012-01-16 22:04:48 +02003899 } else {
3900 BUG_ON(ret != -EEXIST);
3901 spin_lock(&fs_info->balance_lock);
3902 update_balance_args(bctl);
3903 spin_unlock(&fs_info->balance_lock);
3904 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003905
David Sterba3009a622018-03-21 01:31:04 +01003906 ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
3907 set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003908 mutex_unlock(&fs_info->balance_mutex);
3909
3910 ret = __btrfs_balance(fs_info);
3911
3912 mutex_lock(&fs_info->balance_mutex);
David Sterba3009a622018-03-21 01:31:04 +01003913 clear_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003914
3915 if (bargs) {
3916 memset(bargs, 0, sizeof(*bargs));
David Sterba008ef092018-03-21 02:05:27 +01003917 btrfs_update_ioctl_balance_args(fs_info, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003918 }
3919
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003920 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3921 balance_need_close(fs_info)) {
David Sterba149196a2018-03-20 20:23:09 +01003922 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003923 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003924 }
3925
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003926 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003927
3928 return ret;
3929out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003930 if (bctl->flags & BTRFS_BALANCE_RESUME)
David Sterba149196a2018-03-20 20:23:09 +01003931 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01003932 else
Ilya Dryomov59641012012-01-16 22:04:48 +02003933 kfree(bctl);
David Sterbaa17c95d2018-03-20 17:28:05 +01003934 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
3935
Ilya Dryomov59641012012-01-16 22:04:48 +02003936 return ret;
3937}
3938
3939static int balance_kthread(void *data)
3940{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003941 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003942 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003943
Ilya Dryomov59641012012-01-16 22:04:48 +02003944 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003945 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003946 btrfs_info(fs_info, "continuing balance");
David Sterba6fcf6e22018-05-07 17:44:03 +02003947 ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003948 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003949 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003950
Ilya Dryomov59641012012-01-16 22:04:48 +02003951 return ret;
3952}
3953
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003954int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3955{
3956 struct task_struct *tsk;
3957
David Sterba1354e1a2018-03-21 02:29:13 +01003958 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003959 if (!fs_info->balance_ctl) {
David Sterba1354e1a2018-03-21 02:29:13 +01003960 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003961 return 0;
3962 }
David Sterba1354e1a2018-03-21 02:29:13 +01003963 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003964
Jeff Mahoney3cdde222016-06-09 21:38:35 -04003965 if (btrfs_test_opt(fs_info, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003966 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003967 return 0;
3968 }
3969
Anand Jain02ee6542018-05-17 15:16:51 +08003970 /*
3971 * A ro->rw remount sequence should continue with the paused balance
3972 * regardless of who pauses it, system or the user as of now, so set
3973 * the resume flag.
3974 */
3975 spin_lock(&fs_info->balance_lock);
3976 fs_info->balance_ctl->flags |= BTRFS_BALANCE_RESUME;
3977 spin_unlock(&fs_info->balance_lock);
3978
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003979 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303980 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003981}
3982
Ilya Dryomov68310a52012-06-22 12:24:12 -06003983int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003984{
Ilya Dryomov59641012012-01-16 22:04:48 +02003985 struct btrfs_balance_control *bctl;
3986 struct btrfs_balance_item *item;
3987 struct btrfs_disk_balance_args disk_bargs;
3988 struct btrfs_path *path;
3989 struct extent_buffer *leaf;
3990 struct btrfs_key key;
3991 int ret;
3992
3993 path = btrfs_alloc_path();
3994 if (!path)
3995 return -ENOMEM;
3996
Ilya Dryomov68310a52012-06-22 12:24:12 -06003997 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003998 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003999 key.offset = 0;
4000
4001 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4002 if (ret < 0)
4003 goto out;
4004 if (ret > 0) { /* ret = -ENOENT; */
4005 ret = 0;
4006 goto out;
4007 }
4008
Ilya Dryomov59641012012-01-16 22:04:48 +02004009 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
4010 if (!bctl) {
4011 ret = -ENOMEM;
4012 goto out;
4013 }
4014
Ilya Dryomov59641012012-01-16 22:04:48 +02004015 leaf = path->nodes[0];
4016 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
4017
Ilya Dryomov68310a52012-06-22 12:24:12 -06004018 bctl->flags = btrfs_balance_flags(leaf, item);
4019 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02004020
4021 btrfs_balance_data(leaf, item, &disk_bargs);
4022 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
4023 btrfs_balance_meta(leaf, item, &disk_bargs);
4024 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
4025 btrfs_balance_sys(leaf, item, &disk_bargs);
4026 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
4027
David Sterbaeee95e32018-03-20 20:07:58 +01004028 /*
4029 * This should never happen, as the paused balance state is recovered
4030 * during mount without any chance of other exclusive ops to collide.
4031 *
4032 * This gives the exclusive op status to balance and keeps in paused
4033 * state until user intervention (cancel or umount). If the ownership
4034 * cannot be assigned, show a message but do not fail. The balance
4035 * is in a paused state and must have fs_info::balance_ctl properly
4036 * set up.
4037 */
4038 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags))
4039 btrfs_warn(fs_info,
4040 "cannot set exclusive op status to balance, resume manually");
Ilya Dryomoved0fb782013-01-20 15:57:57 +02004041
Ilya Dryomov68310a52012-06-22 12:24:12 -06004042 mutex_lock(&fs_info->balance_mutex);
David Sterba833aae12018-03-21 02:41:30 +01004043 BUG_ON(fs_info->balance_ctl);
4044 spin_lock(&fs_info->balance_lock);
4045 fs_info->balance_ctl = bctl;
4046 spin_unlock(&fs_info->balance_lock);
Ilya Dryomov68310a52012-06-22 12:24:12 -06004047 mutex_unlock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02004048out:
4049 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004050 return ret;
4051}
4052
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004053int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
4054{
4055 int ret = 0;
4056
4057 mutex_lock(&fs_info->balance_mutex);
4058 if (!fs_info->balance_ctl) {
4059 mutex_unlock(&fs_info->balance_mutex);
4060 return -ENOTCONN;
4061 }
4062
David Sterba3009a622018-03-21 01:31:04 +01004063 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004064 atomic_inc(&fs_info->balance_pause_req);
4065 mutex_unlock(&fs_info->balance_mutex);
4066
4067 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004068 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004069
4070 mutex_lock(&fs_info->balance_mutex);
4071 /* we are good with balance_ctl ripped off from under us */
David Sterba3009a622018-03-21 01:31:04 +01004072 BUG_ON(test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomov837d5b62012-01-16 22:04:49 +02004073 atomic_dec(&fs_info->balance_pause_req);
4074 } else {
4075 ret = -ENOTCONN;
4076 }
4077
4078 mutex_unlock(&fs_info->balance_mutex);
4079 return ret;
4080}
4081
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004082int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
4083{
4084 mutex_lock(&fs_info->balance_mutex);
4085 if (!fs_info->balance_ctl) {
4086 mutex_unlock(&fs_info->balance_mutex);
4087 return -ENOTCONN;
4088 }
4089
David Sterbacf7d20f2018-03-21 01:45:32 +01004090 /*
4091 * A paused balance with the item stored on disk can be resumed at
4092 * mount time if the mount is read-write. Otherwise it's still paused
4093 * and we must not allow cancelling as it deletes the item.
4094 */
4095 if (sb_rdonly(fs_info->sb)) {
4096 mutex_unlock(&fs_info->balance_mutex);
4097 return -EROFS;
4098 }
4099
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004100 atomic_inc(&fs_info->balance_cancel_req);
4101 /*
4102 * if we are running just wait and return, balance item is
4103 * deleted in btrfs_balance in this case
4104 */
David Sterba3009a622018-03-21 01:31:04 +01004105 if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004106 mutex_unlock(&fs_info->balance_mutex);
4107 wait_event(fs_info->balance_wait_q,
David Sterba3009a622018-03-21 01:31:04 +01004108 !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004109 mutex_lock(&fs_info->balance_mutex);
4110 } else {
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004111 mutex_unlock(&fs_info->balance_mutex);
David Sterbadccdb072018-03-21 00:20:05 +01004112 /*
4113 * Lock released to allow other waiters to continue, we'll
4114 * reexamine the status again.
4115 */
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004116 mutex_lock(&fs_info->balance_mutex);
4117
David Sterbaa17c95d2018-03-20 17:28:05 +01004118 if (fs_info->balance_ctl) {
David Sterba149196a2018-03-20 20:23:09 +01004119 reset_balance_state(fs_info);
David Sterbaa17c95d2018-03-20 17:28:05 +01004120 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
4121 }
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004122 }
4123
David Sterba3009a622018-03-21 01:31:04 +01004124 BUG_ON(fs_info->balance_ctl ||
4125 test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
Ilya Dryomova7e99c62012-01-16 22:04:49 +02004126 atomic_dec(&fs_info->balance_cancel_req);
4127 mutex_unlock(&fs_info->balance_mutex);
4128 return 0;
4129}
4130
Stefan Behrens803b2f52013-08-15 17:11:21 +02004131static int btrfs_uuid_scan_kthread(void *data)
4132{
4133 struct btrfs_fs_info *fs_info = data;
4134 struct btrfs_root *root = fs_info->tree_root;
4135 struct btrfs_key key;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004136 struct btrfs_path *path = NULL;
4137 int ret = 0;
4138 struct extent_buffer *eb;
4139 int slot;
4140 struct btrfs_root_item root_item;
4141 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004142 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004143
4144 path = btrfs_alloc_path();
4145 if (!path) {
4146 ret = -ENOMEM;
4147 goto out;
4148 }
4149
4150 key.objectid = 0;
4151 key.type = BTRFS_ROOT_ITEM_KEY;
4152 key.offset = 0;
4153
Stefan Behrens803b2f52013-08-15 17:11:21 +02004154 while (1) {
Anand Jain7c829b72018-03-07 17:29:18 +08004155 ret = btrfs_search_forward(root, &key, path,
4156 BTRFS_OLDEST_GENERATION);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004157 if (ret) {
4158 if (ret > 0)
4159 ret = 0;
4160 break;
4161 }
4162
4163 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4164 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4165 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4166 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4167 goto skip;
4168
4169 eb = path->nodes[0];
4170 slot = path->slots[0];
4171 item_size = btrfs_item_size_nr(eb, slot);
4172 if (item_size < sizeof(root_item))
4173 goto skip;
4174
Stefan Behrens803b2f52013-08-15 17:11:21 +02004175 read_extent_buffer(eb, &root_item,
4176 btrfs_item_ptr_offset(eb, slot),
4177 (int)sizeof(root_item));
4178 if (btrfs_root_refs(&root_item) == 0)
4179 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004180
4181 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4182 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4183 if (trans)
4184 goto update_tree;
4185
4186 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004187 /*
4188 * 1 - subvol uuid item
4189 * 1 - received_subvol uuid item
4190 */
4191 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4192 if (IS_ERR(trans)) {
4193 ret = PTR_ERR(trans);
4194 break;
4195 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004196 continue;
4197 } else {
4198 goto skip;
4199 }
4200update_tree:
4201 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004202 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004203 root_item.uuid,
4204 BTRFS_UUID_KEY_SUBVOL,
4205 key.objectid);
4206 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004207 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004208 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004209 break;
4210 }
4211 }
4212
4213 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004214 ret = btrfs_uuid_tree_add(trans, fs_info,
Stefan Behrens803b2f52013-08-15 17:11:21 +02004215 root_item.received_uuid,
4216 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4217 key.objectid);
4218 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004219 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004220 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004221 break;
4222 }
4223 }
4224
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004225skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004226 if (trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004227 ret = btrfs_end_transaction(trans);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004228 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004229 if (ret)
4230 break;
4231 }
4232
Stefan Behrens803b2f52013-08-15 17:11:21 +02004233 btrfs_release_path(path);
4234 if (key.offset < (u64)-1) {
4235 key.offset++;
4236 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4237 key.offset = 0;
4238 key.type = BTRFS_ROOT_ITEM_KEY;
4239 } else if (key.objectid < (u64)-1) {
4240 key.offset = 0;
4241 key.type = BTRFS_ROOT_ITEM_KEY;
4242 key.objectid++;
4243 } else {
4244 break;
4245 }
4246 cond_resched();
4247 }
4248
4249out:
4250 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004251 if (trans && !IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004252 btrfs_end_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004253 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004254 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004255 else
Josef Bacikafcdd122016-09-02 15:40:02 -04004256 set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004257 up(&fs_info->uuid_tree_rescan_sem);
4258 return 0;
4259}
4260
Stefan Behrens70f80172013-08-15 17:11:23 +02004261/*
4262 * Callback for btrfs_uuid_tree_iterate().
4263 * returns:
4264 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004265 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004266 * > 0 if the check failed, which means the caller shall remove the entry.
4267 */
4268static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4269 u8 *uuid, u8 type, u64 subid)
4270{
4271 struct btrfs_key key;
4272 int ret = 0;
4273 struct btrfs_root *subvol_root;
4274
4275 if (type != BTRFS_UUID_KEY_SUBVOL &&
4276 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4277 goto out;
4278
4279 key.objectid = subid;
4280 key.type = BTRFS_ROOT_ITEM_KEY;
4281 key.offset = (u64)-1;
4282 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4283 if (IS_ERR(subvol_root)) {
4284 ret = PTR_ERR(subvol_root);
4285 if (ret == -ENOENT)
4286 ret = 1;
4287 goto out;
4288 }
4289
4290 switch (type) {
4291 case BTRFS_UUID_KEY_SUBVOL:
4292 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4293 ret = 1;
4294 break;
4295 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4296 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4297 BTRFS_UUID_SIZE))
4298 ret = 1;
4299 break;
4300 }
4301
4302out:
4303 return ret;
4304}
4305
4306static int btrfs_uuid_rescan_kthread(void *data)
4307{
4308 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4309 int ret;
4310
4311 /*
4312 * 1st step is to iterate through the existing UUID tree and
4313 * to delete all entries that contain outdated data.
4314 * 2nd step is to add all missing entries to the UUID tree.
4315 */
4316 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4317 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004318 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004319 up(&fs_info->uuid_tree_rescan_sem);
4320 return ret;
4321 }
4322 return btrfs_uuid_scan_kthread(data);
4323}
4324
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004325int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4326{
4327 struct btrfs_trans_handle *trans;
4328 struct btrfs_root *tree_root = fs_info->tree_root;
4329 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004330 struct task_struct *task;
4331 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004332
4333 /*
4334 * 1 - root node
4335 * 1 - root item
4336 */
4337 trans = btrfs_start_transaction(tree_root, 2);
4338 if (IS_ERR(trans))
4339 return PTR_ERR(trans);
4340
4341 uuid_root = btrfs_create_tree(trans, fs_info,
4342 BTRFS_UUID_TREE_OBJECTID);
4343 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004344 ret = PTR_ERR(uuid_root);
Jeff Mahoney66642832016-06-10 18:19:25 -04004345 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004346 btrfs_end_transaction(trans);
David Sterba6d13f542015-04-24 19:12:01 +02004347 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004348 }
4349
4350 fs_info->uuid_root = uuid_root;
4351
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004352 ret = btrfs_commit_transaction(trans);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004353 if (ret)
4354 return ret;
4355
4356 down(&fs_info->uuid_tree_rescan_sem);
4357 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4358 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004359 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004360 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004361 up(&fs_info->uuid_tree_rescan_sem);
4362 return PTR_ERR(task);
4363 }
4364
4365 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004366}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004367
Stefan Behrens70f80172013-08-15 17:11:23 +02004368int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4369{
4370 struct task_struct *task;
4371
4372 down(&fs_info->uuid_tree_rescan_sem);
4373 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4374 if (IS_ERR(task)) {
4375 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004376 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004377 up(&fs_info->uuid_tree_rescan_sem);
4378 return PTR_ERR(task);
4379 }
4380
4381 return 0;
4382}
4383
Chris Mason8f18cf12008-04-25 16:53:30 -04004384/*
4385 * shrinking a device means finding all of the device extents past
4386 * the new size, and then following the back refs to the chunks.
4387 * The chunk relocation code actually frees the device extent
4388 */
4389int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4390{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004391 struct btrfs_fs_info *fs_info = device->fs_info;
4392 struct btrfs_root *root = fs_info->dev_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04004393 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04004394 struct btrfs_dev_extent *dev_extent = NULL;
4395 struct btrfs_path *path;
4396 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004397 u64 chunk_offset;
4398 int ret;
4399 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004400 int failed = 0;
4401 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004402 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004403 struct extent_buffer *l;
4404 struct btrfs_key key;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004405 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004406 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004407 u64 old_size = btrfs_device_get_total_bytes(device);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004408 u64 diff;
4409
4410 new_size = round_down(new_size, fs_info->sectorsize);
Nikolay Borisov0e4324a2017-07-21 11:28:24 +03004411 diff = round_down(old_size - new_size, fs_info->sectorsize);
Chris Mason8f18cf12008-04-25 16:53:30 -04004412
Anand Jain401e29c2017-12-04 12:54:55 +08004413 if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Stefan Behrens63a212a2012-11-05 18:29:28 +01004414 return -EINVAL;
4415
Chris Mason8f18cf12008-04-25 16:53:30 -04004416 path = btrfs_alloc_path();
4417 if (!path)
4418 return -ENOMEM;
4419
Gu Jinxiang0338dff2018-04-27 16:22:07 +08004420 path->reada = READA_BACK;
Chris Mason8f18cf12008-04-25 16:53:30 -04004421
David Sterba34441362016-10-04 19:34:27 +02004422 mutex_lock(&fs_info->chunk_mutex);
Chris Mason7d9eb122008-07-08 14:19:17 -04004423
Miao Xie7cc8e582014-09-03 21:35:38 +08004424 btrfs_device_set_total_bytes(device, new_size);
Anand Jainebbede42017-12-04 12:54:52 +08004425 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05004426 device->fs_devices->total_rw_bytes -= diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004427 atomic64_sub(diff, &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04004428 }
David Sterba34441362016-10-04 19:34:27 +02004429 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004430
Josef Bacikba1bf482009-09-11 16:11:19 -04004431again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004432 key.objectid = device->devid;
4433 key.offset = (u64)-1;
4434 key.type = BTRFS_DEV_EXTENT_KEY;
4435
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004436 do {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004438 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004439 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004440 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004441 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004442 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004443
4444 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004445 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004446 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004447 if (ret < 0)
4448 goto done;
4449 if (ret) {
4450 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004451 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004452 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004453 }
4454
4455 l = path->nodes[0];
4456 slot = path->slots[0];
4457 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4458
Josef Bacikba1bf482009-09-11 16:11:19 -04004459 if (key.objectid != device->devid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004460 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004461 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004462 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004463 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004464
4465 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4466 length = btrfs_dev_extent_length(l, dev_extent);
4467
Josef Bacikba1bf482009-09-11 16:11:19 -04004468 if (key.offset + length <= new_size) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004469 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004470 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004471 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004472 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004473
Chris Mason8f18cf12008-04-25 16:53:30 -04004474 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004475 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004476
Liu Boa6f93c72017-11-15 16:28:11 -07004477 /*
4478 * We may be relocating the only data chunk we have,
4479 * which could potentially end up with losing data's
4480 * raid profile, so lets allocate an empty one in
4481 * advance.
4482 */
4483 ret = btrfs_may_alloc_data_chunk(fs_info, chunk_offset);
4484 if (ret < 0) {
4485 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4486 goto done;
4487 }
4488
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004489 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4490 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004491 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004492 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004493 if (ret == -ENOSPC)
4494 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004495 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004496
4497 if (failed && !retried) {
4498 failed = 0;
4499 retried = true;
4500 goto again;
4501 } else if (failed && retried) {
4502 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004503 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004504 }
4505
Chris Balld6397ba2009-04-27 07:29:03 -04004506 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004507 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004508 if (IS_ERR(trans)) {
4509 ret = PTR_ERR(trans);
4510 goto done;
4511 }
4512
David Sterba34441362016-10-04 19:34:27 +02004513 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004514
4515 /*
4516 * We checked in the above loop all device extents that were already in
4517 * the device tree. However before we have updated the device's
4518 * total_bytes to the new size, we might have had chunk allocations that
4519 * have not complete yet (new block groups attached to transaction
4520 * handles), and therefore their device extents were not yet in the
4521 * device tree and we missed them in the loop above. So if we have any
4522 * pending chunk using a device extent that overlaps the device range
4523 * that we can not use anymore, commit the current transaction and
4524 * repeat the search on the device tree - this way we guarantee we will
4525 * not have chunks using device extents that end beyond 'new_size'.
4526 */
4527 if (!checked_pending_chunks) {
4528 u64 start = new_size;
4529 u64 len = old_size - new_size;
4530
Jeff Mahoney499f3772015-06-15 09:41:17 -04004531 if (contains_pending_extent(trans->transaction, device,
4532 &start, len)) {
David Sterba34441362016-10-04 19:34:27 +02004533 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004534 checked_pending_chunks = true;
4535 failed = 0;
4536 retried = false;
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004537 ret = btrfs_commit_transaction(trans);
Filipe Manana53e489b2015-06-02 14:43:21 +01004538 if (ret)
4539 goto done;
4540 goto again;
4541 }
4542 }
4543
Miao Xie7cc8e582014-09-03 21:35:38 +08004544 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004545 if (list_empty(&device->resized_list))
4546 list_add_tail(&device->resized_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004547 &fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004548
Chris Balld6397ba2009-04-27 07:29:03 -04004549 WARN_ON(diff > old_total);
Nikolay Borisov7dfb8be2017-06-16 14:39:20 +03004550 btrfs_set_super_total_bytes(super_copy,
4551 round_down(old_total - diff, fs_info->sectorsize));
David Sterba34441362016-10-04 19:34:27 +02004552 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08004553
4554 /* Now btrfs_update_device() will change the on-disk size. */
4555 ret = btrfs_update_device(trans, device);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004556 btrfs_end_transaction(trans);
Chris Mason8f18cf12008-04-25 16:53:30 -04004557done:
4558 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004559 if (ret) {
David Sterba34441362016-10-04 19:34:27 +02004560 mutex_lock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004561 btrfs_device_set_total_bytes(device, old_size);
Anand Jainebbede42017-12-04 12:54:52 +08004562 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
Filipe Manana53e489b2015-06-02 14:43:21 +01004563 device->fs_devices->total_rw_bytes += diff;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004564 atomic64_add(diff, &fs_info->free_chunk_space);
David Sterba34441362016-10-04 19:34:27 +02004565 mutex_unlock(&fs_info->chunk_mutex);
Filipe Manana53e489b2015-06-02 14:43:21 +01004566 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004567 return ret;
4568}
4569
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004570static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04004571 struct btrfs_key *key,
4572 struct btrfs_chunk *chunk, int item_size)
4573{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004574 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004575 struct btrfs_disk_key disk_key;
4576 u32 array_size;
4577 u8 *ptr;
4578
David Sterba34441362016-10-04 19:34:27 +02004579 mutex_lock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004580 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004581 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004582 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
David Sterba34441362016-10-04 19:34:27 +02004583 mutex_unlock(&fs_info->chunk_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04004584 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004585 }
Chris Mason0b86a832008-03-24 15:01:56 -04004586
4587 ptr = super_copy->sys_chunk_array + array_size;
4588 btrfs_cpu_key_to_disk(&disk_key, key);
4589 memcpy(ptr, &disk_key, sizeof(disk_key));
4590 ptr += sizeof(disk_key);
4591 memcpy(ptr, chunk, item_size);
4592 item_size += sizeof(disk_key);
4593 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
David Sterba34441362016-10-04 19:34:27 +02004594 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004595
Chris Mason0b86a832008-03-24 15:01:56 -04004596 return 0;
4597}
4598
Miao Xieb2117a32011-01-05 10:07:28 +00004599/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004600 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004601 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004602static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004603{
Arne Jansen73c5de02011-04-12 12:07:57 +02004604 const struct btrfs_device_info *di_a = a;
4605 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004606
Arne Jansen73c5de02011-04-12 12:07:57 +02004607 if (di_a->max_avail > di_b->max_avail)
4608 return -1;
4609 if (di_a->max_avail < di_b->max_avail)
4610 return 1;
4611 if (di_a->total_avail > di_b->total_avail)
4612 return -1;
4613 if (di_a->total_avail < di_b->total_avail)
4614 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004615 return 0;
4616}
4617
David Woodhouse53b381b2013-01-29 18:40:14 -05004618static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4619{
Zhao Leiffe2d202015-01-20 15:11:44 +08004620 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004621 return;
4622
Miao Xieceda0862013-04-11 10:30:16 +00004623 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004624}
4625
Qu Wenruo062d4d12018-01-30 18:20:46 +08004626#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004627 - sizeof(struct btrfs_chunk)) \
4628 / sizeof(struct btrfs_stripe) + 1)
4629
4630#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4631 - 2 * sizeof(struct btrfs_disk_key) \
4632 - 2 * sizeof(struct btrfs_chunk)) \
4633 / sizeof(struct btrfs_stripe) + 1)
4634
Miao Xieb2117a32011-01-05 10:07:28 +00004635static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
David Sterba72b468c2017-02-10 19:46:27 +01004636 u64 start, u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004637{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004638 struct btrfs_fs_info *info = trans->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004639 struct btrfs_fs_devices *fs_devices = info->fs_devices;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004640 struct btrfs_device *device;
Arne Jansen73c5de02011-04-12 12:07:57 +02004641 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004642 struct extent_map_tree *em_tree;
4643 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004644 struct btrfs_device_info *devices_info = NULL;
4645 u64 total_avail;
4646 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004647 int data_stripes; /* number of stripes that count for
4648 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004649 int sub_stripes; /* sub_stripes info for map */
4650 int dev_stripes; /* stripes per dev */
4651 int devs_max; /* max devs to use */
4652 int devs_min; /* min devs needed */
4653 int devs_increment; /* ndevs has to be a multiple of this */
4654 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004655 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004656 u64 max_stripe_size;
4657 u64 max_chunk_size;
4658 u64 stripe_size;
4659 u64 num_bytes;
4660 int ndevs;
4661 int i;
4662 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004663 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004664
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004665 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004666
Qu Wenruo4117f202018-01-22 13:50:54 +08004667 if (list_empty(&fs_devices->alloc_list)) {
4668 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4669 btrfs_debug(info, "%s: no writable device", __func__);
Miao Xieb2117a32011-01-05 10:07:28 +00004670 return -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004671 }
Miao Xieb2117a32011-01-05 10:07:28 +00004672
Qu Wenruo3e72ee82018-01-30 18:20:45 +08004673 index = btrfs_bg_flags_to_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004674
Liu Bo31e50222012-11-21 14:18:10 +00004675 sub_stripes = btrfs_raid_array[index].sub_stripes;
4676 dev_stripes = btrfs_raid_array[index].dev_stripes;
4677 devs_max = btrfs_raid_array[index].devs_max;
4678 devs_min = btrfs_raid_array[index].devs_min;
4679 devs_increment = btrfs_raid_array[index].devs_increment;
4680 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004681
4682 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004683 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004684 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004685 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004686 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004687 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004688 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004689 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4690 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004691 else
Byongho Leeee221842015-12-15 01:42:10 +09004692 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004693 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004694 if (!devs_max)
Qu Wenruo062d4d12018-01-30 18:20:46 +08004695 devs_max = BTRFS_MAX_DEVS(info);
Arne Jansen73c5de02011-04-12 12:07:57 +02004696 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004697 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004698 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004699 if (!devs_max)
4700 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004701 } else {
David Sterba351fd352014-05-15 16:48:20 +02004702 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004703 type);
4704 BUG_ON(1);
4705 }
4706
4707 /* we don't want a chunk larger than 10% of writeable space */
4708 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4709 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004710
David Sterba31e818f2015-02-20 18:00:26 +01004711 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004712 GFP_NOFS);
4713 if (!devices_info)
4714 return -ENOMEM;
4715
Arne Jansen73c5de02011-04-12 12:07:57 +02004716 /*
4717 * in the first pass through the devices list, we gather information
4718 * about the available holes on each device.
4719 */
4720 ndevs = 0;
Nikolay Borisovebcc9302017-06-27 10:02:24 +03004721 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004722 u64 max_avail;
4723 u64 dev_offset;
4724
Anand Jainebbede42017-12-04 12:54:52 +08004725 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004726 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004727 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004728 continue;
4729 }
4730
Anand Jaine12c9622017-12-04 12:54:53 +08004731 if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
4732 &device->dev_state) ||
Anand Jain401e29c2017-12-04 12:54:55 +08004733 test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
Arne Jansen73c5de02011-04-12 12:07:57 +02004734 continue;
4735
4736 if (device->total_bytes > device->bytes_used)
4737 total_avail = device->total_bytes - device->bytes_used;
4738 else
4739 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004740
4741 /* If there is no space on this device, skip it. */
4742 if (total_avail == 0)
4743 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004744
Josef Bacik6df9a952013-06-27 13:22:46 -04004745 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004746 max_stripe_size * dev_stripes,
4747 &dev_offset, &max_avail);
4748 if (ret && ret != -ENOSPC)
4749 goto error;
4750
4751 if (ret == 0)
4752 max_avail = max_stripe_size * dev_stripes;
4753
Qu Wenruo4117f202018-01-22 13:50:54 +08004754 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes) {
4755 if (btrfs_test_opt(info, ENOSPC_DEBUG))
4756 btrfs_debug(info,
4757 "%s: devid %llu has no free space, have=%llu want=%u",
4758 __func__, device->devid, max_avail,
4759 BTRFS_STRIPE_LEN * dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004760 continue;
Qu Wenruo4117f202018-01-22 13:50:54 +08004761 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004762
Eric Sandeen063d0062013-01-31 00:55:01 +00004763 if (ndevs == fs_devices->rw_devices) {
4764 WARN(1, "%s: found more than %llu devices\n",
4765 __func__, fs_devices->rw_devices);
4766 break;
4767 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004768 devices_info[ndevs].dev_offset = dev_offset;
4769 devices_info[ndevs].max_avail = max_avail;
4770 devices_info[ndevs].total_avail = total_avail;
4771 devices_info[ndevs].dev = device;
4772 ++ndevs;
4773 }
4774
4775 /*
4776 * now sort the devices by hole size / available space
4777 */
4778 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4779 btrfs_cmp_device_info, NULL);
4780
4781 /* round down to number of usable stripes */
Nikolay Borisove5600fd2017-06-27 10:02:25 +03004782 ndevs = round_down(ndevs, devs_increment);
Arne Jansen73c5de02011-04-12 12:07:57 +02004783
Qu Wenruoba89b802018-01-31 13:56:15 +08004784 if (ndevs < devs_min) {
Arne Jansen73c5de02011-04-12 12:07:57 +02004785 ret = -ENOSPC;
Qu Wenruo4117f202018-01-22 13:50:54 +08004786 if (btrfs_test_opt(info, ENOSPC_DEBUG)) {
4787 btrfs_debug(info,
4788 "%s: not enough devices with free space: have=%d minimum required=%d",
Qu Wenruoba89b802018-01-31 13:56:15 +08004789 __func__, ndevs, devs_min);
Qu Wenruo4117f202018-01-22 13:50:54 +08004790 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004791 goto error;
4792 }
4793
Nikolay Borisovf148ef42017-06-27 10:02:26 +03004794 ndevs = min(ndevs, devs_max);
4795
Arne Jansen73c5de02011-04-12 12:07:57 +02004796 /*
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004797 * The primary goal is to maximize the number of stripes, so use as
4798 * many devices as possible, even if the stripes are not maximum sized.
4799 *
4800 * The DUP profile stores more than one stripe per device, the
4801 * max_avail is the total size so we have to adjust.
Arne Jansen73c5de02011-04-12 12:07:57 +02004802 */
Hans van Kranenburg92e222d2018-02-05 17:45:11 +01004803 stripe_size = div_u64(devices_info[ndevs - 1].max_avail, dev_stripes);
Arne Jansen73c5de02011-04-12 12:07:57 +02004804 num_stripes = ndevs * dev_stripes;
4805
David Woodhouse53b381b2013-01-29 18:40:14 -05004806 /*
4807 * this will have to be fixed for RAID1 and RAID10 over
4808 * more drives
4809 */
4810 data_stripes = num_stripes / ncopies;
4811
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004812 if (type & BTRFS_BLOCK_GROUP_RAID5)
David Woodhouse53b381b2013-01-29 18:40:14 -05004813 data_stripes = num_stripes - 1;
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004814
4815 if (type & BTRFS_BLOCK_GROUP_RAID6)
David Woodhouse53b381b2013-01-29 18:40:14 -05004816 data_stripes = num_stripes - 2;
Chris Mason86db2572013-02-20 16:23:40 -05004817
4818 /*
4819 * Use the number of data stripes to figure out how big this chunk
4820 * is really going to be in terms of logical address space,
4821 * and compare that answer with the max chunk size
4822 */
4823 if (stripe_size * data_stripes > max_chunk_size) {
David Sterbab8b93ad2015-01-16 17:26:13 +01004824 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004825
4826 /* bump the answer up to a 16MB boundary */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004827 stripe_size = round_up(stripe_size, SZ_16M);
Chris Mason86db2572013-02-20 16:23:40 -05004828
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004829 /*
4830 * But don't go higher than the limits we found while searching
4831 * for free extents
Chris Mason86db2572013-02-20 16:23:40 -05004832 */
Qu Wenruo793ff2c2018-01-31 14:16:34 +08004833 stripe_size = min(devices_info[ndevs - 1].max_avail,
4834 stripe_size);
Chris Mason86db2572013-02-20 16:23:40 -05004835 }
4836
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004837 /* align to BTRFS_STRIPE_LEN */
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004838 stripe_size = round_down(stripe_size, BTRFS_STRIPE_LEN);
Arne Jansen73c5de02011-04-12 12:07:57 +02004839
Miao Xieb2117a32011-01-05 10:07:28 +00004840 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4841 if (!map) {
4842 ret = -ENOMEM;
4843 goto error;
4844 }
4845 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004846
Arne Jansen73c5de02011-04-12 12:07:57 +02004847 for (i = 0; i < ndevs; ++i) {
4848 for (j = 0; j < dev_stripes; ++j) {
4849 int s = i * dev_stripes + j;
4850 map->stripes[s].dev = devices_info[i].dev;
4851 map->stripes[s].physical = devices_info[i].dev_offset +
4852 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004853 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004854 }
Nikolay Borisov500ceed2017-07-14 09:55:41 +03004855 map->stripe_len = BTRFS_STRIPE_LEN;
4856 map->io_align = BTRFS_STRIPE_LEN;
4857 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04004858 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004859 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004860
David Woodhouse53b381b2013-01-29 18:40:14 -05004861 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004862
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004863 trace_btrfs_chunk_alloc(info, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004864
David Sterba172ddd62011-04-21 00:48:27 +02004865 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004866 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004867 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004868 ret = -ENOMEM;
4869 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004870 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004871 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004872 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004873 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004874 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004875 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004876 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004877 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004878
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004879 em_tree = &info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004880 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004881 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004882 if (ret) {
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004883 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004884 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004885 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004886 }
Yan Zheng2b820322008-11-17 21:11:30 -05004887
Nikolay Borisov1efb72a2017-08-21 12:43:49 +03004888 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4889 refcount_inc(&em->refs);
4890 write_unlock(&em_tree->lock);
4891
Nikolay Borisov01744842017-07-27 14:22:11 +03004892 ret = btrfs_make_block_group(trans, info, 0, type, start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004893 if (ret)
4894 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004895
Miao Xie7cc8e582014-09-03 21:35:38 +08004896 for (i = 0; i < map->num_stripes; i++) {
4897 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4898 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4899 }
Miao Xie43530c42014-09-03 21:35:36 +08004900
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03004901 atomic64_sub(stripe_size * map->num_stripes, &info->free_chunk_space);
Miao Xie1c116182014-09-03 21:35:37 +08004902
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004903 free_extent_map(em);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004904 check_raid56_incompat_flag(info, type);
David Woodhouse53b381b2013-01-29 18:40:14 -05004905
Miao Xieb2117a32011-01-05 10:07:28 +00004906 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004907 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004908
Josef Bacik6df9a952013-06-27 13:22:46 -04004909error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004910 write_lock(&em_tree->lock);
4911 remove_extent_mapping(em_tree, em);
4912 write_unlock(&em_tree->lock);
4913
4914 /* One for our allocation */
4915 free_extent_map(em);
4916 /* One for the tree reference */
4917 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004918 /* One for the pending_chunks list reference */
4919 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004920error:
Miao Xieb2117a32011-01-05 10:07:28 +00004921 kfree(devices_info);
4922 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004923}
4924
Josef Bacik6df9a952013-06-27 13:22:46 -04004925int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004926 struct btrfs_fs_info *fs_info,
Josef Bacik6df9a952013-06-27 13:22:46 -04004927 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004928{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04004929 struct btrfs_root *extent_root = fs_info->extent_root;
4930 struct btrfs_root *chunk_root = fs_info->chunk_root;
Yan Zheng2b820322008-11-17 21:11:30 -05004931 struct btrfs_key key;
Yan Zheng2b820322008-11-17 21:11:30 -05004932 struct btrfs_device *device;
4933 struct btrfs_chunk *chunk;
4934 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004935 struct extent_map *em;
4936 struct map_lookup *map;
4937 size_t item_size;
4938 u64 dev_offset;
4939 u64 stripe_size;
4940 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004941 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004942
Liu Bo592d92e2017-03-14 13:33:55 -07004943 em = get_chunk_map(fs_info, chunk_offset, chunk_size);
4944 if (IS_ERR(em))
4945 return PTR_ERR(em);
Josef Bacik6df9a952013-06-27 13:22:46 -04004946
Jeff Mahoney95617d62015-06-03 10:55:48 -04004947 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004948 item_size = btrfs_chunk_item_size(map->num_stripes);
4949 stripe_size = em->orig_block_len;
4950
4951 chunk = kzalloc(item_size, GFP_NOFS);
4952 if (!chunk) {
4953 ret = -ENOMEM;
4954 goto out;
4955 }
4956
Filipe Manana50460e32015-11-20 10:42:47 +00004957 /*
4958 * Take the device list mutex to prevent races with the final phase of
4959 * a device replace operation that replaces the device object associated
4960 * with the map's stripes, because the device object's id can change
4961 * at any time during that final phase of the device replace operation
4962 * (dev-replace.c:btrfs_dev_replace_finishing()).
4963 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004964 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004965 for (i = 0; i < map->num_stripes; i++) {
4966 device = map->stripes[i].dev;
4967 dev_offset = map->stripes[i].physical;
4968
Yan Zheng2b820322008-11-17 21:11:30 -05004969 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004970 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004971 break;
Nikolay Borisovb5d90712017-08-18 17:58:23 +03004972 ret = btrfs_alloc_dev_extent(trans, device, chunk_offset,
4973 dev_offset, stripe_size);
Josef Bacik6df9a952013-06-27 13:22:46 -04004974 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004975 break;
4976 }
4977 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004978 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Filipe Manana50460e32015-11-20 10:42:47 +00004979 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004980 }
4981
Yan Zheng2b820322008-11-17 21:11:30 -05004982 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004983 for (i = 0; i < map->num_stripes; i++) {
4984 device = map->stripes[i].dev;
4985 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004986
4987 btrfs_set_stack_stripe_devid(stripe, device->devid);
4988 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4989 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4990 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004991 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004992 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004993
4994 btrfs_set_stack_chunk_length(chunk, chunk_size);
4995 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4996 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4997 btrfs_set_stack_chunk_type(chunk, map->type);
4998 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4999 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
5000 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005001 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
Yan Zheng2b820322008-11-17 21:11:30 -05005002 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
5003
5004 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
5005 key.type = BTRFS_CHUNK_ITEM_KEY;
5006 key.offset = chunk_offset;
5007
5008 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005009 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
5010 /*
5011 * TODO: Cleanup of inserted chunk root in case of
5012 * failure.
5013 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005014 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05005015 }
liubo1abe9b82011-03-24 11:18:59 +00005016
Josef Bacik6df9a952013-06-27 13:22:46 -04005017out:
Yan Zheng2b820322008-11-17 21:11:30 -05005018 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04005019 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07005020 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005021}
5022
5023/*
5024 * Chunk allocation falls into two parts. The first part does works
5025 * that make the new allocated chunk useable, but not do any operation
5026 * that modifies the chunk tree. The second part does the works that
5027 * require modifying the chunk tree. This division is important for the
5028 * bootstrap process of adding storage to a seed btrfs.
5029 */
5030int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005031 struct btrfs_fs_info *fs_info, u64 type)
Yan Zheng2b820322008-11-17 21:11:30 -05005032{
5033 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005034
David Sterbaa32bf9a2018-03-16 02:21:22 +01005035 lockdep_assert_held(&fs_info->chunk_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005036 chunk_offset = find_next_chunk(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005037 return __btrfs_alloc_chunk(trans, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05005038}
5039
Chris Masond3977122009-01-05 21:25:51 -05005040static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
David Sterbae4a4dce2017-02-10 19:49:01 +01005041 struct btrfs_fs_info *fs_info)
Yan Zheng2b820322008-11-17 21:11:30 -05005042{
5043 u64 chunk_offset;
5044 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05005045 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05005046 int ret;
5047
Josef Bacik6df9a952013-06-27 13:22:46 -04005048 chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005049 alloc_profile = btrfs_metadata_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005050 ret = __btrfs_alloc_chunk(trans, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005051 if (ret)
5052 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005053
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 sys_chunk_offset = find_next_chunk(fs_info);
Jeff Mahoney1b868262017-05-17 11:38:35 -04005055 alloc_profile = btrfs_system_alloc_profile(fs_info);
David Sterba72b468c2017-02-10 19:46:27 +01005056 ret = __btrfs_alloc_chunk(trans, sys_chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005057 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005058}
5059
Miao Xied20983b2014-07-03 18:22:13 +08005060static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5061{
5062 int max_errors;
5063
5064 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5065 BTRFS_BLOCK_GROUP_RAID10 |
5066 BTRFS_BLOCK_GROUP_RAID5 |
5067 BTRFS_BLOCK_GROUP_DUP)) {
5068 max_errors = 1;
5069 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5070 max_errors = 2;
5071 } else {
5072 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005073 }
5074
Miao Xied20983b2014-07-03 18:22:13 +08005075 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05005076}
5077
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005078int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
Yan Zheng2b820322008-11-17 21:11:30 -05005079{
5080 struct extent_map *em;
5081 struct map_lookup *map;
Yan Zheng2b820322008-11-17 21:11:30 -05005082 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08005083 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05005084 int i;
5085
Liu Bo592d92e2017-03-14 13:33:55 -07005086 em = get_chunk_map(fs_info, chunk_offset, 1);
5087 if (IS_ERR(em))
Yan Zheng2b820322008-11-17 21:11:30 -05005088 return 1;
5089
Jeff Mahoney95617d62015-06-03 10:55:48 -04005090 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05005091 for (i = 0; i < map->num_stripes; i++) {
Anand Jaine6e674b2017-12-04 12:54:54 +08005092 if (test_bit(BTRFS_DEV_STATE_MISSING,
5093 &map->stripes[i].dev->dev_state)) {
Miao Xied20983b2014-07-03 18:22:13 +08005094 miss_ndevs++;
5095 continue;
5096 }
Anand Jainebbede42017-12-04 12:54:52 +08005097 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE,
5098 &map->stripes[i].dev->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05005099 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08005100 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005101 }
5102 }
Miao Xied20983b2014-07-03 18:22:13 +08005103
5104 /*
5105 * If the number of missing devices is larger than max errors,
5106 * we can not write the data into that chunk successfully, so
5107 * set it readonly.
5108 */
5109 if (miss_ndevs > btrfs_chunk_max_errors(map))
5110 readonly = 1;
5111end:
Yan Zheng2b820322008-11-17 21:11:30 -05005112 free_extent_map(em);
5113 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005114}
5115
5116void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5117{
David Sterbaa8067e02011-04-21 00:34:43 +02005118 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005119}
5120
5121void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5122{
5123 struct extent_map *em;
5124
Chris Masond3977122009-01-05 21:25:51 -05005125 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005126 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005127 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5128 if (em)
5129 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005130 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005131 if (!em)
5132 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005133 /* once for us */
5134 free_extent_map(em);
5135 /* once for the tree */
5136 free_extent_map(em);
5137 }
5138}
5139
Stefan Behrens5d964052012-11-05 14:59:07 +01005140int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005141{
5142 struct extent_map *em;
5143 struct map_lookup *map;
Chris Masonf1885912008-04-09 16:28:12 -04005144 int ret;
5145
Liu Bo592d92e2017-03-14 13:33:55 -07005146 em = get_chunk_map(fs_info, logical, len);
5147 if (IS_ERR(em))
5148 /*
5149 * We could return errors for these cases, but that could get
5150 * ugly and we'd probably do the same thing which is just not do
5151 * anything else and exit, so return 1 so the callers don't try
5152 * to use other copies.
5153 */
Josef Bacikfb7669b2013-04-23 10:53:18 -04005154 return 1;
Josef Bacikfb7669b2013-04-23 10:53:18 -04005155
Jeff Mahoney95617d62015-06-03 10:55:48 -04005156 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005157 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5158 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005159 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5160 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005161 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5162 ret = 2;
5163 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
Liu Bo8810f752018-01-02 13:36:41 -07005164 /*
5165 * There could be two corrupted data stripes, we need
5166 * to loop retry in order to rebuild the correct data.
5167 *
5168 * Fail a stripe at a time on every retry except the
5169 * stripe under reconstruction.
5170 */
5171 ret = map->num_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04005172 else
5173 ret = 1;
5174 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005175
David Sterba7e79cb82018-03-24 02:11:38 +01005176 btrfs_dev_replace_read_lock(&fs_info->dev_replace);
Liu Bo6fad8232017-03-14 13:33:59 -07005177 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace) &&
5178 fs_info->dev_replace.tgtdev)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005179 ret++;
David Sterba7e79cb82018-03-24 02:11:38 +01005180 btrfs_dev_replace_read_unlock(&fs_info->dev_replace);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005181
Chris Masonf1885912008-04-09 16:28:12 -04005182 return ret;
5183}
5184
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005185unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
David Woodhouse53b381b2013-01-29 18:40:14 -05005186 u64 logical)
5187{
5188 struct extent_map *em;
5189 struct map_lookup *map;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005190 unsigned long len = fs_info->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05005191
Liu Bo592d92e2017-03-14 13:33:55 -07005192 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005193
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005194 if (!WARN_ON(IS_ERR(em))) {
5195 map = em->map_lookup;
5196 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5197 len = map->stripe_len * nr_data_stripes(map);
5198 free_extent_map(em);
5199 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005200 return len;
5201}
5202
Nikolay Borisove4ff5fb2017-07-19 10:48:42 +03005203int btrfs_is_parity_mirror(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
David Woodhouse53b381b2013-01-29 18:40:14 -05005204{
5205 struct extent_map *em;
5206 struct map_lookup *map;
David Woodhouse53b381b2013-01-29 18:40:14 -05005207 int ret = 0;
5208
Liu Bo592d92e2017-03-14 13:33:55 -07005209 em = get_chunk_map(fs_info, logical, len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005210
Nikolay Borisov69f03f12017-07-11 16:55:51 +03005211 if(!WARN_ON(IS_ERR(em))) {
5212 map = em->map_lookup;
5213 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5214 ret = 1;
5215 free_extent_map(em);
5216 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005217 return ret;
5218}
5219
Stefan Behrens30d98612012-11-06 14:52:18 +01005220static int find_live_mirror(struct btrfs_fs_info *fs_info,
Anand Jain99f92a72018-03-14 16:29:12 +08005221 struct map_lookup *map, int first,
Anand Jain8ba0ae72018-03-14 16:29:13 +08005222 int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005223{
5224 int i;
Anand Jain99f92a72018-03-14 16:29:12 +08005225 int num_stripes;
Anand Jain8ba0ae72018-03-14 16:29:13 +08005226 int preferred_mirror;
Stefan Behrens30d98612012-11-06 14:52:18 +01005227 int tolerance;
5228 struct btrfs_device *srcdev;
5229
Anand Jain99f92a72018-03-14 16:29:12 +08005230 ASSERT((map->type &
5231 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)));
5232
5233 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5234 num_stripes = map->sub_stripes;
5235 else
5236 num_stripes = map->num_stripes;
5237
Anand Jain8ba0ae72018-03-14 16:29:13 +08005238 preferred_mirror = first + current->pid % num_stripes;
5239
Stefan Behrens30d98612012-11-06 14:52:18 +01005240 if (dev_replace_is_ongoing &&
5241 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5242 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5243 srcdev = fs_info->dev_replace.srcdev;
5244 else
5245 srcdev = NULL;
5246
5247 /*
5248 * try to avoid the drive that is the source drive for a
5249 * dev-replace procedure, only choose it if no other non-missing
5250 * mirror is available
5251 */
5252 for (tolerance = 0; tolerance < 2; tolerance++) {
Anand Jain8ba0ae72018-03-14 16:29:13 +08005253 if (map->stripes[preferred_mirror].dev->bdev &&
5254 (tolerance || map->stripes[preferred_mirror].dev != srcdev))
5255 return preferred_mirror;
Anand Jain99f92a72018-03-14 16:29:12 +08005256 for (i = first; i < first + num_stripes; i++) {
Stefan Behrens30d98612012-11-06 14:52:18 +01005257 if (map->stripes[i].dev->bdev &&
5258 (tolerance || map->stripes[i].dev != srcdev))
5259 return i;
5260 }
Chris Masondfe25022008-05-13 13:46:40 -04005261 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005262
Chris Masondfe25022008-05-13 13:46:40 -04005263 /* we couldn't find one that doesn't fail. Just return something
5264 * and the io error handling code will clean up eventually
5265 */
Anand Jain8ba0ae72018-03-14 16:29:13 +08005266 return preferred_mirror;
Chris Masondfe25022008-05-13 13:46:40 -04005267}
5268
David Woodhouse53b381b2013-01-29 18:40:14 -05005269static inline int parity_smaller(u64 a, u64 b)
5270{
5271 return a > b;
5272}
5273
5274/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005275static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005276{
5277 struct btrfs_bio_stripe s;
5278 int i;
5279 u64 l;
5280 int again = 1;
5281
5282 while (again) {
5283 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005284 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005285 if (parity_smaller(bbio->raid_map[i],
5286 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005287 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005288 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005289 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005290 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005291 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005292 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005293
David Woodhouse53b381b2013-01-29 18:40:14 -05005294 again = 1;
5295 }
5296 }
5297 }
5298}
5299
Zhao Lei6e9606d2015-01-20 15:11:34 +08005300static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5301{
5302 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005303 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005304 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005305 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005306 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005307 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005308 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005309 /*
5310 * plus the raid_map, which includes both the tgt dev
5311 * and the stripes
5312 */
5313 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005314 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005315
5316 atomic_set(&bbio->error, 0);
Elena Reshetova140475a2017-03-03 10:55:10 +02005317 refcount_set(&bbio->refs, 1);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005318
5319 return bbio;
5320}
5321
5322void btrfs_get_bbio(struct btrfs_bio *bbio)
5323{
Elena Reshetova140475a2017-03-03 10:55:10 +02005324 WARN_ON(!refcount_read(&bbio->refs));
5325 refcount_inc(&bbio->refs);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005326}
5327
5328void btrfs_put_bbio(struct btrfs_bio *bbio)
5329{
5330 if (!bbio)
5331 return;
Elena Reshetova140475a2017-03-03 10:55:10 +02005332 if (refcount_dec_and_test(&bbio->refs))
Zhao Lei6e9606d2015-01-20 15:11:34 +08005333 kfree(bbio);
5334}
5335
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005336/* can REQ_OP_DISCARD be sent with other REQ like REQ_OP_WRITE? */
5337/*
5338 * Please note that, discard won't be sent to target device of device
5339 * replace.
5340 */
5341static int __btrfs_map_block_for_discard(struct btrfs_fs_info *fs_info,
5342 u64 logical, u64 length,
5343 struct btrfs_bio **bbio_ret)
5344{
5345 struct extent_map *em;
5346 struct map_lookup *map;
5347 struct btrfs_bio *bbio;
5348 u64 offset;
5349 u64 stripe_nr;
5350 u64 stripe_nr_end;
5351 u64 stripe_end_offset;
5352 u64 stripe_cnt;
5353 u64 stripe_len;
5354 u64 stripe_offset;
5355 u64 num_stripes;
5356 u32 stripe_index;
5357 u32 factor = 0;
5358 u32 sub_stripes = 0;
5359 u64 stripes_per_dev = 0;
5360 u32 remaining_stripes = 0;
5361 u32 last_stripe = 0;
5362 int ret = 0;
5363 int i;
5364
5365 /* discard always return a bbio */
5366 ASSERT(bbio_ret);
5367
5368 em = get_chunk_map(fs_info, logical, length);
5369 if (IS_ERR(em))
5370 return PTR_ERR(em);
5371
5372 map = em->map_lookup;
5373 /* we don't discard raid56 yet */
5374 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
5375 ret = -EOPNOTSUPP;
5376 goto out;
5377 }
5378
5379 offset = logical - em->start;
5380 length = min_t(u64, em->len - offset, length);
5381
5382 stripe_len = map->stripe_len;
5383 /*
5384 * stripe_nr counts the total number of stripes we have to stride
5385 * to get to this block
5386 */
5387 stripe_nr = div64_u64(offset, stripe_len);
5388
5389 /* stripe_offset is the offset of this block in its stripe */
5390 stripe_offset = offset - stripe_nr * stripe_len;
5391
5392 stripe_nr_end = round_up(offset + length, map->stripe_len);
Liu Bo42c61ab2017-04-03 13:45:24 -07005393 stripe_nr_end = div64_u64(stripe_nr_end, map->stripe_len);
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005394 stripe_cnt = stripe_nr_end - stripe_nr;
5395 stripe_end_offset = stripe_nr_end * map->stripe_len -
5396 (offset + length);
5397 /*
5398 * after this, stripe_nr is the number of stripes on this
5399 * device we have to walk to find the data, and stripe_index is
5400 * the number of our device in the stripe array
5401 */
5402 num_stripes = 1;
5403 stripe_index = 0;
5404 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5405 BTRFS_BLOCK_GROUP_RAID10)) {
5406 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5407 sub_stripes = 1;
5408 else
5409 sub_stripes = map->sub_stripes;
5410
5411 factor = map->num_stripes / sub_stripes;
5412 num_stripes = min_t(u64, map->num_stripes,
5413 sub_stripes * stripe_cnt);
5414 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
5415 stripe_index *= sub_stripes;
5416 stripes_per_dev = div_u64_rem(stripe_cnt, factor,
5417 &remaining_stripes);
5418 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5419 last_stripe *= sub_stripes;
5420 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5421 BTRFS_BLOCK_GROUP_DUP)) {
5422 num_stripes = map->num_stripes;
5423 } else {
5424 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5425 &stripe_index);
5426 }
5427
5428 bbio = alloc_btrfs_bio(num_stripes, 0);
5429 if (!bbio) {
5430 ret = -ENOMEM;
5431 goto out;
5432 }
5433
5434 for (i = 0; i < num_stripes; i++) {
5435 bbio->stripes[i].physical =
5436 map->stripes[stripe_index].physical +
5437 stripe_offset + stripe_nr * map->stripe_len;
5438 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
5439
5440 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5441 BTRFS_BLOCK_GROUP_RAID10)) {
5442 bbio->stripes[i].length = stripes_per_dev *
5443 map->stripe_len;
5444
5445 if (i / sub_stripes < remaining_stripes)
5446 bbio->stripes[i].length +=
5447 map->stripe_len;
5448
5449 /*
5450 * Special for the first stripe and
5451 * the last stripe:
5452 *
5453 * |-------|...|-------|
5454 * |----------|
5455 * off end_off
5456 */
5457 if (i < sub_stripes)
5458 bbio->stripes[i].length -=
5459 stripe_offset;
5460
5461 if (stripe_index >= last_stripe &&
5462 stripe_index <= (last_stripe +
5463 sub_stripes - 1))
5464 bbio->stripes[i].length -=
5465 stripe_end_offset;
5466
5467 if (i == sub_stripes - 1)
5468 stripe_offset = 0;
5469 } else {
5470 bbio->stripes[i].length = length;
5471 }
5472
5473 stripe_index++;
5474 if (stripe_index == map->num_stripes) {
5475 stripe_index = 0;
5476 stripe_nr++;
5477 }
5478 }
5479
5480 *bbio_ret = bbio;
5481 bbio->map_type = map->type;
5482 bbio->num_stripes = num_stripes;
5483out:
5484 free_extent_map(em);
5485 return ret;
5486}
5487
Liu Bo5ab56092017-03-14 13:33:57 -07005488/*
5489 * In dev-replace case, for repair case (that's the only case where the mirror
5490 * is selected explicitly when calling btrfs_map_block), blocks left of the
5491 * left cursor can also be read from the target drive.
5492 *
5493 * For REQ_GET_READ_MIRRORS, the target drive is added as the last one to the
5494 * array of stripes.
5495 * For READ, it also needs to be supported using the same mirror number.
5496 *
5497 * If the requested block is not left of the left cursor, EIO is returned. This
5498 * can happen because btrfs_num_copies() returns one more in the dev-replace
5499 * case.
5500 */
5501static int get_extra_mirror_from_replace(struct btrfs_fs_info *fs_info,
5502 u64 logical, u64 length,
5503 u64 srcdev_devid, int *mirror_num,
5504 u64 *physical)
5505{
5506 struct btrfs_bio *bbio = NULL;
5507 int num_stripes;
5508 int index_srcdev = 0;
5509 int found = 0;
5510 u64 physical_of_found = 0;
5511 int i;
5512 int ret = 0;
5513
5514 ret = __btrfs_map_block(fs_info, BTRFS_MAP_GET_READ_MIRRORS,
5515 logical, &length, &bbio, 0, 0);
5516 if (ret) {
5517 ASSERT(bbio == NULL);
5518 return ret;
5519 }
5520
5521 num_stripes = bbio->num_stripes;
5522 if (*mirror_num > num_stripes) {
5523 /*
5524 * BTRFS_MAP_GET_READ_MIRRORS does not contain this mirror,
5525 * that means that the requested area is not left of the left
5526 * cursor
5527 */
5528 btrfs_put_bbio(bbio);
5529 return -EIO;
5530 }
5531
5532 /*
5533 * process the rest of the function using the mirror_num of the source
5534 * drive. Therefore look it up first. At the end, patch the device
5535 * pointer to the one of the target drive.
5536 */
5537 for (i = 0; i < num_stripes; i++) {
5538 if (bbio->stripes[i].dev->devid != srcdev_devid)
5539 continue;
5540
5541 /*
5542 * In case of DUP, in order to keep it simple, only add the
5543 * mirror with the lowest physical address
5544 */
5545 if (found &&
5546 physical_of_found <= bbio->stripes[i].physical)
5547 continue;
5548
5549 index_srcdev = i;
5550 found = 1;
5551 physical_of_found = bbio->stripes[i].physical;
5552 }
5553
5554 btrfs_put_bbio(bbio);
5555
5556 ASSERT(found);
5557 if (!found)
5558 return -EIO;
5559
5560 *mirror_num = index_srcdev + 1;
5561 *physical = physical_of_found;
5562 return ret;
5563}
5564
Liu Bo73c0f222017-03-14 13:33:58 -07005565static void handle_ops_on_dev_replace(enum btrfs_map_op op,
5566 struct btrfs_bio **bbio_ret,
5567 struct btrfs_dev_replace *dev_replace,
5568 int *num_stripes_ret, int *max_errors_ret)
5569{
5570 struct btrfs_bio *bbio = *bbio_ret;
5571 u64 srcdev_devid = dev_replace->srcdev->devid;
5572 int tgtdev_indexes = 0;
5573 int num_stripes = *num_stripes_ret;
5574 int max_errors = *max_errors_ret;
5575 int i;
5576
5577 if (op == BTRFS_MAP_WRITE) {
5578 int index_where_to_add;
5579
5580 /*
5581 * duplicate the write operations while the dev replace
5582 * procedure is running. Since the copying of the old disk to
5583 * the new disk takes place at run time while the filesystem is
5584 * mounted writable, the regular write operations to the old
5585 * disk have to be duplicated to go to the new disk as well.
5586 *
5587 * Note that device->missing is handled by the caller, and that
5588 * the write to the old disk is already set up in the stripes
5589 * array.
5590 */
5591 index_where_to_add = num_stripes;
5592 for (i = 0; i < num_stripes; i++) {
5593 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5594 /* write to new disk, too */
5595 struct btrfs_bio_stripe *new =
5596 bbio->stripes + index_where_to_add;
5597 struct btrfs_bio_stripe *old =
5598 bbio->stripes + i;
5599
5600 new->physical = old->physical;
5601 new->length = old->length;
5602 new->dev = dev_replace->tgtdev;
5603 bbio->tgtdev_map[i] = index_where_to_add;
5604 index_where_to_add++;
5605 max_errors++;
5606 tgtdev_indexes++;
5607 }
5608 }
5609 num_stripes = index_where_to_add;
5610 } else if (op == BTRFS_MAP_GET_READ_MIRRORS) {
5611 int index_srcdev = 0;
5612 int found = 0;
5613 u64 physical_of_found = 0;
5614
5615 /*
5616 * During the dev-replace procedure, the target drive can also
5617 * be used to read data in case it is needed to repair a corrupt
5618 * block elsewhere. This is possible if the requested area is
5619 * left of the left cursor. In this area, the target drive is a
5620 * full copy of the source drive.
5621 */
5622 for (i = 0; i < num_stripes; i++) {
5623 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5624 /*
5625 * In case of DUP, in order to keep it simple,
5626 * only add the mirror with the lowest physical
5627 * address
5628 */
5629 if (found &&
5630 physical_of_found <=
5631 bbio->stripes[i].physical)
5632 continue;
5633 index_srcdev = i;
5634 found = 1;
5635 physical_of_found = bbio->stripes[i].physical;
5636 }
5637 }
5638 if (found) {
5639 struct btrfs_bio_stripe *tgtdev_stripe =
5640 bbio->stripes + num_stripes;
5641
5642 tgtdev_stripe->physical = physical_of_found;
5643 tgtdev_stripe->length =
5644 bbio->stripes[index_srcdev].length;
5645 tgtdev_stripe->dev = dev_replace->tgtdev;
5646 bbio->tgtdev_map[index_srcdev] = num_stripes;
5647
5648 tgtdev_indexes++;
5649 num_stripes++;
5650 }
5651 }
5652
5653 *num_stripes_ret = num_stripes;
5654 *max_errors_ret = max_errors;
5655 bbio->num_tgtdevs = tgtdev_indexes;
5656 *bbio_ret = bbio;
5657}
5658
Liu Bo2b19a1f2017-03-14 13:34:00 -07005659static bool need_full_stripe(enum btrfs_map_op op)
5660{
5661 return (op == BTRFS_MAP_WRITE || op == BTRFS_MAP_GET_READ_MIRRORS);
5662}
5663
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005664static int __btrfs_map_block(struct btrfs_fs_info *fs_info,
5665 enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005666 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005667 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005668 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005669{
5670 struct extent_map *em;
5671 struct map_lookup *map;
Chris Mason0b86a832008-03-24 15:01:56 -04005672 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005673 u64 stripe_offset;
5674 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005675 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005676 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005677 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005678 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005679 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005680 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005681 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005682 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005683 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5684 int dev_replace_is_ongoing = 0;
5685 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005686 int patch_the_first_stripe_for_dev_replace = 0;
5687 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005688 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005689
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005690 if (op == BTRFS_MAP_DISCARD)
5691 return __btrfs_map_block_for_discard(fs_info, logical,
5692 *length, bbio_ret);
5693
Liu Bo592d92e2017-03-14 13:33:55 -07005694 em = get_chunk_map(fs_info, logical, *length);
5695 if (IS_ERR(em))
5696 return PTR_ERR(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005697
Jeff Mahoney95617d62015-06-03 10:55:48 -04005698 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005699 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005700
David Woodhouse53b381b2013-01-29 18:40:14 -05005701 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005702 stripe_nr = offset;
5703 /*
5704 * stripe_nr counts the total number of stripes we have to stride
5705 * to get to this block
5706 */
David Sterba47c57132015-02-20 18:43:47 +01005707 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005708
David Woodhouse53b381b2013-01-29 18:40:14 -05005709 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005710 if (offset < stripe_offset) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005711 btrfs_crit(fs_info,
5712 "stripe math has gone wrong, stripe_offset=%llu, offset=%llu, start=%llu, logical=%llu, stripe_len=%llu",
Josef Bacike042d1e2016-04-12 12:54:40 -04005713 stripe_offset, offset, em->start, logical,
5714 stripe_len);
5715 free_extent_map(em);
5716 return -EINVAL;
5717 }
Chris Mason593060d2008-03-25 16:50:33 -04005718
5719 /* stripe_offset is the offset of this block in its stripe*/
5720 stripe_offset = offset - stripe_offset;
5721
David Woodhouse53b381b2013-01-29 18:40:14 -05005722 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005723 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005724 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5725 raid56_full_stripe_start = offset;
5726
5727 /* allow a write of a full stripe, but make sure we don't
5728 * allow straddling of stripes
5729 */
David Sterba47c57132015-02-20 18:43:47 +01005730 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5731 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005732 raid56_full_stripe_start *= full_stripe_len;
5733 }
5734
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005735 if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005736 u64 max_len;
5737 /* For writes to RAID[56], allow a full stripeset across all disks.
5738 For other RAID types and for RAID[56] reads, just allow a single
5739 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005740 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005741 (op == BTRFS_MAP_WRITE)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005742 max_len = stripe_len * nr_data_stripes(map) -
5743 (offset - raid56_full_stripe_start);
5744 } else {
5745 /* we limit the length of each bio to what fits in a stripe */
5746 max_len = stripe_len - stripe_offset;
5747 }
5748 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005749 } else {
5750 *length = em->len - offset;
5751 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005752
David Woodhouse53b381b2013-01-29 18:40:14 -05005753 /* This is for when we're called from btrfs_merge_bio_hook() and all
5754 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005755 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005756 goto out;
5757
David Sterba7e79cb82018-03-24 02:11:38 +01005758 btrfs_dev_replace_read_lock(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005759 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5760 if (!dev_replace_is_ongoing)
David Sterba7e79cb82018-03-24 02:11:38 +01005761 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005762 else
5763 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005764
Stefan Behrensad6d6202012-11-06 15:06:47 +01005765 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005766 !need_full_stripe(op) && dev_replace->tgtdev != NULL) {
Liu Bo5ab56092017-03-14 13:33:57 -07005767 ret = get_extra_mirror_from_replace(fs_info, logical, *length,
5768 dev_replace->srcdev->devid,
5769 &mirror_num,
5770 &physical_to_patch_in_first_stripe);
5771 if (ret)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005772 goto out;
Liu Bo5ab56092017-03-14 13:33:57 -07005773 else
5774 patch_the_first_stripe_for_dev_replace = 1;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005775 } else if (mirror_num > map->num_stripes) {
5776 mirror_num = 0;
5777 }
5778
Chris Masonf2d8d742008-04-21 10:03:05 -04005779 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005780 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005781 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
David Sterba47c57132015-02-20 18:43:47 +01005782 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5783 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005784 if (!need_full_stripe(op))
Miao Xie28e1cc72014-09-12 18:44:02 +08005785 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005786 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Anand Jainde483732017-10-12 16:43:00 +08005787 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005788 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005789 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005790 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005791 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005792 stripe_index = find_live_mirror(fs_info, map, 0,
Stefan Behrens30d98612012-11-06 14:52:18 +01005793 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005794 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005795 }
Chris Mason2fff7342008-04-29 14:12:09 -04005796
Chris Mason611f0e02008-04-03 16:29:03 -04005797 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Anand Jainde483732017-10-12 16:43:00 +08005798 if (need_full_stripe(op)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005799 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005800 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005801 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005802 } else {
5803 mirror_num = 1;
5804 }
Chris Mason2fff7342008-04-29 14:12:09 -04005805
Chris Mason321aecc2008-04-16 10:49:51 -04005806 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005807 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005808
David Sterba47c57132015-02-20 18:43:47 +01005809 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005810 stripe_index *= map->sub_stripes;
5811
Anand Jainde483732017-10-12 16:43:00 +08005812 if (need_full_stripe(op))
Chris Masonf2d8d742008-04-21 10:03:05 -04005813 num_stripes = map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005814 else if (mirror_num)
5815 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005816 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005817 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005818 stripe_index = find_live_mirror(fs_info, map,
5819 stripe_index,
Stefan Behrens30d98612012-11-06 14:52:18 +01005820 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005821 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005822 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005823
Zhao Leiffe2d202015-01-20 15:11:44 +08005824 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Anand Jainde483732017-10-12 16:43:00 +08005825 if (need_raid_map && (need_full_stripe(op) || mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005826 /* push stripe_nr back to the start of the full stripe */
Liu Bo42c61ab2017-04-03 13:45:24 -07005827 stripe_nr = div64_u64(raid56_full_stripe_start,
David Sterbab8b93ad2015-01-16 17:26:13 +01005828 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005829
5830 /* RAID[56] write or recovery. Return all stripes */
5831 num_stripes = map->num_stripes;
5832 max_errors = nr_parity_stripes(map);
5833
David Woodhouse53b381b2013-01-29 18:40:14 -05005834 *length = map->stripe_len;
5835 stripe_index = 0;
5836 stripe_offset = 0;
5837 } else {
5838 /*
5839 * Mirror #0 or #1 means the original data block.
5840 * Mirror #2 is RAID5 parity block.
5841 * Mirror #3 is RAID6 Q block.
5842 */
David Sterba47c57132015-02-20 18:43:47 +01005843 stripe_nr = div_u64_rem(stripe_nr,
5844 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005845 if (mirror_num > 1)
5846 stripe_index = nr_data_stripes(map) +
5847 mirror_num - 2;
5848
5849 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005850 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5851 &stripe_index);
Anand Jainde483732017-10-12 16:43:00 +08005852 if (!need_full_stripe(op) && mirror_num <= 1)
Miao Xie28e1cc72014-09-12 18:44:02 +08005853 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005854 }
Chris Mason8790d502008-04-03 16:29:03 -04005855 } else {
5856 /*
David Sterba47c57132015-02-20 18:43:47 +01005857 * after this, stripe_nr is the number of stripes on this
5858 * device we have to walk to find the data, and stripe_index is
5859 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005860 */
David Sterba47c57132015-02-20 18:43:47 +01005861 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5862 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005863 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005864 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005865 if (stripe_index >= map->num_stripes) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04005866 btrfs_crit(fs_info,
5867 "stripe index math went horribly wrong, got stripe_index=%u, num_stripes=%u",
Josef Bacike042d1e2016-04-12 12:54:40 -04005868 stripe_index, map->num_stripes);
5869 ret = -EINVAL;
5870 goto out;
5871 }
Chris Mason593060d2008-03-25 16:50:33 -04005872
Stefan Behrens472262f2012-11-06 14:43:46 +01005873 num_alloc_stripes = num_stripes;
Liu Bo6fad8232017-03-14 13:33:59 -07005874 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL) {
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005875 if (op == BTRFS_MAP_WRITE)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005876 num_alloc_stripes <<= 1;
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005877 if (op == BTRFS_MAP_GET_READ_MIRRORS)
Stefan Behrensad6d6202012-11-06 15:06:47 +01005878 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005879 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005880 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005881
Zhao Lei6e9606d2015-01-20 15:11:34 +08005882 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005883 if (!bbio) {
5884 ret = -ENOMEM;
5885 goto out;
5886 }
Liu Bo6fad8232017-03-14 13:33:59 -07005887 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL)
Miao Xie2c8cdd62014-11-14 16:06:25 +08005888 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005889
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005890 /* build raid_map */
Liu Bo2b19a1f2017-03-14 13:34:00 -07005891 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK && need_raid_map &&
5892 (need_full_stripe(op) || mirror_num > 1)) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005893 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005894 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005895
5896 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5897 sizeof(struct btrfs_bio_stripe) *
5898 num_alloc_stripes +
5899 sizeof(int) * tgtdev_indexes);
5900
5901 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005902 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005903
5904 /* Fill in the logical address of each stripe */
5905 tmp = stripe_nr * nr_data_stripes(map);
5906 for (i = 0; i < nr_data_stripes(map); i++)
5907 bbio->raid_map[(i+rot) % num_stripes] =
5908 em->start + (tmp + i) * map->stripe_len;
5909
5910 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5911 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5912 bbio->raid_map[(i+rot+1) % num_stripes] =
5913 RAID6_Q_STRIPE;
5914 }
5915
Li Zefanec9ef7a2011-12-01 14:06:42 +08005916
Liu Bo0b3d4cd2017-03-14 13:33:56 -07005917 for (i = 0; i < num_stripes; i++) {
5918 bbio->stripes[i].physical =
5919 map->stripes[stripe_index].physical +
5920 stripe_offset +
5921 stripe_nr * map->stripe_len;
5922 bbio->stripes[i].dev =
5923 map->stripes[stripe_index].dev;
5924 stripe_index++;
Chris Mason593060d2008-03-25 16:50:33 -04005925 }
Li Zefande11cc12011-12-01 12:55:47 +08005926
Liu Bo2b19a1f2017-03-14 13:34:00 -07005927 if (need_full_stripe(op))
Miao Xied20983b2014-07-03 18:22:13 +08005928 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005929
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005930 if (bbio->raid_map)
5931 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005932
Liu Bo73c0f222017-03-14 13:33:58 -07005933 if (dev_replace_is_ongoing && dev_replace->tgtdev != NULL &&
Liu Bo2b19a1f2017-03-14 13:34:00 -07005934 need_full_stripe(op)) {
Liu Bo73c0f222017-03-14 13:33:58 -07005935 handle_ops_on_dev_replace(op, &bbio, dev_replace, &num_stripes,
5936 &max_errors);
Stefan Behrens472262f2012-11-06 14:43:46 +01005937 }
5938
Li Zefande11cc12011-12-01 12:55:47 +08005939 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005940 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005941 bbio->num_stripes = num_stripes;
5942 bbio->max_errors = max_errors;
5943 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005944
5945 /*
5946 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5947 * mirror_num == num_stripes + 1 && dev_replace target drive is
5948 * available as a mirror
5949 */
5950 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5951 WARN_ON(num_stripes > 1);
5952 bbio->stripes[0].dev = dev_replace->tgtdev;
5953 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5954 bbio->mirror_num = map->num_stripes + 1;
5955 }
Chris Masoncea9e442008-04-09 16:28:12 -04005956out:
Liu Bo73beece2015-07-17 16:49:19 +08005957 if (dev_replace_is_ongoing) {
5958 btrfs_dev_replace_clear_lock_blocking(dev_replace);
David Sterba7e79cb82018-03-24 02:11:38 +01005959 btrfs_dev_replace_read_unlock(dev_replace);
Liu Bo73beece2015-07-17 16:49:19 +08005960 }
Chris Mason0b86a832008-03-24 15:01:56 -04005961 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005962 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005963}
5964
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005965int btrfs_map_block(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Chris Masonf2d8d742008-04-21 10:03:05 -04005966 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005967 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005968{
Mike Christieb3d3fa52016-06-05 14:31:53 -05005969 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005970 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005971}
5972
Miao Xieaf8e2d12014-10-23 14:42:50 +08005973/* For Scrub/replace */
Christoph Hellwigcf8cddd2016-10-27 09:27:36 +02005974int btrfs_map_sblock(struct btrfs_fs_info *fs_info, enum btrfs_map_op op,
Miao Xieaf8e2d12014-10-23 14:42:50 +08005975 u64 logical, u64 *length,
David Sterba825ad4c2017-03-28 14:45:22 +02005976 struct btrfs_bio **bbio_ret)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005977{
David Sterba825ad4c2017-03-28 14:45:22 +02005978 return __btrfs_map_block(fs_info, op, logical, length, bbio_ret, 0, 1);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005979}
5980
Nikolay Borisov63a9c7b2018-05-04 10:53:05 +03005981int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
5982 u64 physical, u64 **logical, int *naddrs, int *stripe_len)
Yan Zhenga512bbf2008-12-08 16:46:26 -05005983{
Yan Zhenga512bbf2008-12-08 16:46:26 -05005984 struct extent_map *em;
5985 struct map_lookup *map;
5986 u64 *buf;
5987 u64 bytenr;
5988 u64 length;
5989 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005990 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005991 int i, j, nr = 0;
5992
Liu Bo592d92e2017-03-14 13:33:55 -07005993 em = get_chunk_map(fs_info, chunk_start, 1);
5994 if (IS_ERR(em))
Josef Bacik835d9742013-03-19 12:13:25 -04005995 return -EIO;
Josef Bacik835d9742013-03-19 12:13:25 -04005996
Jeff Mahoney95617d62015-06-03 10:55:48 -04005997 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005998 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005999 rmap_len = map->stripe_len;
6000
Yan Zhenga512bbf2008-12-08 16:46:26 -05006001 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01006002 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006003 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01006004 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08006005 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01006006 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05006007 rmap_len = map->stripe_len * nr_data_stripes(map);
6008 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006009
David Sterba31e818f2015-02-20 18:00:26 +01006010 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006011 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05006012
6013 for (i = 0; i < map->num_stripes; i++) {
Yan Zhenga512bbf2008-12-08 16:46:26 -05006014 if (map->stripes[i].physical > physical ||
6015 map->stripes[i].physical + length <= physical)
6016 continue;
6017
6018 stripe_nr = physical - map->stripes[i].physical;
Liu Bo42c61ab2017-04-03 13:45:24 -07006019 stripe_nr = div64_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006020
6021 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
6022 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01006023 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006024 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
6025 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05006026 } /* else if RAID[56], multiply by nr_data_stripes().
6027 * Alternatively, just use rmap_len below instead of
6028 * map->stripe_len */
6029
6030 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05006031 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006032 for (j = 0; j < nr; j++) {
6033 if (buf[j] == bytenr)
6034 break;
6035 }
Chris Mason934d3752008-12-08 16:43:10 -05006036 if (j == nr) {
6037 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05006038 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05006039 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05006040 }
6041
Yan Zhenga512bbf2008-12-08 16:46:26 -05006042 *logical = buf;
6043 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05006044 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05006045
6046 free_extent_map(em);
6047 return 0;
6048}
6049
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006050static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08006051{
Mike Snitzer326e1db2015-05-22 09:14:03 -04006052 bio->bi_private = bbio->private;
6053 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006054 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04006055
Zhao Lei6e9606d2015-01-20 15:11:34 +08006056 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08006057}
6058
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006059static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04006060{
Chris Mason9be33952013-05-17 18:30:14 -04006061 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006062 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04006063
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006064 if (bio->bi_status) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02006065 atomic_inc(&bbio->error);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006066 if (bio->bi_status == BLK_STS_IOERR ||
6067 bio->bi_status == BLK_STS_TARGET) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02006068 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04006069 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08006070 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02006071
6072 BUG_ON(stripe_index >= bbio->num_stripes);
6073 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02006074 if (dev->bdev) {
Mike Christie37226b22016-06-05 14:31:52 -05006075 if (bio_op(bio) == REQ_OP_WRITE)
Anand Jain1cb34c82017-10-21 01:45:33 +08006076 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006077 BTRFS_DEV_STAT_WRITE_ERRS);
6078 else
Anand Jain1cb34c82017-10-21 01:45:33 +08006079 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006080 BTRFS_DEV_STAT_READ_ERRS);
Christoph Hellwig70fd7612016-11-01 07:40:10 -06006081 if (bio->bi_opf & REQ_PREFLUSH)
Anand Jain1cb34c82017-10-21 01:45:33 +08006082 btrfs_dev_stat_inc_and_print(dev,
Stefan Behrens597a60f2012-06-14 16:42:31 +02006083 BTRFS_DEV_STAT_FLUSH_ERRS);
Stefan Behrens597a60f2012-06-14 16:42:31 +02006084 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02006085 }
6086 }
Chris Mason8790d502008-04-03 16:29:03 -04006087
Jan Schmidta1d3c472011-08-04 17:15:33 +02006088 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04006089 is_orig_bio = 1;
6090
Miao Xiec404e0d2014-01-30 16:46:55 +08006091 btrfs_bio_counter_dec(bbio->fs_info);
6092
Jan Schmidta1d3c472011-08-04 17:15:33 +02006093 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04006094 if (!is_orig_bio) {
6095 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02006096 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04006097 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07006098
Chris Mason9be33952013-05-17 18:30:14 -04006099 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04006100 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05006101 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04006102 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02006103 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02006104 bio->bi_status = BLK_STS_IOERR;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05006105 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04006106 /*
6107 * this bio is actually up to date, we didn't
6108 * go over the max number of errors
6109 */
Anand Jain2dbe0c72017-10-14 08:35:56 +08006110 bio->bi_status = BLK_STS_OK;
Chris Mason1259ab72008-05-12 13:39:03 -04006111 }
Miao Xiec55f1392014-06-19 10:42:54 +08006112
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006113 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04006114 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04006115 bio_put(bio);
6116 }
Chris Mason8790d502008-04-03 16:29:03 -04006117}
6118
Chris Mason8b712842008-06-11 16:50:36 -04006119/*
6120 * see run_scheduled_bios for a description of why bios are collected for
6121 * async submit.
6122 *
6123 * This will add one bio to the pending list for a device and make sure
6124 * the work struct is scheduled.
6125 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006126static noinline void btrfs_schedule_bio(struct btrfs_device *device,
Mike Christie4e49ea42016-06-05 14:31:41 -05006127 struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04006128{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006129 struct btrfs_fs_info *fs_info = device->fs_info;
Chris Mason8b712842008-06-11 16:50:36 -04006130 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04006131 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006132
Anand Jaine6e674b2017-12-04 12:54:54 +08006133 if (test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state) ||
6134 !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006135 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05006136 return;
6137 }
6138
Chris Mason8b712842008-06-11 16:50:36 -04006139 /* don't bother with additional async steps for reads, right now */
Mike Christie37226b22016-06-05 14:31:52 -05006140 if (bio_op(bio) == REQ_OP_READ) {
Mike Christie4e49ea42016-06-05 14:31:41 -05006141 btrfsic_submit_bio(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01006142 return;
Chris Mason8b712842008-06-11 16:50:36 -04006143 }
6144
Chris Mason492bb6de2008-07-31 16:29:02 -04006145 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006146 bio->bi_next = NULL;
Chris Mason8b712842008-06-11 16:50:36 -04006147
6148 spin_lock(&device->io_lock);
Christoph Hellwig67f055c2016-11-01 07:40:06 -06006149 if (op_is_sync(bio->bi_opf))
Chris Masonffbd5172009-04-20 15:50:09 -04006150 pending_bios = &device->pending_sync_bios;
6151 else
6152 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006153
Chris Masonffbd5172009-04-20 15:50:09 -04006154 if (pending_bios->tail)
6155 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006156
Chris Masonffbd5172009-04-20 15:50:09 -04006157 pending_bios->tail = bio;
6158 if (!pending_bios->head)
6159 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006160 if (device->running_pending)
6161 should_queue = 0;
6162
6163 spin_unlock(&device->io_lock);
6164
6165 if (should_queue)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006166 btrfs_queue_work(fs_info->submit_workers, &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006167}
6168
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006169static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6170 u64 physical, int dev_nr, int async)
Josef Bacikde1ee922012-10-19 16:50:56 -04006171{
6172 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006173 struct btrfs_fs_info *fs_info = bbio->fs_info;
Josef Bacikde1ee922012-10-19 16:50:56 -04006174
6175 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006176 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006177 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006178 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006179#ifdef DEBUG
6180 {
6181 struct rcu_string *name;
6182
6183 rcu_read_lock();
6184 name = rcu_dereference(dev->name);
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006185 btrfs_debug(fs_info,
6186 "btrfs_map_bio: rw %d 0x%x, sector=%llu, dev=%lu (%s id %llu), size=%u",
6187 bio_op(bio), bio->bi_opf,
6188 (u64)bio->bi_iter.bi_sector,
6189 (u_long)dev->bdev->bd_dev, name->str, dev->devid,
6190 bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006191 rcu_read_unlock();
6192 }
6193#endif
Christoph Hellwig74d46992017-08-23 19:10:32 +02006194 bio_set_dev(bio, dev->bdev);
Miao Xiec404e0d2014-01-30 16:46:55 +08006195
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006196 btrfs_bio_counter_inc_noblocked(fs_info);
Miao Xiec404e0d2014-01-30 16:46:55 +08006197
Josef Bacikde1ee922012-10-19 16:50:56 -04006198 if (async)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006199 btrfs_schedule_bio(dev, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006200 else
Mike Christie4e49ea42016-06-05 14:31:41 -05006201 btrfsic_submit_bio(bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006202}
6203
Josef Bacikde1ee922012-10-19 16:50:56 -04006204static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6205{
6206 atomic_inc(&bbio->error);
6207 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Nicholas D Steeves01327612016-05-19 21:18:45 -04006208 /* Should be the original bio. */
Miao Xie8408c712014-06-19 10:42:55 +08006209 WARN_ON(bio != bbio->orig_bio);
6210
Chris Mason9be33952013-05-17 18:30:14 -04006211 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006212 bio->bi_iter.bi_sector = logical >> 9;
Anand Jain102ed2c2017-10-14 08:34:02 +08006213 if (atomic_read(&bbio->error) > bbio->max_errors)
6214 bio->bi_status = BLK_STS_IOERR;
6215 else
6216 bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006217 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006218 }
6219}
6220
Omar Sandoval58efbc92017-08-22 23:45:59 -07006221blk_status_t btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6222 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006223{
Chris Mason0b86a832008-03-24 15:01:56 -04006224 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006225 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006226 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006227 u64 length = 0;
6228 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006229 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006230 int dev_nr;
6231 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006232 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006233
Kent Overstreet4f024f32013-10-11 15:44:27 -07006234 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006235 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006236
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006237 btrfs_bio_counter_inc_blocked(fs_info);
Liu Bobd7d63c2017-09-19 17:50:09 -06006238 ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical,
Mike Christie37226b22016-06-05 14:31:52 -05006239 &map_length, &bbio, mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006240 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006241 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006242 return errno_to_blk_status(ret);
Miao Xiec404e0d2014-01-30 16:46:55 +08006243 }
Chris Masoncea9e442008-04-09 16:28:12 -04006244
Jan Schmidta1d3c472011-08-04 17:15:33 +02006245 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006246 bbio->orig_bio = first_bio;
6247 bbio->private = first_bio->bi_private;
6248 bbio->end_io = first_bio->bi_end_io;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006249 bbio->fs_info = fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006250 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6251
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006252 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
Mike Christie37226b22016-06-05 14:31:52 -05006253 ((bio_op(bio) == REQ_OP_WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006254 /* In this case, map_length has been set to the length of
6255 a single stripe; not the whole write */
Mike Christie37226b22016-06-05 14:31:52 -05006256 if (bio_op(bio) == REQ_OP_WRITE) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006257 ret = raid56_parity_write(fs_info, bio, bbio,
6258 map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006259 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006260 ret = raid56_parity_recover(fs_info, bio, bbio,
6261 map_length, mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006262 }
Miao Xie42452152014-11-25 16:39:28 +08006263
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006264 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006265 return errno_to_blk_status(ret);
David Woodhouse53b381b2013-01-29 18:40:14 -05006266 }
6267
Chris Masoncea9e442008-04-09 16:28:12 -04006268 if (map_length < length) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006269 btrfs_crit(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006270 "mapping failed logical %llu bio len %llu len %llu",
6271 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006272 BUG();
6273 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006274
Zhao Lei08da7572015-02-12 15:42:16 +08006275 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006276 dev = bbio->stripes[dev_nr].dev;
Mike Christie37226b22016-06-05 14:31:52 -05006277 if (!dev || !dev->bdev ||
Anand Jainebbede42017-12-04 12:54:52 +08006278 (bio_op(first_bio) == REQ_OP_WRITE &&
6279 !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006280 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006281 continue;
6282 }
6283
David Sterba3aa8e072017-06-02 17:38:30 +02006284 if (dev_nr < total_devs - 1)
David Sterba8b6c1d52017-06-02 17:48:13 +02006285 bio = btrfs_bio_clone(first_bio);
David Sterba3aa8e072017-06-02 17:38:30 +02006286 else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006287 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006288
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006289 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6290 dev_nr, async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006291 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006292 btrfs_bio_counter_dec(fs_info);
Omar Sandoval58efbc92017-08-22 23:45:59 -07006293 return BLK_STS_OK;
Chris Mason0b86a832008-03-24 15:01:56 -04006294}
6295
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006296struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006297 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006298{
Yan Zheng2b820322008-11-17 21:11:30 -05006299 struct btrfs_device *device;
6300 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006301
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006302 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006303 while (cur_devices) {
6304 if (!fsid ||
Anand Jain44880fd2017-07-29 17:50:09 +08006305 !memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
David Sterba35c70102017-06-15 19:51:51 +02006306 device = find_device(cur_devices, devid, uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05006307 if (device)
6308 return device;
6309 }
6310 cur_devices = cur_devices->seed;
6311 }
6312 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006313}
6314
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006315static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006316 u64 devid, u8 *dev_uuid)
6317{
6318 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006319
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006320 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6321 if (IS_ERR(device))
Anand Jainadfb69a2017-10-11 12:46:18 +08006322 return device;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006323
6324 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006325 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006326 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006327
Anand Jaine6e674b2017-12-04 12:54:54 +08006328 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Chris Masoncd02dca2010-12-13 14:56:23 -05006329 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006330
Chris Masondfe25022008-05-13 13:46:40 -04006331 return device;
6332}
6333
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006334/**
6335 * btrfs_alloc_device - allocate struct btrfs_device
6336 * @fs_info: used only for generating a new devid, can be NULL if
6337 * devid is provided (i.e. @devid != NULL).
6338 * @devid: a pointer to devid for this device. If NULL a new devid
6339 * is generated.
6340 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6341 * is generated.
6342 *
6343 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
David Sterba48dae9c2017-10-30 18:10:25 +01006344 * on error. Returned struct is not linked onto any lists and must be
David Sterbaa425f9d2018-03-20 15:47:33 +01006345 * destroyed with btrfs_free_device.
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006346 */
6347struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6348 const u64 *devid,
6349 const u8 *uuid)
6350{
6351 struct btrfs_device *dev;
6352 u64 tmp;
6353
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306354 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006355 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006356
6357 dev = __alloc_device();
6358 if (IS_ERR(dev))
6359 return dev;
6360
6361 if (devid)
6362 tmp = *devid;
6363 else {
6364 int ret;
6365
6366 ret = find_next_devid(fs_info, &tmp);
6367 if (ret) {
David Sterbaa425f9d2018-03-20 15:47:33 +01006368 btrfs_free_device(dev);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006369 return ERR_PTR(ret);
6370 }
6371 }
6372 dev->devid = tmp;
6373
6374 if (uuid)
6375 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6376 else
6377 generate_random_uuid(dev->uuid);
6378
Liu Bo9e0af232014-08-15 23:36:53 +08006379 btrfs_init_work(&dev->work, btrfs_submit_helper,
6380 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006381
6382 return dev;
6383}
6384
Liu Boe06cd3d2016-06-03 12:05:15 -07006385/* Return -EIO if any error, otherwise return 0. */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006386static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006387 struct extent_buffer *leaf,
6388 struct btrfs_chunk *chunk, u64 logical)
6389{
6390 u64 length;
6391 u64 stripe_len;
6392 u16 num_stripes;
6393 u16 sub_stripes;
6394 u64 type;
6395
6396 length = btrfs_chunk_length(leaf, chunk);
6397 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6398 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6399 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
6400 type = btrfs_chunk_type(leaf, chunk);
6401
6402 if (!num_stripes) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006403 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006404 num_stripes);
6405 return -EIO;
6406 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006407 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6408 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006409 return -EIO;
6410 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006411 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6412 btrfs_err(fs_info, "invalid chunk sectorsize %u",
Liu Boe06cd3d2016-06-03 12:05:15 -07006413 btrfs_chunk_sector_size(leaf, chunk));
6414 return -EIO;
6415 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006416 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6417 btrfs_err(fs_info, "invalid chunk length %llu", length);
Liu Boe06cd3d2016-06-03 12:05:15 -07006418 return -EIO;
6419 }
6420 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006421 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006422 stripe_len);
6423 return -EIO;
6424 }
6425 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6426 type) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006427 btrfs_err(fs_info, "unrecognized chunk type: %llu",
Liu Boe06cd3d2016-06-03 12:05:15 -07006428 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6429 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6430 btrfs_chunk_type(leaf, chunk));
6431 return -EIO;
6432 }
6433 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
6434 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) ||
6435 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
6436 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
6437 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6438 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6439 num_stripes != 1)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006440 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006441 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6442 num_stripes, sub_stripes,
6443 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
6444 return -EIO;
6445 }
6446
6447 return 0;
6448}
6449
Anand Jain5a2b8e62017-10-09 11:07:45 +08006450static void btrfs_report_missing_device(struct btrfs_fs_info *fs_info,
Anand Jain2b902df2017-10-09 11:07:46 +08006451 u64 devid, u8 *uuid, bool error)
Anand Jain5a2b8e62017-10-09 11:07:45 +08006452{
Anand Jain2b902df2017-10-09 11:07:46 +08006453 if (error)
6454 btrfs_err_rl(fs_info, "devid %llu uuid %pU is missing",
6455 devid, uuid);
6456 else
6457 btrfs_warn_rl(fs_info, "devid %llu uuid %pU is missing",
6458 devid, uuid);
Anand Jain5a2b8e62017-10-09 11:07:45 +08006459}
6460
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
Chris Mason0b86a832008-03-24 15:01:56 -04006462 struct extent_buffer *leaf,
6463 struct btrfs_chunk *chunk)
6464{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006465 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04006466 struct map_lookup *map;
6467 struct extent_map *em;
6468 u64 logical;
6469 u64 length;
6470 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006471 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006472 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006473 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006474 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006475
Chris Masone17cade2008-04-15 15:41:47 -04006476 logical = key->offset;
6477 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006478 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
Liu Boe06cd3d2016-06-03 12:05:15 -07006479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006480 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
Liu Boe06cd3d2016-06-03 12:05:15 -07006481 if (ret)
6482 return ret;
Chris Masona061fc82008-05-07 11:43:44 -04006483
Chris Mason890871b2009-09-02 16:24:52 -04006484 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006485 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006486 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006487
6488 /* already mapped? */
6489 if (em && em->start <= logical && em->start + em->len > logical) {
6490 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006491 return 0;
6492 } else if (em) {
6493 free_extent_map(em);
6494 }
Chris Mason0b86a832008-03-24 15:01:56 -04006495
David Sterba172ddd62011-04-21 00:48:27 +02006496 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006497 if (!em)
6498 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006499 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006500 if (!map) {
6501 free_extent_map(em);
6502 return -ENOMEM;
6503 }
6504
Wang Shilong298a8f92014-06-19 10:42:52 +08006505 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006506 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006507 em->start = logical;
6508 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006509 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006510 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006511 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006512
Chris Mason593060d2008-03-25 16:50:33 -04006513 map->num_stripes = num_stripes;
6514 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6515 map->io_align = btrfs_chunk_io_align(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006516 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6517 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006518 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006519 for (i = 0; i < num_stripes; i++) {
6520 map->stripes[i].physical =
6521 btrfs_stripe_offset_nr(leaf, chunk, i);
6522 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006523 read_extent_buffer(leaf, uuid, (unsigned long)
6524 btrfs_stripe_dev_uuid_nr(chunk, i),
6525 BTRFS_UUID_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006526 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006527 uuid, NULL);
Jeff Mahoney3cdde222016-06-09 21:38:35 -04006528 if (!map->stripes[i].dev &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006529 !btrfs_test_opt(fs_info, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006530 free_extent_map(em);
Anand Jain2b902df2017-10-09 11:07:46 +08006531 btrfs_report_missing_device(fs_info, devid, uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006532 return -ENOENT;
Chris Mason593060d2008-03-25 16:50:33 -04006533 }
Chris Masondfe25022008-05-13 13:46:40 -04006534 if (!map->stripes[i].dev) {
6535 map->stripes[i].dev =
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006536 add_missing_dev(fs_info->fs_devices, devid,
6537 uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006538 if (IS_ERR(map->stripes[i].dev)) {
Chris Masondfe25022008-05-13 13:46:40 -04006539 free_extent_map(em);
Anand Jainadfb69a2017-10-11 12:46:18 +08006540 btrfs_err(fs_info,
6541 "failed to init missing dev %llu: %ld",
6542 devid, PTR_ERR(map->stripes[i].dev));
6543 return PTR_ERR(map->stripes[i].dev);
Chris Masondfe25022008-05-13 13:46:40 -04006544 }
Anand Jain2b902df2017-10-09 11:07:46 +08006545 btrfs_report_missing_device(fs_info, devid, uuid, false);
Chris Masondfe25022008-05-13 13:46:40 -04006546 }
Anand Jaine12c9622017-12-04 12:54:53 +08006547 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA,
6548 &(map->stripes[i].dev->dev_state));
6549
Chris Mason0b86a832008-03-24 15:01:56 -04006550 }
6551
Chris Mason890871b2009-09-02 16:24:52 -04006552 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006553 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006554 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006555 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006556 free_extent_map(em);
6557
6558 return 0;
6559}
6560
Jeff Mahoney143bede2012-03-01 14:56:26 +01006561static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006562 struct btrfs_dev_item *dev_item,
6563 struct btrfs_device *device)
6564{
6565 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006566
6567 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006568 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6569 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006570 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006571 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006572 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006573 device->type = btrfs_device_type(leaf, dev_item);
6574 device->io_align = btrfs_device_io_align(leaf, dev_item);
6575 device->io_width = btrfs_device_io_width(leaf, dev_item);
6576 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006577 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Anand Jain401e29c2017-12-04 12:54:55 +08006578 clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state);
Chris Mason0b86a832008-03-24 15:01:56 -04006579
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006580 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006581 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006582}
6583
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006584static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
Miao Xie5f375832014-09-03 21:35:46 +08006585 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006586{
6587 struct btrfs_fs_devices *fs_devices;
6588 int ret;
6589
David Sterbaa32bf9a2018-03-16 02:21:22 +01006590 lockdep_assert_held(&uuid_mutex);
David Sterba2dfeca92017-06-14 02:48:07 +02006591 ASSERT(fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05006592
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006593 fs_devices = fs_info->fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05006594 while (fs_devices) {
Anand Jain44880fd2017-07-29 17:50:09 +08006595 if (!memcmp(fs_devices->fsid, fsid, BTRFS_FSID_SIZE))
Miao Xie5f375832014-09-03 21:35:46 +08006596 return fs_devices;
6597
Yan Zheng2b820322008-11-17 21:11:30 -05006598 fs_devices = fs_devices->seed;
6599 }
6600
6601 fs_devices = find_fsid(fsid);
6602 if (!fs_devices) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006603 if (!btrfs_test_opt(fs_info, DEGRADED))
Miao Xie5f375832014-09-03 21:35:46 +08006604 return ERR_PTR(-ENOENT);
6605
6606 fs_devices = alloc_fs_devices(fsid);
6607 if (IS_ERR(fs_devices))
6608 return fs_devices;
6609
6610 fs_devices->seeding = 1;
6611 fs_devices->opened = 1;
6612 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006613 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006614
6615 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006616 if (IS_ERR(fs_devices))
6617 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006618
Anand Jain897fb572018-04-12 10:29:28 +08006619 ret = open_fs_devices(fs_devices, FMODE_READ, fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006620 if (ret) {
6621 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006622 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006623 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006624 }
Yan Zheng2b820322008-11-17 21:11:30 -05006625
6626 if (!fs_devices->seeding) {
Anand Jain0226e0e2018-04-12 10:29:27 +08006627 close_fs_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006628 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006629 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006630 goto out;
6631 }
6632
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006633 fs_devices->seed = fs_info->fs_devices->seed;
6634 fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006635out:
Miao Xie5f375832014-09-03 21:35:46 +08006636 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006637}
6638
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006639static int read_one_dev(struct btrfs_fs_info *fs_info,
Chris Mason0b86a832008-03-24 15:01:56 -04006640 struct extent_buffer *leaf,
6641 struct btrfs_dev_item *dev_item)
6642{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006643 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006644 struct btrfs_device *device;
6645 u64 devid;
6646 int ret;
Anand Jain44880fd2017-07-29 17:50:09 +08006647 u8 fs_uuid[BTRFS_FSID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006648 u8 dev_uuid[BTRFS_UUID_SIZE];
6649
Chris Mason0b86a832008-03-24 15:01:56 -04006650 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006651 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006652 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006653 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Anand Jain44880fd2017-07-29 17:50:09 +08006654 BTRFS_FSID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05006655
Anand Jain44880fd2017-07-29 17:50:09 +08006656 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006657 fs_devices = open_seed_devices(fs_info, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006658 if (IS_ERR(fs_devices))
6659 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006660 }
6661
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006662 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006663 if (!device) {
Qu Wenruoc5502452017-03-09 09:34:42 +08006664 if (!btrfs_test_opt(fs_info, DEGRADED)) {
Anand Jain2b902df2017-10-09 11:07:46 +08006665 btrfs_report_missing_device(fs_info, devid,
6666 dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006667 return -ENOENT;
Qu Wenruoc5502452017-03-09 09:34:42 +08006668 }
Yan Zheng2b820322008-11-17 21:11:30 -05006669
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006670 device = add_missing_dev(fs_devices, devid, dev_uuid);
Anand Jainadfb69a2017-10-11 12:46:18 +08006671 if (IS_ERR(device)) {
6672 btrfs_err(fs_info,
6673 "failed to add missing dev %llu: %ld",
6674 devid, PTR_ERR(device));
6675 return PTR_ERR(device);
6676 }
Anand Jain2b902df2017-10-09 11:07:46 +08006677 btrfs_report_missing_device(fs_info, devid, dev_uuid, false);
Miao Xie5f375832014-09-03 21:35:46 +08006678 } else {
Qu Wenruoc5502452017-03-09 09:34:42 +08006679 if (!device->bdev) {
Anand Jain2b902df2017-10-09 11:07:46 +08006680 if (!btrfs_test_opt(fs_info, DEGRADED)) {
6681 btrfs_report_missing_device(fs_info,
6682 devid, dev_uuid, true);
Anand Jain45dbdbc2017-10-09 11:07:44 +08006683 return -ENOENT;
Anand Jain2b902df2017-10-09 11:07:46 +08006684 }
6685 btrfs_report_missing_device(fs_info, devid,
6686 dev_uuid, false);
Qu Wenruoc5502452017-03-09 09:34:42 +08006687 }
Miao Xie5f375832014-09-03 21:35:46 +08006688
Anand Jaine6e674b2017-12-04 12:54:54 +08006689 if (!device->bdev &&
6690 !test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state)) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006691 /*
6692 * this happens when a device that was properly setup
6693 * in the device info lists suddenly goes bad.
6694 * device->bdev is NULL, and so we have to set
6695 * device->missing to one here
6696 */
Miao Xie5f375832014-09-03 21:35:46 +08006697 device->fs_devices->missing_devices++;
Anand Jaine6e674b2017-12-04 12:54:54 +08006698 set_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state);
Yan Zheng2b820322008-11-17 21:11:30 -05006699 }
Miao Xie5f375832014-09-03 21:35:46 +08006700
6701 /* Move the device to its own fs_devices */
6702 if (device->fs_devices != fs_devices) {
Anand Jaine6e674b2017-12-04 12:54:54 +08006703 ASSERT(test_bit(BTRFS_DEV_STATE_MISSING,
6704 &device->dev_state));
Miao Xie5f375832014-09-03 21:35:46 +08006705
6706 list_move(&device->dev_list, &fs_devices->devices);
6707 device->fs_devices->num_devices--;
6708 fs_devices->num_devices++;
6709
6710 device->fs_devices->missing_devices--;
6711 fs_devices->missing_devices++;
6712
6713 device->fs_devices = fs_devices;
6714 }
Yan Zheng2b820322008-11-17 21:11:30 -05006715 }
6716
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006717 if (device->fs_devices != fs_info->fs_devices) {
Anand Jainebbede42017-12-04 12:54:52 +08006718 BUG_ON(test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state));
Yan Zheng2b820322008-11-17 21:11:30 -05006719 if (device->generation !=
6720 btrfs_device_generation(leaf, dev_item))
6721 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006722 }
Chris Mason0b86a832008-03-24 15:01:56 -04006723
6724 fill_device_from_item(leaf, dev_item, device);
Anand Jaine12c9622017-12-04 12:54:53 +08006725 set_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &device->dev_state);
Anand Jainebbede42017-12-04 12:54:52 +08006726 if (test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state) &&
Anand Jain401e29c2017-12-04 12:54:55 +08006727 !test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) {
Yan Zheng2b820322008-11-17 21:11:30 -05006728 device->fs_devices->total_rw_bytes += device->total_bytes;
Nikolay Borisova5ed45f2017-05-11 09:17:46 +03006729 atomic64_add(device->total_bytes - device->bytes_used,
6730 &fs_info->free_chunk_space);
Josef Bacik2bf64752011-09-26 17:12:22 -04006731 }
Chris Mason0b86a832008-03-24 15:01:56 -04006732 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006733 return ret;
6734}
6735
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006736int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006737{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04006738 struct btrfs_root *root = fs_info->tree_root;
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006739 struct btrfs_super_block *super_copy = fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006740 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006741 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006742 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006743 u8 *array_ptr;
6744 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006745 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006746 u32 num_stripes;
6747 u32 array_size;
6748 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006749 u32 cur_offset;
Liu Boe06cd3d2016-06-03 12:05:15 -07006750 u64 type;
Chris Mason84eed902008-04-25 09:04:37 -04006751 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006752
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006753 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
David Sterbaa83fffb2014-06-15 02:39:54 +02006754 /*
6755 * This will create extent buffer of nodesize, superblock size is
6756 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6757 * overallocate but we can keep it as-is, only the first page is used.
6758 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006759 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
Liu Boc871b0f2016-06-06 12:01:23 -07006760 if (IS_ERR(sb))
6761 return PTR_ERR(sb);
David Sterba4db8c522015-12-03 13:06:46 +01006762 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006763 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006764 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04006765 * The sb extent buffer is artificial and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006766 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006767 * pages up-to-date when the page is larger: extent does not cover the
6768 * whole page and consequently check_page_uptodate does not find all
6769 * the page's extents up-to-date (the hole beyond sb),
6770 * write_extent_buffer then triggers a WARN_ON.
6771 *
6772 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6773 * but sb spans only this function. Add an explicit SetPageUptodate call
6774 * to silence the warning eg. on PowerPC 64.
6775 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006776 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006777 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006778
Chris Masona061fc82008-05-07 11:43:44 -04006779 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006780 array_size = btrfs_super_sys_array_size(super_copy);
6781
David Sterba1ffb22c2014-10-31 19:02:42 +01006782 array_ptr = super_copy->sys_chunk_array;
6783 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6784 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006785
David Sterba1ffb22c2014-10-31 19:02:42 +01006786 while (cur_offset < array_size) {
6787 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006788 len = sizeof(*disk_key);
6789 if (cur_offset + len > array_size)
6790 goto out_short_read;
6791
Chris Mason0b86a832008-03-24 15:01:56 -04006792 btrfs_disk_key_to_cpu(&key, disk_key);
6793
David Sterba1ffb22c2014-10-31 19:02:42 +01006794 array_ptr += len;
6795 sb_array_offset += len;
6796 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006797
Chris Mason0d81ba52008-03-24 15:02:07 -04006798 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006799 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006800 /*
6801 * At least one btrfs_chunk with one stripe must be
6802 * present, exact stripe count check comes afterwards
6803 */
6804 len = btrfs_chunk_item_size(1);
6805 if (cur_offset + len > array_size)
6806 goto out_short_read;
6807
6808 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006809 if (!num_stripes) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006810 btrfs_err(fs_info,
6811 "invalid number of stripes %u in sys_array at offset %u",
David Sterbaf5cdedd2015-11-30 17:27:06 +01006812 num_stripes, cur_offset);
6813 ret = -EIO;
6814 break;
6815 }
6816
Liu Boe06cd3d2016-06-03 12:05:15 -07006817 type = btrfs_chunk_type(sb, chunk);
6818 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) == 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006819 btrfs_err(fs_info,
Liu Boe06cd3d2016-06-03 12:05:15 -07006820 "invalid chunk type %llu in sys_array at offset %u",
6821 type, cur_offset);
6822 ret = -EIO;
6823 break;
6824 }
6825
David Sterbae3540ea2014-11-05 15:24:51 +01006826 len = btrfs_chunk_item_size(num_stripes);
6827 if (cur_offset + len > array_size)
6828 goto out_short_read;
6829
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006830 ret = read_one_chunk(fs_info, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006831 if (ret)
6832 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006833 } else {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006834 btrfs_err(fs_info,
6835 "unexpected item type %u in sys_array at offset %u",
6836 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006837 ret = -EIO;
6838 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006839 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006840 array_ptr += len;
6841 sb_array_offset += len;
6842 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006843 }
Liu Bod8651772016-06-03 17:41:42 -07006844 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006845 free_extent_buffer_stale(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006846 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006847
6848out_short_read:
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04006849 btrfs_err(fs_info, "sys_array too short to read %u bytes at offset %u",
David Sterbae3540ea2014-11-05 15:24:51 +01006850 len, cur_offset);
Liu Bod8651772016-06-03 17:41:42 -07006851 clear_extent_buffer_uptodate(sb);
Liu Bo1c8b5b62016-05-13 17:06:59 -07006852 free_extent_buffer_stale(sb);
David Sterbae3540ea2014-11-05 15:24:51 +01006853 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006854}
6855
Qu Wenruo21634a12017-03-09 09:34:36 +08006856/*
6857 * Check if all chunks in the fs are OK for read-write degraded mount
6858 *
Anand Jain6528b992017-12-18 17:08:59 +08006859 * If the @failing_dev is specified, it's accounted as missing.
6860 *
Qu Wenruo21634a12017-03-09 09:34:36 +08006861 * Return true if all chunks meet the minimal RW mount requirements.
6862 * Return false if any chunk doesn't meet the minimal RW mount requirements.
6863 */
Anand Jain6528b992017-12-18 17:08:59 +08006864bool btrfs_check_rw_degradable(struct btrfs_fs_info *fs_info,
6865 struct btrfs_device *failing_dev)
Qu Wenruo21634a12017-03-09 09:34:36 +08006866{
6867 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6868 struct extent_map *em;
6869 u64 next_start = 0;
6870 bool ret = true;
6871
6872 read_lock(&map_tree->map_tree.lock);
6873 em = lookup_extent_mapping(&map_tree->map_tree, 0, (u64)-1);
6874 read_unlock(&map_tree->map_tree.lock);
6875 /* No chunk at all? Return false anyway */
6876 if (!em) {
6877 ret = false;
6878 goto out;
6879 }
6880 while (em) {
6881 struct map_lookup *map;
6882 int missing = 0;
6883 int max_tolerated;
6884 int i;
6885
6886 map = em->map_lookup;
6887 max_tolerated =
6888 btrfs_get_num_tolerated_disk_barrier_failures(
6889 map->type);
6890 for (i = 0; i < map->num_stripes; i++) {
6891 struct btrfs_device *dev = map->stripes[i].dev;
6892
Anand Jaine6e674b2017-12-04 12:54:54 +08006893 if (!dev || !dev->bdev ||
6894 test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state) ||
Qu Wenruo21634a12017-03-09 09:34:36 +08006895 dev->last_flush_error)
6896 missing++;
Anand Jain6528b992017-12-18 17:08:59 +08006897 else if (failing_dev && failing_dev == dev)
6898 missing++;
Qu Wenruo21634a12017-03-09 09:34:36 +08006899 }
6900 if (missing > max_tolerated) {
Anand Jain6528b992017-12-18 17:08:59 +08006901 if (!failing_dev)
6902 btrfs_warn(fs_info,
Qu Wenruo21634a12017-03-09 09:34:36 +08006903 "chunk %llu missing %d devices, max tolerance is %d for writeable mount",
6904 em->start, missing, max_tolerated);
6905 free_extent_map(em);
6906 ret = false;
6907 goto out;
6908 }
6909 next_start = extent_map_end(em);
6910 free_extent_map(em);
6911
6912 read_lock(&map_tree->map_tree.lock);
6913 em = lookup_extent_mapping(&map_tree->map_tree, next_start,
6914 (u64)(-1) - next_start);
6915 read_unlock(&map_tree->map_tree.lock);
6916 }
6917out:
6918 return ret;
6919}
6920
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006921int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04006922{
Jeff Mahoney5b4aace2016-06-21 10:40:19 -04006923 struct btrfs_root *root = fs_info->chunk_root;
Chris Mason0b86a832008-03-24 15:01:56 -04006924 struct btrfs_path *path;
6925 struct extent_buffer *leaf;
6926 struct btrfs_key key;
6927 struct btrfs_key found_key;
6928 int ret;
6929 int slot;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006930 u64 total_dev = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006931
Chris Mason0b86a832008-03-24 15:01:56 -04006932 path = btrfs_alloc_path();
6933 if (!path)
6934 return -ENOMEM;
6935
Anand Jain3dd0f7a2018-04-12 10:29:32 +08006936 /*
6937 * uuid_mutex is needed only if we are mounting a sprout FS
6938 * otherwise we don't need it.
6939 */
Li Zefanb367e472011-12-07 11:38:24 +08006940 mutex_lock(&uuid_mutex);
David Sterba34441362016-10-04 19:34:27 +02006941 mutex_lock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08006942
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006943 /*
6944 * Read all device items, and then all the chunk items. All
6945 * device items are found before any chunk item (their object id
6946 * is smaller than the lowest possible object id for a chunk
6947 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006948 */
6949 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6950 key.offset = 0;
6951 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006952 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006953 if (ret < 0)
6954 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006955 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006956 leaf = path->nodes[0];
6957 slot = path->slots[0];
6958 if (slot >= btrfs_header_nritems(leaf)) {
6959 ret = btrfs_next_leaf(root, path);
6960 if (ret == 0)
6961 continue;
6962 if (ret < 0)
6963 goto error;
6964 break;
6965 }
6966 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006967 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6968 struct btrfs_dev_item *dev_item;
6969 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006970 struct btrfs_dev_item);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006971 ret = read_one_dev(fs_info, leaf, dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006972 if (ret)
6973 goto error;
Liu Bo99e3ecf2016-06-03 12:05:14 -07006974 total_dev++;
Chris Mason0b86a832008-03-24 15:01:56 -04006975 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6976 struct btrfs_chunk *chunk;
6977 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006978 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006979 if (ret)
6980 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006981 }
6982 path->slots[0]++;
6983 }
Liu Bo99e3ecf2016-06-03 12:05:14 -07006984
6985 /*
6986 * After loading chunk tree, we've got all device information,
6987 * do another round of validation checks.
6988 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006989 if (total_dev != fs_info->fs_devices->total_devices) {
6990 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07006991 "super_num_devices %llu mismatch with num_devices %llu found here",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006992 btrfs_super_num_devices(fs_info->super_copy),
Liu Bo99e3ecf2016-06-03 12:05:14 -07006993 total_dev);
6994 ret = -EINVAL;
6995 goto error;
6996 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006997 if (btrfs_super_total_bytes(fs_info->super_copy) <
6998 fs_info->fs_devices->total_rw_bytes) {
6999 btrfs_err(fs_info,
Liu Bo99e3ecf2016-06-03 12:05:14 -07007000 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007001 btrfs_super_total_bytes(fs_info->super_copy),
7002 fs_info->fs_devices->total_rw_bytes);
Liu Bo99e3ecf2016-06-03 12:05:14 -07007003 ret = -EINVAL;
7004 goto error;
7005 }
Chris Mason0b86a832008-03-24 15:01:56 -04007006 ret = 0;
7007error:
David Sterba34441362016-10-04 19:34:27 +02007008 mutex_unlock(&fs_info->chunk_mutex);
Li Zefanb367e472011-12-07 11:38:24 +08007009 mutex_unlock(&uuid_mutex);
7010
Yan Zheng2b820322008-11-17 21:11:30 -05007011 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04007012 return ret;
7013}
Stefan Behrens442a4f62012-05-25 16:06:08 +02007014
Miao Xiecb517ea2013-05-15 07:48:19 +00007015void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
7016{
7017 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7018 struct btrfs_device *device;
7019
Liu Bo29cc83f2014-05-11 23:14:59 +08007020 while (fs_devices) {
7021 mutex_lock(&fs_devices->device_list_mutex);
7022 list_for_each_entry(device, &fs_devices->devices, dev_list)
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007023 device->fs_info = fs_info;
Liu Bo29cc83f2014-05-11 23:14:59 +08007024 mutex_unlock(&fs_devices->device_list_mutex);
7025
7026 fs_devices = fs_devices->seed;
7027 }
Miao Xiecb517ea2013-05-15 07:48:19 +00007028}
7029
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007030static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
7031{
7032 int i;
7033
7034 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7035 btrfs_dev_stat_reset(dev, i);
7036}
7037
7038int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
7039{
7040 struct btrfs_key key;
7041 struct btrfs_key found_key;
7042 struct btrfs_root *dev_root = fs_info->dev_root;
7043 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7044 struct extent_buffer *eb;
7045 int slot;
7046 int ret = 0;
7047 struct btrfs_device *device;
7048 struct btrfs_path *path = NULL;
7049 int i;
7050
7051 path = btrfs_alloc_path();
7052 if (!path) {
7053 ret = -ENOMEM;
7054 goto out;
7055 }
7056
7057 mutex_lock(&fs_devices->device_list_mutex);
7058 list_for_each_entry(device, &fs_devices->devices, dev_list) {
7059 int item_size;
7060 struct btrfs_dev_stats_item *ptr;
7061
David Sterba242e2952016-01-25 17:51:31 +01007062 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7063 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007064 key.offset = device->devid;
7065 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
7066 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007067 __btrfs_reset_dev_stats(device);
7068 device->dev_stats_valid = 1;
7069 btrfs_release_path(path);
7070 continue;
7071 }
7072 slot = path->slots[0];
7073 eb = path->nodes[0];
7074 btrfs_item_key_to_cpu(eb, &found_key, slot);
7075 item_size = btrfs_item_size_nr(eb, slot);
7076
7077 ptr = btrfs_item_ptr(eb, slot,
7078 struct btrfs_dev_stats_item);
7079
7080 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7081 if (item_size >= (1 + i) * sizeof(__le64))
7082 btrfs_dev_stat_set(device, i,
7083 btrfs_dev_stats_value(eb, ptr, i));
7084 else
7085 btrfs_dev_stat_reset(device, i);
7086 }
7087
7088 device->dev_stats_valid = 1;
7089 btrfs_dev_stat_print_on_load(device);
7090 btrfs_release_path(path);
7091 }
7092 mutex_unlock(&fs_devices->device_list_mutex);
7093
7094out:
7095 btrfs_free_path(path);
7096 return ret < 0 ? ret : 0;
7097}
7098
7099static int update_dev_stat_item(struct btrfs_trans_handle *trans,
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007100 struct btrfs_fs_info *fs_info,
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007101 struct btrfs_device *device)
7102{
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007103 struct btrfs_root *dev_root = fs_info->dev_root;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007104 struct btrfs_path *path;
7105 struct btrfs_key key;
7106 struct extent_buffer *eb;
7107 struct btrfs_dev_stats_item *ptr;
7108 int ret;
7109 int i;
7110
David Sterba242e2952016-01-25 17:51:31 +01007111 key.objectid = BTRFS_DEV_STATS_OBJECTID;
7112 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007113 key.offset = device->devid;
7114
7115 path = btrfs_alloc_path();
David Sterbafa252992017-02-15 09:35:01 +01007116 if (!path)
7117 return -ENOMEM;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007118 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
7119 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007120 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007121 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04007122 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007123 goto out;
7124 }
7125
7126 if (ret == 0 &&
7127 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
7128 /* need to delete old one and insert a new one */
7129 ret = btrfs_del_item(trans, dev_root, path);
7130 if (ret != 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007131 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007132 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04007133 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007134 goto out;
7135 }
7136 ret = 1;
7137 }
7138
7139 if (ret == 1) {
7140 /* need to insert a new item */
7141 btrfs_release_path(path);
7142 ret = btrfs_insert_empty_item(trans, dev_root, path,
7143 &key, sizeof(*ptr));
7144 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007145 btrfs_warn_in_rcu(fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007146 "insert dev_stats item for device %s failed %d",
7147 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007148 goto out;
7149 }
7150 }
7151
7152 eb = path->nodes[0];
7153 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
7154 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7155 btrfs_set_dev_stats_value(eb, ptr, i,
7156 btrfs_dev_stat_read(device, i));
7157 btrfs_mark_buffer_dirty(eb);
7158
7159out:
7160 btrfs_free_path(path);
7161 return ret;
7162}
7163
7164/*
7165 * called from commit_transaction. Writes all changed device stats to disk.
7166 */
7167int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
7168 struct btrfs_fs_info *fs_info)
7169{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007170 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7171 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08007172 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007173 int ret = 0;
7174
7175 mutex_lock(&fs_devices->device_list_mutex);
7176 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Nikolay Borisov9deae962017-10-24 13:47:37 +03007177 stats_cnt = atomic_read(&device->dev_stats_ccnt);
7178 if (!device->dev_stats_valid || stats_cnt == 0)
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007179 continue;
7180
Nikolay Borisov9deae962017-10-24 13:47:37 +03007181
7182 /*
7183 * There is a LOAD-LOAD control dependency between the value of
7184 * dev_stats_ccnt and updating the on-disk values which requires
7185 * reading the in-memory counters. Such control dependencies
7186 * require explicit read memory barriers.
7187 *
7188 * This memory barriers pairs with smp_mb__before_atomic in
7189 * btrfs_dev_stat_inc/btrfs_dev_stat_set and with the full
7190 * barrier implied by atomic_xchg in
7191 * btrfs_dev_stats_read_and_reset
7192 */
7193 smp_rmb();
7194
Jeff Mahoney6bccf3a2016-06-21 21:16:51 -04007195 ret = update_dev_stat_item(trans, fs_info, device);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007196 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08007197 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007198 }
7199 mutex_unlock(&fs_devices->device_list_mutex);
7200
7201 return ret;
7202}
7203
Stefan Behrens442a4f62012-05-25 16:06:08 +02007204void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
7205{
7206 btrfs_dev_stat_inc(dev, index);
7207 btrfs_dev_stat_print_on_error(dev);
7208}
7209
Eric Sandeen48a3b632013-04-25 20:41:01 +00007210static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02007211{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007212 if (!dev->dev_stats_valid)
7213 return;
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007214 btrfs_err_rl_in_rcu(dev->fs_info,
David Sterbab14af3b2015-10-08 10:43:10 +02007215 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007216 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02007217 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7218 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7219 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05007220 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7221 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02007222}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007223
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007224static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7225{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06007226 int i;
7227
7228 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7229 if (btrfs_dev_stat_read(dev, i) != 0)
7230 break;
7231 if (i == BTRFS_DEV_STAT_VALUES_MAX)
7232 return; /* all values == 0, suppress message */
7233
Jeff Mahoneyfb456252016-06-22 18:54:56 -04007234 btrfs_info_in_rcu(dev->fs_info,
David Sterbaecaeb142015-10-08 09:01:03 +02007235 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04007236 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007237 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
7238 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
7239 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
7240 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
7241 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7242}
7243
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007244int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
David Sterbab27f7c02012-06-22 06:30:39 -06007245 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007246{
7247 struct btrfs_device *dev;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007248 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007249 int i;
7250
7251 mutex_lock(&fs_devices->device_list_mutex);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007252 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007253 mutex_unlock(&fs_devices->device_list_mutex);
7254
7255 if (!dev) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007256 btrfs_warn(fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007257 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007258 } else if (!dev->dev_stats_valid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007259 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02007260 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06007261 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02007262 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
7263 if (stats->nr_items > i)
7264 stats->values[i] =
7265 btrfs_dev_stat_read_and_reset(dev, i);
7266 else
7267 btrfs_dev_stat_reset(dev, i);
7268 }
7269 } else {
7270 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
7271 if (stats->nr_items > i)
7272 stats->values[i] = btrfs_dev_stat_read(dev, i);
7273 }
7274 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
7275 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
7276 return 0;
7277}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007278
David Sterbada353f62017-02-14 17:55:53 +01007279void btrfs_scratch_superblocks(struct block_device *bdev, const char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007280{
7281 struct buffer_head *bh;
7282 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08007283 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007284
Anand Jain12b1c262015-08-14 18:32:59 +08007285 if (!bdev)
7286 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007287
Anand Jain12b1c262015-08-14 18:32:59 +08007288 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
7289 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007290
Anand Jain12b1c262015-08-14 18:32:59 +08007291 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
7292 continue;
7293
7294 disk_super = (struct btrfs_super_block *)bh->b_data;
7295
7296 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
7297 set_buffer_dirty(bh);
7298 sync_dirty_buffer(bh);
7299 brelse(bh);
7300 }
7301
7302 /* Notify udev that device has changed */
7303 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
7304
7305 /* Update ctime/mtime for device path for libblkid */
7306 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01007307}
Miao Xie935e5cc2014-09-03 21:35:33 +08007308
7309/*
7310 * Update the size of all devices, which is used for writing out the
7311 * super blocks.
7312 */
7313void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7314{
7315 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7316 struct btrfs_device *curr, *next;
7317
7318 if (list_empty(&fs_devices->resized_devices))
7319 return;
7320
7321 mutex_lock(&fs_devices->device_list_mutex);
David Sterba34441362016-10-04 19:34:27 +02007322 mutex_lock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007323 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
7324 resized_list) {
7325 list_del_init(&curr->resized_list);
7326 curr->commit_total_bytes = curr->disk_total_bytes;
7327 }
David Sterba34441362016-10-04 19:34:27 +02007328 mutex_unlock(&fs_info->chunk_mutex);
Miao Xie935e5cc2014-09-03 21:35:33 +08007329 mutex_unlock(&fs_devices->device_list_mutex);
7330}
Miao Xiece7213c2014-09-03 21:35:34 +08007331
7332/* Must be invoked during the transaction commit */
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007333void btrfs_update_commit_device_bytes_used(struct btrfs_transaction *trans)
Miao Xiece7213c2014-09-03 21:35:34 +08007334{
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007335 struct btrfs_fs_info *fs_info = trans->fs_info;
Miao Xiece7213c2014-09-03 21:35:34 +08007336 struct extent_map *em;
7337 struct map_lookup *map;
7338 struct btrfs_device *dev;
7339 int i;
7340
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007341 if (list_empty(&trans->pending_chunks))
Miao Xiece7213c2014-09-03 21:35:34 +08007342 return;
7343
7344 /* In order to kick the device replace finish process */
David Sterba34441362016-10-04 19:34:27 +02007345 mutex_lock(&fs_info->chunk_mutex);
Nikolay Borisove9b919b2018-02-07 17:55:49 +02007346 list_for_each_entry(em, &trans->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007347 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007348
7349 for (i = 0; i < map->num_stripes; i++) {
7350 dev = map->stripes[i].dev;
7351 dev->commit_bytes_used = dev->bytes_used;
7352 }
7353 }
David Sterba34441362016-10-04 19:34:27 +02007354 mutex_unlock(&fs_info->chunk_mutex);
Miao Xiece7213c2014-09-03 21:35:34 +08007355}
Anand Jain5a13f432015-03-10 06:38:31 +08007356
7357void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7358{
7359 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7360 while (fs_devices) {
7361 fs_devices->fs_info = fs_info;
7362 fs_devices = fs_devices->seed;
7363 }
7364}
7365
7366void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7367{
7368 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7369 while (fs_devices) {
7370 fs_devices->fs_info = NULL;
7371 fs_devices = fs_devices->seed;
7372 }
7373}