blob: c5e0d2d431dcd5566d620b51b66f52fc763f582c [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 */
702 btrfs_free_stale_device(device);
703
Chris Mason8a4b83c2008-03-24 15:02:07 -0400704 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100705
706 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400707}
708
Yan Zhenge4404d62008-12-12 10:03:26 -0500709static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
710{
711 struct btrfs_fs_devices *fs_devices;
712 struct btrfs_device *device;
713 struct btrfs_device *orig_dev;
714
Ilya Dryomov2208a372013-08-12 14:33:03 +0300715 fs_devices = alloc_fs_devices(orig->fsid);
716 if (IS_ERR(fs_devices))
717 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500718
Miao Xieadbbb862014-09-03 21:35:42 +0800719 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400720 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500721
Xiao Guangrong46224702011-04-20 10:08:47 +0000722 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500723 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400724 struct rcu_string *name;
725
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300726 device = btrfs_alloc_device(NULL, &orig_dev->devid,
727 orig_dev->uuid);
728 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500729 goto error;
730
Josef Bacik606686e2012-06-04 14:03:51 -0400731 /*
732 * This is ok to do without rcu read locked because we hold the
733 * uuid mutex so nothing we touch in here is going to disappear.
734 */
Anand Jaine755f782014-06-30 17:12:47 +0800735 if (orig_dev->name) {
David Sterba78f2c9e2016-02-11 14:25:38 +0100736 name = rcu_string_strdup(orig_dev->name->str,
737 GFP_KERNEL);
Anand Jaine755f782014-06-30 17:12:47 +0800738 if (!name) {
739 kfree(device);
740 goto error;
741 }
742 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400743 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500744
Yan Zhenge4404d62008-12-12 10:03:26 -0500745 list_add(&device->dev_list, &fs_devices->devices);
746 device->fs_devices = fs_devices;
747 fs_devices->num_devices++;
748 }
Miao Xieadbbb862014-09-03 21:35:42 +0800749 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500750 return fs_devices;
751error:
Miao Xieadbbb862014-09-03 21:35:42 +0800752 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500753 free_fs_devices(fs_devices);
754 return ERR_PTR(-ENOMEM);
755}
756
Eric Sandeen9eaed212014-08-01 18:12:35 -0500757void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400758{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500759 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800760 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500761
Chris Masondfe25022008-05-13 13:46:40 -0400762 mutex_lock(&uuid_mutex);
763again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000764 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500765 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500766 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100767 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800768 (!latest_dev ||
769 device->generation > latest_dev->generation)) {
770 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500771 }
Yan Zheng2b820322008-11-17 21:11:30 -0500772 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500773 }
Yan Zheng2b820322008-11-17 21:11:30 -0500774
Stefan Behrens8dabb742012-11-06 13:15:27 +0100775 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
776 /*
777 * In the first step, keep the device which has
778 * the correct fsid and the devid that is used
779 * for the dev_replace procedure.
780 * In the second step, the dev_replace state is
781 * read from the device tree and it is known
782 * whether the procedure is really active or
783 * not, which means whether this device is
784 * used or whether it should be removed.
785 */
786 if (step == 0 || device->is_tgtdev_for_dev_replace) {
787 continue;
788 }
789 }
Yan Zheng2b820322008-11-17 21:11:30 -0500790 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100791 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500792 device->bdev = NULL;
793 fs_devices->open_devices--;
794 }
795 if (device->writeable) {
796 list_del_init(&device->dev_alloc_list);
797 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100798 if (!device->is_tgtdev_for_dev_replace)
799 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500800 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500801 list_del_init(&device->dev_list);
802 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400803 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500804 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400805 }
Yan Zheng2b820322008-11-17 21:11:30 -0500806
807 if (fs_devices->seed) {
808 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500809 goto again;
810 }
811
Miao Xie443f24f2014-07-24 11:37:15 +0800812 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500813
Chris Masondfe25022008-05-13 13:46:40 -0400814 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400815}
Chris Masona0af4692008-05-13 16:03:06 -0400816
Xiao Guangrong1f781602011-04-20 10:09:16 +0000817static void __free_device(struct work_struct *work)
818{
819 struct btrfs_device *device;
820
821 device = container_of(work, struct btrfs_device, rcu_work);
822
823 if (device->bdev)
824 blkdev_put(device->bdev, device->mode);
825
Josef Bacik606686e2012-06-04 14:03:51 -0400826 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000827 kfree(device);
828}
829
830static void free_device(struct rcu_head *head)
831{
832 struct btrfs_device *device;
833
834 device = container_of(head, struct btrfs_device, rcu);
835
836 INIT_WORK(&device->rcu_work, __free_device);
837 schedule_work(&device->rcu_work);
838}
839
Yan Zheng2b820322008-11-17 21:11:30 -0500840static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400841{
Sasha Levin2037a092015-05-12 19:31:37 -0400842 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500843
Yan Zheng2b820322008-11-17 21:11:30 -0500844 if (--fs_devices->opened > 0)
845 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400846
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000847 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400848 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jainf190aa42015-08-14 18:33:05 +0800849 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400850 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000851 mutex_unlock(&fs_devices->device_list_mutex);
852
Yan Zhenge4404d62008-12-12 10:03:26 -0500853 WARN_ON(fs_devices->open_devices);
854 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500855 fs_devices->opened = 0;
856 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500857
Chris Mason8a4b83c2008-03-24 15:02:07 -0400858 return 0;
859}
860
Yan Zheng2b820322008-11-17 21:11:30 -0500861int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
862{
Yan Zhenge4404d62008-12-12 10:03:26 -0500863 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500864 int ret;
865
866 mutex_lock(&uuid_mutex);
867 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500868 if (!fs_devices->opened) {
869 seed_devices = fs_devices->seed;
870 fs_devices->seed = NULL;
871 }
Yan Zheng2b820322008-11-17 21:11:30 -0500872 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500873
874 while (seed_devices) {
875 fs_devices = seed_devices;
876 seed_devices = fs_devices->seed;
877 __btrfs_close_devices(fs_devices);
878 free_fs_devices(fs_devices);
879 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000880 /*
881 * Wait for rcu kworkers under __btrfs_close_devices
882 * to finish all blkdev_puts so device is really
883 * free when umount is done.
884 */
885 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500886 return ret;
887}
888
Yan Zhenge4404d62008-12-12 10:03:26 -0500889static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
890 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400891{
Josef Bacikd5e20032011-08-04 14:52:27 +0000892 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400893 struct block_device *bdev;
894 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400895 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800896 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400897 struct buffer_head *bh;
898 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400899 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500900 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400901 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400902
Tejun Heod4d77622010-11-13 11:55:18 +0100903 flags |= FMODE_EXCL;
904
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500905 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400906 if (device->bdev)
907 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400908 if (!device->name)
909 continue;
910
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000911 /* Just open everything we can; ignore failures here */
912 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
913 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100914 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400915
916 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000917 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400918 if (devid != device->devid)
919 goto error_brelse;
920
Yan Zheng2b820322008-11-17 21:11:30 -0500921 if (memcmp(device->uuid, disk_super->dev_item.uuid,
922 BTRFS_UUID_SIZE))
923 goto error_brelse;
924
925 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800926 if (!latest_dev ||
927 device->generation > latest_dev->generation)
928 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400929
Yan Zheng2b820322008-11-17 21:11:30 -0500930 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
931 device->writeable = 0;
932 } else {
933 device->writeable = !bdev_read_only(bdev);
934 seeding = 0;
935 }
936
Josef Bacikd5e20032011-08-04 14:52:27 +0000937 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800938 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000939 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000940
Chris Mason8a4b83c2008-03-24 15:02:07 -0400941 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400942 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500943 device->mode = flags;
944
Chris Masonc2898112009-06-10 09:51:32 -0400945 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
946 fs_devices->rotating = 1;
947
Chris Masona0af4692008-05-13 16:03:06 -0400948 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300949 if (device->writeable &&
950 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500951 fs_devices->rw_devices++;
952 list_add(&device->dev_alloc_list,
953 &fs_devices->alloc_list);
954 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000955 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400956 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400957
Chris Masona0af4692008-05-13 16:03:06 -0400958error_brelse:
959 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100960 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400961 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400962 }
Chris Masona0af4692008-05-13 16:03:06 -0400963 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300964 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400965 goto out;
966 }
Yan Zheng2b820322008-11-17 21:11:30 -0500967 fs_devices->seeding = seeding;
968 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800969 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500970 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400971out:
Yan Zheng2b820322008-11-17 21:11:30 -0500972 return ret;
973}
974
975int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500976 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500977{
978 int ret;
979
980 mutex_lock(&uuid_mutex);
981 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500982 fs_devices->opened++;
983 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500984 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500985 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500986 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400987 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400988 return ret;
989}
990
David Sterba6f60cbd2013-02-15 11:31:02 -0700991/*
992 * Look for a btrfs signature on a device. This may be called out of the mount path
993 * and we are not allowed to call set_blocksize during the scan. The superblock
994 * is read via pagecache
995 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500996int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400997 struct btrfs_fs_devices **fs_devices_ret)
998{
999 struct btrfs_super_block *disk_super;
1000 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001001 struct page *page;
1002 void *p;
1003 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001004 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001005 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001006 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001007 u64 bytenr;
1008 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001009
David Sterba6f60cbd2013-02-15 11:31:02 -07001010 /*
1011 * we would like to check all the supers, but that would make
1012 * a btrfs mount succeed after a mkfs from a different FS.
1013 * So, we need to add a special mount option to scan for
1014 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1015 */
1016 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001017 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001018 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001019
1020 bdev = blkdev_get_by_path(path, flags, holder);
1021
1022 if (IS_ERR(bdev)) {
1023 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001024 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001025 }
1026
1027 /* make sure our super fits in the device */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
David Sterba6f60cbd2013-02-15 11:31:02 -07001029 goto error_bdev_put;
1030
1031 /* make sure our super fits in the page */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 if (sizeof(*disk_super) > PAGE_SIZE)
David Sterba6f60cbd2013-02-15 11:31:02 -07001033 goto error_bdev_put;
1034
1035 /* make sure our super doesn't straddle pages on disk */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001036 index = bytenr >> PAGE_SHIFT;
1037 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
David Sterba6f60cbd2013-02-15 11:31:02 -07001038 goto error_bdev_put;
1039
1040 /* pull in the page with our super */
1041 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1042 index, GFP_NOFS);
1043
1044 if (IS_ERR_OR_NULL(page))
1045 goto error_bdev_put;
1046
1047 p = kmap(page);
1048
1049 /* align our pointer to the offset of the super block */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001050 disk_super = p + (bytenr & ~PAGE_MASK);
David Sterba6f60cbd2013-02-15 11:31:02 -07001051
1052 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001053 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -07001054 goto error_unmap;
1055
Xiao Guangronga3438322010-01-06 11:48:18 +00001056 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001057 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001058 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001059
Chris Mason8a4b83c2008-03-24 15:02:07 -04001060 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001061 if (ret > 0) {
1062 if (disk_super->label[0]) {
1063 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1064 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1065 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1066 } else {
1067 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1068 }
1069
1070 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1071 ret = 0;
1072 }
Josef Bacik02db0842012-06-21 16:03:58 -04001073 if (!ret && fs_devices_ret)
1074 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001075
1076error_unmap:
1077 kunmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001078 put_page(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001079
1080error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001081 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001082error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001083 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001084 return ret;
1085}
Chris Mason0b86a832008-03-24 15:01:56 -04001086
Miao Xie6d07bce2011-01-05 10:07:31 +00001087/* helper to account the used device space in the range */
1088int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1089 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001090{
1091 struct btrfs_key key;
1092 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001093 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001094 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001095 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001096 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001097 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001098 struct extent_buffer *l;
1099
Miao Xie6d07bce2011-01-05 10:07:31 +00001100 *length = 0;
1101
Stefan Behrens63a212a2012-11-05 18:29:28 +01001102 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001103 return 0;
1104
Yan Zheng2b820322008-11-17 21:11:30 -05001105 path = btrfs_alloc_path();
1106 if (!path)
1107 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001108 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001109
Chris Mason0b86a832008-03-24 15:01:56 -04001110 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001111 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001112 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001113
1114 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001115 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001116 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001117 if (ret > 0) {
1118 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1119 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001120 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001121 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001122
Chris Mason0b86a832008-03-24 15:01:56 -04001123 while (1) {
1124 l = path->nodes[0];
1125 slot = path->slots[0];
1126 if (slot >= btrfs_header_nritems(l)) {
1127 ret = btrfs_next_leaf(root, path);
1128 if (ret == 0)
1129 continue;
1130 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001131 goto out;
1132
1133 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001134 }
1135 btrfs_item_key_to_cpu(l, &key, slot);
1136
1137 if (key.objectid < device->devid)
1138 goto next;
1139
1140 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001141 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001142
David Sterba962a2982014-06-04 18:41:45 +02001143 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001144 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001145
Chris Mason0b86a832008-03-24 15:01:56 -04001146 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001147 extent_end = key.offset + btrfs_dev_extent_length(l,
1148 dev_extent);
1149 if (key.offset <= start && extent_end > end) {
1150 *length = end - start + 1;
1151 break;
1152 } else if (key.offset <= start && extent_end > start)
1153 *length += extent_end - start;
1154 else if (key.offset > start && extent_end <= end)
1155 *length += extent_end - key.offset;
1156 else if (key.offset > start && key.offset <= end) {
1157 *length += end - key.offset + 1;
1158 break;
1159 } else if (key.offset > end)
1160 break;
1161
1162next:
1163 path->slots[0]++;
1164 }
1165 ret = 0;
1166out:
1167 btrfs_free_path(path);
1168 return ret;
1169}
1170
Jeff Mahoney499f3772015-06-15 09:41:17 -04001171static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001172 struct btrfs_device *device,
1173 u64 *start, u64 len)
1174{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001175 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001176 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001177 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001178 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001179 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001180
Jeff Mahoney499f3772015-06-15 09:41:17 -04001181 if (transaction)
1182 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001183again:
1184 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001185 struct map_lookup *map;
1186 int i;
1187
Jeff Mahoney95617d62015-06-03 10:55:48 -04001188 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001189 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001190 u64 end;
1191
Josef Bacik6df9a952013-06-27 13:22:46 -04001192 if (map->stripes[i].dev != device)
1193 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001194 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001195 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001196 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001197 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001198 /*
1199 * Make sure that while processing the pinned list we do
1200 * not override our *start with a lower value, because
1201 * we can have pinned chunks that fall within this
1202 * device hole and that have lower physical addresses
1203 * than the pending chunks we processed before. If we
1204 * do not take this special care we can end up getting
1205 * 2 pending chunks that start at the same physical
1206 * device offsets because the end offset of a pinned
1207 * chunk can be equal to the start offset of some
1208 * pending chunk.
1209 */
1210 end = map->stripes[i].physical + em->orig_block_len;
1211 if (end > *start) {
1212 *start = end;
1213 ret = 1;
1214 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001215 }
1216 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001217 if (search_list != &fs_info->pinned_chunks) {
1218 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001219 goto again;
1220 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001221
1222 return ret;
1223}
1224
1225
Chris Mason0b86a832008-03-24 15:01:56 -04001226/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001227 * find_free_dev_extent_start - find free space in the specified device
1228 * @device: the device which we search the free space in
1229 * @num_bytes: the size of the free space that we need
1230 * @search_start: the position from which to begin the search
1231 * @start: store the start of the free space.
1232 * @len: the size of the free space. that we find, or the size
1233 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001234 *
Chris Mason0b86a832008-03-24 15:01:56 -04001235 * this uses a pretty simple search, the expectation is that it is
1236 * called very infrequently and that a given device has a small number
1237 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001238 *
1239 * @start is used to store the start of the free space if we find. But if we
1240 * don't find suitable free space, it will be used to store the start position
1241 * of the max free space.
1242 *
1243 * @len is used to store the size of the free space that we find.
1244 * But if we don't find suitable free space, it is used to store the size of
1245 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001246 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001247int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1248 struct btrfs_device *device, u64 num_bytes,
1249 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001250{
1251 struct btrfs_key key;
1252 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001253 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001254 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001255 u64 hole_size;
1256 u64 max_hole_start;
1257 u64 max_hole_size;
1258 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001259 u64 search_end = device->total_bytes;
1260 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001261 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001262 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001263 u64 min_search_start;
1264
1265 /*
1266 * We don't want to overwrite the superblock on the drive nor any area
1267 * used by the boot loader (grub for example), so we make sure to start
1268 * at an offset of at least 1MB.
1269 */
1270 min_search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1271 search_start = max(search_start, min_search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001272
Josef Bacik6df9a952013-06-27 13:22:46 -04001273 path = btrfs_alloc_path();
1274 if (!path)
1275 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001276
Miao Xie7bfc8372011-01-05 10:07:26 +00001277 max_hole_start = search_start;
1278 max_hole_size = 0;
1279
Zhao Leif2ab7612015-02-16 18:52:17 +08001280again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001281 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001282 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001283 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001284 }
1285
David Sterbae4058b52015-11-27 16:31:35 +01001286 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001287 path->search_commit_root = 1;
1288 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001289
Chris Mason0b86a832008-03-24 15:01:56 -04001290 key.objectid = device->devid;
1291 key.offset = search_start;
1292 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001293
Li Zefan125ccb02011-12-08 15:07:24 +08001294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001295 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001296 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001297 if (ret > 0) {
1298 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1299 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001300 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001301 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001302
Chris Mason0b86a832008-03-24 15:01:56 -04001303 while (1) {
1304 l = path->nodes[0];
1305 slot = path->slots[0];
1306 if (slot >= btrfs_header_nritems(l)) {
1307 ret = btrfs_next_leaf(root, path);
1308 if (ret == 0)
1309 continue;
1310 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001311 goto out;
1312
1313 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001314 }
1315 btrfs_item_key_to_cpu(l, &key, slot);
1316
1317 if (key.objectid < device->devid)
1318 goto next;
1319
1320 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001321 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001322
David Sterba962a2982014-06-04 18:41:45 +02001323 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001324 goto next;
1325
Miao Xie7bfc8372011-01-05 10:07:26 +00001326 if (key.offset > search_start) {
1327 hole_size = key.offset - search_start;
1328
Josef Bacik6df9a952013-06-27 13:22:46 -04001329 /*
1330 * Have to check before we set max_hole_start, otherwise
1331 * we could end up sending back this offset anyway.
1332 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001333 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001334 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001335 hole_size)) {
1336 if (key.offset >= search_start) {
1337 hole_size = key.offset - search_start;
1338 } else {
1339 WARN_ON_ONCE(1);
1340 hole_size = 0;
1341 }
1342 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001343
Miao Xie7bfc8372011-01-05 10:07:26 +00001344 if (hole_size > max_hole_size) {
1345 max_hole_start = search_start;
1346 max_hole_size = hole_size;
1347 }
1348
1349 /*
1350 * If this free space is greater than which we need,
1351 * it must be the max free space that we have found
1352 * until now, so max_hole_start must point to the start
1353 * of this free space and the length of this free space
1354 * is stored in max_hole_size. Thus, we return
1355 * max_hole_start and max_hole_size and go back to the
1356 * caller.
1357 */
1358 if (hole_size >= num_bytes) {
1359 ret = 0;
1360 goto out;
1361 }
1362 }
1363
Chris Mason0b86a832008-03-24 15:01:56 -04001364 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001365 extent_end = key.offset + btrfs_dev_extent_length(l,
1366 dev_extent);
1367 if (extent_end > search_start)
1368 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001369next:
1370 path->slots[0]++;
1371 cond_resched();
1372 }
Chris Mason0b86a832008-03-24 15:01:56 -04001373
liubo38c01b92011-08-02 02:39:03 +00001374 /*
1375 * At this point, search_start should be the end of
1376 * allocated dev extents, and when shrinking the device,
1377 * search_end may be smaller than search_start.
1378 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001379 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001380 hole_size = search_end - search_start;
1381
Jeff Mahoney499f3772015-06-15 09:41:17 -04001382 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001383 hole_size)) {
1384 btrfs_release_path(path);
1385 goto again;
1386 }
Chris Mason0b86a832008-03-24 15:01:56 -04001387
Zhao Leif2ab7612015-02-16 18:52:17 +08001388 if (hole_size > max_hole_size) {
1389 max_hole_start = search_start;
1390 max_hole_size = hole_size;
1391 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001392 }
1393
Miao Xie7bfc8372011-01-05 10:07:26 +00001394 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001395 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001396 ret = -ENOSPC;
1397 else
1398 ret = 0;
1399
1400out:
Yan Zheng2b820322008-11-17 21:11:30 -05001401 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001402 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001403 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001404 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001405 return ret;
1406}
1407
Jeff Mahoney499f3772015-06-15 09:41:17 -04001408int find_free_dev_extent(struct btrfs_trans_handle *trans,
1409 struct btrfs_device *device, u64 num_bytes,
1410 u64 *start, u64 *len)
1411{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001412 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001413 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001414 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001415}
1416
Christoph Hellwigb2950862008-12-02 09:54:17 -05001417static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001418 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001419 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001420{
1421 int ret;
1422 struct btrfs_path *path;
1423 struct btrfs_root *root = device->dev_root;
1424 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001425 struct btrfs_key found_key;
1426 struct extent_buffer *leaf = NULL;
1427 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001428
1429 path = btrfs_alloc_path();
1430 if (!path)
1431 return -ENOMEM;
1432
1433 key.objectid = device->devid;
1434 key.offset = start;
1435 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001436again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001437 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001438 if (ret > 0) {
1439 ret = btrfs_previous_item(root, path, key.objectid,
1440 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001441 if (ret)
1442 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001443 leaf = path->nodes[0];
1444 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1445 extent = btrfs_item_ptr(leaf, path->slots[0],
1446 struct btrfs_dev_extent);
1447 BUG_ON(found_key.offset > start || found_key.offset +
1448 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001449 key = found_key;
1450 btrfs_release_path(path);
1451 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001452 } else if (ret == 0) {
1453 leaf = path->nodes[0];
1454 extent = btrfs_item_ptr(leaf, path->slots[0],
1455 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 } else {
Anand Jaina4553fe2015-09-25 14:43:01 +08001457 btrfs_std_error(root->fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001458 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001459 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001460
Miao Xie2196d6e2014-09-03 21:35:41 +08001461 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1462
Chris Mason8f18cf12008-04-25 16:53:30 -04001463 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464 if (ret) {
Anand Jaina4553fe2015-09-25 14:43:01 +08001465 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001466 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001467 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001468 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001469 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001470out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001471 btrfs_free_path(path);
1472 return ret;
1473}
1474
Eric Sandeen48a3b632013-04-25 20:41:01 +00001475static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1476 struct btrfs_device *device,
1477 u64 chunk_tree, u64 chunk_objectid,
1478 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001479{
1480 int ret;
1481 struct btrfs_path *path;
1482 struct btrfs_root *root = device->dev_root;
1483 struct btrfs_dev_extent *extent;
1484 struct extent_buffer *leaf;
1485 struct btrfs_key key;
1486
Chris Masondfe25022008-05-13 13:46:40 -04001487 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001488 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001489 path = btrfs_alloc_path();
1490 if (!path)
1491 return -ENOMEM;
1492
Chris Mason0b86a832008-03-24 15:01:56 -04001493 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001494 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001495 key.type = BTRFS_DEV_EXTENT_KEY;
1496 ret = btrfs_insert_empty_item(trans, root, path, &key,
1497 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001498 if (ret)
1499 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001500
1501 leaf = path->nodes[0];
1502 extent = btrfs_item_ptr(leaf, path->slots[0],
1503 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001504 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1505 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1506 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1507
1508 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001509 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001510
Chris Mason0b86a832008-03-24 15:01:56 -04001511 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1512 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001513out:
Chris Mason0b86a832008-03-24 15:01:56 -04001514 btrfs_free_path(path);
1515 return ret;
1516}
1517
Josef Bacik6df9a952013-06-27 13:22:46 -04001518static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001519{
Josef Bacik6df9a952013-06-27 13:22:46 -04001520 struct extent_map_tree *em_tree;
1521 struct extent_map *em;
1522 struct rb_node *n;
1523 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001524
Josef Bacik6df9a952013-06-27 13:22:46 -04001525 em_tree = &fs_info->mapping_tree.map_tree;
1526 read_lock(&em_tree->lock);
1527 n = rb_last(&em_tree->map);
1528 if (n) {
1529 em = rb_entry(n, struct extent_map, rb_node);
1530 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001531 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001532 read_unlock(&em_tree->lock);
1533
Chris Mason0b86a832008-03-24 15:01:56 -04001534 return ret;
1535}
1536
Ilya Dryomov53f10652013-08-12 14:33:01 +03001537static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1538 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001539{
1540 int ret;
1541 struct btrfs_key key;
1542 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001543 struct btrfs_path *path;
1544
Yan Zheng2b820322008-11-17 21:11:30 -05001545 path = btrfs_alloc_path();
1546 if (!path)
1547 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001548
1549 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1550 key.type = BTRFS_DEV_ITEM_KEY;
1551 key.offset = (u64)-1;
1552
Ilya Dryomov53f10652013-08-12 14:33:01 +03001553 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001554 if (ret < 0)
1555 goto error;
1556
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001557 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001558
Ilya Dryomov53f10652013-08-12 14:33:01 +03001559 ret = btrfs_previous_item(fs_info->chunk_root, path,
1560 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001561 BTRFS_DEV_ITEM_KEY);
1562 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001563 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001564 } else {
1565 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1566 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001567 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001568 }
1569 ret = 0;
1570error:
Yan Zheng2b820322008-11-17 21:11:30 -05001571 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001572 return ret;
1573}
1574
1575/*
1576 * the device information is stored in the chunk root
1577 * the btrfs_device struct should be fully filled in
1578 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001579static int btrfs_add_device(struct btrfs_trans_handle *trans,
1580 struct btrfs_root *root,
1581 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001582{
1583 int ret;
1584 struct btrfs_path *path;
1585 struct btrfs_dev_item *dev_item;
1586 struct extent_buffer *leaf;
1587 struct btrfs_key key;
1588 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001589
1590 root = root->fs_info->chunk_root;
1591
1592 path = btrfs_alloc_path();
1593 if (!path)
1594 return -ENOMEM;
1595
Chris Mason0b86a832008-03-24 15:01:56 -04001596 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1597 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001598 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001599
1600 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001601 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001602 if (ret)
1603 goto out;
1604
1605 leaf = path->nodes[0];
1606 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1607
1608 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001609 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001610 btrfs_set_device_type(leaf, dev_item, device->type);
1611 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1612 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1613 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001614 btrfs_set_device_total_bytes(leaf, dev_item,
1615 btrfs_device_get_disk_total_bytes(device));
1616 btrfs_set_device_bytes_used(leaf, dev_item,
1617 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001618 btrfs_set_device_group(leaf, dev_item, 0);
1619 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1620 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001621 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001622
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001623 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001624 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001625 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001626 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001627 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001628
Yan Zheng2b820322008-11-17 21:11:30 -05001629 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001630out:
1631 btrfs_free_path(path);
1632 return ret;
1633}
Chris Mason8f18cf12008-04-25 16:53:30 -04001634
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001635/*
1636 * Function to update ctime/mtime for a given device path.
1637 * Mainly used for ctime/mtime based probe like libblkid.
1638 */
1639static void update_dev_time(char *path_name)
1640{
1641 struct file *filp;
1642
1643 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001644 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001645 return;
1646 file_update_time(filp);
1647 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001648}
1649
Chris Masona061fc82008-05-07 11:43:44 -04001650static int btrfs_rm_dev_item(struct btrfs_root *root,
1651 struct btrfs_device *device)
1652{
1653 int ret;
1654 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001655 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001656 struct btrfs_trans_handle *trans;
1657
1658 root = root->fs_info->chunk_root;
1659
1660 path = btrfs_alloc_path();
1661 if (!path)
1662 return -ENOMEM;
1663
Yan, Zhenga22285a2010-05-16 10:48:46 -04001664 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001665 if (IS_ERR(trans)) {
1666 btrfs_free_path(path);
1667 return PTR_ERR(trans);
1668 }
Chris Masona061fc82008-05-07 11:43:44 -04001669 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1670 key.type = BTRFS_DEV_ITEM_KEY;
1671 key.offset = device->devid;
1672
1673 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1674 if (ret < 0)
1675 goto out;
1676
1677 if (ret > 0) {
1678 ret = -ENOENT;
1679 goto out;
1680 }
1681
1682 ret = btrfs_del_item(trans, root, path);
1683 if (ret)
1684 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001685out:
1686 btrfs_free_path(path);
1687 btrfs_commit_transaction(trans, root);
1688 return ret;
1689}
1690
1691int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1692{
1693 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001694 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001695 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001696 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001697 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001698 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001699 u64 all_avail;
1700 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001701 u64 num_devices;
1702 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001703 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001704 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001705 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001706
Chris Masona061fc82008-05-07 11:43:44 -04001707 mutex_lock(&uuid_mutex);
1708
Miao Xiede98ced2013-01-29 10:13:12 +00001709 do {
1710 seq = read_seqbegin(&root->fs_info->profiles_lock);
1711
1712 all_avail = root->fs_info->avail_data_alloc_bits |
1713 root->fs_info->avail_system_alloc_bits |
1714 root->fs_info->avail_metadata_alloc_bits;
1715 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001716
Stefan Behrens8dabb742012-11-06 13:15:27 +01001717 num_devices = root->fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08001718 btrfs_dev_replace_lock(&root->fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01001719 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1720 WARN_ON(num_devices < 1);
1721 num_devices--;
1722 }
Liu Bo73beece2015-07-17 16:49:19 +08001723 btrfs_dev_replace_unlock(&root->fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01001724
1725 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001726 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001727 goto out;
1728 }
1729
Stefan Behrens8dabb742012-11-06 13:15:27 +01001730 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001731 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001732 goto out;
1733 }
1734
David Woodhouse53b381b2013-01-29 18:40:14 -05001735 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1736 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001737 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001738 goto out;
1739 }
1740 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1741 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001742 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001743 goto out;
1744 }
1745
Chris Masondfe25022008-05-13 13:46:40 -04001746 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001747 struct list_head *devices;
1748 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001749
Chris Masondfe25022008-05-13 13:46:40 -04001750 device = NULL;
1751 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001752 /*
1753 * It is safe to read the devices since the volume_mutex
1754 * is held.
1755 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001756 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001757 if (tmp->in_fs_metadata &&
1758 !tmp->is_tgtdev_for_dev_replace &&
1759 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001760 device = tmp;
1761 break;
1762 }
1763 }
1764 bdev = NULL;
1765 bh = NULL;
1766 disk_super = NULL;
1767 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001768 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001769 goto out;
1770 }
Chris Masondfe25022008-05-13 13:46:40 -04001771 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001772 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001773 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001774 root->fs_info->bdev_holder, 0,
1775 &bdev, &bh);
1776 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001777 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001778 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001779 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001780 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001781 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001782 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001783 if (!device) {
1784 ret = -ENOENT;
1785 goto error_brelse;
1786 }
Chris Masondfe25022008-05-13 13:46:40 -04001787 }
Yan Zheng2b820322008-11-17 21:11:30 -05001788
Stefan Behrens63a212a2012-11-05 18:29:28 +01001789 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001790 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001791 goto error_brelse;
1792 }
1793
Yan Zheng2b820322008-11-17 21:11:30 -05001794 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001795 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001796 goto error_brelse;
1797 }
1798
1799 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001800 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001801 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001802 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001803 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001804 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001805 }
Chris Masona061fc82008-05-07 11:43:44 -04001806
Carey Underwoodd7901552013-03-04 16:37:06 -06001807 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001808 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001809 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001810 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001811 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001812
Stefan Behrens63a212a2012-11-05 18:29:28 +01001813 /*
1814 * TODO: the superblock still includes this device in its num_devices
1815 * counter although write_all_supers() is not locked out. This
1816 * could give a filesystem state which requires a degraded mount.
1817 */
Chris Masona061fc82008-05-07 11:43:44 -04001818 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1819 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001820 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001821
Yan Zheng2b820322008-11-17 21:11:30 -05001822 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001823 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001824
1825 /*
1826 * the device list mutex makes sure that we don't change
1827 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001828 * the device supers. Whoever is writing all supers, should
1829 * lock the device list mutex before getting the number of
1830 * devices in the super block (super_copy). Conversely,
1831 * whoever updates the number of devices in the super block
1832 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001833 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001834
1835 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001836 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001837 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001838
Yan Zhenge4404d62008-12-12 10:03:26 -05001839 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001840 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001841
Chris Masoncd02dca2010-12-13 14:56:23 -05001842 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001843 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001844
Yan Zheng2b820322008-11-17 21:11:30 -05001845 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1846 struct btrfs_device, dev_list);
1847 if (device->bdev == root->fs_info->sb->s_bdev)
1848 root->fs_info->sb->s_bdev = next_device->bdev;
1849 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1850 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1851
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001852 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001853 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001854 /* remove sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +08001855 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001856 }
Anand Jain99994cd2014-06-03 11:36:00 +08001857
Xiao Guangrong1f781602011-04-20 10:09:16 +00001858 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001859
David Sterba6c417612011-04-13 15:41:04 +02001860 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1861 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001862 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001863
Xiao Guangrong1f781602011-04-20 10:09:16 +00001864 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001865 struct btrfs_fs_devices *fs_devices;
1866 fs_devices = root->fs_info->fs_devices;
1867 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001868 if (fs_devices->seed == cur_devices) {
1869 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001870 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001871 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001872 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001873 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001874 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001875 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001876 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001877 }
1878
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001879 root->fs_info->num_tolerated_disk_barrier_failures =
1880 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1881
Yan Zheng2b820322008-11-17 21:11:30 -05001882 /*
1883 * at this point, the device is zero sized. We want to
1884 * remove it from the devices list and zero out the old super
1885 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001886 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001887 u64 bytenr;
1888 int i;
1889
Chris Masondfe25022008-05-13 13:46:40 -04001890 /* make sure this device isn't detected as part of
1891 * the FS anymore
1892 */
1893 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1894 set_buffer_dirty(bh);
1895 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001896
1897 /* clear the mirror copies of super block on the disk
1898 * being removed, 0th copy is been taken care above and
1899 * the below would take of the rest
1900 */
1901 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1902 bytenr = btrfs_sb_offset(i);
1903 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1904 i_size_read(bdev->bd_inode))
1905 break;
1906
1907 brelse(bh);
1908 bh = __bread(bdev, bytenr / 4096,
1909 BTRFS_SUPER_INFO_SIZE);
1910 if (!bh)
1911 continue;
1912
1913 disk_super = (struct btrfs_super_block *)bh->b_data;
1914
1915 if (btrfs_super_bytenr(disk_super) != bytenr ||
1916 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1917 continue;
1918 }
1919 memset(&disk_super->magic, 0,
1920 sizeof(disk_super->magic));
1921 set_buffer_dirty(bh);
1922 sync_dirty_buffer(bh);
1923 }
Chris Masondfe25022008-05-13 13:46:40 -04001924 }
Chris Masona061fc82008-05-07 11:43:44 -04001925
Chris Masona061fc82008-05-07 11:43:44 -04001926 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001927
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001928 if (bdev) {
1929 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001930 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001931
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001932 /* Update ctime/mtime for device path for libblkid */
1933 update_dev_time(device_path);
1934 }
1935
Chris Masona061fc82008-05-07 11:43:44 -04001936error_brelse:
1937 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001938 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001939 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001940out:
1941 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001942 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001943error_undo:
1944 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001945 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001946 list_add(&device->dev_alloc_list,
1947 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001948 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001949 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001950 }
1951 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001952}
1953
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001954void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1955 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001956{
Anand Jaind51908c2014-08-13 14:24:19 +08001957 struct btrfs_fs_devices *fs_devices;
1958
Stefan Behrense93c89c2012-11-05 17:33:06 +01001959 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001960
Anand Jain25e8e912014-08-20 10:56:56 +08001961 /*
1962 * in case of fs with no seed, srcdev->fs_devices will point
1963 * to fs_devices of fs_info. However when the dev being replaced is
1964 * a seed dev it will point to the seed's local fs_devices. In short
1965 * srcdev will have its correct fs_devices in both the cases.
1966 */
1967 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001968
Stefan Behrense93c89c2012-11-05 17:33:06 +01001969 list_del_rcu(&srcdev->dev_list);
1970 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001971 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001972 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001973 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001974
Anand Jain48b3b9d2016-04-12 21:36:16 +08001975 if (srcdev->writeable)
Miao Xie82372bc2014-09-03 21:35:44 +08001976 fs_devices->rw_devices--;
Ilya Dryomov13572722013-10-02 20:41:01 +03001977
Miao Xie82372bc2014-09-03 21:35:44 +08001978 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001979 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001980}
1981
1982void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1983 struct btrfs_device *srcdev)
1984{
1985 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001986
Anand Jain48b3b9d2016-04-12 21:36:16 +08001987 if (srcdev->writeable) {
1988 /* zero out the old super if it is writable */
1989 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
1990 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001991 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001992
1993 /*
1994 * unless fs_devices is seed fs, num_devices shouldn't go
1995 * zero
1996 */
1997 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1998
1999 /* if this is no devs we rather delete the fs_devices */
2000 if (!fs_devices->num_devices) {
2001 struct btrfs_fs_devices *tmp_fs_devices;
2002
2003 tmp_fs_devices = fs_info->fs_devices;
2004 while (tmp_fs_devices) {
2005 if (tmp_fs_devices->seed == fs_devices) {
2006 tmp_fs_devices->seed = fs_devices->seed;
2007 break;
2008 }
2009 tmp_fs_devices = tmp_fs_devices->seed;
2010 }
2011 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002012 __btrfs_close_devices(fs_devices);
2013 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002014 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002015}
2016
2017void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2018 struct btrfs_device *tgtdev)
2019{
2020 struct btrfs_device *next_device;
2021
Miao Xie67a2c452014-09-03 21:35:43 +08002022 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002023 WARN_ON(!tgtdev);
2024 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002025
Anand Jain32576042015-08-14 18:32:49 +08002026 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002027
Anand Jain779bf3f2016-04-18 16:51:23 +08002028 if (tgtdev->bdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01002029 fs_info->fs_devices->open_devices--;
Anand Jain779bf3f2016-04-18 16:51:23 +08002030
Stefan Behrense93c89c2012-11-05 17:33:06 +01002031 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002032
2033 next_device = list_entry(fs_info->fs_devices->devices.next,
2034 struct btrfs_device, dev_list);
2035 if (tgtdev->bdev == fs_info->sb->s_bdev)
2036 fs_info->sb->s_bdev = next_device->bdev;
2037 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
2038 fs_info->fs_devices->latest_bdev = next_device->bdev;
2039 list_del_rcu(&tgtdev->dev_list);
2040
Stefan Behrense93c89c2012-11-05 17:33:06 +01002041 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002042 mutex_unlock(&uuid_mutex);
Anand Jain779bf3f2016-04-18 16:51:23 +08002043
2044 /*
2045 * The update_dev_time() with in btrfs_scratch_superblocks()
2046 * may lead to a call to btrfs_show_devname() which will try
2047 * to hold device_list_mutex. And here this device
2048 * is already out of device list, so we don't have to hold
2049 * the device_list_mutex lock.
2050 */
2051 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
2052 call_rcu(&tgtdev->rcu, free_device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002053}
2054
Eric Sandeen48a3b632013-04-25 20:41:01 +00002055static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2056 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002057{
2058 int ret = 0;
2059 struct btrfs_super_block *disk_super;
2060 u64 devid;
2061 u8 *dev_uuid;
2062 struct block_device *bdev;
2063 struct buffer_head *bh;
2064
2065 *device = NULL;
2066 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2067 root->fs_info->bdev_holder, 0, &bdev, &bh);
2068 if (ret)
2069 return ret;
2070 disk_super = (struct btrfs_super_block *)bh->b_data;
2071 devid = btrfs_stack_device_id(&disk_super->dev_item);
2072 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002073 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002074 disk_super->fsid);
2075 brelse(bh);
2076 if (!*device)
2077 ret = -ENOENT;
2078 blkdev_put(bdev, FMODE_READ);
2079 return ret;
2080}
2081
2082int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2083 char *device_path,
2084 struct btrfs_device **device)
2085{
2086 *device = NULL;
2087 if (strcmp(device_path, "missing") == 0) {
2088 struct list_head *devices;
2089 struct btrfs_device *tmp;
2090
2091 devices = &root->fs_info->fs_devices->devices;
2092 /*
2093 * It is safe to read the devices since the volume_mutex
2094 * is held by the caller.
2095 */
2096 list_for_each_entry(tmp, devices, dev_list) {
2097 if (tmp->in_fs_metadata && !tmp->bdev) {
2098 *device = tmp;
2099 break;
2100 }
2101 }
2102
Anand Jaind74a6252015-08-14 18:32:56 +08002103 if (!*device)
2104 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002105
2106 return 0;
2107 } else {
2108 return btrfs_find_device_by_path(root, device_path, device);
2109 }
2110}
2111
Yan Zheng2b820322008-11-17 21:11:30 -05002112/*
2113 * does all the dirty work required for changing file system's UUID.
2114 */
Li Zefan125ccb02011-12-08 15:07:24 +08002115static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002116{
2117 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2118 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002119 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002120 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002121 struct btrfs_device *device;
2122 u64 super_flags;
2123
2124 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002125 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002126 return -EINVAL;
2127
Ilya Dryomov2208a372013-08-12 14:33:03 +03002128 seed_devices = __alloc_fs_devices();
2129 if (IS_ERR(seed_devices))
2130 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002131
Yan Zhenge4404d62008-12-12 10:03:26 -05002132 old_devices = clone_fs_devices(fs_devices);
2133 if (IS_ERR(old_devices)) {
2134 kfree(seed_devices);
2135 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002136 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002137
Yan Zheng2b820322008-11-17 21:11:30 -05002138 list_add(&old_devices->list, &fs_uuids);
2139
Yan Zhenge4404d62008-12-12 10:03:26 -05002140 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2141 seed_devices->opened = 1;
2142 INIT_LIST_HEAD(&seed_devices->devices);
2143 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002144 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002145
2146 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002147 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2148 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002149 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002150 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002151
2152 lock_chunks(root);
2153 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2154 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002155
Yan Zheng2b820322008-11-17 21:11:30 -05002156 fs_devices->seeding = 0;
2157 fs_devices->num_devices = 0;
2158 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002159 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002160 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002161 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002162
2163 generate_random_uuid(fs_devices->fsid);
2164 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2165 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002166 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2167
Yan Zheng2b820322008-11-17 21:11:30 -05002168 super_flags = btrfs_super_flags(disk_super) &
2169 ~BTRFS_SUPER_FLAG_SEEDING;
2170 btrfs_set_super_flags(disk_super, super_flags);
2171
2172 return 0;
2173}
2174
2175/*
2176 * strore the expected generation for seed devices in device items.
2177 */
2178static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2179 struct btrfs_root *root)
2180{
2181 struct btrfs_path *path;
2182 struct extent_buffer *leaf;
2183 struct btrfs_dev_item *dev_item;
2184 struct btrfs_device *device;
2185 struct btrfs_key key;
2186 u8 fs_uuid[BTRFS_UUID_SIZE];
2187 u8 dev_uuid[BTRFS_UUID_SIZE];
2188 u64 devid;
2189 int ret;
2190
2191 path = btrfs_alloc_path();
2192 if (!path)
2193 return -ENOMEM;
2194
2195 root = root->fs_info->chunk_root;
2196 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2197 key.offset = 0;
2198 key.type = BTRFS_DEV_ITEM_KEY;
2199
2200 while (1) {
2201 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2202 if (ret < 0)
2203 goto error;
2204
2205 leaf = path->nodes[0];
2206next_slot:
2207 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2208 ret = btrfs_next_leaf(root, path);
2209 if (ret > 0)
2210 break;
2211 if (ret < 0)
2212 goto error;
2213 leaf = path->nodes[0];
2214 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002215 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002216 continue;
2217 }
2218
2219 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2220 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2221 key.type != BTRFS_DEV_ITEM_KEY)
2222 break;
2223
2224 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2225 struct btrfs_dev_item);
2226 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002227 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002228 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002229 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002230 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002231 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2232 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002233 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002234
2235 if (device->fs_devices->seeding) {
2236 btrfs_set_device_generation(leaf, dev_item,
2237 device->generation);
2238 btrfs_mark_buffer_dirty(leaf);
2239 }
2240
2241 path->slots[0]++;
2242 goto next_slot;
2243 }
2244 ret = 0;
2245error:
2246 btrfs_free_path(path);
2247 return ret;
2248}
2249
Chris Mason788f20e2008-04-28 15:29:42 -04002250int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2251{
Josef Bacikd5e20032011-08-04 14:52:27 +00002252 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002253 struct btrfs_trans_handle *trans;
2254 struct btrfs_device *device;
2255 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002256 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002257 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002258 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002259 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002260 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002261 int ret = 0;
2262
Yan Zheng2b820322008-11-17 21:11:30 -05002263 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002264 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002265
Li Zefana5d16332011-12-07 20:08:40 -05002266 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002267 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002268 if (IS_ERR(bdev))
2269 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002270
Yan Zheng2b820322008-11-17 21:11:30 -05002271 if (root->fs_info->fs_devices->seeding) {
2272 seeding_dev = 1;
2273 down_write(&sb->s_umount);
2274 mutex_lock(&uuid_mutex);
2275 }
2276
Chris Mason8c8bee12008-09-29 11:19:10 -04002277 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002278
Chris Mason788f20e2008-04-28 15:29:42 -04002279 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002280
2281 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002282 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002283 if (device->bdev == bdev) {
2284 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002285 mutex_unlock(
2286 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002287 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002288 }
2289 }
Liu Bod25628b2012-11-14 14:35:30 +00002290 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002291
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002292 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2293 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002294 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002295 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002296 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002297 }
2298
David Sterba78f2c9e2016-02-11 14:25:38 +01002299 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002300 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002301 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002302 ret = -ENOMEM;
2303 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002304 }
Josef Bacik606686e2012-06-04 14:03:51 -04002305 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002306
Yan, Zhenga22285a2010-05-16 10:48:46 -04002307 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002308 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002309 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002310 kfree(device);
2311 ret = PTR_ERR(trans);
2312 goto error;
2313 }
2314
Josef Bacikd5e20032011-08-04 14:52:27 +00002315 q = bdev_get_queue(bdev);
2316 if (blk_queue_discard(q))
2317 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002318 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002319 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002320 device->io_width = root->sectorsize;
2321 device->io_align = root->sectorsize;
2322 device->sector_size = root->sectorsize;
2323 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002324 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002325 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002326 device->dev_root = root->fs_info->dev_root;
2327 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002328 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002329 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002330 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002331 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002332 set_blocksize(device->bdev, 4096);
2333
Yan Zheng2b820322008-11-17 21:11:30 -05002334 if (seeding_dev) {
2335 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002336 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002337 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002338 }
2339
2340 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002341
Chris Masone5e9a522009-06-10 15:17:02 -04002342 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002343 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002344 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002345 list_add(&device->dev_alloc_list,
2346 &root->fs_info->fs_devices->alloc_list);
2347 root->fs_info->fs_devices->num_devices++;
2348 root->fs_info->fs_devices->open_devices++;
2349 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002350 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002351 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2352
Josef Bacik2bf64752011-09-26 17:12:22 -04002353 spin_lock(&root->fs_info->free_chunk_lock);
2354 root->fs_info->free_chunk_space += device->total_bytes;
2355 spin_unlock(&root->fs_info->free_chunk_lock);
2356
Chris Masonc2898112009-06-10 09:51:32 -04002357 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2358 root->fs_info->fs_devices->rotating = 1;
2359
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002360 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002361 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002362 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002363
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002364 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002365 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002366 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002367
2368 /* add sysfs device entry */
Anand Jaine3bd6972015-08-14 18:32:48 +08002369 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002370
Miao Xie2196d6e2014-09-03 21:35:41 +08002371 /*
2372 * we've got more storage, clear any full flags on the space
2373 * infos
2374 */
2375 btrfs_clear_space_info_full(root->fs_info);
2376
2377 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002378 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002379
Yan Zheng2b820322008-11-17 21:11:30 -05002380 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002381 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002382 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002383 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002384 if (ret) {
2385 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002386 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002387 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002388 }
2389
2390 ret = btrfs_add_device(trans, root, device);
2391 if (ret) {
2392 btrfs_abort_transaction(trans, root, ret);
2393 goto error_trans;
2394 }
2395
2396 if (seeding_dev) {
2397 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2398
Yan Zheng2b820322008-11-17 21:11:30 -05002399 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002400 if (ret) {
2401 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002402 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002403 }
Anand Jainb2373f22014-06-03 11:36:03 +08002404
2405 /* Sprouting would change fsid of the mounted root,
2406 * so rename the fsid on the sysfs
2407 */
2408 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2409 root->fs_info->fsid);
Anand Jainc1b7e472015-08-14 18:32:50 +08002410 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
Anand Jain2e7910d2015-03-10 06:38:29 +08002411 fsid_buf))
David Sterbaf14d1042015-10-08 11:37:06 +02002412 btrfs_warn(root->fs_info,
2413 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002414 }
2415
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002416 root->fs_info->num_tolerated_disk_barrier_failures =
2417 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002418 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002419
2420 if (seeding_dev) {
2421 mutex_unlock(&uuid_mutex);
2422 up_write(&sb->s_umount);
2423
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002424 if (ret) /* transaction commit */
2425 return ret;
2426
Yan Zheng2b820322008-11-17 21:11:30 -05002427 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002428 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002429 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002430 "Failed to relocate sys chunks after "
2431 "device initialization. This can be fixed "
2432 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002433 trans = btrfs_attach_transaction(root);
2434 if (IS_ERR(trans)) {
2435 if (PTR_ERR(trans) == -ENOENT)
2436 return 0;
2437 return PTR_ERR(trans);
2438 }
2439 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002440 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002441
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002442 /* Update ctime/mtime for libblkid */
2443 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002444 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002445
2446error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002447 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002448 rcu_string_free(device->name);
Anand Jain32576042015-08-14 18:32:49 +08002449 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002450 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002451error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002452 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002453 if (seeding_dev) {
2454 mutex_unlock(&uuid_mutex);
2455 up_write(&sb->s_umount);
2456 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002457 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002458}
2459
Stefan Behrense93c89c2012-11-05 17:33:06 +01002460int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002461 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002462 struct btrfs_device **device_out)
2463{
2464 struct request_queue *q;
2465 struct btrfs_device *device;
2466 struct block_device *bdev;
2467 struct btrfs_fs_info *fs_info = root->fs_info;
2468 struct list_head *devices;
2469 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002470 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002471 int ret = 0;
2472
2473 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002474 if (fs_info->fs_devices->seeding) {
2475 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002476 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002477 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002478
2479 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2480 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002481 if (IS_ERR(bdev)) {
2482 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002483 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002484 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002485
2486 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2487
2488 devices = &fs_info->fs_devices->devices;
2489 list_for_each_entry(device, devices, dev_list) {
2490 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002491 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002492 ret = -EEXIST;
2493 goto error;
2494 }
2495 }
2496
Miao Xie1c433662014-09-03 21:35:32 +08002497
Miao Xie7cc8e582014-09-03 21:35:38 +08002498 if (i_size_read(bdev->bd_inode) <
2499 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002500 btrfs_err(fs_info, "target device is smaller than source device!");
2501 ret = -EINVAL;
2502 goto error;
2503 }
2504
2505
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002506 device = btrfs_alloc_device(NULL, &devid, NULL);
2507 if (IS_ERR(device)) {
2508 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002509 goto error;
2510 }
2511
2512 name = rcu_string_strdup(device_path, GFP_NOFS);
2513 if (!name) {
2514 kfree(device);
2515 ret = -ENOMEM;
2516 goto error;
2517 }
2518 rcu_assign_pointer(device->name, name);
2519
2520 q = bdev_get_queue(bdev);
2521 if (blk_queue_discard(q))
2522 device->can_discard = 1;
2523 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2524 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002525 device->generation = 0;
2526 device->io_width = root->sectorsize;
2527 device->io_align = root->sectorsize;
2528 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002529 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2530 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2531 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002532 ASSERT(list_empty(&srcdev->resized_list));
2533 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002534 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002535 device->dev_root = fs_info->dev_root;
2536 device->bdev = bdev;
2537 device->in_fs_metadata = 1;
2538 device->is_tgtdev_for_dev_replace = 1;
2539 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002540 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002541 set_blocksize(device->bdev, 4096);
2542 device->fs_devices = fs_info->fs_devices;
2543 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2544 fs_info->fs_devices->num_devices++;
2545 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002546 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2547
2548 *device_out = device;
2549 return ret;
2550
2551error:
2552 blkdev_put(bdev, FMODE_EXCL);
2553 return ret;
2554}
2555
2556void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2557 struct btrfs_device *tgtdev)
2558{
2559 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2560 tgtdev->io_width = fs_info->dev_root->sectorsize;
2561 tgtdev->io_align = fs_info->dev_root->sectorsize;
2562 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2563 tgtdev->dev_root = fs_info->dev_root;
2564 tgtdev->in_fs_metadata = 1;
2565}
2566
Chris Masond3977122009-01-05 21:25:51 -05002567static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2568 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002569{
2570 int ret;
2571 struct btrfs_path *path;
2572 struct btrfs_root *root;
2573 struct btrfs_dev_item *dev_item;
2574 struct extent_buffer *leaf;
2575 struct btrfs_key key;
2576
2577 root = device->dev_root->fs_info->chunk_root;
2578
2579 path = btrfs_alloc_path();
2580 if (!path)
2581 return -ENOMEM;
2582
2583 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2584 key.type = BTRFS_DEV_ITEM_KEY;
2585 key.offset = device->devid;
2586
2587 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2588 if (ret < 0)
2589 goto out;
2590
2591 if (ret > 0) {
2592 ret = -ENOENT;
2593 goto out;
2594 }
2595
2596 leaf = path->nodes[0];
2597 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2598
2599 btrfs_set_device_id(leaf, dev_item, device->devid);
2600 btrfs_set_device_type(leaf, dev_item, device->type);
2601 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2602 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2603 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002604 btrfs_set_device_total_bytes(leaf, dev_item,
2605 btrfs_device_get_disk_total_bytes(device));
2606 btrfs_set_device_bytes_used(leaf, dev_item,
2607 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002608 btrfs_mark_buffer_dirty(leaf);
2609
2610out:
2611 btrfs_free_path(path);
2612 return ret;
2613}
2614
Miao Xie2196d6e2014-09-03 21:35:41 +08002615int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002616 struct btrfs_device *device, u64 new_size)
2617{
2618 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002619 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002620 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002621 u64 old_total;
2622 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002623
Yan Zheng2b820322008-11-17 21:11:30 -05002624 if (!device->writeable)
2625 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002626
2627 lock_chunks(device->dev_root);
2628 old_total = btrfs_super_total_bytes(super_copy);
2629 diff = new_size - device->total_bytes;
2630
Stefan Behrens63a212a2012-11-05 18:29:28 +01002631 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002632 device->is_tgtdev_for_dev_replace) {
2633 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002634 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002635 }
Yan Zheng2b820322008-11-17 21:11:30 -05002636
Miao Xie935e5cc2014-09-03 21:35:33 +08002637 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002638
2639 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002640 device->fs_devices->total_rw_bytes += diff;
2641
Miao Xie7cc8e582014-09-03 21:35:38 +08002642 btrfs_device_set_total_bytes(device, new_size);
2643 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002644 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002645 if (list_empty(&device->resized_list))
2646 list_add_tail(&device->resized_list,
2647 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002648 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002649
Chris Mason8f18cf12008-04-25 16:53:30 -04002650 return btrfs_update_device(trans, device);
2651}
2652
2653static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002654 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002655 u64 chunk_offset)
2656{
2657 int ret;
2658 struct btrfs_path *path;
2659 struct btrfs_key key;
2660
2661 root = root->fs_info->chunk_root;
2662 path = btrfs_alloc_path();
2663 if (!path)
2664 return -ENOMEM;
2665
2666 key.objectid = chunk_objectid;
2667 key.offset = chunk_offset;
2668 key.type = BTRFS_CHUNK_ITEM_KEY;
2669
2670 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002671 if (ret < 0)
2672 goto out;
2673 else if (ret > 0) { /* Logic error or corruption */
Anand Jaina4553fe2015-09-25 14:43:01 +08002674 btrfs_std_error(root->fs_info, -ENOENT,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002675 "Failed lookup while freeing chunk.");
2676 ret = -ENOENT;
2677 goto out;
2678 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002679
2680 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002681 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002682 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 "Failed to delete chunk item.");
2684out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002685 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002686 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002687}
2688
Christoph Hellwigb2950862008-12-02 09:54:17 -05002689static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002690 chunk_offset)
2691{
David Sterba6c417612011-04-13 15:41:04 +02002692 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002693 struct btrfs_disk_key *disk_key;
2694 struct btrfs_chunk *chunk;
2695 u8 *ptr;
2696 int ret = 0;
2697 u32 num_stripes;
2698 u32 array_size;
2699 u32 len = 0;
2700 u32 cur;
2701 struct btrfs_key key;
2702
Miao Xie2196d6e2014-09-03 21:35:41 +08002703 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002704 array_size = btrfs_super_sys_array_size(super_copy);
2705
2706 ptr = super_copy->sys_chunk_array;
2707 cur = 0;
2708
2709 while (cur < array_size) {
2710 disk_key = (struct btrfs_disk_key *)ptr;
2711 btrfs_disk_key_to_cpu(&key, disk_key);
2712
2713 len = sizeof(*disk_key);
2714
2715 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2716 chunk = (struct btrfs_chunk *)(ptr + len);
2717 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2718 len += btrfs_chunk_item_size(num_stripes);
2719 } else {
2720 ret = -EIO;
2721 break;
2722 }
2723 if (key.objectid == chunk_objectid &&
2724 key.offset == chunk_offset) {
2725 memmove(ptr, ptr + len, array_size - (cur + len));
2726 array_size -= len;
2727 btrfs_set_super_sys_array_size(super_copy, array_size);
2728 } else {
2729 ptr += len;
2730 cur += len;
2731 }
2732 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002733 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002734 return ret;
2735}
2736
Josef Bacik47ab2a62014-09-18 11:20:02 -04002737int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2738 struct btrfs_root *root, u64 chunk_offset)
2739{
2740 struct extent_map_tree *em_tree;
2741 struct extent_map *em;
2742 struct btrfs_root *extent_root = root->fs_info->extent_root;
2743 struct map_lookup *map;
2744 u64 dev_extent_len = 0;
2745 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002746 int i, ret = 0;
2747
2748 /* Just in case */
2749 root = root->fs_info->chunk_root;
2750 em_tree = &root->fs_info->mapping_tree.map_tree;
2751
2752 read_lock(&em_tree->lock);
2753 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2754 read_unlock(&em_tree->lock);
2755
2756 if (!em || em->start > chunk_offset ||
2757 em->start + em->len < chunk_offset) {
2758 /*
2759 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002760 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002761 * do anything we still error out.
2762 */
2763 ASSERT(0);
2764 if (em)
2765 free_extent_map(em);
2766 return -EINVAL;
2767 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002768 map = em->map_lookup;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002769 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002770 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002771 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002772
2773 for (i = 0; i < map->num_stripes; i++) {
2774 struct btrfs_device *device = map->stripes[i].dev;
2775 ret = btrfs_free_dev_extent(trans, device,
2776 map->stripes[i].physical,
2777 &dev_extent_len);
2778 if (ret) {
2779 btrfs_abort_transaction(trans, root, ret);
2780 goto out;
2781 }
2782
2783 if (device->bytes_used > 0) {
2784 lock_chunks(root);
2785 btrfs_device_set_bytes_used(device,
2786 device->bytes_used - dev_extent_len);
2787 spin_lock(&root->fs_info->free_chunk_lock);
2788 root->fs_info->free_chunk_space += dev_extent_len;
2789 spin_unlock(&root->fs_info->free_chunk_lock);
2790 btrfs_clear_space_info_full(root->fs_info);
2791 unlock_chunks(root);
2792 }
2793
2794 if (map->stripes[i].dev) {
2795 ret = btrfs_update_device(trans, map->stripes[i].dev);
2796 if (ret) {
2797 btrfs_abort_transaction(trans, root, ret);
2798 goto out;
2799 }
2800 }
2801 }
Zhao Leia688a042015-02-09 20:31:44 +08002802 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002803 if (ret) {
2804 btrfs_abort_transaction(trans, root, ret);
2805 goto out;
2806 }
2807
2808 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2809
2810 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2811 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2812 if (ret) {
2813 btrfs_abort_transaction(trans, root, ret);
2814 goto out;
2815 }
2816 }
2817
Filipe Manana04216822014-11-27 21:14:15 +00002818 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002819 if (ret) {
2820 btrfs_abort_transaction(trans, extent_root, ret);
2821 goto out;
2822 }
2823
Josef Bacik47ab2a62014-09-18 11:20:02 -04002824out:
2825 /* once for us */
2826 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002827 return ret;
2828}
2829
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002830static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002831{
Chris Mason8f18cf12008-04-25 16:53:30 -04002832 struct btrfs_root *extent_root;
2833 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002834 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002835
2836 root = root->fs_info->chunk_root;
2837 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002838
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002839 /*
2840 * Prevent races with automatic removal of unused block groups.
2841 * After we relocate and before we remove the chunk with offset
2842 * chunk_offset, automatic removal of the block group can kick in,
2843 * resulting in a failure when calling btrfs_remove_chunk() below.
2844 *
2845 * Make sure to acquire this mutex before doing a tree search (dev
2846 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2847 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2848 * we release the path used to search the chunk/dev tree and before
2849 * the current task acquires this mutex and calls us.
2850 */
2851 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2852
Josef Bacikba1bf482009-09-11 16:11:19 -04002853 ret = btrfs_can_relocate(extent_root, chunk_offset);
2854 if (ret)
2855 return -ENOSPC;
2856
Chris Mason8f18cf12008-04-25 16:53:30 -04002857 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002858 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002859 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002860 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002861 if (ret)
2862 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002863
Filipe Manana7fd01182015-11-13 23:57:17 +00002864 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2865 chunk_offset);
Liu Bo0f788c52013-03-04 16:25:40 +00002866 if (IS_ERR(trans)) {
2867 ret = PTR_ERR(trans);
Anand Jaina4553fe2015-09-25 14:43:01 +08002868 btrfs_std_error(root->fs_info, ret, NULL);
Liu Bo0f788c52013-03-04 16:25:40 +00002869 return ret;
2870 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002871
2872 /*
2873 * step two, delete the device extents and the
2874 * chunk tree entries
2875 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002876 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002877 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002878 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002879}
2880
Yan Zheng2b820322008-11-17 21:11:30 -05002881static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2882{
2883 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2884 struct btrfs_path *path;
2885 struct extent_buffer *leaf;
2886 struct btrfs_chunk *chunk;
2887 struct btrfs_key key;
2888 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002889 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002890 bool retried = false;
2891 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002892 int ret;
2893
2894 path = btrfs_alloc_path();
2895 if (!path)
2896 return -ENOMEM;
2897
Josef Bacikba1bf482009-09-11 16:11:19 -04002898again:
Yan Zheng2b820322008-11-17 21:11:30 -05002899 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2900 key.offset = (u64)-1;
2901 key.type = BTRFS_CHUNK_ITEM_KEY;
2902
2903 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002904 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002905 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002906 if (ret < 0) {
2907 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002908 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002909 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002910 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002911
2912 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2913 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002914 if (ret)
2915 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002916 if (ret < 0)
2917 goto error;
2918 if (ret > 0)
2919 break;
2920
2921 leaf = path->nodes[0];
2922 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2923
2924 chunk = btrfs_item_ptr(leaf, path->slots[0],
2925 struct btrfs_chunk);
2926 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002927 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002928
2929 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002930 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002931 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002932 if (ret == -ENOSPC)
2933 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302934 else
2935 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002936 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002937 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002938
2939 if (found_key.offset == 0)
2940 break;
2941 key.offset = found_key.offset - 1;
2942 }
2943 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002944 if (failed && !retried) {
2945 failed = 0;
2946 retried = true;
2947 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302948 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002949 ret = -ENOSPC;
2950 }
Yan Zheng2b820322008-11-17 21:11:30 -05002951error:
2952 btrfs_free_path(path);
2953 return ret;
2954}
2955
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002956static int insert_balance_item(struct btrfs_root *root,
2957 struct btrfs_balance_control *bctl)
2958{
2959 struct btrfs_trans_handle *trans;
2960 struct btrfs_balance_item *item;
2961 struct btrfs_disk_balance_args disk_bargs;
2962 struct btrfs_path *path;
2963 struct extent_buffer *leaf;
2964 struct btrfs_key key;
2965 int ret, err;
2966
2967 path = btrfs_alloc_path();
2968 if (!path)
2969 return -ENOMEM;
2970
2971 trans = btrfs_start_transaction(root, 0);
2972 if (IS_ERR(trans)) {
2973 btrfs_free_path(path);
2974 return PTR_ERR(trans);
2975 }
2976
2977 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01002978 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002979 key.offset = 0;
2980
2981 ret = btrfs_insert_empty_item(trans, root, path, &key,
2982 sizeof(*item));
2983 if (ret)
2984 goto out;
2985
2986 leaf = path->nodes[0];
2987 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2988
2989 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2990
2991 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2992 btrfs_set_balance_data(leaf, item, &disk_bargs);
2993 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2994 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2995 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2996 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2997
2998 btrfs_set_balance_flags(leaf, item, bctl->flags);
2999
3000 btrfs_mark_buffer_dirty(leaf);
3001out:
3002 btrfs_free_path(path);
3003 err = btrfs_commit_transaction(trans, root);
3004 if (err && !ret)
3005 ret = err;
3006 return ret;
3007}
3008
3009static int del_balance_item(struct btrfs_root *root)
3010{
3011 struct btrfs_trans_handle *trans;
3012 struct btrfs_path *path;
3013 struct btrfs_key key;
3014 int ret, err;
3015
3016 path = btrfs_alloc_path();
3017 if (!path)
3018 return -ENOMEM;
3019
3020 trans = btrfs_start_transaction(root, 0);
3021 if (IS_ERR(trans)) {
3022 btrfs_free_path(path);
3023 return PTR_ERR(trans);
3024 }
3025
3026 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003027 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003028 key.offset = 0;
3029
3030 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3031 if (ret < 0)
3032 goto out;
3033 if (ret > 0) {
3034 ret = -ENOENT;
3035 goto out;
3036 }
3037
3038 ret = btrfs_del_item(trans, root, path);
3039out:
3040 btrfs_free_path(path);
3041 err = btrfs_commit_transaction(trans, root);
3042 if (err && !ret)
3043 ret = err;
3044 return ret;
3045}
3046
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003047/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003048 * This is a heuristic used to reduce the number of chunks balanced on
3049 * resume after balance was interrupted.
3050 */
3051static void update_balance_args(struct btrfs_balance_control *bctl)
3052{
3053 /*
3054 * Turn on soft mode for chunk types that were being converted.
3055 */
3056 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3057 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3058 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3059 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3060 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3061 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3062
3063 /*
3064 * Turn on usage filter if is not already used. The idea is
3065 * that chunks that we have already balanced should be
3066 * reasonably full. Don't do it for chunks that are being
3067 * converted - that will keep us from relocating unconverted
3068 * (albeit full) chunks.
3069 */
3070 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003071 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003072 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3073 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3074 bctl->data.usage = 90;
3075 }
3076 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003077 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003078 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3079 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3080 bctl->sys.usage = 90;
3081 }
3082 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003083 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003084 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3085 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3086 bctl->meta.usage = 90;
3087 }
3088}
3089
3090/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003091 * Should be called with both balance and volume mutexes held to
3092 * serialize other volume operations (add_dev/rm_dev/resize) with
3093 * restriper. Same goes for unset_balance_control.
3094 */
3095static void set_balance_control(struct btrfs_balance_control *bctl)
3096{
3097 struct btrfs_fs_info *fs_info = bctl->fs_info;
3098
3099 BUG_ON(fs_info->balance_ctl);
3100
3101 spin_lock(&fs_info->balance_lock);
3102 fs_info->balance_ctl = bctl;
3103 spin_unlock(&fs_info->balance_lock);
3104}
3105
3106static void unset_balance_control(struct btrfs_fs_info *fs_info)
3107{
3108 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3109
3110 BUG_ON(!fs_info->balance_ctl);
3111
3112 spin_lock(&fs_info->balance_lock);
3113 fs_info->balance_ctl = NULL;
3114 spin_unlock(&fs_info->balance_lock);
3115
3116 kfree(bctl);
3117}
3118
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003119/*
3120 * Balance filters. Return 1 if chunk should be filtered out
3121 * (should not be balanced).
3122 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003123static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003124 struct btrfs_balance_args *bargs)
3125{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003126 chunk_type = chunk_to_extended(chunk_type) &
3127 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003128
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003129 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003130 return 0;
3131
3132 return 1;
3133}
3134
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003135static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003136 struct btrfs_balance_args *bargs)
3137{
3138 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003139 u64 chunk_used;
3140 u64 user_thresh_min;
3141 u64 user_thresh_max;
3142 int ret = 1;
3143
3144 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3145 chunk_used = btrfs_block_group_used(&cache->item);
3146
3147 if (bargs->usage_min == 0)
3148 user_thresh_min = 0;
3149 else
3150 user_thresh_min = div_factor_fine(cache->key.offset,
3151 bargs->usage_min);
3152
3153 if (bargs->usage_max == 0)
3154 user_thresh_max = 1;
3155 else if (bargs->usage_max > 100)
3156 user_thresh_max = cache->key.offset;
3157 else
3158 user_thresh_max = div_factor_fine(cache->key.offset,
3159 bargs->usage_max);
3160
3161 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3162 ret = 0;
3163
3164 btrfs_put_block_group(cache);
3165 return ret;
3166}
3167
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003168static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003169 u64 chunk_offset, struct btrfs_balance_args *bargs)
3170{
3171 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003172 u64 chunk_used, user_thresh;
3173 int ret = 1;
3174
3175 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3176 chunk_used = btrfs_block_group_used(&cache->item);
3177
David Sterbabc309462015-10-20 18:22:13 +02003178 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003179 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003180 else if (bargs->usage > 100)
3181 user_thresh = cache->key.offset;
3182 else
3183 user_thresh = div_factor_fine(cache->key.offset,
3184 bargs->usage);
3185
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003186 if (chunk_used < user_thresh)
3187 ret = 0;
3188
3189 btrfs_put_block_group(cache);
3190 return ret;
3191}
3192
Ilya Dryomov409d4042012-01-16 22:04:47 +02003193static int chunk_devid_filter(struct extent_buffer *leaf,
3194 struct btrfs_chunk *chunk,
3195 struct btrfs_balance_args *bargs)
3196{
3197 struct btrfs_stripe *stripe;
3198 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3199 int i;
3200
3201 for (i = 0; i < num_stripes; i++) {
3202 stripe = btrfs_stripe_nr(chunk, i);
3203 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3204 return 0;
3205 }
3206
3207 return 1;
3208}
3209
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003210/* [pstart, pend) */
3211static int chunk_drange_filter(struct extent_buffer *leaf,
3212 struct btrfs_chunk *chunk,
3213 u64 chunk_offset,
3214 struct btrfs_balance_args *bargs)
3215{
3216 struct btrfs_stripe *stripe;
3217 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3218 u64 stripe_offset;
3219 u64 stripe_length;
3220 int factor;
3221 int i;
3222
3223 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3224 return 0;
3225
3226 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003227 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3228 factor = num_stripes / 2;
3229 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3230 factor = num_stripes - 1;
3231 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3232 factor = num_stripes - 2;
3233 } else {
3234 factor = num_stripes;
3235 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003236
3237 for (i = 0; i < num_stripes; i++) {
3238 stripe = btrfs_stripe_nr(chunk, i);
3239 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3240 continue;
3241
3242 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3243 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003244 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003245
3246 if (stripe_offset < bargs->pend &&
3247 stripe_offset + stripe_length > bargs->pstart)
3248 return 0;
3249 }
3250
3251 return 1;
3252}
3253
Ilya Dryomovea671762012-01-16 22:04:48 +02003254/* [vstart, vend) */
3255static int chunk_vrange_filter(struct extent_buffer *leaf,
3256 struct btrfs_chunk *chunk,
3257 u64 chunk_offset,
3258 struct btrfs_balance_args *bargs)
3259{
3260 if (chunk_offset < bargs->vend &&
3261 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3262 /* at least part of the chunk is inside this vrange */
3263 return 0;
3264
3265 return 1;
3266}
3267
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003268static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3269 struct btrfs_chunk *chunk,
3270 struct btrfs_balance_args *bargs)
3271{
3272 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3273
3274 if (bargs->stripes_min <= num_stripes
3275 && num_stripes <= bargs->stripes_max)
3276 return 0;
3277
3278 return 1;
3279}
3280
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003281static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003282 struct btrfs_balance_args *bargs)
3283{
3284 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3285 return 0;
3286
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003287 chunk_type = chunk_to_extended(chunk_type) &
3288 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003289
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003290 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003291 return 1;
3292
3293 return 0;
3294}
3295
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003296static int should_balance_chunk(struct btrfs_root *root,
3297 struct extent_buffer *leaf,
3298 struct btrfs_chunk *chunk, u64 chunk_offset)
3299{
3300 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3301 struct btrfs_balance_args *bargs = NULL;
3302 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3303
3304 /* type filter */
3305 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3306 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3307 return 0;
3308 }
3309
3310 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3311 bargs = &bctl->data;
3312 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3313 bargs = &bctl->sys;
3314 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3315 bargs = &bctl->meta;
3316
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003317 /* profiles filter */
3318 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3319 chunk_profiles_filter(chunk_type, bargs)) {
3320 return 0;
3321 }
3322
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003323 /* usage filter */
3324 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3325 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3326 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003327 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3328 chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) {
3329 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003330 }
3331
Ilya Dryomov409d4042012-01-16 22:04:47 +02003332 /* devid filter */
3333 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3334 chunk_devid_filter(leaf, chunk, bargs)) {
3335 return 0;
3336 }
3337
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003338 /* drange filter, makes sense only with devid filter */
3339 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3340 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3341 return 0;
3342 }
3343
Ilya Dryomovea671762012-01-16 22:04:48 +02003344 /* vrange filter */
3345 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3346 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3347 return 0;
3348 }
3349
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003350 /* stripes filter */
3351 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3352 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3353 return 0;
3354 }
3355
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003356 /* soft profile changing mode */
3357 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3358 chunk_soft_convert_filter(chunk_type, bargs)) {
3359 return 0;
3360 }
3361
David Sterba7d824b62014-05-07 17:37:51 +02003362 /*
3363 * limited by count, must be the last filter
3364 */
3365 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3366 if (bargs->limit == 0)
3367 return 0;
3368 else
3369 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003370 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3371 /*
3372 * Same logic as the 'limit' filter; the minimum cannot be
3373 * determined here because we do not have the global informatoin
3374 * about the count of all chunks that satisfy the filters.
3375 */
3376 if (bargs->limit_max == 0)
3377 return 0;
3378 else
3379 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003380 }
3381
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003382 return 1;
3383}
3384
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003385static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003386{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003387 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003388 struct btrfs_root *chunk_root = fs_info->chunk_root;
3389 struct btrfs_root *dev_root = fs_info->dev_root;
3390 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003391 struct btrfs_device *device;
3392 u64 old_size;
3393 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003394 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003395 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003396 struct btrfs_path *path;
3397 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003398 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003399 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003400 struct extent_buffer *leaf;
3401 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003402 int ret;
3403 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003404 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003405 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003406 u64 limit_data = bctl->data.limit;
3407 u64 limit_meta = bctl->meta.limit;
3408 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003409 u32 count_data = 0;
3410 u32 count_meta = 0;
3411 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003412 int chunk_reserved = 0;
Liu Bocf25ce52015-12-14 18:29:32 -08003413 u64 bytes_used = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003414
Chris Masonec44a352008-04-28 15:29:52 -04003415 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003416 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003417 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003418 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003419 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003420 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003421 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003422 btrfs_device_get_total_bytes(device) -
3423 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003424 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003425 continue;
3426
3427 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003428 if (ret == -ENOSPC)
3429 break;
Chris Masonec44a352008-04-28 15:29:52 -04003430 BUG_ON(ret);
3431
Yan, Zhenga22285a2010-05-16 10:48:46 -04003432 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003433 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003434
3435 ret = btrfs_grow_device(trans, device, old_size);
3436 BUG_ON(ret);
3437
3438 btrfs_end_transaction(trans, dev_root);
3439 }
3440
3441 /* step two, relocate all the chunks */
3442 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003443 if (!path) {
3444 ret = -ENOMEM;
3445 goto error;
3446 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003447
3448 /* zero out stat counters */
3449 spin_lock(&fs_info->balance_lock);
3450 memset(&bctl->stat, 0, sizeof(bctl->stat));
3451 spin_unlock(&fs_info->balance_lock);
3452again:
David Sterba7d824b62014-05-07 17:37:51 +02003453 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003454 /*
3455 * The single value limit and min/max limits use the same bytes
3456 * in the
3457 */
David Sterba7d824b62014-05-07 17:37:51 +02003458 bctl->data.limit = limit_data;
3459 bctl->meta.limit = limit_meta;
3460 bctl->sys.limit = limit_sys;
3461 }
Chris Masonec44a352008-04-28 15:29:52 -04003462 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3463 key.offset = (u64)-1;
3464 key.type = BTRFS_CHUNK_ITEM_KEY;
3465
Chris Masond3977122009-01-05 21:25:51 -05003466 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003467 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003468 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003469 ret = -ECANCELED;
3470 goto error;
3471 }
3472
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003473 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003474 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003475 if (ret < 0) {
3476 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003477 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003478 }
Chris Masonec44a352008-04-28 15:29:52 -04003479
3480 /*
3481 * this shouldn't happen, it means the last relocate
3482 * failed
3483 */
3484 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003485 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003486
3487 ret = btrfs_previous_item(chunk_root, path, 0,
3488 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003489 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003490 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003491 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003492 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003493 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003494
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003495 leaf = path->nodes[0];
3496 slot = path->slots[0];
3497 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3498
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003499 if (found_key.objectid != key.objectid) {
3500 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003501 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003502 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003503
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003504 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003505 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003506
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003507 if (!counting) {
3508 spin_lock(&fs_info->balance_lock);
3509 bctl->stat.considered++;
3510 spin_unlock(&fs_info->balance_lock);
3511 }
3512
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003513 ret = should_balance_chunk(chunk_root, leaf, chunk,
3514 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003515
David Sterbab3b4aa72011-04-21 01:20:15 +02003516 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003517 if (!ret) {
3518 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003519 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003520 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003521
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003522 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003523 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003524 spin_lock(&fs_info->balance_lock);
3525 bctl->stat.expected++;
3526 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003527
3528 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3529 count_data++;
3530 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3531 count_sys++;
3532 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3533 count_meta++;
3534
3535 goto loop;
3536 }
3537
3538 /*
3539 * Apply limit_min filter, no need to check if the LIMITS
3540 * filter is used, limit_min is 0 by default
3541 */
3542 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3543 count_data < bctl->data.limit_min)
3544 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3545 count_meta < bctl->meta.limit_min)
3546 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3547 count_sys < bctl->sys.limit_min)) {
3548 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003549 goto loop;
3550 }
3551
Liu Bocf25ce52015-12-14 18:29:32 -08003552 ASSERT(fs_info->data_sinfo);
3553 spin_lock(&fs_info->data_sinfo->lock);
3554 bytes_used = fs_info->data_sinfo->bytes_used;
3555 spin_unlock(&fs_info->data_sinfo->lock);
3556
3557 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3558 !chunk_reserved && !bytes_used) {
Zhao Lei2c9fe832015-11-09 11:51:32 +08003559 trans = btrfs_start_transaction(chunk_root, 0);
3560 if (IS_ERR(trans)) {
3561 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3562 ret = PTR_ERR(trans);
3563 goto error;
3564 }
3565
3566 ret = btrfs_force_chunk_alloc(trans, chunk_root,
3567 BTRFS_BLOCK_GROUP_DATA);
Filipe Manana8a7d6562015-12-10 11:08:59 +00003568 btrfs_end_transaction(trans, chunk_root);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003569 if (ret < 0) {
3570 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3571 goto error;
3572 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003573 chunk_reserved = 1;
3574 }
3575
Chris Masonec44a352008-04-28 15:29:52 -04003576 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003577 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003578 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003579 if (ret && ret != -ENOSPC)
3580 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003581 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003582 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003583 } else {
3584 spin_lock(&fs_info->balance_lock);
3585 bctl->stat.completed++;
3586 spin_unlock(&fs_info->balance_lock);
3587 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003588loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003589 if (found_key.offset == 0)
3590 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003591 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003592 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003593
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003594 if (counting) {
3595 btrfs_release_path(path);
3596 counting = false;
3597 goto again;
3598 }
Chris Masonec44a352008-04-28 15:29:52 -04003599error:
3600 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003601 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003602 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003603 enospc_errors);
3604 if (!ret)
3605 ret = -ENOSPC;
3606 }
3607
Chris Masonec44a352008-04-28 15:29:52 -04003608 return ret;
3609}
3610
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003611/**
3612 * alloc_profile_is_valid - see if a given profile is valid and reduced
3613 * @flags: profile to validate
3614 * @extended: if true @flags is treated as an extended profile
3615 */
3616static int alloc_profile_is_valid(u64 flags, int extended)
3617{
3618 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3619 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3620
3621 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3622
3623 /* 1) check that all other bits are zeroed */
3624 if (flags & ~mask)
3625 return 0;
3626
3627 /* 2) see if profile is reduced */
3628 if (flags == 0)
3629 return !extended; /* "0" is valid for usual profiles */
3630
3631 /* true if exactly one bit set */
3632 return (flags & (flags - 1)) == 0;
3633}
3634
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003635static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3636{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003637 /* cancel requested || normal exit path */
3638 return atomic_read(&fs_info->balance_cancel_req) ||
3639 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3640 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003641}
3642
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003643static void __cancel_balance(struct btrfs_fs_info *fs_info)
3644{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003645 int ret;
3646
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003647 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003648 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003649 if (ret)
Anand Jaina4553fe2015-09-25 14:43:01 +08003650 btrfs_std_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003651
3652 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003653}
3654
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003655/* Non-zero return value signifies invalidity */
3656static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3657 u64 allowed)
3658{
3659 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3660 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3661 (bctl_arg->target & ~allowed)));
3662}
3663
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003664/*
3665 * Should be called with both balance and volume mutexes held
3666 */
3667int btrfs_balance(struct btrfs_balance_control *bctl,
3668 struct btrfs_ioctl_balance_args *bargs)
3669{
3670 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003671 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003672 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003673 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003674 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003675 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003676
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003677 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003678 atomic_read(&fs_info->balance_pause_req) ||
3679 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003680 ret = -EINVAL;
3681 goto out;
3682 }
3683
Ilya Dryomove4837f82012-03-27 17:09:17 +03003684 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3685 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3686 mixed = 1;
3687
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003688 /*
3689 * In case of mixed groups both data and meta should be picked,
3690 * and identical options should be given for both of them.
3691 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003692 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3693 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003694 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3695 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3696 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003697 btrfs_err(fs_info, "with mixed groups data and "
3698 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003699 ret = -EINVAL;
3700 goto out;
3701 }
3702 }
3703
Stefan Behrens8dabb742012-11-06 13:15:27 +01003704 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003705 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003706 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3707 BUG_ON(num_devices < 1);
3708 num_devices--;
3709 }
Liu Bo73beece2015-07-17 16:49:19 +08003710 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Austin S. Hemmelgarn88be1592016-03-23 14:22:59 -04003711 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE | BTRFS_BLOCK_GROUP_DUP;
3712 if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003713 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003714 if (num_devices > 2)
3715 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3716 if (num_devices > 3)
3717 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3718 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003719 if (validate_convert_profile(&bctl->data, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003720 btrfs_err(fs_info, "unable to start balance with target "
3721 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003722 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003723 ret = -EINVAL;
3724 goto out;
3725 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003726 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003727 btrfs_err(fs_info,
3728 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003729 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003730 ret = -EINVAL;
3731 goto out;
3732 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003733 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003734 btrfs_err(fs_info,
3735 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003736 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003737 ret = -EINVAL;
3738 goto out;
3739 }
3740
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003741 /* allow to reduce meta or sys integrity only if force set */
3742 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003743 BTRFS_BLOCK_GROUP_RAID10 |
3744 BTRFS_BLOCK_GROUP_RAID5 |
3745 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003746 do {
3747 seq = read_seqbegin(&fs_info->profiles_lock);
3748
3749 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3750 (fs_info->avail_system_alloc_bits & allowed) &&
3751 !(bctl->sys.target & allowed)) ||
3752 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3753 (fs_info->avail_metadata_alloc_bits & allowed) &&
3754 !(bctl->meta.target & allowed))) {
3755 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003756 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003757 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003758 btrfs_err(fs_info, "balance will reduce metadata "
3759 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003760 ret = -EINVAL;
3761 goto out;
3762 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003763 }
Miao Xiede98ced2013-01-29 10:13:12 +00003764 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003765
Sam Tygieree592d02016-01-06 08:46:12 +00003766 if (btrfs_get_num_tolerated_disk_barrier_failures(bctl->meta.target) <
3767 btrfs_get_num_tolerated_disk_barrier_failures(bctl->data.target)) {
3768 btrfs_warn(fs_info,
Filipe Mananafedc0042016-01-15 10:56:15 +00003769 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Sam Tygieree592d02016-01-06 08:46:12 +00003770 bctl->meta.target, bctl->data.target);
3771 }
3772
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003773 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003774 fs_info->num_tolerated_disk_barrier_failures = min(
3775 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3776 btrfs_get_num_tolerated_disk_barrier_failures(
3777 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003778 }
3779
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003780 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003781 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003782 goto out;
3783
Ilya Dryomov59641012012-01-16 22:04:48 +02003784 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3785 BUG_ON(ret == -EEXIST);
3786 set_balance_control(bctl);
3787 } else {
3788 BUG_ON(ret != -EEXIST);
3789 spin_lock(&fs_info->balance_lock);
3790 update_balance_args(bctl);
3791 spin_unlock(&fs_info->balance_lock);
3792 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003793
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003794 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003795 mutex_unlock(&fs_info->balance_mutex);
3796
3797 ret = __btrfs_balance(fs_info);
3798
3799 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003800 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003801
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003802 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3803 fs_info->num_tolerated_disk_barrier_failures =
3804 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3805 }
3806
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003807 if (bargs) {
3808 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003809 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003810 }
3811
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003812 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3813 balance_need_close(fs_info)) {
3814 __cancel_balance(fs_info);
3815 }
3816
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003817 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003818
3819 return ret;
3820out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003821 if (bctl->flags & BTRFS_BALANCE_RESUME)
3822 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003823 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003824 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003825 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3826 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003827 return ret;
3828}
3829
3830static int balance_kthread(void *data)
3831{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003832 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003833 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003834
3835 mutex_lock(&fs_info->volume_mutex);
3836 mutex_lock(&fs_info->balance_mutex);
3837
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003838 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003839 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003840 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003841 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003842
3843 mutex_unlock(&fs_info->balance_mutex);
3844 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003845
Ilya Dryomov59641012012-01-16 22:04:48 +02003846 return ret;
3847}
3848
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003849int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3850{
3851 struct task_struct *tsk;
3852
3853 spin_lock(&fs_info->balance_lock);
3854 if (!fs_info->balance_ctl) {
3855 spin_unlock(&fs_info->balance_lock);
3856 return 0;
3857 }
3858 spin_unlock(&fs_info->balance_lock);
3859
3860 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003861 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003862 return 0;
3863 }
3864
3865 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303866 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003867}
3868
Ilya Dryomov68310a52012-06-22 12:24:12 -06003869int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003870{
Ilya Dryomov59641012012-01-16 22:04:48 +02003871 struct btrfs_balance_control *bctl;
3872 struct btrfs_balance_item *item;
3873 struct btrfs_disk_balance_args disk_bargs;
3874 struct btrfs_path *path;
3875 struct extent_buffer *leaf;
3876 struct btrfs_key key;
3877 int ret;
3878
3879 path = btrfs_alloc_path();
3880 if (!path)
3881 return -ENOMEM;
3882
Ilya Dryomov68310a52012-06-22 12:24:12 -06003883 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003884 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003885 key.offset = 0;
3886
3887 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3888 if (ret < 0)
3889 goto out;
3890 if (ret > 0) { /* ret = -ENOENT; */
3891 ret = 0;
3892 goto out;
3893 }
3894
Ilya Dryomov59641012012-01-16 22:04:48 +02003895 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3896 if (!bctl) {
3897 ret = -ENOMEM;
3898 goto out;
3899 }
3900
Ilya Dryomov59641012012-01-16 22:04:48 +02003901 leaf = path->nodes[0];
3902 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3903
Ilya Dryomov68310a52012-06-22 12:24:12 -06003904 bctl->fs_info = fs_info;
3905 bctl->flags = btrfs_balance_flags(leaf, item);
3906 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003907
3908 btrfs_balance_data(leaf, item, &disk_bargs);
3909 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3910 btrfs_balance_meta(leaf, item, &disk_bargs);
3911 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3912 btrfs_balance_sys(leaf, item, &disk_bargs);
3913 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3914
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003915 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3916
Ilya Dryomov68310a52012-06-22 12:24:12 -06003917 mutex_lock(&fs_info->volume_mutex);
3918 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003919
Ilya Dryomov68310a52012-06-22 12:24:12 -06003920 set_balance_control(bctl);
3921
3922 mutex_unlock(&fs_info->balance_mutex);
3923 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003924out:
3925 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003926 return ret;
3927}
3928
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003929int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3930{
3931 int ret = 0;
3932
3933 mutex_lock(&fs_info->balance_mutex);
3934 if (!fs_info->balance_ctl) {
3935 mutex_unlock(&fs_info->balance_mutex);
3936 return -ENOTCONN;
3937 }
3938
3939 if (atomic_read(&fs_info->balance_running)) {
3940 atomic_inc(&fs_info->balance_pause_req);
3941 mutex_unlock(&fs_info->balance_mutex);
3942
3943 wait_event(fs_info->balance_wait_q,
3944 atomic_read(&fs_info->balance_running) == 0);
3945
3946 mutex_lock(&fs_info->balance_mutex);
3947 /* we are good with balance_ctl ripped off from under us */
3948 BUG_ON(atomic_read(&fs_info->balance_running));
3949 atomic_dec(&fs_info->balance_pause_req);
3950 } else {
3951 ret = -ENOTCONN;
3952 }
3953
3954 mutex_unlock(&fs_info->balance_mutex);
3955 return ret;
3956}
3957
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003958int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3959{
Ilya Dryomove649e582013-10-10 20:40:21 +03003960 if (fs_info->sb->s_flags & MS_RDONLY)
3961 return -EROFS;
3962
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003963 mutex_lock(&fs_info->balance_mutex);
3964 if (!fs_info->balance_ctl) {
3965 mutex_unlock(&fs_info->balance_mutex);
3966 return -ENOTCONN;
3967 }
3968
3969 atomic_inc(&fs_info->balance_cancel_req);
3970 /*
3971 * if we are running just wait and return, balance item is
3972 * deleted in btrfs_balance in this case
3973 */
3974 if (atomic_read(&fs_info->balance_running)) {
3975 mutex_unlock(&fs_info->balance_mutex);
3976 wait_event(fs_info->balance_wait_q,
3977 atomic_read(&fs_info->balance_running) == 0);
3978 mutex_lock(&fs_info->balance_mutex);
3979 } else {
3980 /* __cancel_balance needs volume_mutex */
3981 mutex_unlock(&fs_info->balance_mutex);
3982 mutex_lock(&fs_info->volume_mutex);
3983 mutex_lock(&fs_info->balance_mutex);
3984
3985 if (fs_info->balance_ctl)
3986 __cancel_balance(fs_info);
3987
3988 mutex_unlock(&fs_info->volume_mutex);
3989 }
3990
3991 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3992 atomic_dec(&fs_info->balance_cancel_req);
3993 mutex_unlock(&fs_info->balance_mutex);
3994 return 0;
3995}
3996
Stefan Behrens803b2f52013-08-15 17:11:21 +02003997static int btrfs_uuid_scan_kthread(void *data)
3998{
3999 struct btrfs_fs_info *fs_info = data;
4000 struct btrfs_root *root = fs_info->tree_root;
4001 struct btrfs_key key;
4002 struct btrfs_key max_key;
4003 struct btrfs_path *path = NULL;
4004 int ret = 0;
4005 struct extent_buffer *eb;
4006 int slot;
4007 struct btrfs_root_item root_item;
4008 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004009 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004010
4011 path = btrfs_alloc_path();
4012 if (!path) {
4013 ret = -ENOMEM;
4014 goto out;
4015 }
4016
4017 key.objectid = 0;
4018 key.type = BTRFS_ROOT_ITEM_KEY;
4019 key.offset = 0;
4020
4021 max_key.objectid = (u64)-1;
4022 max_key.type = BTRFS_ROOT_ITEM_KEY;
4023 max_key.offset = (u64)-1;
4024
Stefan Behrens803b2f52013-08-15 17:11:21 +02004025 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004026 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004027 if (ret) {
4028 if (ret > 0)
4029 ret = 0;
4030 break;
4031 }
4032
4033 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4034 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4035 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4036 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4037 goto skip;
4038
4039 eb = path->nodes[0];
4040 slot = path->slots[0];
4041 item_size = btrfs_item_size_nr(eb, slot);
4042 if (item_size < sizeof(root_item))
4043 goto skip;
4044
Stefan Behrens803b2f52013-08-15 17:11:21 +02004045 read_extent_buffer(eb, &root_item,
4046 btrfs_item_ptr_offset(eb, slot),
4047 (int)sizeof(root_item));
4048 if (btrfs_root_refs(&root_item) == 0)
4049 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004050
4051 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4052 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4053 if (trans)
4054 goto update_tree;
4055
4056 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004057 /*
4058 * 1 - subvol uuid item
4059 * 1 - received_subvol uuid item
4060 */
4061 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4062 if (IS_ERR(trans)) {
4063 ret = PTR_ERR(trans);
4064 break;
4065 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004066 continue;
4067 } else {
4068 goto skip;
4069 }
4070update_tree:
4071 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004072 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4073 root_item.uuid,
4074 BTRFS_UUID_KEY_SUBVOL,
4075 key.objectid);
4076 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004077 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004078 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004079 break;
4080 }
4081 }
4082
4083 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004084 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4085 root_item.received_uuid,
4086 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4087 key.objectid);
4088 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004089 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004090 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004091 break;
4092 }
4093 }
4094
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004095skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004096 if (trans) {
4097 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004098 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004099 if (ret)
4100 break;
4101 }
4102
Stefan Behrens803b2f52013-08-15 17:11:21 +02004103 btrfs_release_path(path);
4104 if (key.offset < (u64)-1) {
4105 key.offset++;
4106 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4107 key.offset = 0;
4108 key.type = BTRFS_ROOT_ITEM_KEY;
4109 } else if (key.objectid < (u64)-1) {
4110 key.offset = 0;
4111 key.type = BTRFS_ROOT_ITEM_KEY;
4112 key.objectid++;
4113 } else {
4114 break;
4115 }
4116 cond_resched();
4117 }
4118
4119out:
4120 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004121 if (trans && !IS_ERR(trans))
4122 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004123 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004124 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004125 else
4126 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004127 up(&fs_info->uuid_tree_rescan_sem);
4128 return 0;
4129}
4130
Stefan Behrens70f80172013-08-15 17:11:23 +02004131/*
4132 * Callback for btrfs_uuid_tree_iterate().
4133 * returns:
4134 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004135 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004136 * > 0 if the check failed, which means the caller shall remove the entry.
4137 */
4138static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4139 u8 *uuid, u8 type, u64 subid)
4140{
4141 struct btrfs_key key;
4142 int ret = 0;
4143 struct btrfs_root *subvol_root;
4144
4145 if (type != BTRFS_UUID_KEY_SUBVOL &&
4146 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4147 goto out;
4148
4149 key.objectid = subid;
4150 key.type = BTRFS_ROOT_ITEM_KEY;
4151 key.offset = (u64)-1;
4152 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4153 if (IS_ERR(subvol_root)) {
4154 ret = PTR_ERR(subvol_root);
4155 if (ret == -ENOENT)
4156 ret = 1;
4157 goto out;
4158 }
4159
4160 switch (type) {
4161 case BTRFS_UUID_KEY_SUBVOL:
4162 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4163 ret = 1;
4164 break;
4165 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4166 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4167 BTRFS_UUID_SIZE))
4168 ret = 1;
4169 break;
4170 }
4171
4172out:
4173 return ret;
4174}
4175
4176static int btrfs_uuid_rescan_kthread(void *data)
4177{
4178 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4179 int ret;
4180
4181 /*
4182 * 1st step is to iterate through the existing UUID tree and
4183 * to delete all entries that contain outdated data.
4184 * 2nd step is to add all missing entries to the UUID tree.
4185 */
4186 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4187 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004188 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004189 up(&fs_info->uuid_tree_rescan_sem);
4190 return ret;
4191 }
4192 return btrfs_uuid_scan_kthread(data);
4193}
4194
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004195int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4196{
4197 struct btrfs_trans_handle *trans;
4198 struct btrfs_root *tree_root = fs_info->tree_root;
4199 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004200 struct task_struct *task;
4201 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004202
4203 /*
4204 * 1 - root node
4205 * 1 - root item
4206 */
4207 trans = btrfs_start_transaction(tree_root, 2);
4208 if (IS_ERR(trans))
4209 return PTR_ERR(trans);
4210
4211 uuid_root = btrfs_create_tree(trans, fs_info,
4212 BTRFS_UUID_TREE_OBJECTID);
4213 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004214 ret = PTR_ERR(uuid_root);
4215 btrfs_abort_transaction(trans, tree_root, ret);
4216 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004217 }
4218
4219 fs_info->uuid_root = uuid_root;
4220
Stefan Behrens803b2f52013-08-15 17:11:21 +02004221 ret = btrfs_commit_transaction(trans, tree_root);
4222 if (ret)
4223 return ret;
4224
4225 down(&fs_info->uuid_tree_rescan_sem);
4226 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4227 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004228 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004229 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004230 up(&fs_info->uuid_tree_rescan_sem);
4231 return PTR_ERR(task);
4232 }
4233
4234 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004235}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004236
Stefan Behrens70f80172013-08-15 17:11:23 +02004237int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4238{
4239 struct task_struct *task;
4240
4241 down(&fs_info->uuid_tree_rescan_sem);
4242 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4243 if (IS_ERR(task)) {
4244 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004245 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004246 up(&fs_info->uuid_tree_rescan_sem);
4247 return PTR_ERR(task);
4248 }
4249
4250 return 0;
4251}
4252
Chris Mason8f18cf12008-04-25 16:53:30 -04004253/*
4254 * shrinking a device means finding all of the device extents past
4255 * the new size, and then following the back refs to the chunks.
4256 * The chunk relocation code actually frees the device extent
4257 */
4258int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4259{
4260 struct btrfs_trans_handle *trans;
4261 struct btrfs_root *root = device->dev_root;
4262 struct btrfs_dev_extent *dev_extent = NULL;
4263 struct btrfs_path *path;
4264 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004265 u64 chunk_offset;
4266 int ret;
4267 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004268 int failed = 0;
4269 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004270 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004271 struct extent_buffer *l;
4272 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004273 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004274 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004275 u64 old_size = btrfs_device_get_total_bytes(device);
4276 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004277
Stefan Behrens63a212a2012-11-05 18:29:28 +01004278 if (device->is_tgtdev_for_dev_replace)
4279 return -EINVAL;
4280
Chris Mason8f18cf12008-04-25 16:53:30 -04004281 path = btrfs_alloc_path();
4282 if (!path)
4283 return -ENOMEM;
4284
David Sterbae4058b52015-11-27 16:31:35 +01004285 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004286
Chris Mason7d9eb122008-07-08 14:19:17 -04004287 lock_chunks(root);
4288
Miao Xie7cc8e582014-09-03 21:35:38 +08004289 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004290 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004291 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004292 spin_lock(&root->fs_info->free_chunk_lock);
4293 root->fs_info->free_chunk_space -= diff;
4294 spin_unlock(&root->fs_info->free_chunk_lock);
4295 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004296 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004297
Josef Bacikba1bf482009-09-11 16:11:19 -04004298again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004299 key.objectid = device->devid;
4300 key.offset = (u64)-1;
4301 key.type = BTRFS_DEV_EXTENT_KEY;
4302
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004303 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004304 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004305 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004306 if (ret < 0) {
4307 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004308 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004309 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004310
4311 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004312 if (ret)
4313 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004314 if (ret < 0)
4315 goto done;
4316 if (ret) {
4317 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004318 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004319 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004320 }
4321
4322 l = path->nodes[0];
4323 slot = path->slots[0];
4324 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4325
Josef Bacikba1bf482009-09-11 16:11:19 -04004326 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004327 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004328 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004329 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004330 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004331
4332 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4333 length = btrfs_dev_extent_length(l, dev_extent);
4334
Josef Bacikba1bf482009-09-11 16:11:19 -04004335 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004336 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004337 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004338 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004339 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004340
Chris Mason8f18cf12008-04-25 16:53:30 -04004341 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004342 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004343
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004344 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004345 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004346 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004347 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004348 if (ret == -ENOSPC)
4349 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004350 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004351
4352 if (failed && !retried) {
4353 failed = 0;
4354 retried = true;
4355 goto again;
4356 } else if (failed && retried) {
4357 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004358 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004359 }
4360
Chris Balld6397ba2009-04-27 07:29:03 -04004361 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004362 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004363 if (IS_ERR(trans)) {
4364 ret = PTR_ERR(trans);
4365 goto done;
4366 }
4367
Chris Balld6397ba2009-04-27 07:29:03 -04004368 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004369
4370 /*
4371 * We checked in the above loop all device extents that were already in
4372 * the device tree. However before we have updated the device's
4373 * total_bytes to the new size, we might have had chunk allocations that
4374 * have not complete yet (new block groups attached to transaction
4375 * handles), and therefore their device extents were not yet in the
4376 * device tree and we missed them in the loop above. So if we have any
4377 * pending chunk using a device extent that overlaps the device range
4378 * that we can not use anymore, commit the current transaction and
4379 * repeat the search on the device tree - this way we guarantee we will
4380 * not have chunks using device extents that end beyond 'new_size'.
4381 */
4382 if (!checked_pending_chunks) {
4383 u64 start = new_size;
4384 u64 len = old_size - new_size;
4385
Jeff Mahoney499f3772015-06-15 09:41:17 -04004386 if (contains_pending_extent(trans->transaction, device,
4387 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004388 unlock_chunks(root);
4389 checked_pending_chunks = true;
4390 failed = 0;
4391 retried = false;
4392 ret = btrfs_commit_transaction(trans, root);
4393 if (ret)
4394 goto done;
4395 goto again;
4396 }
4397 }
4398
Miao Xie7cc8e582014-09-03 21:35:38 +08004399 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004400 if (list_empty(&device->resized_list))
4401 list_add_tail(&device->resized_list,
4402 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004403
Chris Balld6397ba2009-04-27 07:29:03 -04004404 WARN_ON(diff > old_total);
4405 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4406 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004407
4408 /* Now btrfs_update_device() will change the on-disk size. */
4409 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004410 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004411done:
4412 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004413 if (ret) {
4414 lock_chunks(root);
4415 btrfs_device_set_total_bytes(device, old_size);
4416 if (device->writeable)
4417 device->fs_devices->total_rw_bytes += diff;
4418 spin_lock(&root->fs_info->free_chunk_lock);
4419 root->fs_info->free_chunk_space += diff;
4420 spin_unlock(&root->fs_info->free_chunk_lock);
4421 unlock_chunks(root);
4422 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004423 return ret;
4424}
4425
Li Zefan125ccb02011-12-08 15:07:24 +08004426static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004427 struct btrfs_key *key,
4428 struct btrfs_chunk *chunk, int item_size)
4429{
David Sterba6c417612011-04-13 15:41:04 +02004430 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004431 struct btrfs_disk_key disk_key;
4432 u32 array_size;
4433 u8 *ptr;
4434
Miao Xiefe48a5c2014-09-03 21:35:39 +08004435 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004436 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004437 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004438 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4439 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004440 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004441 }
Chris Mason0b86a832008-03-24 15:01:56 -04004442
4443 ptr = super_copy->sys_chunk_array + array_size;
4444 btrfs_cpu_key_to_disk(&disk_key, key);
4445 memcpy(ptr, &disk_key, sizeof(disk_key));
4446 ptr += sizeof(disk_key);
4447 memcpy(ptr, chunk, item_size);
4448 item_size += sizeof(disk_key);
4449 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004450 unlock_chunks(root);
4451
Chris Mason0b86a832008-03-24 15:01:56 -04004452 return 0;
4453}
4454
Miao Xieb2117a32011-01-05 10:07:28 +00004455/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004456 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004457 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004458static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004459{
Arne Jansen73c5de02011-04-12 12:07:57 +02004460 const struct btrfs_device_info *di_a = a;
4461 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004462
Arne Jansen73c5de02011-04-12 12:07:57 +02004463 if (di_a->max_avail > di_b->max_avail)
4464 return -1;
4465 if (di_a->max_avail < di_b->max_avail)
4466 return 1;
4467 if (di_a->total_avail > di_b->total_avail)
4468 return -1;
4469 if (di_a->total_avail < di_b->total_avail)
4470 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004471 return 0;
4472}
4473
David Woodhouse53b381b2013-01-29 18:40:14 -05004474static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4475{
4476 /* TODO allow them to set a preferred stripe size */
Byongho Leeee221842015-12-15 01:42:10 +09004477 return SZ_64K;
David Woodhouse53b381b2013-01-29 18:40:14 -05004478}
4479
4480static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4481{
Zhao Leiffe2d202015-01-20 15:11:44 +08004482 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004483 return;
4484
Miao Xieceda0862013-04-11 10:30:16 +00004485 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004486}
4487
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004488#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4489 - sizeof(struct btrfs_item) \
4490 - sizeof(struct btrfs_chunk)) \
4491 / sizeof(struct btrfs_stripe) + 1)
4492
4493#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4494 - 2 * sizeof(struct btrfs_disk_key) \
4495 - 2 * sizeof(struct btrfs_chunk)) \
4496 / sizeof(struct btrfs_stripe) + 1)
4497
Miao Xieb2117a32011-01-05 10:07:28 +00004498static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004499 struct btrfs_root *extent_root, u64 start,
4500 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004501{
4502 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004503 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4504 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004505 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004506 struct extent_map_tree *em_tree;
4507 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004508 struct btrfs_device_info *devices_info = NULL;
4509 u64 total_avail;
4510 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004511 int data_stripes; /* number of stripes that count for
4512 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004513 int sub_stripes; /* sub_stripes info for map */
4514 int dev_stripes; /* stripes per dev */
4515 int devs_max; /* max devs to use */
4516 int devs_min; /* min devs needed */
4517 int devs_increment; /* ndevs has to be a multiple of this */
4518 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004519 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004520 u64 max_stripe_size;
4521 u64 max_chunk_size;
4522 u64 stripe_size;
4523 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004524 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004525 int ndevs;
4526 int i;
4527 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004528 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004529
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004530 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004531
Miao Xieb2117a32011-01-05 10:07:28 +00004532 if (list_empty(&fs_devices->alloc_list))
4533 return -ENOSPC;
4534
Liu Bo31e50222012-11-21 14:18:10 +00004535 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004536
Liu Bo31e50222012-11-21 14:18:10 +00004537 sub_stripes = btrfs_raid_array[index].sub_stripes;
4538 dev_stripes = btrfs_raid_array[index].dev_stripes;
4539 devs_max = btrfs_raid_array[index].devs_max;
4540 devs_min = btrfs_raid_array[index].devs_min;
4541 devs_increment = btrfs_raid_array[index].devs_increment;
4542 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004543
4544 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004545 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004546 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004547 if (!devs_max)
4548 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004549 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004550 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004551 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4552 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004553 else
Byongho Leeee221842015-12-15 01:42:10 +09004554 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004555 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004556 if (!devs_max)
4557 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004558 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004559 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004560 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004561 if (!devs_max)
4562 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004563 } else {
David Sterba351fd352014-05-15 16:48:20 +02004564 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004565 type);
4566 BUG_ON(1);
4567 }
4568
4569 /* we don't want a chunk larger than 10% of writeable space */
4570 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4571 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004572
David Sterba31e818f2015-02-20 18:00:26 +01004573 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004574 GFP_NOFS);
4575 if (!devices_info)
4576 return -ENOMEM;
4577
Arne Jansen73c5de02011-04-12 12:07:57 +02004578 cur = fs_devices->alloc_list.next;
4579
4580 /*
4581 * in the first pass through the devices list, we gather information
4582 * about the available holes on each device.
4583 */
4584 ndevs = 0;
4585 while (cur != &fs_devices->alloc_list) {
4586 struct btrfs_device *device;
4587 u64 max_avail;
4588 u64 dev_offset;
4589
4590 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4591
4592 cur = cur->next;
4593
4594 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004595 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004596 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004597 continue;
4598 }
4599
Stefan Behrens63a212a2012-11-05 18:29:28 +01004600 if (!device->in_fs_metadata ||
4601 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004602 continue;
4603
4604 if (device->total_bytes > device->bytes_used)
4605 total_avail = device->total_bytes - device->bytes_used;
4606 else
4607 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004608
4609 /* If there is no space on this device, skip it. */
4610 if (total_avail == 0)
4611 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004612
Josef Bacik6df9a952013-06-27 13:22:46 -04004613 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004614 max_stripe_size * dev_stripes,
4615 &dev_offset, &max_avail);
4616 if (ret && ret != -ENOSPC)
4617 goto error;
4618
4619 if (ret == 0)
4620 max_avail = max_stripe_size * dev_stripes;
4621
4622 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4623 continue;
4624
Eric Sandeen063d0062013-01-31 00:55:01 +00004625 if (ndevs == fs_devices->rw_devices) {
4626 WARN(1, "%s: found more than %llu devices\n",
4627 __func__, fs_devices->rw_devices);
4628 break;
4629 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004630 devices_info[ndevs].dev_offset = dev_offset;
4631 devices_info[ndevs].max_avail = max_avail;
4632 devices_info[ndevs].total_avail = total_avail;
4633 devices_info[ndevs].dev = device;
4634 ++ndevs;
4635 }
4636
4637 /*
4638 * now sort the devices by hole size / available space
4639 */
4640 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4641 btrfs_cmp_device_info, NULL);
4642
4643 /* round down to number of usable stripes */
4644 ndevs -= ndevs % devs_increment;
4645
4646 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4647 ret = -ENOSPC;
4648 goto error;
4649 }
4650
4651 if (devs_max && ndevs > devs_max)
4652 ndevs = devs_max;
4653 /*
4654 * the primary goal is to maximize the number of stripes, so use as many
4655 * devices as possible, even if the stripes are not maximum sized.
4656 */
4657 stripe_size = devices_info[ndevs-1].max_avail;
4658 num_stripes = ndevs * dev_stripes;
4659
David Woodhouse53b381b2013-01-29 18:40:14 -05004660 /*
4661 * this will have to be fixed for RAID1 and RAID10 over
4662 * more drives
4663 */
4664 data_stripes = num_stripes / ncopies;
4665
David Woodhouse53b381b2013-01-29 18:40:14 -05004666 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4667 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4668 btrfs_super_stripesize(info->super_copy));
4669 data_stripes = num_stripes - 1;
4670 }
4671 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4672 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4673 btrfs_super_stripesize(info->super_copy));
4674 data_stripes = num_stripes - 2;
4675 }
Chris Mason86db2572013-02-20 16:23:40 -05004676
4677 /*
4678 * Use the number of data stripes to figure out how big this chunk
4679 * is really going to be in terms of logical address space,
4680 * and compare that answer with the max chunk size
4681 */
4682 if (stripe_size * data_stripes > max_chunk_size) {
4683 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004684
4685 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004686
4687 /* bump the answer up to a 16MB boundary */
4688 stripe_size = (stripe_size + mask) & ~mask;
4689
4690 /* but don't go higher than the limits we found
4691 * while searching for free extents
4692 */
4693 if (stripe_size > devices_info[ndevs-1].max_avail)
4694 stripe_size = devices_info[ndevs-1].max_avail;
4695 }
4696
David Sterbab8b93ad2015-01-16 17:26:13 +01004697 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004698
4699 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004700 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004701 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004702
Miao Xieb2117a32011-01-05 10:07:28 +00004703 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4704 if (!map) {
4705 ret = -ENOMEM;
4706 goto error;
4707 }
4708 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004709
Arne Jansen73c5de02011-04-12 12:07:57 +02004710 for (i = 0; i < ndevs; ++i) {
4711 for (j = 0; j < dev_stripes; ++j) {
4712 int s = i * dev_stripes + j;
4713 map->stripes[s].dev = devices_info[i].dev;
4714 map->stripes[s].physical = devices_info[i].dev_offset +
4715 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004716 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004717 }
Chris Mason593060d2008-03-25 16:50:33 -04004718 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004719 map->stripe_len = raid_stripe_len;
4720 map->io_align = raid_stripe_len;
4721 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004722 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004723 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004724
David Woodhouse53b381b2013-01-29 18:40:14 -05004725 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004726
Arne Jansen73c5de02011-04-12 12:07:57 +02004727 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004728
David Sterba172ddd62011-04-21 00:48:27 +02004729 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004730 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004731 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004732 ret = -ENOMEM;
4733 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004734 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004735 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004736 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004737 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004738 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004739 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004740 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004741 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004742
Chris Mason0b86a832008-03-24 15:01:56 -04004743 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004744 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004745 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004746 if (!ret) {
4747 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4748 atomic_inc(&em->refs);
4749 }
Chris Mason890871b2009-09-02 16:24:52 -04004750 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004751 if (ret) {
4752 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004753 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004754 }
Yan Zheng2b820322008-11-17 21:11:30 -05004755
Josef Bacik04487482013-01-29 15:03:37 -05004756 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4757 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4758 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004759 if (ret)
4760 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004761
Miao Xie7cc8e582014-09-03 21:35:38 +08004762 for (i = 0; i < map->num_stripes; i++) {
4763 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4764 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4765 }
Miao Xie43530c42014-09-03 21:35:36 +08004766
Miao Xie1c116182014-09-03 21:35:37 +08004767 spin_lock(&extent_root->fs_info->free_chunk_lock);
4768 extent_root->fs_info->free_chunk_space -= (stripe_size *
4769 map->num_stripes);
4770 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4771
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004772 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004773 check_raid56_incompat_flag(extent_root->fs_info, type);
4774
Miao Xieb2117a32011-01-05 10:07:28 +00004775 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004776 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004777
Josef Bacik6df9a952013-06-27 13:22:46 -04004778error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004779 write_lock(&em_tree->lock);
4780 remove_extent_mapping(em_tree, em);
4781 write_unlock(&em_tree->lock);
4782
4783 /* One for our allocation */
4784 free_extent_map(em);
4785 /* One for the tree reference */
4786 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004787 /* One for the pending_chunks list reference */
4788 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004789error:
Miao Xieb2117a32011-01-05 10:07:28 +00004790 kfree(devices_info);
4791 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004792}
4793
Josef Bacik6df9a952013-06-27 13:22:46 -04004794int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004795 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004796 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004797{
Yan Zheng2b820322008-11-17 21:11:30 -05004798 struct btrfs_key key;
4799 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4800 struct btrfs_device *device;
4801 struct btrfs_chunk *chunk;
4802 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004803 struct extent_map_tree *em_tree;
4804 struct extent_map *em;
4805 struct map_lookup *map;
4806 size_t item_size;
4807 u64 dev_offset;
4808 u64 stripe_size;
4809 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004810 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004811
Josef Bacik6df9a952013-06-27 13:22:46 -04004812 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4813 read_lock(&em_tree->lock);
4814 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4815 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004816
Josef Bacik6df9a952013-06-27 13:22:46 -04004817 if (!em) {
4818 btrfs_crit(extent_root->fs_info, "unable to find logical "
4819 "%Lu len %Lu", chunk_offset, chunk_size);
4820 return -EINVAL;
4821 }
4822
4823 if (em->start != chunk_offset || em->len != chunk_size) {
4824 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004825 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004826 chunk_size, em->start, em->len);
4827 free_extent_map(em);
4828 return -EINVAL;
4829 }
4830
Jeff Mahoney95617d62015-06-03 10:55:48 -04004831 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004832 item_size = btrfs_chunk_item_size(map->num_stripes);
4833 stripe_size = em->orig_block_len;
4834
4835 chunk = kzalloc(item_size, GFP_NOFS);
4836 if (!chunk) {
4837 ret = -ENOMEM;
4838 goto out;
4839 }
4840
Filipe Manana50460e32015-11-20 10:42:47 +00004841 /*
4842 * Take the device list mutex to prevent races with the final phase of
4843 * a device replace operation that replaces the device object associated
4844 * with the map's stripes, because the device object's id can change
4845 * at any time during that final phase of the device replace operation
4846 * (dev-replace.c:btrfs_dev_replace_finishing()).
4847 */
4848 mutex_lock(&chunk_root->fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004849 for (i = 0; i < map->num_stripes; i++) {
4850 device = map->stripes[i].dev;
4851 dev_offset = map->stripes[i].physical;
4852
Yan Zheng2b820322008-11-17 21:11:30 -05004853 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004854 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004855 break;
Josef Bacik6df9a952013-06-27 13:22:46 -04004856 ret = btrfs_alloc_dev_extent(trans, device,
4857 chunk_root->root_key.objectid,
4858 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4859 chunk_offset, dev_offset,
4860 stripe_size);
4861 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004862 break;
4863 }
4864 if (ret) {
4865 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex);
4866 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004867 }
4868
Yan Zheng2b820322008-11-17 21:11:30 -05004869 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004870 for (i = 0; i < map->num_stripes; i++) {
4871 device = map->stripes[i].dev;
4872 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004873
4874 btrfs_set_stack_stripe_devid(stripe, device->devid);
4875 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4876 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4877 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004878 }
Filipe Manana50460e32015-11-20 10:42:47 +00004879 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004880
4881 btrfs_set_stack_chunk_length(chunk, chunk_size);
4882 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4883 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4884 btrfs_set_stack_chunk_type(chunk, map->type);
4885 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4886 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4887 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4888 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4889 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4890
4891 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4892 key.type = BTRFS_CHUNK_ITEM_KEY;
4893 key.offset = chunk_offset;
4894
4895 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004896 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4897 /*
4898 * TODO: Cleanup of inserted chunk root in case of
4899 * failure.
4900 */
Li Zefan125ccb02011-12-08 15:07:24 +08004901 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004902 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004903 }
liubo1abe9b82011-03-24 11:18:59 +00004904
Josef Bacik6df9a952013-06-27 13:22:46 -04004905out:
Yan Zheng2b820322008-11-17 21:11:30 -05004906 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004907 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004908 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004909}
4910
4911/*
4912 * Chunk allocation falls into two parts. The first part does works
4913 * that make the new allocated chunk useable, but not do any operation
4914 * that modifies the chunk tree. The second part does the works that
4915 * require modifying the chunk tree. This division is important for the
4916 * bootstrap process of adding storage to a seed btrfs.
4917 */
4918int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4919 struct btrfs_root *extent_root, u64 type)
4920{
4921 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004922
Filipe Mananaa9629592015-05-18 19:11:40 +01004923 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004924 chunk_offset = find_next_chunk(extent_root->fs_info);
4925 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004926}
4927
Chris Masond3977122009-01-05 21:25:51 -05004928static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004929 struct btrfs_root *root,
4930 struct btrfs_device *device)
4931{
4932 u64 chunk_offset;
4933 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004934 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004935 struct btrfs_fs_info *fs_info = root->fs_info;
4936 struct btrfs_root *extent_root = fs_info->extent_root;
4937 int ret;
4938
Josef Bacik6df9a952013-06-27 13:22:46 -04004939 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004940 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004941 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4942 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004943 if (ret)
4944 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004945
Josef Bacik6df9a952013-06-27 13:22:46 -04004946 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004947 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004948 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4949 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004950 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004951}
4952
Miao Xied20983b2014-07-03 18:22:13 +08004953static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4954{
4955 int max_errors;
4956
4957 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4958 BTRFS_BLOCK_GROUP_RAID10 |
4959 BTRFS_BLOCK_GROUP_RAID5 |
4960 BTRFS_BLOCK_GROUP_DUP)) {
4961 max_errors = 1;
4962 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4963 max_errors = 2;
4964 } else {
4965 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004966 }
4967
Miao Xied20983b2014-07-03 18:22:13 +08004968 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004969}
4970
4971int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4972{
4973 struct extent_map *em;
4974 struct map_lookup *map;
4975 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4976 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004977 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004978 int i;
4979
Chris Mason890871b2009-09-02 16:24:52 -04004980 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004981 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004982 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004983 if (!em)
4984 return 1;
4985
Jeff Mahoney95617d62015-06-03 10:55:48 -04004986 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05004987 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004988 if (map->stripes[i].dev->missing) {
4989 miss_ndevs++;
4990 continue;
4991 }
4992
Yan Zheng2b820322008-11-17 21:11:30 -05004993 if (!map->stripes[i].dev->writeable) {
4994 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004995 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004996 }
4997 }
Miao Xied20983b2014-07-03 18:22:13 +08004998
4999 /*
5000 * If the number of missing devices is larger than max errors,
5001 * we can not write the data into that chunk successfully, so
5002 * set it readonly.
5003 */
5004 if (miss_ndevs > btrfs_chunk_max_errors(map))
5005 readonly = 1;
5006end:
Yan Zheng2b820322008-11-17 21:11:30 -05005007 free_extent_map(em);
5008 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005009}
5010
5011void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5012{
David Sterbaa8067e02011-04-21 00:34:43 +02005013 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005014}
5015
5016void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5017{
5018 struct extent_map *em;
5019
Chris Masond3977122009-01-05 21:25:51 -05005020 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005021 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005022 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5023 if (em)
5024 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005025 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005026 if (!em)
5027 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005028 /* once for us */
5029 free_extent_map(em);
5030 /* once for the tree */
5031 free_extent_map(em);
5032 }
5033}
5034
Stefan Behrens5d964052012-11-05 14:59:07 +01005035int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005036{
Stefan Behrens5d964052012-11-05 14:59:07 +01005037 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04005038 struct extent_map *em;
5039 struct map_lookup *map;
5040 struct extent_map_tree *em_tree = &map_tree->map_tree;
5041 int ret;
5042
Chris Mason890871b2009-09-02 16:24:52 -04005043 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005044 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04005045 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005046
Josef Bacikfb7669b2013-04-23 10:53:18 -04005047 /*
5048 * We could return errors for these cases, but that could get ugly and
5049 * we'd probably do the same thing which is just not do anything else
5050 * and exit, so return 1 so the callers don't try to use other copies.
5051 */
5052 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02005053 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005054 logical+len);
5055 return 1;
5056 }
5057
5058 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02005059 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02005060 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005061 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005062 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04005063 return 1;
5064 }
5065
Jeff Mahoney95617d62015-06-03 10:55:48 -04005066 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005067 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5068 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005069 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5070 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005071 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5072 ret = 2;
5073 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5074 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005075 else
5076 ret = 1;
5077 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005078
Liu Bo73beece2015-07-17 16:49:19 +08005079 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005080 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
5081 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005082 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005083
Chris Masonf1885912008-04-09 16:28:12 -04005084 return ret;
5085}
5086
David Woodhouse53b381b2013-01-29 18:40:14 -05005087unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
5088 struct btrfs_mapping_tree *map_tree,
5089 u64 logical)
5090{
5091 struct extent_map *em;
5092 struct map_lookup *map;
5093 struct extent_map_tree *em_tree = &map_tree->map_tree;
5094 unsigned long len = root->sectorsize;
5095
5096 read_lock(&em_tree->lock);
5097 em = lookup_extent_mapping(em_tree, logical, len);
5098 read_unlock(&em_tree->lock);
5099 BUG_ON(!em);
5100
5101 BUG_ON(em->start > logical || em->start + em->len < logical);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005102 map = em->map_lookup;
Zhao Leiffe2d202015-01-20 15:11:44 +08005103 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005104 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05005105 free_extent_map(em);
5106 return len;
5107}
5108
5109int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
5110 u64 logical, u64 len, int mirror_num)
5111{
5112 struct extent_map *em;
5113 struct map_lookup *map;
5114 struct extent_map_tree *em_tree = &map_tree->map_tree;
5115 int ret = 0;
5116
5117 read_lock(&em_tree->lock);
5118 em = lookup_extent_mapping(em_tree, logical, len);
5119 read_unlock(&em_tree->lock);
5120 BUG_ON(!em);
5121
5122 BUG_ON(em->start > logical || em->start + em->len < logical);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005123 map = em->map_lookup;
Zhao Leiffe2d202015-01-20 15:11:44 +08005124 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005125 ret = 1;
5126 free_extent_map(em);
5127 return ret;
5128}
5129
Stefan Behrens30d98612012-11-06 14:52:18 +01005130static int find_live_mirror(struct btrfs_fs_info *fs_info,
5131 struct map_lookup *map, int first, int num,
5132 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005133{
5134 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005135 int tolerance;
5136 struct btrfs_device *srcdev;
5137
5138 if (dev_replace_is_ongoing &&
5139 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5140 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5141 srcdev = fs_info->dev_replace.srcdev;
5142 else
5143 srcdev = NULL;
5144
5145 /*
5146 * try to avoid the drive that is the source drive for a
5147 * dev-replace procedure, only choose it if no other non-missing
5148 * mirror is available
5149 */
5150 for (tolerance = 0; tolerance < 2; tolerance++) {
5151 if (map->stripes[optimal].dev->bdev &&
5152 (tolerance || map->stripes[optimal].dev != srcdev))
5153 return optimal;
5154 for (i = first; i < first + num; i++) {
5155 if (map->stripes[i].dev->bdev &&
5156 (tolerance || map->stripes[i].dev != srcdev))
5157 return i;
5158 }
Chris Masondfe25022008-05-13 13:46:40 -04005159 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005160
Chris Masondfe25022008-05-13 13:46:40 -04005161 /* we couldn't find one that doesn't fail. Just return something
5162 * and the io error handling code will clean up eventually
5163 */
5164 return optimal;
5165}
5166
David Woodhouse53b381b2013-01-29 18:40:14 -05005167static inline int parity_smaller(u64 a, u64 b)
5168{
5169 return a > b;
5170}
5171
5172/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005173static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005174{
5175 struct btrfs_bio_stripe s;
5176 int i;
5177 u64 l;
5178 int again = 1;
5179
5180 while (again) {
5181 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005182 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005183 if (parity_smaller(bbio->raid_map[i],
5184 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005185 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005186 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005187 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005188 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005189 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005190 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005191
David Woodhouse53b381b2013-01-29 18:40:14 -05005192 again = 1;
5193 }
5194 }
5195 }
5196}
5197
Zhao Lei6e9606d2015-01-20 15:11:34 +08005198static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5199{
5200 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005201 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005202 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005203 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005204 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005205 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005206 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005207 /*
5208 * plus the raid_map, which includes both the tgt dev
5209 * and the stripes
5210 */
5211 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005212 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005213
5214 atomic_set(&bbio->error, 0);
5215 atomic_set(&bbio->refs, 1);
5216
5217 return bbio;
5218}
5219
5220void btrfs_get_bbio(struct btrfs_bio *bbio)
5221{
5222 WARN_ON(!atomic_read(&bbio->refs));
5223 atomic_inc(&bbio->refs);
5224}
5225
5226void btrfs_put_bbio(struct btrfs_bio *bbio)
5227{
5228 if (!bbio)
5229 return;
5230 if (atomic_dec_and_test(&bbio->refs))
5231 kfree(bbio);
5232}
5233
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005234static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005235 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005236 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005237 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005238{
5239 struct extent_map *em;
5240 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005241 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005242 struct extent_map_tree *em_tree = &map_tree->map_tree;
5243 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005244 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005245 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005246 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005247 u64 stripe_nr_orig;
5248 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005249 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005250 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005251 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005252 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005253 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005254 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005255 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005256 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005257 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5258 int dev_replace_is_ongoing = 0;
5259 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005260 int patch_the_first_stripe_for_dev_replace = 0;
5261 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005262 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005263
Chris Mason890871b2009-09-02 16:24:52 -04005264 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005265 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005266 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005267
Chris Mason3b951512008-04-17 11:29:12 -04005268 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005269 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005270 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005271 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005272 }
Chris Mason0b86a832008-03-24 15:01:56 -04005273
Josef Bacik9bb91872013-04-19 23:45:33 -04005274 if (em->start > logical || em->start + em->len < logical) {
5275 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005276 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005277 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005278 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005279 return -EINVAL;
5280 }
5281
Jeff Mahoney95617d62015-06-03 10:55:48 -04005282 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005283 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005284
David Woodhouse53b381b2013-01-29 18:40:14 -05005285 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005286 stripe_nr = offset;
5287 /*
5288 * stripe_nr counts the total number of stripes we have to stride
5289 * to get to this block
5290 */
David Sterba47c57132015-02-20 18:43:47 +01005291 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005292
David Woodhouse53b381b2013-01-29 18:40:14 -05005293 stripe_offset = stripe_nr * stripe_len;
Josef Bacike042d1e2016-04-12 12:54:40 -04005294 if (offset < stripe_offset) {
5295 btrfs_crit(fs_info, "stripe math has gone wrong, "
5296 "stripe_offset=%llu, offset=%llu, start=%llu, "
5297 "logical=%llu, stripe_len=%llu",
5298 stripe_offset, offset, em->start, logical,
5299 stripe_len);
5300 free_extent_map(em);
5301 return -EINVAL;
5302 }
Chris Mason593060d2008-03-25 16:50:33 -04005303
5304 /* stripe_offset is the offset of this block in its stripe*/
5305 stripe_offset = offset - stripe_offset;
5306
David Woodhouse53b381b2013-01-29 18:40:14 -05005307 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005308 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005309 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5310 raid56_full_stripe_start = offset;
5311
5312 /* allow a write of a full stripe, but make sure we don't
5313 * allow straddling of stripes
5314 */
David Sterba47c57132015-02-20 18:43:47 +01005315 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5316 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005317 raid56_full_stripe_start *= full_stripe_len;
5318 }
5319
5320 if (rw & REQ_DISCARD) {
5321 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005322 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005323 ret = -EOPNOTSUPP;
5324 goto out;
5325 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005326 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005327 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5328 u64 max_len;
5329 /* For writes to RAID[56], allow a full stripeset across all disks.
5330 For other RAID types and for RAID[56] reads, just allow a single
5331 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005332 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005333 (rw & REQ_WRITE)) {
5334 max_len = stripe_len * nr_data_stripes(map) -
5335 (offset - raid56_full_stripe_start);
5336 } else {
5337 /* we limit the length of each bio to what fits in a stripe */
5338 max_len = stripe_len - stripe_offset;
5339 }
5340 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005341 } else {
5342 *length = em->len - offset;
5343 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005344
David Woodhouse53b381b2013-01-29 18:40:14 -05005345 /* This is for when we're called from btrfs_merge_bio_hook() and all
5346 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005347 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005348 goto out;
5349
Liu Bo73beece2015-07-17 16:49:19 +08005350 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005351 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5352 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005353 btrfs_dev_replace_unlock(dev_replace, 0);
5354 else
5355 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005356
Stefan Behrensad6d6202012-11-06 15:06:47 +01005357 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5358 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5359 dev_replace->tgtdev != NULL) {
5360 /*
5361 * in dev-replace case, for repair case (that's the only
5362 * case where the mirror is selected explicitly when
5363 * calling btrfs_map_block), blocks left of the left cursor
5364 * can also be read from the target drive.
5365 * For REQ_GET_READ_MIRRORS, the target drive is added as
5366 * the last one to the array of stripes. For READ, it also
5367 * needs to be supported using the same mirror number.
5368 * If the requested block is not left of the left cursor,
5369 * EIO is returned. This can happen because btrfs_num_copies()
5370 * returns one more in the dev-replace case.
5371 */
5372 u64 tmp_length = *length;
5373 struct btrfs_bio *tmp_bbio = NULL;
5374 int tmp_num_stripes;
5375 u64 srcdev_devid = dev_replace->srcdev->devid;
5376 int index_srcdev = 0;
5377 int found = 0;
5378 u64 physical_of_found = 0;
5379
5380 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005381 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005382 if (ret) {
5383 WARN_ON(tmp_bbio != NULL);
5384 goto out;
5385 }
5386
5387 tmp_num_stripes = tmp_bbio->num_stripes;
5388 if (mirror_num > tmp_num_stripes) {
5389 /*
5390 * REQ_GET_READ_MIRRORS does not contain this
5391 * mirror, that means that the requested area
5392 * is not left of the left cursor
5393 */
5394 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005395 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005396 goto out;
5397 }
5398
5399 /*
5400 * process the rest of the function using the mirror_num
5401 * of the source drive. Therefore look it up first.
5402 * At the end, patch the device pointer to the one of the
5403 * target drive.
5404 */
5405 for (i = 0; i < tmp_num_stripes; i++) {
Zhao Lei94a97df2015-12-09 21:03:49 +08005406 if (tmp_bbio->stripes[i].dev->devid != srcdev_devid)
5407 continue;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005408
Zhao Lei94a97df2015-12-09 21:03:49 +08005409 /*
5410 * In case of DUP, in order to keep it simple, only add
5411 * the mirror with the lowest physical address
5412 */
5413 if (found &&
5414 physical_of_found <= tmp_bbio->stripes[i].physical)
5415 continue;
5416
5417 index_srcdev = i;
5418 found = 1;
5419 physical_of_found = tmp_bbio->stripes[i].physical;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005420 }
5421
Zhao Lei6e9606d2015-01-20 15:11:34 +08005422 btrfs_put_bbio(tmp_bbio);
Zhao Lei94a97df2015-12-09 21:03:49 +08005423
5424 if (!found) {
5425 WARN_ON(1);
5426 ret = -EIO;
5427 goto out;
5428 }
5429
5430 mirror_num = index_srcdev + 1;
5431 patch_the_first_stripe_for_dev_replace = 1;
5432 physical_to_patch_in_first_stripe = physical_of_found;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005433 } else if (mirror_num > map->num_stripes) {
5434 mirror_num = 0;
5435 }
5436
Chris Masonf2d8d742008-04-21 10:03:05 -04005437 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005438 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005439 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005440 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005441 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005442 stripe_end_offset = stripe_nr_end * map->stripe_len -
5443 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005444
Li Dongyangfce3bb92011-03-24 10:24:26 +00005445 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5446 if (rw & REQ_DISCARD)
5447 num_stripes = min_t(u64, map->num_stripes,
5448 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005449 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5450 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005451 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5452 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005453 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005454 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005455 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005456 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005457 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005458 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005459 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005460 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005461 current->pid % map->num_stripes,
5462 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005463 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005464 }
Chris Mason2fff7342008-04-29 14:12:09 -04005465
Chris Mason611f0e02008-04-03 16:29:03 -04005466 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005467 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005468 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005469 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005470 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005471 } else {
5472 mirror_num = 1;
5473 }
Chris Mason2fff7342008-04-29 14:12:09 -04005474
Chris Mason321aecc2008-04-16 10:49:51 -04005475 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005476 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005477
David Sterba47c57132015-02-20 18:43:47 +01005478 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005479 stripe_index *= map->sub_stripes;
5480
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005481 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005482 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005483 else if (rw & REQ_DISCARD)
5484 num_stripes = min_t(u64, map->sub_stripes *
5485 (stripe_nr_end - stripe_nr_orig),
5486 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005487 else if (mirror_num)
5488 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005489 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005490 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005491 stripe_index = find_live_mirror(fs_info, map,
5492 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005493 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005494 current->pid % map->sub_stripes,
5495 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005496 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005497 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005498
Zhao Leiffe2d202015-01-20 15:11:44 +08005499 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005500 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005501 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5502 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005503 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005504 stripe_nr = div_u64(raid56_full_stripe_start,
5505 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005506
5507 /* RAID[56] write or recovery. Return all stripes */
5508 num_stripes = map->num_stripes;
5509 max_errors = nr_parity_stripes(map);
5510
David Woodhouse53b381b2013-01-29 18:40:14 -05005511 *length = map->stripe_len;
5512 stripe_index = 0;
5513 stripe_offset = 0;
5514 } else {
5515 /*
5516 * Mirror #0 or #1 means the original data block.
5517 * Mirror #2 is RAID5 parity block.
5518 * Mirror #3 is RAID6 Q block.
5519 */
David Sterba47c57132015-02-20 18:43:47 +01005520 stripe_nr = div_u64_rem(stripe_nr,
5521 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005522 if (mirror_num > 1)
5523 stripe_index = nr_data_stripes(map) +
5524 mirror_num - 2;
5525
5526 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005527 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5528 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005529 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5530 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5531 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005532 }
Chris Mason8790d502008-04-03 16:29:03 -04005533 } else {
5534 /*
David Sterba47c57132015-02-20 18:43:47 +01005535 * after this, stripe_nr is the number of stripes on this
5536 * device we have to walk to find the data, and stripe_index is
5537 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005538 */
David Sterba47c57132015-02-20 18:43:47 +01005539 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5540 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005541 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005542 }
Josef Bacike042d1e2016-04-12 12:54:40 -04005543 if (stripe_index >= map->num_stripes) {
5544 btrfs_crit(fs_info, "stripe index math went horribly wrong, "
5545 "got stripe_index=%u, num_stripes=%u",
5546 stripe_index, map->num_stripes);
5547 ret = -EINVAL;
5548 goto out;
5549 }
Chris Mason593060d2008-03-25 16:50:33 -04005550
Stefan Behrens472262f2012-11-06 14:43:46 +01005551 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005552 if (dev_replace_is_ongoing) {
5553 if (rw & (REQ_WRITE | REQ_DISCARD))
5554 num_alloc_stripes <<= 1;
5555 if (rw & REQ_GET_READ_MIRRORS)
5556 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005557 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005558 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005559
Zhao Lei6e9606d2015-01-20 15:11:34 +08005560 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005561 if (!bbio) {
5562 ret = -ENOMEM;
5563 goto out;
5564 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005565 if (dev_replace_is_ongoing)
5566 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005567
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005568 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005569 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005570 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5571 mirror_num > 1)) {
5572 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005573 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005574
5575 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5576 sizeof(struct btrfs_bio_stripe) *
5577 num_alloc_stripes +
5578 sizeof(int) * tgtdev_indexes);
5579
5580 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005581 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005582
5583 /* Fill in the logical address of each stripe */
5584 tmp = stripe_nr * nr_data_stripes(map);
5585 for (i = 0; i < nr_data_stripes(map); i++)
5586 bbio->raid_map[(i+rot) % num_stripes] =
5587 em->start + (tmp + i) * map->stripe_len;
5588
5589 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5590 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5591 bbio->raid_map[(i+rot+1) % num_stripes] =
5592 RAID6_Q_STRIPE;
5593 }
5594
Li Dongyangfce3bb92011-03-24 10:24:26 +00005595 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005596 u32 factor = 0;
5597 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005598 u64 stripes_per_dev = 0;
5599 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005600 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005601
5602 if (map->type &
5603 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5604 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5605 sub_stripes = 1;
5606 else
5607 sub_stripes = map->sub_stripes;
5608
5609 factor = map->num_stripes / sub_stripes;
5610 stripes_per_dev = div_u64_rem(stripe_nr_end -
5611 stripe_nr_orig,
5612 factor,
5613 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005614 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5615 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005616 }
5617
Li Dongyangfce3bb92011-03-24 10:24:26 +00005618 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005619 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005620 map->stripes[stripe_index].physical +
5621 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005622 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005623
Li Zefanec9ef7a2011-12-01 14:06:42 +08005624 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5625 BTRFS_BLOCK_GROUP_RAID10)) {
5626 bbio->stripes[i].length = stripes_per_dev *
5627 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005628
Li Zefanec9ef7a2011-12-01 14:06:42 +08005629 if (i / sub_stripes < remaining_stripes)
5630 bbio->stripes[i].length +=
5631 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005632
5633 /*
5634 * Special for the first stripe and
5635 * the last stripe:
5636 *
5637 * |-------|...|-------|
5638 * |----------|
5639 * off end_off
5640 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005641 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005642 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005643 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005644
5645 if (stripe_index >= last_stripe &&
5646 stripe_index <= (last_stripe +
5647 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005648 bbio->stripes[i].length -=
5649 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005650
Li Zefanec9ef7a2011-12-01 14:06:42 +08005651 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005652 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005653 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005654 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005655
5656 stripe_index++;
5657 if (stripe_index == map->num_stripes) {
5658 /* This could only happen for RAID0/10 */
5659 stripe_index = 0;
5660 stripe_nr++;
5661 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005662 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005663 } else {
5664 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005665 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005666 map->stripes[stripe_index].physical +
5667 stripe_offset +
5668 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005669 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005670 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005671 stripe_index++;
5672 }
Chris Mason593060d2008-03-25 16:50:33 -04005673 }
Li Zefande11cc12011-12-01 12:55:47 +08005674
Miao Xied20983b2014-07-03 18:22:13 +08005675 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5676 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005677
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005678 if (bbio->raid_map)
5679 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005680
Miao Xie2c8cdd62014-11-14 16:06:25 +08005681 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005682 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5683 dev_replace->tgtdev != NULL) {
5684 int index_where_to_add;
5685 u64 srcdev_devid = dev_replace->srcdev->devid;
5686
5687 /*
5688 * duplicate the write operations while the dev replace
5689 * procedure is running. Since the copying of the old disk
5690 * to the new disk takes place at run time while the
5691 * filesystem is mounted writable, the regular write
5692 * operations to the old disk have to be duplicated to go
5693 * to the new disk as well.
5694 * Note that device->missing is handled by the caller, and
5695 * that the write to the old disk is already set up in the
5696 * stripes array.
5697 */
5698 index_where_to_add = num_stripes;
5699 for (i = 0; i < num_stripes; i++) {
5700 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5701 /* write to new disk, too */
5702 struct btrfs_bio_stripe *new =
5703 bbio->stripes + index_where_to_add;
5704 struct btrfs_bio_stripe *old =
5705 bbio->stripes + i;
5706
5707 new->physical = old->physical;
5708 new->length = old->length;
5709 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005710 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005711 index_where_to_add++;
5712 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005713 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005714 }
5715 }
5716 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005717 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5718 dev_replace->tgtdev != NULL) {
5719 u64 srcdev_devid = dev_replace->srcdev->devid;
5720 int index_srcdev = 0;
5721 int found = 0;
5722 u64 physical_of_found = 0;
5723
5724 /*
5725 * During the dev-replace procedure, the target drive can
5726 * also be used to read data in case it is needed to repair
5727 * a corrupt block elsewhere. This is possible if the
5728 * requested area is left of the left cursor. In this area,
5729 * the target drive is a full copy of the source drive.
5730 */
5731 for (i = 0; i < num_stripes; i++) {
5732 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5733 /*
5734 * In case of DUP, in order to keep it
5735 * simple, only add the mirror with the
5736 * lowest physical address
5737 */
5738 if (found &&
5739 physical_of_found <=
5740 bbio->stripes[i].physical)
5741 continue;
5742 index_srcdev = i;
5743 found = 1;
5744 physical_of_found = bbio->stripes[i].physical;
5745 }
5746 }
5747 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005748 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005749 dev_replace->cursor_left) {
5750 struct btrfs_bio_stripe *tgtdev_stripe =
5751 bbio->stripes + num_stripes;
5752
5753 tgtdev_stripe->physical = physical_of_found;
5754 tgtdev_stripe->length =
5755 bbio->stripes[index_srcdev].length;
5756 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005757 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005758
Miao Xie2c8cdd62014-11-14 16:06:25 +08005759 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005760 num_stripes++;
5761 }
5762 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005763 }
5764
Li Zefande11cc12011-12-01 12:55:47 +08005765 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005766 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005767 bbio->num_stripes = num_stripes;
5768 bbio->max_errors = max_errors;
5769 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005770 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005771
5772 /*
5773 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5774 * mirror_num == num_stripes + 1 && dev_replace target drive is
5775 * available as a mirror
5776 */
5777 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5778 WARN_ON(num_stripes > 1);
5779 bbio->stripes[0].dev = dev_replace->tgtdev;
5780 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5781 bbio->mirror_num = map->num_stripes + 1;
5782 }
Chris Masoncea9e442008-04-09 16:28:12 -04005783out:
Liu Bo73beece2015-07-17 16:49:19 +08005784 if (dev_replace_is_ongoing) {
5785 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5786 btrfs_dev_replace_unlock(dev_replace, 0);
5787 }
Chris Mason0b86a832008-03-24 15:01:56 -04005788 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005789 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005790}
5791
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005792int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005793 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005794 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005795{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005796 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005797 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005798}
5799
Miao Xieaf8e2d12014-10-23 14:42:50 +08005800/* For Scrub/replace */
5801int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5802 u64 logical, u64 *length,
5803 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005804 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005805{
5806 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005807 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005808}
5809
Yan Zhenga512bbf2008-12-08 16:46:26 -05005810int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5811 u64 chunk_start, u64 physical, u64 devid,
5812 u64 **logical, int *naddrs, int *stripe_len)
5813{
5814 struct extent_map_tree *em_tree = &map_tree->map_tree;
5815 struct extent_map *em;
5816 struct map_lookup *map;
5817 u64 *buf;
5818 u64 bytenr;
5819 u64 length;
5820 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005821 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005822 int i, j, nr = 0;
5823
Chris Mason890871b2009-09-02 16:24:52 -04005824 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005825 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005826 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005827
Josef Bacik835d9742013-03-19 12:13:25 -04005828 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005829 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005830 chunk_start);
5831 return -EIO;
5832 }
5833
5834 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005835 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005836 em->start, chunk_start);
5837 free_extent_map(em);
5838 return -EIO;
5839 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04005840 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005841
5842 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005843 rmap_len = map->stripe_len;
5844
Yan Zhenga512bbf2008-12-08 16:46:26 -05005845 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005846 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005847 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005848 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005849 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005850 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005851 rmap_len = map->stripe_len * nr_data_stripes(map);
5852 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005853
David Sterba31e818f2015-02-20 18:00:26 +01005854 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005855 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005856
5857 for (i = 0; i < map->num_stripes; i++) {
5858 if (devid && map->stripes[i].dev->devid != devid)
5859 continue;
5860 if (map->stripes[i].physical > physical ||
5861 map->stripes[i].physical + length <= physical)
5862 continue;
5863
5864 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005865 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005866
5867 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5868 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005869 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005870 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5871 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005872 } /* else if RAID[56], multiply by nr_data_stripes().
5873 * Alternatively, just use rmap_len below instead of
5874 * map->stripe_len */
5875
5876 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005877 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005878 for (j = 0; j < nr; j++) {
5879 if (buf[j] == bytenr)
5880 break;
5881 }
Chris Mason934d3752008-12-08 16:43:10 -05005882 if (j == nr) {
5883 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005884 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005885 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005886 }
5887
Yan Zhenga512bbf2008-12-08 16:46:26 -05005888 *logical = buf;
5889 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005890 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005891
5892 free_extent_map(em);
5893 return 0;
5894}
5895
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005896static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005897{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005898 bio->bi_private = bbio->private;
5899 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005900 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005901
Zhao Lei6e9606d2015-01-20 15:11:34 +08005902 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005903}
5904
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005905static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005906{
Chris Mason9be33952013-05-17 18:30:14 -04005907 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005908 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005909
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005910 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005911 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005912 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005913 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005914 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005915 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005916
5917 BUG_ON(stripe_index >= bbio->num_stripes);
5918 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005919 if (dev->bdev) {
5920 if (bio->bi_rw & WRITE)
5921 btrfs_dev_stat_inc(dev,
5922 BTRFS_DEV_STAT_WRITE_ERRS);
5923 else
5924 btrfs_dev_stat_inc(dev,
5925 BTRFS_DEV_STAT_READ_ERRS);
5926 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5927 btrfs_dev_stat_inc(dev,
5928 BTRFS_DEV_STAT_FLUSH_ERRS);
5929 btrfs_dev_stat_print_on_error(dev);
5930 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005931 }
5932 }
Chris Mason8790d502008-04-03 16:29:03 -04005933
Jan Schmidta1d3c472011-08-04 17:15:33 +02005934 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005935 is_orig_bio = 1;
5936
Miao Xiec404e0d2014-01-30 16:46:55 +08005937 btrfs_bio_counter_dec(bbio->fs_info);
5938
Jan Schmidta1d3c472011-08-04 17:15:33 +02005939 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005940 if (!is_orig_bio) {
5941 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005942 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005943 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005944
Chris Mason9be33952013-05-17 18:30:14 -04005945 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005946 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005947 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005948 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005949 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005950 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005951 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005952 /*
5953 * this bio is actually up to date, we didn't
5954 * go over the max number of errors
5955 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005956 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005957 }
Miao Xiec55f1392014-06-19 10:42:54 +08005958
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005959 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005960 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005961 bio_put(bio);
5962 }
Chris Mason8790d502008-04-03 16:29:03 -04005963}
5964
Chris Mason8b712842008-06-11 16:50:36 -04005965/*
5966 * see run_scheduled_bios for a description of why bios are collected for
5967 * async submit.
5968 *
5969 * This will add one bio to the pending list for a device and make sure
5970 * the work struct is scheduled.
5971 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005972static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5973 struct btrfs_device *device,
5974 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005975{
5976 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005977 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005978
David Woodhouse53b381b2013-01-29 18:40:14 -05005979 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005980 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005981 return;
5982 }
5983
Chris Mason8b712842008-06-11 16:50:36 -04005984 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005985 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005986 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005987 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005988 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005989 return;
Chris Mason8b712842008-06-11 16:50:36 -04005990 }
5991
5992 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005993 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005994 * higher layers. Otherwise, the async bio makes it appear we have
5995 * made progress against dirty pages when we've really just put it
5996 * on a queue for later
5997 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005998 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005999 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04006000 bio->bi_next = NULL;
6001 bio->bi_rw |= rw;
6002
6003 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02006004 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04006005 pending_bios = &device->pending_sync_bios;
6006 else
6007 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04006008
Chris Masonffbd5172009-04-20 15:50:09 -04006009 if (pending_bios->tail)
6010 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006011
Chris Masonffbd5172009-04-20 15:50:09 -04006012 pending_bios->tail = bio;
6013 if (!pending_bios->head)
6014 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006015 if (device->running_pending)
6016 should_queue = 0;
6017
6018 spin_unlock(&device->io_lock);
6019
6020 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08006021 btrfs_queue_work(root->fs_info->submit_workers,
6022 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006023}
6024
Josef Bacikde1ee922012-10-19 16:50:56 -04006025static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
6026 struct bio *bio, u64 physical, int dev_nr,
6027 int rw, int async)
6028{
6029 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
6030
6031 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006032 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006033 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006034 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006035#ifdef DEBUG
6036 {
6037 struct rcu_string *name;
6038
6039 rcu_read_lock();
6040 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00006041 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04006042 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02006043 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
6044 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006045 rcu_read_unlock();
6046 }
6047#endif
6048 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08006049
6050 btrfs_bio_counter_inc_noblocked(root->fs_info);
6051
Josef Bacikde1ee922012-10-19 16:50:56 -04006052 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05006053 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006054 else
6055 btrfsic_submit_bio(rw, bio);
6056}
6057
Josef Bacikde1ee922012-10-19 16:50:56 -04006058static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6059{
6060 atomic_inc(&bbio->error);
6061 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08006062 /* Shoud be the original bio. */
6063 WARN_ON(bio != bbio->orig_bio);
6064
Chris Mason9be33952013-05-17 18:30:14 -04006065 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006066 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006067 bio->bi_error = -EIO;
6068 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006069 }
6070}
6071
Chris Masonf1885912008-04-09 16:28:12 -04006072int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04006073 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006074{
Chris Mason0b86a832008-03-24 15:01:56 -04006075 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006076 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006077 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006078 u64 length = 0;
6079 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006080 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006081 int dev_nr;
6082 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006083 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006084
Kent Overstreet4f024f32013-10-11 15:44:27 -07006085 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006086 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006087
Miao Xiec404e0d2014-01-30 16:46:55 +08006088 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05006089 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006090 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006091 if (ret) {
6092 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006093 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08006094 }
Chris Masoncea9e442008-04-09 16:28:12 -04006095
Jan Schmidta1d3c472011-08-04 17:15:33 +02006096 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006097 bbio->orig_bio = first_bio;
6098 bbio->private = first_bio->bi_private;
6099 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08006100 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006101 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6102
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006103 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6104 ((rw & WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006105 /* In this case, map_length has been set to the length of
6106 a single stripe; not the whole write */
6107 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006108 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006109 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006110 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08006111 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006112 }
Miao Xie42452152014-11-25 16:39:28 +08006113
Miao Xiec404e0d2014-01-30 16:46:55 +08006114 btrfs_bio_counter_dec(root->fs_info);
6115 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05006116 }
6117
Chris Masoncea9e442008-04-09 16:28:12 -04006118 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006119 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006120 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006121 BUG();
6122 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006123
Zhao Lei08da7572015-02-12 15:42:16 +08006124 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006125 dev = bbio->stripes[dev_nr].dev;
6126 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
6127 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006128 continue;
6129 }
6130
Jan Schmidta1d3c472011-08-04 17:15:33 +02006131 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04006132 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006133 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04006134 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006135 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006136
Josef Bacikde1ee922012-10-19 16:50:56 -04006137 submit_stripe_bio(root, bbio, bio,
6138 bbio->stripes[dev_nr].physical, dev_nr, rw,
6139 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006140 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006141 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006142 return 0;
6143}
6144
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006145struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006146 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006147{
Yan Zheng2b820322008-11-17 21:11:30 -05006148 struct btrfs_device *device;
6149 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006150
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006151 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006152 while (cur_devices) {
6153 if (!fsid ||
6154 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6155 device = __find_device(&cur_devices->devices,
6156 devid, uuid);
6157 if (device)
6158 return device;
6159 }
6160 cur_devices = cur_devices->seed;
6161 }
6162 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006163}
6164
Chris Masondfe25022008-05-13 13:46:40 -04006165static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006166 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006167 u64 devid, u8 *dev_uuid)
6168{
6169 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006170
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006171 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6172 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006173 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006174
6175 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006176 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006177 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006178
6179 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006180 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006181
Chris Masondfe25022008-05-13 13:46:40 -04006182 return device;
6183}
6184
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006185/**
6186 * btrfs_alloc_device - allocate struct btrfs_device
6187 * @fs_info: used only for generating a new devid, can be NULL if
6188 * devid is provided (i.e. @devid != NULL).
6189 * @devid: a pointer to devid for this device. If NULL a new devid
6190 * is generated.
6191 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6192 * is generated.
6193 *
6194 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6195 * on error. Returned struct is not linked onto any lists and can be
6196 * destroyed with kfree() right away.
6197 */
6198struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6199 const u64 *devid,
6200 const u8 *uuid)
6201{
6202 struct btrfs_device *dev;
6203 u64 tmp;
6204
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306205 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006206 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006207
6208 dev = __alloc_device();
6209 if (IS_ERR(dev))
6210 return dev;
6211
6212 if (devid)
6213 tmp = *devid;
6214 else {
6215 int ret;
6216
6217 ret = find_next_devid(fs_info, &tmp);
6218 if (ret) {
6219 kfree(dev);
6220 return ERR_PTR(ret);
6221 }
6222 }
6223 dev->devid = tmp;
6224
6225 if (uuid)
6226 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6227 else
6228 generate_random_uuid(dev->uuid);
6229
Liu Bo9e0af232014-08-15 23:36:53 +08006230 btrfs_init_work(&dev->work, btrfs_submit_helper,
6231 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006232
6233 return dev;
6234}
6235
Chris Mason0b86a832008-03-24 15:01:56 -04006236static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6237 struct extent_buffer *leaf,
6238 struct btrfs_chunk *chunk)
6239{
6240 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6241 struct map_lookup *map;
6242 struct extent_map *em;
6243 u64 logical;
6244 u64 length;
Qu Wenruof04b7722015-12-15 09:14:37 +08006245 u64 stripe_len;
Chris Mason0b86a832008-03-24 15:01:56 -04006246 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006247 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006248 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006249 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006250 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006251
Chris Masone17cade2008-04-15 15:41:47 -04006252 logical = key->offset;
6253 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006254 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6255 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6256 /* Validation check */
6257 if (!num_stripes) {
6258 btrfs_err(root->fs_info, "invalid chunk num_stripes: %u",
6259 num_stripes);
6260 return -EIO;
6261 }
6262 if (!IS_ALIGNED(logical, root->sectorsize)) {
6263 btrfs_err(root->fs_info,
6264 "invalid chunk logical %llu", logical);
6265 return -EIO;
6266 }
6267 if (!length || !IS_ALIGNED(length, root->sectorsize)) {
6268 btrfs_err(root->fs_info,
6269 "invalid chunk length %llu", length);
6270 return -EIO;
6271 }
Liu Bo3d8da672016-04-26 17:53:31 -07006272 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
Qu Wenruof04b7722015-12-15 09:14:37 +08006273 btrfs_err(root->fs_info, "invalid chunk stripe length: %llu",
6274 stripe_len);
6275 return -EIO;
6276 }
6277 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6278 btrfs_chunk_type(leaf, chunk)) {
6279 btrfs_err(root->fs_info, "unrecognized chunk type: %llu",
6280 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6281 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6282 btrfs_chunk_type(leaf, chunk));
6283 return -EIO;
6284 }
Chris Masona061fc82008-05-07 11:43:44 -04006285
Chris Mason890871b2009-09-02 16:24:52 -04006286 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006287 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006288 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006289
6290 /* already mapped? */
6291 if (em && em->start <= logical && em->start + em->len > logical) {
6292 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006293 return 0;
6294 } else if (em) {
6295 free_extent_map(em);
6296 }
Chris Mason0b86a832008-03-24 15:01:56 -04006297
David Sterba172ddd62011-04-21 00:48:27 +02006298 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006299 if (!em)
6300 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006301 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006302 if (!map) {
6303 free_extent_map(em);
6304 return -ENOMEM;
6305 }
6306
Wang Shilong298a8f92014-06-19 10:42:52 +08006307 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006308 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006309 em->start = logical;
6310 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006311 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006312 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006313 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006314
Chris Mason593060d2008-03-25 16:50:33 -04006315 map->num_stripes = num_stripes;
6316 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6317 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6318 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6319 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6320 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006321 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006322 for (i = 0; i < num_stripes; i++) {
6323 map->stripes[i].physical =
6324 btrfs_stripe_offset_nr(leaf, chunk, i);
6325 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006326 read_extent_buffer(leaf, uuid, (unsigned long)
6327 btrfs_stripe_dev_uuid_nr(chunk, i),
6328 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006329 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6330 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006331 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006332 free_extent_map(em);
6333 return -EIO;
6334 }
Chris Masondfe25022008-05-13 13:46:40 -04006335 if (!map->stripes[i].dev) {
6336 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006337 add_missing_dev(root, root->fs_info->fs_devices,
6338 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006339 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006340 free_extent_map(em);
6341 return -EIO;
6342 }
Anand Jain816fceb2015-04-27 12:46:18 +08006343 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6344 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006345 }
6346 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006347 }
6348
Chris Mason890871b2009-09-02 16:24:52 -04006349 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006350 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006351 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006352 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006353 free_extent_map(em);
6354
6355 return 0;
6356}
6357
Jeff Mahoney143bede2012-03-01 14:56:26 +01006358static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006359 struct btrfs_dev_item *dev_item,
6360 struct btrfs_device *device)
6361{
6362 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006363
6364 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006365 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6366 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006367 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006368 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006369 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006370 device->type = btrfs_device_type(leaf, dev_item);
6371 device->io_align = btrfs_device_io_align(leaf, dev_item);
6372 device->io_width = btrfs_device_io_width(leaf, dev_item);
6373 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006374 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006375 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006376
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006377 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006378 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006379}
6380
Miao Xie5f375832014-09-03 21:35:46 +08006381static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6382 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006383{
6384 struct btrfs_fs_devices *fs_devices;
6385 int ret;
6386
Li Zefanb367e472011-12-07 11:38:24 +08006387 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006388
6389 fs_devices = root->fs_info->fs_devices->seed;
6390 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006391 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6392 return fs_devices;
6393
Yan Zheng2b820322008-11-17 21:11:30 -05006394 fs_devices = fs_devices->seed;
6395 }
6396
6397 fs_devices = find_fsid(fsid);
6398 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006399 if (!btrfs_test_opt(root, DEGRADED))
6400 return ERR_PTR(-ENOENT);
6401
6402 fs_devices = alloc_fs_devices(fsid);
6403 if (IS_ERR(fs_devices))
6404 return fs_devices;
6405
6406 fs_devices->seeding = 1;
6407 fs_devices->opened = 1;
6408 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006409 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006410
6411 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006412 if (IS_ERR(fs_devices))
6413 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006414
Christoph Hellwig97288f22008-12-02 06:36:09 -05006415 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006416 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006417 if (ret) {
6418 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006419 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006420 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006421 }
Yan Zheng2b820322008-11-17 21:11:30 -05006422
6423 if (!fs_devices->seeding) {
6424 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006425 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006426 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006427 goto out;
6428 }
6429
6430 fs_devices->seed = root->fs_info->fs_devices->seed;
6431 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006432out:
Miao Xie5f375832014-09-03 21:35:46 +08006433 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006434}
6435
Chris Mason0d81ba52008-03-24 15:02:07 -04006436static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006437 struct extent_buffer *leaf,
6438 struct btrfs_dev_item *dev_item)
6439{
Miao Xie5f375832014-09-03 21:35:46 +08006440 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006441 struct btrfs_device *device;
6442 u64 devid;
6443 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006444 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006445 u8 dev_uuid[BTRFS_UUID_SIZE];
6446
Chris Mason0b86a832008-03-24 15:01:56 -04006447 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006448 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006449 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006450 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006451 BTRFS_UUID_SIZE);
6452
6453 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006454 fs_devices = open_seed_devices(root, fs_uuid);
6455 if (IS_ERR(fs_devices))
6456 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006457 }
6458
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006459 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006460 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006461 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006462 return -EIO;
6463
Miao Xie5f375832014-09-03 21:35:46 +08006464 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6465 if (!device)
6466 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006467 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6468 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006469 } else {
6470 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6471 return -EIO;
6472
6473 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006474 /*
6475 * this happens when a device that was properly setup
6476 * in the device info lists suddenly goes bad.
6477 * device->bdev is NULL, and so we have to set
6478 * device->missing to one here
6479 */
Miao Xie5f375832014-09-03 21:35:46 +08006480 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006481 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006482 }
Miao Xie5f375832014-09-03 21:35:46 +08006483
6484 /* Move the device to its own fs_devices */
6485 if (device->fs_devices != fs_devices) {
6486 ASSERT(device->missing);
6487
6488 list_move(&device->dev_list, &fs_devices->devices);
6489 device->fs_devices->num_devices--;
6490 fs_devices->num_devices++;
6491
6492 device->fs_devices->missing_devices--;
6493 fs_devices->missing_devices++;
6494
6495 device->fs_devices = fs_devices;
6496 }
Yan Zheng2b820322008-11-17 21:11:30 -05006497 }
6498
6499 if (device->fs_devices != root->fs_info->fs_devices) {
6500 BUG_ON(device->writeable);
6501 if (device->generation !=
6502 btrfs_device_generation(leaf, dev_item))
6503 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006504 }
Chris Mason0b86a832008-03-24 15:01:56 -04006505
6506 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006507 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006508 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006509 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006510 spin_lock(&root->fs_info->free_chunk_lock);
6511 root->fs_info->free_chunk_space += device->total_bytes -
6512 device->bytes_used;
6513 spin_unlock(&root->fs_info->free_chunk_lock);
6514 }
Chris Mason0b86a832008-03-24 15:01:56 -04006515 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006516 return ret;
6517}
6518
Yan Zhenge4404d62008-12-12 10:03:26 -05006519int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006520{
David Sterba6c417612011-04-13 15:41:04 +02006521 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006522 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006523 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006524 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006525 u8 *array_ptr;
6526 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006527 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006528 u32 num_stripes;
6529 u32 array_size;
6530 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006531 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006532 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006533
David Sterbaa83fffb2014-06-15 02:39:54 +02006534 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6535 /*
6536 * This will create extent buffer of nodesize, superblock size is
6537 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6538 * overallocate but we can keep it as-is, only the first page is used.
6539 */
6540 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006541 if (!sb)
6542 return -ENOMEM;
David Sterba4db8c522015-12-03 13:06:46 +01006543 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006544 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006545 /*
6546 * The sb extent buffer is artifical and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006547 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006548 * pages up-to-date when the page is larger: extent does not cover the
6549 * whole page and consequently check_page_uptodate does not find all
6550 * the page's extents up-to-date (the hole beyond sb),
6551 * write_extent_buffer then triggers a WARN_ON.
6552 *
6553 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6554 * but sb spans only this function. Add an explicit SetPageUptodate call
6555 * to silence the warning eg. on PowerPC 64.
6556 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006557 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006558 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006559
Chris Masona061fc82008-05-07 11:43:44 -04006560 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006561 array_size = btrfs_super_sys_array_size(super_copy);
6562
David Sterba1ffb22c2014-10-31 19:02:42 +01006563 array_ptr = super_copy->sys_chunk_array;
6564 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6565 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006566
David Sterba1ffb22c2014-10-31 19:02:42 +01006567 while (cur_offset < array_size) {
6568 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006569 len = sizeof(*disk_key);
6570 if (cur_offset + len > array_size)
6571 goto out_short_read;
6572
Chris Mason0b86a832008-03-24 15:01:56 -04006573 btrfs_disk_key_to_cpu(&key, disk_key);
6574
David Sterba1ffb22c2014-10-31 19:02:42 +01006575 array_ptr += len;
6576 sb_array_offset += len;
6577 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006578
Chris Mason0d81ba52008-03-24 15:02:07 -04006579 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006580 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006581 /*
6582 * At least one btrfs_chunk with one stripe must be
6583 * present, exact stripe count check comes afterwards
6584 */
6585 len = btrfs_chunk_item_size(1);
6586 if (cur_offset + len > array_size)
6587 goto out_short_read;
6588
6589 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006590 if (!num_stripes) {
6591 printk(KERN_ERR
6592 "BTRFS: invalid number of stripes %u in sys_array at offset %u\n",
6593 num_stripes, cur_offset);
6594 ret = -EIO;
6595 break;
6596 }
6597
David Sterbae3540ea2014-11-05 15:24:51 +01006598 len = btrfs_chunk_item_size(num_stripes);
6599 if (cur_offset + len > array_size)
6600 goto out_short_read;
6601
Chris Mason0d81ba52008-03-24 15:02:07 -04006602 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006603 if (ret)
6604 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006605 } else {
David Sterba93a3d462015-11-30 17:27:09 +01006606 printk(KERN_ERR
6607 "BTRFS: unexpected item type %u in sys_array at offset %u\n",
6608 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006609 ret = -EIO;
6610 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006611 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006612 array_ptr += len;
6613 sb_array_offset += len;
6614 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006615 }
Chris Masona061fc82008-05-07 11:43:44 -04006616 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006617 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006618
6619out_short_read:
6620 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6621 len, cur_offset);
6622 free_extent_buffer(sb);
6623 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006624}
6625
6626int btrfs_read_chunk_tree(struct btrfs_root *root)
6627{
6628 struct btrfs_path *path;
6629 struct extent_buffer *leaf;
6630 struct btrfs_key key;
6631 struct btrfs_key found_key;
6632 int ret;
6633 int slot;
6634
6635 root = root->fs_info->chunk_root;
6636
6637 path = btrfs_alloc_path();
6638 if (!path)
6639 return -ENOMEM;
6640
Li Zefanb367e472011-12-07 11:38:24 +08006641 mutex_lock(&uuid_mutex);
6642 lock_chunks(root);
6643
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006644 /*
6645 * Read all device items, and then all the chunk items. All
6646 * device items are found before any chunk item (their object id
6647 * is smaller than the lowest possible object id for a chunk
6648 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006649 */
6650 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6651 key.offset = 0;
6652 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006653 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006654 if (ret < 0)
6655 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006656 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006657 leaf = path->nodes[0];
6658 slot = path->slots[0];
6659 if (slot >= btrfs_header_nritems(leaf)) {
6660 ret = btrfs_next_leaf(root, path);
6661 if (ret == 0)
6662 continue;
6663 if (ret < 0)
6664 goto error;
6665 break;
6666 }
6667 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006668 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6669 struct btrfs_dev_item *dev_item;
6670 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006671 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006672 ret = read_one_dev(root, leaf, dev_item);
6673 if (ret)
6674 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006675 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6676 struct btrfs_chunk *chunk;
6677 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6678 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006679 if (ret)
6680 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006681 }
6682 path->slots[0]++;
6683 }
Chris Mason0b86a832008-03-24 15:01:56 -04006684 ret = 0;
6685error:
Li Zefanb367e472011-12-07 11:38:24 +08006686 unlock_chunks(root);
6687 mutex_unlock(&uuid_mutex);
6688
Yan Zheng2b820322008-11-17 21:11:30 -05006689 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006690 return ret;
6691}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006692
Miao Xiecb517ea2013-05-15 07:48:19 +00006693void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6694{
6695 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6696 struct btrfs_device *device;
6697
Liu Bo29cc83f2014-05-11 23:14:59 +08006698 while (fs_devices) {
6699 mutex_lock(&fs_devices->device_list_mutex);
6700 list_for_each_entry(device, &fs_devices->devices, dev_list)
6701 device->dev_root = fs_info->dev_root;
6702 mutex_unlock(&fs_devices->device_list_mutex);
6703
6704 fs_devices = fs_devices->seed;
6705 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006706}
6707
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006708static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6709{
6710 int i;
6711
6712 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6713 btrfs_dev_stat_reset(dev, i);
6714}
6715
6716int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6717{
6718 struct btrfs_key key;
6719 struct btrfs_key found_key;
6720 struct btrfs_root *dev_root = fs_info->dev_root;
6721 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6722 struct extent_buffer *eb;
6723 int slot;
6724 int ret = 0;
6725 struct btrfs_device *device;
6726 struct btrfs_path *path = NULL;
6727 int i;
6728
6729 path = btrfs_alloc_path();
6730 if (!path) {
6731 ret = -ENOMEM;
6732 goto out;
6733 }
6734
6735 mutex_lock(&fs_devices->device_list_mutex);
6736 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6737 int item_size;
6738 struct btrfs_dev_stats_item *ptr;
6739
David Sterba242e2952016-01-25 17:51:31 +01006740 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6741 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006742 key.offset = device->devid;
6743 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6744 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006745 __btrfs_reset_dev_stats(device);
6746 device->dev_stats_valid = 1;
6747 btrfs_release_path(path);
6748 continue;
6749 }
6750 slot = path->slots[0];
6751 eb = path->nodes[0];
6752 btrfs_item_key_to_cpu(eb, &found_key, slot);
6753 item_size = btrfs_item_size_nr(eb, slot);
6754
6755 ptr = btrfs_item_ptr(eb, slot,
6756 struct btrfs_dev_stats_item);
6757
6758 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6759 if (item_size >= (1 + i) * sizeof(__le64))
6760 btrfs_dev_stat_set(device, i,
6761 btrfs_dev_stats_value(eb, ptr, i));
6762 else
6763 btrfs_dev_stat_reset(device, i);
6764 }
6765
6766 device->dev_stats_valid = 1;
6767 btrfs_dev_stat_print_on_load(device);
6768 btrfs_release_path(path);
6769 }
6770 mutex_unlock(&fs_devices->device_list_mutex);
6771
6772out:
6773 btrfs_free_path(path);
6774 return ret < 0 ? ret : 0;
6775}
6776
6777static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6778 struct btrfs_root *dev_root,
6779 struct btrfs_device *device)
6780{
6781 struct btrfs_path *path;
6782 struct btrfs_key key;
6783 struct extent_buffer *eb;
6784 struct btrfs_dev_stats_item *ptr;
6785 int ret;
6786 int i;
6787
David Sterba242e2952016-01-25 17:51:31 +01006788 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6789 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006790 key.offset = device->devid;
6791
6792 path = btrfs_alloc_path();
6793 BUG_ON(!path);
6794 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6795 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006796 btrfs_warn_in_rcu(dev_root->fs_info,
6797 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04006798 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006799 goto out;
6800 }
6801
6802 if (ret == 0 &&
6803 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6804 /* need to delete old one and insert a new one */
6805 ret = btrfs_del_item(trans, dev_root, path);
6806 if (ret != 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006807 btrfs_warn_in_rcu(dev_root->fs_info,
6808 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04006809 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006810 goto out;
6811 }
6812 ret = 1;
6813 }
6814
6815 if (ret == 1) {
6816 /* need to insert a new item */
6817 btrfs_release_path(path);
6818 ret = btrfs_insert_empty_item(trans, dev_root, path,
6819 &key, sizeof(*ptr));
6820 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006821 btrfs_warn_in_rcu(dev_root->fs_info,
6822 "insert dev_stats item for device %s failed %d",
6823 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006824 goto out;
6825 }
6826 }
6827
6828 eb = path->nodes[0];
6829 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6830 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6831 btrfs_set_dev_stats_value(eb, ptr, i,
6832 btrfs_dev_stat_read(device, i));
6833 btrfs_mark_buffer_dirty(eb);
6834
6835out:
6836 btrfs_free_path(path);
6837 return ret;
6838}
6839
6840/*
6841 * called from commit_transaction. Writes all changed device stats to disk.
6842 */
6843int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6844 struct btrfs_fs_info *fs_info)
6845{
6846 struct btrfs_root *dev_root = fs_info->dev_root;
6847 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6848 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006849 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006850 int ret = 0;
6851
6852 mutex_lock(&fs_devices->device_list_mutex);
6853 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006854 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006855 continue;
6856
Miao Xieaddc3fa2014-07-24 11:37:11 +08006857 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006858 ret = update_dev_stat_item(trans, dev_root, device);
6859 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006860 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006861 }
6862 mutex_unlock(&fs_devices->device_list_mutex);
6863
6864 return ret;
6865}
6866
Stefan Behrens442a4f62012-05-25 16:06:08 +02006867void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6868{
6869 btrfs_dev_stat_inc(dev, index);
6870 btrfs_dev_stat_print_on_error(dev);
6871}
6872
Eric Sandeen48a3b632013-04-25 20:41:01 +00006873static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006874{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006875 if (!dev->dev_stats_valid)
6876 return;
David Sterbab14af3b2015-10-08 10:43:10 +02006877 btrfs_err_rl_in_rcu(dev->dev_root->fs_info,
6878 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006879 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006880 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6881 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6882 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006883 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6884 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006885}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006886
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006887static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6888{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006889 int i;
6890
6891 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6892 if (btrfs_dev_stat_read(dev, i) != 0)
6893 break;
6894 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6895 return; /* all values == 0, suppress message */
6896
David Sterbaecaeb142015-10-08 09:01:03 +02006897 btrfs_info_in_rcu(dev->dev_root->fs_info,
6898 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006899 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006900 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6901 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6902 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6903 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6904 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6905}
6906
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006907int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006908 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006909{
6910 struct btrfs_device *dev;
6911 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6912 int i;
6913
6914 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006915 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006916 mutex_unlock(&fs_devices->device_list_mutex);
6917
6918 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006919 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006920 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006921 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006922 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006923 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006924 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006925 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6926 if (stats->nr_items > i)
6927 stats->values[i] =
6928 btrfs_dev_stat_read_and_reset(dev, i);
6929 else
6930 btrfs_dev_stat_reset(dev, i);
6931 }
6932 } else {
6933 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6934 if (stats->nr_items > i)
6935 stats->values[i] = btrfs_dev_stat_read(dev, i);
6936 }
6937 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6938 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6939 return 0;
6940}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006941
Anand Jain12b1c262015-08-14 18:32:59 +08006942void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006943{
6944 struct buffer_head *bh;
6945 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08006946 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006947
Anand Jain12b1c262015-08-14 18:32:59 +08006948 if (!bdev)
6949 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006950
Anand Jain12b1c262015-08-14 18:32:59 +08006951 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
6952 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006953
Anand Jain12b1c262015-08-14 18:32:59 +08006954 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
6955 continue;
6956
6957 disk_super = (struct btrfs_super_block *)bh->b_data;
6958
6959 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6960 set_buffer_dirty(bh);
6961 sync_dirty_buffer(bh);
6962 brelse(bh);
6963 }
6964
6965 /* Notify udev that device has changed */
6966 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
6967
6968 /* Update ctime/mtime for device path for libblkid */
6969 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006970}
Miao Xie935e5cc2014-09-03 21:35:33 +08006971
6972/*
6973 * Update the size of all devices, which is used for writing out the
6974 * super blocks.
6975 */
6976void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6977{
6978 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6979 struct btrfs_device *curr, *next;
6980
6981 if (list_empty(&fs_devices->resized_devices))
6982 return;
6983
6984 mutex_lock(&fs_devices->device_list_mutex);
6985 lock_chunks(fs_info->dev_root);
6986 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6987 resized_list) {
6988 list_del_init(&curr->resized_list);
6989 curr->commit_total_bytes = curr->disk_total_bytes;
6990 }
6991 unlock_chunks(fs_info->dev_root);
6992 mutex_unlock(&fs_devices->device_list_mutex);
6993}
Miao Xiece7213c2014-09-03 21:35:34 +08006994
6995/* Must be invoked during the transaction commit */
6996void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6997 struct btrfs_transaction *transaction)
6998{
6999 struct extent_map *em;
7000 struct map_lookup *map;
7001 struct btrfs_device *dev;
7002 int i;
7003
7004 if (list_empty(&transaction->pending_chunks))
7005 return;
7006
7007 /* In order to kick the device replace finish process */
7008 lock_chunks(root);
7009 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007010 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007011
7012 for (i = 0; i < map->num_stripes; i++) {
7013 dev = map->stripes[i].dev;
7014 dev->commit_bytes_used = dev->bytes_used;
7015 }
7016 }
7017 unlock_chunks(root);
7018}
Anand Jain5a13f432015-03-10 06:38:31 +08007019
7020void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7021{
7022 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7023 while (fs_devices) {
7024 fs_devices->fs_info = fs_info;
7025 fs_devices = fs_devices->seed;
7026 }
7027}
7028
7029void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7030{
7031 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7032 while (fs_devices) {
7033 fs_devices->fs_info = NULL;
7034 fs_devices = fs_devices->seed;
7035 }
7036}
Anand Jainf190aa42015-08-14 18:33:05 +08007037
David Sterba87ad58c2015-11-27 18:49:39 +01007038static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf190aa42015-08-14 18:33:05 +08007039{
7040 struct btrfs_fs_devices *fs_devices = device->fs_devices;
7041 struct btrfs_device *new_device;
7042 struct rcu_string *name;
7043
7044 if (device->bdev)
7045 fs_devices->open_devices--;
7046
7047 if (device->writeable &&
7048 device->devid != BTRFS_DEV_REPLACE_DEVID) {
7049 list_del_init(&device->dev_alloc_list);
7050 fs_devices->rw_devices--;
7051 }
7052
7053 if (device->missing)
7054 fs_devices->missing_devices--;
7055
7056 new_device = btrfs_alloc_device(NULL, &device->devid,
7057 device->uuid);
7058 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
7059
7060 /* Safe because we are under uuid_mutex */
7061 if (device->name) {
7062 name = rcu_string_strdup(device->name->str, GFP_NOFS);
7063 BUG_ON(!name); /* -ENOMEM */
7064 rcu_assign_pointer(new_device->name, name);
7065 }
7066
7067 list_replace_rcu(&device->dev_list, &new_device->dev_list);
7068 new_device->fs_devices = device->fs_devices;
7069
7070 call_rcu(&device->rcu, free_device);
7071}