blob: 5006683283ae5c77be7b142f9b17e34fd79ab809 [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
Anand Jain6cf86a002016-02-13 10:01:29 +0800991void btrfs_release_disk_super(struct page *page)
992{
993 kunmap(page);
994 put_page(page);
995}
996
997int btrfs_read_disk_super(struct block_device *bdev, u64 bytenr,
998 struct page **page, struct btrfs_super_block **disk_super)
999{
1000 void *p;
1001 pgoff_t index;
1002
1003 /* make sure our super fits in the device */
1004 if (bytenr + PAGE_SIZE >= i_size_read(bdev->bd_inode))
1005 return 1;
1006
1007 /* make sure our super fits in the page */
1008 if (sizeof(**disk_super) > PAGE_SIZE)
1009 return 1;
1010
1011 /* make sure our super doesn't straddle pages on disk */
1012 index = bytenr >> PAGE_SHIFT;
1013 if ((bytenr + sizeof(**disk_super) - 1) >> PAGE_SHIFT != index)
1014 return 1;
1015
1016 /* pull in the page with our super */
1017 *page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1018 index, GFP_KERNEL);
1019
1020 if (IS_ERR_OR_NULL(*page))
1021 return 1;
1022
1023 p = kmap(*page);
1024
1025 /* align our pointer to the offset of the super block */
1026 *disk_super = p + (bytenr & ~PAGE_MASK);
1027
1028 if (btrfs_super_bytenr(*disk_super) != bytenr ||
1029 btrfs_super_magic(*disk_super) != BTRFS_MAGIC) {
1030 btrfs_release_disk_super(*page);
1031 return 1;
1032 }
1033
1034 if ((*disk_super)->label[0] &&
1035 (*disk_super)->label[BTRFS_LABEL_SIZE - 1])
1036 (*disk_super)->label[BTRFS_LABEL_SIZE - 1] = '\0';
1037
1038 return 0;
1039}
1040
David Sterba6f60cbd2013-02-15 11:31:02 -07001041/*
1042 * Look for a btrfs signature on a device. This may be called out of the mount path
1043 * and we are not allowed to call set_blocksize during the scan. The superblock
1044 * is read via pagecache
1045 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001046int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001047 struct btrfs_fs_devices **fs_devices_ret)
1048{
1049 struct btrfs_super_block *disk_super;
1050 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001051 struct page *page;
David Sterba6f60cbd2013-02-15 11:31:02 -07001052 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001053 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001054 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001055 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001056 u64 bytenr;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001057
David Sterba6f60cbd2013-02-15 11:31:02 -07001058 /*
1059 * we would like to check all the supers, but that would make
1060 * a btrfs mount succeed after a mkfs from a different FS.
1061 * So, we need to add a special mount option to scan for
1062 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1063 */
1064 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001065 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001066 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001067
1068 bdev = blkdev_get_by_path(path, flags, holder);
David Sterba6f60cbd2013-02-15 11:31:02 -07001069 if (IS_ERR(bdev)) {
1070 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001071 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001072 }
1073
Anand Jain6cf86a002016-02-13 10:01:29 +08001074 if (btrfs_read_disk_super(bdev, bytenr, &page, &disk_super))
David Sterba6f60cbd2013-02-15 11:31:02 -07001075 goto error_bdev_put;
1076
Xiao Guangronga3438322010-01-06 11:48:18 +00001077 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001078 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001079 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001080
Chris Mason8a4b83c2008-03-24 15:02:07 -04001081 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001082 if (ret > 0) {
1083 if (disk_super->label[0]) {
David Sterba60999ca2014-03-26 18:26:36 +01001084 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1085 } else {
1086 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1087 }
1088
1089 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1090 ret = 0;
1091 }
Josef Bacik02db0842012-06-21 16:03:58 -04001092 if (!ret && fs_devices_ret)
1093 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001094
Anand Jain6cf86a002016-02-13 10:01:29 +08001095 btrfs_release_disk_super(page);
David Sterba6f60cbd2013-02-15 11:31:02 -07001096
1097error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001098 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001099error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001100 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001101 return ret;
1102}
Chris Mason0b86a832008-03-24 15:01:56 -04001103
Miao Xie6d07bce2011-01-05 10:07:31 +00001104/* helper to account the used device space in the range */
1105int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1106 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001107{
1108 struct btrfs_key key;
1109 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001110 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001111 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001112 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001113 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001114 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001115 struct extent_buffer *l;
1116
Miao Xie6d07bce2011-01-05 10:07:31 +00001117 *length = 0;
1118
Stefan Behrens63a212a2012-11-05 18:29:28 +01001119 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001120 return 0;
1121
Yan Zheng2b820322008-11-17 21:11:30 -05001122 path = btrfs_alloc_path();
1123 if (!path)
1124 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01001125 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04001126
Chris Mason0b86a832008-03-24 15:01:56 -04001127 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001128 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001129 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001130
1131 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001132 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001133 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001134 if (ret > 0) {
1135 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1136 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001137 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001138 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001139
Chris Mason0b86a832008-03-24 15:01:56 -04001140 while (1) {
1141 l = path->nodes[0];
1142 slot = path->slots[0];
1143 if (slot >= btrfs_header_nritems(l)) {
1144 ret = btrfs_next_leaf(root, path);
1145 if (ret == 0)
1146 continue;
1147 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001148 goto out;
1149
1150 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001151 }
1152 btrfs_item_key_to_cpu(l, &key, slot);
1153
1154 if (key.objectid < device->devid)
1155 goto next;
1156
1157 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001158 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001159
David Sterba962a2982014-06-04 18:41:45 +02001160 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001161 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001162
Chris Mason0b86a832008-03-24 15:01:56 -04001163 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001164 extent_end = key.offset + btrfs_dev_extent_length(l,
1165 dev_extent);
1166 if (key.offset <= start && extent_end > end) {
1167 *length = end - start + 1;
1168 break;
1169 } else if (key.offset <= start && extent_end > start)
1170 *length += extent_end - start;
1171 else if (key.offset > start && extent_end <= end)
1172 *length += extent_end - key.offset;
1173 else if (key.offset > start && key.offset <= end) {
1174 *length += end - key.offset + 1;
1175 break;
1176 } else if (key.offset > end)
1177 break;
1178
1179next:
1180 path->slots[0]++;
1181 }
1182 ret = 0;
1183out:
1184 btrfs_free_path(path);
1185 return ret;
1186}
1187
Jeff Mahoney499f3772015-06-15 09:41:17 -04001188static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001189 struct btrfs_device *device,
1190 u64 *start, u64 len)
1191{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001192 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001193 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001194 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001195 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001196 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001197
Jeff Mahoney499f3772015-06-15 09:41:17 -04001198 if (transaction)
1199 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001200again:
1201 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001202 struct map_lookup *map;
1203 int i;
1204
Jeff Mahoney95617d62015-06-03 10:55:48 -04001205 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04001206 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001207 u64 end;
1208
Josef Bacik6df9a952013-06-27 13:22:46 -04001209 if (map->stripes[i].dev != device)
1210 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001211 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001212 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001213 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001214 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001215 /*
1216 * Make sure that while processing the pinned list we do
1217 * not override our *start with a lower value, because
1218 * we can have pinned chunks that fall within this
1219 * device hole and that have lower physical addresses
1220 * than the pending chunks we processed before. If we
1221 * do not take this special care we can end up getting
1222 * 2 pending chunks that start at the same physical
1223 * device offsets because the end offset of a pinned
1224 * chunk can be equal to the start offset of some
1225 * pending chunk.
1226 */
1227 end = map->stripes[i].physical + em->orig_block_len;
1228 if (end > *start) {
1229 *start = end;
1230 ret = 1;
1231 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001232 }
1233 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001234 if (search_list != &fs_info->pinned_chunks) {
1235 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001236 goto again;
1237 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001238
1239 return ret;
1240}
1241
1242
Chris Mason0b86a832008-03-24 15:01:56 -04001243/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001244 * find_free_dev_extent_start - find free space in the specified device
1245 * @device: the device which we search the free space in
1246 * @num_bytes: the size of the free space that we need
1247 * @search_start: the position from which to begin the search
1248 * @start: store the start of the free space.
1249 * @len: the size of the free space. that we find, or the size
1250 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001251 *
Chris Mason0b86a832008-03-24 15:01:56 -04001252 * this uses a pretty simple search, the expectation is that it is
1253 * called very infrequently and that a given device has a small number
1254 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001255 *
1256 * @start is used to store the start of the free space if we find. But if we
1257 * don't find suitable free space, it will be used to store the start position
1258 * of the max free space.
1259 *
1260 * @len is used to store the size of the free space that we find.
1261 * But if we don't find suitable free space, it is used to store the size of
1262 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001263 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001264int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1265 struct btrfs_device *device, u64 num_bytes,
1266 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001267{
1268 struct btrfs_key key;
1269 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001270 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001271 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001272 u64 hole_size;
1273 u64 max_hole_start;
1274 u64 max_hole_size;
1275 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001276 u64 search_end = device->total_bytes;
1277 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001278 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001279 struct extent_buffer *l;
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001280 u64 min_search_start;
1281
1282 /*
1283 * We don't want to overwrite the superblock on the drive nor any area
1284 * used by the boot loader (grub for example), so we make sure to start
1285 * at an offset of at least 1MB.
1286 */
1287 min_search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1288 search_start = max(search_start, min_search_start);
Chris Mason0b86a832008-03-24 15:01:56 -04001289
Josef Bacik6df9a952013-06-27 13:22:46 -04001290 path = btrfs_alloc_path();
1291 if (!path)
1292 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001293
Miao Xie7bfc8372011-01-05 10:07:26 +00001294 max_hole_start = search_start;
1295 max_hole_size = 0;
1296
Zhao Leif2ab7612015-02-16 18:52:17 +08001297again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001298 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001299 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001300 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001301 }
1302
David Sterbae4058b52015-11-27 16:31:35 +01001303 path->reada = READA_FORWARD;
Josef Bacik6df9a952013-06-27 13:22:46 -04001304 path->search_commit_root = 1;
1305 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001306
Chris Mason0b86a832008-03-24 15:01:56 -04001307 key.objectid = device->devid;
1308 key.offset = search_start;
1309 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001310
Li Zefan125ccb02011-12-08 15:07:24 +08001311 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001312 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001313 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001314 if (ret > 0) {
1315 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1316 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001317 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001318 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001319
Chris Mason0b86a832008-03-24 15:01:56 -04001320 while (1) {
1321 l = path->nodes[0];
1322 slot = path->slots[0];
1323 if (slot >= btrfs_header_nritems(l)) {
1324 ret = btrfs_next_leaf(root, path);
1325 if (ret == 0)
1326 continue;
1327 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001328 goto out;
1329
1330 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001331 }
1332 btrfs_item_key_to_cpu(l, &key, slot);
1333
1334 if (key.objectid < device->devid)
1335 goto next;
1336
1337 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001338 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001339
David Sterba962a2982014-06-04 18:41:45 +02001340 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001341 goto next;
1342
Miao Xie7bfc8372011-01-05 10:07:26 +00001343 if (key.offset > search_start) {
1344 hole_size = key.offset - search_start;
1345
Josef Bacik6df9a952013-06-27 13:22:46 -04001346 /*
1347 * Have to check before we set max_hole_start, otherwise
1348 * we could end up sending back this offset anyway.
1349 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001350 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001351 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001352 hole_size)) {
1353 if (key.offset >= search_start) {
1354 hole_size = key.offset - search_start;
1355 } else {
1356 WARN_ON_ONCE(1);
1357 hole_size = 0;
1358 }
1359 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001360
Miao Xie7bfc8372011-01-05 10:07:26 +00001361 if (hole_size > max_hole_size) {
1362 max_hole_start = search_start;
1363 max_hole_size = hole_size;
1364 }
1365
1366 /*
1367 * If this free space is greater than which we need,
1368 * it must be the max free space that we have found
1369 * until now, so max_hole_start must point to the start
1370 * of this free space and the length of this free space
1371 * is stored in max_hole_size. Thus, we return
1372 * max_hole_start and max_hole_size and go back to the
1373 * caller.
1374 */
1375 if (hole_size >= num_bytes) {
1376 ret = 0;
1377 goto out;
1378 }
1379 }
1380
Chris Mason0b86a832008-03-24 15:01:56 -04001381 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001382 extent_end = key.offset + btrfs_dev_extent_length(l,
1383 dev_extent);
1384 if (extent_end > search_start)
1385 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001386next:
1387 path->slots[0]++;
1388 cond_resched();
1389 }
Chris Mason0b86a832008-03-24 15:01:56 -04001390
liubo38c01b92011-08-02 02:39:03 +00001391 /*
1392 * At this point, search_start should be the end of
1393 * allocated dev extents, and when shrinking the device,
1394 * search_end may be smaller than search_start.
1395 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001396 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001397 hole_size = search_end - search_start;
1398
Jeff Mahoney499f3772015-06-15 09:41:17 -04001399 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001400 hole_size)) {
1401 btrfs_release_path(path);
1402 goto again;
1403 }
Chris Mason0b86a832008-03-24 15:01:56 -04001404
Zhao Leif2ab7612015-02-16 18:52:17 +08001405 if (hole_size > max_hole_size) {
1406 max_hole_start = search_start;
1407 max_hole_size = hole_size;
1408 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001409 }
1410
Miao Xie7bfc8372011-01-05 10:07:26 +00001411 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001412 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001413 ret = -ENOSPC;
1414 else
1415 ret = 0;
1416
1417out:
Yan Zheng2b820322008-11-17 21:11:30 -05001418 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001419 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001420 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001421 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001422 return ret;
1423}
1424
Jeff Mahoney499f3772015-06-15 09:41:17 -04001425int find_free_dev_extent(struct btrfs_trans_handle *trans,
1426 struct btrfs_device *device, u64 num_bytes,
1427 u64 *start, u64 *len)
1428{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001429 /* FIXME use last free of some kind */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001430 return find_free_dev_extent_start(trans->transaction, device,
Filipe Manana8cdc7c52016-01-06 22:42:35 +00001431 num_bytes, 0, start, len);
Jeff Mahoney499f3772015-06-15 09:41:17 -04001432}
1433
Christoph Hellwigb2950862008-12-02 09:54:17 -05001434static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001435 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001436 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001437{
1438 int ret;
1439 struct btrfs_path *path;
1440 struct btrfs_root *root = device->dev_root;
1441 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001442 struct btrfs_key found_key;
1443 struct extent_buffer *leaf = NULL;
1444 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001445
1446 path = btrfs_alloc_path();
1447 if (!path)
1448 return -ENOMEM;
1449
1450 key.objectid = device->devid;
1451 key.offset = start;
1452 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001453again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001454 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001455 if (ret > 0) {
1456 ret = btrfs_previous_item(root, path, key.objectid,
1457 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001458 if (ret)
1459 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001460 leaf = path->nodes[0];
1461 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1462 extent = btrfs_item_ptr(leaf, path->slots[0],
1463 struct btrfs_dev_extent);
1464 BUG_ON(found_key.offset > start || found_key.offset +
1465 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001466 key = found_key;
1467 btrfs_release_path(path);
1468 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001469 } else if (ret == 0) {
1470 leaf = path->nodes[0];
1471 extent = btrfs_item_ptr(leaf, path->slots[0],
1472 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473 } else {
Anand Jaina4553fe2015-09-25 14:43:01 +08001474 btrfs_std_error(root->fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001475 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001476 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001477
Miao Xie2196d6e2014-09-03 21:35:41 +08001478 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1479
Chris Mason8f18cf12008-04-25 16:53:30 -04001480 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001481 if (ret) {
Anand Jaina4553fe2015-09-25 14:43:01 +08001482 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001483 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001484 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001485 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001486 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001487out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001488 btrfs_free_path(path);
1489 return ret;
1490}
1491
Eric Sandeen48a3b632013-04-25 20:41:01 +00001492static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1493 struct btrfs_device *device,
1494 u64 chunk_tree, u64 chunk_objectid,
1495 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001496{
1497 int ret;
1498 struct btrfs_path *path;
1499 struct btrfs_root *root = device->dev_root;
1500 struct btrfs_dev_extent *extent;
1501 struct extent_buffer *leaf;
1502 struct btrfs_key key;
1503
Chris Masondfe25022008-05-13 13:46:40 -04001504 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001505 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001506 path = btrfs_alloc_path();
1507 if (!path)
1508 return -ENOMEM;
1509
Chris Mason0b86a832008-03-24 15:01:56 -04001510 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001511 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001512 key.type = BTRFS_DEV_EXTENT_KEY;
1513 ret = btrfs_insert_empty_item(trans, root, path, &key,
1514 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001515 if (ret)
1516 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001517
1518 leaf = path->nodes[0];
1519 extent = btrfs_item_ptr(leaf, path->slots[0],
1520 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001521 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1522 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1523 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1524
1525 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001526 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001527
Chris Mason0b86a832008-03-24 15:01:56 -04001528 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1529 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001530out:
Chris Mason0b86a832008-03-24 15:01:56 -04001531 btrfs_free_path(path);
1532 return ret;
1533}
1534
Josef Bacik6df9a952013-06-27 13:22:46 -04001535static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001536{
Josef Bacik6df9a952013-06-27 13:22:46 -04001537 struct extent_map_tree *em_tree;
1538 struct extent_map *em;
1539 struct rb_node *n;
1540 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001541
Josef Bacik6df9a952013-06-27 13:22:46 -04001542 em_tree = &fs_info->mapping_tree.map_tree;
1543 read_lock(&em_tree->lock);
1544 n = rb_last(&em_tree->map);
1545 if (n) {
1546 em = rb_entry(n, struct extent_map, rb_node);
1547 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001548 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001549 read_unlock(&em_tree->lock);
1550
Chris Mason0b86a832008-03-24 15:01:56 -04001551 return ret;
1552}
1553
Ilya Dryomov53f10652013-08-12 14:33:01 +03001554static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1555 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001556{
1557 int ret;
1558 struct btrfs_key key;
1559 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001560 struct btrfs_path *path;
1561
Yan Zheng2b820322008-11-17 21:11:30 -05001562 path = btrfs_alloc_path();
1563 if (!path)
1564 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001565
1566 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1567 key.type = BTRFS_DEV_ITEM_KEY;
1568 key.offset = (u64)-1;
1569
Ilya Dryomov53f10652013-08-12 14:33:01 +03001570 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001571 if (ret < 0)
1572 goto error;
1573
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001574 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001575
Ilya Dryomov53f10652013-08-12 14:33:01 +03001576 ret = btrfs_previous_item(fs_info->chunk_root, path,
1577 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001578 BTRFS_DEV_ITEM_KEY);
1579 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001580 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001581 } else {
1582 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1583 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001584 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001585 }
1586 ret = 0;
1587error:
Yan Zheng2b820322008-11-17 21:11:30 -05001588 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001589 return ret;
1590}
1591
1592/*
1593 * the device information is stored in the chunk root
1594 * the btrfs_device struct should be fully filled in
1595 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001596static int btrfs_add_device(struct btrfs_trans_handle *trans,
1597 struct btrfs_root *root,
1598 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001599{
1600 int ret;
1601 struct btrfs_path *path;
1602 struct btrfs_dev_item *dev_item;
1603 struct extent_buffer *leaf;
1604 struct btrfs_key key;
1605 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001606
1607 root = root->fs_info->chunk_root;
1608
1609 path = btrfs_alloc_path();
1610 if (!path)
1611 return -ENOMEM;
1612
Chris Mason0b86a832008-03-24 15:01:56 -04001613 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1614 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001615 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001616
1617 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001618 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001619 if (ret)
1620 goto out;
1621
1622 leaf = path->nodes[0];
1623 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1624
1625 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001626 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001627 btrfs_set_device_type(leaf, dev_item, device->type);
1628 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1629 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1630 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001631 btrfs_set_device_total_bytes(leaf, dev_item,
1632 btrfs_device_get_disk_total_bytes(device));
1633 btrfs_set_device_bytes_used(leaf, dev_item,
1634 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001635 btrfs_set_device_group(leaf, dev_item, 0);
1636 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1637 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001638 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001639
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001640 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001641 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001642 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001643 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001644 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001645
Yan Zheng2b820322008-11-17 21:11:30 -05001646 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001647out:
1648 btrfs_free_path(path);
1649 return ret;
1650}
Chris Mason8f18cf12008-04-25 16:53:30 -04001651
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001652/*
1653 * Function to update ctime/mtime for a given device path.
1654 * Mainly used for ctime/mtime based probe like libblkid.
1655 */
1656static void update_dev_time(char *path_name)
1657{
1658 struct file *filp;
1659
1660 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001661 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001662 return;
1663 file_update_time(filp);
1664 filp_close(filp, NULL);
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001665}
1666
Chris Masona061fc82008-05-07 11:43:44 -04001667static int btrfs_rm_dev_item(struct btrfs_root *root,
1668 struct btrfs_device *device)
1669{
1670 int ret;
1671 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001672 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001673 struct btrfs_trans_handle *trans;
1674
1675 root = root->fs_info->chunk_root;
1676
1677 path = btrfs_alloc_path();
1678 if (!path)
1679 return -ENOMEM;
1680
Yan, Zhenga22285a2010-05-16 10:48:46 -04001681 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001682 if (IS_ERR(trans)) {
1683 btrfs_free_path(path);
1684 return PTR_ERR(trans);
1685 }
Chris Masona061fc82008-05-07 11:43:44 -04001686 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1687 key.type = BTRFS_DEV_ITEM_KEY;
1688 key.offset = device->devid;
1689
1690 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1691 if (ret < 0)
1692 goto out;
1693
1694 if (ret > 0) {
1695 ret = -ENOENT;
1696 goto out;
1697 }
1698
1699 ret = btrfs_del_item(trans, root, path);
1700 if (ret)
1701 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001702out:
1703 btrfs_free_path(path);
1704 btrfs_commit_transaction(trans, root);
1705 return ret;
1706}
1707
1708int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1709{
1710 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001711 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001712 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001713 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001714 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001715 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001716 u64 all_avail;
1717 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001718 u64 num_devices;
1719 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001720 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001721 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001722 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001723
Chris Masona061fc82008-05-07 11:43:44 -04001724 mutex_lock(&uuid_mutex);
1725
Miao Xiede98ced2013-01-29 10:13:12 +00001726 do {
1727 seq = read_seqbegin(&root->fs_info->profiles_lock);
1728
1729 all_avail = root->fs_info->avail_data_alloc_bits |
1730 root->fs_info->avail_system_alloc_bits |
1731 root->fs_info->avail_metadata_alloc_bits;
1732 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001733
Stefan Behrens8dabb742012-11-06 13:15:27 +01001734 num_devices = root->fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08001735 btrfs_dev_replace_lock(&root->fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01001736 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1737 WARN_ON(num_devices < 1);
1738 num_devices--;
1739 }
Liu Bo73beece2015-07-17 16:49:19 +08001740 btrfs_dev_replace_unlock(&root->fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01001741
1742 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001743 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001744 goto out;
1745 }
1746
Stefan Behrens8dabb742012-11-06 13:15:27 +01001747 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001748 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001749 goto out;
1750 }
1751
David Woodhouse53b381b2013-01-29 18:40:14 -05001752 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1753 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001754 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001755 goto out;
1756 }
1757 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1758 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001759 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001760 goto out;
1761 }
1762
Chris Masondfe25022008-05-13 13:46:40 -04001763 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001764 struct list_head *devices;
1765 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001766
Chris Masondfe25022008-05-13 13:46:40 -04001767 device = NULL;
1768 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001769 /*
1770 * It is safe to read the devices since the volume_mutex
1771 * is held.
1772 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001773 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001774 if (tmp->in_fs_metadata &&
1775 !tmp->is_tgtdev_for_dev_replace &&
1776 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001777 device = tmp;
1778 break;
1779 }
1780 }
1781 bdev = NULL;
1782 bh = NULL;
1783 disk_super = NULL;
1784 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001785 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001786 goto out;
1787 }
Chris Masondfe25022008-05-13 13:46:40 -04001788 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001789 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001790 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001791 root->fs_info->bdev_holder, 0,
1792 &bdev, &bh);
1793 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001794 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001795 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001796 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001797 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001798 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001799 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001800 if (!device) {
1801 ret = -ENOENT;
1802 goto error_brelse;
1803 }
Chris Masondfe25022008-05-13 13:46:40 -04001804 }
Yan Zheng2b820322008-11-17 21:11:30 -05001805
Stefan Behrens63a212a2012-11-05 18:29:28 +01001806 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001807 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001808 goto error_brelse;
1809 }
1810
Yan Zheng2b820322008-11-17 21:11:30 -05001811 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001812 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001813 goto error_brelse;
1814 }
1815
1816 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001817 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001818 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001819 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001820 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001821 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001822 }
Chris Masona061fc82008-05-07 11:43:44 -04001823
Carey Underwoodd7901552013-03-04 16:37:06 -06001824 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001825 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001826 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001827 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001828 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001829
Stefan Behrens63a212a2012-11-05 18:29:28 +01001830 /*
1831 * TODO: the superblock still includes this device in its num_devices
1832 * counter although write_all_supers() is not locked out. This
1833 * could give a filesystem state which requires a degraded mount.
1834 */
Chris Masona061fc82008-05-07 11:43:44 -04001835 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1836 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001837 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001838
Yan Zheng2b820322008-11-17 21:11:30 -05001839 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001840 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001841
1842 /*
1843 * the device list mutex makes sure that we don't change
1844 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001845 * the device supers. Whoever is writing all supers, should
1846 * lock the device list mutex before getting the number of
1847 * devices in the super block (super_copy). Conversely,
1848 * whoever updates the number of devices in the super block
1849 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001850 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001851
1852 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001853 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001854 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001855
Yan Zhenge4404d62008-12-12 10:03:26 -05001856 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001857 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001858
Chris Masoncd02dca2010-12-13 14:56:23 -05001859 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001860 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001861
Yan Zheng2b820322008-11-17 21:11:30 -05001862 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1863 struct btrfs_device, dev_list);
1864 if (device->bdev == root->fs_info->sb->s_bdev)
1865 root->fs_info->sb->s_bdev = next_device->bdev;
1866 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1867 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1868
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001869 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001870 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001871 /* remove sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +08001872 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001873 }
Anand Jain99994cd2014-06-03 11:36:00 +08001874
Xiao Guangrong1f781602011-04-20 10:09:16 +00001875 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001876
David Sterba6c417612011-04-13 15:41:04 +02001877 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1878 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001879 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001880
Xiao Guangrong1f781602011-04-20 10:09:16 +00001881 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001882 struct btrfs_fs_devices *fs_devices;
1883 fs_devices = root->fs_info->fs_devices;
1884 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001885 if (fs_devices->seed == cur_devices) {
1886 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001887 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001888 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001889 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001890 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001891 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001892 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001893 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001894 }
1895
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001896 root->fs_info->num_tolerated_disk_barrier_failures =
1897 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1898
Yan Zheng2b820322008-11-17 21:11:30 -05001899 /*
1900 * at this point, the device is zero sized. We want to
1901 * remove it from the devices list and zero out the old super
1902 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001903 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001904 u64 bytenr;
1905 int i;
1906
Chris Masondfe25022008-05-13 13:46:40 -04001907 /* make sure this device isn't detected as part of
1908 * the FS anymore
1909 */
1910 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1911 set_buffer_dirty(bh);
1912 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001913
1914 /* clear the mirror copies of super block on the disk
1915 * being removed, 0th copy is been taken care above and
1916 * the below would take of the rest
1917 */
1918 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1919 bytenr = btrfs_sb_offset(i);
1920 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1921 i_size_read(bdev->bd_inode))
1922 break;
1923
1924 brelse(bh);
1925 bh = __bread(bdev, bytenr / 4096,
1926 BTRFS_SUPER_INFO_SIZE);
1927 if (!bh)
1928 continue;
1929
1930 disk_super = (struct btrfs_super_block *)bh->b_data;
1931
1932 if (btrfs_super_bytenr(disk_super) != bytenr ||
1933 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1934 continue;
1935 }
1936 memset(&disk_super->magic, 0,
1937 sizeof(disk_super->magic));
1938 set_buffer_dirty(bh);
1939 sync_dirty_buffer(bh);
1940 }
Chris Masondfe25022008-05-13 13:46:40 -04001941 }
Chris Masona061fc82008-05-07 11:43:44 -04001942
Chris Masona061fc82008-05-07 11:43:44 -04001943 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001944
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001945 if (bdev) {
1946 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001947 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001948
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001949 /* Update ctime/mtime for device path for libblkid */
1950 update_dev_time(device_path);
1951 }
1952
Chris Masona061fc82008-05-07 11:43:44 -04001953error_brelse:
1954 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001955 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001956 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001957out:
1958 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001959 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001960error_undo:
1961 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001962 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001963 list_add(&device->dev_alloc_list,
1964 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001965 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001966 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001967 }
1968 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001969}
1970
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001971void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1972 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001973{
Anand Jaind51908c2014-08-13 14:24:19 +08001974 struct btrfs_fs_devices *fs_devices;
1975
Stefan Behrense93c89c2012-11-05 17:33:06 +01001976 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001977
Anand Jain25e8e912014-08-20 10:56:56 +08001978 /*
1979 * in case of fs with no seed, srcdev->fs_devices will point
1980 * to fs_devices of fs_info. However when the dev being replaced is
1981 * a seed dev it will point to the seed's local fs_devices. In short
1982 * srcdev will have its correct fs_devices in both the cases.
1983 */
1984 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001985
Stefan Behrense93c89c2012-11-05 17:33:06 +01001986 list_del_rcu(&srcdev->dev_list);
1987 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001988 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001989 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001990 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001991
Miao Xie82372bc2014-09-03 21:35:44 +08001992 if (srcdev->writeable) {
1993 fs_devices->rw_devices--;
1994 /* zero out the old super if it is writable */
David Sterba31388ab2015-11-19 11:35:17 +01001995 btrfs_scratch_superblocks(srcdev->bdev, srcdev->name->str);
Ilya Dryomov13572722013-10-02 20:41:01 +03001996 }
1997
Miao Xie82372bc2014-09-03 21:35:44 +08001998 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001999 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002000}
2001
2002void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2003 struct btrfs_device *srcdev)
2004{
2005 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002006
Stefan Behrense93c89c2012-11-05 17:33:06 +01002007 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002008
2009 /*
2010 * unless fs_devices is seed fs, num_devices shouldn't go
2011 * zero
2012 */
2013 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
2014
2015 /* if this is no devs we rather delete the fs_devices */
2016 if (!fs_devices->num_devices) {
2017 struct btrfs_fs_devices *tmp_fs_devices;
2018
2019 tmp_fs_devices = fs_info->fs_devices;
2020 while (tmp_fs_devices) {
2021 if (tmp_fs_devices->seed == fs_devices) {
2022 tmp_fs_devices->seed = fs_devices->seed;
2023 break;
2024 }
2025 tmp_fs_devices = tmp_fs_devices->seed;
2026 }
2027 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002028 __btrfs_close_devices(fs_devices);
2029 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002030 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002031}
2032
2033void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2034 struct btrfs_device *tgtdev)
2035{
2036 struct btrfs_device *next_device;
2037
Miao Xie67a2c452014-09-03 21:35:43 +08002038 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002039 WARN_ON(!tgtdev);
2040 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002041
Anand Jain32576042015-08-14 18:32:49 +08002042 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002043
Stefan Behrense93c89c2012-11-05 17:33:06 +01002044 if (tgtdev->bdev) {
David Sterba31388ab2015-11-19 11:35:17 +01002045 btrfs_scratch_superblocks(tgtdev->bdev, tgtdev->name->str);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002046 fs_info->fs_devices->open_devices--;
2047 }
2048 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002049
2050 next_device = list_entry(fs_info->fs_devices->devices.next,
2051 struct btrfs_device, dev_list);
2052 if (tgtdev->bdev == fs_info->sb->s_bdev)
2053 fs_info->sb->s_bdev = next_device->bdev;
2054 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
2055 fs_info->fs_devices->latest_bdev = next_device->bdev;
2056 list_del_rcu(&tgtdev->dev_list);
2057
2058 call_rcu(&tgtdev->rcu, free_device);
2059
2060 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002061 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002062}
2063
Eric Sandeen48a3b632013-04-25 20:41:01 +00002064static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2065 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002066{
2067 int ret = 0;
2068 struct btrfs_super_block *disk_super;
2069 u64 devid;
2070 u8 *dev_uuid;
2071 struct block_device *bdev;
2072 struct buffer_head *bh;
2073
2074 *device = NULL;
2075 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2076 root->fs_info->bdev_holder, 0, &bdev, &bh);
2077 if (ret)
2078 return ret;
2079 disk_super = (struct btrfs_super_block *)bh->b_data;
2080 devid = btrfs_stack_device_id(&disk_super->dev_item);
2081 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002082 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002083 disk_super->fsid);
2084 brelse(bh);
2085 if (!*device)
2086 ret = -ENOENT;
2087 blkdev_put(bdev, FMODE_READ);
2088 return ret;
2089}
2090
2091int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2092 char *device_path,
2093 struct btrfs_device **device)
2094{
2095 *device = NULL;
2096 if (strcmp(device_path, "missing") == 0) {
2097 struct list_head *devices;
2098 struct btrfs_device *tmp;
2099
2100 devices = &root->fs_info->fs_devices->devices;
2101 /*
2102 * It is safe to read the devices since the volume_mutex
2103 * is held by the caller.
2104 */
2105 list_for_each_entry(tmp, devices, dev_list) {
2106 if (tmp->in_fs_metadata && !tmp->bdev) {
2107 *device = tmp;
2108 break;
2109 }
2110 }
2111
Anand Jaind74a6252015-08-14 18:32:56 +08002112 if (!*device)
2113 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002114
2115 return 0;
2116 } else {
2117 return btrfs_find_device_by_path(root, device_path, device);
2118 }
2119}
2120
Yan Zheng2b820322008-11-17 21:11:30 -05002121/*
2122 * does all the dirty work required for changing file system's UUID.
2123 */
Li Zefan125ccb02011-12-08 15:07:24 +08002124static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002125{
2126 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2127 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002128 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002129 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002130 struct btrfs_device *device;
2131 u64 super_flags;
2132
2133 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002134 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002135 return -EINVAL;
2136
Ilya Dryomov2208a372013-08-12 14:33:03 +03002137 seed_devices = __alloc_fs_devices();
2138 if (IS_ERR(seed_devices))
2139 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002140
Yan Zhenge4404d62008-12-12 10:03:26 -05002141 old_devices = clone_fs_devices(fs_devices);
2142 if (IS_ERR(old_devices)) {
2143 kfree(seed_devices);
2144 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002145 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002146
Yan Zheng2b820322008-11-17 21:11:30 -05002147 list_add(&old_devices->list, &fs_uuids);
2148
Yan Zhenge4404d62008-12-12 10:03:26 -05002149 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2150 seed_devices->opened = 1;
2151 INIT_LIST_HEAD(&seed_devices->devices);
2152 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002153 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002154
2155 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002156 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2157 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002158 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002159 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002160
2161 lock_chunks(root);
2162 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2163 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002164
Yan Zheng2b820322008-11-17 21:11:30 -05002165 fs_devices->seeding = 0;
2166 fs_devices->num_devices = 0;
2167 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002168 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002169 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002170 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002171
2172 generate_random_uuid(fs_devices->fsid);
2173 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2174 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002175 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2176
Yan Zheng2b820322008-11-17 21:11:30 -05002177 super_flags = btrfs_super_flags(disk_super) &
2178 ~BTRFS_SUPER_FLAG_SEEDING;
2179 btrfs_set_super_flags(disk_super, super_flags);
2180
2181 return 0;
2182}
2183
2184/*
2185 * strore the expected generation for seed devices in device items.
2186 */
2187static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2188 struct btrfs_root *root)
2189{
2190 struct btrfs_path *path;
2191 struct extent_buffer *leaf;
2192 struct btrfs_dev_item *dev_item;
2193 struct btrfs_device *device;
2194 struct btrfs_key key;
2195 u8 fs_uuid[BTRFS_UUID_SIZE];
2196 u8 dev_uuid[BTRFS_UUID_SIZE];
2197 u64 devid;
2198 int ret;
2199
2200 path = btrfs_alloc_path();
2201 if (!path)
2202 return -ENOMEM;
2203
2204 root = root->fs_info->chunk_root;
2205 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2206 key.offset = 0;
2207 key.type = BTRFS_DEV_ITEM_KEY;
2208
2209 while (1) {
2210 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2211 if (ret < 0)
2212 goto error;
2213
2214 leaf = path->nodes[0];
2215next_slot:
2216 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2217 ret = btrfs_next_leaf(root, path);
2218 if (ret > 0)
2219 break;
2220 if (ret < 0)
2221 goto error;
2222 leaf = path->nodes[0];
2223 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002224 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002225 continue;
2226 }
2227
2228 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2229 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2230 key.type != BTRFS_DEV_ITEM_KEY)
2231 break;
2232
2233 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2234 struct btrfs_dev_item);
2235 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002236 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002237 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002238 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002239 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002240 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2241 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002242 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002243
2244 if (device->fs_devices->seeding) {
2245 btrfs_set_device_generation(leaf, dev_item,
2246 device->generation);
2247 btrfs_mark_buffer_dirty(leaf);
2248 }
2249
2250 path->slots[0]++;
2251 goto next_slot;
2252 }
2253 ret = 0;
2254error:
2255 btrfs_free_path(path);
2256 return ret;
2257}
2258
Chris Mason788f20e2008-04-28 15:29:42 -04002259int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2260{
Josef Bacikd5e20032011-08-04 14:52:27 +00002261 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002262 struct btrfs_trans_handle *trans;
2263 struct btrfs_device *device;
2264 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002265 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002266 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002267 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002268 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002269 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002270 int ret = 0;
2271
Yan Zheng2b820322008-11-17 21:11:30 -05002272 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002273 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002274
Li Zefana5d16332011-12-07 20:08:40 -05002275 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002276 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002277 if (IS_ERR(bdev))
2278 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002279
Yan Zheng2b820322008-11-17 21:11:30 -05002280 if (root->fs_info->fs_devices->seeding) {
2281 seeding_dev = 1;
2282 down_write(&sb->s_umount);
2283 mutex_lock(&uuid_mutex);
2284 }
2285
Chris Mason8c8bee12008-09-29 11:19:10 -04002286 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002287
Chris Mason788f20e2008-04-28 15:29:42 -04002288 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002289
2290 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002291 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002292 if (device->bdev == bdev) {
2293 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002294 mutex_unlock(
2295 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002296 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002297 }
2298 }
Liu Bod25628b2012-11-14 14:35:30 +00002299 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002300
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002301 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2302 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002303 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002304 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002305 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002306 }
2307
David Sterba78f2c9e2016-02-11 14:25:38 +01002308 name = rcu_string_strdup(device_path, GFP_KERNEL);
Josef Bacik606686e2012-06-04 14:03:51 -04002309 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002310 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002311 ret = -ENOMEM;
2312 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002313 }
Josef Bacik606686e2012-06-04 14:03:51 -04002314 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002315
Yan, Zhenga22285a2010-05-16 10:48:46 -04002316 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002317 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002318 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002319 kfree(device);
2320 ret = PTR_ERR(trans);
2321 goto error;
2322 }
2323
Josef Bacikd5e20032011-08-04 14:52:27 +00002324 q = bdev_get_queue(bdev);
2325 if (blk_queue_discard(q))
2326 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002327 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002328 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002329 device->io_width = root->sectorsize;
2330 device->io_align = root->sectorsize;
2331 device->sector_size = root->sectorsize;
2332 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002333 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002334 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002335 device->dev_root = root->fs_info->dev_root;
2336 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002337 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002338 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002339 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002340 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002341 set_blocksize(device->bdev, 4096);
2342
Yan Zheng2b820322008-11-17 21:11:30 -05002343 if (seeding_dev) {
2344 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002345 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002346 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002347 }
2348
2349 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002350
Chris Masone5e9a522009-06-10 15:17:02 -04002351 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002352 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002353 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002354 list_add(&device->dev_alloc_list,
2355 &root->fs_info->fs_devices->alloc_list);
2356 root->fs_info->fs_devices->num_devices++;
2357 root->fs_info->fs_devices->open_devices++;
2358 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002359 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002360 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2361
Josef Bacik2bf64752011-09-26 17:12:22 -04002362 spin_lock(&root->fs_info->free_chunk_lock);
2363 root->fs_info->free_chunk_space += device->total_bytes;
2364 spin_unlock(&root->fs_info->free_chunk_lock);
2365
Chris Masonc2898112009-06-10 09:51:32 -04002366 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2367 root->fs_info->fs_devices->rotating = 1;
2368
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002369 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002370 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002371 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002372
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002373 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002374 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002375 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002376
2377 /* add sysfs device entry */
Anand Jaine3bd6972015-08-14 18:32:48 +08002378 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002379
Miao Xie2196d6e2014-09-03 21:35:41 +08002380 /*
2381 * we've got more storage, clear any full flags on the space
2382 * infos
2383 */
2384 btrfs_clear_space_info_full(root->fs_info);
2385
2386 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002387 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002388
Yan Zheng2b820322008-11-17 21:11:30 -05002389 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002390 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002391 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002392 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002393 if (ret) {
2394 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002395 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002396 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002397 }
2398
2399 ret = btrfs_add_device(trans, root, device);
2400 if (ret) {
2401 btrfs_abort_transaction(trans, root, ret);
2402 goto error_trans;
2403 }
2404
2405 if (seeding_dev) {
2406 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2407
Yan Zheng2b820322008-11-17 21:11:30 -05002408 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002409 if (ret) {
2410 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002411 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002412 }
Anand Jainb2373f22014-06-03 11:36:03 +08002413
2414 /* Sprouting would change fsid of the mounted root,
2415 * so rename the fsid on the sysfs
2416 */
2417 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2418 root->fs_info->fsid);
Anand Jainc1b7e472015-08-14 18:32:50 +08002419 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
Anand Jain2e7910d2015-03-10 06:38:29 +08002420 fsid_buf))
David Sterbaf14d1042015-10-08 11:37:06 +02002421 btrfs_warn(root->fs_info,
2422 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002423 }
2424
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002425 root->fs_info->num_tolerated_disk_barrier_failures =
2426 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002427 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002428
2429 if (seeding_dev) {
2430 mutex_unlock(&uuid_mutex);
2431 up_write(&sb->s_umount);
2432
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002433 if (ret) /* transaction commit */
2434 return ret;
2435
Yan Zheng2b820322008-11-17 21:11:30 -05002436 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002437 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002438 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002439 "Failed to relocate sys chunks after "
2440 "device initialization. This can be fixed "
2441 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002442 trans = btrfs_attach_transaction(root);
2443 if (IS_ERR(trans)) {
2444 if (PTR_ERR(trans) == -ENOENT)
2445 return 0;
2446 return PTR_ERR(trans);
2447 }
2448 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002449 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002450
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002451 /* Update ctime/mtime for libblkid */
2452 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002453 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002454
2455error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002456 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002457 rcu_string_free(device->name);
Anand Jain32576042015-08-14 18:32:49 +08002458 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002459 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002460error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002461 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002462 if (seeding_dev) {
2463 mutex_unlock(&uuid_mutex);
2464 up_write(&sb->s_umount);
2465 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002466 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002467}
2468
Stefan Behrense93c89c2012-11-05 17:33:06 +01002469int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002470 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002471 struct btrfs_device **device_out)
2472{
2473 struct request_queue *q;
2474 struct btrfs_device *device;
2475 struct block_device *bdev;
2476 struct btrfs_fs_info *fs_info = root->fs_info;
2477 struct list_head *devices;
2478 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002479 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002480 int ret = 0;
2481
2482 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002483 if (fs_info->fs_devices->seeding) {
2484 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002485 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002486 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002487
2488 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2489 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002490 if (IS_ERR(bdev)) {
2491 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002492 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002493 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002494
2495 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2496
2497 devices = &fs_info->fs_devices->devices;
2498 list_for_each_entry(device, devices, dev_list) {
2499 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002500 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002501 ret = -EEXIST;
2502 goto error;
2503 }
2504 }
2505
Miao Xie1c433662014-09-03 21:35:32 +08002506
Miao Xie7cc8e582014-09-03 21:35:38 +08002507 if (i_size_read(bdev->bd_inode) <
2508 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002509 btrfs_err(fs_info, "target device is smaller than source device!");
2510 ret = -EINVAL;
2511 goto error;
2512 }
2513
2514
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002515 device = btrfs_alloc_device(NULL, &devid, NULL);
2516 if (IS_ERR(device)) {
2517 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002518 goto error;
2519 }
2520
2521 name = rcu_string_strdup(device_path, GFP_NOFS);
2522 if (!name) {
2523 kfree(device);
2524 ret = -ENOMEM;
2525 goto error;
2526 }
2527 rcu_assign_pointer(device->name, name);
2528
2529 q = bdev_get_queue(bdev);
2530 if (blk_queue_discard(q))
2531 device->can_discard = 1;
2532 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2533 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002534 device->generation = 0;
2535 device->io_width = root->sectorsize;
2536 device->io_align = root->sectorsize;
2537 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002538 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2539 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2540 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002541 ASSERT(list_empty(&srcdev->resized_list));
2542 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002543 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002544 device->dev_root = fs_info->dev_root;
2545 device->bdev = bdev;
2546 device->in_fs_metadata = 1;
2547 device->is_tgtdev_for_dev_replace = 1;
2548 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002549 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002550 set_blocksize(device->bdev, 4096);
2551 device->fs_devices = fs_info->fs_devices;
2552 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2553 fs_info->fs_devices->num_devices++;
2554 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002555 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2556
2557 *device_out = device;
2558 return ret;
2559
2560error:
2561 blkdev_put(bdev, FMODE_EXCL);
2562 return ret;
2563}
2564
2565void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2566 struct btrfs_device *tgtdev)
2567{
2568 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2569 tgtdev->io_width = fs_info->dev_root->sectorsize;
2570 tgtdev->io_align = fs_info->dev_root->sectorsize;
2571 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2572 tgtdev->dev_root = fs_info->dev_root;
2573 tgtdev->in_fs_metadata = 1;
2574}
2575
Chris Masond3977122009-01-05 21:25:51 -05002576static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2577 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002578{
2579 int ret;
2580 struct btrfs_path *path;
2581 struct btrfs_root *root;
2582 struct btrfs_dev_item *dev_item;
2583 struct extent_buffer *leaf;
2584 struct btrfs_key key;
2585
2586 root = device->dev_root->fs_info->chunk_root;
2587
2588 path = btrfs_alloc_path();
2589 if (!path)
2590 return -ENOMEM;
2591
2592 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2593 key.type = BTRFS_DEV_ITEM_KEY;
2594 key.offset = device->devid;
2595
2596 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2597 if (ret < 0)
2598 goto out;
2599
2600 if (ret > 0) {
2601 ret = -ENOENT;
2602 goto out;
2603 }
2604
2605 leaf = path->nodes[0];
2606 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2607
2608 btrfs_set_device_id(leaf, dev_item, device->devid);
2609 btrfs_set_device_type(leaf, dev_item, device->type);
2610 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2611 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2612 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002613 btrfs_set_device_total_bytes(leaf, dev_item,
2614 btrfs_device_get_disk_total_bytes(device));
2615 btrfs_set_device_bytes_used(leaf, dev_item,
2616 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002617 btrfs_mark_buffer_dirty(leaf);
2618
2619out:
2620 btrfs_free_path(path);
2621 return ret;
2622}
2623
Miao Xie2196d6e2014-09-03 21:35:41 +08002624int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002625 struct btrfs_device *device, u64 new_size)
2626{
2627 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002628 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002629 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002630 u64 old_total;
2631 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002632
Yan Zheng2b820322008-11-17 21:11:30 -05002633 if (!device->writeable)
2634 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002635
2636 lock_chunks(device->dev_root);
2637 old_total = btrfs_super_total_bytes(super_copy);
2638 diff = new_size - device->total_bytes;
2639
Stefan Behrens63a212a2012-11-05 18:29:28 +01002640 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002641 device->is_tgtdev_for_dev_replace) {
2642 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002643 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002644 }
Yan Zheng2b820322008-11-17 21:11:30 -05002645
Miao Xie935e5cc2014-09-03 21:35:33 +08002646 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002647
2648 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002649 device->fs_devices->total_rw_bytes += diff;
2650
Miao Xie7cc8e582014-09-03 21:35:38 +08002651 btrfs_device_set_total_bytes(device, new_size);
2652 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002653 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002654 if (list_empty(&device->resized_list))
2655 list_add_tail(&device->resized_list,
2656 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002657 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002658
Chris Mason8f18cf12008-04-25 16:53:30 -04002659 return btrfs_update_device(trans, device);
2660}
2661
2662static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002663 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002664 u64 chunk_offset)
2665{
2666 int ret;
2667 struct btrfs_path *path;
2668 struct btrfs_key key;
2669
2670 root = root->fs_info->chunk_root;
2671 path = btrfs_alloc_path();
2672 if (!path)
2673 return -ENOMEM;
2674
2675 key.objectid = chunk_objectid;
2676 key.offset = chunk_offset;
2677 key.type = BTRFS_CHUNK_ITEM_KEY;
2678
2679 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 if (ret < 0)
2681 goto out;
2682 else if (ret > 0) { /* Logic error or corruption */
Anand Jaina4553fe2015-09-25 14:43:01 +08002683 btrfs_std_error(root->fs_info, -ENOENT,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002684 "Failed lookup while freeing chunk.");
2685 ret = -ENOENT;
2686 goto out;
2687 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002688
2689 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002690 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002691 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 "Failed to delete chunk item.");
2693out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002694 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002695 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002696}
2697
Christoph Hellwigb2950862008-12-02 09:54:17 -05002698static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002699 chunk_offset)
2700{
David Sterba6c417612011-04-13 15:41:04 +02002701 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002702 struct btrfs_disk_key *disk_key;
2703 struct btrfs_chunk *chunk;
2704 u8 *ptr;
2705 int ret = 0;
2706 u32 num_stripes;
2707 u32 array_size;
2708 u32 len = 0;
2709 u32 cur;
2710 struct btrfs_key key;
2711
Miao Xie2196d6e2014-09-03 21:35:41 +08002712 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002713 array_size = btrfs_super_sys_array_size(super_copy);
2714
2715 ptr = super_copy->sys_chunk_array;
2716 cur = 0;
2717
2718 while (cur < array_size) {
2719 disk_key = (struct btrfs_disk_key *)ptr;
2720 btrfs_disk_key_to_cpu(&key, disk_key);
2721
2722 len = sizeof(*disk_key);
2723
2724 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2725 chunk = (struct btrfs_chunk *)(ptr + len);
2726 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2727 len += btrfs_chunk_item_size(num_stripes);
2728 } else {
2729 ret = -EIO;
2730 break;
2731 }
2732 if (key.objectid == chunk_objectid &&
2733 key.offset == chunk_offset) {
2734 memmove(ptr, ptr + len, array_size - (cur + len));
2735 array_size -= len;
2736 btrfs_set_super_sys_array_size(super_copy, array_size);
2737 } else {
2738 ptr += len;
2739 cur += len;
2740 }
2741 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002742 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002743 return ret;
2744}
2745
Josef Bacik47ab2a62014-09-18 11:20:02 -04002746int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2747 struct btrfs_root *root, u64 chunk_offset)
2748{
2749 struct extent_map_tree *em_tree;
2750 struct extent_map *em;
2751 struct btrfs_root *extent_root = root->fs_info->extent_root;
2752 struct map_lookup *map;
2753 u64 dev_extent_len = 0;
2754 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002755 int i, ret = 0;
2756
2757 /* Just in case */
2758 root = root->fs_info->chunk_root;
2759 em_tree = &root->fs_info->mapping_tree.map_tree;
2760
2761 read_lock(&em_tree->lock);
2762 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2763 read_unlock(&em_tree->lock);
2764
2765 if (!em || em->start > chunk_offset ||
2766 em->start + em->len < chunk_offset) {
2767 /*
2768 * This is a logic error, but we don't want to just rely on the
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08002769 * user having built with ASSERT enabled, so if ASSERT doesn't
Josef Bacik47ab2a62014-09-18 11:20:02 -04002770 * do anything we still error out.
2771 */
2772 ASSERT(0);
2773 if (em)
2774 free_extent_map(em);
2775 return -EINVAL;
2776 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04002777 map = em->map_lookup;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002778 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002779 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002780 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002781
2782 for (i = 0; i < map->num_stripes; i++) {
2783 struct btrfs_device *device = map->stripes[i].dev;
2784 ret = btrfs_free_dev_extent(trans, device,
2785 map->stripes[i].physical,
2786 &dev_extent_len);
2787 if (ret) {
2788 btrfs_abort_transaction(trans, root, ret);
2789 goto out;
2790 }
2791
2792 if (device->bytes_used > 0) {
2793 lock_chunks(root);
2794 btrfs_device_set_bytes_used(device,
2795 device->bytes_used - dev_extent_len);
2796 spin_lock(&root->fs_info->free_chunk_lock);
2797 root->fs_info->free_chunk_space += dev_extent_len;
2798 spin_unlock(&root->fs_info->free_chunk_lock);
2799 btrfs_clear_space_info_full(root->fs_info);
2800 unlock_chunks(root);
2801 }
2802
2803 if (map->stripes[i].dev) {
2804 ret = btrfs_update_device(trans, map->stripes[i].dev);
2805 if (ret) {
2806 btrfs_abort_transaction(trans, root, ret);
2807 goto out;
2808 }
2809 }
2810 }
Zhao Leia688a042015-02-09 20:31:44 +08002811 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002812 if (ret) {
2813 btrfs_abort_transaction(trans, root, ret);
2814 goto out;
2815 }
2816
2817 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2818
2819 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2820 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2821 if (ret) {
2822 btrfs_abort_transaction(trans, root, ret);
2823 goto out;
2824 }
2825 }
2826
Filipe Manana04216822014-11-27 21:14:15 +00002827 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002828 if (ret) {
2829 btrfs_abort_transaction(trans, extent_root, ret);
2830 goto out;
2831 }
2832
Josef Bacik47ab2a62014-09-18 11:20:02 -04002833out:
2834 /* once for us */
2835 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002836 return ret;
2837}
2838
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002839static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002840{
Chris Mason8f18cf12008-04-25 16:53:30 -04002841 struct btrfs_root *extent_root;
2842 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002843 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002844
2845 root = root->fs_info->chunk_root;
2846 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002847
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002848 /*
2849 * Prevent races with automatic removal of unused block groups.
2850 * After we relocate and before we remove the chunk with offset
2851 * chunk_offset, automatic removal of the block group can kick in,
2852 * resulting in a failure when calling btrfs_remove_chunk() below.
2853 *
2854 * Make sure to acquire this mutex before doing a tree search (dev
2855 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2856 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2857 * we release the path used to search the chunk/dev tree and before
2858 * the current task acquires this mutex and calls us.
2859 */
2860 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2861
Josef Bacikba1bf482009-09-11 16:11:19 -04002862 ret = btrfs_can_relocate(extent_root, chunk_offset);
2863 if (ret)
2864 return -ENOSPC;
2865
Chris Mason8f18cf12008-04-25 16:53:30 -04002866 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002867 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002868 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002869 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002870 if (ret)
2871 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002872
Filipe Manana7fd01182015-11-13 23:57:17 +00002873 trans = btrfs_start_trans_remove_block_group(root->fs_info,
2874 chunk_offset);
Liu Bo0f788c52013-03-04 16:25:40 +00002875 if (IS_ERR(trans)) {
2876 ret = PTR_ERR(trans);
Anand Jaina4553fe2015-09-25 14:43:01 +08002877 btrfs_std_error(root->fs_info, ret, NULL);
Liu Bo0f788c52013-03-04 16:25:40 +00002878 return ret;
2879 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002880
2881 /*
2882 * step two, delete the device extents and the
2883 * chunk tree entries
2884 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002885 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002886 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002887 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002888}
2889
Yan Zheng2b820322008-11-17 21:11:30 -05002890static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2891{
2892 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2893 struct btrfs_path *path;
2894 struct extent_buffer *leaf;
2895 struct btrfs_chunk *chunk;
2896 struct btrfs_key key;
2897 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002898 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002899 bool retried = false;
2900 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002901 int ret;
2902
2903 path = btrfs_alloc_path();
2904 if (!path)
2905 return -ENOMEM;
2906
Josef Bacikba1bf482009-09-11 16:11:19 -04002907again:
Yan Zheng2b820322008-11-17 21:11:30 -05002908 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2909 key.offset = (u64)-1;
2910 key.type = BTRFS_CHUNK_ITEM_KEY;
2911
2912 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002913 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002914 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002915 if (ret < 0) {
2916 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002917 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002918 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002919 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002920
2921 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2922 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002923 if (ret)
2924 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002925 if (ret < 0)
2926 goto error;
2927 if (ret > 0)
2928 break;
2929
2930 leaf = path->nodes[0];
2931 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2932
2933 chunk = btrfs_item_ptr(leaf, path->slots[0],
2934 struct btrfs_chunk);
2935 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002936 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002937
2938 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002939 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002940 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002941 if (ret == -ENOSPC)
2942 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302943 else
2944 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002945 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002946 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002947
2948 if (found_key.offset == 0)
2949 break;
2950 key.offset = found_key.offset - 1;
2951 }
2952 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002953 if (failed && !retried) {
2954 failed = 0;
2955 retried = true;
2956 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302957 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002958 ret = -ENOSPC;
2959 }
Yan Zheng2b820322008-11-17 21:11:30 -05002960error:
2961 btrfs_free_path(path);
2962 return ret;
2963}
2964
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002965static int insert_balance_item(struct btrfs_root *root,
2966 struct btrfs_balance_control *bctl)
2967{
2968 struct btrfs_trans_handle *trans;
2969 struct btrfs_balance_item *item;
2970 struct btrfs_disk_balance_args disk_bargs;
2971 struct btrfs_path *path;
2972 struct extent_buffer *leaf;
2973 struct btrfs_key key;
2974 int ret, err;
2975
2976 path = btrfs_alloc_path();
2977 if (!path)
2978 return -ENOMEM;
2979
2980 trans = btrfs_start_transaction(root, 0);
2981 if (IS_ERR(trans)) {
2982 btrfs_free_path(path);
2983 return PTR_ERR(trans);
2984 }
2985
2986 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01002987 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002988 key.offset = 0;
2989
2990 ret = btrfs_insert_empty_item(trans, root, path, &key,
2991 sizeof(*item));
2992 if (ret)
2993 goto out;
2994
2995 leaf = path->nodes[0];
2996 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2997
2998 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2999
3000 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3001 btrfs_set_balance_data(leaf, item, &disk_bargs);
3002 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3003 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3004 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3005 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3006
3007 btrfs_set_balance_flags(leaf, item, bctl->flags);
3008
3009 btrfs_mark_buffer_dirty(leaf);
3010out:
3011 btrfs_free_path(path);
3012 err = btrfs_commit_transaction(trans, root);
3013 if (err && !ret)
3014 ret = err;
3015 return ret;
3016}
3017
3018static int del_balance_item(struct btrfs_root *root)
3019{
3020 struct btrfs_trans_handle *trans;
3021 struct btrfs_path *path;
3022 struct btrfs_key key;
3023 int ret, err;
3024
3025 path = btrfs_alloc_path();
3026 if (!path)
3027 return -ENOMEM;
3028
3029 trans = btrfs_start_transaction(root, 0);
3030 if (IS_ERR(trans)) {
3031 btrfs_free_path(path);
3032 return PTR_ERR(trans);
3033 }
3034
3035 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003036 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003037 key.offset = 0;
3038
3039 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3040 if (ret < 0)
3041 goto out;
3042 if (ret > 0) {
3043 ret = -ENOENT;
3044 goto out;
3045 }
3046
3047 ret = btrfs_del_item(trans, root, path);
3048out:
3049 btrfs_free_path(path);
3050 err = btrfs_commit_transaction(trans, root);
3051 if (err && !ret)
3052 ret = err;
3053 return ret;
3054}
3055
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003056/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003057 * This is a heuristic used to reduce the number of chunks balanced on
3058 * resume after balance was interrupted.
3059 */
3060static void update_balance_args(struct btrfs_balance_control *bctl)
3061{
3062 /*
3063 * Turn on soft mode for chunk types that were being converted.
3064 */
3065 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3066 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3067 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3068 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3069 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3070 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3071
3072 /*
3073 * Turn on usage filter if is not already used. The idea is
3074 * that chunks that we have already balanced should be
3075 * reasonably full. Don't do it for chunks that are being
3076 * converted - that will keep us from relocating unconverted
3077 * (albeit full) chunks.
3078 */
3079 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003080 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003081 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3082 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3083 bctl->data.usage = 90;
3084 }
3085 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003086 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003087 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3088 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3089 bctl->sys.usage = 90;
3090 }
3091 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003092 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003093 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3094 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3095 bctl->meta.usage = 90;
3096 }
3097}
3098
3099/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003100 * Should be called with both balance and volume mutexes held to
3101 * serialize other volume operations (add_dev/rm_dev/resize) with
3102 * restriper. Same goes for unset_balance_control.
3103 */
3104static void set_balance_control(struct btrfs_balance_control *bctl)
3105{
3106 struct btrfs_fs_info *fs_info = bctl->fs_info;
3107
3108 BUG_ON(fs_info->balance_ctl);
3109
3110 spin_lock(&fs_info->balance_lock);
3111 fs_info->balance_ctl = bctl;
3112 spin_unlock(&fs_info->balance_lock);
3113}
3114
3115static void unset_balance_control(struct btrfs_fs_info *fs_info)
3116{
3117 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3118
3119 BUG_ON(!fs_info->balance_ctl);
3120
3121 spin_lock(&fs_info->balance_lock);
3122 fs_info->balance_ctl = NULL;
3123 spin_unlock(&fs_info->balance_lock);
3124
3125 kfree(bctl);
3126}
3127
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003128/*
3129 * Balance filters. Return 1 if chunk should be filtered out
3130 * (should not be balanced).
3131 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003132static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003133 struct btrfs_balance_args *bargs)
3134{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003135 chunk_type = chunk_to_extended(chunk_type) &
3136 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003137
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003138 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003139 return 0;
3140
3141 return 1;
3142}
3143
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003144static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003145 struct btrfs_balance_args *bargs)
3146{
3147 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003148 u64 chunk_used;
3149 u64 user_thresh_min;
3150 u64 user_thresh_max;
3151 int ret = 1;
3152
3153 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3154 chunk_used = btrfs_block_group_used(&cache->item);
3155
3156 if (bargs->usage_min == 0)
3157 user_thresh_min = 0;
3158 else
3159 user_thresh_min = div_factor_fine(cache->key.offset,
3160 bargs->usage_min);
3161
3162 if (bargs->usage_max == 0)
3163 user_thresh_max = 1;
3164 else if (bargs->usage_max > 100)
3165 user_thresh_max = cache->key.offset;
3166 else
3167 user_thresh_max = div_factor_fine(cache->key.offset,
3168 bargs->usage_max);
3169
3170 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3171 ret = 0;
3172
3173 btrfs_put_block_group(cache);
3174 return ret;
3175}
3176
Holger Hoffstättedba72cb2015-11-17 12:29:32 +01003177static int chunk_usage_filter(struct btrfs_fs_info *fs_info,
David Sterbabc309462015-10-20 18:22:13 +02003178 u64 chunk_offset, struct btrfs_balance_args *bargs)
3179{
3180 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003181 u64 chunk_used, user_thresh;
3182 int ret = 1;
3183
3184 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3185 chunk_used = btrfs_block_group_used(&cache->item);
3186
David Sterbabc309462015-10-20 18:22:13 +02003187 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003188 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003189 else if (bargs->usage > 100)
3190 user_thresh = cache->key.offset;
3191 else
3192 user_thresh = div_factor_fine(cache->key.offset,
3193 bargs->usage);
3194
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003195 if (chunk_used < user_thresh)
3196 ret = 0;
3197
3198 btrfs_put_block_group(cache);
3199 return ret;
3200}
3201
Ilya Dryomov409d4042012-01-16 22:04:47 +02003202static int chunk_devid_filter(struct extent_buffer *leaf,
3203 struct btrfs_chunk *chunk,
3204 struct btrfs_balance_args *bargs)
3205{
3206 struct btrfs_stripe *stripe;
3207 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3208 int i;
3209
3210 for (i = 0; i < num_stripes; i++) {
3211 stripe = btrfs_stripe_nr(chunk, i);
3212 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3213 return 0;
3214 }
3215
3216 return 1;
3217}
3218
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003219/* [pstart, pend) */
3220static int chunk_drange_filter(struct extent_buffer *leaf,
3221 struct btrfs_chunk *chunk,
3222 u64 chunk_offset,
3223 struct btrfs_balance_args *bargs)
3224{
3225 struct btrfs_stripe *stripe;
3226 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3227 u64 stripe_offset;
3228 u64 stripe_length;
3229 int factor;
3230 int i;
3231
3232 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3233 return 0;
3234
3235 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003236 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3237 factor = num_stripes / 2;
3238 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3239 factor = num_stripes - 1;
3240 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3241 factor = num_stripes - 2;
3242 } else {
3243 factor = num_stripes;
3244 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003245
3246 for (i = 0; i < num_stripes; i++) {
3247 stripe = btrfs_stripe_nr(chunk, i);
3248 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3249 continue;
3250
3251 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3252 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003253 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003254
3255 if (stripe_offset < bargs->pend &&
3256 stripe_offset + stripe_length > bargs->pstart)
3257 return 0;
3258 }
3259
3260 return 1;
3261}
3262
Ilya Dryomovea671762012-01-16 22:04:48 +02003263/* [vstart, vend) */
3264static int chunk_vrange_filter(struct extent_buffer *leaf,
3265 struct btrfs_chunk *chunk,
3266 u64 chunk_offset,
3267 struct btrfs_balance_args *bargs)
3268{
3269 if (chunk_offset < bargs->vend &&
3270 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3271 /* at least part of the chunk is inside this vrange */
3272 return 0;
3273
3274 return 1;
3275}
3276
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003277static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3278 struct btrfs_chunk *chunk,
3279 struct btrfs_balance_args *bargs)
3280{
3281 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3282
3283 if (bargs->stripes_min <= num_stripes
3284 && num_stripes <= bargs->stripes_max)
3285 return 0;
3286
3287 return 1;
3288}
3289
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003290static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003291 struct btrfs_balance_args *bargs)
3292{
3293 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3294 return 0;
3295
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003296 chunk_type = chunk_to_extended(chunk_type) &
3297 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003298
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003299 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003300 return 1;
3301
3302 return 0;
3303}
3304
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003305static int should_balance_chunk(struct btrfs_root *root,
3306 struct extent_buffer *leaf,
3307 struct btrfs_chunk *chunk, u64 chunk_offset)
3308{
3309 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3310 struct btrfs_balance_args *bargs = NULL;
3311 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3312
3313 /* type filter */
3314 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3315 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3316 return 0;
3317 }
3318
3319 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3320 bargs = &bctl->data;
3321 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3322 bargs = &bctl->sys;
3323 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3324 bargs = &bctl->meta;
3325
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003326 /* profiles filter */
3327 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3328 chunk_profiles_filter(chunk_type, bargs)) {
3329 return 0;
3330 }
3331
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003332 /* usage filter */
3333 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3334 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3335 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003336 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3337 chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) {
3338 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003339 }
3340
Ilya Dryomov409d4042012-01-16 22:04:47 +02003341 /* devid filter */
3342 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3343 chunk_devid_filter(leaf, chunk, bargs)) {
3344 return 0;
3345 }
3346
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003347 /* drange filter, makes sense only with devid filter */
3348 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3349 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3350 return 0;
3351 }
3352
Ilya Dryomovea671762012-01-16 22:04:48 +02003353 /* vrange filter */
3354 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3355 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3356 return 0;
3357 }
3358
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003359 /* stripes filter */
3360 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3361 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3362 return 0;
3363 }
3364
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003365 /* soft profile changing mode */
3366 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3367 chunk_soft_convert_filter(chunk_type, bargs)) {
3368 return 0;
3369 }
3370
David Sterba7d824b62014-05-07 17:37:51 +02003371 /*
3372 * limited by count, must be the last filter
3373 */
3374 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3375 if (bargs->limit == 0)
3376 return 0;
3377 else
3378 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003379 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3380 /*
3381 * Same logic as the 'limit' filter; the minimum cannot be
3382 * determined here because we do not have the global informatoin
3383 * about the count of all chunks that satisfy the filters.
3384 */
3385 if (bargs->limit_max == 0)
3386 return 0;
3387 else
3388 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003389 }
3390
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003391 return 1;
3392}
3393
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003394static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003395{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003396 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003397 struct btrfs_root *chunk_root = fs_info->chunk_root;
3398 struct btrfs_root *dev_root = fs_info->dev_root;
3399 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003400 struct btrfs_device *device;
3401 u64 old_size;
3402 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003403 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003404 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003405 struct btrfs_path *path;
3406 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003407 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003408 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003409 struct extent_buffer *leaf;
3410 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003411 int ret;
3412 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003413 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003414 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003415 u64 limit_data = bctl->data.limit;
3416 u64 limit_meta = bctl->meta.limit;
3417 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003418 u32 count_data = 0;
3419 u32 count_meta = 0;
3420 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003421 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003422
Chris Masonec44a352008-04-28 15:29:52 -04003423 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003424 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003425 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003426 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003427 size_to_free = div_factor(old_size, 1);
Byongho Leeee221842015-12-15 01:42:10 +09003428 size_to_free = min_t(u64, size_to_free, SZ_1M);
Yan Zheng2b820322008-11-17 21:11:30 -05003429 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003430 btrfs_device_get_total_bytes(device) -
3431 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003432 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003433 continue;
3434
3435 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003436 if (ret == -ENOSPC)
3437 break;
Chris Masonec44a352008-04-28 15:29:52 -04003438 BUG_ON(ret);
3439
Yan, Zhenga22285a2010-05-16 10:48:46 -04003440 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003441 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003442
3443 ret = btrfs_grow_device(trans, device, old_size);
3444 BUG_ON(ret);
3445
3446 btrfs_end_transaction(trans, dev_root);
3447 }
3448
3449 /* step two, relocate all the chunks */
3450 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003451 if (!path) {
3452 ret = -ENOMEM;
3453 goto error;
3454 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003455
3456 /* zero out stat counters */
3457 spin_lock(&fs_info->balance_lock);
3458 memset(&bctl->stat, 0, sizeof(bctl->stat));
3459 spin_unlock(&fs_info->balance_lock);
3460again:
David Sterba7d824b62014-05-07 17:37:51 +02003461 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003462 /*
3463 * The single value limit and min/max limits use the same bytes
3464 * in the
3465 */
David Sterba7d824b62014-05-07 17:37:51 +02003466 bctl->data.limit = limit_data;
3467 bctl->meta.limit = limit_meta;
3468 bctl->sys.limit = limit_sys;
3469 }
Chris Masonec44a352008-04-28 15:29:52 -04003470 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3471 key.offset = (u64)-1;
3472 key.type = BTRFS_CHUNK_ITEM_KEY;
3473
Chris Masond3977122009-01-05 21:25:51 -05003474 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003475 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003476 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003477 ret = -ECANCELED;
3478 goto error;
3479 }
3480
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003481 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003482 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003483 if (ret < 0) {
3484 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003485 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003486 }
Chris Masonec44a352008-04-28 15:29:52 -04003487
3488 /*
3489 * this shouldn't happen, it means the last relocate
3490 * failed
3491 */
3492 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003493 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003494
3495 ret = btrfs_previous_item(chunk_root, path, 0,
3496 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003497 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003498 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003499 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003500 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003501 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003502
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003503 leaf = path->nodes[0];
3504 slot = path->slots[0];
3505 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3506
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003507 if (found_key.objectid != key.objectid) {
3508 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003509 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003510 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003511
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003512 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003513 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003514
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003515 if (!counting) {
3516 spin_lock(&fs_info->balance_lock);
3517 bctl->stat.considered++;
3518 spin_unlock(&fs_info->balance_lock);
3519 }
3520
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003521 ret = should_balance_chunk(chunk_root, leaf, chunk,
3522 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003523
David Sterbab3b4aa72011-04-21 01:20:15 +02003524 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003525 if (!ret) {
3526 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003527 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003528 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003529
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003530 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003531 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003532 spin_lock(&fs_info->balance_lock);
3533 bctl->stat.expected++;
3534 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003535
3536 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3537 count_data++;
3538 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3539 count_sys++;
3540 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3541 count_meta++;
3542
3543 goto loop;
3544 }
3545
3546 /*
3547 * Apply limit_min filter, no need to check if the LIMITS
3548 * filter is used, limit_min is 0 by default
3549 */
3550 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3551 count_data < bctl->data.limit_min)
3552 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3553 count_meta < bctl->meta.limit_min)
3554 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3555 count_sys < bctl->sys.limit_min)) {
3556 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003557 goto loop;
3558 }
3559
Zhao Lei2c9fe832015-11-09 11:51:32 +08003560 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) && !chunk_reserved) {
3561 trans = btrfs_start_transaction(chunk_root, 0);
3562 if (IS_ERR(trans)) {
3563 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3564 ret = PTR_ERR(trans);
3565 goto error;
3566 }
3567
3568 ret = btrfs_force_chunk_alloc(trans, chunk_root,
3569 BTRFS_BLOCK_GROUP_DATA);
Filipe Manana8a7d6562015-12-10 11:08:59 +00003570 btrfs_end_transaction(trans, chunk_root);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003571 if (ret < 0) {
3572 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3573 goto error;
3574 }
Zhao Lei2c9fe832015-11-09 11:51:32 +08003575 chunk_reserved = 1;
3576 }
3577
Chris Masonec44a352008-04-28 15:29:52 -04003578 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003579 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003580 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003581 if (ret && ret != -ENOSPC)
3582 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003583 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003584 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003585 } else {
3586 spin_lock(&fs_info->balance_lock);
3587 bctl->stat.completed++;
3588 spin_unlock(&fs_info->balance_lock);
3589 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003590loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003591 if (found_key.offset == 0)
3592 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003593 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003594 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003595
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003596 if (counting) {
3597 btrfs_release_path(path);
3598 counting = false;
3599 goto again;
3600 }
Chris Masonec44a352008-04-28 15:29:52 -04003601error:
3602 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003603 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003604 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003605 enospc_errors);
3606 if (!ret)
3607 ret = -ENOSPC;
3608 }
3609
Chris Masonec44a352008-04-28 15:29:52 -04003610 return ret;
3611}
3612
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003613/**
3614 * alloc_profile_is_valid - see if a given profile is valid and reduced
3615 * @flags: profile to validate
3616 * @extended: if true @flags is treated as an extended profile
3617 */
3618static int alloc_profile_is_valid(u64 flags, int extended)
3619{
3620 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3621 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3622
3623 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3624
3625 /* 1) check that all other bits are zeroed */
3626 if (flags & ~mask)
3627 return 0;
3628
3629 /* 2) see if profile is reduced */
3630 if (flags == 0)
3631 return !extended; /* "0" is valid for usual profiles */
3632
3633 /* true if exactly one bit set */
3634 return (flags & (flags - 1)) == 0;
3635}
3636
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003637static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3638{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003639 /* cancel requested || normal exit path */
3640 return atomic_read(&fs_info->balance_cancel_req) ||
3641 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3642 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003643}
3644
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003645static void __cancel_balance(struct btrfs_fs_info *fs_info)
3646{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003647 int ret;
3648
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003649 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003650 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003651 if (ret)
Anand Jaina4553fe2015-09-25 14:43:01 +08003652 btrfs_std_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003653
3654 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003655}
3656
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003657/* Non-zero return value signifies invalidity */
3658static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3659 u64 allowed)
3660{
3661 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3662 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3663 (bctl_arg->target & ~allowed)));
3664}
3665
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003666/*
3667 * Should be called with both balance and volume mutexes held
3668 */
3669int btrfs_balance(struct btrfs_balance_control *bctl,
3670 struct btrfs_ioctl_balance_args *bargs)
3671{
3672 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003673 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003674 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003675 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003676 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003677 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003678
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003679 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003680 atomic_read(&fs_info->balance_pause_req) ||
3681 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003682 ret = -EINVAL;
3683 goto out;
3684 }
3685
Ilya Dryomove4837f82012-03-27 17:09:17 +03003686 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3687 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3688 mixed = 1;
3689
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003690 /*
3691 * In case of mixed groups both data and meta should be picked,
3692 * and identical options should be given for both of them.
3693 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003694 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3695 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003696 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3697 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3698 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003699 btrfs_err(fs_info, "with mixed groups data and "
3700 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003701 ret = -EINVAL;
3702 goto out;
3703 }
3704 }
3705
Stefan Behrens8dabb742012-11-06 13:15:27 +01003706 num_devices = fs_info->fs_devices->num_devices;
Liu Bo73beece2015-07-17 16:49:19 +08003707 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrens8dabb742012-11-06 13:15:27 +01003708 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3709 BUG_ON(num_devices < 1);
3710 num_devices--;
3711 }
Liu Bo73beece2015-07-17 16:49:19 +08003712 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003713 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003714 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003715 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003716 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003717 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003718 if (num_devices > 2)
3719 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3720 if (num_devices > 3)
3721 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3722 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003723 if (validate_convert_profile(&bctl->data, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003724 btrfs_err(fs_info, "unable to start balance with target "
3725 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003726 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003727 ret = -EINVAL;
3728 goto out;
3729 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003730 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003731 btrfs_err(fs_info,
3732 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003733 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003734 ret = -EINVAL;
3735 goto out;
3736 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003737 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003738 btrfs_err(fs_info,
3739 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003740 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003741 ret = -EINVAL;
3742 goto out;
3743 }
3744
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003745 /* allow to reduce meta or sys integrity only if force set */
3746 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003747 BTRFS_BLOCK_GROUP_RAID10 |
3748 BTRFS_BLOCK_GROUP_RAID5 |
3749 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003750 do {
3751 seq = read_seqbegin(&fs_info->profiles_lock);
3752
3753 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3754 (fs_info->avail_system_alloc_bits & allowed) &&
3755 !(bctl->sys.target & allowed)) ||
3756 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3757 (fs_info->avail_metadata_alloc_bits & allowed) &&
3758 !(bctl->meta.target & allowed))) {
3759 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003760 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003761 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003762 btrfs_err(fs_info, "balance will reduce metadata "
3763 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003764 ret = -EINVAL;
3765 goto out;
3766 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003767 }
Miao Xiede98ced2013-01-29 10:13:12 +00003768 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003769
Sam Tygieree592d02016-01-06 08:46:12 +00003770 if (btrfs_get_num_tolerated_disk_barrier_failures(bctl->meta.target) <
3771 btrfs_get_num_tolerated_disk_barrier_failures(bctl->data.target)) {
3772 btrfs_warn(fs_info,
Filipe Mananafedc0042016-01-15 10:56:15 +00003773 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
Sam Tygieree592d02016-01-06 08:46:12 +00003774 bctl->meta.target, bctl->data.target);
3775 }
3776
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003777 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003778 fs_info->num_tolerated_disk_barrier_failures = min(
3779 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3780 btrfs_get_num_tolerated_disk_barrier_failures(
3781 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003782 }
3783
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003784 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003785 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003786 goto out;
3787
Ilya Dryomov59641012012-01-16 22:04:48 +02003788 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3789 BUG_ON(ret == -EEXIST);
3790 set_balance_control(bctl);
3791 } else {
3792 BUG_ON(ret != -EEXIST);
3793 spin_lock(&fs_info->balance_lock);
3794 update_balance_args(bctl);
3795 spin_unlock(&fs_info->balance_lock);
3796 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003797
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003798 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003799 mutex_unlock(&fs_info->balance_mutex);
3800
3801 ret = __btrfs_balance(fs_info);
3802
3803 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003804 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003805
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003806 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3807 fs_info->num_tolerated_disk_barrier_failures =
3808 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3809 }
3810
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003811 if (bargs) {
3812 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003813 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003814 }
3815
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003816 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3817 balance_need_close(fs_info)) {
3818 __cancel_balance(fs_info);
3819 }
3820
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003821 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003822
3823 return ret;
3824out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003825 if (bctl->flags & BTRFS_BALANCE_RESUME)
3826 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003827 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003828 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003829 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3830 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003831 return ret;
3832}
3833
3834static int balance_kthread(void *data)
3835{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003836 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003837 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003838
3839 mutex_lock(&fs_info->volume_mutex);
3840 mutex_lock(&fs_info->balance_mutex);
3841
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003842 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003843 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003844 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003845 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003846
3847 mutex_unlock(&fs_info->balance_mutex);
3848 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003849
Ilya Dryomov59641012012-01-16 22:04:48 +02003850 return ret;
3851}
3852
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003853int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3854{
3855 struct task_struct *tsk;
3856
3857 spin_lock(&fs_info->balance_lock);
3858 if (!fs_info->balance_ctl) {
3859 spin_unlock(&fs_info->balance_lock);
3860 return 0;
3861 }
3862 spin_unlock(&fs_info->balance_lock);
3863
3864 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003865 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003866 return 0;
3867 }
3868
3869 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303870 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003871}
3872
Ilya Dryomov68310a52012-06-22 12:24:12 -06003873int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003874{
Ilya Dryomov59641012012-01-16 22:04:48 +02003875 struct btrfs_balance_control *bctl;
3876 struct btrfs_balance_item *item;
3877 struct btrfs_disk_balance_args disk_bargs;
3878 struct btrfs_path *path;
3879 struct extent_buffer *leaf;
3880 struct btrfs_key key;
3881 int ret;
3882
3883 path = btrfs_alloc_path();
3884 if (!path)
3885 return -ENOMEM;
3886
Ilya Dryomov68310a52012-06-22 12:24:12 -06003887 key.objectid = BTRFS_BALANCE_OBJECTID;
David Sterbac479cb42016-01-25 17:51:31 +01003888 key.type = BTRFS_TEMPORARY_ITEM_KEY;
Ilya Dryomov68310a52012-06-22 12:24:12 -06003889 key.offset = 0;
3890
3891 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3892 if (ret < 0)
3893 goto out;
3894 if (ret > 0) { /* ret = -ENOENT; */
3895 ret = 0;
3896 goto out;
3897 }
3898
Ilya Dryomov59641012012-01-16 22:04:48 +02003899 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3900 if (!bctl) {
3901 ret = -ENOMEM;
3902 goto out;
3903 }
3904
Ilya Dryomov59641012012-01-16 22:04:48 +02003905 leaf = path->nodes[0];
3906 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3907
Ilya Dryomov68310a52012-06-22 12:24:12 -06003908 bctl->fs_info = fs_info;
3909 bctl->flags = btrfs_balance_flags(leaf, item);
3910 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003911
3912 btrfs_balance_data(leaf, item, &disk_bargs);
3913 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3914 btrfs_balance_meta(leaf, item, &disk_bargs);
3915 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3916 btrfs_balance_sys(leaf, item, &disk_bargs);
3917 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3918
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003919 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3920
Ilya Dryomov68310a52012-06-22 12:24:12 -06003921 mutex_lock(&fs_info->volume_mutex);
3922 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003923
Ilya Dryomov68310a52012-06-22 12:24:12 -06003924 set_balance_control(bctl);
3925
3926 mutex_unlock(&fs_info->balance_mutex);
3927 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003928out:
3929 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003930 return ret;
3931}
3932
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003933int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3934{
3935 int ret = 0;
3936
3937 mutex_lock(&fs_info->balance_mutex);
3938 if (!fs_info->balance_ctl) {
3939 mutex_unlock(&fs_info->balance_mutex);
3940 return -ENOTCONN;
3941 }
3942
3943 if (atomic_read(&fs_info->balance_running)) {
3944 atomic_inc(&fs_info->balance_pause_req);
3945 mutex_unlock(&fs_info->balance_mutex);
3946
3947 wait_event(fs_info->balance_wait_q,
3948 atomic_read(&fs_info->balance_running) == 0);
3949
3950 mutex_lock(&fs_info->balance_mutex);
3951 /* we are good with balance_ctl ripped off from under us */
3952 BUG_ON(atomic_read(&fs_info->balance_running));
3953 atomic_dec(&fs_info->balance_pause_req);
3954 } else {
3955 ret = -ENOTCONN;
3956 }
3957
3958 mutex_unlock(&fs_info->balance_mutex);
3959 return ret;
3960}
3961
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003962int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3963{
Ilya Dryomove649e582013-10-10 20:40:21 +03003964 if (fs_info->sb->s_flags & MS_RDONLY)
3965 return -EROFS;
3966
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003967 mutex_lock(&fs_info->balance_mutex);
3968 if (!fs_info->balance_ctl) {
3969 mutex_unlock(&fs_info->balance_mutex);
3970 return -ENOTCONN;
3971 }
3972
3973 atomic_inc(&fs_info->balance_cancel_req);
3974 /*
3975 * if we are running just wait and return, balance item is
3976 * deleted in btrfs_balance in this case
3977 */
3978 if (atomic_read(&fs_info->balance_running)) {
3979 mutex_unlock(&fs_info->balance_mutex);
3980 wait_event(fs_info->balance_wait_q,
3981 atomic_read(&fs_info->balance_running) == 0);
3982 mutex_lock(&fs_info->balance_mutex);
3983 } else {
3984 /* __cancel_balance needs volume_mutex */
3985 mutex_unlock(&fs_info->balance_mutex);
3986 mutex_lock(&fs_info->volume_mutex);
3987 mutex_lock(&fs_info->balance_mutex);
3988
3989 if (fs_info->balance_ctl)
3990 __cancel_balance(fs_info);
3991
3992 mutex_unlock(&fs_info->volume_mutex);
3993 }
3994
3995 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3996 atomic_dec(&fs_info->balance_cancel_req);
3997 mutex_unlock(&fs_info->balance_mutex);
3998 return 0;
3999}
4000
Stefan Behrens803b2f52013-08-15 17:11:21 +02004001static int btrfs_uuid_scan_kthread(void *data)
4002{
4003 struct btrfs_fs_info *fs_info = data;
4004 struct btrfs_root *root = fs_info->tree_root;
4005 struct btrfs_key key;
4006 struct btrfs_key max_key;
4007 struct btrfs_path *path = NULL;
4008 int ret = 0;
4009 struct extent_buffer *eb;
4010 int slot;
4011 struct btrfs_root_item root_item;
4012 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004013 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004014
4015 path = btrfs_alloc_path();
4016 if (!path) {
4017 ret = -ENOMEM;
4018 goto out;
4019 }
4020
4021 key.objectid = 0;
4022 key.type = BTRFS_ROOT_ITEM_KEY;
4023 key.offset = 0;
4024
4025 max_key.objectid = (u64)-1;
4026 max_key.type = BTRFS_ROOT_ITEM_KEY;
4027 max_key.offset = (u64)-1;
4028
Stefan Behrens803b2f52013-08-15 17:11:21 +02004029 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004030 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004031 if (ret) {
4032 if (ret > 0)
4033 ret = 0;
4034 break;
4035 }
4036
4037 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4038 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4039 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4040 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4041 goto skip;
4042
4043 eb = path->nodes[0];
4044 slot = path->slots[0];
4045 item_size = btrfs_item_size_nr(eb, slot);
4046 if (item_size < sizeof(root_item))
4047 goto skip;
4048
Stefan Behrens803b2f52013-08-15 17:11:21 +02004049 read_extent_buffer(eb, &root_item,
4050 btrfs_item_ptr_offset(eb, slot),
4051 (int)sizeof(root_item));
4052 if (btrfs_root_refs(&root_item) == 0)
4053 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004054
4055 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4056 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4057 if (trans)
4058 goto update_tree;
4059
4060 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004061 /*
4062 * 1 - subvol uuid item
4063 * 1 - received_subvol uuid item
4064 */
4065 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4066 if (IS_ERR(trans)) {
4067 ret = PTR_ERR(trans);
4068 break;
4069 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004070 continue;
4071 } else {
4072 goto skip;
4073 }
4074update_tree:
4075 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004076 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4077 root_item.uuid,
4078 BTRFS_UUID_KEY_SUBVOL,
4079 key.objectid);
4080 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004081 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004082 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004083 break;
4084 }
4085 }
4086
4087 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004088 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4089 root_item.received_uuid,
4090 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4091 key.objectid);
4092 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004093 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004094 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004095 break;
4096 }
4097 }
4098
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004099skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004100 if (trans) {
4101 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004102 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004103 if (ret)
4104 break;
4105 }
4106
Stefan Behrens803b2f52013-08-15 17:11:21 +02004107 btrfs_release_path(path);
4108 if (key.offset < (u64)-1) {
4109 key.offset++;
4110 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4111 key.offset = 0;
4112 key.type = BTRFS_ROOT_ITEM_KEY;
4113 } else if (key.objectid < (u64)-1) {
4114 key.offset = 0;
4115 key.type = BTRFS_ROOT_ITEM_KEY;
4116 key.objectid++;
4117 } else {
4118 break;
4119 }
4120 cond_resched();
4121 }
4122
4123out:
4124 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004125 if (trans && !IS_ERR(trans))
4126 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004127 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004128 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004129 else
4130 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004131 up(&fs_info->uuid_tree_rescan_sem);
4132 return 0;
4133}
4134
Stefan Behrens70f80172013-08-15 17:11:23 +02004135/*
4136 * Callback for btrfs_uuid_tree_iterate().
4137 * returns:
4138 * 0 check succeeded, the entry is not outdated.
Adam Buchbinderbb7ab3b2016-03-04 11:23:12 -08004139 * < 0 if an error occurred.
Stefan Behrens70f80172013-08-15 17:11:23 +02004140 * > 0 if the check failed, which means the caller shall remove the entry.
4141 */
4142static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4143 u8 *uuid, u8 type, u64 subid)
4144{
4145 struct btrfs_key key;
4146 int ret = 0;
4147 struct btrfs_root *subvol_root;
4148
4149 if (type != BTRFS_UUID_KEY_SUBVOL &&
4150 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4151 goto out;
4152
4153 key.objectid = subid;
4154 key.type = BTRFS_ROOT_ITEM_KEY;
4155 key.offset = (u64)-1;
4156 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4157 if (IS_ERR(subvol_root)) {
4158 ret = PTR_ERR(subvol_root);
4159 if (ret == -ENOENT)
4160 ret = 1;
4161 goto out;
4162 }
4163
4164 switch (type) {
4165 case BTRFS_UUID_KEY_SUBVOL:
4166 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4167 ret = 1;
4168 break;
4169 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4170 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4171 BTRFS_UUID_SIZE))
4172 ret = 1;
4173 break;
4174 }
4175
4176out:
4177 return ret;
4178}
4179
4180static int btrfs_uuid_rescan_kthread(void *data)
4181{
4182 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4183 int ret;
4184
4185 /*
4186 * 1st step is to iterate through the existing UUID tree and
4187 * to delete all entries that contain outdated data.
4188 * 2nd step is to add all missing entries to the UUID tree.
4189 */
4190 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4191 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004192 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004193 up(&fs_info->uuid_tree_rescan_sem);
4194 return ret;
4195 }
4196 return btrfs_uuid_scan_kthread(data);
4197}
4198
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004199int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4200{
4201 struct btrfs_trans_handle *trans;
4202 struct btrfs_root *tree_root = fs_info->tree_root;
4203 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004204 struct task_struct *task;
4205 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004206
4207 /*
4208 * 1 - root node
4209 * 1 - root item
4210 */
4211 trans = btrfs_start_transaction(tree_root, 2);
4212 if (IS_ERR(trans))
4213 return PTR_ERR(trans);
4214
4215 uuid_root = btrfs_create_tree(trans, fs_info,
4216 BTRFS_UUID_TREE_OBJECTID);
4217 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004218 ret = PTR_ERR(uuid_root);
4219 btrfs_abort_transaction(trans, tree_root, ret);
4220 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004221 }
4222
4223 fs_info->uuid_root = uuid_root;
4224
Stefan Behrens803b2f52013-08-15 17:11:21 +02004225 ret = btrfs_commit_transaction(trans, tree_root);
4226 if (ret)
4227 return ret;
4228
4229 down(&fs_info->uuid_tree_rescan_sem);
4230 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4231 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004232 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004233 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004234 up(&fs_info->uuid_tree_rescan_sem);
4235 return PTR_ERR(task);
4236 }
4237
4238 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004239}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004240
Stefan Behrens70f80172013-08-15 17:11:23 +02004241int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4242{
4243 struct task_struct *task;
4244
4245 down(&fs_info->uuid_tree_rescan_sem);
4246 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4247 if (IS_ERR(task)) {
4248 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004249 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004250 up(&fs_info->uuid_tree_rescan_sem);
4251 return PTR_ERR(task);
4252 }
4253
4254 return 0;
4255}
4256
Chris Mason8f18cf12008-04-25 16:53:30 -04004257/*
4258 * shrinking a device means finding all of the device extents past
4259 * the new size, and then following the back refs to the chunks.
4260 * The chunk relocation code actually frees the device extent
4261 */
4262int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4263{
4264 struct btrfs_trans_handle *trans;
4265 struct btrfs_root *root = device->dev_root;
4266 struct btrfs_dev_extent *dev_extent = NULL;
4267 struct btrfs_path *path;
4268 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004269 u64 chunk_offset;
4270 int ret;
4271 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004272 int failed = 0;
4273 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004274 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004275 struct extent_buffer *l;
4276 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004277 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004278 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004279 u64 old_size = btrfs_device_get_total_bytes(device);
4280 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004281
Stefan Behrens63a212a2012-11-05 18:29:28 +01004282 if (device->is_tgtdev_for_dev_replace)
4283 return -EINVAL;
4284
Chris Mason8f18cf12008-04-25 16:53:30 -04004285 path = btrfs_alloc_path();
4286 if (!path)
4287 return -ENOMEM;
4288
David Sterbae4058b52015-11-27 16:31:35 +01004289 path->reada = READA_FORWARD;
Chris Mason8f18cf12008-04-25 16:53:30 -04004290
Chris Mason7d9eb122008-07-08 14:19:17 -04004291 lock_chunks(root);
4292
Miao Xie7cc8e582014-09-03 21:35:38 +08004293 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004294 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004295 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004296 spin_lock(&root->fs_info->free_chunk_lock);
4297 root->fs_info->free_chunk_space -= diff;
4298 spin_unlock(&root->fs_info->free_chunk_lock);
4299 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004300 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004301
Josef Bacikba1bf482009-09-11 16:11:19 -04004302again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004303 key.objectid = device->devid;
4304 key.offset = (u64)-1;
4305 key.type = BTRFS_DEV_EXTENT_KEY;
4306
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004307 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004308 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004309 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004310 if (ret < 0) {
4311 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004312 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004313 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004314
4315 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004316 if (ret)
4317 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004318 if (ret < 0)
4319 goto done;
4320 if (ret) {
4321 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004322 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004323 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004324 }
4325
4326 l = path->nodes[0];
4327 slot = path->slots[0];
4328 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4329
Josef Bacikba1bf482009-09-11 16:11:19 -04004330 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004331 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004332 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004333 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004334 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004335
4336 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4337 length = btrfs_dev_extent_length(l, dev_extent);
4338
Josef Bacikba1bf482009-09-11 16:11:19 -04004339 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004340 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004341 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004342 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004343 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004344
Chris Mason8f18cf12008-04-25 16:53:30 -04004345 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004346 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004347
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004348 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004349 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004350 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004351 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004352 if (ret == -ENOSPC)
4353 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004354 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004355
4356 if (failed && !retried) {
4357 failed = 0;
4358 retried = true;
4359 goto again;
4360 } else if (failed && retried) {
4361 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004362 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004363 }
4364
Chris Balld6397ba2009-04-27 07:29:03 -04004365 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004366 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004367 if (IS_ERR(trans)) {
4368 ret = PTR_ERR(trans);
4369 goto done;
4370 }
4371
Chris Balld6397ba2009-04-27 07:29:03 -04004372 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004373
4374 /*
4375 * We checked in the above loop all device extents that were already in
4376 * the device tree. However before we have updated the device's
4377 * total_bytes to the new size, we might have had chunk allocations that
4378 * have not complete yet (new block groups attached to transaction
4379 * handles), and therefore their device extents were not yet in the
4380 * device tree and we missed them in the loop above. So if we have any
4381 * pending chunk using a device extent that overlaps the device range
4382 * that we can not use anymore, commit the current transaction and
4383 * repeat the search on the device tree - this way we guarantee we will
4384 * not have chunks using device extents that end beyond 'new_size'.
4385 */
4386 if (!checked_pending_chunks) {
4387 u64 start = new_size;
4388 u64 len = old_size - new_size;
4389
Jeff Mahoney499f3772015-06-15 09:41:17 -04004390 if (contains_pending_extent(trans->transaction, device,
4391 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004392 unlock_chunks(root);
4393 checked_pending_chunks = true;
4394 failed = 0;
4395 retried = false;
4396 ret = btrfs_commit_transaction(trans, root);
4397 if (ret)
4398 goto done;
4399 goto again;
4400 }
4401 }
4402
Miao Xie7cc8e582014-09-03 21:35:38 +08004403 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004404 if (list_empty(&device->resized_list))
4405 list_add_tail(&device->resized_list,
4406 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004407
Chris Balld6397ba2009-04-27 07:29:03 -04004408 WARN_ON(diff > old_total);
4409 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4410 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004411
4412 /* Now btrfs_update_device() will change the on-disk size. */
4413 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004414 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004415done:
4416 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004417 if (ret) {
4418 lock_chunks(root);
4419 btrfs_device_set_total_bytes(device, old_size);
4420 if (device->writeable)
4421 device->fs_devices->total_rw_bytes += diff;
4422 spin_lock(&root->fs_info->free_chunk_lock);
4423 root->fs_info->free_chunk_space += diff;
4424 spin_unlock(&root->fs_info->free_chunk_lock);
4425 unlock_chunks(root);
4426 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004427 return ret;
4428}
4429
Li Zefan125ccb02011-12-08 15:07:24 +08004430static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004431 struct btrfs_key *key,
4432 struct btrfs_chunk *chunk, int item_size)
4433{
David Sterba6c417612011-04-13 15:41:04 +02004434 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004435 struct btrfs_disk_key disk_key;
4436 u32 array_size;
4437 u8 *ptr;
4438
Miao Xiefe48a5c2014-09-03 21:35:39 +08004439 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004440 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004441 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004442 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4443 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004444 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004445 }
Chris Mason0b86a832008-03-24 15:01:56 -04004446
4447 ptr = super_copy->sys_chunk_array + array_size;
4448 btrfs_cpu_key_to_disk(&disk_key, key);
4449 memcpy(ptr, &disk_key, sizeof(disk_key));
4450 ptr += sizeof(disk_key);
4451 memcpy(ptr, chunk, item_size);
4452 item_size += sizeof(disk_key);
4453 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004454 unlock_chunks(root);
4455
Chris Mason0b86a832008-03-24 15:01:56 -04004456 return 0;
4457}
4458
Miao Xieb2117a32011-01-05 10:07:28 +00004459/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004460 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004461 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004462static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004463{
Arne Jansen73c5de02011-04-12 12:07:57 +02004464 const struct btrfs_device_info *di_a = a;
4465 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004466
Arne Jansen73c5de02011-04-12 12:07:57 +02004467 if (di_a->max_avail > di_b->max_avail)
4468 return -1;
4469 if (di_a->max_avail < di_b->max_avail)
4470 return 1;
4471 if (di_a->total_avail > di_b->total_avail)
4472 return -1;
4473 if (di_a->total_avail < di_b->total_avail)
4474 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004475 return 0;
4476}
4477
David Woodhouse53b381b2013-01-29 18:40:14 -05004478static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4479{
4480 /* TODO allow them to set a preferred stripe size */
Byongho Leeee221842015-12-15 01:42:10 +09004481 return SZ_64K;
David Woodhouse53b381b2013-01-29 18:40:14 -05004482}
4483
4484static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4485{
Zhao Leiffe2d202015-01-20 15:11:44 +08004486 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004487 return;
4488
Miao Xieceda0862013-04-11 10:30:16 +00004489 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004490}
4491
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004492#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4493 - sizeof(struct btrfs_item) \
4494 - sizeof(struct btrfs_chunk)) \
4495 / sizeof(struct btrfs_stripe) + 1)
4496
4497#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4498 - 2 * sizeof(struct btrfs_disk_key) \
4499 - 2 * sizeof(struct btrfs_chunk)) \
4500 / sizeof(struct btrfs_stripe) + 1)
4501
Miao Xieb2117a32011-01-05 10:07:28 +00004502static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004503 struct btrfs_root *extent_root, u64 start,
4504 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004505{
4506 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004507 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4508 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004509 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004510 struct extent_map_tree *em_tree;
4511 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004512 struct btrfs_device_info *devices_info = NULL;
4513 u64 total_avail;
4514 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004515 int data_stripes; /* number of stripes that count for
4516 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004517 int sub_stripes; /* sub_stripes info for map */
4518 int dev_stripes; /* stripes per dev */
4519 int devs_max; /* max devs to use */
4520 int devs_min; /* min devs needed */
4521 int devs_increment; /* ndevs has to be a multiple of this */
4522 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004523 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004524 u64 max_stripe_size;
4525 u64 max_chunk_size;
4526 u64 stripe_size;
4527 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004528 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004529 int ndevs;
4530 int i;
4531 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004532 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004533
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004534 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004535
Miao Xieb2117a32011-01-05 10:07:28 +00004536 if (list_empty(&fs_devices->alloc_list))
4537 return -ENOSPC;
4538
Liu Bo31e50222012-11-21 14:18:10 +00004539 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004540
Liu Bo31e50222012-11-21 14:18:10 +00004541 sub_stripes = btrfs_raid_array[index].sub_stripes;
4542 dev_stripes = btrfs_raid_array[index].dev_stripes;
4543 devs_max = btrfs_raid_array[index].devs_max;
4544 devs_min = btrfs_raid_array[index].devs_min;
4545 devs_increment = btrfs_raid_array[index].devs_increment;
4546 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004547
4548 if (type & BTRFS_BLOCK_GROUP_DATA) {
Byongho Leeee221842015-12-15 01:42:10 +09004549 max_stripe_size = SZ_1G;
Arne Jansen73c5de02011-04-12 12:07:57 +02004550 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004551 if (!devs_max)
4552 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004553 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004554 /* for larger filesystems, use larger metadata chunks */
Byongho Leeee221842015-12-15 01:42:10 +09004555 if (fs_devices->total_rw_bytes > 50ULL * SZ_1G)
4556 max_stripe_size = SZ_1G;
Chris Mason11003732012-01-06 15:47:38 -05004557 else
Byongho Leeee221842015-12-15 01:42:10 +09004558 max_stripe_size = SZ_256M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004559 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004560 if (!devs_max)
4561 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004562 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Byongho Leeee221842015-12-15 01:42:10 +09004563 max_stripe_size = SZ_32M;
Arne Jansen73c5de02011-04-12 12:07:57 +02004564 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004565 if (!devs_max)
4566 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004567 } else {
David Sterba351fd352014-05-15 16:48:20 +02004568 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004569 type);
4570 BUG_ON(1);
4571 }
4572
4573 /* we don't want a chunk larger than 10% of writeable space */
4574 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4575 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004576
David Sterba31e818f2015-02-20 18:00:26 +01004577 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004578 GFP_NOFS);
4579 if (!devices_info)
4580 return -ENOMEM;
4581
Arne Jansen73c5de02011-04-12 12:07:57 +02004582 cur = fs_devices->alloc_list.next;
4583
4584 /*
4585 * in the first pass through the devices list, we gather information
4586 * about the available holes on each device.
4587 */
4588 ndevs = 0;
4589 while (cur != &fs_devices->alloc_list) {
4590 struct btrfs_device *device;
4591 u64 max_avail;
4592 u64 dev_offset;
4593
4594 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4595
4596 cur = cur->next;
4597
4598 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004599 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004600 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004601 continue;
4602 }
4603
Stefan Behrens63a212a2012-11-05 18:29:28 +01004604 if (!device->in_fs_metadata ||
4605 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004606 continue;
4607
4608 if (device->total_bytes > device->bytes_used)
4609 total_avail = device->total_bytes - device->bytes_used;
4610 else
4611 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004612
4613 /* If there is no space on this device, skip it. */
4614 if (total_avail == 0)
4615 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004616
Josef Bacik6df9a952013-06-27 13:22:46 -04004617 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004618 max_stripe_size * dev_stripes,
4619 &dev_offset, &max_avail);
4620 if (ret && ret != -ENOSPC)
4621 goto error;
4622
4623 if (ret == 0)
4624 max_avail = max_stripe_size * dev_stripes;
4625
4626 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4627 continue;
4628
Eric Sandeen063d0062013-01-31 00:55:01 +00004629 if (ndevs == fs_devices->rw_devices) {
4630 WARN(1, "%s: found more than %llu devices\n",
4631 __func__, fs_devices->rw_devices);
4632 break;
4633 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004634 devices_info[ndevs].dev_offset = dev_offset;
4635 devices_info[ndevs].max_avail = max_avail;
4636 devices_info[ndevs].total_avail = total_avail;
4637 devices_info[ndevs].dev = device;
4638 ++ndevs;
4639 }
4640
4641 /*
4642 * now sort the devices by hole size / available space
4643 */
4644 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4645 btrfs_cmp_device_info, NULL);
4646
4647 /* round down to number of usable stripes */
4648 ndevs -= ndevs % devs_increment;
4649
4650 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4651 ret = -ENOSPC;
4652 goto error;
4653 }
4654
4655 if (devs_max && ndevs > devs_max)
4656 ndevs = devs_max;
4657 /*
4658 * the primary goal is to maximize the number of stripes, so use as many
4659 * devices as possible, even if the stripes are not maximum sized.
4660 */
4661 stripe_size = devices_info[ndevs-1].max_avail;
4662 num_stripes = ndevs * dev_stripes;
4663
David Woodhouse53b381b2013-01-29 18:40:14 -05004664 /*
4665 * this will have to be fixed for RAID1 and RAID10 over
4666 * more drives
4667 */
4668 data_stripes = num_stripes / ncopies;
4669
David Woodhouse53b381b2013-01-29 18:40:14 -05004670 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4671 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4672 btrfs_super_stripesize(info->super_copy));
4673 data_stripes = num_stripes - 1;
4674 }
4675 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4676 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4677 btrfs_super_stripesize(info->super_copy));
4678 data_stripes = num_stripes - 2;
4679 }
Chris Mason86db2572013-02-20 16:23:40 -05004680
4681 /*
4682 * Use the number of data stripes to figure out how big this chunk
4683 * is really going to be in terms of logical address space,
4684 * and compare that answer with the max chunk size
4685 */
4686 if (stripe_size * data_stripes > max_chunk_size) {
4687 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004688
4689 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004690
4691 /* bump the answer up to a 16MB boundary */
4692 stripe_size = (stripe_size + mask) & ~mask;
4693
4694 /* but don't go higher than the limits we found
4695 * while searching for free extents
4696 */
4697 if (stripe_size > devices_info[ndevs-1].max_avail)
4698 stripe_size = devices_info[ndevs-1].max_avail;
4699 }
4700
David Sterbab8b93ad2015-01-16 17:26:13 +01004701 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004702
4703 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004704 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004705 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004706
Miao Xieb2117a32011-01-05 10:07:28 +00004707 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4708 if (!map) {
4709 ret = -ENOMEM;
4710 goto error;
4711 }
4712 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004713
Arne Jansen73c5de02011-04-12 12:07:57 +02004714 for (i = 0; i < ndevs; ++i) {
4715 for (j = 0; j < dev_stripes; ++j) {
4716 int s = i * dev_stripes + j;
4717 map->stripes[s].dev = devices_info[i].dev;
4718 map->stripes[s].physical = devices_info[i].dev_offset +
4719 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004720 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004721 }
Chris Mason593060d2008-03-25 16:50:33 -04004722 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004723 map->stripe_len = raid_stripe_len;
4724 map->io_align = raid_stripe_len;
4725 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004726 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004727 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004728
David Woodhouse53b381b2013-01-29 18:40:14 -05004729 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004730
Arne Jansen73c5de02011-04-12 12:07:57 +02004731 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004732
David Sterba172ddd62011-04-21 00:48:27 +02004733 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004734 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004735 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004736 ret = -ENOMEM;
4737 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004738 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004739 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04004740 em->map_lookup = map;
Yan Zheng2b820322008-11-17 21:11:30 -05004741 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004742 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004743 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004744 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004745 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004746
Chris Mason0b86a832008-03-24 15:01:56 -04004747 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004748 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004749 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004750 if (!ret) {
4751 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4752 atomic_inc(&em->refs);
4753 }
Chris Mason890871b2009-09-02 16:24:52 -04004754 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004755 if (ret) {
4756 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004757 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004758 }
Yan Zheng2b820322008-11-17 21:11:30 -05004759
Josef Bacik04487482013-01-29 15:03:37 -05004760 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4761 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4762 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004763 if (ret)
4764 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004765
Miao Xie7cc8e582014-09-03 21:35:38 +08004766 for (i = 0; i < map->num_stripes; i++) {
4767 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4768 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4769 }
Miao Xie43530c42014-09-03 21:35:36 +08004770
Miao Xie1c116182014-09-03 21:35:37 +08004771 spin_lock(&extent_root->fs_info->free_chunk_lock);
4772 extent_root->fs_info->free_chunk_space -= (stripe_size *
4773 map->num_stripes);
4774 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4775
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004776 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004777 check_raid56_incompat_flag(extent_root->fs_info, type);
4778
Miao Xieb2117a32011-01-05 10:07:28 +00004779 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004780 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004781
Josef Bacik6df9a952013-06-27 13:22:46 -04004782error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004783 write_lock(&em_tree->lock);
4784 remove_extent_mapping(em_tree, em);
4785 write_unlock(&em_tree->lock);
4786
4787 /* One for our allocation */
4788 free_extent_map(em);
4789 /* One for the tree reference */
4790 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004791 /* One for the pending_chunks list reference */
4792 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004793error:
Miao Xieb2117a32011-01-05 10:07:28 +00004794 kfree(devices_info);
4795 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004796}
4797
Josef Bacik6df9a952013-06-27 13:22:46 -04004798int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004799 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004800 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004801{
Yan Zheng2b820322008-11-17 21:11:30 -05004802 struct btrfs_key key;
4803 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4804 struct btrfs_device *device;
4805 struct btrfs_chunk *chunk;
4806 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004807 struct extent_map_tree *em_tree;
4808 struct extent_map *em;
4809 struct map_lookup *map;
4810 size_t item_size;
4811 u64 dev_offset;
4812 u64 stripe_size;
4813 int i = 0;
Chris Mason140e6392015-12-23 13:30:51 -08004814 int ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004815
Josef Bacik6df9a952013-06-27 13:22:46 -04004816 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4817 read_lock(&em_tree->lock);
4818 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4819 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004820
Josef Bacik6df9a952013-06-27 13:22:46 -04004821 if (!em) {
4822 btrfs_crit(extent_root->fs_info, "unable to find logical "
4823 "%Lu len %Lu", chunk_offset, chunk_size);
4824 return -EINVAL;
4825 }
4826
4827 if (em->start != chunk_offset || em->len != chunk_size) {
4828 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004829 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004830 chunk_size, em->start, em->len);
4831 free_extent_map(em);
4832 return -EINVAL;
4833 }
4834
Jeff Mahoney95617d62015-06-03 10:55:48 -04004835 map = em->map_lookup;
Josef Bacik6df9a952013-06-27 13:22:46 -04004836 item_size = btrfs_chunk_item_size(map->num_stripes);
4837 stripe_size = em->orig_block_len;
4838
4839 chunk = kzalloc(item_size, GFP_NOFS);
4840 if (!chunk) {
4841 ret = -ENOMEM;
4842 goto out;
4843 }
4844
Filipe Manana50460e32015-11-20 10:42:47 +00004845 /*
4846 * Take the device list mutex to prevent races with the final phase of
4847 * a device replace operation that replaces the device object associated
4848 * with the map's stripes, because the device object's id can change
4849 * at any time during that final phase of the device replace operation
4850 * (dev-replace.c:btrfs_dev_replace_finishing()).
4851 */
4852 mutex_lock(&chunk_root->fs_info->fs_devices->device_list_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04004853 for (i = 0; i < map->num_stripes; i++) {
4854 device = map->stripes[i].dev;
4855 dev_offset = map->stripes[i].physical;
4856
Yan Zheng2b820322008-11-17 21:11:30 -05004857 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004858 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004859 break;
Josef Bacik6df9a952013-06-27 13:22:46 -04004860 ret = btrfs_alloc_dev_extent(trans, device,
4861 chunk_root->root_key.objectid,
4862 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4863 chunk_offset, dev_offset,
4864 stripe_size);
4865 if (ret)
Filipe Manana50460e32015-11-20 10:42:47 +00004866 break;
4867 }
4868 if (ret) {
4869 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex);
4870 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004871 }
4872
Yan Zheng2b820322008-11-17 21:11:30 -05004873 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004874 for (i = 0; i < map->num_stripes; i++) {
4875 device = map->stripes[i].dev;
4876 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004877
4878 btrfs_set_stack_stripe_devid(stripe, device->devid);
4879 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4880 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4881 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004882 }
Filipe Manana50460e32015-11-20 10:42:47 +00004883 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05004884
4885 btrfs_set_stack_chunk_length(chunk, chunk_size);
4886 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4887 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4888 btrfs_set_stack_chunk_type(chunk, map->type);
4889 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4890 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4891 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4892 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4893 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4894
4895 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4896 key.type = BTRFS_CHUNK_ITEM_KEY;
4897 key.offset = chunk_offset;
4898
4899 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004900 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4901 /*
4902 * TODO: Cleanup of inserted chunk root in case of
4903 * failure.
4904 */
Li Zefan125ccb02011-12-08 15:07:24 +08004905 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004906 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004907 }
liubo1abe9b82011-03-24 11:18:59 +00004908
Josef Bacik6df9a952013-06-27 13:22:46 -04004909out:
Yan Zheng2b820322008-11-17 21:11:30 -05004910 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004911 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004912 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004913}
4914
4915/*
4916 * Chunk allocation falls into two parts. The first part does works
4917 * that make the new allocated chunk useable, but not do any operation
4918 * that modifies the chunk tree. The second part does the works that
4919 * require modifying the chunk tree. This division is important for the
4920 * bootstrap process of adding storage to a seed btrfs.
4921 */
4922int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4923 struct btrfs_root *extent_root, u64 type)
4924{
4925 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004926
Filipe Mananaa9629592015-05-18 19:11:40 +01004927 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004928 chunk_offset = find_next_chunk(extent_root->fs_info);
4929 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004930}
4931
Chris Masond3977122009-01-05 21:25:51 -05004932static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004933 struct btrfs_root *root,
4934 struct btrfs_device *device)
4935{
4936 u64 chunk_offset;
4937 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004938 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004939 struct btrfs_fs_info *fs_info = root->fs_info;
4940 struct btrfs_root *extent_root = fs_info->extent_root;
4941 int ret;
4942
Josef Bacik6df9a952013-06-27 13:22:46 -04004943 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004944 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004945 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4946 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004947 if (ret)
4948 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004949
Josef Bacik6df9a952013-06-27 13:22:46 -04004950 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004951 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004952 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4953 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004954 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004955}
4956
Miao Xied20983b2014-07-03 18:22:13 +08004957static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4958{
4959 int max_errors;
4960
4961 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4962 BTRFS_BLOCK_GROUP_RAID10 |
4963 BTRFS_BLOCK_GROUP_RAID5 |
4964 BTRFS_BLOCK_GROUP_DUP)) {
4965 max_errors = 1;
4966 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4967 max_errors = 2;
4968 } else {
4969 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004970 }
4971
Miao Xied20983b2014-07-03 18:22:13 +08004972 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004973}
4974
4975int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4976{
4977 struct extent_map *em;
4978 struct map_lookup *map;
4979 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4980 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004981 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004982 int i;
4983
Chris Mason890871b2009-09-02 16:24:52 -04004984 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004985 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004986 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004987 if (!em)
4988 return 1;
4989
Jeff Mahoney95617d62015-06-03 10:55:48 -04004990 map = em->map_lookup;
Yan Zheng2b820322008-11-17 21:11:30 -05004991 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004992 if (map->stripes[i].dev->missing) {
4993 miss_ndevs++;
4994 continue;
4995 }
4996
Yan Zheng2b820322008-11-17 21:11:30 -05004997 if (!map->stripes[i].dev->writeable) {
4998 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004999 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05005000 }
5001 }
Miao Xied20983b2014-07-03 18:22:13 +08005002
5003 /*
5004 * If the number of missing devices is larger than max errors,
5005 * we can not write the data into that chunk successfully, so
5006 * set it readonly.
5007 */
5008 if (miss_ndevs > btrfs_chunk_max_errors(map))
5009 readonly = 1;
5010end:
Yan Zheng2b820322008-11-17 21:11:30 -05005011 free_extent_map(em);
5012 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04005013}
5014
5015void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
5016{
David Sterbaa8067e02011-04-21 00:34:43 +02005017 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04005018}
5019
5020void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
5021{
5022 struct extent_map *em;
5023
Chris Masond3977122009-01-05 21:25:51 -05005024 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04005025 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005026 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
5027 if (em)
5028 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005029 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005030 if (!em)
5031 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005032 /* once for us */
5033 free_extent_map(em);
5034 /* once for the tree */
5035 free_extent_map(em);
5036 }
5037}
5038
Stefan Behrens5d964052012-11-05 14:59:07 +01005039int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005040{
Stefan Behrens5d964052012-11-05 14:59:07 +01005041 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04005042 struct extent_map *em;
5043 struct map_lookup *map;
5044 struct extent_map_tree *em_tree = &map_tree->map_tree;
5045 int ret;
5046
Chris Mason890871b2009-09-02 16:24:52 -04005047 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005048 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04005049 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005050
Josef Bacikfb7669b2013-04-23 10:53:18 -04005051 /*
5052 * We could return errors for these cases, but that could get ugly and
5053 * we'd probably do the same thing which is just not do anything else
5054 * and exit, so return 1 so the callers don't try to use other copies.
5055 */
5056 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02005057 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005058 logical+len);
5059 return 1;
5060 }
5061
5062 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02005063 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02005064 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005065 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005066 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04005067 return 1;
5068 }
5069
Jeff Mahoney95617d62015-06-03 10:55:48 -04005070 map = em->map_lookup;
Chris Masonf1885912008-04-09 16:28:12 -04005071 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5072 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005073 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5074 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005075 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5076 ret = 2;
5077 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5078 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005079 else
5080 ret = 1;
5081 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005082
Liu Bo73beece2015-07-17 16:49:19 +08005083 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005084 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
5085 ret++;
Liu Bo73beece2015-07-17 16:49:19 +08005086 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005087
Chris Masonf1885912008-04-09 16:28:12 -04005088 return ret;
5089}
5090
David Woodhouse53b381b2013-01-29 18:40:14 -05005091unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
5092 struct btrfs_mapping_tree *map_tree,
5093 u64 logical)
5094{
5095 struct extent_map *em;
5096 struct map_lookup *map;
5097 struct extent_map_tree *em_tree = &map_tree->map_tree;
5098 unsigned long len = root->sectorsize;
5099
5100 read_lock(&em_tree->lock);
5101 em = lookup_extent_mapping(em_tree, logical, len);
5102 read_unlock(&em_tree->lock);
5103 BUG_ON(!em);
5104
5105 BUG_ON(em->start > logical || em->start + em->len < logical);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005106 map = em->map_lookup;
Zhao Leiffe2d202015-01-20 15:11:44 +08005107 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005108 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05005109 free_extent_map(em);
5110 return len;
5111}
5112
5113int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
5114 u64 logical, u64 len, int mirror_num)
5115{
5116 struct extent_map *em;
5117 struct map_lookup *map;
5118 struct extent_map_tree *em_tree = &map_tree->map_tree;
5119 int ret = 0;
5120
5121 read_lock(&em_tree->lock);
5122 em = lookup_extent_mapping(em_tree, logical, len);
5123 read_unlock(&em_tree->lock);
5124 BUG_ON(!em);
5125
5126 BUG_ON(em->start > logical || em->start + em->len < logical);
Jeff Mahoney95617d62015-06-03 10:55:48 -04005127 map = em->map_lookup;
Zhao Leiffe2d202015-01-20 15:11:44 +08005128 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005129 ret = 1;
5130 free_extent_map(em);
5131 return ret;
5132}
5133
Stefan Behrens30d98612012-11-06 14:52:18 +01005134static int find_live_mirror(struct btrfs_fs_info *fs_info,
5135 struct map_lookup *map, int first, int num,
5136 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005137{
5138 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005139 int tolerance;
5140 struct btrfs_device *srcdev;
5141
5142 if (dev_replace_is_ongoing &&
5143 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5144 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5145 srcdev = fs_info->dev_replace.srcdev;
5146 else
5147 srcdev = NULL;
5148
5149 /*
5150 * try to avoid the drive that is the source drive for a
5151 * dev-replace procedure, only choose it if no other non-missing
5152 * mirror is available
5153 */
5154 for (tolerance = 0; tolerance < 2; tolerance++) {
5155 if (map->stripes[optimal].dev->bdev &&
5156 (tolerance || map->stripes[optimal].dev != srcdev))
5157 return optimal;
5158 for (i = first; i < first + num; i++) {
5159 if (map->stripes[i].dev->bdev &&
5160 (tolerance || map->stripes[i].dev != srcdev))
5161 return i;
5162 }
Chris Masondfe25022008-05-13 13:46:40 -04005163 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005164
Chris Masondfe25022008-05-13 13:46:40 -04005165 /* we couldn't find one that doesn't fail. Just return something
5166 * and the io error handling code will clean up eventually
5167 */
5168 return optimal;
5169}
5170
David Woodhouse53b381b2013-01-29 18:40:14 -05005171static inline int parity_smaller(u64 a, u64 b)
5172{
5173 return a > b;
5174}
5175
5176/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005177static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005178{
5179 struct btrfs_bio_stripe s;
5180 int i;
5181 u64 l;
5182 int again = 1;
5183
5184 while (again) {
5185 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005186 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005187 if (parity_smaller(bbio->raid_map[i],
5188 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005189 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005190 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005191 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005192 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005193 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005194 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005195
David Woodhouse53b381b2013-01-29 18:40:14 -05005196 again = 1;
5197 }
5198 }
5199 }
5200}
5201
Zhao Lei6e9606d2015-01-20 15:11:34 +08005202static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5203{
5204 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005205 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005206 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005207 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005208 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005209 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005210 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005211 /*
5212 * plus the raid_map, which includes both the tgt dev
5213 * and the stripes
5214 */
5215 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005216 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005217
5218 atomic_set(&bbio->error, 0);
5219 atomic_set(&bbio->refs, 1);
5220
5221 return bbio;
5222}
5223
5224void btrfs_get_bbio(struct btrfs_bio *bbio)
5225{
5226 WARN_ON(!atomic_read(&bbio->refs));
5227 atomic_inc(&bbio->refs);
5228}
5229
5230void btrfs_put_bbio(struct btrfs_bio *bbio)
5231{
5232 if (!bbio)
5233 return;
5234 if (atomic_dec_and_test(&bbio->refs))
5235 kfree(bbio);
5236}
5237
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005238static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005239 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005240 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005241 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005242{
5243 struct extent_map *em;
5244 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005245 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005246 struct extent_map_tree *em_tree = &map_tree->map_tree;
5247 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005248 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005249 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005250 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005251 u64 stripe_nr_orig;
5252 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005253 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005254 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005255 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005256 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005257 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005258 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005259 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005260 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005261 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5262 int dev_replace_is_ongoing = 0;
5263 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005264 int patch_the_first_stripe_for_dev_replace = 0;
5265 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005266 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005267
Chris Mason890871b2009-09-02 16:24:52 -04005268 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005269 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005270 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005271
Chris Mason3b951512008-04-17 11:29:12 -04005272 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005273 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005274 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005275 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005276 }
Chris Mason0b86a832008-03-24 15:01:56 -04005277
Josef Bacik9bb91872013-04-19 23:45:33 -04005278 if (em->start > logical || em->start + em->len < logical) {
5279 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005280 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005281 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005282 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005283 return -EINVAL;
5284 }
5285
Jeff Mahoney95617d62015-06-03 10:55:48 -04005286 map = em->map_lookup;
Chris Mason0b86a832008-03-24 15:01:56 -04005287 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005288
David Woodhouse53b381b2013-01-29 18:40:14 -05005289 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005290 stripe_nr = offset;
5291 /*
5292 * stripe_nr counts the total number of stripes we have to stride
5293 * to get to this block
5294 */
David Sterba47c57132015-02-20 18:43:47 +01005295 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005296
David Woodhouse53b381b2013-01-29 18:40:14 -05005297 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005298 BUG_ON(offset < stripe_offset);
5299
5300 /* stripe_offset is the offset of this block in its stripe*/
5301 stripe_offset = offset - stripe_offset;
5302
David Woodhouse53b381b2013-01-29 18:40:14 -05005303 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005304 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005305 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5306 raid56_full_stripe_start = offset;
5307
5308 /* allow a write of a full stripe, but make sure we don't
5309 * allow straddling of stripes
5310 */
David Sterba47c57132015-02-20 18:43:47 +01005311 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5312 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005313 raid56_full_stripe_start *= full_stripe_len;
5314 }
5315
5316 if (rw & REQ_DISCARD) {
5317 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005318 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005319 ret = -EOPNOTSUPP;
5320 goto out;
5321 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005322 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005323 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5324 u64 max_len;
5325 /* For writes to RAID[56], allow a full stripeset across all disks.
5326 For other RAID types and for RAID[56] reads, just allow a single
5327 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005328 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005329 (rw & REQ_WRITE)) {
5330 max_len = stripe_len * nr_data_stripes(map) -
5331 (offset - raid56_full_stripe_start);
5332 } else {
5333 /* we limit the length of each bio to what fits in a stripe */
5334 max_len = stripe_len - stripe_offset;
5335 }
5336 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005337 } else {
5338 *length = em->len - offset;
5339 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005340
David Woodhouse53b381b2013-01-29 18:40:14 -05005341 /* This is for when we're called from btrfs_merge_bio_hook() and all
5342 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005343 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005344 goto out;
5345
Liu Bo73beece2015-07-17 16:49:19 +08005346 btrfs_dev_replace_lock(dev_replace, 0);
Stefan Behrens472262f2012-11-06 14:43:46 +01005347 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5348 if (!dev_replace_is_ongoing)
Liu Bo73beece2015-07-17 16:49:19 +08005349 btrfs_dev_replace_unlock(dev_replace, 0);
5350 else
5351 btrfs_dev_replace_set_lock_blocking(dev_replace);
Stefan Behrens472262f2012-11-06 14:43:46 +01005352
Stefan Behrensad6d6202012-11-06 15:06:47 +01005353 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5354 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5355 dev_replace->tgtdev != NULL) {
5356 /*
5357 * in dev-replace case, for repair case (that's the only
5358 * case where the mirror is selected explicitly when
5359 * calling btrfs_map_block), blocks left of the left cursor
5360 * can also be read from the target drive.
5361 * For REQ_GET_READ_MIRRORS, the target drive is added as
5362 * the last one to the array of stripes. For READ, it also
5363 * needs to be supported using the same mirror number.
5364 * If the requested block is not left of the left cursor,
5365 * EIO is returned. This can happen because btrfs_num_copies()
5366 * returns one more in the dev-replace case.
5367 */
5368 u64 tmp_length = *length;
5369 struct btrfs_bio *tmp_bbio = NULL;
5370 int tmp_num_stripes;
5371 u64 srcdev_devid = dev_replace->srcdev->devid;
5372 int index_srcdev = 0;
5373 int found = 0;
5374 u64 physical_of_found = 0;
5375
5376 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005377 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005378 if (ret) {
5379 WARN_ON(tmp_bbio != NULL);
5380 goto out;
5381 }
5382
5383 tmp_num_stripes = tmp_bbio->num_stripes;
5384 if (mirror_num > tmp_num_stripes) {
5385 /*
5386 * REQ_GET_READ_MIRRORS does not contain this
5387 * mirror, that means that the requested area
5388 * is not left of the left cursor
5389 */
5390 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005391 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005392 goto out;
5393 }
5394
5395 /*
5396 * process the rest of the function using the mirror_num
5397 * of the source drive. Therefore look it up first.
5398 * At the end, patch the device pointer to the one of the
5399 * target drive.
5400 */
5401 for (i = 0; i < tmp_num_stripes; i++) {
Zhao Lei94a97df2015-12-09 21:03:49 +08005402 if (tmp_bbio->stripes[i].dev->devid != srcdev_devid)
5403 continue;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005404
Zhao Lei94a97df2015-12-09 21:03:49 +08005405 /*
5406 * In case of DUP, in order to keep it simple, only add
5407 * the mirror with the lowest physical address
5408 */
5409 if (found &&
5410 physical_of_found <= tmp_bbio->stripes[i].physical)
5411 continue;
5412
5413 index_srcdev = i;
5414 found = 1;
5415 physical_of_found = tmp_bbio->stripes[i].physical;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005416 }
5417
Zhao Lei6e9606d2015-01-20 15:11:34 +08005418 btrfs_put_bbio(tmp_bbio);
Zhao Lei94a97df2015-12-09 21:03:49 +08005419
5420 if (!found) {
5421 WARN_ON(1);
5422 ret = -EIO;
5423 goto out;
5424 }
5425
5426 mirror_num = index_srcdev + 1;
5427 patch_the_first_stripe_for_dev_replace = 1;
5428 physical_to_patch_in_first_stripe = physical_of_found;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005429 } else if (mirror_num > map->num_stripes) {
5430 mirror_num = 0;
5431 }
5432
Chris Masonf2d8d742008-04-21 10:03:05 -04005433 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005434 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005435 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005436 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005437 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005438 stripe_end_offset = stripe_nr_end * map->stripe_len -
5439 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005440
Li Dongyangfce3bb92011-03-24 10:24:26 +00005441 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5442 if (rw & REQ_DISCARD)
5443 num_stripes = min_t(u64, map->num_stripes,
5444 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005445 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5446 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005447 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5448 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005449 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005450 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005451 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005452 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005453 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005454 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005455 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005456 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005457 current->pid % map->num_stripes,
5458 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005459 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005460 }
Chris Mason2fff7342008-04-29 14:12:09 -04005461
Chris Mason611f0e02008-04-03 16:29:03 -04005462 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005463 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005464 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005465 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005466 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005467 } else {
5468 mirror_num = 1;
5469 }
Chris Mason2fff7342008-04-29 14:12:09 -04005470
Chris Mason321aecc2008-04-16 10:49:51 -04005471 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005472 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005473
David Sterba47c57132015-02-20 18:43:47 +01005474 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005475 stripe_index *= map->sub_stripes;
5476
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005477 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005478 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005479 else if (rw & REQ_DISCARD)
5480 num_stripes = min_t(u64, map->sub_stripes *
5481 (stripe_nr_end - stripe_nr_orig),
5482 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005483 else if (mirror_num)
5484 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005485 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005486 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005487 stripe_index = find_live_mirror(fs_info, map,
5488 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005489 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005490 current->pid % map->sub_stripes,
5491 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005492 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005493 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005494
Zhao Leiffe2d202015-01-20 15:11:44 +08005495 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005496 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005497 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5498 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005499 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005500 stripe_nr = div_u64(raid56_full_stripe_start,
5501 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005502
5503 /* RAID[56] write or recovery. Return all stripes */
5504 num_stripes = map->num_stripes;
5505 max_errors = nr_parity_stripes(map);
5506
David Woodhouse53b381b2013-01-29 18:40:14 -05005507 *length = map->stripe_len;
5508 stripe_index = 0;
5509 stripe_offset = 0;
5510 } else {
5511 /*
5512 * Mirror #0 or #1 means the original data block.
5513 * Mirror #2 is RAID5 parity block.
5514 * Mirror #3 is RAID6 Q block.
5515 */
David Sterba47c57132015-02-20 18:43:47 +01005516 stripe_nr = div_u64_rem(stripe_nr,
5517 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005518 if (mirror_num > 1)
5519 stripe_index = nr_data_stripes(map) +
5520 mirror_num - 2;
5521
5522 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005523 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5524 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005525 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5526 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5527 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005528 }
Chris Mason8790d502008-04-03 16:29:03 -04005529 } else {
5530 /*
David Sterba47c57132015-02-20 18:43:47 +01005531 * after this, stripe_nr is the number of stripes on this
5532 * device we have to walk to find the data, and stripe_index is
5533 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005534 */
David Sterba47c57132015-02-20 18:43:47 +01005535 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5536 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005537 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005538 }
Chris Mason593060d2008-03-25 16:50:33 -04005539 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005540
Stefan Behrens472262f2012-11-06 14:43:46 +01005541 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005542 if (dev_replace_is_ongoing) {
5543 if (rw & (REQ_WRITE | REQ_DISCARD))
5544 num_alloc_stripes <<= 1;
5545 if (rw & REQ_GET_READ_MIRRORS)
5546 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005547 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005548 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005549
Zhao Lei6e9606d2015-01-20 15:11:34 +08005550 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005551 if (!bbio) {
5552 ret = -ENOMEM;
5553 goto out;
5554 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005555 if (dev_replace_is_ongoing)
5556 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005557
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005558 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005559 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005560 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5561 mirror_num > 1)) {
5562 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005563 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005564
5565 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5566 sizeof(struct btrfs_bio_stripe) *
5567 num_alloc_stripes +
5568 sizeof(int) * tgtdev_indexes);
5569
5570 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005571 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005572
5573 /* Fill in the logical address of each stripe */
5574 tmp = stripe_nr * nr_data_stripes(map);
5575 for (i = 0; i < nr_data_stripes(map); i++)
5576 bbio->raid_map[(i+rot) % num_stripes] =
5577 em->start + (tmp + i) * map->stripe_len;
5578
5579 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5580 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5581 bbio->raid_map[(i+rot+1) % num_stripes] =
5582 RAID6_Q_STRIPE;
5583 }
5584
Li Dongyangfce3bb92011-03-24 10:24:26 +00005585 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005586 u32 factor = 0;
5587 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005588 u64 stripes_per_dev = 0;
5589 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005590 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005591
5592 if (map->type &
5593 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5594 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5595 sub_stripes = 1;
5596 else
5597 sub_stripes = map->sub_stripes;
5598
5599 factor = map->num_stripes / sub_stripes;
5600 stripes_per_dev = div_u64_rem(stripe_nr_end -
5601 stripe_nr_orig,
5602 factor,
5603 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005604 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5605 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005606 }
5607
Li Dongyangfce3bb92011-03-24 10:24:26 +00005608 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005609 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005610 map->stripes[stripe_index].physical +
5611 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005612 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005613
Li Zefanec9ef7a2011-12-01 14:06:42 +08005614 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5615 BTRFS_BLOCK_GROUP_RAID10)) {
5616 bbio->stripes[i].length = stripes_per_dev *
5617 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005618
Li Zefanec9ef7a2011-12-01 14:06:42 +08005619 if (i / sub_stripes < remaining_stripes)
5620 bbio->stripes[i].length +=
5621 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005622
5623 /*
5624 * Special for the first stripe and
5625 * the last stripe:
5626 *
5627 * |-------|...|-------|
5628 * |----------|
5629 * off end_off
5630 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005631 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005632 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005633 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005634
5635 if (stripe_index >= last_stripe &&
5636 stripe_index <= (last_stripe +
5637 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005638 bbio->stripes[i].length -=
5639 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005640
Li Zefanec9ef7a2011-12-01 14:06:42 +08005641 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005642 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005643 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005644 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005645
5646 stripe_index++;
5647 if (stripe_index == map->num_stripes) {
5648 /* This could only happen for RAID0/10 */
5649 stripe_index = 0;
5650 stripe_nr++;
5651 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005652 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005653 } else {
5654 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005655 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005656 map->stripes[stripe_index].physical +
5657 stripe_offset +
5658 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005659 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005660 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005661 stripe_index++;
5662 }
Chris Mason593060d2008-03-25 16:50:33 -04005663 }
Li Zefande11cc12011-12-01 12:55:47 +08005664
Miao Xied20983b2014-07-03 18:22:13 +08005665 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5666 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005667
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005668 if (bbio->raid_map)
5669 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005670
Miao Xie2c8cdd62014-11-14 16:06:25 +08005671 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005672 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5673 dev_replace->tgtdev != NULL) {
5674 int index_where_to_add;
5675 u64 srcdev_devid = dev_replace->srcdev->devid;
5676
5677 /*
5678 * duplicate the write operations while the dev replace
5679 * procedure is running. Since the copying of the old disk
5680 * to the new disk takes place at run time while the
5681 * filesystem is mounted writable, the regular write
5682 * operations to the old disk have to be duplicated to go
5683 * to the new disk as well.
5684 * Note that device->missing is handled by the caller, and
5685 * that the write to the old disk is already set up in the
5686 * stripes array.
5687 */
5688 index_where_to_add = num_stripes;
5689 for (i = 0; i < num_stripes; i++) {
5690 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5691 /* write to new disk, too */
5692 struct btrfs_bio_stripe *new =
5693 bbio->stripes + index_where_to_add;
5694 struct btrfs_bio_stripe *old =
5695 bbio->stripes + i;
5696
5697 new->physical = old->physical;
5698 new->length = old->length;
5699 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005700 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005701 index_where_to_add++;
5702 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005703 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005704 }
5705 }
5706 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005707 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5708 dev_replace->tgtdev != NULL) {
5709 u64 srcdev_devid = dev_replace->srcdev->devid;
5710 int index_srcdev = 0;
5711 int found = 0;
5712 u64 physical_of_found = 0;
5713
5714 /*
5715 * During the dev-replace procedure, the target drive can
5716 * also be used to read data in case it is needed to repair
5717 * a corrupt block elsewhere. This is possible if the
5718 * requested area is left of the left cursor. In this area,
5719 * the target drive is a full copy of the source drive.
5720 */
5721 for (i = 0; i < num_stripes; i++) {
5722 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5723 /*
5724 * In case of DUP, in order to keep it
5725 * simple, only add the mirror with the
5726 * lowest physical address
5727 */
5728 if (found &&
5729 physical_of_found <=
5730 bbio->stripes[i].physical)
5731 continue;
5732 index_srcdev = i;
5733 found = 1;
5734 physical_of_found = bbio->stripes[i].physical;
5735 }
5736 }
5737 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005738 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005739 dev_replace->cursor_left) {
5740 struct btrfs_bio_stripe *tgtdev_stripe =
5741 bbio->stripes + num_stripes;
5742
5743 tgtdev_stripe->physical = physical_of_found;
5744 tgtdev_stripe->length =
5745 bbio->stripes[index_srcdev].length;
5746 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005747 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005748
Miao Xie2c8cdd62014-11-14 16:06:25 +08005749 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005750 num_stripes++;
5751 }
5752 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005753 }
5754
Li Zefande11cc12011-12-01 12:55:47 +08005755 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005756 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005757 bbio->num_stripes = num_stripes;
5758 bbio->max_errors = max_errors;
5759 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005760 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005761
5762 /*
5763 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5764 * mirror_num == num_stripes + 1 && dev_replace target drive is
5765 * available as a mirror
5766 */
5767 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5768 WARN_ON(num_stripes > 1);
5769 bbio->stripes[0].dev = dev_replace->tgtdev;
5770 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5771 bbio->mirror_num = map->num_stripes + 1;
5772 }
Chris Masoncea9e442008-04-09 16:28:12 -04005773out:
Liu Bo73beece2015-07-17 16:49:19 +08005774 if (dev_replace_is_ongoing) {
5775 btrfs_dev_replace_clear_lock_blocking(dev_replace);
5776 btrfs_dev_replace_unlock(dev_replace, 0);
5777 }
Chris Mason0b86a832008-03-24 15:01:56 -04005778 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005779 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005780}
5781
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005782int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005783 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005784 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005785{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005786 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005787 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005788}
5789
Miao Xieaf8e2d12014-10-23 14:42:50 +08005790/* For Scrub/replace */
5791int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5792 u64 logical, u64 *length,
5793 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005794 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005795{
5796 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005797 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005798}
5799
Yan Zhenga512bbf2008-12-08 16:46:26 -05005800int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5801 u64 chunk_start, u64 physical, u64 devid,
5802 u64 **logical, int *naddrs, int *stripe_len)
5803{
5804 struct extent_map_tree *em_tree = &map_tree->map_tree;
5805 struct extent_map *em;
5806 struct map_lookup *map;
5807 u64 *buf;
5808 u64 bytenr;
5809 u64 length;
5810 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005811 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005812 int i, j, nr = 0;
5813
Chris Mason890871b2009-09-02 16:24:52 -04005814 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005815 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005816 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005817
Josef Bacik835d9742013-03-19 12:13:25 -04005818 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005819 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005820 chunk_start);
5821 return -EIO;
5822 }
5823
5824 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005825 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005826 em->start, chunk_start);
5827 free_extent_map(em);
5828 return -EIO;
5829 }
Jeff Mahoney95617d62015-06-03 10:55:48 -04005830 map = em->map_lookup;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005831
5832 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005833 rmap_len = map->stripe_len;
5834
Yan Zhenga512bbf2008-12-08 16:46:26 -05005835 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005836 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005837 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005838 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005839 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005840 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005841 rmap_len = map->stripe_len * nr_data_stripes(map);
5842 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005843
David Sterba31e818f2015-02-20 18:00:26 +01005844 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005845 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005846
5847 for (i = 0; i < map->num_stripes; i++) {
5848 if (devid && map->stripes[i].dev->devid != devid)
5849 continue;
5850 if (map->stripes[i].physical > physical ||
5851 map->stripes[i].physical + length <= physical)
5852 continue;
5853
5854 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005855 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005856
5857 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5858 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005859 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005860 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5861 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005862 } /* else if RAID[56], multiply by nr_data_stripes().
5863 * Alternatively, just use rmap_len below instead of
5864 * map->stripe_len */
5865
5866 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005867 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005868 for (j = 0; j < nr; j++) {
5869 if (buf[j] == bytenr)
5870 break;
5871 }
Chris Mason934d3752008-12-08 16:43:10 -05005872 if (j == nr) {
5873 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005874 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005875 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005876 }
5877
Yan Zhenga512bbf2008-12-08 16:46:26 -05005878 *logical = buf;
5879 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005880 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005881
5882 free_extent_map(em);
5883 return 0;
5884}
5885
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005886static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005887{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005888 bio->bi_private = bbio->private;
5889 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005890 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005891
Zhao Lei6e9606d2015-01-20 15:11:34 +08005892 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005893}
5894
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005895static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005896{
Chris Mason9be33952013-05-17 18:30:14 -04005897 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005898 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005899
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005900 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005901 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005902 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005903 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005904 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005905 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005906
5907 BUG_ON(stripe_index >= bbio->num_stripes);
5908 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005909 if (dev->bdev) {
5910 if (bio->bi_rw & WRITE)
5911 btrfs_dev_stat_inc(dev,
5912 BTRFS_DEV_STAT_WRITE_ERRS);
5913 else
5914 btrfs_dev_stat_inc(dev,
5915 BTRFS_DEV_STAT_READ_ERRS);
5916 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5917 btrfs_dev_stat_inc(dev,
5918 BTRFS_DEV_STAT_FLUSH_ERRS);
5919 btrfs_dev_stat_print_on_error(dev);
5920 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005921 }
5922 }
Chris Mason8790d502008-04-03 16:29:03 -04005923
Jan Schmidta1d3c472011-08-04 17:15:33 +02005924 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005925 is_orig_bio = 1;
5926
Miao Xiec404e0d2014-01-30 16:46:55 +08005927 btrfs_bio_counter_dec(bbio->fs_info);
5928
Jan Schmidta1d3c472011-08-04 17:15:33 +02005929 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005930 if (!is_orig_bio) {
5931 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005932 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005933 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005934
Chris Mason9be33952013-05-17 18:30:14 -04005935 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005936 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005937 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005938 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005939 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005940 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005941 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005942 /*
5943 * this bio is actually up to date, we didn't
5944 * go over the max number of errors
5945 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005946 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005947 }
Miao Xiec55f1392014-06-19 10:42:54 +08005948
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005949 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005950 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005951 bio_put(bio);
5952 }
Chris Mason8790d502008-04-03 16:29:03 -04005953}
5954
Chris Mason8b712842008-06-11 16:50:36 -04005955/*
5956 * see run_scheduled_bios for a description of why bios are collected for
5957 * async submit.
5958 *
5959 * This will add one bio to the pending list for a device and make sure
5960 * the work struct is scheduled.
5961 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005962static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5963 struct btrfs_device *device,
5964 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005965{
5966 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005967 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005968
David Woodhouse53b381b2013-01-29 18:40:14 -05005969 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005970 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005971 return;
5972 }
5973
Chris Mason8b712842008-06-11 16:50:36 -04005974 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005975 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005976 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005977 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005978 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005979 return;
Chris Mason8b712842008-06-11 16:50:36 -04005980 }
5981
5982 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005983 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005984 * higher layers. Otherwise, the async bio makes it appear we have
5985 * made progress against dirty pages when we've really just put it
5986 * on a queue for later
5987 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005988 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005989 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005990 bio->bi_next = NULL;
5991 bio->bi_rw |= rw;
5992
5993 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005994 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005995 pending_bios = &device->pending_sync_bios;
5996 else
5997 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005998
Chris Masonffbd5172009-04-20 15:50:09 -04005999 if (pending_bios->tail)
6000 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006001
Chris Masonffbd5172009-04-20 15:50:09 -04006002 pending_bios->tail = bio;
6003 if (!pending_bios->head)
6004 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04006005 if (device->running_pending)
6006 should_queue = 0;
6007
6008 spin_unlock(&device->io_lock);
6009
6010 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08006011 btrfs_queue_work(root->fs_info->submit_workers,
6012 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04006013}
6014
Josef Bacikde1ee922012-10-19 16:50:56 -04006015static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
6016 struct bio *bio, u64 physical, int dev_nr,
6017 int rw, int async)
6018{
6019 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
6020
6021 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04006022 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04006023 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006024 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04006025#ifdef DEBUG
6026 {
6027 struct rcu_string *name;
6028
6029 rcu_read_lock();
6030 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00006031 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04006032 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02006033 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
6034 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006035 rcu_read_unlock();
6036 }
6037#endif
6038 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08006039
6040 btrfs_bio_counter_inc_noblocked(root->fs_info);
6041
Josef Bacikde1ee922012-10-19 16:50:56 -04006042 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05006043 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006044 else
6045 btrfsic_submit_bio(rw, bio);
6046}
6047
Josef Bacikde1ee922012-10-19 16:50:56 -04006048static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6049{
6050 atomic_inc(&bbio->error);
6051 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08006052 /* Shoud be the original bio. */
6053 WARN_ON(bio != bbio->orig_bio);
6054
Chris Mason9be33952013-05-17 18:30:14 -04006055 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006056 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006057 bio->bi_error = -EIO;
6058 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006059 }
6060}
6061
Chris Masonf1885912008-04-09 16:28:12 -04006062int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04006063 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006064{
Chris Mason0b86a832008-03-24 15:01:56 -04006065 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006066 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006067 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006068 u64 length = 0;
6069 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006070 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006071 int dev_nr;
6072 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006073 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006074
Kent Overstreet4f024f32013-10-11 15:44:27 -07006075 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006076 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006077
Miao Xiec404e0d2014-01-30 16:46:55 +08006078 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05006079 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006080 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006081 if (ret) {
6082 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006083 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08006084 }
Chris Masoncea9e442008-04-09 16:28:12 -04006085
Jan Schmidta1d3c472011-08-04 17:15:33 +02006086 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006087 bbio->orig_bio = first_bio;
6088 bbio->private = first_bio->bi_private;
6089 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08006090 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006091 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6092
Zhao Leiad1ba2a2015-12-15 18:18:09 +08006093 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6094 ((rw & WRITE) || (mirror_num > 1))) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006095 /* In this case, map_length has been set to the length of
6096 a single stripe; not the whole write */
6097 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006098 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006099 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006100 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08006101 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006102 }
Miao Xie42452152014-11-25 16:39:28 +08006103
Miao Xiec404e0d2014-01-30 16:46:55 +08006104 btrfs_bio_counter_dec(root->fs_info);
6105 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05006106 }
6107
Chris Masoncea9e442008-04-09 16:28:12 -04006108 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006109 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006110 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006111 BUG();
6112 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006113
Zhao Lei08da7572015-02-12 15:42:16 +08006114 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006115 dev = bbio->stripes[dev_nr].dev;
6116 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
6117 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006118 continue;
6119 }
6120
Jan Schmidta1d3c472011-08-04 17:15:33 +02006121 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04006122 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006123 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04006124 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006125 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006126
Josef Bacikde1ee922012-10-19 16:50:56 -04006127 submit_stripe_bio(root, bbio, bio,
6128 bbio->stripes[dev_nr].physical, dev_nr, rw,
6129 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006130 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006131 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006132 return 0;
6133}
6134
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006135struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006136 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006137{
Yan Zheng2b820322008-11-17 21:11:30 -05006138 struct btrfs_device *device;
6139 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006140
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006141 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006142 while (cur_devices) {
6143 if (!fsid ||
6144 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6145 device = __find_device(&cur_devices->devices,
6146 devid, uuid);
6147 if (device)
6148 return device;
6149 }
6150 cur_devices = cur_devices->seed;
6151 }
6152 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006153}
6154
Chris Masondfe25022008-05-13 13:46:40 -04006155static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006156 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006157 u64 devid, u8 *dev_uuid)
6158{
6159 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006160
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006161 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6162 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006163 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006164
6165 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006166 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006167 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006168
6169 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006170 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006171
Chris Masondfe25022008-05-13 13:46:40 -04006172 return device;
6173}
6174
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006175/**
6176 * btrfs_alloc_device - allocate struct btrfs_device
6177 * @fs_info: used only for generating a new devid, can be NULL if
6178 * devid is provided (i.e. @devid != NULL).
6179 * @devid: a pointer to devid for this device. If NULL a new devid
6180 * is generated.
6181 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6182 * is generated.
6183 *
6184 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6185 * on error. Returned struct is not linked onto any lists and can be
6186 * destroyed with kfree() right away.
6187 */
6188struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6189 const u64 *devid,
6190 const u8 *uuid)
6191{
6192 struct btrfs_device *dev;
6193 u64 tmp;
6194
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306195 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006196 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006197
6198 dev = __alloc_device();
6199 if (IS_ERR(dev))
6200 return dev;
6201
6202 if (devid)
6203 tmp = *devid;
6204 else {
6205 int ret;
6206
6207 ret = find_next_devid(fs_info, &tmp);
6208 if (ret) {
6209 kfree(dev);
6210 return ERR_PTR(ret);
6211 }
6212 }
6213 dev->devid = tmp;
6214
6215 if (uuid)
6216 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6217 else
6218 generate_random_uuid(dev->uuid);
6219
Liu Bo9e0af232014-08-15 23:36:53 +08006220 btrfs_init_work(&dev->work, btrfs_submit_helper,
6221 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006222
6223 return dev;
6224}
6225
Chris Mason0b86a832008-03-24 15:01:56 -04006226static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6227 struct extent_buffer *leaf,
6228 struct btrfs_chunk *chunk)
6229{
6230 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6231 struct map_lookup *map;
6232 struct extent_map *em;
6233 u64 logical;
6234 u64 length;
Qu Wenruof04b7722015-12-15 09:14:37 +08006235 u64 stripe_len;
Chris Mason0b86a832008-03-24 15:01:56 -04006236 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006237 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006238 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006239 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006240 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006241
Chris Masone17cade2008-04-15 15:41:47 -04006242 logical = key->offset;
6243 length = btrfs_chunk_length(leaf, chunk);
Qu Wenruof04b7722015-12-15 09:14:37 +08006244 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6245 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6246 /* Validation check */
6247 if (!num_stripes) {
6248 btrfs_err(root->fs_info, "invalid chunk num_stripes: %u",
6249 num_stripes);
6250 return -EIO;
6251 }
6252 if (!IS_ALIGNED(logical, root->sectorsize)) {
6253 btrfs_err(root->fs_info,
6254 "invalid chunk logical %llu", logical);
6255 return -EIO;
6256 }
6257 if (!length || !IS_ALIGNED(length, root->sectorsize)) {
6258 btrfs_err(root->fs_info,
6259 "invalid chunk length %llu", length);
6260 return -EIO;
6261 }
6262 if (!is_power_of_2(stripe_len)) {
6263 btrfs_err(root->fs_info, "invalid chunk stripe length: %llu",
6264 stripe_len);
6265 return -EIO;
6266 }
6267 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6268 btrfs_chunk_type(leaf, chunk)) {
6269 btrfs_err(root->fs_info, "unrecognized chunk type: %llu",
6270 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6271 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6272 btrfs_chunk_type(leaf, chunk));
6273 return -EIO;
6274 }
Chris Masona061fc82008-05-07 11:43:44 -04006275
Chris Mason890871b2009-09-02 16:24:52 -04006276 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006277 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006278 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006279
6280 /* already mapped? */
6281 if (em && em->start <= logical && em->start + em->len > logical) {
6282 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006283 return 0;
6284 } else if (em) {
6285 free_extent_map(em);
6286 }
Chris Mason0b86a832008-03-24 15:01:56 -04006287
David Sterba172ddd62011-04-21 00:48:27 +02006288 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006289 if (!em)
6290 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006291 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006292 if (!map) {
6293 free_extent_map(em);
6294 return -ENOMEM;
6295 }
6296
Wang Shilong298a8f92014-06-19 10:42:52 +08006297 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Jeff Mahoney95617d62015-06-03 10:55:48 -04006298 em->map_lookup = map;
Chris Mason0b86a832008-03-24 15:01:56 -04006299 em->start = logical;
6300 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006301 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006302 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006303 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006304
Chris Mason593060d2008-03-25 16:50:33 -04006305 map->num_stripes = num_stripes;
6306 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6307 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6308 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6309 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6310 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006311 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006312 for (i = 0; i < num_stripes; i++) {
6313 map->stripes[i].physical =
6314 btrfs_stripe_offset_nr(leaf, chunk, i);
6315 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006316 read_extent_buffer(leaf, uuid, (unsigned long)
6317 btrfs_stripe_dev_uuid_nr(chunk, i),
6318 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006319 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6320 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006321 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006322 free_extent_map(em);
6323 return -EIO;
6324 }
Chris Masondfe25022008-05-13 13:46:40 -04006325 if (!map->stripes[i].dev) {
6326 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006327 add_missing_dev(root, root->fs_info->fs_devices,
6328 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006329 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006330 free_extent_map(em);
6331 return -EIO;
6332 }
Anand Jain816fceb2015-04-27 12:46:18 +08006333 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6334 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006335 }
6336 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006337 }
6338
Chris Mason890871b2009-09-02 16:24:52 -04006339 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006340 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006341 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006342 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006343 free_extent_map(em);
6344
6345 return 0;
6346}
6347
Jeff Mahoney143bede2012-03-01 14:56:26 +01006348static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006349 struct btrfs_dev_item *dev_item,
6350 struct btrfs_device *device)
6351{
6352 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006353
6354 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006355 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6356 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006357 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006358 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006359 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006360 device->type = btrfs_device_type(leaf, dev_item);
6361 device->io_align = btrfs_device_io_align(leaf, dev_item);
6362 device->io_width = btrfs_device_io_width(leaf, dev_item);
6363 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006364 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006365 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006366
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006367 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006368 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006369}
6370
Miao Xie5f375832014-09-03 21:35:46 +08006371static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6372 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006373{
6374 struct btrfs_fs_devices *fs_devices;
6375 int ret;
6376
Li Zefanb367e472011-12-07 11:38:24 +08006377 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006378
6379 fs_devices = root->fs_info->fs_devices->seed;
6380 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006381 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6382 return fs_devices;
6383
Yan Zheng2b820322008-11-17 21:11:30 -05006384 fs_devices = fs_devices->seed;
6385 }
6386
6387 fs_devices = find_fsid(fsid);
6388 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006389 if (!btrfs_test_opt(root, DEGRADED))
6390 return ERR_PTR(-ENOENT);
6391
6392 fs_devices = alloc_fs_devices(fsid);
6393 if (IS_ERR(fs_devices))
6394 return fs_devices;
6395
6396 fs_devices->seeding = 1;
6397 fs_devices->opened = 1;
6398 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006399 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006400
6401 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006402 if (IS_ERR(fs_devices))
6403 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006404
Christoph Hellwig97288f22008-12-02 06:36:09 -05006405 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006406 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006407 if (ret) {
6408 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006409 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006410 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006411 }
Yan Zheng2b820322008-11-17 21:11:30 -05006412
6413 if (!fs_devices->seeding) {
6414 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006415 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006416 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006417 goto out;
6418 }
6419
6420 fs_devices->seed = root->fs_info->fs_devices->seed;
6421 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006422out:
Miao Xie5f375832014-09-03 21:35:46 +08006423 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006424}
6425
Chris Mason0d81ba52008-03-24 15:02:07 -04006426static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006427 struct extent_buffer *leaf,
6428 struct btrfs_dev_item *dev_item)
6429{
Miao Xie5f375832014-09-03 21:35:46 +08006430 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006431 struct btrfs_device *device;
6432 u64 devid;
6433 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006434 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006435 u8 dev_uuid[BTRFS_UUID_SIZE];
6436
Chris Mason0b86a832008-03-24 15:01:56 -04006437 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006438 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006439 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006440 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006441 BTRFS_UUID_SIZE);
6442
6443 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006444 fs_devices = open_seed_devices(root, fs_uuid);
6445 if (IS_ERR(fs_devices))
6446 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006447 }
6448
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006449 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006450 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006451 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006452 return -EIO;
6453
Miao Xie5f375832014-09-03 21:35:46 +08006454 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6455 if (!device)
6456 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006457 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6458 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006459 } else {
6460 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6461 return -EIO;
6462
6463 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006464 /*
6465 * this happens when a device that was properly setup
6466 * in the device info lists suddenly goes bad.
6467 * device->bdev is NULL, and so we have to set
6468 * device->missing to one here
6469 */
Miao Xie5f375832014-09-03 21:35:46 +08006470 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006471 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006472 }
Miao Xie5f375832014-09-03 21:35:46 +08006473
6474 /* Move the device to its own fs_devices */
6475 if (device->fs_devices != fs_devices) {
6476 ASSERT(device->missing);
6477
6478 list_move(&device->dev_list, &fs_devices->devices);
6479 device->fs_devices->num_devices--;
6480 fs_devices->num_devices++;
6481
6482 device->fs_devices->missing_devices--;
6483 fs_devices->missing_devices++;
6484
6485 device->fs_devices = fs_devices;
6486 }
Yan Zheng2b820322008-11-17 21:11:30 -05006487 }
6488
6489 if (device->fs_devices != root->fs_info->fs_devices) {
6490 BUG_ON(device->writeable);
6491 if (device->generation !=
6492 btrfs_device_generation(leaf, dev_item))
6493 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006494 }
Chris Mason0b86a832008-03-24 15:01:56 -04006495
6496 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006497 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006498 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006499 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006500 spin_lock(&root->fs_info->free_chunk_lock);
6501 root->fs_info->free_chunk_space += device->total_bytes -
6502 device->bytes_used;
6503 spin_unlock(&root->fs_info->free_chunk_lock);
6504 }
Chris Mason0b86a832008-03-24 15:01:56 -04006505 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006506 return ret;
6507}
6508
Yan Zhenge4404d62008-12-12 10:03:26 -05006509int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006510{
David Sterba6c417612011-04-13 15:41:04 +02006511 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006512 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006513 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006514 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006515 u8 *array_ptr;
6516 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006517 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006518 u32 num_stripes;
6519 u32 array_size;
6520 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006521 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006522 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006523
David Sterbaa83fffb2014-06-15 02:39:54 +02006524 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6525 /*
6526 * This will create extent buffer of nodesize, superblock size is
6527 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6528 * overallocate but we can keep it as-is, only the first page is used.
6529 */
6530 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006531 if (!sb)
6532 return -ENOMEM;
David Sterba4db8c522015-12-03 13:06:46 +01006533 set_extent_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006534 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006535 /*
6536 * The sb extent buffer is artifical and just used to read the system array.
David Sterba4db8c522015-12-03 13:06:46 +01006537 * set_extent_buffer_uptodate() call does not properly mark all it's
David Sterba8a334422011-10-07 18:06:13 +02006538 * pages up-to-date when the page is larger: extent does not cover the
6539 * whole page and consequently check_page_uptodate does not find all
6540 * the page's extents up-to-date (the hole beyond sb),
6541 * write_extent_buffer then triggers a WARN_ON.
6542 *
6543 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6544 * but sb spans only this function. Add an explicit SetPageUptodate call
6545 * to silence the warning eg. on PowerPC 64.
6546 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006547 if (PAGE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006548 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006549
Chris Masona061fc82008-05-07 11:43:44 -04006550 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006551 array_size = btrfs_super_sys_array_size(super_copy);
6552
David Sterba1ffb22c2014-10-31 19:02:42 +01006553 array_ptr = super_copy->sys_chunk_array;
6554 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6555 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006556
David Sterba1ffb22c2014-10-31 19:02:42 +01006557 while (cur_offset < array_size) {
6558 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006559 len = sizeof(*disk_key);
6560 if (cur_offset + len > array_size)
6561 goto out_short_read;
6562
Chris Mason0b86a832008-03-24 15:01:56 -04006563 btrfs_disk_key_to_cpu(&key, disk_key);
6564
David Sterba1ffb22c2014-10-31 19:02:42 +01006565 array_ptr += len;
6566 sb_array_offset += len;
6567 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006568
Chris Mason0d81ba52008-03-24 15:02:07 -04006569 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006570 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006571 /*
6572 * At least one btrfs_chunk with one stripe must be
6573 * present, exact stripe count check comes afterwards
6574 */
6575 len = btrfs_chunk_item_size(1);
6576 if (cur_offset + len > array_size)
6577 goto out_short_read;
6578
6579 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
David Sterbaf5cdedd2015-11-30 17:27:06 +01006580 if (!num_stripes) {
6581 printk(KERN_ERR
6582 "BTRFS: invalid number of stripes %u in sys_array at offset %u\n",
6583 num_stripes, cur_offset);
6584 ret = -EIO;
6585 break;
6586 }
6587
David Sterbae3540ea2014-11-05 15:24:51 +01006588 len = btrfs_chunk_item_size(num_stripes);
6589 if (cur_offset + len > array_size)
6590 goto out_short_read;
6591
Chris Mason0d81ba52008-03-24 15:02:07 -04006592 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006593 if (ret)
6594 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006595 } else {
David Sterba93a3d462015-11-30 17:27:09 +01006596 printk(KERN_ERR
6597 "BTRFS: unexpected item type %u in sys_array at offset %u\n",
6598 (u32)key.type, cur_offset);
Chris Mason84eed902008-04-25 09:04:37 -04006599 ret = -EIO;
6600 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006601 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006602 array_ptr += len;
6603 sb_array_offset += len;
6604 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006605 }
Chris Masona061fc82008-05-07 11:43:44 -04006606 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006607 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006608
6609out_short_read:
6610 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6611 len, cur_offset);
6612 free_extent_buffer(sb);
6613 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006614}
6615
6616int btrfs_read_chunk_tree(struct btrfs_root *root)
6617{
6618 struct btrfs_path *path;
6619 struct extent_buffer *leaf;
6620 struct btrfs_key key;
6621 struct btrfs_key found_key;
6622 int ret;
6623 int slot;
6624
6625 root = root->fs_info->chunk_root;
6626
6627 path = btrfs_alloc_path();
6628 if (!path)
6629 return -ENOMEM;
6630
Li Zefanb367e472011-12-07 11:38:24 +08006631 mutex_lock(&uuid_mutex);
6632 lock_chunks(root);
6633
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006634 /*
6635 * Read all device items, and then all the chunk items. All
6636 * device items are found before any chunk item (their object id
6637 * is smaller than the lowest possible object id for a chunk
6638 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006639 */
6640 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6641 key.offset = 0;
6642 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006643 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006644 if (ret < 0)
6645 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006646 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006647 leaf = path->nodes[0];
6648 slot = path->slots[0];
6649 if (slot >= btrfs_header_nritems(leaf)) {
6650 ret = btrfs_next_leaf(root, path);
6651 if (ret == 0)
6652 continue;
6653 if (ret < 0)
6654 goto error;
6655 break;
6656 }
6657 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006658 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6659 struct btrfs_dev_item *dev_item;
6660 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006661 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006662 ret = read_one_dev(root, leaf, dev_item);
6663 if (ret)
6664 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006665 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6666 struct btrfs_chunk *chunk;
6667 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6668 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006669 if (ret)
6670 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006671 }
6672 path->slots[0]++;
6673 }
Chris Mason0b86a832008-03-24 15:01:56 -04006674 ret = 0;
6675error:
Li Zefanb367e472011-12-07 11:38:24 +08006676 unlock_chunks(root);
6677 mutex_unlock(&uuid_mutex);
6678
Yan Zheng2b820322008-11-17 21:11:30 -05006679 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006680 return ret;
6681}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006682
Miao Xiecb517ea2013-05-15 07:48:19 +00006683void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6684{
6685 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6686 struct btrfs_device *device;
6687
Liu Bo29cc83f2014-05-11 23:14:59 +08006688 while (fs_devices) {
6689 mutex_lock(&fs_devices->device_list_mutex);
6690 list_for_each_entry(device, &fs_devices->devices, dev_list)
6691 device->dev_root = fs_info->dev_root;
6692 mutex_unlock(&fs_devices->device_list_mutex);
6693
6694 fs_devices = fs_devices->seed;
6695 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006696}
6697
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006698static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6699{
6700 int i;
6701
6702 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6703 btrfs_dev_stat_reset(dev, i);
6704}
6705
6706int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6707{
6708 struct btrfs_key key;
6709 struct btrfs_key found_key;
6710 struct btrfs_root *dev_root = fs_info->dev_root;
6711 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6712 struct extent_buffer *eb;
6713 int slot;
6714 int ret = 0;
6715 struct btrfs_device *device;
6716 struct btrfs_path *path = NULL;
6717 int i;
6718
6719 path = btrfs_alloc_path();
6720 if (!path) {
6721 ret = -ENOMEM;
6722 goto out;
6723 }
6724
6725 mutex_lock(&fs_devices->device_list_mutex);
6726 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6727 int item_size;
6728 struct btrfs_dev_stats_item *ptr;
6729
David Sterba242e2952016-01-25 17:51:31 +01006730 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6731 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006732 key.offset = device->devid;
6733 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6734 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006735 __btrfs_reset_dev_stats(device);
6736 device->dev_stats_valid = 1;
6737 btrfs_release_path(path);
6738 continue;
6739 }
6740 slot = path->slots[0];
6741 eb = path->nodes[0];
6742 btrfs_item_key_to_cpu(eb, &found_key, slot);
6743 item_size = btrfs_item_size_nr(eb, slot);
6744
6745 ptr = btrfs_item_ptr(eb, slot,
6746 struct btrfs_dev_stats_item);
6747
6748 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6749 if (item_size >= (1 + i) * sizeof(__le64))
6750 btrfs_dev_stat_set(device, i,
6751 btrfs_dev_stats_value(eb, ptr, i));
6752 else
6753 btrfs_dev_stat_reset(device, i);
6754 }
6755
6756 device->dev_stats_valid = 1;
6757 btrfs_dev_stat_print_on_load(device);
6758 btrfs_release_path(path);
6759 }
6760 mutex_unlock(&fs_devices->device_list_mutex);
6761
6762out:
6763 btrfs_free_path(path);
6764 return ret < 0 ? ret : 0;
6765}
6766
6767static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6768 struct btrfs_root *dev_root,
6769 struct btrfs_device *device)
6770{
6771 struct btrfs_path *path;
6772 struct btrfs_key key;
6773 struct extent_buffer *eb;
6774 struct btrfs_dev_stats_item *ptr;
6775 int ret;
6776 int i;
6777
David Sterba242e2952016-01-25 17:51:31 +01006778 key.objectid = BTRFS_DEV_STATS_OBJECTID;
6779 key.type = BTRFS_PERSISTENT_ITEM_KEY;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006780 key.offset = device->devid;
6781
6782 path = btrfs_alloc_path();
6783 BUG_ON(!path);
6784 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6785 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006786 btrfs_warn_in_rcu(dev_root->fs_info,
6787 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04006788 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006789 goto out;
6790 }
6791
6792 if (ret == 0 &&
6793 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6794 /* need to delete old one and insert a new one */
6795 ret = btrfs_del_item(trans, dev_root, path);
6796 if (ret != 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006797 btrfs_warn_in_rcu(dev_root->fs_info,
6798 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04006799 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006800 goto out;
6801 }
6802 ret = 1;
6803 }
6804
6805 if (ret == 1) {
6806 /* need to insert a new item */
6807 btrfs_release_path(path);
6808 ret = btrfs_insert_empty_item(trans, dev_root, path,
6809 &key, sizeof(*ptr));
6810 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006811 btrfs_warn_in_rcu(dev_root->fs_info,
6812 "insert dev_stats item for device %s failed %d",
6813 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006814 goto out;
6815 }
6816 }
6817
6818 eb = path->nodes[0];
6819 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6820 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6821 btrfs_set_dev_stats_value(eb, ptr, i,
6822 btrfs_dev_stat_read(device, i));
6823 btrfs_mark_buffer_dirty(eb);
6824
6825out:
6826 btrfs_free_path(path);
6827 return ret;
6828}
6829
6830/*
6831 * called from commit_transaction. Writes all changed device stats to disk.
6832 */
6833int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6834 struct btrfs_fs_info *fs_info)
6835{
6836 struct btrfs_root *dev_root = fs_info->dev_root;
6837 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6838 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006839 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006840 int ret = 0;
6841
6842 mutex_lock(&fs_devices->device_list_mutex);
6843 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006844 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006845 continue;
6846
Miao Xieaddc3fa2014-07-24 11:37:11 +08006847 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006848 ret = update_dev_stat_item(trans, dev_root, device);
6849 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006850 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006851 }
6852 mutex_unlock(&fs_devices->device_list_mutex);
6853
6854 return ret;
6855}
6856
Stefan Behrens442a4f62012-05-25 16:06:08 +02006857void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6858{
6859 btrfs_dev_stat_inc(dev, index);
6860 btrfs_dev_stat_print_on_error(dev);
6861}
6862
Eric Sandeen48a3b632013-04-25 20:41:01 +00006863static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006864{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006865 if (!dev->dev_stats_valid)
6866 return;
David Sterbab14af3b2015-10-08 10:43:10 +02006867 btrfs_err_rl_in_rcu(dev->dev_root->fs_info,
6868 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006869 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006870 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6871 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6872 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006873 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6874 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006875}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006876
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006877static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6878{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006879 int i;
6880
6881 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6882 if (btrfs_dev_stat_read(dev, i) != 0)
6883 break;
6884 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6885 return; /* all values == 0, suppress message */
6886
David Sterbaecaeb142015-10-08 09:01:03 +02006887 btrfs_info_in_rcu(dev->dev_root->fs_info,
6888 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006889 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006890 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6891 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6892 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6893 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6894 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6895}
6896
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006897int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006898 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006899{
6900 struct btrfs_device *dev;
6901 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6902 int i;
6903
6904 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006905 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006906 mutex_unlock(&fs_devices->device_list_mutex);
6907
6908 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006909 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006910 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006911 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006912 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006913 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006914 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006915 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6916 if (stats->nr_items > i)
6917 stats->values[i] =
6918 btrfs_dev_stat_read_and_reset(dev, i);
6919 else
6920 btrfs_dev_stat_reset(dev, i);
6921 }
6922 } else {
6923 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6924 if (stats->nr_items > i)
6925 stats->values[i] = btrfs_dev_stat_read(dev, i);
6926 }
6927 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6928 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6929 return 0;
6930}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006931
Anand Jain12b1c262015-08-14 18:32:59 +08006932void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006933{
6934 struct buffer_head *bh;
6935 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08006936 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006937
Anand Jain12b1c262015-08-14 18:32:59 +08006938 if (!bdev)
6939 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006940
Anand Jain12b1c262015-08-14 18:32:59 +08006941 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
6942 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006943
Anand Jain12b1c262015-08-14 18:32:59 +08006944 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
6945 continue;
6946
6947 disk_super = (struct btrfs_super_block *)bh->b_data;
6948
6949 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6950 set_buffer_dirty(bh);
6951 sync_dirty_buffer(bh);
6952 brelse(bh);
6953 }
6954
6955 /* Notify udev that device has changed */
6956 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
6957
6958 /* Update ctime/mtime for device path for libblkid */
6959 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006960}
Miao Xie935e5cc2014-09-03 21:35:33 +08006961
6962/*
6963 * Update the size of all devices, which is used for writing out the
6964 * super blocks.
6965 */
6966void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6967{
6968 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6969 struct btrfs_device *curr, *next;
6970
6971 if (list_empty(&fs_devices->resized_devices))
6972 return;
6973
6974 mutex_lock(&fs_devices->device_list_mutex);
6975 lock_chunks(fs_info->dev_root);
6976 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6977 resized_list) {
6978 list_del_init(&curr->resized_list);
6979 curr->commit_total_bytes = curr->disk_total_bytes;
6980 }
6981 unlock_chunks(fs_info->dev_root);
6982 mutex_unlock(&fs_devices->device_list_mutex);
6983}
Miao Xiece7213c2014-09-03 21:35:34 +08006984
6985/* Must be invoked during the transaction commit */
6986void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6987 struct btrfs_transaction *transaction)
6988{
6989 struct extent_map *em;
6990 struct map_lookup *map;
6991 struct btrfs_device *dev;
6992 int i;
6993
6994 if (list_empty(&transaction->pending_chunks))
6995 return;
6996
6997 /* In order to kick the device replace finish process */
6998 lock_chunks(root);
6999 list_for_each_entry(em, &transaction->pending_chunks, list) {
Jeff Mahoney95617d62015-06-03 10:55:48 -04007000 map = em->map_lookup;
Miao Xiece7213c2014-09-03 21:35:34 +08007001
7002 for (i = 0; i < map->num_stripes; i++) {
7003 dev = map->stripes[i].dev;
7004 dev->commit_bytes_used = dev->bytes_used;
7005 }
7006 }
7007 unlock_chunks(root);
7008}
Anand Jain5a13f432015-03-10 06:38:31 +08007009
7010void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
7011{
7012 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7013 while (fs_devices) {
7014 fs_devices->fs_info = fs_info;
7015 fs_devices = fs_devices->seed;
7016 }
7017}
7018
7019void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
7020{
7021 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7022 while (fs_devices) {
7023 fs_devices->fs_info = NULL;
7024 fs_devices = fs_devices->seed;
7025 }
7026}
Anand Jainf190aa42015-08-14 18:33:05 +08007027
David Sterba87ad58c2015-11-27 18:49:39 +01007028static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf190aa42015-08-14 18:33:05 +08007029{
7030 struct btrfs_fs_devices *fs_devices = device->fs_devices;
7031 struct btrfs_device *new_device;
7032 struct rcu_string *name;
7033
7034 if (device->bdev)
7035 fs_devices->open_devices--;
7036
7037 if (device->writeable &&
7038 device->devid != BTRFS_DEV_REPLACE_DEVID) {
7039 list_del_init(&device->dev_alloc_list);
7040 fs_devices->rw_devices--;
7041 }
7042
7043 if (device->missing)
7044 fs_devices->missing_devices--;
7045
7046 new_device = btrfs_alloc_device(NULL, &device->devid,
7047 device->uuid);
7048 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
7049
7050 /* Safe because we are under uuid_mutex */
7051 if (device->name) {
7052 name = rcu_string_strdup(device->name->str, GFP_NOFS);
7053 BUG_ON(!name); /* -ENOMEM */
7054 rcu_assign_pointer(new_device->name, name);
7055 }
7056
7057 list_replace_rcu(&device->dev_list, &new_device->dev_list);
7058 new_device->fs_devices = device->fs_devices;
7059
7060 call_rcu(&device->rcu, free_device);
7061}