blob: e966f0ce07f80783c376e96533e12d3ce171b5fd [file] [log] [blame]
Chris Mason0b86a832008-03-24 15:01:56 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18#include <linux/sched.h>
19#include <linux/bio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Chris Mason8a4b83c2008-03-24 15:02:07 -040021#include <linux/buffer_head.h>
Chris Masonf2d8d742008-04-21 10:03:05 -040022#include <linux/blkdev.h>
Chris Mason788f20e2008-04-28 15:29:42 -040023#include <linux/random.h>
Chris Masonb765ead2009-04-03 10:27:10 -040024#include <linux/iocontext.h>
Ben Hutchings6f88a442010-12-29 14:55:03 +000025#include <linux/capability.h>
Stefan Behrens442a4f62012-05-25 16:06:08 +020026#include <linux/ratelimit.h>
Ilya Dryomov59641012012-01-16 22:04:48 +020027#include <linux/kthread.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050028#include <linux/raid/pq.h>
Stefan Behrens803b2f52013-08-15 17:11:21 +020029#include <linux/semaphore.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050030#include <asm/div64.h>
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "ctree.h"
32#include "extent_map.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "print-tree.h"
36#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050037#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040038#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010039#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040040#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060041#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010042#include "dev-replace.h"
Anand Jain99994cd2014-06-03 11:36:00 +080043#include "sysfs.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044
Zhao Leiaf902042015-09-15 21:08:06 +080045const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
46 [BTRFS_RAID_RAID10] = {
47 .sub_stripes = 2,
48 .dev_stripes = 1,
49 .devs_max = 0, /* 0 == as many as possible */
50 .devs_min = 4,
Zhao Lei8789f4f2015-09-15 21:08:07 +080051 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080052 .devs_increment = 2,
53 .ncopies = 2,
54 },
55 [BTRFS_RAID_RAID1] = {
56 .sub_stripes = 1,
57 .dev_stripes = 1,
58 .devs_max = 2,
59 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080060 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080061 .devs_increment = 2,
62 .ncopies = 2,
63 },
64 [BTRFS_RAID_DUP] = {
65 .sub_stripes = 1,
66 .dev_stripes = 2,
67 .devs_max = 1,
68 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080069 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080070 .devs_increment = 1,
71 .ncopies = 2,
72 },
73 [BTRFS_RAID_RAID0] = {
74 .sub_stripes = 1,
75 .dev_stripes = 1,
76 .devs_max = 0,
77 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080078 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080079 .devs_increment = 1,
80 .ncopies = 1,
81 },
82 [BTRFS_RAID_SINGLE] = {
83 .sub_stripes = 1,
84 .dev_stripes = 1,
85 .devs_max = 1,
86 .devs_min = 1,
Zhao Lei8789f4f2015-09-15 21:08:07 +080087 .tolerated_failures = 0,
Zhao Leiaf902042015-09-15 21:08:06 +080088 .devs_increment = 1,
89 .ncopies = 1,
90 },
91 [BTRFS_RAID_RAID5] = {
92 .sub_stripes = 1,
93 .dev_stripes = 1,
94 .devs_max = 0,
95 .devs_min = 2,
Zhao Lei8789f4f2015-09-15 21:08:07 +080096 .tolerated_failures = 1,
Zhao Leiaf902042015-09-15 21:08:06 +080097 .devs_increment = 1,
98 .ncopies = 2,
99 },
100 [BTRFS_RAID_RAID6] = {
101 .sub_stripes = 1,
102 .dev_stripes = 1,
103 .devs_max = 0,
104 .devs_min = 3,
Zhao Lei8789f4f2015-09-15 21:08:07 +0800105 .tolerated_failures = 2,
Zhao Leiaf902042015-09-15 21:08:06 +0800106 .devs_increment = 1,
107 .ncopies = 3,
108 },
109};
110
Colin Ian Kingfb75d852016-01-19 00:05:28 +0000111const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
Zhao Leiaf902042015-09-15 21:08:06 +0800112 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
113 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
114 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
115 [BTRFS_RAID_RAID0] = BTRFS_BLOCK_GROUP_RAID0,
116 [BTRFS_RAID_SINGLE] = 0,
117 [BTRFS_RAID_RAID5] = BTRFS_BLOCK_GROUP_RAID5,
118 [BTRFS_RAID_RAID6] = BTRFS_BLOCK_GROUP_RAID6,
119};
120
Yan Zheng2b820322008-11-17 21:11:30 -0500121static int init_first_rw_device(struct btrfs_trans_handle *trans,
122 struct btrfs_root *root,
123 struct btrfs_device *device);
124static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200125static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000126static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200127static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
David Sterba87ad58c2015-11-27 18:49:39 +0100128static void btrfs_close_one_device(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -0500129
Miao Xie67a2c452014-09-03 21:35:43 +0800130DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400131static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800132struct list_head *btrfs_get_fs_uuids(void)
133{
134 return &fs_uuids;
135}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136
Ilya Dryomov2208a372013-08-12 14:33:03 +0300137static struct btrfs_fs_devices *__alloc_fs_devices(void)
138{
139 struct btrfs_fs_devices *fs_devs;
140
David Sterba78f2c9e2016-02-11 14:25:38 +0100141 fs_devs = kzalloc(sizeof(*fs_devs), GFP_KERNEL);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300142 if (!fs_devs)
143 return ERR_PTR(-ENOMEM);
144
145 mutex_init(&fs_devs->device_list_mutex);
146
147 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800148 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300149 INIT_LIST_HEAD(&fs_devs->alloc_list);
150 INIT_LIST_HEAD(&fs_devs->list);
151
152 return fs_devs;
153}
154
155/**
156 * alloc_fs_devices - allocate struct btrfs_fs_devices
157 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
158 * generated.
159 *
160 * Return: a pointer to a new &struct btrfs_fs_devices on success;
161 * ERR_PTR() on error. Returned struct is not linked onto any lists and
162 * can be destroyed with kfree() right away.
163 */
164static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
165{
166 struct btrfs_fs_devices *fs_devs;
167
168 fs_devs = __alloc_fs_devices();
169 if (IS_ERR(fs_devs))
170 return fs_devs;
171
172 if (fsid)
173 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
174 else
175 generate_random_uuid(fs_devs->fsid);
176
177 return fs_devs;
178}
179
Yan Zhenge4404d62008-12-12 10:03:26 -0500180static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
181{
182 struct btrfs_device *device;
183 WARN_ON(fs_devices->opened);
184 while (!list_empty(&fs_devices->devices)) {
185 device = list_entry(fs_devices->devices.next,
186 struct btrfs_device, dev_list);
187 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400188 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500189 kfree(device);
190 }
191 kfree(fs_devices);
192}
193
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000194static void btrfs_kobject_uevent(struct block_device *bdev,
195 enum kobject_action action)
196{
197 int ret;
198
199 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
200 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500201 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000202 action,
203 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
204 &disk_to_dev(bdev->bd_disk)->kobj);
205}
206
Jeff Mahoney143bede2012-03-01 14:56:26 +0100207void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400208{
209 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400210
Yan Zheng2b820322008-11-17 21:11:30 -0500211 while (!list_empty(&fs_uuids)) {
212 fs_devices = list_entry(fs_uuids.next,
213 struct btrfs_fs_devices, list);
214 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500215 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400216 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400217}
218
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300219static struct btrfs_device *__alloc_device(void)
220{
221 struct btrfs_device *dev;
222
David Sterba78f2c9e2016-02-11 14:25:38 +0100223 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300224 if (!dev)
225 return ERR_PTR(-ENOMEM);
226
227 INIT_LIST_HEAD(&dev->dev_list);
228 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800229 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300230
231 spin_lock_init(&dev->io_lock);
232
233 spin_lock_init(&dev->reada_lock);
234 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800235 atomic_set(&dev->dev_stats_ccnt, 0);
Sebastian Andrzej Siewior546bed62016-01-15 14:37:15 +0100236 btrfs_device_data_ordered_init(dev);
Mel Gormand0164ad2015-11-06 16:28:21 -0800237 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
238 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300239
240 return dev;
241}
242
Chris Masona1b32a52008-09-05 16:09:51 -0400243static noinline struct btrfs_device *__find_device(struct list_head *head,
244 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400245{
246 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400247
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500248 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400249 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400250 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400251 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400252 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400253 }
254 return NULL;
255}
256
Chris Masona1b32a52008-09-05 16:09:51 -0400257static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400258{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400259 struct btrfs_fs_devices *fs_devices;
260
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500261 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400262 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
263 return fs_devices;
264 }
265 return NULL;
266}
267
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100268static int
269btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
270 int flush, struct block_device **bdev,
271 struct buffer_head **bh)
272{
273 int ret;
274
275 *bdev = blkdev_get_by_path(device_path, flags, holder);
276
277 if (IS_ERR(*bdev)) {
278 ret = PTR_ERR(*bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100279 goto error;
280 }
281
282 if (flush)
283 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
284 ret = set_blocksize(*bdev, 4096);
285 if (ret) {
286 blkdev_put(*bdev, flags);
287 goto error;
288 }
289 invalidate_bdev(*bdev);
290 *bh = btrfs_read_dev_super(*bdev);
Anand Jain92fc03f2015-08-14 18:32:51 +0800291 if (IS_ERR(*bh)) {
292 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100293 blkdev_put(*bdev, flags);
294 goto error;
295 }
296
297 return 0;
298
299error:
300 *bdev = NULL;
301 *bh = NULL;
302 return ret;
303}
304
Chris Masonffbd5172009-04-20 15:50:09 -0400305static void requeue_list(struct btrfs_pending_bios *pending_bios,
306 struct bio *head, struct bio *tail)
307{
308
309 struct bio *old_head;
310
311 old_head = pending_bios->head;
312 pending_bios->head = head;
313 if (pending_bios->tail)
314 tail->bi_next = old_head;
315 else
316 pending_bios->tail = tail;
317}
318
Chris Mason8b712842008-06-11 16:50:36 -0400319/*
320 * we try to collect pending bios for a device so we don't get a large
321 * number of procs sending bios down to the same device. This greatly
322 * improves the schedulers ability to collect and merge the bios.
323 *
324 * But, it also turns into a long list of bios to process and that is sure
325 * to eventually make the worker thread block. The solution here is to
326 * make some progress and then put this work struct back at the end of
327 * the list if the block device is congested. This way, multiple devices
328 * can make progress from a single worker thread.
329 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100330static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400331{
332 struct bio *pending;
333 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400334 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400335 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400336 struct bio *tail;
337 struct bio *cur;
338 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400339 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400340 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400341 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400342 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400343 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000344 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400345 struct blk_plug plug;
346
347 /*
348 * this function runs all the bios we've collected for
349 * a particular device. We don't want to wander off to
350 * another device without first sending all of these down.
351 * So, setup a plug here and finish it off before we return
352 */
353 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400354
Chris Masonbedf7622009-04-03 10:32:58 -0400355 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400356 fs_info = device->dev_root->fs_info;
357 limit = btrfs_async_submit_limit(fs_info);
358 limit = limit * 2 / 3;
359
Chris Mason8b712842008-06-11 16:50:36 -0400360loop:
361 spin_lock(&device->io_lock);
362
Chris Masona6837052009-02-04 09:19:41 -0500363loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400364 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400365
Chris Mason8b712842008-06-11 16:50:36 -0400366 /* take all the bios off the list at once and process them
367 * later on (without the lock held). But, remember the
368 * tail and other pointers so the bios can be properly reinserted
369 * into the list if we hit congestion
370 */
Chris Masond84275c2009-06-09 15:39:08 -0400371 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400372 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400373 force_reg = 1;
374 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400375 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400376 force_reg = 0;
377 }
Chris Masonffbd5172009-04-20 15:50:09 -0400378
379 pending = pending_bios->head;
380 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400381 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400382
383 /*
384 * if pending was null this time around, no bios need processing
385 * at all and we can stop. Otherwise it'll loop back up again
386 * and do an additional check so no bios are missed.
387 *
388 * device->running_pending is used to synchronize with the
389 * schedule_bio code.
390 */
Chris Masonffbd5172009-04-20 15:50:09 -0400391 if (device->pending_sync_bios.head == NULL &&
392 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400393 again = 0;
394 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400395 } else {
396 again = 1;
397 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400398 }
Chris Masonffbd5172009-04-20 15:50:09 -0400399
400 pending_bios->head = NULL;
401 pending_bios->tail = NULL;
402
Chris Mason8b712842008-06-11 16:50:36 -0400403 spin_unlock(&device->io_lock);
404
Chris Masond3977122009-01-05 21:25:51 -0500405 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400406
407 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400408 /* we want to work on both lists, but do more bios on the
409 * sync list than the regular list
410 */
411 if ((num_run > 32 &&
412 pending_bios != &device->pending_sync_bios &&
413 device->pending_sync_bios.head) ||
414 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
415 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400416 spin_lock(&device->io_lock);
417 requeue_list(pending_bios, pending, tail);
418 goto loop_lock;
419 }
420
Chris Mason8b712842008-06-11 16:50:36 -0400421 cur = pending;
422 pending = pending->bi_next;
423 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400424
David Sterbaee863952015-02-16 19:41:40 +0100425 /*
426 * atomic_dec_return implies a barrier for waitqueue_active
427 */
Josef Bacik66657b32012-08-01 15:36:24 -0400428 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400429 waitqueue_active(&fs_info->async_submit_wait))
430 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400431
Jens Axboedac56212015-04-17 16:23:59 -0600432 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400433
Chris Mason2ab1ba62011-08-04 14:28:36 -0400434 /*
435 * if we're doing the sync list, record that our
436 * plug has some sync requests on it
437 *
438 * If we're doing the regular list and there are
439 * sync requests sitting around, unplug before
440 * we add more
441 */
442 if (pending_bios == &device->pending_sync_bios) {
443 sync_pending = 1;
444 } else if (sync_pending) {
445 blk_finish_plug(&plug);
446 blk_start_plug(&plug);
447 sync_pending = 0;
448 }
449
Stefan Behrens21adbd52011-11-09 13:44:05 +0100450 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400451 num_run++;
452 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100453
454 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400455
456 /*
457 * we made progress, there is more work to do and the bdi
458 * is now congested. Back off and let other work structs
459 * run instead
460 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400461 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500462 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400463 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400464
Chris Masonb765ead2009-04-03 10:27:10 -0400465 ioc = current->io_context;
466
467 /*
468 * the main goal here is that we don't want to
469 * block if we're going to be able to submit
470 * more requests without blocking.
471 *
472 * This code does two great things, it pokes into
473 * the elevator code from a filesystem _and_
474 * it makes assumptions about how batching works.
475 */
476 if (ioc && ioc->nr_batch_requests > 0 &&
477 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
478 (last_waited == 0 ||
479 ioc->last_waited == last_waited)) {
480 /*
481 * we want to go through our batch of
482 * requests and stop. So, we copy out
483 * the ioc->last_waited time and test
484 * against it before looping
485 */
486 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100487 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400488 continue;
489 }
Chris Mason8b712842008-06-11 16:50:36 -0400490 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400491 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500492 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400493
494 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d42014-02-28 10:46:08 +0800495 btrfs_queue_work(fs_info->submit_workers,
496 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400497 goto done;
498 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500499 /* unplug every 64 requests just for good measure */
500 if (batch_run % 64 == 0) {
501 blk_finish_plug(&plug);
502 blk_start_plug(&plug);
503 sync_pending = 0;
504 }
Chris Mason8b712842008-06-11 16:50:36 -0400505 }
Chris Masonffbd5172009-04-20 15:50:09 -0400506
Chris Mason51684082010-03-10 15:33:32 -0500507 cond_resched();
508 if (again)
509 goto loop;
510
511 spin_lock(&device->io_lock);
512 if (device->pending_bios.head || device->pending_sync_bios.head)
513 goto loop_lock;
514 spin_unlock(&device->io_lock);
515
Chris Mason8b712842008-06-11 16:50:36 -0400516done:
Chris Mason211588a2011-04-19 20:12:40 -0400517 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400518}
519
Christoph Hellwigb2950862008-12-02 09:54:17 -0500520static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400521{
522 struct btrfs_device *device;
523
524 device = container_of(work, struct btrfs_device, work);
525 run_scheduled_bios(device);
526}
527
Anand Jain4fde46f2015-06-17 21:10:48 +0800528
529void btrfs_free_stale_device(struct btrfs_device *cur_dev)
530{
531 struct btrfs_fs_devices *fs_devs;
532 struct btrfs_device *dev;
533
534 if (!cur_dev->name)
535 return;
536
537 list_for_each_entry(fs_devs, &fs_uuids, list) {
538 int del = 1;
539
540 if (fs_devs->opened)
541 continue;
542 if (fs_devs->seeding)
543 continue;
544
545 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
546
547 if (dev == cur_dev)
548 continue;
549 if (!dev->name)
550 continue;
551
552 /*
553 * Todo: This won't be enough. What if the same device
554 * comes back (with new uuid and) with its mapper path?
555 * But for now, this does help as mostly an admin will
556 * either use mapper or non mapper path throughout.
557 */
558 rcu_read_lock();
559 del = strcmp(rcu_str_deref(dev->name),
560 rcu_str_deref(cur_dev->name));
561 rcu_read_unlock();
562 if (!del)
563 break;
564 }
565
566 if (!del) {
567 /* delete the stale device */
568 if (fs_devs->num_devices == 1) {
569 btrfs_sysfs_remove_fsid(fs_devs);
570 list_del(&fs_devs->list);
571 free_fs_devices(fs_devs);
572 } else {
573 fs_devs->num_devices--;
574 list_del(&dev->dev_list);
575 rcu_string_free(dev->name);
576 kfree(dev);
577 }
578 break;
579 }
580 }
581}
582
David Sterba60999ca2014-03-26 18:26:36 +0100583/*
584 * Add new device to list of registered devices
585 *
586 * Returns:
587 * 1 - first time device is seen
588 * 0 - device already known
589 * < 0 - error
590 */
Chris Masona1b32a52008-09-05 16:09:51 -0400591static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400592 struct btrfs_super_block *disk_super,
593 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
594{
595 struct btrfs_device *device;
596 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400597 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100598 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400599 u64 found_transid = btrfs_super_generation(disk_super);
600
601 fs_devices = find_fsid(disk_super->fsid);
602 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300603 fs_devices = alloc_fs_devices(disk_super->fsid);
604 if (IS_ERR(fs_devices))
605 return PTR_ERR(fs_devices);
606
Chris Mason8a4b83c2008-03-24 15:02:07 -0400607 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300608
Chris Mason8a4b83c2008-03-24 15:02:07 -0400609 device = NULL;
610 } else {
Chris Masona4437552008-04-18 10:29:38 -0400611 device = __find_device(&fs_devices->devices, devid,
612 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400613 }
Miao Xie443f24f2014-07-24 11:37:15 +0800614
Chris Mason8a4b83c2008-03-24 15:02:07 -0400615 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500616 if (fs_devices->opened)
617 return -EBUSY;
618
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300619 device = btrfs_alloc_device(NULL, &devid,
620 disk_super->dev_item.uuid);
621 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400622 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300623 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 }
Josef Bacik606686e2012-06-04 14:03:51 -0400625
626 name = rcu_string_strdup(path, GFP_NOFS);
627 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400628 kfree(device);
629 return -ENOMEM;
630 }
Josef Bacik606686e2012-06-04 14:03:51 -0400631 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200632
Chris Masone5e9a522009-06-10 15:17:02 -0400633 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000634 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100635 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400636 mutex_unlock(&fs_devices->device_list_mutex);
637
David Sterba60999ca2014-03-26 18:26:36 +0100638 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500639 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400640 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800641 /*
642 * When FS is already mounted.
643 * 1. If you are here and if the device->name is NULL that
644 * means this device was missing at time of FS mount.
645 * 2. If you are here and if the device->name is different
646 * from 'path' that means either
647 * a. The same device disappeared and reappeared with
648 * different name. or
649 * b. The missing-disk-which-was-replaced, has
650 * reappeared now.
651 *
652 * We must allow 1 and 2a above. But 2b would be a spurious
653 * and unintentional.
654 *
655 * Further in case of 1 and 2a above, the disk at 'path'
656 * would have missed some transaction when it was away and
657 * in case of 2a the stale bdev has to be updated as well.
658 * 2b must not be allowed at all time.
659 */
660
661 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700662 * For now, we do allow update to btrfs_fs_device through the
663 * btrfs dev scan cli after FS has been mounted. We're still
664 * tracking a problem where systems fail mount by subvolume id
665 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800666 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700667 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800668 /*
669 * That is if the FS is _not_ mounted and if you
670 * are here, that means there is more than one
671 * disk with same uuid and devid.We keep the one
672 * with larger generation number or the last-in if
673 * generation are equal.
674 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700675 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800676 }
Anand Jainb96de002014-07-03 18:22:05 +0800677
Josef Bacik606686e2012-06-04 14:03:51 -0400678 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000679 if (!name)
680 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400681 rcu_string_free(device->name);
682 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500683 if (device->missing) {
684 fs_devices->missing_devices--;
685 device->missing = 0;
686 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400687 }
688
Anand Jain77bdae42014-07-03 18:22:06 +0800689 /*
690 * Unmount does not free the btrfs_device struct but would zero
691 * generation along with most of the other members. So just update
692 * it back. We need it to pick the disk with largest generation
693 * (as above).
694 */
695 if (!fs_devices->opened)
696 device->generation = found_transid;
697
Anand Jain4fde46f2015-06-17 21:10:48 +0800698 /*
699 * if there is new btrfs on an already registered device,
700 * then remove the stale device entry.
701 */
Anand Jain02feae32016-02-13 10:01:40 +0800702 if (ret > 0)
703 btrfs_free_stale_device(device);
Anand Jain4fde46f2015-06-17 21:10:48 +0800704
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100706
707 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400708}
709
Yan Zhenge4404d62008-12-12 10:03:26 -0500710static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
711{
712 struct btrfs_fs_devices *fs_devices;
713 struct btrfs_device *device;
714 struct btrfs_device *orig_dev;
715
Ilya Dryomov2208a372013-08-12 14:33:03 +0300716 fs_devices = alloc_fs_devices(orig->fsid);
717 if (IS_ERR(fs_devices))
718 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500719
Miao Xieadbbb862014-09-03 21:35:42 +0800720 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400721 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500722
Xiao Guangrong46224702011-04-20 10:08:47 +0000723 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500724 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400725 struct rcu_string *name;
726
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300727 device = btrfs_alloc_device(NULL, &orig_dev->devid,
728 orig_dev->uuid);
729 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500730 goto error;
731
Josef Bacik606686e2012-06-04 14:03:51 -0400732 /*
733 * This is ok to do without rcu read locked because we hold the
734 * uuid mutex so nothing we touch in here is going to disappear.
735 */
Anand Jaine755f782014-06-30 17:12:47 +0800736 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100737 name = rcu_string_strdup(orig_dev->name->str,
738 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800739 if (!name) {
740 kfree(device);
741 goto error;
742 }
743 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400744 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500745
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 list_add(&device->dev_list, &fs_devices->devices);
747 device->fs_devices = fs_devices;
748 fs_devices->num_devices++;
749 }
Miao Xieadbbb862014-09-03 21:35:42 +0800750 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500751 return fs_devices;
752error:
Miao Xieadbbb862014-09-03 21:35:42 +0800753 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500754 free_fs_devices(fs_devices);
755 return ERR_PTR(-ENOMEM);
756}
757
Eric Sandeen9eaed212014-08-01 18:12:35 -0500758void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400759{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500760 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800761 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500762
Chris Masondfe25022008-05-13 13:46:40 -0400763 mutex_lock(&uuid_mutex);
764again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000765 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500766 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500767 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100768 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800769 (!latest_dev ||
770 device->generation > latest_dev->generation)) {
771 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500772 }
Yan Zheng2b820322008-11-17 21:11:30 -0500773 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500774 }
Yan Zheng2b820322008-11-17 21:11:30 -0500775
Stefan Behrens8dabb742012-11-06 13:15:27 +0100776 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
777 /*
778 * In the first step, keep the device which has
779 * the correct fsid and the devid that is used
780 * for the dev_replace procedure.
781 * In the second step, the dev_replace state is
782 * read from the device tree and it is known
783 * whether the procedure is really active or
784 * not, which means whether this device is
785 * used or whether it should be removed.
786 */
787 if (step == 0 || device->is_tgtdev_for_dev_replace) {
788 continue;
789 }
790 }
Yan Zheng2b820322008-11-17 21:11:30 -0500791 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100792 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500793 device->bdev = NULL;
794 fs_devices->open_devices--;
795 }
796 if (device->writeable) {
797 list_del_init(&device->dev_alloc_list);
798 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100799 if (!device->is_tgtdev_for_dev_replace)
800 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500801 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500802 list_del_init(&device->dev_list);
803 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400804 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500805 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400806 }
Yan Zheng2b820322008-11-17 21:11:30 -0500807
808 if (fs_devices->seed) {
809 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500810 goto again;
811 }
812
Miao Xie443f24f2014-07-24 11:37:15 +0800813 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500814
Chris Masondfe25022008-05-13 13:46:40 -0400815 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400816}
Chris Masona0af4692008-05-13 16:03:06 -0400817
Xiao Guangrong1f781602011-04-20 10:09:16 +0000818static void __free_device(struct work_struct *work)
819{
820 struct btrfs_device *device;
821
822 device = container_of(work, struct btrfs_device, rcu_work);
823
824 if (device->bdev)
825 blkdev_put(device->bdev, device->mode);
826
Josef Bacik606686e2012-06-04 14:03:51 -0400827 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000828 kfree(device);
829}
830
831static void free_device(struct rcu_head *head)
832{
833 struct btrfs_device *device;
834
835 device = container_of(head, struct btrfs_device, rcu);
836
837 INIT_WORK(&device->rcu_work, __free_device);
838 schedule_work(&device->rcu_work);
839}
840
Yan Zheng2b820322008-11-17 21:11:30 -0500841static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400842{
Sasha Levin2037a092015-05-12 19:31:37 -0400843 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500844
Yan Zheng2b820322008-11-17 21:11:30 -0500845 if (--fs_devices->opened > 0)
846 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400847
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000848 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400849 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jainf190aa42015-08-14 18:33:05 +0800850 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400851 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000852 mutex_unlock(&fs_devices->device_list_mutex);
853
Yan Zhenge4404d62008-12-12 10:03:26 -0500854 WARN_ON(fs_devices->open_devices);
855 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500856 fs_devices->opened = 0;
857 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500858
Chris Mason8a4b83c2008-03-24 15:02:07 -0400859 return 0;
860}
861
Yan Zheng2b820322008-11-17 21:11:30 -0500862int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
863{
Yan Zhenge4404d62008-12-12 10:03:26 -0500864 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500865 int ret;
866
867 mutex_lock(&uuid_mutex);
868 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500869 if (!fs_devices->opened) {
870 seed_devices = fs_devices->seed;
871 fs_devices->seed = NULL;
872 }
Yan Zheng2b820322008-11-17 21:11:30 -0500873 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500874
875 while (seed_devices) {
876 fs_devices = seed_devices;
877 seed_devices = fs_devices->seed;
878 __btrfs_close_devices(fs_devices);
879 free_fs_devices(fs_devices);
880 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000881 /*
882 * Wait for rcu kworkers under __btrfs_close_devices
883 * to finish all blkdev_puts so device is really
884 * free when umount is done.
885 */
886 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500887 return ret;
888}
889
Yan Zhenge4404d62008-12-12 10:03:26 -0500890static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
891 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892{
Josef Bacikd5e20032011-08-04 14:52:27 +0000893 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400894 struct block_device *bdev;
895 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400896 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800897 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400898 struct buffer_head *bh;
899 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400900 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500901 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400902 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400903
Tejun Heod4d77622010-11-13 11:55:18 +0100904 flags |= FMODE_EXCL;
905
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500906 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400907 if (device->bdev)
908 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400909 if (!device->name)
910 continue;
911
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000912 /* Just open everything we can; ignore failures here */
913 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
914 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100915 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400916
917 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000918 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400919 if (devid != device->devid)
920 goto error_brelse;
921
Yan Zheng2b820322008-11-17 21:11:30 -0500922 if (memcmp(device->uuid, disk_super->dev_item.uuid,
923 BTRFS_UUID_SIZE))
924 goto error_brelse;
925
926 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800927 if (!latest_dev ||
928 device->generation > latest_dev->generation)
929 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400930
Yan Zheng2b820322008-11-17 21:11:30 -0500931 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
932 device->writeable = 0;
933 } else {
934 device->writeable = !bdev_read_only(bdev);
935 seeding = 0;
936 }
937
Josef Bacikd5e20032011-08-04 14:52:27 +0000938 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800939 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000940 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000941
Chris Mason8a4b83c2008-03-24 15:02:07 -0400942 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400943 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500944 device->mode = flags;
945
Chris Masonc2898112009-06-10 09:51:32 -0400946 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
947 fs_devices->rotating = 1;
948
Chris Masona0af4692008-05-13 16:03:06 -0400949 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300950 if (device->writeable &&
951 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500952 fs_devices->rw_devices++;
953 list_add(&device->dev_alloc_list,
954 &fs_devices->alloc_list);
955 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000956 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400957 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400958
Chris Masona0af4692008-05-13 16:03:06 -0400959error_brelse:
960 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100961 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400962 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400963 }
Chris Masona0af4692008-05-13 16:03:06 -0400964 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300965 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400966 goto out;
967 }
Yan Zheng2b820322008-11-17 21:11:30 -0500968 fs_devices->seeding = seeding;
969 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800970 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500971 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400972out:
Yan Zheng2b820322008-11-17 21:11:30 -0500973 return ret;
974}
975
976int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500977 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500978{
979 int ret;
980
981 mutex_lock(&uuid_mutex);
982 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500983 fs_devices->opened++;
984 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500985 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500986 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500987 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400988 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400989 return ret;
990}
991
Anand Jain6cf86a002016-02-13 10:01:29 +0800992void btrfs_release_disk_super(struct page *page)
993{
994 kunmap(page);
995 put_page(page);
996}
997
998int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
999 struct page **page, struct btrfs_super_block **disk_super)
1000{
1001 void *p;
1002 pgoff_t index;
1003
1004 /* make sure our super fits in the device */
1005 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1006 return 1;
1007
1008 /* make sure our super fits in the page */
1009 if (sizeof(**disk_super) > PAGE_SIZE)
1010 return 1;
1011
1012 /* make sure our super doesn't straddle pages on disk */
1013 index = bytenr >> PAGE_SHIFT;
1014 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1015 return 1;
1016
1017 /* pull in the page with our super */
1018 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1019 index, GFP_KERNEL);
1020
1021 if (IS_ERR_OR_NULL(*page))
1022 return 1;
1023
1024 p = kmap(*page);
1025
1026 /* align our pointer to the offset of the super block */
1027 *disk_super = p + (bytenr & ~PAGE_MASK);
1028
1029 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1030 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1031 btrfs_release_disk_super(*page);
1032 return 1;
1033 }
1034
1035 if ((*disk_super)->label[0] &&
1036 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1037 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1038
1039 return 0;
1040}
1041
David Sterba6f60cbd2013-02-15 11:31:02 -07001042/*
1043 * Look for a btrfs signature on a device. This may be called out of the mount path
1044 * and we are not allowed to call set_blocksize during the scan. The superblock
1045 * is read via pagecache
1046 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001047int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001048 struct btrfs_fs_devices **fs_devices_ret)
1049{
1050 struct btrfs_super_block *disk_super;
1051 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001052 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001053 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001054 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001055 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001056 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001057 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001058
David Sterba6f60cbd2013-02-15 11:31:02 -07001059 /*
1060 * we would like to check all the supers, but that would make
1061 * a btrfs mount succeed after a mkfs from a different FS.
1062 * So, we need to add a special mount option to scan for
1063 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1064 */
1065 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001066 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001067 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001068
1069 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001070 if (IS_ERR(bdev)) {
1071 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001072 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001073 }
1074
Anand Jain6cf86a002016-02-13 10:01:29 +08001075 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
David Sterba6f60cbd2013-02-15 11:31:02 -07001076 goto error_bdev_put;
1077
Xiao Guangronga3438322010-01-06 11:48:18 +00001078 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001079 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001080 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001081
Chris Mason8a4b83c2008-03-24 15:02:07 -04001082 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001083 if (ret > 0) {
1084 if (disk_super->label[0]) {
David Sterba60999ca2014-03-26 18:26:36 +01001085 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1086 } else {
1087 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1088 }
1089
1090 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1091 ret = 0;
1092 }
Josef Bacik02db0842012-06-21 16:03:58 -04001093 if (!ret && fs_devices_ret)
1094 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001095
Anand Jain6cf86a002016-02-13 10:01:29 +08001096 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001097
1098error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001099 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001100error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001101 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001102 return ret;
1103}
Chris Mason0b86a832008-03-24 15:01:56 -04001104
Miao Xie6d07bce2011-01-05 10:07:31 +00001105/* helper to account the used device space in the range */
1106int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1107 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001108{
1109 struct btrfs_key key;
1110 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001111 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001112 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001113 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001114 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001115 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001116 struct extent_buffer *l;
1117
Miao Xie6d07bce2011-01-05 10:07:31 +00001118 *length = 0;
1119
Stefan Behrens63a212a2012-11-05 18:29:28 +01001120 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001121 return 0;
1122
Yan Zheng2b820322008-11-17 21:11:30 -05001123 path = btrfs_alloc_path();
1124 if (!path)
1125 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001126 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001127
Chris Mason0b86a832008-03-24 15:01:56 -04001128 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001129 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001130 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001131
1132 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001133 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001134 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001135 if (ret > 0) {
1136 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1137 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001138 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001139 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001140
Chris Mason0b86a832008-03-24 15:01:56 -04001141 while (1) {
1142 l = path->nodes[0];
1143 slot = path->slots[0];
1144 if (slot >= btrfs_header_nritems(l)) {
1145 ret = btrfs_next_leaf(root, path);
1146 if (ret == 0)
1147 continue;
1148 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001149 goto out;
1150
1151 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001152 }
1153 btrfs_item_key_to_cpu(l, &key, slot);
1154
1155 if (key.objectid < device->devid)
1156 goto next;
1157
1158 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001159 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001160
David Sterba962a2982014-06-04 18:41:45 +02001161 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001162 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001163
Chris Mason0b86a832008-03-24 15:01:56 -04001164 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001165 extent_end = key.offset + btrfs_dev_extent_length(l,
1166 dev_extent);
1167 if (key.offset <= start && extent_end > end) {
1168 *length = end - start + 1;
1169 break;
1170 } else if (key.offset <= start && extent_end > start)
1171 *length += extent_end - start;
1172 else if (key.offset > start && extent_end <= end)
1173 *length += extent_end - key.offset;
1174 else if (key.offset > start && key.offset <= end) {
1175 *length += end - key.offset + 1;
1176 break;
1177 } else if (key.offset > end)
1178 break;
1179
1180next:
1181 path->slots[0]++;
1182 }
1183 ret = 0;
1184out:
1185 btrfs_free_path(path);
1186 return ret;
1187}
1188
Jeff Mahoney499f3772015-06-15 09:41:17 -04001189static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001190 struct btrfs_device *device,
1191 u64 *start, u64 len)
1192{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001193 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001194 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001195 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001196 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001197 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001198
Jeff Mahoney499f3772015-06-15 09:41:17 -04001199 if (transaction)
1200 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001201again:
1202 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001203 struct map_lookup *map;
1204 int i;
1205
Jeff Mahoney95617d62015-06-03 10:55:48 -04001206 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001207 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001208 u64 end;
1209
Josef Bacik6df9a952013-06-27 13:22:46 -04001210 if (map->stripes[i].dev != device)
1211 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001212 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001213 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001214 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001215 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001216 /*
1217 * Make sure that while processing the pinned list we do
1218 * not override our *start with a lower value, because
1219 * we can have pinned chunks that fall within this
1220 * device hole and that have lower physical addresses
1221 * than the pending chunks we processed before. If we
1222 * do not take this special care we can end up getting
1223 * 2 pending chunks that start at the same physical
1224 * device offsets because the end offset of a pinned
1225 * chunk can be equal to the start offset of some
1226 * pending chunk.
1227 */
1228 end = map->stripes[i].physical + em->orig_block_len;
1229 if (end > *start) {
1230 *start = end;
1231 ret = 1;
1232 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001233 }
1234 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001235 if (search_list != &fs_info->pinned_chunks) {
1236 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001237 goto again;
1238 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001239
1240 return ret;
1241}
1242
1243
Chris Mason0b86a832008-03-24 15:01:56 -04001244/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001245 * find_free_dev_extent_start - find free space in the specified device
1246 * @device: the device which we search the free space in
1247 * @num_bytes: the size of the free space that we need
1248 * @search_start: the position from which to begin the search
1249 * @start: store the start of the free space.
1250 * @len: the size of the free space. that we find, or the size
1251 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001252 *
Chris Mason0b86a832008-03-24 15:01:56 -04001253 * this uses a pretty simple search, the expectation is that it is
1254 * called very infrequently and that a given device has a small number
1255 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001256 *
1257 * @start is used to store the start of the free space if we find. But if we
1258 * don't find suitable free space, it will be used to store the start position
1259 * of the max free space.
1260 *
1261 * @len is used to store the size of the free space that we find.
1262 * But if we don't find suitable free space, it is used to store the size of
1263 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001264 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001265int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1266 struct btrfs_device *device, u64 num_bytes,
1267 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001268{
1269 struct btrfs_key key;
1270 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001271 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001272 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001273 u64 hole_size;
1274 u64 max_hole_start;
1275 u64 max_hole_size;
1276 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001277 u64 search_end = device->total_bytes;
1278 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001279 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001280 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001281 u64 min_search_start;
1282
1283 /*
1284 * We don't want to overwrite the superblock on the drive nor any area
1285 * used by the boot loader (grub for example), so we make sure to start
1286 * at an offset of at least 1MB.
1287 */
1288 min_search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1289 search_start = max(search_start, min_search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001290
Josef Bacik6df9a952013-06-27 13:22:46 -04001291 path = btrfs_alloc_path();
1292 if (!path)
1293 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001294
Miao Xie7bfc8372011-01-05 10:07:26 +00001295 max_hole_start = search_start;
1296 max_hole_size = 0;
1297
Zhao Leif2ab7612015-02-16 18:52:17 +08001298again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001299 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001300 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001301 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001302 }
1303
David Sterbae4058b52015-11-27 16:31:35 +01001304 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001305 path->search_commit_root = 1;
1306 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001307
Chris Mason0b86a832008-03-24 15:01:56 -04001308 key.objectid = device->devid;
1309 key.offset = search_start;
1310 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001311
Li Zefan125ccb02011-12-08 15:07:24 +08001312 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001313 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001314 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001315 if (ret > 0) {
1316 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1317 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001318 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001319 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001320
Chris Mason0b86a832008-03-24 15:01:56 -04001321 while (1) {
1322 l = path->nodes[0];
1323 slot = path->slots[0];
1324 if (slot >= btrfs_header_nritems(l)) {
1325 ret = btrfs_next_leaf(root, path);
1326 if (ret == 0)
1327 continue;
1328 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001329 goto out;
1330
1331 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001332 }
1333 btrfs_item_key_to_cpu(l, &key, slot);
1334
1335 if (key.objectid < device->devid)
1336 goto next;
1337
1338 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001339 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001340
David Sterba962a2982014-06-04 18:41:45 +02001341 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001342 goto next;
1343
Miao Xie7bfc8372011-01-05 10:07:26 +00001344 if (key.offset > search_start) {
1345 hole_size = key.offset - search_start;
1346
Josef Bacik6df9a952013-06-27 13:22:46 -04001347 /*
1348 * Have to check before we set max_hole_start, otherwise
1349 * we could end up sending back this offset anyway.
1350 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001351 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001352 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001353 hole_size)) {
1354 if (key.offset >= search_start) {
1355 hole_size = key.offset - search_start;
1356 } else {
1357 WARN_ON_ONCE(1);
1358 hole_size = 0;
1359 }
1360 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001361
Miao Xie7bfc8372011-01-05 10:07:26 +00001362 if (hole_size > max_hole_size) {
1363 max_hole_start = search_start;
1364 max_hole_size = hole_size;
1365 }
1366
1367 /*
1368 * If this free space is greater than which we need,
1369 * it must be the max free space that we have found
1370 * until now, so max_hole_start must point to the start
1371 * of this free space and the length of this free space
1372 * is stored in max_hole_size. Thus, we return
1373 * max_hole_start and max_hole_size and go back to the
1374 * caller.
1375 */
1376 if (hole_size >= num_bytes) {
1377 ret = 0;
1378 goto out;
1379 }
1380 }
1381
Chris Mason0b86a832008-03-24 15:01:56 -04001382 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001383 extent_end = key.offset + btrfs_dev_extent_length(l,
1384 dev_extent);
1385 if (extent_end > search_start)
1386 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001387next:
1388 path->slots[0]++;
1389 cond_resched();
1390 }
Chris Mason0b86a832008-03-24 15:01:56 -04001391
liubo38c01b92011-08-02 02:39:03 +00001392 /*
1393 * At this point, search_start should be the end of
1394 * allocated dev extents, and when shrinking the device,
1395 * search_end may be smaller than search_start.
1396 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001397 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001398 hole_size = search_end - search_start;
1399
Jeff Mahoney499f3772015-06-15 09:41:17 -04001400 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001401 hole_size)) {
1402 btrfs_release_path(path);
1403 goto again;
1404 }
Chris Mason0b86a832008-03-24 15:01:56 -04001405
Zhao Leif2ab7612015-02-16 18:52:17 +08001406 if (hole_size > max_hole_size) {
1407 max_hole_start = search_start;
1408 max_hole_size = hole_size;
1409 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001410 }
1411
Miao Xie7bfc8372011-01-05 10:07:26 +00001412 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001413 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001414 ret = -ENOSPC;
1415 else
1416 ret = 0;
1417
1418out:
Yan Zheng2b820322008-11-17 21:11:30 -05001419 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001420 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001421 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001422 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001423 return ret;
1424}
1425
Jeff Mahoney499f3772015-06-15 09:41:17 -04001426int find_free_dev_extent(struct btrfs_trans_handle *trans,
1427 struct btrfs_device *device, u64 num_bytes,
1428 u64 *start, u64 *len)
1429{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001430 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001431 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001432 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001433}
1434
Christoph Hellwigb2950862008-12-02 09:54:17 -05001435static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001436 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001437 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001438{
1439 int ret;
1440 struct btrfs_path *path;
1441 struct btrfs_root *root = device->dev_root;
1442 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001443 struct btrfs_key found_key;
1444 struct extent_buffer *leaf = NULL;
1445 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001446
1447 path = btrfs_alloc_path();
1448 if (!path)
1449 return -ENOMEM;
1450
1451 key.objectid = device->devid;
1452 key.offset = start;
1453 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001454again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001455 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001456 if (ret > 0) {
1457 ret = btrfs_previous_item(root, path, key.objectid,
1458 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001459 if (ret)
1460 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001461 leaf = path->nodes[0];
1462 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1463 extent = btrfs_item_ptr(leaf, path->slots[0],
1464 struct btrfs_dev_extent);
1465 BUG_ON(found_key.offset > start || found_key.offset +
1466 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001467 key = found_key;
1468 btrfs_release_path(path);
1469 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001470 } else if (ret == 0) {
1471 leaf = path->nodes[0];
1472 extent = btrfs_item_ptr(leaf, path->slots[0],
1473 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001474 } else {
Anand Jaina4553fe2015-09-25 14:43:01 +08001475 btrfs_std_error(root->fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001476 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001477 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001478
Miao Xie2196d6e2014-09-03 21:35:41 +08001479 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1480
Chris Mason8f18cf12008-04-25 16:53:30 -04001481 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001482 if (ret) {
Anand Jaina4553fe2015-09-25 14:43:01 +08001483 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001484 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001485 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001486 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001487 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001488out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001489 btrfs_free_path(path);
1490 return ret;
1491}
1492
Eric Sandeen48a3b632013-04-25 20:41:01 +00001493static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1494 struct btrfs_device *device,
1495 u64 chunk_tree, u64 chunk_objectid,
1496 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001497{
1498 int ret;
1499 struct btrfs_path *path;
1500 struct btrfs_root *root = device->dev_root;
1501 struct btrfs_dev_extent *extent;
1502 struct extent_buffer *leaf;
1503 struct btrfs_key key;
1504
Chris Masondfe25022008-05-13 13:46:40 -04001505 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001506 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001507 path = btrfs_alloc_path();
1508 if (!path)
1509 return -ENOMEM;
1510
Chris Mason0b86a832008-03-24 15:01:56 -04001511 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001512 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001513 key.type = BTRFS_DEV_EXTENT_KEY;
1514 ret = btrfs_insert_empty_item(trans, root, path, &key,
1515 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001516 if (ret)
1517 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001518
1519 leaf = path->nodes[0];
1520 extent = btrfs_item_ptr(leaf, path->slots[0],
1521 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001522 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1523 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1524 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1525
1526 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001527 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001528
Chris Mason0b86a832008-03-24 15:01:56 -04001529 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1530 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001531out:
Chris Mason0b86a832008-03-24 15:01:56 -04001532 btrfs_free_path(path);
1533 return ret;
1534}
1535
Josef Bacik6df9a952013-06-27 13:22:46 -04001536static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001537{
Josef Bacik6df9a952013-06-27 13:22:46 -04001538 struct extent_map_tree *em_tree;
1539 struct extent_map *em;
1540 struct rb_node *n;
1541 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001542
Josef Bacik6df9a952013-06-27 13:22:46 -04001543 em_tree = &fs_info->mapping_tree.map_tree;
1544 read_lock(&em_tree->lock);
1545 n = rb_last(&em_tree->map);
1546 if (n) {
1547 em = rb_entry(n, struct extent_map, rb_node);
1548 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001549 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001550 read_unlock(&em_tree->lock);
1551
Chris Mason0b86a832008-03-24 15:01:56 -04001552 return ret;
1553}
1554
Ilya Dryomov53f10652013-08-12 14:33:01 +03001555static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1556 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001557{
1558 int ret;
1559 struct btrfs_key key;
1560 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001561 struct btrfs_path *path;
1562
Yan Zheng2b820322008-11-17 21:11:30 -05001563 path = btrfs_alloc_path();
1564 if (!path)
1565 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001566
1567 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1568 key.type = BTRFS_DEV_ITEM_KEY;
1569 key.offset = (u64)-1;
1570
Ilya Dryomov53f10652013-08-12 14:33:01 +03001571 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001572 if (ret < 0)
1573 goto error;
1574
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001575 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001576
Ilya Dryomov53f10652013-08-12 14:33:01 +03001577 ret = btrfs_previous_item(fs_info->chunk_root, path,
1578 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001579 BTRFS_DEV_ITEM_KEY);
1580 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001581 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001582 } else {
1583 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1584 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001585 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001586 }
1587 ret = 0;
1588error:
Yan Zheng2b820322008-11-17 21:11:30 -05001589 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001590 return ret;
1591}
1592
1593/*
1594 * the device information is stored in the chunk root
1595 * the btrfs_device struct should be fully filled in
1596 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001597static int btrfs_add_device(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001600{
1601 int ret;
1602 struct btrfs_path *path;
1603 struct btrfs_dev_item *dev_item;
1604 struct extent_buffer *leaf;
1605 struct btrfs_key key;
1606 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001607
1608 root = root->fs_info->chunk_root;
1609
1610 path = btrfs_alloc_path();
1611 if (!path)
1612 return -ENOMEM;
1613
Chris Mason0b86a832008-03-24 15:01:56 -04001614 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1615 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001616 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001617
1618 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001619 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001620 if (ret)
1621 goto out;
1622
1623 leaf = path->nodes[0];
1624 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1625
1626 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001627 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001628 btrfs_set_device_type(leaf, dev_item, device->type);
1629 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1630 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1631 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001632 btrfs_set_device_total_bytes(leaf, dev_item,
1633 btrfs_device_get_disk_total_bytes(device));
1634 btrfs_set_device_bytes_used(leaf, dev_item,
1635 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001636 btrfs_set_device_group(leaf, dev_item, 0);
1637 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1638 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001639 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001640
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001641 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001642 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001643 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001644 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001645 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001646
Yan Zheng2b820322008-11-17 21:11:30 -05001647 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001648out:
1649 btrfs_free_path(path);
1650 return ret;
1651}
Chris Mason8f18cf12008-04-25 16:53:30 -04001652
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001653/*
1654 * Function to update ctime/mtime for a given device path.
1655 * Mainly used for ctime/mtime based probe like libblkid.
1656 */
1657static void update_dev_time(char *path_name)
1658{
1659 struct file *filp;
1660
1661 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001662 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001663 return;
1664 file_update_time(filp);
1665 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001666}
1667
Chris Masona061fc82008-05-07 11:43:44 -04001668static int btrfs_rm_dev_item(struct btrfs_root *root,
1669 struct btrfs_device *device)
1670{
1671 int ret;
1672 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001673 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001674 struct btrfs_trans_handle *trans;
1675
1676 root = root->fs_info->chunk_root;
1677
1678 path = btrfs_alloc_path();
1679 if (!path)
1680 return -ENOMEM;
1681
Yan, Zhenga22285a2010-05-16 10:48:46 -04001682 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001683 if (IS_ERR(trans)) {
1684 btrfs_free_path(path);
1685 return PTR_ERR(trans);
1686 }
Chris Masona061fc82008-05-07 11:43:44 -04001687 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1688 key.type = BTRFS_DEV_ITEM_KEY;
1689 key.offset = device->devid;
1690
1691 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1692 if (ret < 0)
1693 goto out;
1694
1695 if (ret > 0) {
1696 ret = -ENOENT;
1697 goto out;
1698 }
1699
1700 ret = btrfs_del_item(trans, root, path);
1701 if (ret)
1702 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001703out:
1704 btrfs_free_path(path);
1705 btrfs_commit_transaction(trans, root);
1706 return ret;
1707}
1708
David Sterba3cc31a02016-02-15 16:00:26 +01001709/*
1710 * Verify that @num_devices satisfies the RAID profile constraints in the whole
1711 * filesystem. It's up to the caller to adjust that number regarding eg. device
1712 * replace.
1713 */
1714static int btrfs_check_raid_min_devices(struct btrfs_fs_info *fs_info,
1715 u64 num_devices)
Chris Masona061fc82008-05-07 11:43:44 -04001716{
Chris Masona061fc82008-05-07 11:43:44 -04001717 u64 all_avail;
Miao Xiede98ced2013-01-29 10:13:12 +00001718 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001719
Anand Jainf1fa7f22016-02-13 10:01:33 +08001720 do {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001721 seq = read_seqbegin(&fs_info->profiles_lock);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001722
Anand Jainbd45ffb2016-02-13 10:01:34 +08001723 all_avail = fs_info->avail_data_alloc_bits |
1724 fs_info->avail_system_alloc_bits |
1725 fs_info->avail_metadata_alloc_bits;
1726 } while (read_seqretry(&fs_info->profiles_lock, seq));
Anand Jainf1fa7f22016-02-13 10:01:33 +08001727
David Sterba3cc31a02016-02-15 16:00:26 +01001728 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices < 4) {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001729 return BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001730 }
1731
David Sterba3cc31a02016-02-15 16:00:26 +01001732 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices < 2) {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001733 return BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001734 }
1735
David Woodhouse53b381b2013-01-29 18:40:14 -05001736 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
David Sterba3cc31a02016-02-15 16:00:26 +01001737 fs_info->fs_devices->rw_devices < 2) {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001738 return BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001739 }
1740
Anand Jainbd45ffb2016-02-13 10:01:34 +08001741 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
David Sterba3cc31a02016-02-15 16:00:26 +01001742 fs_info->fs_devices->rw_devices < 3) {
Anand Jainbd45ffb2016-02-13 10:01:34 +08001743 return BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
1744 }
1745
1746 return 0;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001747}
1748
Anand Jain6b526ed2016-02-13 10:01:39 +08001749int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
Anand Jainf1fa7f22016-02-13 10:01:33 +08001750{
1751 struct btrfs_device *device;
1752 struct btrfs_device *next_device;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001753 struct btrfs_fs_devices *cur_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001754 u64 num_devices;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001755 int ret = 0;
1756 bool clear_super = false;
Anand Jain42b67422016-02-13 10:01:38 +08001757 char *dev_name = NULL;
Anand Jainf1fa7f22016-02-13 10:01:33 +08001758
1759 mutex_lock(&uuid_mutex);
1760
David Sterba3cc31a02016-02-15 16:00:26 +01001761 num_devices = root->fs_info->fs_devices->num_devices;
1762 btrfs_dev_replace_lock(&root->fs_info->dev_replace, 0);
1763 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1764 WARN_ON(num_devices < 1);
1765 num_devices--;
1766 }
1767 btrfs_dev_replace_unlock(&root->fs_info->dev_replace, 0);
1768
1769 ret = btrfs_check_raid_min_devices(root->fs_info, num_devices - 1);
Anand Jainf1fa7f22016-02-13 10:01:33 +08001770 if (ret)
1771 goto out;
1772
Anand Jain6b526ed2016-02-13 10:01:39 +08001773 ret = btrfs_find_device_by_user_input(root, devid, device_path,
Anand Jain24fc5722016-02-13 10:01:36 +08001774 &device);
1775 if (ret)
1776 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001777
Stefan Behrens63a212a2012-11-05 18:29:28 +01001778 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001779 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Anand Jain24fc5722016-02-13 10:01:36 +08001780 goto out;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001781 }
1782
Yan Zheng2b820322008-11-17 21:11:30 -05001783 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001784 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Anand Jain24fc5722016-02-13 10:01:36 +08001785 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05001786 }
1787
1788 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001789 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001790 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001791 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001792 unlock_chunks(root);
Anand Jain42b67422016-02-13 10:01:38 +08001793 dev_name = kstrdup(device->name->str, GFP_KERNEL);
1794 if (!dev_name) {
1795 ret = -ENOMEM;
1796 goto error_undo;
1797 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001798 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001799 }
Chris Masona061fc82008-05-07 11:43:44 -04001800
Carey Underwoodd7901552013-03-04 16:37:06 -06001801 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001802 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001803 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001804 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001805 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001806
Stefan Behrens63a212a2012-11-05 18:29:28 +01001807 /*
1808 * TODO: the superblock still includes this device in its num_devices
1809 * counter although write_all_supers() is not locked out. This
1810 * could give a filesystem state which requires a degraded mount.
1811 */
Chris Masona061fc82008-05-07 11:43:44 -04001812 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1813 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001814 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001815
Yan Zheng2b820322008-11-17 21:11:30 -05001816 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001817 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001818
1819 /*
1820 * the device list mutex makes sure that we don't change
1821 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001822 * the device supers. Whoever is writing all supers, should
1823 * lock the device list mutex before getting the number of
1824 * devices in the super block (super_copy). Conversely,
1825 * whoever updates the number of devices in the super block
1826 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001827 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001828
1829 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001830 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001831 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001832
Yan Zhenge4404d62008-12-12 10:03:26 -05001833 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001834 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001835
Chris Masoncd02dca2010-12-13 14:56:23 -05001836 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001837 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001838
Yan Zheng2b820322008-11-17 21:11:30 -05001839 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1840 struct btrfs_device, dev_list);
1841 if (device->bdev == root->fs_info->sb->s_bdev)
1842 root->fs_info->sb->s_bdev = next_device->bdev;
1843 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1844 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1845
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001846 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001847 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001848 /* remove sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +08001849 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001850 }
Anand Jain99994cd2014-06-03 11:36:00 +08001851
Xiao Guangrong1f781602011-04-20 10:09:16 +00001852 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001853
David Sterba6c417612011-04-13 15:41:04 +02001854 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1855 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001856 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001857
Xiao Guangrong1f781602011-04-20 10:09:16 +00001858 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001859 struct btrfs_fs_devices *fs_devices;
1860 fs_devices = root->fs_info->fs_devices;
1861 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001862 if (fs_devices->seed == cur_devices) {
1863 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001864 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001865 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001866 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001867 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001868 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001869 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001870 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001871 }
1872
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001873 root->fs_info->num_tolerated_disk_barrier_failures =
1874 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1875
Yan Zheng2b820322008-11-17 21:11:30 -05001876 /*
1877 * at this point, the device is zero sized. We want to
1878 * remove it from the devices list and zero out the old super
1879 */
Anand Jain24fc5722016-02-13 10:01:36 +08001880 if (clear_super) {
Anand Jain42b67422016-02-13 10:01:38 +08001881 struct block_device *bdev;
Anand Jain4d90d282014-04-06 12:59:07 +08001882
Anand Jain42b67422016-02-13 10:01:38 +08001883 bdev = blkdev_get_by_path(dev_name, FMODE_READ | FMODE_EXCL,
1884 root->fs_info->bdev_holder);
1885 if (!IS_ERR(bdev)) {
1886 btrfs_scratch_superblocks(bdev, dev_name);
Anand Jain24fc5722016-02-13 10:01:36 +08001887 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Anand Jain4d90d282014-04-06 12:59:07 +08001888 }
Chris Masondfe25022008-05-13 13:46:40 -04001889 }
Chris Masona061fc82008-05-07 11:43:44 -04001890
Chris Masona061fc82008-05-07 11:43:44 -04001891out:
Anand Jain42b67422016-02-13 10:01:38 +08001892 kfree(dev_name);
1893
Chris Masona061fc82008-05-07 11:43:44 -04001894 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001895 return ret;
Anand Jain24fc5722016-02-13 10:01:36 +08001896
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001897error_undo:
1898 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001899 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001900 list_add(&device->dev_alloc_list,
1901 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001902 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001903 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001904 }
Anand Jain24fc5722016-02-13 10:01:36 +08001905 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001906}
1907
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001908void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1909 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001910{
Anand Jaind51908c2014-08-13 14:24:19 +08001911 struct btrfs_fs_devices *fs_devices;
1912
Stefan Behrense93c89c2012-11-05 17:33:06 +01001913 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001914
Anand Jain25e8e912014-08-20 10:56:56 +08001915 /*
1916 * in case of fs with no seed, srcdev->fs_devices will point
1917 * to fs_devices of fs_info. However when the dev being replaced is
1918 * a seed dev it will point to the seed's local fs_devices. In short
1919 * srcdev will have its correct fs_devices in both the cases.
1920 */
1921 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001922
Stefan Behrense93c89c2012-11-05 17:33:06 +01001923 list_del_rcu(&srcdev->dev_list);
1924 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001925 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001926 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001927 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001928
Miao Xie82372bc2014-09-03 21:35:44 +08001929 if (srcdev->writeable) {
1930 fs_devices->rw_devices--;
1931 /* zero out the old super if it is writable */
David Sterba31388ab2015-11-19 11:35:17 +01001932 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
Ilya Dryomov13572722013-10-02 20:41:01 +03001933 }
1934
Miao Xie82372bc2014-09-03 21:35:44 +08001935 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001936 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001937}
1938
1939void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1940 struct btrfs_device *srcdev)
1941{
1942 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001943
Stefan Behrense93c89c2012-11-05 17:33:06 +01001944 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001945
1946 /*
1947 * unless fs_devices is seed fs, num_devices shouldn't go
1948 * zero
1949 */
1950 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1951
1952 /* if this is no devs we rather delete the fs_devices */
1953 if (!fs_devices->num_devices) {
1954 struct btrfs_fs_devices *tmp_fs_devices;
1955
1956 tmp_fs_devices = fs_info->fs_devices;
1957 while (tmp_fs_devices) {
1958 if (tmp_fs_devices->seed == fs_devices) {
1959 tmp_fs_devices->seed = fs_devices->seed;
1960 break;
1961 }
1962 tmp_fs_devices = tmp_fs_devices->seed;
1963 }
1964 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001965 __btrfs_close_devices(fs_devices);
1966 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001967 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001968}
1969
1970void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1971 struct btrfs_device *tgtdev)
1972{
1973 struct btrfs_device *next_device;
1974
Miao Xie67a2c452014-09-03 21:35:43 +08001975 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001976 WARN_ON(!tgtdev);
1977 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08001978
Anand Jain32576042015-08-14 18:32:49 +08001979 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08001980
Stefan Behrense93c89c2012-11-05 17:33:06 +01001981 if (tgtdev->bdev) {
David Sterba31388ab2015-11-19 11:35:17 +01001982 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001983 fs_info->fs_devices->open_devices--;
1984 }
1985 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001986
1987 next_device = list_entry(fs_info->fs_devices->devices.next,
1988 struct btrfs_device, dev_list);
1989 if (tgtdev->bdev == fs_info->sb->s_bdev)
1990 fs_info->sb->s_bdev = next_device->bdev;
1991 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1992 fs_info->fs_devices->latest_bdev = next_device->bdev;
1993 list_del_rcu(&tgtdev->dev_list);
1994
1995 call_rcu(&tgtdev->rcu, free_device);
1996
1997 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001998 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001999}
2000
Eric Sandeen48a3b632013-04-25 20:41:01 +00002001static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2002 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002003{
2004 int ret = 0;
2005 struct btrfs_super_block *disk_super;
2006 u64 devid;
2007 u8 *dev_uuid;
2008 struct block_device *bdev;
2009 struct buffer_head *bh;
2010
2011 *device = NULL;
2012 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2013 root->fs_info->bdev_holder, 0, &bdev, &bh);
2014 if (ret)
2015 return ret;
2016 disk_super = (struct btrfs_super_block *)bh->b_data;
2017 devid = btrfs_stack_device_id(&disk_super->dev_item);
2018 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002019 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002020 disk_super->fsid);
2021 brelse(bh);
2022 if (!*device)
2023 ret = -ENOENT;
2024 blkdev_put(bdev, FMODE_READ);
2025 return ret;
2026}
2027
2028int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2029 char *device_path,
2030 struct btrfs_device **device)
2031{
2032 *device = NULL;
2033 if (strcmp(device_path, "missing") == 0) {
2034 struct list_head *devices;
2035 struct btrfs_device *tmp;
2036
2037 devices = &root->fs_info->fs_devices->devices;
2038 /*
2039 * It is safe to read the devices since the volume_mutex
2040 * is held by the caller.
2041 */
2042 list_for_each_entry(tmp, devices, dev_list) {
2043 if (tmp->in_fs_metadata && !tmp->bdev) {
2044 *device = tmp;
2045 break;
2046 }
2047 }
2048
Anand Jaind74a6252015-08-14 18:32:56 +08002049 if (!*device)
2050 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002051
2052 return 0;
2053 } else {
2054 return btrfs_find_device_by_path(root, device_path, device);
2055 }
2056}
2057
Anand Jain24e04742016-02-13 10:01:35 +08002058int btrfs_find_device_by_user_input(struct btrfs_root *root, u64 srcdevid,
2059 char *srcdev_name,
2060 struct btrfs_device **device)
2061{
2062 int ret;
2063
2064 if (srcdevid) {
2065 ret = 0;
2066 *device = btrfs_find_device(root->fs_info, srcdevid, NULL,
2067 NULL);
2068 if (!*device)
2069 ret = -ENOENT;
2070 } else {
Anand Jainb3d1b152016-02-13 10:01:37 +08002071 if (!srcdev_name || !srcdev_name[0])
2072 return -EINVAL;
2073
Anand Jain24e04742016-02-13 10:01:35 +08002074 ret = btrfs_find_device_missing_or_by_path(root, srcdev_name,
2075 device);
2076 }
2077 return ret;
2078}
2079
Yan Zheng2b820322008-11-17 21:11:30 -05002080/*
2081 * does all the dirty work required for changing file system's UUID.
2082 */
Li Zefan125ccb02011-12-08 15:07:24 +08002083static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002084{
2085 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2086 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002087 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002088 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002089 struct btrfs_device *device;
2090 u64 super_flags;
2091
2092 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002093 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002094 return -EINVAL;
2095
Ilya Dryomov2208a372013-08-12 14:33:03 +03002096 seed_devices = __alloc_fs_devices();
2097 if (IS_ERR(seed_devices))
2098 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002099
Yan Zhenge4404d62008-12-12 10:03:26 -05002100 old_devices = clone_fs_devices(fs_devices);
2101 if (IS_ERR(old_devices)) {
2102 kfree(seed_devices);
2103 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002104 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002105
Yan Zheng2b820322008-11-17 21:11:30 -05002106 list_add(&old_devices->list, &fs_uuids);
2107
Yan Zhenge4404d62008-12-12 10:03:26 -05002108 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2109 seed_devices->opened = 1;
2110 INIT_LIST_HEAD(&seed_devices->devices);
2111 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002112 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002113
2114 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002115 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2116 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002117 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002118 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002119
2120 lock_chunks(root);
2121 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2122 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002123
Yan Zheng2b820322008-11-17 21:11:30 -05002124 fs_devices->seeding = 0;
2125 fs_devices->num_devices = 0;
2126 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002127 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002128 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002129 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002130
2131 generate_random_uuid(fs_devices->fsid);
2132 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2133 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002134 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2135
Yan Zheng2b820322008-11-17 21:11:30 -05002136 super_flags = btrfs_super_flags(disk_super) &
2137 ~BTRFS_SUPER_FLAG_SEEDING;
2138 btrfs_set_super_flags(disk_super, super_flags);
2139
2140 return 0;
2141}
2142
2143/*
2144 * strore the expected generation for seed devices in device items.
2145 */
2146static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2147 struct btrfs_root *root)
2148{
2149 struct btrfs_path *path;
2150 struct extent_buffer *leaf;
2151 struct btrfs_dev_item *dev_item;
2152 struct btrfs_device *device;
2153 struct btrfs_key key;
2154 u8 fs_uuid[BTRFS_UUID_SIZE];
2155 u8 dev_uuid[BTRFS_UUID_SIZE];
2156 u64 devid;
2157 int ret;
2158
2159 path = btrfs_alloc_path();
2160 if (!path)
2161 return -ENOMEM;
2162
2163 root = root->fs_info->chunk_root;
2164 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2165 key.offset = 0;
2166 key.type = BTRFS_DEV_ITEM_KEY;
2167
2168 while (1) {
2169 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2170 if (ret < 0)
2171 goto error;
2172
2173 leaf = path->nodes[0];
2174next_slot:
2175 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2176 ret = btrfs_next_leaf(root, path);
2177 if (ret > 0)
2178 break;
2179 if (ret < 0)
2180 goto error;
2181 leaf = path->nodes[0];
2182 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002183 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002184 continue;
2185 }
2186
2187 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2188 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2189 key.type != BTRFS_DEV_ITEM_KEY)
2190 break;
2191
2192 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2193 struct btrfs_dev_item);
2194 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002195 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002196 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002197 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002198 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002199 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2200 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002201 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002202
2203 if (device->fs_devices->seeding) {
2204 btrfs_set_device_generation(leaf, dev_item,
2205 device->generation);
2206 btrfs_mark_buffer_dirty(leaf);
2207 }
2208
2209 path->slots[0]++;
2210 goto next_slot;
2211 }
2212 ret = 0;
2213error:
2214 btrfs_free_path(path);
2215 return ret;
2216}
2217
Chris Mason788f20e2008-04-28 15:29:42 -04002218int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2219{
Josef Bacikd5e20032011-08-04 14:52:27 +00002220 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002221 struct btrfs_trans_handle *trans;
2222 struct btrfs_device *device;
2223 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002224 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002225 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002226 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002227 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002228 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002229 int ret = 0;
2230
Yan Zheng2b820322008-11-17 21:11:30 -05002231 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002232 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002233
Li Zefana5d16332011-12-07 20:08:40 -05002234 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002235 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002236 if (IS_ERR(bdev))
2237 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002238
Yan Zheng2b820322008-11-17 21:11:30 -05002239 if (root->fs_info->fs_devices->seeding) {
2240 seeding_dev = 1;
2241 down_write(&sb->s_umount);
2242 mutex_lock(&uuid_mutex);
2243 }
2244
Chris Mason8c8bee12008-09-29 11:19:10 -04002245 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002246
Chris Mason788f20e2008-04-28 15:29:42 -04002247 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002248
2249 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002250 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002251 if (device->bdev == bdev) {
2252 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002253 mutex_unlock(
2254 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002255 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002256 }
2257 }
Liu Bod25628b2012-11-14 14:35:30 +00002258 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002259
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002260 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2261 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002262 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002263 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002264 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002265 }
2266
David Sterba78f2c9e2016-02-11 14:25:38 +01002267 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002268 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002269 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002270 ret = -ENOMEM;
2271 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002272 }
Josef Bacik606686e2012-06-04 14:03:51 -04002273 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002274
Yan, Zhenga22285a2010-05-16 10:48:46 -04002275 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002276 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002277 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002278 kfree(device);
2279 ret = PTR_ERR(trans);
2280 goto error;
2281 }
2282
Josef Bacikd5e20032011-08-04 14:52:27 +00002283 q = bdev_get_queue(bdev);
2284 if (blk_queue_discard(q))
2285 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002286 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002287 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002288 device->io_width = root->sectorsize;
2289 device->io_align = root->sectorsize;
2290 device->sector_size = root->sectorsize;
2291 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002292 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002293 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002294 device->dev_root = root->fs_info->dev_root;
2295 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002296 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002297 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002298 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002299 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002300 set_blocksize(device->bdev, 4096);
2301
Yan Zheng2b820322008-11-17 21:11:30 -05002302 if (seeding_dev) {
2303 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002304 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002305 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002306 }
2307
2308 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002309
Chris Masone5e9a522009-06-10 15:17:02 -04002310 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002311 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002312 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002313 list_add(&device->dev_alloc_list,
2314 &root->fs_info->fs_devices->alloc_list);
2315 root->fs_info->fs_devices->num_devices++;
2316 root->fs_info->fs_devices->open_devices++;
2317 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002318 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002319 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2320
Josef Bacik2bf64752011-09-26 17:12:22 -04002321 spin_lock(&root->fs_info->free_chunk_lock);
2322 root->fs_info->free_chunk_space += device->total_bytes;
2323 spin_unlock(&root->fs_info->free_chunk_lock);
2324
Chris Masonc2898112009-06-10 09:51:32 -04002325 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2326 root->fs_info->fs_devices->rotating = 1;
2327
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002328 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002329 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002330 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002331
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002332 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002333 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002334 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002335
2336 /* add sysfs device entry */
Anand Jaine3bd6972015-08-14 18:32:48 +08002337 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002338
Miao Xie2196d6e2014-09-03 21:35:41 +08002339 /*
2340 * we've got more storage, clear any full flags on the space
2341 * infos
2342 */
2343 btrfs_clear_space_info_full(root->fs_info);
2344
2345 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002346 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002347
Yan Zheng2b820322008-11-17 21:11:30 -05002348 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002349 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002350 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002351 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002352 if (ret) {
2353 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002354 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002355 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002356 }
2357
2358 ret = btrfs_add_device(trans, root, device);
2359 if (ret) {
2360 btrfs_abort_transaction(trans, root, ret);
2361 goto error_trans;
2362 }
2363
2364 if (seeding_dev) {
2365 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2366
Yan Zheng2b820322008-11-17 21:11:30 -05002367 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002368 if (ret) {
2369 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002370 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002371 }
Anand Jainb2373f22014-06-03 11:36:03 +08002372
2373 /* Sprouting would change fsid of the mounted root,
2374 * so rename the fsid on the sysfs
2375 */
2376 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2377 root->fs_info->fsid);
Anand Jainc1b7e472015-08-14 18:32:50 +08002378 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
Anand Jain2e7910d2015-03-10 06:38:29 +08002379 fsid_buf))
David Sterbaf14d1042015-10-08 11:37:06 +02002380 btrfs_warn(root->fs_info,
2381 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002382 }
2383
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002384 root->fs_info->num_tolerated_disk_barrier_failures =
2385 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002386 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002387
2388 if (seeding_dev) {
2389 mutex_unlock(&uuid_mutex);
2390 up_write(&sb->s_umount);
2391
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002392 if (ret) /* transaction commit */
2393 return ret;
2394
Yan Zheng2b820322008-11-17 21:11:30 -05002395 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002396 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002397 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002398 "Failed to relocate sys chunks after "
2399 "device initialization. This can be fixed "
2400 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002401 trans = btrfs_attach_transaction(root);
2402 if (IS_ERR(trans)) {
2403 if (PTR_ERR(trans) == -ENOENT)
2404 return 0;
2405 return PTR_ERR(trans);
2406 }
2407 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002408 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002409
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002410 /* Update ctime/mtime for libblkid */
2411 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002412 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002413
2414error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002415 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002416 rcu_string_free(device->name);
Anand Jain32576042015-08-14 18:32:49 +08002417 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002418 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002419error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002420 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002421 if (seeding_dev) {
2422 mutex_unlock(&uuid_mutex);
2423 up_write(&sb->s_umount);
2424 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002425 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002426}
2427
Stefan Behrense93c89c2012-11-05 17:33:06 +01002428int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002429 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002430 struct btrfs_device **device_out)
2431{
2432 struct request_queue *q;
2433 struct btrfs_device *device;
2434 struct block_device *bdev;
2435 struct btrfs_fs_info *fs_info = root->fs_info;
2436 struct list_head *devices;
2437 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002438 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002439 int ret = 0;
2440
2441 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002442 if (fs_info->fs_devices->seeding) {
2443 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002444 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002445 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002446
2447 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2448 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002449 if (IS_ERR(bdev)) {
2450 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002451 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002452 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002453
2454 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2455
2456 devices = &fs_info->fs_devices->devices;
2457 list_for_each_entry(device, devices, dev_list) {
2458 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002459 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002460 ret = -EEXIST;
2461 goto error;
2462 }
2463 }
2464
Miao Xie1c433662014-09-03 21:35:32 +08002465
Miao Xie7cc8e582014-09-03 21:35:38 +08002466 if (i_size_read(bdev->bd_inode) <
2467 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002468 btrfs_err(fs_info, "target device is smaller than source device!");
2469 ret = -EINVAL;
2470 goto error;
2471 }
2472
2473
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002474 device = btrfs_alloc_device(NULL, &devid, NULL);
2475 if (IS_ERR(device)) {
2476 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002477 goto error;
2478 }
2479
2480 name = rcu_string_strdup(device_path, GFP_NOFS);
2481 if (!name) {
2482 kfree(device);
2483 ret = -ENOMEM;
2484 goto error;
2485 }
2486 rcu_assign_pointer(device->name, name);
2487
2488 q = bdev_get_queue(bdev);
2489 if (blk_queue_discard(q))
2490 device->can_discard = 1;
2491 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2492 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002493 device->generation = 0;
2494 device->io_width = root->sectorsize;
2495 device->io_align = root->sectorsize;
2496 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002497 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2498 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2499 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002500 ASSERT(list_empty(&srcdev->resized_list));
2501 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002502 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002503 device->dev_root = fs_info->dev_root;
2504 device->bdev = bdev;
2505 device->in_fs_metadata = 1;
2506 device->is_tgtdev_for_dev_replace = 1;
2507 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002508 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002509 set_blocksize(device->bdev, 4096);
2510 device->fs_devices = fs_info->fs_devices;
2511 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2512 fs_info->fs_devices->num_devices++;
2513 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002514 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2515
2516 *device_out = device;
2517 return ret;
2518
2519error:
2520 blkdev_put(bdev, FMODE_EXCL);
2521 return ret;
2522}
2523
2524void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2525 struct btrfs_device *tgtdev)
2526{
2527 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2528 tgtdev->io_width = fs_info->dev_root->sectorsize;
2529 tgtdev->io_align = fs_info->dev_root->sectorsize;
2530 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2531 tgtdev->dev_root = fs_info->dev_root;
2532 tgtdev->in_fs_metadata = 1;
2533}
2534
Chris Masond3977122009-01-05 21:25:51 -05002535static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2536 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002537{
2538 int ret;
2539 struct btrfs_path *path;
2540 struct btrfs_root *root;
2541 struct btrfs_dev_item *dev_item;
2542 struct extent_buffer *leaf;
2543 struct btrfs_key key;
2544
2545 root = device->dev_root->fs_info->chunk_root;
2546
2547 path = btrfs_alloc_path();
2548 if (!path)
2549 return -ENOMEM;
2550
2551 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2552 key.type = BTRFS_DEV_ITEM_KEY;
2553 key.offset = device->devid;
2554
2555 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2556 if (ret < 0)
2557 goto out;
2558
2559 if (ret > 0) {
2560 ret = -ENOENT;
2561 goto out;
2562 }
2563
2564 leaf = path->nodes[0];
2565 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2566
2567 btrfs_set_device_id(leaf, dev_item, device->devid);
2568 btrfs_set_device_type(leaf, dev_item, device->type);
2569 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2570 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2571 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002572 btrfs_set_device_total_bytes(leaf, dev_item,
2573 btrfs_device_get_disk_total_bytes(device));
2574 btrfs_set_device_bytes_used(leaf, dev_item,
2575 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002576 btrfs_mark_buffer_dirty(leaf);
2577
2578out:
2579 btrfs_free_path(path);
2580 return ret;
2581}
2582
Miao Xie2196d6e2014-09-03 21:35:41 +08002583int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002584 struct btrfs_device *device, u64 new_size)
2585{
2586 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002587 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002588 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002589 u64 old_total;
2590 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002591
Yan Zheng2b820322008-11-17 21:11:30 -05002592 if (!device->writeable)
2593 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002594
2595 lock_chunks(device->dev_root);
2596 old_total = btrfs_super_total_bytes(super_copy);
2597 diff = new_size - device->total_bytes;
2598
Stefan Behrens63a212a2012-11-05 18:29:28 +01002599 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002600 device->is_tgtdev_for_dev_replace) {
2601 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002602 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002603 }
Yan Zheng2b820322008-11-17 21:11:30 -05002604
Miao Xie935e5cc2014-09-03 21:35:33 +08002605 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002606
2607 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002608 device->fs_devices->total_rw_bytes += diff;
2609
Miao Xie7cc8e582014-09-03 21:35:38 +08002610 btrfs_device_set_total_bytes(device, new_size);
2611 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002612 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002613 if (list_empty(&device->resized_list))
2614 list_add_tail(&device->resized_list,
2615 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002616 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002617
Chris Mason8f18cf12008-04-25 16:53:30 -04002618 return btrfs_update_device(trans, device);
2619}
2620
2621static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002622 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002623 u64 chunk_offset)
2624{
2625 int ret;
2626 struct btrfs_path *path;
2627 struct btrfs_key key;
2628
2629 root = root->fs_info->chunk_root;
2630 path = btrfs_alloc_path();
2631 if (!path)
2632 return -ENOMEM;
2633
2634 key.objectid = chunk_objectid;
2635 key.offset = chunk_offset;
2636 key.type = BTRFS_CHUNK_ITEM_KEY;
2637
2638 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002639 if (ret < 0)
2640 goto out;
2641 else if (ret > 0) { /* Logic error or corruption */
Anand Jaina4553fe2015-09-25 14:43:01 +08002642 btrfs_std_error(root->fs_info, -ENOENT,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002643 "Failed lookup while freeing chunk.");
2644 ret = -ENOENT;
2645 goto out;
2646 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002647
2648 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002649 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002650 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 "Failed to delete chunk item.");
2652out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002653 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002654 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002655}
2656
Christoph Hellwigb2950862008-12-02 09:54:17 -05002657static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002658 chunk_offset)
2659{
David Sterba6c417612011-04-13 15:41:04 +02002660 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002661 struct btrfs_disk_key *disk_key;
2662 struct btrfs_chunk *chunk;
2663 u8 *ptr;
2664 int ret = 0;
2665 u32 num_stripes;
2666 u32 array_size;
2667 u32 len = 0;
2668 u32 cur;
2669 struct btrfs_key key;
2670
Miao Xie2196d6e2014-09-03 21:35:41 +08002671 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002672 array_size = btrfs_super_sys_array_size(super_copy);
2673
2674 ptr = super_copy->sys_chunk_array;
2675 cur = 0;
2676
2677 while (cur < array_size) {
2678 disk_key = (struct btrfs_disk_key *)ptr;
2679 btrfs_disk_key_to_cpu(&key, disk_key);
2680
2681 len = sizeof(*disk_key);
2682
2683 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2684 chunk = (struct btrfs_chunk *)(ptr + len);
2685 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2686 len += btrfs_chunk_item_size(num_stripes);
2687 } else {
2688 ret = -EIO;
2689 break;
2690 }
2691 if (key.objectid == chunk_objectid &&
2692 key.offset == chunk_offset) {
2693 memmove(ptr, ptr + len, array_size - (cur + len));
2694 array_size -= len;
2695 btrfs_set_super_sys_array_size(super_copy, array_size);
2696 } else {
2697 ptr += len;
2698 cur += len;
2699 }
2700 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002701 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002702 return ret;
2703}
2704
Josef Bacik47ab2a62014-09-18 11:20:02 -04002705int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2706 struct btrfs_root *root, u64 chunk_offset)
2707{
2708 struct extent_map_tree *em_tree;
2709 struct extent_map *em;
2710 struct btrfs_root *extent_root = root->fs_info->extent_root;
2711 struct map_lookup *map;
2712 u64 dev_extent_len = 0;
2713 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002714 int i, ret = 0;
2715
2716 /* Just in case */
2717 root = root->fs_info->chunk_root;
2718 em_tree = &root->fs_info->mapping_tree.map_tree;
2719
2720 read_lock(&em_tree->lock);
2721 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2722 read_unlock(&em_tree->lock);
2723
2724 if (!em || em->start > chunk_offset ||
2725 em->start + em->len < chunk_offset) {
2726 /*
2727 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002728 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002729 * do anything we still error out.
2730 */
2731 ASSERT(0);
2732 if (em)
2733 free_extent_map(em);
2734 return -EINVAL;
2735 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002736 map = em->map_lookup;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002737 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002738 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002739 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002740
2741 for (i = 0; i < map->num_stripes; i++) {
2742 struct btrfs_device *device = map->stripes[i].dev;
2743 ret = btrfs_free_dev_extent(trans, device,
2744 map->stripes[i].physical,
2745 &dev_extent_len);
2746 if (ret) {
2747 btrfs_abort_transaction(trans, root, ret);
2748 goto out;
2749 }
2750
2751 if (device->bytes_used > 0) {
2752 lock_chunks(root);
2753 btrfs_device_set_bytes_used(device,
2754 device->bytes_used - dev_extent_len);
2755 spin_lock(&root->fs_info->free_chunk_lock);
2756 root->fs_info->free_chunk_space += dev_extent_len;
2757 spin_unlock(&root->fs_info->free_chunk_lock);
2758 btrfs_clear_space_info_full(root->fs_info);
2759 unlock_chunks(root);
2760 }
2761
2762 if (map->stripes[i].dev) {
2763 ret = btrfs_update_device(trans, map->stripes[i].dev);
2764 if (ret) {
2765 btrfs_abort_transaction(trans, root, ret);
2766 goto out;
2767 }
2768 }
2769 }
Zhao Leia688a042015-02-09 20:31:44 +08002770 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002771 if (ret) {
2772 btrfs_abort_transaction(trans, root, ret);
2773 goto out;
2774 }
2775
2776 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2777
2778 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2779 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2780 if (ret) {
2781 btrfs_abort_transaction(trans, root, ret);
2782 goto out;
2783 }
2784 }
2785
Filipe Manana04216822014-11-27 21:14:15 +00002786 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002787 if (ret) {
2788 btrfs_abort_transaction(trans, extent_root, ret);
2789 goto out;
2790 }
2791
Josef Bacik47ab2a62014-09-18 11:20:02 -04002792out:
2793 /* once for us */
2794 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002795 return ret;
2796}
2797
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002798static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002799{
Chris Mason8f18cf12008-04-25 16:53:30 -04002800 struct btrfs_root *extent_root;
2801 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002802 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002803
2804 root = root->fs_info->chunk_root;
2805 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002806
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002807 /*
2808 * Prevent races with automatic removal of unused block groups.
2809 * After we relocate and before we remove the chunk with offset
2810 * chunk_offset, automatic removal of the block group can kick in,
2811 * resulting in a failure when calling btrfs_remove_chunk() below.
2812 *
2813 * Make sure to acquire this mutex before doing a tree search (dev
2814 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2815 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2816 * we release the path used to search the chunk/dev tree and before
2817 * the current task acquires this mutex and calls us.
2818 */
2819 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2820
Josef Bacikba1bf482009-09-11 16:11:19 -04002821 ret = btrfs_can_relocate(extent_root, chunk_offset);
2822 if (ret)
2823 return -ENOSPC;
2824
Chris Mason8f18cf12008-04-25 16:53:30 -04002825 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002826 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002827 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002828 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002829 if (ret)
2830 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002831
Filipe Manana7fd01182015-11-13 23:57:17 +00002832 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2833 chunk_offset);
Liu Bo0f788c52013-03-04 16:25:40 +00002834 if (IS_ERR(trans)) {
2835 ret = PTR_ERR(trans);
Anand Jaina4553fe2015-09-25 14:43:01 +08002836 btrfs_std_error(root->fs_info, ret, NULL);
Liu Bo0f788c52013-03-04 16:25:40 +00002837 return ret;
2838 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002839
2840 /*
2841 * step two, delete the device extents and the
2842 * chunk tree entries
2843 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002844 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002845 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002846 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002847}
2848
Yan Zheng2b820322008-11-17 21:11:30 -05002849static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2850{
2851 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2852 struct btrfs_path *path;
2853 struct extent_buffer *leaf;
2854 struct btrfs_chunk *chunk;
2855 struct btrfs_key key;
2856 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002857 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002858 bool retried = false;
2859 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002860 int ret;
2861
2862 path = btrfs_alloc_path();
2863 if (!path)
2864 return -ENOMEM;
2865
Josef Bacikba1bf482009-09-11 16:11:19 -04002866again:
Yan Zheng2b820322008-11-17 21:11:30 -05002867 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2868 key.offset = (u64)-1;
2869 key.type = BTRFS_CHUNK_ITEM_KEY;
2870
2871 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002872 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002873 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002874 if (ret < 0) {
2875 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002876 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002877 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002878 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002879
2880 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2881 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002882 if (ret)
2883 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002884 if (ret < 0)
2885 goto error;
2886 if (ret > 0)
2887 break;
2888
2889 leaf = path->nodes[0];
2890 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2891
2892 chunk = btrfs_item_ptr(leaf, path->slots[0],
2893 struct btrfs_chunk);
2894 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002895 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002896
2897 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002898 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002899 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002900 if (ret == -ENOSPC)
2901 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302902 else
2903 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002904 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002905 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002906
2907 if (found_key.offset == 0)
2908 break;
2909 key.offset = found_key.offset - 1;
2910 }
2911 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002912 if (failed && !retried) {
2913 failed = 0;
2914 retried = true;
2915 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302916 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002917 ret = -ENOSPC;
2918 }
Yan Zheng2b820322008-11-17 21:11:30 -05002919error:
2920 btrfs_free_path(path);
2921 return ret;
2922}
2923
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002924static int insert_balance_item(struct btrfs_root *root,
2925 struct btrfs_balance_control *bctl)
2926{
2927 struct btrfs_trans_handle *trans;
2928 struct btrfs_balance_item *item;
2929 struct btrfs_disk_balance_args disk_bargs;
2930 struct btrfs_path *path;
2931 struct extent_buffer *leaf;
2932 struct btrfs_key key;
2933 int ret, err;
2934
2935 path = btrfs_alloc_path();
2936 if (!path)
2937 return -ENOMEM;
2938
2939 trans = btrfs_start_transaction(root, 0);
2940 if (IS_ERR(trans)) {
2941 btrfs_free_path(path);
2942 return PTR_ERR(trans);
2943 }
2944
2945 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01002946 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002947 key.offset = 0;
2948
2949 ret = btrfs_insert_empty_item(trans, root, path, &key,
2950 sizeof(*item));
2951 if (ret)
2952 goto out;
2953
2954 leaf = path->nodes[0];
2955 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2956
2957 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2958
2959 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2960 btrfs_set_balance_data(leaf, item, &disk_bargs);
2961 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2962 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2963 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2964 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2965
2966 btrfs_set_balance_flags(leaf, item, bctl->flags);
2967
2968 btrfs_mark_buffer_dirty(leaf);
2969out:
2970 btrfs_free_path(path);
2971 err = btrfs_commit_transaction(trans, root);
2972 if (err && !ret)
2973 ret = err;
2974 return ret;
2975}
2976
2977static int del_balance_item(struct btrfs_root *root)
2978{
2979 struct btrfs_trans_handle *trans;
2980 struct btrfs_path *path;
2981 struct btrfs_key key;
2982 int ret, err;
2983
2984 path = btrfs_alloc_path();
2985 if (!path)
2986 return -ENOMEM;
2987
2988 trans = btrfs_start_transaction(root, 0);
2989 if (IS_ERR(trans)) {
2990 btrfs_free_path(path);
2991 return PTR_ERR(trans);
2992 }
2993
2994 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01002995 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002996 key.offset = 0;
2997
2998 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2999 if (ret < 0)
3000 goto out;
3001 if (ret > 0) {
3002 ret = -ENOENT;
3003 goto out;
3004 }
3005
3006 ret = btrfs_del_item(trans, root, path);
3007out:
3008 btrfs_free_path(path);
3009 err = btrfs_commit_transaction(trans, root);
3010 if (err && !ret)
3011 ret = err;
3012 return ret;
3013}
3014
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003015/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003016 * This is a heuristic used to reduce the number of chunks balanced on
3017 * resume after balance was interrupted.
3018 */
3019static void update_balance_args(struct btrfs_balance_control *bctl)
3020{
3021 /*
3022 * Turn on soft mode for chunk types that were being converted.
3023 */
3024 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3025 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3026 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3027 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3028 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3029 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3030
3031 /*
3032 * Turn on usage filter if is not already used. The idea is
3033 * that chunks that we have already balanced should be
3034 * reasonably full. Don't do it for chunks that are being
3035 * converted - that will keep us from relocating unconverted
3036 * (albeit full) chunks.
3037 */
3038 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003039 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003040 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3041 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3042 bctl->data.usage = 90;
3043 }
3044 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003045 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003046 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3047 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3048 bctl->sys.usage = 90;
3049 }
3050 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003051 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003052 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3053 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3054 bctl->meta.usage = 90;
3055 }
3056}
3057
3058/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003059 * Should be called with both balance and volume mutexes held to
3060 * serialize other volume operations (add_dev/rm_dev/resize) with
3061 * restriper. Same goes for unset_balance_control.
3062 */
3063static void set_balance_control(struct btrfs_balance_control *bctl)
3064{
3065 struct btrfs_fs_info *fs_info = bctl->fs_info;
3066
3067 BUG_ON(fs_info->balance_ctl);
3068
3069 spin_lock(&fs_info->balance_lock);
3070 fs_info->balance_ctl = bctl;
3071 spin_unlock(&fs_info->balance_lock);
3072}
3073
3074static void unset_balance_control(struct btrfs_fs_info *fs_info)
3075{
3076 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3077
3078 BUG_ON(!fs_info->balance_ctl);
3079
3080 spin_lock(&fs_info->balance_lock);
3081 fs_info->balance_ctl = NULL;
3082 spin_unlock(&fs_info->balance_lock);
3083
3084 kfree(bctl);
3085}
3086
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003087/*
3088 * Balance filters. Return 1 if chunk should be filtered out
3089 * (should not be balanced).
3090 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003091static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003092 struct btrfs_balance_args *bargs)
3093{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003094 chunk_type = chunk_to_extended(chunk_type) &
3095 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003096
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003097 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003098 return 0;
3099
3100 return 1;
3101}
3102
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003103static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003104 struct btrfs_balance_args *bargs)
3105{
3106 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003107 u64 chunk_used;
3108 u64 user_thresh_min;
3109 u64 user_thresh_max;
3110 int ret = 1;
3111
3112 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3113 chunk_used = btrfs_block_group_used(&cache->item);
3114
3115 if (bargs->usage_min == 0)
3116 user_thresh_min = 0;
3117 else
3118 user_thresh_min = div_factor_fine(cache->key.offset,
3119 bargs->usage_min);
3120
3121 if (bargs->usage_max == 0)
3122 user_thresh_max = 1;
3123 else if (bargs->usage_max > 100)
3124 user_thresh_max = cache->key.offset;
3125 else
3126 user_thresh_max = div_factor_fine(cache->key.offset,
3127 bargs->usage_max);
3128
3129 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3130 ret = 0;
3131
3132 btrfs_put_block_group(cache);
3133 return ret;
3134}
3135
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003136static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003137 u64 chunk_offset, struct btrfs_balance_args *bargs)
3138{
3139 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003140 u64 chunk_used, user_thresh;
3141 int ret = 1;
3142
3143 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3144 chunk_used = btrfs_block_group_used(&cache->item);
3145
David Sterbabc309462015-10-20 18:22:13 +02003146 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003147 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003148 else if (bargs->usage > 100)
3149 user_thresh = cache->key.offset;
3150 else
3151 user_thresh = div_factor_fine(cache->key.offset,
3152 bargs->usage);
3153
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003154 if (chunk_used < user_thresh)
3155 ret = 0;
3156
3157 btrfs_put_block_group(cache);
3158 return ret;
3159}
3160
Ilya Dryomov409d4042012-01-16 22:04:47 +02003161static int chunk_devid_filter(struct extent_buffer *leaf,
3162 struct btrfs_chunk *chunk,
3163 struct btrfs_balance_args *bargs)
3164{
3165 struct btrfs_stripe *stripe;
3166 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3167 int i;
3168
3169 for (i = 0; i < num_stripes; i++) {
3170 stripe = btrfs_stripe_nr(chunk, i);
3171 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3172 return 0;
3173 }
3174
3175 return 1;
3176}
3177
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003178/* [pstart, pend) */
3179static int chunk_drange_filter(struct extent_buffer *leaf,
3180 struct btrfs_chunk *chunk,
3181 u64 chunk_offset,
3182 struct btrfs_balance_args *bargs)
3183{
3184 struct btrfs_stripe *stripe;
3185 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3186 u64 stripe_offset;
3187 u64 stripe_length;
3188 int factor;
3189 int i;
3190
3191 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3192 return 0;
3193
3194 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003195 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3196 factor = num_stripes / 2;
3197 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3198 factor = num_stripes - 1;
3199 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3200 factor = num_stripes - 2;
3201 } else {
3202 factor = num_stripes;
3203 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003204
3205 for (i = 0; i < num_stripes; i++) {
3206 stripe = btrfs_stripe_nr(chunk, i);
3207 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3208 continue;
3209
3210 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3211 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003212 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003213
3214 if (stripe_offset < bargs->pend &&
3215 stripe_offset + stripe_length > bargs->pstart)
3216 return 0;
3217 }
3218
3219 return 1;
3220}
3221
Ilya Dryomovea671762012-01-16 22:04:48 +02003222/* [vstart, vend) */
3223static int chunk_vrange_filter(struct extent_buffer *leaf,
3224 struct btrfs_chunk *chunk,
3225 u64 chunk_offset,
3226 struct btrfs_balance_args *bargs)
3227{
3228 if (chunk_offset < bargs->vend &&
3229 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3230 /* at least part of the chunk is inside this vrange */
3231 return 0;
3232
3233 return 1;
3234}
3235
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003236static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3237 struct btrfs_chunk *chunk,
3238 struct btrfs_balance_args *bargs)
3239{
3240 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3241
3242 if (bargs->stripes_min <= num_stripes
3243 && num_stripes <= bargs->stripes_max)
3244 return 0;
3245
3246 return 1;
3247}
3248
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003249static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003250 struct btrfs_balance_args *bargs)
3251{
3252 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3253 return 0;
3254
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003255 chunk_type = chunk_to_extended(chunk_type) &
3256 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003257
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003258 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003259 return 1;
3260
3261 return 0;
3262}
3263
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003264static int should_balance_chunk(struct btrfs_root *root,
3265 struct extent_buffer *leaf,
3266 struct btrfs_chunk *chunk, u64 chunk_offset)
3267{
3268 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3269 struct btrfs_balance_args *bargs = NULL;
3270 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3271
3272 /* type filter */
3273 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3274 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3275 return 0;
3276 }
3277
3278 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3279 bargs = &bctl->data;
3280 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3281 bargs = &bctl->sys;
3282 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3283 bargs = &bctl->meta;
3284
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003285 /* profiles filter */
3286 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3287 chunk_profiles_filter(chunk_type, bargs)) {
3288 return 0;
3289 }
3290
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003291 /* usage filter */
3292 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3293 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3294 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003295 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3296 chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) {
3297 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003298 }
3299
Ilya Dryomov409d4042012-01-16 22:04:47 +02003300 /* devid filter */
3301 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3302 chunk_devid_filter(leaf, chunk, bargs)) {
3303 return 0;
3304 }
3305
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003306 /* drange filter, makes sense only with devid filter */
3307 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3308 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3309 return 0;
3310 }
3311
Ilya Dryomovea671762012-01-16 22:04:48 +02003312 /* vrange filter */
3313 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3314 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3315 return 0;
3316 }
3317
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003318 /* stripes filter */
3319 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3320 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3321 return 0;
3322 }
3323
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003324 /* soft profile changing mode */
3325 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3326 chunk_soft_convert_filter(chunk_type, bargs)) {
3327 return 0;
3328 }
3329
David Sterba7d824b62014-05-07 17:37:51 +02003330 /*
3331 * limited by count, must be the last filter
3332 */
3333 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3334 if (bargs->limit == 0)
3335 return 0;
3336 else
3337 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003338 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3339 /*
3340 * Same logic as the 'limit' filter; the minimum cannot be
3341 * determined here because we do not have the global informatoin
3342 * about the count of all chunks that satisfy the filters.
3343 */
3344 if (bargs->limit_max == 0)
3345 return 0;
3346 else
3347 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003348 }
3349
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003350 return 1;
3351}
3352
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003353static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003354{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003355 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003356 struct btrfs_root *chunk_root = fs_info->chunk_root;
3357 struct btrfs_root *dev_root = fs_info->dev_root;
3358 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003359 struct btrfs_device *device;
3360 u64 old_size;
3361 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003362 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003363 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003364 struct btrfs_path *path;
3365 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003366 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003367 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003368 struct extent_buffer *leaf;
3369 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003370 int ret;
3371 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003372 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003373 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003374 u64 limit_data = bctl->data.limit;
3375 u64 limit_meta = bctl->meta.limit;
3376 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003377 u32 count_data = 0;
3378 u32 count_meta = 0;
3379 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003380 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003381
Chris Masonec44a352008-04-28 15:29:52 -04003382 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003383 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003384 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003385 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003386 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003387 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003388 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003389 btrfs_device_get_total_bytes(device) -
3390 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003391 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003392 continue;
3393
3394 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003395 if (ret == -ENOSPC)
3396 break;
Chris Masonec44a352008-04-28 15:29:52 -04003397 BUG_ON(ret);
3398
Yan, Zhenga22285a2010-05-16 10:48:46 -04003399 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003400 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003401
3402 ret = btrfs_grow_device(trans, device, old_size);
3403 BUG_ON(ret);
3404
3405 btrfs_end_transaction(trans, dev_root);
3406 }
3407
3408 /* step two, relocate all the chunks */
3409 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003410 if (!path) {
3411 ret = -ENOMEM;
3412 goto error;
3413 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003414
3415 /* zero out stat counters */
3416 spin_lock(&fs_info->balance_lock);
3417 memset(&bctl->stat, 0, sizeof(bctl->stat));
3418 spin_unlock(&fs_info->balance_lock);
3419again:
David Sterba7d824b62014-05-07 17:37:51 +02003420 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003421 /*
3422 * The single value limit and min/max limits use the same bytes
3423 * in the
3424 */
David Sterba7d824b62014-05-07 17:37:51 +02003425 bctl->data.limit = limit_data;
3426 bctl->meta.limit = limit_meta;
3427 bctl->sys.limit = limit_sys;
3428 }
Chris Masonec44a352008-04-28 15:29:52 -04003429 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3430 key.offset = (u64)-1;
3431 key.type = BTRFS_CHUNK_ITEM_KEY;
3432
Chris Masond3977122009-01-05 21:25:51 -05003433 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003434 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003435 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003436 ret = -ECANCELED;
3437 goto error;
3438 }
3439
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003440 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003441 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003442 if (ret < 0) {
3443 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003444 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003445 }
Chris Masonec44a352008-04-28 15:29:52 -04003446
3447 /*
3448 * this shouldn't happen, it means the last relocate
3449 * failed
3450 */
3451 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003452 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003453
3454 ret = btrfs_previous_item(chunk_root, path, 0,
3455 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003456 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003457 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003458 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003459 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003460 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003461
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003462 leaf = path->nodes[0];
3463 slot = path->slots[0];
3464 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3465
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003466 if (found_key.objectid != key.objectid) {
3467 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003468 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003469 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003470
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003471 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003472 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003473
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003474 if (!counting) {
3475 spin_lock(&fs_info->balance_lock);
3476 bctl->stat.considered++;
3477 spin_unlock(&fs_info->balance_lock);
3478 }
3479
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003480 ret = should_balance_chunk(chunk_root, leaf, chunk,
3481 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003482
David Sterbab3b4aa72011-04-21 01:20:15 +02003483 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003484 if (!ret) {
3485 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003486 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003487 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003488
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003489 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003490 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003491 spin_lock(&fs_info->balance_lock);
3492 bctl->stat.expected++;
3493 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003494
3495 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3496 count_data++;
3497 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3498 count_sys++;
3499 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3500 count_meta++;
3501
3502 goto loop;
3503 }
3504
3505 /*
3506 * Apply limit_min filter, no need to check if the LIMITS
3507 * filter is used, limit_min is 0 by default
3508 */
3509 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3510 count_data < bctl->data.limit_min)
3511 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3512 count_meta < bctl->meta.limit_min)
3513 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3514 count_sys < bctl->sys.limit_min)) {
3515 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003516 goto loop;
3517 }
3518
Zhao Lei2c9fe832015-11-09 11:51:32 +08003519 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) && !chunk_reserved) {
3520 trans = btrfs_start_transaction(chunk_root, 0);
3521 if (IS_ERR(trans)) {
3522 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3523 ret = PTR_ERR(trans);
3524 goto error;
3525 }
3526
3527 ret = btrfs_force_chunk_alloc(trans, chunk_root,
3528 BTRFS_BLOCK_GROUP_DATA);
Filipe Manana8a7d6562015-12-10 11:08:59 +00003529 btrfs_end_transaction(trans, chunk_root);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003530 if (ret < 0) {
3531 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3532 goto error;
3533 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003534 chunk_reserved = 1;
3535 }
3536
Chris Masonec44a352008-04-28 15:29:52 -04003537 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003538 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003539 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003540 if (ret && ret != -ENOSPC)
3541 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003542 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003543 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003544 } else {
3545 spin_lock(&fs_info->balance_lock);
3546 bctl->stat.completed++;
3547 spin_unlock(&fs_info->balance_lock);
3548 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003549loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003550 if (found_key.offset == 0)
3551 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003552 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003553 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003554
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003555 if (counting) {
3556 btrfs_release_path(path);
3557 counting = false;
3558 goto again;
3559 }
Chris Masonec44a352008-04-28 15:29:52 -04003560error:
3561 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003562 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003563 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003564 enospc_errors);
3565 if (!ret)
3566 ret = -ENOSPC;
3567 }
3568
Chris Masonec44a352008-04-28 15:29:52 -04003569 return ret;
3570}
3571
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003572/**
3573 * alloc_profile_is_valid - see if a given profile is valid and reduced
3574 * @flags: profile to validate
3575 * @extended: if true @flags is treated as an extended profile
3576 */
3577static int alloc_profile_is_valid(u64 flags, int extended)
3578{
3579 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3580 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3581
3582 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3583
3584 /* 1) check that all other bits are zeroed */
3585 if (flags & ~mask)
3586 return 0;
3587
3588 /* 2) see if profile is reduced */
3589 if (flags == 0)
3590 return !extended; /* "0" is valid for usual profiles */
3591
3592 /* true if exactly one bit set */
3593 return (flags & (flags - 1)) == 0;
3594}
3595
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003596static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3597{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003598 /* cancel requested || normal exit path */
3599 return atomic_read(&fs_info->balance_cancel_req) ||
3600 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3601 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003602}
3603
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003604static void __cancel_balance(struct btrfs_fs_info *fs_info)
3605{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003606 int ret;
3607
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003608 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003609 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003610 if (ret)
Anand Jaina4553fe2015-09-25 14:43:01 +08003611 btrfs_std_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003612
3613 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003614}
3615
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003616/* Non-zero return value signifies invalidity */
3617static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3618 u64 allowed)
3619{
3620 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3621 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3622 (bctl_arg->target & ~allowed)));
3623}
3624
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003625/*
3626 * Should be called with both balance and volume mutexes held
3627 */
3628int btrfs_balance(struct btrfs_balance_control *bctl,
3629 struct btrfs_ioctl_balance_args *bargs)
3630{
3631 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003632 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003633 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003634 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003635 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003636 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003637
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003638 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003639 atomic_read(&fs_info->balance_pause_req) ||
3640 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003641 ret = -EINVAL;
3642 goto out;
3643 }
3644
Ilya Dryomove4837f82012-03-27 17:09:17 +03003645 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3646 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3647 mixed = 1;
3648
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003649 /*
3650 * In case of mixed groups both data and meta should be picked,
3651 * and identical options should be given for both of them.
3652 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003653 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3654 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003655 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3656 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3657 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003658 btrfs_err(fs_info, "with mixed groups data and "
3659 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003660 ret = -EINVAL;
3661 goto out;
3662 }
3663 }
3664
Stefan Behrens8dabb742012-11-06 13:15:27 +01003665 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003666 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003667 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3668 BUG_ON(num_devices < 1);
3669 num_devices--;
3670 }
Liu Bo73beece2015-07-17 16:49:19 +08003671 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003672 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003673 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003674 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003675 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003676 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003677 if (num_devices > 2)
3678 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3679 if (num_devices > 3)
3680 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3681 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003682 if (validate_convert_profile(&bctl->data, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003683 btrfs_err(fs_info, "unable to start balance with target "
3684 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003685 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003686 ret = -EINVAL;
3687 goto out;
3688 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003689 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003690 btrfs_err(fs_info,
3691 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003692 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003693 ret = -EINVAL;
3694 goto out;
3695 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003696 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003697 btrfs_err(fs_info,
3698 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003699 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003700 ret = -EINVAL;
3701 goto out;
3702 }
3703
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003704 /* allow to reduce meta or sys integrity only if force set */
3705 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003706 BTRFS_BLOCK_GROUP_RAID10 |
3707 BTRFS_BLOCK_GROUP_RAID5 |
3708 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003709 do {
3710 seq = read_seqbegin(&fs_info->profiles_lock);
3711
3712 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3713 (fs_info->avail_system_alloc_bits & allowed) &&
3714 !(bctl->sys.target & allowed)) ||
3715 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3716 (fs_info->avail_metadata_alloc_bits & allowed) &&
3717 !(bctl->meta.target & allowed))) {
3718 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003719 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003720 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003721 btrfs_err(fs_info, "balance will reduce metadata "
3722 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003723 ret = -EINVAL;
3724 goto out;
3725 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003726 }
Miao Xiede98ced2013-01-29 10:13:12 +00003727 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003728
Sam Tygieree592d02016-01-06 08:46:12 +00003729 if (btrfs_get_num_tolerated_disk_barrier_failures(bctl->meta.target) <
3730 btrfs_get_num_tolerated_disk_barrier_failures(bctl->data.target)) {
3731 btrfs_warn(fs_info,
Filipe Mananafedc0042016-01-15 10:56:15 +00003732 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Sam Tygieree592d02016-01-06 08:46:12 +00003733 bctl->meta.target, bctl->data.target);
3734 }
3735
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003736 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003737 fs_info->num_tolerated_disk_barrier_failures = min(
3738 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3739 btrfs_get_num_tolerated_disk_barrier_failures(
3740 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003741 }
3742
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003743 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003744 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003745 goto out;
3746
Ilya Dryomov59641012012-01-16 22:04:48 +02003747 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3748 BUG_ON(ret == -EEXIST);
3749 set_balance_control(bctl);
3750 } else {
3751 BUG_ON(ret != -EEXIST);
3752 spin_lock(&fs_info->balance_lock);
3753 update_balance_args(bctl);
3754 spin_unlock(&fs_info->balance_lock);
3755 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003756
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003757 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003758 mutex_unlock(&fs_info->balance_mutex);
3759
3760 ret = __btrfs_balance(fs_info);
3761
3762 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003763 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003764
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003765 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3766 fs_info->num_tolerated_disk_barrier_failures =
3767 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3768 }
3769
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003770 if (bargs) {
3771 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003772 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003773 }
3774
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003775 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3776 balance_need_close(fs_info)) {
3777 __cancel_balance(fs_info);
3778 }
3779
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003780 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003781
3782 return ret;
3783out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003784 if (bctl->flags & BTRFS_BALANCE_RESUME)
3785 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003786 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003787 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003788 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3789 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003790 return ret;
3791}
3792
3793static int balance_kthread(void *data)
3794{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003795 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003796 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003797
3798 mutex_lock(&fs_info->volume_mutex);
3799 mutex_lock(&fs_info->balance_mutex);
3800
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003801 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003802 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003803 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003804 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003805
3806 mutex_unlock(&fs_info->balance_mutex);
3807 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003808
Ilya Dryomov59641012012-01-16 22:04:48 +02003809 return ret;
3810}
3811
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003812int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3813{
3814 struct task_struct *tsk;
3815
3816 spin_lock(&fs_info->balance_lock);
3817 if (!fs_info->balance_ctl) {
3818 spin_unlock(&fs_info->balance_lock);
3819 return 0;
3820 }
3821 spin_unlock(&fs_info->balance_lock);
3822
3823 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003824 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003825 return 0;
3826 }
3827
3828 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303829 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003830}
3831
Ilya Dryomov68310a52012-06-22 12:24:12 -06003832int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003833{
Ilya Dryomov59641012012-01-16 22:04:48 +02003834 struct btrfs_balance_control *bctl;
3835 struct btrfs_balance_item *item;
3836 struct btrfs_disk_balance_args disk_bargs;
3837 struct btrfs_path *path;
3838 struct extent_buffer *leaf;
3839 struct btrfs_key key;
3840 int ret;
3841
3842 path = btrfs_alloc_path();
3843 if (!path)
3844 return -ENOMEM;
3845
Ilya Dryomov68310a52012-06-22 12:24:12 -06003846 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003847 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003848 key.offset = 0;
3849
3850 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3851 if (ret < 0)
3852 goto out;
3853 if (ret > 0) { /* ret = -ENOENT; */
3854 ret = 0;
3855 goto out;
3856 }
3857
Ilya Dryomov59641012012-01-16 22:04:48 +02003858 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3859 if (!bctl) {
3860 ret = -ENOMEM;
3861 goto out;
3862 }
3863
Ilya Dryomov59641012012-01-16 22:04:48 +02003864 leaf = path->nodes[0];
3865 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3866
Ilya Dryomov68310a52012-06-22 12:24:12 -06003867 bctl->fs_info = fs_info;
3868 bctl->flags = btrfs_balance_flags(leaf, item);
3869 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003870
3871 btrfs_balance_data(leaf, item, &disk_bargs);
3872 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3873 btrfs_balance_meta(leaf, item, &disk_bargs);
3874 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3875 btrfs_balance_sys(leaf, item, &disk_bargs);
3876 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3877
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003878 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3879
Ilya Dryomov68310a52012-06-22 12:24:12 -06003880 mutex_lock(&fs_info->volume_mutex);
3881 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003882
Ilya Dryomov68310a52012-06-22 12:24:12 -06003883 set_balance_control(bctl);
3884
3885 mutex_unlock(&fs_info->balance_mutex);
3886 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003887out:
3888 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003889 return ret;
3890}
3891
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003892int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3893{
3894 int ret = 0;
3895
3896 mutex_lock(&fs_info->balance_mutex);
3897 if (!fs_info->balance_ctl) {
3898 mutex_unlock(&fs_info->balance_mutex);
3899 return -ENOTCONN;
3900 }
3901
3902 if (atomic_read(&fs_info->balance_running)) {
3903 atomic_inc(&fs_info->balance_pause_req);
3904 mutex_unlock(&fs_info->balance_mutex);
3905
3906 wait_event(fs_info->balance_wait_q,
3907 atomic_read(&fs_info->balance_running) == 0);
3908
3909 mutex_lock(&fs_info->balance_mutex);
3910 /* we are good with balance_ctl ripped off from under us */
3911 BUG_ON(atomic_read(&fs_info->balance_running));
3912 atomic_dec(&fs_info->balance_pause_req);
3913 } else {
3914 ret = -ENOTCONN;
3915 }
3916
3917 mutex_unlock(&fs_info->balance_mutex);
3918 return ret;
3919}
3920
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003921int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3922{
Ilya Dryomove649e582013-10-10 20:40:21 +03003923 if (fs_info->sb->s_flags & MS_RDONLY)
3924 return -EROFS;
3925
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003926 mutex_lock(&fs_info->balance_mutex);
3927 if (!fs_info->balance_ctl) {
3928 mutex_unlock(&fs_info->balance_mutex);
3929 return -ENOTCONN;
3930 }
3931
3932 atomic_inc(&fs_info->balance_cancel_req);
3933 /*
3934 * if we are running just wait and return, balance item is
3935 * deleted in btrfs_balance in this case
3936 */
3937 if (atomic_read(&fs_info->balance_running)) {
3938 mutex_unlock(&fs_info->balance_mutex);
3939 wait_event(fs_info->balance_wait_q,
3940 atomic_read(&fs_info->balance_running) == 0);
3941 mutex_lock(&fs_info->balance_mutex);
3942 } else {
3943 /* __cancel_balance needs volume_mutex */
3944 mutex_unlock(&fs_info->balance_mutex);
3945 mutex_lock(&fs_info->volume_mutex);
3946 mutex_lock(&fs_info->balance_mutex);
3947
3948 if (fs_info->balance_ctl)
3949 __cancel_balance(fs_info);
3950
3951 mutex_unlock(&fs_info->volume_mutex);
3952 }
3953
3954 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3955 atomic_dec(&fs_info->balance_cancel_req);
3956 mutex_unlock(&fs_info->balance_mutex);
3957 return 0;
3958}
3959
Stefan Behrens803b2f52013-08-15 17:11:21 +02003960static int btrfs_uuid_scan_kthread(void *data)
3961{
3962 struct btrfs_fs_info *fs_info = data;
3963 struct btrfs_root *root = fs_info->tree_root;
3964 struct btrfs_key key;
3965 struct btrfs_key max_key;
3966 struct btrfs_path *path = NULL;
3967 int ret = 0;
3968 struct extent_buffer *eb;
3969 int slot;
3970 struct btrfs_root_item root_item;
3971 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003972 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003973
3974 path = btrfs_alloc_path();
3975 if (!path) {
3976 ret = -ENOMEM;
3977 goto out;
3978 }
3979
3980 key.objectid = 0;
3981 key.type = BTRFS_ROOT_ITEM_KEY;
3982 key.offset = 0;
3983
3984 max_key.objectid = (u64)-1;
3985 max_key.type = BTRFS_ROOT_ITEM_KEY;
3986 max_key.offset = (u64)-1;
3987
Stefan Behrens803b2f52013-08-15 17:11:21 +02003988 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003989 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003990 if (ret) {
3991 if (ret > 0)
3992 ret = 0;
3993 break;
3994 }
3995
3996 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3997 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3998 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3999 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4000 goto skip;
4001
4002 eb = path->nodes[0];
4003 slot = path->slots[0];
4004 item_size = btrfs_item_size_nr(eb, slot);
4005 if (item_size < sizeof(root_item))
4006 goto skip;
4007
Stefan Behrens803b2f52013-08-15 17:11:21 +02004008 read_extent_buffer(eb, &root_item,
4009 btrfs_item_ptr_offset(eb, slot),
4010 (int)sizeof(root_item));
4011 if (btrfs_root_refs(&root_item) == 0)
4012 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004013
4014 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4015 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4016 if (trans)
4017 goto update_tree;
4018
4019 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004020 /*
4021 * 1 - subvol uuid item
4022 * 1 - received_subvol uuid item
4023 */
4024 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4025 if (IS_ERR(trans)) {
4026 ret = PTR_ERR(trans);
4027 break;
4028 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004029 continue;
4030 } else {
4031 goto skip;
4032 }
4033update_tree:
4034 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004035 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4036 root_item.uuid,
4037 BTRFS_UUID_KEY_SUBVOL,
4038 key.objectid);
4039 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004040 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004041 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004042 break;
4043 }
4044 }
4045
4046 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004047 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4048 root_item.received_uuid,
4049 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4050 key.objectid);
4051 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004052 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004053 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004054 break;
4055 }
4056 }
4057
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004058skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004059 if (trans) {
4060 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004061 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004062 if (ret)
4063 break;
4064 }
4065
Stefan Behrens803b2f52013-08-15 17:11:21 +02004066 btrfs_release_path(path);
4067 if (key.offset < (u64)-1) {
4068 key.offset++;
4069 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4070 key.offset = 0;
4071 key.type = BTRFS_ROOT_ITEM_KEY;
4072 } else if (key.objectid < (u64)-1) {
4073 key.offset = 0;
4074 key.type = BTRFS_ROOT_ITEM_KEY;
4075 key.objectid++;
4076 } else {
4077 break;
4078 }
4079 cond_resched();
4080 }
4081
4082out:
4083 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004084 if (trans && !IS_ERR(trans))
4085 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004086 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004087 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004088 else
4089 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004090 up(&fs_info->uuid_tree_rescan_sem);
4091 return 0;
4092}
4093
Stefan Behrens70f80172013-08-15 17:11:23 +02004094/*
4095 * Callback for btrfs_uuid_tree_iterate().
4096 * returns:
4097 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004098 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004099 * > 0 if the check failed, which means the caller shall remove the entry.
4100 */
4101static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4102 u8 *uuid, u8 type, u64 subid)
4103{
4104 struct btrfs_key key;
4105 int ret = 0;
4106 struct btrfs_root *subvol_root;
4107
4108 if (type != BTRFS_UUID_KEY_SUBVOL &&
4109 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4110 goto out;
4111
4112 key.objectid = subid;
4113 key.type = BTRFS_ROOT_ITEM_KEY;
4114 key.offset = (u64)-1;
4115 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4116 if (IS_ERR(subvol_root)) {
4117 ret = PTR_ERR(subvol_root);
4118 if (ret == -ENOENT)
4119 ret = 1;
4120 goto out;
4121 }
4122
4123 switch (type) {
4124 case BTRFS_UUID_KEY_SUBVOL:
4125 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4126 ret = 1;
4127 break;
4128 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4129 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4130 BTRFS_UUID_SIZE))
4131 ret = 1;
4132 break;
4133 }
4134
4135out:
4136 return ret;
4137}
4138
4139static int btrfs_uuid_rescan_kthread(void *data)
4140{
4141 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4142 int ret;
4143
4144 /*
4145 * 1st step is to iterate through the existing UUID tree and
4146 * to delete all entries that contain outdated data.
4147 * 2nd step is to add all missing entries to the UUID tree.
4148 */
4149 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4150 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004151 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004152 up(&fs_info->uuid_tree_rescan_sem);
4153 return ret;
4154 }
4155 return btrfs_uuid_scan_kthread(data);
4156}
4157
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004158int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4159{
4160 struct btrfs_trans_handle *trans;
4161 struct btrfs_root *tree_root = fs_info->tree_root;
4162 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004163 struct task_struct *task;
4164 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004165
4166 /*
4167 * 1 - root node
4168 * 1 - root item
4169 */
4170 trans = btrfs_start_transaction(tree_root, 2);
4171 if (IS_ERR(trans))
4172 return PTR_ERR(trans);
4173
4174 uuid_root = btrfs_create_tree(trans, fs_info,
4175 BTRFS_UUID_TREE_OBJECTID);
4176 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004177 ret = PTR_ERR(uuid_root);
4178 btrfs_abort_transaction(trans, tree_root, ret);
4179 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004180 }
4181
4182 fs_info->uuid_root = uuid_root;
4183
Stefan Behrens803b2f52013-08-15 17:11:21 +02004184 ret = btrfs_commit_transaction(trans, tree_root);
4185 if (ret)
4186 return ret;
4187
4188 down(&fs_info->uuid_tree_rescan_sem);
4189 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4190 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004191 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004192 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004193 up(&fs_info->uuid_tree_rescan_sem);
4194 return PTR_ERR(task);
4195 }
4196
4197 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004198}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004199
Stefan Behrens70f80172013-08-15 17:11:23 +02004200int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4201{
4202 struct task_struct *task;
4203
4204 down(&fs_info->uuid_tree_rescan_sem);
4205 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4206 if (IS_ERR(task)) {
4207 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004208 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004209 up(&fs_info->uuid_tree_rescan_sem);
4210 return PTR_ERR(task);
4211 }
4212
4213 return 0;
4214}
4215
Chris Mason8f18cf12008-04-25 16:53:30 -04004216/*
4217 * shrinking a device means finding all of the device extents past
4218 * the new size, and then following the back refs to the chunks.
4219 * The chunk relocation code actually frees the device extent
4220 */
4221int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4222{
4223 struct btrfs_trans_handle *trans;
4224 struct btrfs_root *root = device->dev_root;
4225 struct btrfs_dev_extent *dev_extent = NULL;
4226 struct btrfs_path *path;
4227 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004228 u64 chunk_offset;
4229 int ret;
4230 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004231 int failed = 0;
4232 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004233 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004234 struct extent_buffer *l;
4235 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004236 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004237 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004238 u64 old_size = btrfs_device_get_total_bytes(device);
4239 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004240
Stefan Behrens63a212a2012-11-05 18:29:28 +01004241 if (device->is_tgtdev_for_dev_replace)
4242 return -EINVAL;
4243
Chris Mason8f18cf12008-04-25 16:53:30 -04004244 path = btrfs_alloc_path();
4245 if (!path)
4246 return -ENOMEM;
4247
David Sterbae4058b52015-11-27 16:31:35 +01004248 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004249
Chris Mason7d9eb122008-07-08 14:19:17 -04004250 lock_chunks(root);
4251
Miao Xie7cc8e582014-09-03 21:35:38 +08004252 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004253 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004254 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004255 spin_lock(&root->fs_info->free_chunk_lock);
4256 root->fs_info->free_chunk_space -= diff;
4257 spin_unlock(&root->fs_info->free_chunk_lock);
4258 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004259 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004260
Josef Bacikba1bf482009-09-11 16:11:19 -04004261again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004262 key.objectid = device->devid;
4263 key.offset = (u64)-1;
4264 key.type = BTRFS_DEV_EXTENT_KEY;
4265
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004266 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004267 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004268 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004269 if (ret < 0) {
4270 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004271 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004272 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004273
4274 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004275 if (ret)
4276 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004277 if (ret < 0)
4278 goto done;
4279 if (ret) {
4280 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004281 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004282 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004283 }
4284
4285 l = path->nodes[0];
4286 slot = path->slots[0];
4287 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4288
Josef Bacikba1bf482009-09-11 16:11:19 -04004289 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004290 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004291 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004292 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004293 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004294
4295 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4296 length = btrfs_dev_extent_length(l, dev_extent);
4297
Josef Bacikba1bf482009-09-11 16:11:19 -04004298 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004299 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004300 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004301 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004302 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004303
Chris Mason8f18cf12008-04-25 16:53:30 -04004304 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004305 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004306
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004307 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004308 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004309 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004310 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004311 if (ret == -ENOSPC)
4312 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004313 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004314
4315 if (failed && !retried) {
4316 failed = 0;
4317 retried = true;
4318 goto again;
4319 } else if (failed && retried) {
4320 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004321 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004322 }
4323
Chris Balld6397ba2009-04-27 07:29:03 -04004324 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004325 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004326 if (IS_ERR(trans)) {
4327 ret = PTR_ERR(trans);
4328 goto done;
4329 }
4330
Chris Balld6397ba2009-04-27 07:29:03 -04004331 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004332
4333 /*
4334 * We checked in the above loop all device extents that were already in
4335 * the device tree. However before we have updated the device's
4336 * total_bytes to the new size, we might have had chunk allocations that
4337 * have not complete yet (new block groups attached to transaction
4338 * handles), and therefore their device extents were not yet in the
4339 * device tree and we missed them in the loop above. So if we have any
4340 * pending chunk using a device extent that overlaps the device range
4341 * that we can not use anymore, commit the current transaction and
4342 * repeat the search on the device tree - this way we guarantee we will
4343 * not have chunks using device extents that end beyond 'new_size'.
4344 */
4345 if (!checked_pending_chunks) {
4346 u64 start = new_size;
4347 u64 len = old_size - new_size;
4348
Jeff Mahoney499f3772015-06-15 09:41:17 -04004349 if (contains_pending_extent(trans->transaction, device,
4350 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004351 unlock_chunks(root);
4352 checked_pending_chunks = true;
4353 failed = 0;
4354 retried = false;
4355 ret = btrfs_commit_transaction(trans, root);
4356 if (ret)
4357 goto done;
4358 goto again;
4359 }
4360 }
4361
Miao Xie7cc8e582014-09-03 21:35:38 +08004362 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004363 if (list_empty(&device->resized_list))
4364 list_add_tail(&device->resized_list,
4365 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004366
Chris Balld6397ba2009-04-27 07:29:03 -04004367 WARN_ON(diff > old_total);
4368 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4369 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004370
4371 /* Now btrfs_update_device() will change the on-disk size. */
4372 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004373 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004374done:
4375 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004376 if (ret) {
4377 lock_chunks(root);
4378 btrfs_device_set_total_bytes(device, old_size);
4379 if (device->writeable)
4380 device->fs_devices->total_rw_bytes += diff;
4381 spin_lock(&root->fs_info->free_chunk_lock);
4382 root->fs_info->free_chunk_space += diff;
4383 spin_unlock(&root->fs_info->free_chunk_lock);
4384 unlock_chunks(root);
4385 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004386 return ret;
4387}
4388
Li Zefan125ccb02011-12-08 15:07:24 +08004389static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004390 struct btrfs_key *key,
4391 struct btrfs_chunk *chunk, int item_size)
4392{
David Sterba6c417612011-04-13 15:41:04 +02004393 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004394 struct btrfs_disk_key disk_key;
4395 u32 array_size;
4396 u8 *ptr;
4397
Miao Xiefe48a5c2014-09-03 21:35:39 +08004398 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004399 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004400 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004401 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4402 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004403 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004404 }
Chris Mason0b86a832008-03-24 15:01:56 -04004405
4406 ptr = super_copy->sys_chunk_array + array_size;
4407 btrfs_cpu_key_to_disk(&disk_key, key);
4408 memcpy(ptr, &disk_key, sizeof(disk_key));
4409 ptr += sizeof(disk_key);
4410 memcpy(ptr, chunk, item_size);
4411 item_size += sizeof(disk_key);
4412 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004413 unlock_chunks(root);
4414
Chris Mason0b86a832008-03-24 15:01:56 -04004415 return 0;
4416}
4417
Miao Xieb2117a32011-01-05 10:07:28 +00004418/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004419 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004420 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004421static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004422{
Arne Jansen73c5de02011-04-12 12:07:57 +02004423 const struct btrfs_device_info *di_a = a;
4424 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004425
Arne Jansen73c5de02011-04-12 12:07:57 +02004426 if (di_a->max_avail > di_b->max_avail)
4427 return -1;
4428 if (di_a->max_avail < di_b->max_avail)
4429 return 1;
4430 if (di_a->total_avail > di_b->total_avail)
4431 return -1;
4432 if (di_a->total_avail < di_b->total_avail)
4433 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004434 return 0;
4435}
4436
David Woodhouse53b381b2013-01-29 18:40:14 -05004437static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4438{
4439 /* TODO allow them to set a preferred stripe size */
Byongho Leeee221842015-12-15 01:42:10 +09004440 return SZ_64K;
David Woodhouse53b381b2013-01-29 18:40:14 -05004441}
4442
4443static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4444{
Zhao Leiffe2d202015-01-20 15:11:44 +08004445 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004446 return;
4447
Miao Xieceda0862013-04-11 10:30:16 +00004448 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004449}
4450
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004451#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4452 - sizeof(struct btrfs_item) \
4453 - sizeof(struct btrfs_chunk)) \
4454 / sizeof(struct btrfs_stripe) + 1)
4455
4456#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4457 - 2 * sizeof(struct btrfs_disk_key) \
4458 - 2 * sizeof(struct btrfs_chunk)) \
4459 / sizeof(struct btrfs_stripe) + 1)
4460
Miao Xieb2117a32011-01-05 10:07:28 +00004461static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004462 struct btrfs_root *extent_root, u64 start,
4463 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004464{
4465 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004466 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4467 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004468 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004469 struct extent_map_tree *em_tree;
4470 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004471 struct btrfs_device_info *devices_info = NULL;
4472 u64 total_avail;
4473 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004474 int data_stripes; /* number of stripes that count for
4475 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004476 int sub_stripes; /* sub_stripes info for map */
4477 int dev_stripes; /* stripes per dev */
4478 int devs_max; /* max devs to use */
4479 int devs_min; /* min devs needed */
4480 int devs_increment; /* ndevs has to be a multiple of this */
4481 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004482 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004483 u64 max_stripe_size;
4484 u64 max_chunk_size;
4485 u64 stripe_size;
4486 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004487 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004488 int ndevs;
4489 int i;
4490 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004491 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004492
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004493 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004494
Miao Xieb2117a32011-01-05 10:07:28 +00004495 if (list_empty(&fs_devices->alloc_list))
4496 return -ENOSPC;
4497
Liu Bo31e50222012-11-21 14:18:10 +00004498 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004499
Liu Bo31e50222012-11-21 14:18:10 +00004500 sub_stripes = btrfs_raid_array[index].sub_stripes;
4501 dev_stripes = btrfs_raid_array[index].dev_stripes;
4502 devs_max = btrfs_raid_array[index].devs_max;
4503 devs_min = btrfs_raid_array[index].devs_min;
4504 devs_increment = btrfs_raid_array[index].devs_increment;
4505 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004506
4507 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004508 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004509 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004510 if (!devs_max)
4511 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004512 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004513 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004514 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4515 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004516 else
Byongho Leeee221842015-12-15 01:42:10 +09004517 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004518 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004519 if (!devs_max)
4520 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004521 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004522 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004523 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004524 if (!devs_max)
4525 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004526 } else {
David Sterba351fd352014-05-15 16:48:20 +02004527 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004528 type);
4529 BUG_ON(1);
4530 }
4531
4532 /* we don't want a chunk larger than 10% of writeable space */
4533 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4534 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004535
David Sterba31e818f2015-02-20 18:00:26 +01004536 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004537 GFP_NOFS);
4538 if (!devices_info)
4539 return -ENOMEM;
4540
Arne Jansen73c5de02011-04-12 12:07:57 +02004541 cur = fs_devices->alloc_list.next;
4542
4543 /*
4544 * in the first pass through the devices list, we gather information
4545 * about the available holes on each device.
4546 */
4547 ndevs = 0;
4548 while (cur != &fs_devices->alloc_list) {
4549 struct btrfs_device *device;
4550 u64 max_avail;
4551 u64 dev_offset;
4552
4553 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4554
4555 cur = cur->next;
4556
4557 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004558 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004559 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004560 continue;
4561 }
4562
Stefan Behrens63a212a2012-11-05 18:29:28 +01004563 if (!device->in_fs_metadata ||
4564 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004565 continue;
4566
4567 if (device->total_bytes > device->bytes_used)
4568 total_avail = device->total_bytes - device->bytes_used;
4569 else
4570 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004571
4572 /* If there is no space on this device, skip it. */
4573 if (total_avail == 0)
4574 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004575
Josef Bacik6df9a952013-06-27 13:22:46 -04004576 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004577 max_stripe_size * dev_stripes,
4578 &dev_offset, &max_avail);
4579 if (ret && ret != -ENOSPC)
4580 goto error;
4581
4582 if (ret == 0)
4583 max_avail = max_stripe_size * dev_stripes;
4584
4585 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4586 continue;
4587
Eric Sandeen063d0062013-01-31 00:55:01 +00004588 if (ndevs == fs_devices->rw_devices) {
4589 WARN(1, "%s: found more than %llu devices\n",
4590 __func__, fs_devices->rw_devices);
4591 break;
4592 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004593 devices_info[ndevs].dev_offset = dev_offset;
4594 devices_info[ndevs].max_avail = max_avail;
4595 devices_info[ndevs].total_avail = total_avail;
4596 devices_info[ndevs].dev = device;
4597 ++ndevs;
4598 }
4599
4600 /*
4601 * now sort the devices by hole size / available space
4602 */
4603 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4604 btrfs_cmp_device_info, NULL);
4605
4606 /* round down to number of usable stripes */
4607 ndevs -= ndevs % devs_increment;
4608
4609 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4610 ret = -ENOSPC;
4611 goto error;
4612 }
4613
4614 if (devs_max && ndevs > devs_max)
4615 ndevs = devs_max;
4616 /*
4617 * the primary goal is to maximize the number of stripes, so use as many
4618 * devices as possible, even if the stripes are not maximum sized.
4619 */
4620 stripe_size = devices_info[ndevs-1].max_avail;
4621 num_stripes = ndevs * dev_stripes;
4622
David Woodhouse53b381b2013-01-29 18:40:14 -05004623 /*
4624 * this will have to be fixed for RAID1 and RAID10 over
4625 * more drives
4626 */
4627 data_stripes = num_stripes / ncopies;
4628
David Woodhouse53b381b2013-01-29 18:40:14 -05004629 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4630 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4631 btrfs_super_stripesize(info->super_copy));
4632 data_stripes = num_stripes - 1;
4633 }
4634 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4635 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4636 btrfs_super_stripesize(info->super_copy));
4637 data_stripes = num_stripes - 2;
4638 }
Chris Mason86db2572013-02-20 16:23:40 -05004639
4640 /*
4641 * Use the number of data stripes to figure out how big this chunk
4642 * is really going to be in terms of logical address space,
4643 * and compare that answer with the max chunk size
4644 */
4645 if (stripe_size * data_stripes > max_chunk_size) {
4646 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004647
4648 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004649
4650 /* bump the answer up to a 16MB boundary */
4651 stripe_size = (stripe_size + mask) & ~mask;
4652
4653 /* but don't go higher than the limits we found
4654 * while searching for free extents
4655 */
4656 if (stripe_size > devices_info[ndevs-1].max_avail)
4657 stripe_size = devices_info[ndevs-1].max_avail;
4658 }
4659
David Sterbab8b93ad2015-01-16 17:26:13 +01004660 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004661
4662 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004663 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004664 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004665
Miao Xieb2117a32011-01-05 10:07:28 +00004666 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4667 if (!map) {
4668 ret = -ENOMEM;
4669 goto error;
4670 }
4671 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004672
Arne Jansen73c5de02011-04-12 12:07:57 +02004673 for (i = 0; i < ndevs; ++i) {
4674 for (j = 0; j < dev_stripes; ++j) {
4675 int s = i * dev_stripes + j;
4676 map->stripes[s].dev = devices_info[i].dev;
4677 map->stripes[s].physical = devices_info[i].dev_offset +
4678 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004679 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004680 }
Chris Mason593060d2008-03-25 16:50:33 -04004681 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004682 map->stripe_len = raid_stripe_len;
4683 map->io_align = raid_stripe_len;
4684 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004685 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004686 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004687
David Woodhouse53b381b2013-01-29 18:40:14 -05004688 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004689
Arne Jansen73c5de02011-04-12 12:07:57 +02004690 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004691
David Sterba172ddd62011-04-21 00:48:27 +02004692 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004693 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004694 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004695 ret = -ENOMEM;
4696 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004697 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004698 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004699 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004700 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004701 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004702 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004703 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004704 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004705
Chris Mason0b86a832008-03-24 15:01:56 -04004706 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004707 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004708 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004709 if (!ret) {
4710 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4711 atomic_inc(&em->refs);
4712 }
Chris Mason890871b2009-09-02 16:24:52 -04004713 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004714 if (ret) {
4715 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004716 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004717 }
Yan Zheng2b820322008-11-17 21:11:30 -05004718
Josef Bacik04487482013-01-29 15:03:37 -05004719 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4720 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4721 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004722 if (ret)
4723 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004724
Miao Xie7cc8e582014-09-03 21:35:38 +08004725 for (i = 0; i < map->num_stripes; i++) {
4726 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4727 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4728 }
Miao Xie43530c42014-09-03 21:35:36 +08004729
Miao Xie1c116182014-09-03 21:35:37 +08004730 spin_lock(&extent_root->fs_info->free_chunk_lock);
4731 extent_root->fs_info->free_chunk_space -= (stripe_size *
4732 map->num_stripes);
4733 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4734
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004735 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004736 check_raid56_incompat_flag(extent_root->fs_info, type);
4737
Miao Xieb2117a32011-01-05 10:07:28 +00004738 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004739 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004740
Josef Bacik6df9a952013-06-27 13:22:46 -04004741error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004742 write_lock(&em_tree->lock);
4743 remove_extent_mapping(em_tree, em);
4744 write_unlock(&em_tree->lock);
4745
4746 /* One for our allocation */
4747 free_extent_map(em);
4748 /* One for the tree reference */
4749 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004750 /* One for the pending_chunks list reference */
4751 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004752error:
Miao Xieb2117a32011-01-05 10:07:28 +00004753 kfree(devices_info);
4754 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004755}
4756
Josef Bacik6df9a952013-06-27 13:22:46 -04004757int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004758 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004759 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004760{
Yan Zheng2b820322008-11-17 21:11:30 -05004761 struct btrfs_key key;
4762 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4763 struct btrfs_device *device;
4764 struct btrfs_chunk *chunk;
4765 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004766 struct extent_map_tree *em_tree;
4767 struct extent_map *em;
4768 struct map_lookup *map;
4769 size_t item_size;
4770 u64 dev_offset;
4771 u64 stripe_size;
4772 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004773 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004774
Josef Bacik6df9a952013-06-27 13:22:46 -04004775 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4776 read_lock(&em_tree->lock);
4777 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4778 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004779
Josef Bacik6df9a952013-06-27 13:22:46 -04004780 if (!em) {
4781 btrfs_crit(extent_root->fs_info, "unable to find logical "
4782 "%Lu len %Lu", chunk_offset, chunk_size);
4783 return -EINVAL;
4784 }
4785
4786 if (em->start != chunk_offset || em->len != chunk_size) {
4787 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004788 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004789 chunk_size, em->start, em->len);
4790 free_extent_map(em);
4791 return -EINVAL;
4792 }
4793
Jeff Mahoney95617d62015-06-03 10:55:48 -04004794 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004795 item_size = btrfs_chunk_item_size(map->num_stripes);
4796 stripe_size = em->orig_block_len;
4797
4798 chunk = kzalloc(item_size, GFP_NOFS);
4799 if (!chunk) {
4800 ret = -ENOMEM;
4801 goto out;
4802 }
4803
Filipe Manana50460e32015-11-20 10:42:47 +00004804 /*
4805 * Take the device list mutex to prevent races with the final phase of
4806 * a device replace operation that replaces the device object associated
4807 * with the map's stripes, because the device object's id can change
4808 * at any time during that final phase of the device replace operation
4809 * (dev-replace.c:btrfs_dev_replace_finishing()).
4810 */
4811 mutex_lock(&chunk_root->fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004812 for (i = 0; i < map->num_stripes; i++) {
4813 device = map->stripes[i].dev;
4814 dev_offset = map->stripes[i].physical;
4815
Yan Zheng2b820322008-11-17 21:11:30 -05004816 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004817 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004818 break;
Josef Bacik6df9a952013-06-27 13:22:46 -04004819 ret = btrfs_alloc_dev_extent(trans, device,
4820 chunk_root->root_key.objectid,
4821 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4822 chunk_offset, dev_offset,
4823 stripe_size);
4824 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004825 break;
4826 }
4827 if (ret) {
4828 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex);
4829 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004830 }
4831
Yan Zheng2b820322008-11-17 21:11:30 -05004832 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004833 for (i = 0; i < map->num_stripes; i++) {
4834 device = map->stripes[i].dev;
4835 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004836
4837 btrfs_set_stack_stripe_devid(stripe, device->devid);
4838 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4839 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4840 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004841 }
Filipe Manana50460e32015-11-20 10:42:47 +00004842 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004843
4844 btrfs_set_stack_chunk_length(chunk, chunk_size);
4845 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4846 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4847 btrfs_set_stack_chunk_type(chunk, map->type);
4848 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4849 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4850 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4851 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4852 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4853
4854 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4855 key.type = BTRFS_CHUNK_ITEM_KEY;
4856 key.offset = chunk_offset;
4857
4858 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004859 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4860 /*
4861 * TODO: Cleanup of inserted chunk root in case of
4862 * failure.
4863 */
Li Zefan125ccb02011-12-08 15:07:24 +08004864 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004865 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004866 }
liubo1abe9b82011-03-24 11:18:59 +00004867
Josef Bacik6df9a952013-06-27 13:22:46 -04004868out:
Yan Zheng2b820322008-11-17 21:11:30 -05004869 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004870 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004871 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004872}
4873
4874/*
4875 * Chunk allocation falls into two parts. The first part does works
4876 * that make the new allocated chunk useable, but not do any operation
4877 * that modifies the chunk tree. The second part does the works that
4878 * require modifying the chunk tree. This division is important for the
4879 * bootstrap process of adding storage to a seed btrfs.
4880 */
4881int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4882 struct btrfs_root *extent_root, u64 type)
4883{
4884 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004885
Filipe Mananaa9629592015-05-18 19:11:40 +01004886 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004887 chunk_offset = find_next_chunk(extent_root->fs_info);
4888 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004889}
4890
Chris Masond3977122009-01-05 21:25:51 -05004891static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004892 struct btrfs_root *root,
4893 struct btrfs_device *device)
4894{
4895 u64 chunk_offset;
4896 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004897 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004898 struct btrfs_fs_info *fs_info = root->fs_info;
4899 struct btrfs_root *extent_root = fs_info->extent_root;
4900 int ret;
4901
Josef Bacik6df9a952013-06-27 13:22:46 -04004902 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004903 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004904 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4905 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004906 if (ret)
4907 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004908
Josef Bacik6df9a952013-06-27 13:22:46 -04004909 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004910 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004911 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4912 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004913 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004914}
4915
Miao Xied20983b2014-07-03 18:22:13 +08004916static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4917{
4918 int max_errors;
4919
4920 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4921 BTRFS_BLOCK_GROUP_RAID10 |
4922 BTRFS_BLOCK_GROUP_RAID5 |
4923 BTRFS_BLOCK_GROUP_DUP)) {
4924 max_errors = 1;
4925 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4926 max_errors = 2;
4927 } else {
4928 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004929 }
4930
Miao Xied20983b2014-07-03 18:22:13 +08004931 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004932}
4933
4934int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4935{
4936 struct extent_map *em;
4937 struct map_lookup *map;
4938 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4939 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004940 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004941 int i;
4942
Chris Mason890871b2009-09-02 16:24:52 -04004943 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004944 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004945 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004946 if (!em)
4947 return 1;
4948
Jeff Mahoney95617d62015-06-03 10:55:48 -04004949 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05004950 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004951 if (map->stripes[i].dev->missing) {
4952 miss_ndevs++;
4953 continue;
4954 }
4955
Yan Zheng2b820322008-11-17 21:11:30 -05004956 if (!map->stripes[i].dev->writeable) {
4957 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004958 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004959 }
4960 }
Miao Xied20983b2014-07-03 18:22:13 +08004961
4962 /*
4963 * If the number of missing devices is larger than max errors,
4964 * we can not write the data into that chunk successfully, so
4965 * set it readonly.
4966 */
4967 if (miss_ndevs > btrfs_chunk_max_errors(map))
4968 readonly = 1;
4969end:
Yan Zheng2b820322008-11-17 21:11:30 -05004970 free_extent_map(em);
4971 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004972}
4973
4974void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4975{
David Sterbaa8067e02011-04-21 00:34:43 +02004976 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004977}
4978
4979void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4980{
4981 struct extent_map *em;
4982
Chris Masond3977122009-01-05 21:25:51 -05004983 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004984 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004985 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4986 if (em)
4987 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004988 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004989 if (!em)
4990 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004991 /* once for us */
4992 free_extent_map(em);
4993 /* once for the tree */
4994 free_extent_map(em);
4995 }
4996}
4997
Stefan Behrens5d964052012-11-05 14:59:07 +01004998int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004999{
Stefan Behrens5d964052012-11-05 14:59:07 +01005000 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04005001 struct extent_map *em;
5002 struct map_lookup *map;
5003 struct extent_map_tree *em_tree = &map_tree->map_tree;
5004 int ret;
5005
Chris Mason890871b2009-09-02 16:24:52 -04005006 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005007 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04005008 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005009
Josef Bacikfb7669b2013-04-23 10:53:18 -04005010 /*
5011 * We could return errors for these cases, but that could get ugly and
5012 * we'd probably do the same thing which is just not do anything else
5013 * and exit, so return 1 so the callers don't try to use other copies.
5014 */
5015 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02005016 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005017 logical+len);
5018 return 1;
5019 }
5020
5021 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02005022 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02005023 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005024 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005025 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04005026 return 1;
5027 }
5028
Jeff Mahoney95617d62015-06-03 10:55:48 -04005029 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005030 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5031 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005032 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5033 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005034 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5035 ret = 2;
5036 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5037 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005038 else
5039 ret = 1;
5040 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005041
Liu Bo73beece2015-07-17 16:49:19 +08005042 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005043 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
5044 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005045 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005046
Chris Masonf1885912008-04-09 16:28:12 -04005047 return ret;
5048}
5049
David Woodhouse53b381b2013-01-29 18:40:14 -05005050unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
5051 struct btrfs_mapping_tree *map_tree,
5052 u64 logical)
5053{
5054 struct extent_map *em;
5055 struct map_lookup *map;
5056 struct extent_map_tree *em_tree = &map_tree->map_tree;
5057 unsigned long len = root->sectorsize;
5058
5059 read_lock(&em_tree->lock);
5060 em = lookup_extent_mapping(em_tree, logical, len);
5061 read_unlock(&em_tree->lock);
5062 BUG_ON(!em);
5063
5064 BUG_ON(em->start > logical || em->start + em->len < logical);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005065 map = em->map_lookup;
Zhao Leiffe2d202015-01-20 15:11:44 +08005066 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005067 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05005068 free_extent_map(em);
5069 return len;
5070}
5071
5072int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
5073 u64 logical, u64 len, int mirror_num)
5074{
5075 struct extent_map *em;
5076 struct map_lookup *map;
5077 struct extent_map_tree *em_tree = &map_tree->map_tree;
5078 int ret = 0;
5079
5080 read_lock(&em_tree->lock);
5081 em = lookup_extent_mapping(em_tree, logical, len);
5082 read_unlock(&em_tree->lock);
5083 BUG_ON(!em);
5084
5085 BUG_ON(em->start > logical || em->start + em->len < logical);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005086 map = em->map_lookup;
Zhao Leiffe2d202015-01-20 15:11:44 +08005087 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005088 ret = 1;
5089 free_extent_map(em);
5090 return ret;
5091}
5092
Stefan Behrens30d98612012-11-06 14:52:18 +01005093static int find_live_mirror(struct btrfs_fs_info *fs_info,
5094 struct map_lookup *map, int first, int num,
5095 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005096{
5097 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005098 int tolerance;
5099 struct btrfs_device *srcdev;
5100
5101 if (dev_replace_is_ongoing &&
5102 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5103 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5104 srcdev = fs_info->dev_replace.srcdev;
5105 else
5106 srcdev = NULL;
5107
5108 /*
5109 * try to avoid the drive that is the source drive for a
5110 * dev-replace procedure, only choose it if no other non-missing
5111 * mirror is available
5112 */
5113 for (tolerance = 0; tolerance < 2; tolerance++) {
5114 if (map->stripes[optimal].dev->bdev &&
5115 (tolerance || map->stripes[optimal].dev != srcdev))
5116 return optimal;
5117 for (i = first; i < first + num; i++) {
5118 if (map->stripes[i].dev->bdev &&
5119 (tolerance || map->stripes[i].dev != srcdev))
5120 return i;
5121 }
Chris Masondfe25022008-05-13 13:46:40 -04005122 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005123
Chris Masondfe25022008-05-13 13:46:40 -04005124 /* we couldn't find one that doesn't fail. Just return something
5125 * and the io error handling code will clean up eventually
5126 */
5127 return optimal;
5128}
5129
David Woodhouse53b381b2013-01-29 18:40:14 -05005130static inline int parity_smaller(u64 a, u64 b)
5131{
5132 return a > b;
5133}
5134
5135/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005136static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005137{
5138 struct btrfs_bio_stripe s;
5139 int i;
5140 u64 l;
5141 int again = 1;
5142
5143 while (again) {
5144 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005145 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005146 if (parity_smaller(bbio->raid_map[i],
5147 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005148 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005149 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005150 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005151 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005152 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005153 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005154
David Woodhouse53b381b2013-01-29 18:40:14 -05005155 again = 1;
5156 }
5157 }
5158 }
5159}
5160
Zhao Lei6e9606d2015-01-20 15:11:34 +08005161static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5162{
5163 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005164 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005165 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005166 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005167 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005168 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005169 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005170 /*
5171 * plus the raid_map, which includes both the tgt dev
5172 * and the stripes
5173 */
5174 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005175 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005176
5177 atomic_set(&bbio->error, 0);
5178 atomic_set(&bbio->refs, 1);
5179
5180 return bbio;
5181}
5182
5183void btrfs_get_bbio(struct btrfs_bio *bbio)
5184{
5185 WARN_ON(!atomic_read(&bbio->refs));
5186 atomic_inc(&bbio->refs);
5187}
5188
5189void btrfs_put_bbio(struct btrfs_bio *bbio)
5190{
5191 if (!bbio)
5192 return;
5193 if (atomic_dec_and_test(&bbio->refs))
5194 kfree(bbio);
5195}
5196
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005197static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005198 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005199 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005200 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005201{
5202 struct extent_map *em;
5203 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005204 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005205 struct extent_map_tree *em_tree = &map_tree->map_tree;
5206 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005207 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005208 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005209 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005210 u64 stripe_nr_orig;
5211 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005212 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005213 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005214 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005215 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005216 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005217 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005218 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005219 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005220 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5221 int dev_replace_is_ongoing = 0;
5222 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005223 int patch_the_first_stripe_for_dev_replace = 0;
5224 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005225 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005226
Chris Mason890871b2009-09-02 16:24:52 -04005227 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005228 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005229 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005230
Chris Mason3b951512008-04-17 11:29:12 -04005231 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005232 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005233 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005234 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005235 }
Chris Mason0b86a832008-03-24 15:01:56 -04005236
Josef Bacik9bb91872013-04-19 23:45:33 -04005237 if (em->start > logical || em->start + em->len < logical) {
5238 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005239 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005240 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005241 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005242 return -EINVAL;
5243 }
5244
Jeff Mahoney95617d62015-06-03 10:55:48 -04005245 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005246 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005247
David Woodhouse53b381b2013-01-29 18:40:14 -05005248 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005249 stripe_nr = offset;
5250 /*
5251 * stripe_nr counts the total number of stripes we have to stride
5252 * to get to this block
5253 */
David Sterba47c57132015-02-20 18:43:47 +01005254 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005255
David Woodhouse53b381b2013-01-29 18:40:14 -05005256 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005257 BUG_ON(offset < stripe_offset);
5258
5259 /* stripe_offset is the offset of this block in its stripe*/
5260 stripe_offset = offset - stripe_offset;
5261
David Woodhouse53b381b2013-01-29 18:40:14 -05005262 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005263 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005264 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5265 raid56_full_stripe_start = offset;
5266
5267 /* allow a write of a full stripe, but make sure we don't
5268 * allow straddling of stripes
5269 */
David Sterba47c57132015-02-20 18:43:47 +01005270 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5271 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005272 raid56_full_stripe_start *= full_stripe_len;
5273 }
5274
5275 if (rw & REQ_DISCARD) {
5276 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005277 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005278 ret = -EOPNOTSUPP;
5279 goto out;
5280 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005281 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005282 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5283 u64 max_len;
5284 /* For writes to RAID[56], allow a full stripeset across all disks.
5285 For other RAID types and for RAID[56] reads, just allow a single
5286 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005287 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005288 (rw & REQ_WRITE)) {
5289 max_len = stripe_len * nr_data_stripes(map) -
5290 (offset - raid56_full_stripe_start);
5291 } else {
5292 /* we limit the length of each bio to what fits in a stripe */
5293 max_len = stripe_len - stripe_offset;
5294 }
5295 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005296 } else {
5297 *length = em->len - offset;
5298 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005299
David Woodhouse53b381b2013-01-29 18:40:14 -05005300 /* This is for when we're called from btrfs_merge_bio_hook() and all
5301 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005302 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005303 goto out;
5304
Liu Bo73beece2015-07-17 16:49:19 +08005305 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005306 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5307 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005308 btrfs_dev_replace_unlock(dev_replace, 0);
5309 else
5310 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005311
Stefan Behrensad6d6202012-11-06 15:06:47 +01005312 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5313 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5314 dev_replace->tgtdev != NULL) {
5315 /*
5316 * in dev-replace case, for repair case (that's the only
5317 * case where the mirror is selected explicitly when
5318 * calling btrfs_map_block), blocks left of the left cursor
5319 * can also be read from the target drive.
5320 * For REQ_GET_READ_MIRRORS, the target drive is added as
5321 * the last one to the array of stripes. For READ, it also
5322 * needs to be supported using the same mirror number.
5323 * If the requested block is not left of the left cursor,
5324 * EIO is returned. This can happen because btrfs_num_copies()
5325 * returns one more in the dev-replace case.
5326 */
5327 u64 tmp_length = *length;
5328 struct btrfs_bio *tmp_bbio = NULL;
5329 int tmp_num_stripes;
5330 u64 srcdev_devid = dev_replace->srcdev->devid;
5331 int index_srcdev = 0;
5332 int found = 0;
5333 u64 physical_of_found = 0;
5334
5335 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005336 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005337 if (ret) {
5338 WARN_ON(tmp_bbio != NULL);
5339 goto out;
5340 }
5341
5342 tmp_num_stripes = tmp_bbio->num_stripes;
5343 if (mirror_num > tmp_num_stripes) {
5344 /*
5345 * REQ_GET_READ_MIRRORS does not contain this
5346 * mirror, that means that the requested area
5347 * is not left of the left cursor
5348 */
5349 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005350 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005351 goto out;
5352 }
5353
5354 /*
5355 * process the rest of the function using the mirror_num
5356 * of the source drive. Therefore look it up first.
5357 * At the end, patch the device pointer to the one of the
5358 * target drive.
5359 */
5360 for (i = 0; i < tmp_num_stripes; i++) {
Zhao Lei94a97df2015-12-09 21:03:49 +08005361 if (tmp_bbio->stripes[i].dev->devid != srcdev_devid)
5362 continue;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005363
Zhao Lei94a97df2015-12-09 21:03:49 +08005364 /*
5365 * In case of DUP, in order to keep it simple, only add
5366 * the mirror with the lowest physical address
5367 */
5368 if (found &&
5369 physical_of_found <= tmp_bbio->stripes[i].physical)
5370 continue;
5371
5372 index_srcdev = i;
5373 found = 1;
5374 physical_of_found = tmp_bbio->stripes[i].physical;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005375 }
5376
Zhao Lei6e9606d2015-01-20 15:11:34 +08005377 btrfs_put_bbio(tmp_bbio);
Zhao Lei94a97df2015-12-09 21:03:49 +08005378
5379 if (!found) {
5380 WARN_ON(1);
5381 ret = -EIO;
5382 goto out;
5383 }
5384
5385 mirror_num = index_srcdev + 1;
5386 patch_the_first_stripe_for_dev_replace = 1;
5387 physical_to_patch_in_first_stripe = physical_of_found;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005388 } else if (mirror_num > map->num_stripes) {
5389 mirror_num = 0;
5390 }
5391
Chris Masonf2d8d742008-04-21 10:03:05 -04005392 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005393 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005394 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005395 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005396 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005397 stripe_end_offset = stripe_nr_end * map->stripe_len -
5398 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005399
Li Dongyangfce3bb92011-03-24 10:24:26 +00005400 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5401 if (rw & REQ_DISCARD)
5402 num_stripes = min_t(u64, map->num_stripes,
5403 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005404 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5405 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005406 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5407 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005408 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005409 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005410 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005411 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005412 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005413 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005414 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005415 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005416 current->pid % map->num_stripes,
5417 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005418 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005419 }
Chris Mason2fff7342008-04-29 14:12:09 -04005420
Chris Mason611f0e02008-04-03 16:29:03 -04005421 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005422 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005423 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005424 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005425 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005426 } else {
5427 mirror_num = 1;
5428 }
Chris Mason2fff7342008-04-29 14:12:09 -04005429
Chris Mason321aecc2008-04-16 10:49:51 -04005430 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005431 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005432
David Sterba47c57132015-02-20 18:43:47 +01005433 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005434 stripe_index *= map->sub_stripes;
5435
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005436 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005437 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005438 else if (rw & REQ_DISCARD)
5439 num_stripes = min_t(u64, map->sub_stripes *
5440 (stripe_nr_end - stripe_nr_orig),
5441 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005442 else if (mirror_num)
5443 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005444 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005445 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005446 stripe_index = find_live_mirror(fs_info, map,
5447 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005448 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005449 current->pid % map->sub_stripes,
5450 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005451 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005452 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005453
Zhao Leiffe2d202015-01-20 15:11:44 +08005454 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005455 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005456 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5457 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005458 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005459 stripe_nr = div_u64(raid56_full_stripe_start,
5460 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005461
5462 /* RAID[56] write or recovery. Return all stripes */
5463 num_stripes = map->num_stripes;
5464 max_errors = nr_parity_stripes(map);
5465
David Woodhouse53b381b2013-01-29 18:40:14 -05005466 *length = map->stripe_len;
5467 stripe_index = 0;
5468 stripe_offset = 0;
5469 } else {
5470 /*
5471 * Mirror #0 or #1 means the original data block.
5472 * Mirror #2 is RAID5 parity block.
5473 * Mirror #3 is RAID6 Q block.
5474 */
David Sterba47c57132015-02-20 18:43:47 +01005475 stripe_nr = div_u64_rem(stripe_nr,
5476 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005477 if (mirror_num > 1)
5478 stripe_index = nr_data_stripes(map) +
5479 mirror_num - 2;
5480
5481 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005482 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5483 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005484 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5485 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5486 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005487 }
Chris Mason8790d502008-04-03 16:29:03 -04005488 } else {
5489 /*
David Sterba47c57132015-02-20 18:43:47 +01005490 * after this, stripe_nr is the number of stripes on this
5491 * device we have to walk to find the data, and stripe_index is
5492 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005493 */
David Sterba47c57132015-02-20 18:43:47 +01005494 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5495 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005496 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005497 }
Chris Mason593060d2008-03-25 16:50:33 -04005498 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005499
Stefan Behrens472262f2012-11-06 14:43:46 +01005500 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005501 if (dev_replace_is_ongoing) {
5502 if (rw & (REQ_WRITE | REQ_DISCARD))
5503 num_alloc_stripes <<= 1;
5504 if (rw & REQ_GET_READ_MIRRORS)
5505 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005506 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005507 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005508
Zhao Lei6e9606d2015-01-20 15:11:34 +08005509 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005510 if (!bbio) {
5511 ret = -ENOMEM;
5512 goto out;
5513 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005514 if (dev_replace_is_ongoing)
5515 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005516
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005517 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005518 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005519 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5520 mirror_num > 1)) {
5521 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005522 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005523
5524 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5525 sizeof(struct btrfs_bio_stripe) *
5526 num_alloc_stripes +
5527 sizeof(int) * tgtdev_indexes);
5528
5529 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005530 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005531
5532 /* Fill in the logical address of each stripe */
5533 tmp = stripe_nr * nr_data_stripes(map);
5534 for (i = 0; i < nr_data_stripes(map); i++)
5535 bbio->raid_map[(i+rot) % num_stripes] =
5536 em->start + (tmp + i) * map->stripe_len;
5537
5538 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5539 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5540 bbio->raid_map[(i+rot+1) % num_stripes] =
5541 RAID6_Q_STRIPE;
5542 }
5543
Li Dongyangfce3bb92011-03-24 10:24:26 +00005544 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005545 u32 factor = 0;
5546 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005547 u64 stripes_per_dev = 0;
5548 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005549 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005550
5551 if (map->type &
5552 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5553 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5554 sub_stripes = 1;
5555 else
5556 sub_stripes = map->sub_stripes;
5557
5558 factor = map->num_stripes / sub_stripes;
5559 stripes_per_dev = div_u64_rem(stripe_nr_end -
5560 stripe_nr_orig,
5561 factor,
5562 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005563 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5564 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005565 }
5566
Li Dongyangfce3bb92011-03-24 10:24:26 +00005567 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005568 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005569 map->stripes[stripe_index].physical +
5570 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005571 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005572
Li Zefanec9ef7a2011-12-01 14:06:42 +08005573 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5574 BTRFS_BLOCK_GROUP_RAID10)) {
5575 bbio->stripes[i].length = stripes_per_dev *
5576 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005577
Li Zefanec9ef7a2011-12-01 14:06:42 +08005578 if (i / sub_stripes < remaining_stripes)
5579 bbio->stripes[i].length +=
5580 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005581
5582 /*
5583 * Special for the first stripe and
5584 * the last stripe:
5585 *
5586 * |-------|...|-------|
5587 * |----------|
5588 * off end_off
5589 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005590 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005591 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005592 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005593
5594 if (stripe_index >= last_stripe &&
5595 stripe_index <= (last_stripe +
5596 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005597 bbio->stripes[i].length -=
5598 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005599
Li Zefanec9ef7a2011-12-01 14:06:42 +08005600 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005601 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005602 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005603 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005604
5605 stripe_index++;
5606 if (stripe_index == map->num_stripes) {
5607 /* This could only happen for RAID0/10 */
5608 stripe_index = 0;
5609 stripe_nr++;
5610 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005611 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005612 } else {
5613 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005614 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005615 map->stripes[stripe_index].physical +
5616 stripe_offset +
5617 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005618 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005619 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005620 stripe_index++;
5621 }
Chris Mason593060d2008-03-25 16:50:33 -04005622 }
Li Zefande11cc12011-12-01 12:55:47 +08005623
Miao Xied20983b2014-07-03 18:22:13 +08005624 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5625 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005626
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005627 if (bbio->raid_map)
5628 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005629
Miao Xie2c8cdd62014-11-14 16:06:25 +08005630 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005631 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5632 dev_replace->tgtdev != NULL) {
5633 int index_where_to_add;
5634 u64 srcdev_devid = dev_replace->srcdev->devid;
5635
5636 /*
5637 * duplicate the write operations while the dev replace
5638 * procedure is running. Since the copying of the old disk
5639 * to the new disk takes place at run time while the
5640 * filesystem is mounted writable, the regular write
5641 * operations to the old disk have to be duplicated to go
5642 * to the new disk as well.
5643 * Note that device->missing is handled by the caller, and
5644 * that the write to the old disk is already set up in the
5645 * stripes array.
5646 */
5647 index_where_to_add = num_stripes;
5648 for (i = 0; i < num_stripes; i++) {
5649 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5650 /* write to new disk, too */
5651 struct btrfs_bio_stripe *new =
5652 bbio->stripes + index_where_to_add;
5653 struct btrfs_bio_stripe *old =
5654 bbio->stripes + i;
5655
5656 new->physical = old->physical;
5657 new->length = old->length;
5658 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005659 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005660 index_where_to_add++;
5661 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005662 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005663 }
5664 }
5665 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005666 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5667 dev_replace->tgtdev != NULL) {
5668 u64 srcdev_devid = dev_replace->srcdev->devid;
5669 int index_srcdev = 0;
5670 int found = 0;
5671 u64 physical_of_found = 0;
5672
5673 /*
5674 * During the dev-replace procedure, the target drive can
5675 * also be used to read data in case it is needed to repair
5676 * a corrupt block elsewhere. This is possible if the
5677 * requested area is left of the left cursor. In this area,
5678 * the target drive is a full copy of the source drive.
5679 */
5680 for (i = 0; i < num_stripes; i++) {
5681 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5682 /*
5683 * In case of DUP, in order to keep it
5684 * simple, only add the mirror with the
5685 * lowest physical address
5686 */
5687 if (found &&
5688 physical_of_found <=
5689 bbio->stripes[i].physical)
5690 continue;
5691 index_srcdev = i;
5692 found = 1;
5693 physical_of_found = bbio->stripes[i].physical;
5694 }
5695 }
5696 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005697 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005698 dev_replace->cursor_left) {
5699 struct btrfs_bio_stripe *tgtdev_stripe =
5700 bbio->stripes + num_stripes;
5701
5702 tgtdev_stripe->physical = physical_of_found;
5703 tgtdev_stripe->length =
5704 bbio->stripes[index_srcdev].length;
5705 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005706 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005707
Miao Xie2c8cdd62014-11-14 16:06:25 +08005708 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005709 num_stripes++;
5710 }
5711 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005712 }
5713
Li Zefande11cc12011-12-01 12:55:47 +08005714 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005715 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005716 bbio->num_stripes = num_stripes;
5717 bbio->max_errors = max_errors;
5718 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005719 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005720
5721 /*
5722 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5723 * mirror_num == num_stripes + 1 && dev_replace target drive is
5724 * available as a mirror
5725 */
5726 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5727 WARN_ON(num_stripes > 1);
5728 bbio->stripes[0].dev = dev_replace->tgtdev;
5729 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5730 bbio->mirror_num = map->num_stripes + 1;
5731 }
Chris Masoncea9e442008-04-09 16:28:12 -04005732out:
Liu Bo73beece2015-07-17 16:49:19 +08005733 if (dev_replace_is_ongoing) {
5734 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5735 btrfs_dev_replace_unlock(dev_replace, 0);
5736 }
Chris Mason0b86a832008-03-24 15:01:56 -04005737 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005738 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005739}
5740
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005741int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005742 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005743 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005744{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005745 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005746 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005747}
5748
Miao Xieaf8e2d12014-10-23 14:42:50 +08005749/* For Scrub/replace */
5750int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5751 u64 logical, u64 *length,
5752 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005753 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005754{
5755 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005756 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005757}
5758
Yan Zhenga512bbf2008-12-08 16:46:26 -05005759int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5760 u64 chunk_start, u64 physical, u64 devid,
5761 u64 **logical, int *naddrs, int *stripe_len)
5762{
5763 struct extent_map_tree *em_tree = &map_tree->map_tree;
5764 struct extent_map *em;
5765 struct map_lookup *map;
5766 u64 *buf;
5767 u64 bytenr;
5768 u64 length;
5769 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005770 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005771 int i, j, nr = 0;
5772
Chris Mason890871b2009-09-02 16:24:52 -04005773 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005774 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005775 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005776
Josef Bacik835d9742013-03-19 12:13:25 -04005777 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005778 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005779 chunk_start);
5780 return -EIO;
5781 }
5782
5783 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005784 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005785 em->start, chunk_start);
5786 free_extent_map(em);
5787 return -EIO;
5788 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04005789 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005790
5791 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005792 rmap_len = map->stripe_len;
5793
Yan Zhenga512bbf2008-12-08 16:46:26 -05005794 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005795 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005796 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005797 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005798 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005799 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005800 rmap_len = map->stripe_len * nr_data_stripes(map);
5801 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005802
David Sterba31e818f2015-02-20 18:00:26 +01005803 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005804 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005805
5806 for (i = 0; i < map->num_stripes; i++) {
5807 if (devid && map->stripes[i].dev->devid != devid)
5808 continue;
5809 if (map->stripes[i].physical > physical ||
5810 map->stripes[i].physical + length <= physical)
5811 continue;
5812
5813 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005814 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005815
5816 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5817 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005818 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005819 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5820 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005821 } /* else if RAID[56], multiply by nr_data_stripes().
5822 * Alternatively, just use rmap_len below instead of
5823 * map->stripe_len */
5824
5825 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005826 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005827 for (j = 0; j < nr; j++) {
5828 if (buf[j] == bytenr)
5829 break;
5830 }
Chris Mason934d3752008-12-08 16:43:10 -05005831 if (j == nr) {
5832 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005833 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005834 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005835 }
5836
Yan Zhenga512bbf2008-12-08 16:46:26 -05005837 *logical = buf;
5838 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005839 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005840
5841 free_extent_map(em);
5842 return 0;
5843}
5844
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005845static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005846{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005847 bio->bi_private = bbio->private;
5848 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005849 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005850
Zhao Lei6e9606d2015-01-20 15:11:34 +08005851 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005852}
5853
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005854static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005855{
Chris Mason9be33952013-05-17 18:30:14 -04005856 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005857 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005858
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005859 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005860 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005861 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005862 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005863 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005864 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005865
5866 BUG_ON(stripe_index >= bbio->num_stripes);
5867 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005868 if (dev->bdev) {
5869 if (bio->bi_rw & WRITE)
5870 btrfs_dev_stat_inc(dev,
5871 BTRFS_DEV_STAT_WRITE_ERRS);
5872 else
5873 btrfs_dev_stat_inc(dev,
5874 BTRFS_DEV_STAT_READ_ERRS);
5875 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5876 btrfs_dev_stat_inc(dev,
5877 BTRFS_DEV_STAT_FLUSH_ERRS);
5878 btrfs_dev_stat_print_on_error(dev);
5879 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005880 }
5881 }
Chris Mason8790d502008-04-03 16:29:03 -04005882
Jan Schmidta1d3c472011-08-04 17:15:33 +02005883 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005884 is_orig_bio = 1;
5885
Miao Xiec404e0d2014-01-30 16:46:55 +08005886 btrfs_bio_counter_dec(bbio->fs_info);
5887
Jan Schmidta1d3c472011-08-04 17:15:33 +02005888 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005889 if (!is_orig_bio) {
5890 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005891 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005892 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005893
Chris Mason9be33952013-05-17 18:30:14 -04005894 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005895 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005896 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005897 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005898 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005899 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005900 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005901 /*
5902 * this bio is actually up to date, we didn't
5903 * go over the max number of errors
5904 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005905 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005906 }
Miao Xiec55f1392014-06-19 10:42:54 +08005907
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005908 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005909 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005910 bio_put(bio);
5911 }
Chris Mason8790d502008-04-03 16:29:03 -04005912}
5913
Chris Mason8b712842008-06-11 16:50:36 -04005914/*
5915 * see run_scheduled_bios for a description of why bios are collected for
5916 * async submit.
5917 *
5918 * This will add one bio to the pending list for a device and make sure
5919 * the work struct is scheduled.
5920 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005921static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5922 struct btrfs_device *device,
5923 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005924{
5925 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005926 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005927
David Woodhouse53b381b2013-01-29 18:40:14 -05005928 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005929 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005930 return;
5931 }
5932
Chris Mason8b712842008-06-11 16:50:36 -04005933 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005934 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005935 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005936 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005937 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005938 return;
Chris Mason8b712842008-06-11 16:50:36 -04005939 }
5940
5941 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005942 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005943 * higher layers. Otherwise, the async bio makes it appear we have
5944 * made progress against dirty pages when we've really just put it
5945 * on a queue for later
5946 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005947 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005948 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005949 bio->bi_next = NULL;
5950 bio->bi_rw |= rw;
5951
5952 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005953 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005954 pending_bios = &device->pending_sync_bios;
5955 else
5956 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005957
Chris Masonffbd5172009-04-20 15:50:09 -04005958 if (pending_bios->tail)
5959 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005960
Chris Masonffbd5172009-04-20 15:50:09 -04005961 pending_bios->tail = bio;
5962 if (!pending_bios->head)
5963 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005964 if (device->running_pending)
5965 should_queue = 0;
5966
5967 spin_unlock(&device->io_lock);
5968
5969 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005970 btrfs_queue_work(root->fs_info->submit_workers,
5971 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005972}
5973
Josef Bacikde1ee922012-10-19 16:50:56 -04005974static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5975 struct bio *bio, u64 physical, int dev_nr,
5976 int rw, int async)
5977{
5978 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5979
5980 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005981 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005982 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005983 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005984#ifdef DEBUG
5985 {
5986 struct rcu_string *name;
5987
5988 rcu_read_lock();
5989 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005990 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005991 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005992 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5993 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005994 rcu_read_unlock();
5995 }
5996#endif
5997 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005998
5999 btrfs_bio_counter_inc_noblocked(root->fs_info);
6000
Josef Bacikde1ee922012-10-19 16:50:56 -04006001 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05006002 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006003 else
6004 btrfsic_submit_bio(rw, bio);
6005}
6006
Josef Bacikde1ee922012-10-19 16:50:56 -04006007static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6008{
6009 atomic_inc(&bbio->error);
6010 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08006011 /* Shoud be the original bio. */
6012 WARN_ON(bio != bbio->orig_bio);
6013
Chris Mason9be33952013-05-17 18:30:14 -04006014 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006015 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006016 bio->bi_error = -EIO;
6017 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006018 }
6019}
6020
Chris Masonf1885912008-04-09 16:28:12 -04006021int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04006022 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006023{
Chris Mason0b86a832008-03-24 15:01:56 -04006024 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006025 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006026 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006027 u64 length = 0;
6028 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006029 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006030 int dev_nr;
6031 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006032 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006033
Kent Overstreet4f024f32013-10-11 15:44:27 -07006034 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006035 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006036
Miao Xiec404e0d2014-01-30 16:46:55 +08006037 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05006038 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006039 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006040 if (ret) {
6041 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006042 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08006043 }
Chris Masoncea9e442008-04-09 16:28:12 -04006044
Jan Schmidta1d3c472011-08-04 17:15:33 +02006045 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006046 bbio->orig_bio = first_bio;
6047 bbio->private = first_bio->bi_private;
6048 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08006049 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006050 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6051
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006052 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6053 ((rw & WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006054 /* In this case, map_length has been set to the length of
6055 a single stripe; not the whole write */
6056 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006057 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006058 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006059 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08006060 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006061 }
Miao Xie42452152014-11-25 16:39:28 +08006062
Miao Xiec404e0d2014-01-30 16:46:55 +08006063 btrfs_bio_counter_dec(root->fs_info);
6064 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05006065 }
6066
Chris Masoncea9e442008-04-09 16:28:12 -04006067 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006068 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006069 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006070 BUG();
6071 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006072
Zhao Lei08da7572015-02-12 15:42:16 +08006073 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006074 dev = bbio->stripes[dev_nr].dev;
6075 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
6076 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006077 continue;
6078 }
6079
Jan Schmidta1d3c472011-08-04 17:15:33 +02006080 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04006081 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006082 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04006083 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006084 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006085
Josef Bacikde1ee922012-10-19 16:50:56 -04006086 submit_stripe_bio(root, bbio, bio,
6087 bbio->stripes[dev_nr].physical, dev_nr, rw,
6088 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006089 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006090 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006091 return 0;
6092}
6093
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006094struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006095 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006096{
Yan Zheng2b820322008-11-17 21:11:30 -05006097 struct btrfs_device *device;
6098 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006099
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006100 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006101 while (cur_devices) {
6102 if (!fsid ||
6103 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6104 device = __find_device(&cur_devices->devices,
6105 devid, uuid);
6106 if (device)
6107 return device;
6108 }
6109 cur_devices = cur_devices->seed;
6110 }
6111 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006112}
6113
Chris Masondfe25022008-05-13 13:46:40 -04006114static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006115 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006116 u64 devid, u8 *dev_uuid)
6117{
6118 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006119
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006120 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6121 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006122 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006123
6124 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006125 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006126 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006127
6128 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006129 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006130
Chris Masondfe25022008-05-13 13:46:40 -04006131 return device;
6132}
6133
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006134/**
6135 * btrfs_alloc_device - allocate struct btrfs_device
6136 * @fs_info: used only for generating a new devid, can be NULL if
6137 * devid is provided (i.e. @devid != NULL).
6138 * @devid: a pointer to devid for this device. If NULL a new devid
6139 * is generated.
6140 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6141 * is generated.
6142 *
6143 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6144 * on error. Returned struct is not linked onto any lists and can be
6145 * destroyed with kfree() right away.
6146 */
6147struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6148 const u64 *devid,
6149 const u8 *uuid)
6150{
6151 struct btrfs_device *dev;
6152 u64 tmp;
6153
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306154 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006155 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006156
6157 dev = __alloc_device();
6158 if (IS_ERR(dev))
6159 return dev;
6160
6161 if (devid)
6162 tmp = *devid;
6163 else {
6164 int ret;
6165
6166 ret = find_next_devid(fs_info, &tmp);
6167 if (ret) {
6168 kfree(dev);
6169 return ERR_PTR(ret);
6170 }
6171 }
6172 dev->devid = tmp;
6173
6174 if (uuid)
6175 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6176 else
6177 generate_random_uuid(dev->uuid);
6178
Liu Bo9e0af232014-08-15 23:36:53 +08006179 btrfs_init_work(&dev->work, btrfs_submit_helper,
6180 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006181
6182 return dev;
6183}
6184
Chris Mason0b86a832008-03-24 15:01:56 -04006185static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6186 struct extent_buffer *leaf,
6187 struct btrfs_chunk *chunk)
6188{
6189 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6190 struct map_lookup *map;
6191 struct extent_map *em;
6192 u64 logical;
6193 u64 length;
Qu Wenruof04b7722015-12-15 09:14:37 +08006194 u64 stripe_len;
Chris Mason0b86a832008-03-24 15:01:56 -04006195 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006196 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006197 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006198 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006199 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006200
Chris Masone17cade2008-04-15 15:41:47 -04006201 logical = key->offset;
6202 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006203 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6204 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6205 /* Validation check */
6206 if (!num_stripes) {
6207 btrfs_err(root->fs_info, "invalid chunk num_stripes: %u",
6208 num_stripes);
6209 return -EIO;
6210 }
6211 if (!IS_ALIGNED(logical, root->sectorsize)) {
6212 btrfs_err(root->fs_info,
6213 "invalid chunk logical %llu", logical);
6214 return -EIO;
6215 }
6216 if (!length || !IS_ALIGNED(length, root->sectorsize)) {
6217 btrfs_err(root->fs_info,
6218 "invalid chunk length %llu", length);
6219 return -EIO;
6220 }
6221 if (!is_power_of_2(stripe_len)) {
6222 btrfs_err(root->fs_info, "invalid chunk stripe length: %llu",
6223 stripe_len);
6224 return -EIO;
6225 }
6226 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6227 btrfs_chunk_type(leaf, chunk)) {
6228 btrfs_err(root->fs_info, "unrecognized chunk type: %llu",
6229 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6230 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6231 btrfs_chunk_type(leaf, chunk));
6232 return -EIO;
6233 }
Chris Masona061fc82008-05-07 11:43:44 -04006234
Chris Mason890871b2009-09-02 16:24:52 -04006235 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006236 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006237 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006238
6239 /* already mapped? */
6240 if (em && em->start <= logical && em->start + em->len > logical) {
6241 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006242 return 0;
6243 } else if (em) {
6244 free_extent_map(em);
6245 }
Chris Mason0b86a832008-03-24 15:01:56 -04006246
David Sterba172ddd62011-04-21 00:48:27 +02006247 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006248 if (!em)
6249 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006250 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006251 if (!map) {
6252 free_extent_map(em);
6253 return -ENOMEM;
6254 }
6255
Wang Shilong298a8f92014-06-19 10:42:52 +08006256 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006257 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006258 em->start = logical;
6259 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006260 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006261 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006262 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006263
Chris Mason593060d2008-03-25 16:50:33 -04006264 map->num_stripes = num_stripes;
6265 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6266 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6267 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6268 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6269 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006270 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006271 for (i = 0; i < num_stripes; i++) {
6272 map->stripes[i].physical =
6273 btrfs_stripe_offset_nr(leaf, chunk, i);
6274 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006275 read_extent_buffer(leaf, uuid, (unsigned long)
6276 btrfs_stripe_dev_uuid_nr(chunk, i),
6277 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006278 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6279 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006280 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006281 free_extent_map(em);
6282 return -EIO;
6283 }
Chris Masondfe25022008-05-13 13:46:40 -04006284 if (!map->stripes[i].dev) {
6285 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006286 add_missing_dev(root, root->fs_info->fs_devices,
6287 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006288 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006289 free_extent_map(em);
6290 return -EIO;
6291 }
Anand Jain816fceb2015-04-27 12:46:18 +08006292 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6293 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006294 }
6295 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006296 }
6297
Chris Mason890871b2009-09-02 16:24:52 -04006298 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006299 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006300 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006301 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006302 free_extent_map(em);
6303
6304 return 0;
6305}
6306
Jeff Mahoney143bede2012-03-01 14:56:26 +01006307static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006308 struct btrfs_dev_item *dev_item,
6309 struct btrfs_device *device)
6310{
6311 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006312
6313 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006314 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6315 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006316 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006317 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006318 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006319 device->type = btrfs_device_type(leaf, dev_item);
6320 device->io_align = btrfs_device_io_align(leaf, dev_item);
6321 device->io_width = btrfs_device_io_width(leaf, dev_item);
6322 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006323 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006324 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006325
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006326 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006327 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006328}
6329
Miao Xie5f375832014-09-03 21:35:46 +08006330static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6331 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006332{
6333 struct btrfs_fs_devices *fs_devices;
6334 int ret;
6335
Li Zefanb367e472011-12-07 11:38:24 +08006336 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006337
6338 fs_devices = root->fs_info->fs_devices->seed;
6339 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006340 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6341 return fs_devices;
6342
Yan Zheng2b820322008-11-17 21:11:30 -05006343 fs_devices = fs_devices->seed;
6344 }
6345
6346 fs_devices = find_fsid(fsid);
6347 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006348 if (!btrfs_test_opt(root, DEGRADED))
6349 return ERR_PTR(-ENOENT);
6350
6351 fs_devices = alloc_fs_devices(fsid);
6352 if (IS_ERR(fs_devices))
6353 return fs_devices;
6354
6355 fs_devices->seeding = 1;
6356 fs_devices->opened = 1;
6357 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006358 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006359
6360 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006361 if (IS_ERR(fs_devices))
6362 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006363
Christoph Hellwig97288f22008-12-02 06:36:09 -05006364 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006365 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006366 if (ret) {
6367 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006368 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006369 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006370 }
Yan Zheng2b820322008-11-17 21:11:30 -05006371
6372 if (!fs_devices->seeding) {
6373 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006374 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006375 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006376 goto out;
6377 }
6378
6379 fs_devices->seed = root->fs_info->fs_devices->seed;
6380 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006381out:
Miao Xie5f375832014-09-03 21:35:46 +08006382 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006383}
6384
Chris Mason0d81ba52008-03-24 15:02:07 -04006385static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006386 struct extent_buffer *leaf,
6387 struct btrfs_dev_item *dev_item)
6388{
Miao Xie5f375832014-09-03 21:35:46 +08006389 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006390 struct btrfs_device *device;
6391 u64 devid;
6392 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006393 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006394 u8 dev_uuid[BTRFS_UUID_SIZE];
6395
Chris Mason0b86a832008-03-24 15:01:56 -04006396 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006397 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006398 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006399 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006400 BTRFS_UUID_SIZE);
6401
6402 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006403 fs_devices = open_seed_devices(root, fs_uuid);
6404 if (IS_ERR(fs_devices))
6405 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006406 }
6407
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006408 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006409 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006410 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006411 return -EIO;
6412
Miao Xie5f375832014-09-03 21:35:46 +08006413 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6414 if (!device)
6415 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006416 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6417 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006418 } else {
6419 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6420 return -EIO;
6421
6422 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006423 /*
6424 * this happens when a device that was properly setup
6425 * in the device info lists suddenly goes bad.
6426 * device->bdev is NULL, and so we have to set
6427 * device->missing to one here
6428 */
Miao Xie5f375832014-09-03 21:35:46 +08006429 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006430 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006431 }
Miao Xie5f375832014-09-03 21:35:46 +08006432
6433 /* Move the device to its own fs_devices */
6434 if (device->fs_devices != fs_devices) {
6435 ASSERT(device->missing);
6436
6437 list_move(&device->dev_list, &fs_devices->devices);
6438 device->fs_devices->num_devices--;
6439 fs_devices->num_devices++;
6440
6441 device->fs_devices->missing_devices--;
6442 fs_devices->missing_devices++;
6443
6444 device->fs_devices = fs_devices;
6445 }
Yan Zheng2b820322008-11-17 21:11:30 -05006446 }
6447
6448 if (device->fs_devices != root->fs_info->fs_devices) {
6449 BUG_ON(device->writeable);
6450 if (device->generation !=
6451 btrfs_device_generation(leaf, dev_item))
6452 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006453 }
Chris Mason0b86a832008-03-24 15:01:56 -04006454
6455 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006456 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006457 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006458 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006459 spin_lock(&root->fs_info->free_chunk_lock);
6460 root->fs_info->free_chunk_space += device->total_bytes -
6461 device->bytes_used;
6462 spin_unlock(&root->fs_info->free_chunk_lock);
6463 }
Chris Mason0b86a832008-03-24 15:01:56 -04006464 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006465 return ret;
6466}
6467
Yan Zhenge4404d62008-12-12 10:03:26 -05006468int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006469{
David Sterba6c417612011-04-13 15:41:04 +02006470 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006471 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006472 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006473 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006474 u8 *array_ptr;
6475 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006476 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006477 u32 num_stripes;
6478 u32 array_size;
6479 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006480 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006481 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006482
David Sterbaa83fffb2014-06-15 02:39:54 +02006483 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6484 /*
6485 * This will create extent buffer of nodesize, superblock size is
6486 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6487 * overallocate but we can keep it as-is, only the first page is used.
6488 */
6489 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006490 if (!sb)
6491 return -ENOMEM;
David Sterba4db8c522015-12-03 13:06:46 +01006492 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006493 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006494 /*
6495 * The sb extent buffer is artifical and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006496 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006497 * pages up-to-date when the page is larger: extent does not cover the
6498 * whole page and consequently check_page_uptodate does not find all
6499 * the page's extents up-to-date (the hole beyond sb),
6500 * write_extent_buffer then triggers a WARN_ON.
6501 *
6502 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6503 * but sb spans only this function. Add an explicit SetPageUptodate call
6504 * to silence the warning eg. on PowerPC 64.
6505 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006506 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006507 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006508
Chris Masona061fc82008-05-07 11:43:44 -04006509 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006510 array_size = btrfs_super_sys_array_size(super_copy);
6511
David Sterba1ffb22c2014-10-31 19:02:42 +01006512 array_ptr = super_copy->sys_chunk_array;
6513 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6514 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006515
David Sterba1ffb22c2014-10-31 19:02:42 +01006516 while (cur_offset < array_size) {
6517 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006518 len = sizeof(*disk_key);
6519 if (cur_offset + len > array_size)
6520 goto out_short_read;
6521
Chris Mason0b86a832008-03-24 15:01:56 -04006522 btrfs_disk_key_to_cpu(&key, disk_key);
6523
David Sterba1ffb22c2014-10-31 19:02:42 +01006524 array_ptr += len;
6525 sb_array_offset += len;
6526 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006527
Chris Mason0d81ba52008-03-24 15:02:07 -04006528 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006529 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006530 /*
6531 * At least one btrfs_chunk with one stripe must be
6532 * present, exact stripe count check comes afterwards
6533 */
6534 len = btrfs_chunk_item_size(1);
6535 if (cur_offset + len > array_size)
6536 goto out_short_read;
6537
6538 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006539 if (!num_stripes) {
6540 printk(KERN_ERR
6541 "BTRFS: invalid number of stripes %u in sys_array at offset %u\n",
6542 num_stripes, cur_offset);
6543 ret = -EIO;
6544 break;
6545 }
6546
David Sterbae3540ea2014-11-05 15:24:51 +01006547 len = btrfs_chunk_item_size(num_stripes);
6548 if (cur_offset + len > array_size)
6549 goto out_short_read;
6550
Chris Mason0d81ba52008-03-24 15:02:07 -04006551 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006552 if (ret)
6553 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006554 } else {
David Sterba93a3d462015-11-30 17:27:09 +01006555 printk(KERN_ERR
6556 "BTRFS: unexpected item type %u in sys_array at offset %u\n",
6557 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006558 ret = -EIO;
6559 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006560 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006561 array_ptr += len;
6562 sb_array_offset += len;
6563 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006564 }
Chris Masona061fc82008-05-07 11:43:44 -04006565 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006566 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006567
6568out_short_read:
6569 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6570 len, cur_offset);
6571 free_extent_buffer(sb);
6572 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006573}
6574
6575int btrfs_read_chunk_tree(struct btrfs_root *root)
6576{
6577 struct btrfs_path *path;
6578 struct extent_buffer *leaf;
6579 struct btrfs_key key;
6580 struct btrfs_key found_key;
6581 int ret;
6582 int slot;
6583
6584 root = root->fs_info->chunk_root;
6585
6586 path = btrfs_alloc_path();
6587 if (!path)
6588 return -ENOMEM;
6589
Li Zefanb367e472011-12-07 11:38:24 +08006590 mutex_lock(&uuid_mutex);
6591 lock_chunks(root);
6592
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006593 /*
6594 * Read all device items, and then all the chunk items. All
6595 * device items are found before any chunk item (their object id
6596 * is smaller than the lowest possible object id for a chunk
6597 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006598 */
6599 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6600 key.offset = 0;
6601 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006602 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006603 if (ret < 0)
6604 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006605 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006606 leaf = path->nodes[0];
6607 slot = path->slots[0];
6608 if (slot >= btrfs_header_nritems(leaf)) {
6609 ret = btrfs_next_leaf(root, path);
6610 if (ret == 0)
6611 continue;
6612 if (ret < 0)
6613 goto error;
6614 break;
6615 }
6616 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006617 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6618 struct btrfs_dev_item *dev_item;
6619 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006620 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006621 ret = read_one_dev(root, leaf, dev_item);
6622 if (ret)
6623 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006624 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6625 struct btrfs_chunk *chunk;
6626 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6627 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006628 if (ret)
6629 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006630 }
6631 path->slots[0]++;
6632 }
Chris Mason0b86a832008-03-24 15:01:56 -04006633 ret = 0;
6634error:
Li Zefanb367e472011-12-07 11:38:24 +08006635 unlock_chunks(root);
6636 mutex_unlock(&uuid_mutex);
6637
Yan Zheng2b820322008-11-17 21:11:30 -05006638 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006639 return ret;
6640}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006641
Miao Xiecb517ea2013-05-15 07:48:19 +00006642void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6643{
6644 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6645 struct btrfs_device *device;
6646
Liu Bo29cc83f2014-05-11 23:14:59 +08006647 while (fs_devices) {
6648 mutex_lock(&fs_devices->device_list_mutex);
6649 list_for_each_entry(device, &fs_devices->devices, dev_list)
6650 device->dev_root = fs_info->dev_root;
6651 mutex_unlock(&fs_devices->device_list_mutex);
6652
6653 fs_devices = fs_devices->seed;
6654 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006655}
6656
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006657static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6658{
6659 int i;
6660
6661 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6662 btrfs_dev_stat_reset(dev, i);
6663}
6664
6665int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6666{
6667 struct btrfs_key key;
6668 struct btrfs_key found_key;
6669 struct btrfs_root *dev_root = fs_info->dev_root;
6670 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6671 struct extent_buffer *eb;
6672 int slot;
6673 int ret = 0;
6674 struct btrfs_device *device;
6675 struct btrfs_path *path = NULL;
6676 int i;
6677
6678 path = btrfs_alloc_path();
6679 if (!path) {
6680 ret = -ENOMEM;
6681 goto out;
6682 }
6683
6684 mutex_lock(&fs_devices->device_list_mutex);
6685 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6686 int item_size;
6687 struct btrfs_dev_stats_item *ptr;
6688
David Sterba242e2952016-01-25 17:51:31 +01006689 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6690 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006691 key.offset = device->devid;
6692 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6693 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006694 __btrfs_reset_dev_stats(device);
6695 device->dev_stats_valid = 1;
6696 btrfs_release_path(path);
6697 continue;
6698 }
6699 slot = path->slots[0];
6700 eb = path->nodes[0];
6701 btrfs_item_key_to_cpu(eb, &found_key, slot);
6702 item_size = btrfs_item_size_nr(eb, slot);
6703
6704 ptr = btrfs_item_ptr(eb, slot,
6705 struct btrfs_dev_stats_item);
6706
6707 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6708 if (item_size >= (1 + i) * sizeof(__le64))
6709 btrfs_dev_stat_set(device, i,
6710 btrfs_dev_stats_value(eb, ptr, i));
6711 else
6712 btrfs_dev_stat_reset(device, i);
6713 }
6714
6715 device->dev_stats_valid = 1;
6716 btrfs_dev_stat_print_on_load(device);
6717 btrfs_release_path(path);
6718 }
6719 mutex_unlock(&fs_devices->device_list_mutex);
6720
6721out:
6722 btrfs_free_path(path);
6723 return ret < 0 ? ret : 0;
6724}
6725
6726static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6727 struct btrfs_root *dev_root,
6728 struct btrfs_device *device)
6729{
6730 struct btrfs_path *path;
6731 struct btrfs_key key;
6732 struct extent_buffer *eb;
6733 struct btrfs_dev_stats_item *ptr;
6734 int ret;
6735 int i;
6736
David Sterba242e2952016-01-25 17:51:31 +01006737 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6738 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006739 key.offset = device->devid;
6740
6741 path = btrfs_alloc_path();
6742 BUG_ON(!path);
6743 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6744 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006745 btrfs_warn_in_rcu(dev_root->fs_info,
6746 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04006747 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006748 goto out;
6749 }
6750
6751 if (ret == 0 &&
6752 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6753 /* need to delete old one and insert a new one */
6754 ret = btrfs_del_item(trans, dev_root, path);
6755 if (ret != 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006756 btrfs_warn_in_rcu(dev_root->fs_info,
6757 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04006758 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006759 goto out;
6760 }
6761 ret = 1;
6762 }
6763
6764 if (ret == 1) {
6765 /* need to insert a new item */
6766 btrfs_release_path(path);
6767 ret = btrfs_insert_empty_item(trans, dev_root, path,
6768 &key, sizeof(*ptr));
6769 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006770 btrfs_warn_in_rcu(dev_root->fs_info,
6771 "insert dev_stats item for device %s failed %d",
6772 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006773 goto out;
6774 }
6775 }
6776
6777 eb = path->nodes[0];
6778 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6779 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6780 btrfs_set_dev_stats_value(eb, ptr, i,
6781 btrfs_dev_stat_read(device, i));
6782 btrfs_mark_buffer_dirty(eb);
6783
6784out:
6785 btrfs_free_path(path);
6786 return ret;
6787}
6788
6789/*
6790 * called from commit_transaction. Writes all changed device stats to disk.
6791 */
6792int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6793 struct btrfs_fs_info *fs_info)
6794{
6795 struct btrfs_root *dev_root = fs_info->dev_root;
6796 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6797 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006798 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006799 int ret = 0;
6800
6801 mutex_lock(&fs_devices->device_list_mutex);
6802 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006803 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006804 continue;
6805
Miao Xieaddc3fa2014-07-24 11:37:11 +08006806 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006807 ret = update_dev_stat_item(trans, dev_root, device);
6808 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006809 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006810 }
6811 mutex_unlock(&fs_devices->device_list_mutex);
6812
6813 return ret;
6814}
6815
Stefan Behrens442a4f62012-05-25 16:06:08 +02006816void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6817{
6818 btrfs_dev_stat_inc(dev, index);
6819 btrfs_dev_stat_print_on_error(dev);
6820}
6821
Eric Sandeen48a3b632013-04-25 20:41:01 +00006822static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006823{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006824 if (!dev->dev_stats_valid)
6825 return;
David Sterbab14af3b2015-10-08 10:43:10 +02006826 btrfs_err_rl_in_rcu(dev->dev_root->fs_info,
6827 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006828 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006829 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6830 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6831 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006832 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6833 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006834}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006835
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006836static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6837{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006838 int i;
6839
6840 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6841 if (btrfs_dev_stat_read(dev, i) != 0)
6842 break;
6843 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6844 return; /* all values == 0, suppress message */
6845
David Sterbaecaeb142015-10-08 09:01:03 +02006846 btrfs_info_in_rcu(dev->dev_root->fs_info,
6847 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006848 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006849 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6850 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6851 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6852 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6853 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6854}
6855
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006856int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006857 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006858{
6859 struct btrfs_device *dev;
6860 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6861 int i;
6862
6863 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006864 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006865 mutex_unlock(&fs_devices->device_list_mutex);
6866
6867 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006868 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006869 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006870 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006871 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006872 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006873 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006874 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6875 if (stats->nr_items > i)
6876 stats->values[i] =
6877 btrfs_dev_stat_read_and_reset(dev, i);
6878 else
6879 btrfs_dev_stat_reset(dev, i);
6880 }
6881 } else {
6882 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6883 if (stats->nr_items > i)
6884 stats->values[i] = btrfs_dev_stat_read(dev, i);
6885 }
6886 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6887 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6888 return 0;
6889}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006890
Anand Jain12b1c262015-08-14 18:32:59 +08006891void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006892{
6893 struct buffer_head *bh;
6894 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08006895 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006896
Anand Jain12b1c262015-08-14 18:32:59 +08006897 if (!bdev)
6898 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006899
Anand Jain12b1c262015-08-14 18:32:59 +08006900 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
6901 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006902
Anand Jain12b1c262015-08-14 18:32:59 +08006903 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
6904 continue;
6905
6906 disk_super = (struct btrfs_super_block *)bh->b_data;
6907
6908 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6909 set_buffer_dirty(bh);
6910 sync_dirty_buffer(bh);
6911 brelse(bh);
6912 }
6913
6914 /* Notify udev that device has changed */
6915 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
6916
6917 /* Update ctime/mtime for device path for libblkid */
6918 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006919}
Miao Xie935e5cc2014-09-03 21:35:33 +08006920
6921/*
6922 * Update the size of all devices, which is used for writing out the
6923 * super blocks.
6924 */
6925void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6926{
6927 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6928 struct btrfs_device *curr, *next;
6929
6930 if (list_empty(&fs_devices->resized_devices))
6931 return;
6932
6933 mutex_lock(&fs_devices->device_list_mutex);
6934 lock_chunks(fs_info->dev_root);
6935 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6936 resized_list) {
6937 list_del_init(&curr->resized_list);
6938 curr->commit_total_bytes = curr->disk_total_bytes;
6939 }
6940 unlock_chunks(fs_info->dev_root);
6941 mutex_unlock(&fs_devices->device_list_mutex);
6942}
Miao Xiece7213c2014-09-03 21:35:34 +08006943
6944/* Must be invoked during the transaction commit */
6945void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6946 struct btrfs_transaction *transaction)
6947{
6948 struct extent_map *em;
6949 struct map_lookup *map;
6950 struct btrfs_device *dev;
6951 int i;
6952
6953 if (list_empty(&transaction->pending_chunks))
6954 return;
6955
6956 /* In order to kick the device replace finish process */
6957 lock_chunks(root);
6958 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04006959 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08006960
6961 for (i = 0; i < map->num_stripes; i++) {
6962 dev = map->stripes[i].dev;
6963 dev->commit_bytes_used = dev->bytes_used;
6964 }
6965 }
6966 unlock_chunks(root);
6967}
Anand Jain5a13f432015-03-10 06:38:31 +08006968
6969void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6970{
6971 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6972 while (fs_devices) {
6973 fs_devices->fs_info = fs_info;
6974 fs_devices = fs_devices->seed;
6975 }
6976}
6977
6978void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6979{
6980 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6981 while (fs_devices) {
6982 fs_devices->fs_info = NULL;
6983 fs_devices = fs_devices->seed;
6984 }
6985}
Anand Jainf190aa42015-08-14 18:33:05 +08006986
David Sterba87ad58c2015-11-27 18:49:39 +01006987static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf190aa42015-08-14 18:33:05 +08006988{
6989 struct btrfs_fs_devices *fs_devices = device->fs_devices;
6990 struct btrfs_device *new_device;
6991 struct rcu_string *name;
6992
6993 if (device->bdev)
6994 fs_devices->open_devices--;
6995
6996 if (device->writeable &&
6997 device->devid != BTRFS_DEV_REPLACE_DEVID) {
6998 list_del_init(&device->dev_alloc_list);
6999 fs_devices->rw_devices--;
7000 }
7001
7002 if (device->missing)
7003 fs_devices->missing_devices--;
7004
7005 new_device = btrfs_alloc_device(NULL, &device->devid,
7006 device->uuid);
7007 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
7008
7009 /* Safe because we are under uuid_mutex */
7010 if (device->name) {
7011 name = rcu_string_strdup(device->name->str, GFP_NOFS);
7012 BUG_ON(!name); /* -ENOMEM */
7013 rcu_assign_pointer(new_device->name, name);
7014 }
7015
7016 list_replace_rcu(&device->dev_list, &new_device->dev_list);
7017 new_device->fs_devices = device->fs_devices;
7018
7019 call_rcu(&device->rcu, free_device);
7020}