blob: 59538a78989c4a6684fd6c8e11775ced06879456 [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
111const u64 const btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
112 [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);
Yan Zheng2b820322008-11-17 21:11:30 -0500128
Miao Xie67a2c452014-09-03 21:35:43 +0800129DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400130static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800131struct list_head *btrfs_get_fs_uuids(void)
132{
133 return &fs_uuids;
134}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400135
Ilya Dryomov2208a372013-08-12 14:33:03 +0300136static struct btrfs_fs_devices *__alloc_fs_devices(void)
137{
138 struct btrfs_fs_devices *fs_devs;
139
140 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
141 if (!fs_devs)
142 return ERR_PTR(-ENOMEM);
143
144 mutex_init(&fs_devs->device_list_mutex);
145
146 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800147 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300148 INIT_LIST_HEAD(&fs_devs->alloc_list);
149 INIT_LIST_HEAD(&fs_devs->list);
150
151 return fs_devs;
152}
153
154/**
155 * alloc_fs_devices - allocate struct btrfs_fs_devices
156 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
157 * generated.
158 *
159 * Return: a pointer to a new &struct btrfs_fs_devices on success;
160 * ERR_PTR() on error. Returned struct is not linked onto any lists and
161 * can be destroyed with kfree() right away.
162 */
163static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
164{
165 struct btrfs_fs_devices *fs_devs;
166
167 fs_devs = __alloc_fs_devices();
168 if (IS_ERR(fs_devs))
169 return fs_devs;
170
171 if (fsid)
172 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
173 else
174 generate_random_uuid(fs_devs->fsid);
175
176 return fs_devs;
177}
178
Yan Zhenge4404d62008-12-12 10:03:26 -0500179static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
180{
181 struct btrfs_device *device;
182 WARN_ON(fs_devices->opened);
183 while (!list_empty(&fs_devices->devices)) {
184 device = list_entry(fs_devices->devices.next,
185 struct btrfs_device, dev_list);
186 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400187 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500188 kfree(device);
189 }
190 kfree(fs_devices);
191}
192
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000193static void btrfs_kobject_uevent(struct block_device *bdev,
194 enum kobject_action action)
195{
196 int ret;
197
198 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
199 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500200 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000201 action,
202 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
203 &disk_to_dev(bdev->bd_disk)->kobj);
204}
205
Jeff Mahoney143bede2012-03-01 14:56:26 +0100206void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400207{
208 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400209
Yan Zheng2b820322008-11-17 21:11:30 -0500210 while (!list_empty(&fs_uuids)) {
211 fs_devices = list_entry(fs_uuids.next,
212 struct btrfs_fs_devices, list);
213 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500214 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400215 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400216}
217
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300218static struct btrfs_device *__alloc_device(void)
219{
220 struct btrfs_device *dev;
221
222 dev = kzalloc(sizeof(*dev), GFP_NOFS);
223 if (!dev)
224 return ERR_PTR(-ENOMEM);
225
226 INIT_LIST_HEAD(&dev->dev_list);
227 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800228 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300229
230 spin_lock_init(&dev->io_lock);
231
232 spin_lock_init(&dev->reada_lock);
233 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800234 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300235 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
236 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
237
238 return dev;
239}
240
Chris Masona1b32a52008-09-05 16:09:51 -0400241static noinline struct btrfs_device *__find_device(struct list_head *head,
242 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400243{
244 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400245
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500246 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400247 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400248 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400249 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400250 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400251 }
252 return NULL;
253}
254
Chris Masona1b32a52008-09-05 16:09:51 -0400255static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400256{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400257 struct btrfs_fs_devices *fs_devices;
258
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500259 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400260 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
261 return fs_devices;
262 }
263 return NULL;
264}
265
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100266static int
267btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
268 int flush, struct block_device **bdev,
269 struct buffer_head **bh)
270{
271 int ret;
272
273 *bdev = blkdev_get_by_path(device_path, flags, holder);
274
275 if (IS_ERR(*bdev)) {
276 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500277 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100278 goto error;
279 }
280
281 if (flush)
282 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
283 ret = set_blocksize(*bdev, 4096);
284 if (ret) {
285 blkdev_put(*bdev, flags);
286 goto error;
287 }
288 invalidate_bdev(*bdev);
289 *bh = btrfs_read_dev_super(*bdev);
290 if (!*bh) {
291 ret = -EINVAL;
292 blkdev_put(*bdev, flags);
293 goto error;
294 }
295
296 return 0;
297
298error:
299 *bdev = NULL;
300 *bh = NULL;
301 return ret;
302}
303
Chris Masonffbd5172009-04-20 15:50:09 -0400304static void requeue_list(struct btrfs_pending_bios *pending_bios,
305 struct bio *head, struct bio *tail)
306{
307
308 struct bio *old_head;
309
310 old_head = pending_bios->head;
311 pending_bios->head = head;
312 if (pending_bios->tail)
313 tail->bi_next = old_head;
314 else
315 pending_bios->tail = tail;
316}
317
Chris Mason8b712842008-06-11 16:50:36 -0400318/*
319 * we try to collect pending bios for a device so we don't get a large
320 * number of procs sending bios down to the same device. This greatly
321 * improves the schedulers ability to collect and merge the bios.
322 *
323 * But, it also turns into a long list of bios to process and that is sure
324 * to eventually make the worker thread block. The solution here is to
325 * make some progress and then put this work struct back at the end of
326 * the list if the block device is congested. This way, multiple devices
327 * can make progress from a single worker thread.
328 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100329static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400330{
331 struct bio *pending;
332 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400333 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400334 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400335 struct bio *tail;
336 struct bio *cur;
337 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400338 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400339 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400340 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400341 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400342 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000343 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400344 struct blk_plug plug;
345
346 /*
347 * this function runs all the bios we've collected for
348 * a particular device. We don't want to wander off to
349 * another device without first sending all of these down.
350 * So, setup a plug here and finish it off before we return
351 */
352 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400353
Chris Masonbedf7622009-04-03 10:32:58 -0400354 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400355 fs_info = device->dev_root->fs_info;
356 limit = btrfs_async_submit_limit(fs_info);
357 limit = limit * 2 / 3;
358
Chris Mason8b712842008-06-11 16:50:36 -0400359loop:
360 spin_lock(&device->io_lock);
361
Chris Masona6837052009-02-04 09:19:41 -0500362loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400363 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400364
Chris Mason8b712842008-06-11 16:50:36 -0400365 /* take all the bios off the list at once and process them
366 * later on (without the lock held). But, remember the
367 * tail and other pointers so the bios can be properly reinserted
368 * into the list if we hit congestion
369 */
Chris Masond84275c2009-06-09 15:39:08 -0400370 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400371 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400372 force_reg = 1;
373 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400374 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400375 force_reg = 0;
376 }
Chris Masonffbd5172009-04-20 15:50:09 -0400377
378 pending = pending_bios->head;
379 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400380 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400381
382 /*
383 * if pending was null this time around, no bios need processing
384 * at all and we can stop. Otherwise it'll loop back up again
385 * and do an additional check so no bios are missed.
386 *
387 * device->running_pending is used to synchronize with the
388 * schedule_bio code.
389 */
Chris Masonffbd5172009-04-20 15:50:09 -0400390 if (device->pending_sync_bios.head == NULL &&
391 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400392 again = 0;
393 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400394 } else {
395 again = 1;
396 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400397 }
Chris Masonffbd5172009-04-20 15:50:09 -0400398
399 pending_bios->head = NULL;
400 pending_bios->tail = NULL;
401
Chris Mason8b712842008-06-11 16:50:36 -0400402 spin_unlock(&device->io_lock);
403
Chris Masond3977122009-01-05 21:25:51 -0500404 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400405
406 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400407 /* we want to work on both lists, but do more bios on the
408 * sync list than the regular list
409 */
410 if ((num_run > 32 &&
411 pending_bios != &device->pending_sync_bios &&
412 device->pending_sync_bios.head) ||
413 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
414 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400415 spin_lock(&device->io_lock);
416 requeue_list(pending_bios, pending, tail);
417 goto loop_lock;
418 }
419
Chris Mason8b712842008-06-11 16:50:36 -0400420 cur = pending;
421 pending = pending->bi_next;
422 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400423
Josef Bacik66657b32012-08-01 15:36:24 -0400424 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400425 waitqueue_active(&fs_info->async_submit_wait))
426 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400427
Jens Axboedac56212015-04-17 16:23:59 -0600428 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400429
Chris Mason2ab1ba62011-08-04 14:28:36 -0400430 /*
431 * if we're doing the sync list, record that our
432 * plug has some sync requests on it
433 *
434 * If we're doing the regular list and there are
435 * sync requests sitting around, unplug before
436 * we add more
437 */
438 if (pending_bios == &device->pending_sync_bios) {
439 sync_pending = 1;
440 } else if (sync_pending) {
441 blk_finish_plug(&plug);
442 blk_start_plug(&plug);
443 sync_pending = 0;
444 }
445
Stefan Behrens21adbd52011-11-09 13:44:05 +0100446 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400447 num_run++;
448 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100449
450 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400451
452 /*
453 * we made progress, there is more work to do and the bdi
454 * is now congested. Back off and let other work structs
455 * run instead
456 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400457 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500458 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400459 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400460
Chris Masonb765ead2009-04-03 10:27:10 -0400461 ioc = current->io_context;
462
463 /*
464 * the main goal here is that we don't want to
465 * block if we're going to be able to submit
466 * more requests without blocking.
467 *
468 * This code does two great things, it pokes into
469 * the elevator code from a filesystem _and_
470 * it makes assumptions about how batching works.
471 */
472 if (ioc && ioc->nr_batch_requests > 0 &&
473 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
474 (last_waited == 0 ||
475 ioc->last_waited == last_waited)) {
476 /*
477 * we want to go through our batch of
478 * requests and stop. So, we copy out
479 * the ioc->last_waited time and test
480 * against it before looping
481 */
482 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100483 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400484 continue;
485 }
Chris Mason8b712842008-06-11 16:50:36 -0400486 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400487 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500488 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400489
490 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800491 btrfs_queue_work(fs_info->submit_workers,
492 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400493 goto done;
494 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500495 /* unplug every 64 requests just for good measure */
496 if (batch_run % 64 == 0) {
497 blk_finish_plug(&plug);
498 blk_start_plug(&plug);
499 sync_pending = 0;
500 }
Chris Mason8b712842008-06-11 16:50:36 -0400501 }
Chris Masonffbd5172009-04-20 15:50:09 -0400502
Chris Mason51684082010-03-10 15:33:32 -0500503 cond_resched();
504 if (again)
505 goto loop;
506
507 spin_lock(&device->io_lock);
508 if (device->pending_bios.head || device->pending_sync_bios.head)
509 goto loop_lock;
510 spin_unlock(&device->io_lock);
511
Chris Mason8b712842008-06-11 16:50:36 -0400512done:
Chris Mason211588a2011-04-19 20:12:40 -0400513 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400514}
515
Christoph Hellwigb2950862008-12-02 09:54:17 -0500516static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400517{
518 struct btrfs_device *device;
519
520 device = container_of(work, struct btrfs_device, work);
521 run_scheduled_bios(device);
522}
523
Anand Jain4fde46f2015-06-17 21:10:48 +0800524
525void btrfs_free_stale_device(struct btrfs_device *cur_dev)
526{
527 struct btrfs_fs_devices *fs_devs;
528 struct btrfs_device *dev;
529
530 if (!cur_dev->name)
531 return;
532
533 list_for_each_entry(fs_devs, &fs_uuids, list) {
534 int del = 1;
535
536 if (fs_devs->opened)
537 continue;
538 if (fs_devs->seeding)
539 continue;
540
541 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
542
543 if (dev == cur_dev)
544 continue;
545 if (!dev->name)
546 continue;
547
548 /*
549 * Todo: This won't be enough. What if the same device
550 * comes back (with new uuid and) with its mapper path?
551 * But for now, this does help as mostly an admin will
552 * either use mapper or non mapper path throughout.
553 */
554 rcu_read_lock();
555 del = strcmp(rcu_str_deref(dev->name),
556 rcu_str_deref(cur_dev->name));
557 rcu_read_unlock();
558 if (!del)
559 break;
560 }
561
562 if (!del) {
563 /* delete the stale device */
564 if (fs_devs->num_devices == 1) {
565 btrfs_sysfs_remove_fsid(fs_devs);
566 list_del(&fs_devs->list);
567 free_fs_devices(fs_devs);
568 } else {
569 fs_devs->num_devices--;
570 list_del(&dev->dev_list);
571 rcu_string_free(dev->name);
572 kfree(dev);
573 }
574 break;
575 }
576 }
577}
578
David Sterba60999ca2014-03-26 18:26:36 +0100579/*
580 * Add new device to list of registered devices
581 *
582 * Returns:
583 * 1 - first time device is seen
584 * 0 - device already known
585 * < 0 - error
586 */
Chris Masona1b32a52008-09-05 16:09:51 -0400587static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400588 struct btrfs_super_block *disk_super,
589 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
590{
591 struct btrfs_device *device;
592 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400593 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100594 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400595 u64 found_transid = btrfs_super_generation(disk_super);
596
597 fs_devices = find_fsid(disk_super->fsid);
598 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300599 fs_devices = alloc_fs_devices(disk_super->fsid);
600 if (IS_ERR(fs_devices))
601 return PTR_ERR(fs_devices);
602
Chris Mason8a4b83c2008-03-24 15:02:07 -0400603 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300604
Chris Mason8a4b83c2008-03-24 15:02:07 -0400605 device = NULL;
606 } else {
Chris Masona4437552008-04-18 10:29:38 -0400607 device = __find_device(&fs_devices->devices, devid,
608 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400609 }
Miao Xie443f24f2014-07-24 11:37:15 +0800610
Chris Mason8a4b83c2008-03-24 15:02:07 -0400611 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500612 if (fs_devices->opened)
613 return -EBUSY;
614
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300615 device = btrfs_alloc_device(NULL, &devid,
616 disk_super->dev_item.uuid);
617 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400618 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300619 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620 }
Josef Bacik606686e2012-06-04 14:03:51 -0400621
622 name = rcu_string_strdup(path, GFP_NOFS);
623 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400624 kfree(device);
625 return -ENOMEM;
626 }
Josef Bacik606686e2012-06-04 14:03:51 -0400627 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200628
Chris Masone5e9a522009-06-10 15:17:02 -0400629 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000630 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100631 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400632 mutex_unlock(&fs_devices->device_list_mutex);
633
David Sterba60999ca2014-03-26 18:26:36 +0100634 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500635 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400636 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800637 /*
638 * When FS is already mounted.
639 * 1. If you are here and if the device->name is NULL that
640 * means this device was missing at time of FS mount.
641 * 2. If you are here and if the device->name is different
642 * from 'path' that means either
643 * a. The same device disappeared and reappeared with
644 * different name. or
645 * b. The missing-disk-which-was-replaced, has
646 * reappeared now.
647 *
648 * We must allow 1 and 2a above. But 2b would be a spurious
649 * and unintentional.
650 *
651 * Further in case of 1 and 2a above, the disk at 'path'
652 * would have missed some transaction when it was away and
653 * in case of 2a the stale bdev has to be updated as well.
654 * 2b must not be allowed at all time.
655 */
656
657 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700658 * For now, we do allow update to btrfs_fs_device through the
659 * btrfs dev scan cli after FS has been mounted. We're still
660 * tracking a problem where systems fail mount by subvolume id
661 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800662 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700663 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800664 /*
665 * That is if the FS is _not_ mounted and if you
666 * are here, that means there is more than one
667 * disk with same uuid and devid.We keep the one
668 * with larger generation number or the last-in if
669 * generation are equal.
670 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700671 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800672 }
Anand Jainb96de002014-07-03 18:22:05 +0800673
Josef Bacik606686e2012-06-04 14:03:51 -0400674 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000675 if (!name)
676 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400677 rcu_string_free(device->name);
678 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500679 if (device->missing) {
680 fs_devices->missing_devices--;
681 device->missing = 0;
682 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400683 }
684
Anand Jain77bdae42014-07-03 18:22:06 +0800685 /*
686 * Unmount does not free the btrfs_device struct but would zero
687 * generation along with most of the other members. So just update
688 * it back. We need it to pick the disk with largest generation
689 * (as above).
690 */
691 if (!fs_devices->opened)
692 device->generation = found_transid;
693
Anand Jain4fde46f2015-06-17 21:10:48 +0800694 /*
695 * if there is new btrfs on an already registered device,
696 * then remove the stale device entry.
697 */
698 btrfs_free_stale_device(device);
699
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100701
702 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703}
704
Yan Zhenge4404d62008-12-12 10:03:26 -0500705static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
706{
707 struct btrfs_fs_devices *fs_devices;
708 struct btrfs_device *device;
709 struct btrfs_device *orig_dev;
710
Ilya Dryomov2208a372013-08-12 14:33:03 +0300711 fs_devices = alloc_fs_devices(orig->fsid);
712 if (IS_ERR(fs_devices))
713 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500714
Miao Xieadbbb862014-09-03 21:35:42 +0800715 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400716 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500717
Xiao Guangrong46224702011-04-20 10:08:47 +0000718 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500719 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400720 struct rcu_string *name;
721
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300722 device = btrfs_alloc_device(NULL, &orig_dev->devid,
723 orig_dev->uuid);
724 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500725 goto error;
726
Josef Bacik606686e2012-06-04 14:03:51 -0400727 /*
728 * This is ok to do without rcu read locked because we hold the
729 * uuid mutex so nothing we touch in here is going to disappear.
730 */
Anand Jaine755f782014-06-30 17:12:47 +0800731 if (orig_dev->name) {
732 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
733 if (!name) {
734 kfree(device);
735 goto error;
736 }
737 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400738 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500739
Yan Zhenge4404d62008-12-12 10:03:26 -0500740 list_add(&device->dev_list, &fs_devices->devices);
741 device->fs_devices = fs_devices;
742 fs_devices->num_devices++;
743 }
Miao Xieadbbb862014-09-03 21:35:42 +0800744 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500745 return fs_devices;
746error:
Miao Xieadbbb862014-09-03 21:35:42 +0800747 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500748 free_fs_devices(fs_devices);
749 return ERR_PTR(-ENOMEM);
750}
751
Eric Sandeen9eaed212014-08-01 18:12:35 -0500752void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400753{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500754 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800755 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500756
Chris Masondfe25022008-05-13 13:46:40 -0400757 mutex_lock(&uuid_mutex);
758again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000759 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500760 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500761 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100762 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800763 (!latest_dev ||
764 device->generation > latest_dev->generation)) {
765 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500766 }
Yan Zheng2b820322008-11-17 21:11:30 -0500767 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500768 }
Yan Zheng2b820322008-11-17 21:11:30 -0500769
Stefan Behrens8dabb742012-11-06 13:15:27 +0100770 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
771 /*
772 * In the first step, keep the device which has
773 * the correct fsid and the devid that is used
774 * for the dev_replace procedure.
775 * In the second step, the dev_replace state is
776 * read from the device tree and it is known
777 * whether the procedure is really active or
778 * not, which means whether this device is
779 * used or whether it should be removed.
780 */
781 if (step == 0 || device->is_tgtdev_for_dev_replace) {
782 continue;
783 }
784 }
Yan Zheng2b820322008-11-17 21:11:30 -0500785 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100786 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500787 device->bdev = NULL;
788 fs_devices->open_devices--;
789 }
790 if (device->writeable) {
791 list_del_init(&device->dev_alloc_list);
792 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100793 if (!device->is_tgtdev_for_dev_replace)
794 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500795 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500796 list_del_init(&device->dev_list);
797 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400798 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500799 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400800 }
Yan Zheng2b820322008-11-17 21:11:30 -0500801
802 if (fs_devices->seed) {
803 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500804 goto again;
805 }
806
Miao Xie443f24f2014-07-24 11:37:15 +0800807 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500808
Chris Masondfe25022008-05-13 13:46:40 -0400809 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400810}
Chris Masona0af4692008-05-13 16:03:06 -0400811
Xiao Guangrong1f781602011-04-20 10:09:16 +0000812static void __free_device(struct work_struct *work)
813{
814 struct btrfs_device *device;
815
816 device = container_of(work, struct btrfs_device, rcu_work);
817
818 if (device->bdev)
819 blkdev_put(device->bdev, device->mode);
820
Josef Bacik606686e2012-06-04 14:03:51 -0400821 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000822 kfree(device);
823}
824
825static void free_device(struct rcu_head *head)
826{
827 struct btrfs_device *device;
828
829 device = container_of(head, struct btrfs_device, rcu);
830
831 INIT_WORK(&device->rcu_work, __free_device);
832 schedule_work(&device->rcu_work);
833}
834
Yan Zheng2b820322008-11-17 21:11:30 -0500835static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400836{
Sasha Levin2037a092015-05-12 19:31:37 -0400837 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500838
Yan Zheng2b820322008-11-17 21:11:30 -0500839 if (--fs_devices->opened > 0)
840 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400841
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000842 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400843 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000844 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400845 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000846
847 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400848 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000849
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300850 if (device->writeable &&
851 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500852 list_del_init(&device->dev_alloc_list);
853 fs_devices->rw_devices--;
854 }
855
Josef Bacik726551e2013-08-26 13:45:53 -0400856 if (device->missing)
857 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000858
Ilya Dryomova1e87802013-08-12 14:33:04 +0300859 new_device = btrfs_alloc_device(NULL, &device->devid,
860 device->uuid);
861 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400862
863 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400864 if (device->name) {
865 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300866 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400867 rcu_assign_pointer(new_device->name, name);
868 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300869
Xiao Guangrong1f781602011-04-20 10:09:16 +0000870 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300871 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000872
873 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400874 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000875 mutex_unlock(&fs_devices->device_list_mutex);
876
Yan Zhenge4404d62008-12-12 10:03:26 -0500877 WARN_ON(fs_devices->open_devices);
878 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500879 fs_devices->opened = 0;
880 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500881
Chris Mason8a4b83c2008-03-24 15:02:07 -0400882 return 0;
883}
884
Yan Zheng2b820322008-11-17 21:11:30 -0500885int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
886{
Yan Zhenge4404d62008-12-12 10:03:26 -0500887 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500888 int ret;
889
890 mutex_lock(&uuid_mutex);
891 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500892 if (!fs_devices->opened) {
893 seed_devices = fs_devices->seed;
894 fs_devices->seed = NULL;
895 }
Yan Zheng2b820322008-11-17 21:11:30 -0500896 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500897
898 while (seed_devices) {
899 fs_devices = seed_devices;
900 seed_devices = fs_devices->seed;
901 __btrfs_close_devices(fs_devices);
902 free_fs_devices(fs_devices);
903 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000904 /*
905 * Wait for rcu kworkers under __btrfs_close_devices
906 * to finish all blkdev_puts so device is really
907 * free when umount is done.
908 */
909 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500910 return ret;
911}
912
Yan Zhenge4404d62008-12-12 10:03:26 -0500913static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
914 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400915{
Josef Bacikd5e20032011-08-04 14:52:27 +0000916 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400917 struct block_device *bdev;
918 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400919 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800920 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400921 struct buffer_head *bh;
922 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400923 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500924 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400925 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400926
Tejun Heod4d77622010-11-13 11:55:18 +0100927 flags |= FMODE_EXCL;
928
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500929 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400930 if (device->bdev)
931 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400932 if (!device->name)
933 continue;
934
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000935 /* Just open everything we can; ignore failures here */
936 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
937 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100938 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400939
940 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000941 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400942 if (devid != device->devid)
943 goto error_brelse;
944
Yan Zheng2b820322008-11-17 21:11:30 -0500945 if (memcmp(device->uuid, disk_super->dev_item.uuid,
946 BTRFS_UUID_SIZE))
947 goto error_brelse;
948
949 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800950 if (!latest_dev ||
951 device->generation > latest_dev->generation)
952 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400953
Yan Zheng2b820322008-11-17 21:11:30 -0500954 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
955 device->writeable = 0;
956 } else {
957 device->writeable = !bdev_read_only(bdev);
958 seeding = 0;
959 }
960
Josef Bacikd5e20032011-08-04 14:52:27 +0000961 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800962 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000963 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000964
Chris Mason8a4b83c2008-03-24 15:02:07 -0400965 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400966 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500967 device->mode = flags;
968
Chris Masonc2898112009-06-10 09:51:32 -0400969 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
970 fs_devices->rotating = 1;
971
Chris Masona0af4692008-05-13 16:03:06 -0400972 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300973 if (device->writeable &&
974 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500975 fs_devices->rw_devices++;
976 list_add(&device->dev_alloc_list,
977 &fs_devices->alloc_list);
978 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000979 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400980 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400981
Chris Masona0af4692008-05-13 16:03:06 -0400982error_brelse:
983 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100984 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400985 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400986 }
Chris Masona0af4692008-05-13 16:03:06 -0400987 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300988 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400989 goto out;
990 }
Yan Zheng2b820322008-11-17 21:11:30 -0500991 fs_devices->seeding = seeding;
992 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800993 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500994 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400995out:
Yan Zheng2b820322008-11-17 21:11:30 -0500996 return ret;
997}
998
999int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -05001000 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -05001001{
1002 int ret;
1003
1004 mutex_lock(&uuid_mutex);
1005 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001006 fs_devices->opened++;
1007 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05001008 } else {
Chris Mason15916de2008-11-19 21:17:22 -05001009 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -05001010 }
Chris Mason8a4b83c2008-03-24 15:02:07 -04001011 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001012 return ret;
1013}
1014
David Sterba6f60cbd2013-02-15 11:31:02 -07001015/*
1016 * Look for a btrfs signature on a device. This may be called out of the mount path
1017 * and we are not allowed to call set_blocksize during the scan. The superblock
1018 * is read via pagecache
1019 */
Christoph Hellwig97288f22008-12-02 06:36:09 -05001020int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -04001021 struct btrfs_fs_devices **fs_devices_ret)
1022{
1023 struct btrfs_super_block *disk_super;
1024 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -07001025 struct page *page;
1026 void *p;
1027 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001028 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001029 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001030 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001031 u64 bytenr;
1032 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001033
David Sterba6f60cbd2013-02-15 11:31:02 -07001034 /*
1035 * we would like to check all the supers, but that would make
1036 * a btrfs mount succeed after a mkfs from a different FS.
1037 * So, we need to add a special mount option to scan for
1038 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1039 */
1040 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001041 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001042 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001043
1044 bdev = blkdev_get_by_path(path, flags, holder);
1045
1046 if (IS_ERR(bdev)) {
1047 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001048 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001049 }
1050
1051 /* make sure our super fits in the device */
1052 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
1053 goto error_bdev_put;
1054
1055 /* make sure our super fits in the page */
1056 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
1057 goto error_bdev_put;
1058
1059 /* make sure our super doesn't straddle pages on disk */
1060 index = bytenr >> PAGE_CACHE_SHIFT;
1061 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
1062 goto error_bdev_put;
1063
1064 /* pull in the page with our super */
1065 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1066 index, GFP_NOFS);
1067
1068 if (IS_ERR_OR_NULL(page))
1069 goto error_bdev_put;
1070
1071 p = kmap(page);
1072
1073 /* align our pointer to the offset of the super block */
1074 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
1075
1076 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001077 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -07001078 goto error_unmap;
1079
Xiao Guangronga3438322010-01-06 11:48:18 +00001080 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001081 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001082 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001083
Chris Mason8a4b83c2008-03-24 15:02:07 -04001084 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001085 if (ret > 0) {
1086 if (disk_super->label[0]) {
1087 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1088 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1089 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1090 } else {
1091 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1092 }
1093
1094 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1095 ret = 0;
1096 }
Josef Bacik02db0842012-06-21 16:03:58 -04001097 if (!ret && fs_devices_ret)
1098 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001099
1100error_unmap:
1101 kunmap(page);
1102 page_cache_release(page);
1103
1104error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001105 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001106error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001107 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001108 return ret;
1109}
Chris Mason0b86a832008-03-24 15:01:56 -04001110
Miao Xie6d07bce2011-01-05 10:07:31 +00001111/* helper to account the used device space in the range */
1112int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1113 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001114{
1115 struct btrfs_key key;
1116 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001117 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001118 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001119 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001120 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001121 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001122 struct extent_buffer *l;
1123
Miao Xie6d07bce2011-01-05 10:07:31 +00001124 *length = 0;
1125
Stefan Behrens63a212a2012-11-05 18:29:28 +01001126 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001127 return 0;
1128
Yan Zheng2b820322008-11-17 21:11:30 -05001129 path = btrfs_alloc_path();
1130 if (!path)
1131 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001132 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001133
Chris Mason0b86a832008-03-24 15:01:56 -04001134 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001135 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001136 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001137
1138 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001139 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001140 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001141 if (ret > 0) {
1142 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1143 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001144 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001145 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001146
Chris Mason0b86a832008-03-24 15:01:56 -04001147 while (1) {
1148 l = path->nodes[0];
1149 slot = path->slots[0];
1150 if (slot >= btrfs_header_nritems(l)) {
1151 ret = btrfs_next_leaf(root, path);
1152 if (ret == 0)
1153 continue;
1154 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001155 goto out;
1156
1157 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001158 }
1159 btrfs_item_key_to_cpu(l, &key, slot);
1160
1161 if (key.objectid < device->devid)
1162 goto next;
1163
1164 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001165 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001166
David Sterba962a2982014-06-04 18:41:45 +02001167 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001168 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001169
Chris Mason0b86a832008-03-24 15:01:56 -04001170 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001171 extent_end = key.offset + btrfs_dev_extent_length(l,
1172 dev_extent);
1173 if (key.offset <= start && extent_end > end) {
1174 *length = end - start + 1;
1175 break;
1176 } else if (key.offset <= start && extent_end > start)
1177 *length += extent_end - start;
1178 else if (key.offset > start && extent_end <= end)
1179 *length += extent_end - key.offset;
1180 else if (key.offset > start && key.offset <= end) {
1181 *length += end - key.offset + 1;
1182 break;
1183 } else if (key.offset > end)
1184 break;
1185
1186next:
1187 path->slots[0]++;
1188 }
1189 ret = 0;
1190out:
1191 btrfs_free_path(path);
1192 return ret;
1193}
1194
Jeff Mahoney499f3772015-06-15 09:41:17 -04001195static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001196 struct btrfs_device *device,
1197 u64 *start, u64 len)
1198{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001199 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001200 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001201 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001202 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001203 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001204
Jeff Mahoney499f3772015-06-15 09:41:17 -04001205 if (transaction)
1206 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001207again:
1208 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001209 struct map_lookup *map;
1210 int i;
1211
1212 map = (struct map_lookup *)em->bdev;
1213 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001214 u64 end;
1215
Josef Bacik6df9a952013-06-27 13:22:46 -04001216 if (map->stripes[i].dev != device)
1217 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001218 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001219 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001220 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001221 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001222 /*
1223 * Make sure that while processing the pinned list we do
1224 * not override our *start with a lower value, because
1225 * we can have pinned chunks that fall within this
1226 * device hole and that have lower physical addresses
1227 * than the pending chunks we processed before. If we
1228 * do not take this special care we can end up getting
1229 * 2 pending chunks that start at the same physical
1230 * device offsets because the end offset of a pinned
1231 * chunk can be equal to the start offset of some
1232 * pending chunk.
1233 */
1234 end = map->stripes[i].physical + em->orig_block_len;
1235 if (end > *start) {
1236 *start = end;
1237 ret = 1;
1238 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001239 }
1240 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001241 if (search_list != &fs_info->pinned_chunks) {
1242 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001243 goto again;
1244 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001245
1246 return ret;
1247}
1248
1249
Chris Mason0b86a832008-03-24 15:01:56 -04001250/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001251 * find_free_dev_extent_start - find free space in the specified device
1252 * @device: the device which we search the free space in
1253 * @num_bytes: the size of the free space that we need
1254 * @search_start: the position from which to begin the search
1255 * @start: store the start of the free space.
1256 * @len: the size of the free space. that we find, or the size
1257 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001258 *
Chris Mason0b86a832008-03-24 15:01:56 -04001259 * this uses a pretty simple search, the expectation is that it is
1260 * called very infrequently and that a given device has a small number
1261 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001262 *
1263 * @start is used to store the start of the free space if we find. But if we
1264 * don't find suitable free space, it will be used to store the start position
1265 * of the max free space.
1266 *
1267 * @len is used to store the size of the free space that we find.
1268 * But if we don't find suitable free space, it is used to store the size of
1269 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001270 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001271int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1272 struct btrfs_device *device, u64 num_bytes,
1273 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001274{
1275 struct btrfs_key key;
1276 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001277 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001278 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001279 u64 hole_size;
1280 u64 max_hole_start;
1281 u64 max_hole_size;
1282 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001283 u64 search_end = device->total_bytes;
1284 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001285 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001286 struct extent_buffer *l;
1287
Josef Bacik6df9a952013-06-27 13:22:46 -04001288 path = btrfs_alloc_path();
1289 if (!path)
1290 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001291
Miao Xie7bfc8372011-01-05 10:07:26 +00001292 max_hole_start = search_start;
1293 max_hole_size = 0;
1294
Zhao Leif2ab7612015-02-16 18:52:17 +08001295again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001296 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001297 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001298 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001299 }
1300
Miao Xie7bfc8372011-01-05 10:07:26 +00001301 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001302 path->search_commit_root = 1;
1303 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001304
Chris Mason0b86a832008-03-24 15:01:56 -04001305 key.objectid = device->devid;
1306 key.offset = search_start;
1307 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001308
Li Zefan125ccb02011-12-08 15:07:24 +08001309 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001310 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001311 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001312 if (ret > 0) {
1313 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1314 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001315 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001316 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001317
Chris Mason0b86a832008-03-24 15:01:56 -04001318 while (1) {
1319 l = path->nodes[0];
1320 slot = path->slots[0];
1321 if (slot >= btrfs_header_nritems(l)) {
1322 ret = btrfs_next_leaf(root, path);
1323 if (ret == 0)
1324 continue;
1325 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001326 goto out;
1327
1328 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001329 }
1330 btrfs_item_key_to_cpu(l, &key, slot);
1331
1332 if (key.objectid < device->devid)
1333 goto next;
1334
1335 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001336 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001337
David Sterba962a2982014-06-04 18:41:45 +02001338 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001339 goto next;
1340
Miao Xie7bfc8372011-01-05 10:07:26 +00001341 if (key.offset > search_start) {
1342 hole_size = key.offset - search_start;
1343
Josef Bacik6df9a952013-06-27 13:22:46 -04001344 /*
1345 * Have to check before we set max_hole_start, otherwise
1346 * we could end up sending back this offset anyway.
1347 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001348 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001349 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001350 hole_size)) {
1351 if (key.offset >= search_start) {
1352 hole_size = key.offset - search_start;
1353 } else {
1354 WARN_ON_ONCE(1);
1355 hole_size = 0;
1356 }
1357 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001358
Miao Xie7bfc8372011-01-05 10:07:26 +00001359 if (hole_size > max_hole_size) {
1360 max_hole_start = search_start;
1361 max_hole_size = hole_size;
1362 }
1363
1364 /*
1365 * If this free space is greater than which we need,
1366 * it must be the max free space that we have found
1367 * until now, so max_hole_start must point to the start
1368 * of this free space and the length of this free space
1369 * is stored in max_hole_size. Thus, we return
1370 * max_hole_start and max_hole_size and go back to the
1371 * caller.
1372 */
1373 if (hole_size >= num_bytes) {
1374 ret = 0;
1375 goto out;
1376 }
1377 }
1378
Chris Mason0b86a832008-03-24 15:01:56 -04001379 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001380 extent_end = key.offset + btrfs_dev_extent_length(l,
1381 dev_extent);
1382 if (extent_end > search_start)
1383 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001384next:
1385 path->slots[0]++;
1386 cond_resched();
1387 }
Chris Mason0b86a832008-03-24 15:01:56 -04001388
liubo38c01b92011-08-02 02:39:03 +00001389 /*
1390 * At this point, search_start should be the end of
1391 * allocated dev extents, and when shrinking the device,
1392 * search_end may be smaller than search_start.
1393 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001394 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001395 hole_size = search_end - search_start;
1396
Jeff Mahoney499f3772015-06-15 09:41:17 -04001397 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001398 hole_size)) {
1399 btrfs_release_path(path);
1400 goto again;
1401 }
Chris Mason0b86a832008-03-24 15:01:56 -04001402
Zhao Leif2ab7612015-02-16 18:52:17 +08001403 if (hole_size > max_hole_size) {
1404 max_hole_start = search_start;
1405 max_hole_size = hole_size;
1406 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001407 }
1408
Miao Xie7bfc8372011-01-05 10:07:26 +00001409 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001410 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001411 ret = -ENOSPC;
1412 else
1413 ret = 0;
1414
1415out:
Yan Zheng2b820322008-11-17 21:11:30 -05001416 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001417 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001418 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001419 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001420 return ret;
1421}
1422
Jeff Mahoney499f3772015-06-15 09:41:17 -04001423int find_free_dev_extent(struct btrfs_trans_handle *trans,
1424 struct btrfs_device *device, u64 num_bytes,
1425 u64 *start, u64 *len)
1426{
1427 struct btrfs_root *root = device->dev_root;
1428 u64 search_start;
1429
1430 /* FIXME use last free of some kind */
1431
1432 /*
1433 * we don't want to overwrite the superblock on the drive,
1434 * so we make sure to start at an offset of at least 1MB
1435 */
1436 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1437 return find_free_dev_extent_start(trans->transaction, device,
1438 num_bytes, search_start, start, len);
1439}
1440
Christoph Hellwigb2950862008-12-02 09:54:17 -05001441static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001442 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001443 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001444{
1445 int ret;
1446 struct btrfs_path *path;
1447 struct btrfs_root *root = device->dev_root;
1448 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001449 struct btrfs_key found_key;
1450 struct extent_buffer *leaf = NULL;
1451 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001452
1453 path = btrfs_alloc_path();
1454 if (!path)
1455 return -ENOMEM;
1456
1457 key.objectid = device->devid;
1458 key.offset = start;
1459 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001460again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001461 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001462 if (ret > 0) {
1463 ret = btrfs_previous_item(root, path, key.objectid,
1464 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001465 if (ret)
1466 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001467 leaf = path->nodes[0];
1468 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1469 extent = btrfs_item_ptr(leaf, path->slots[0],
1470 struct btrfs_dev_extent);
1471 BUG_ON(found_key.offset > start || found_key.offset +
1472 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001473 key = found_key;
1474 btrfs_release_path(path);
1475 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001476 } else if (ret == 0) {
1477 leaf = path->nodes[0];
1478 extent = btrfs_item_ptr(leaf, path->slots[0],
1479 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001480 } else {
1481 btrfs_error(root->fs_info, ret, "Slot search failed");
1482 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001483 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001484
Miao Xie2196d6e2014-09-03 21:35:41 +08001485 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1486
Chris Mason8f18cf12008-04-25 16:53:30 -04001487 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001488 if (ret) {
1489 btrfs_error(root->fs_info, ret,
1490 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001491 } else {
1492 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001493 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001494out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001495 btrfs_free_path(path);
1496 return ret;
1497}
1498
Eric Sandeen48a3b632013-04-25 20:41:01 +00001499static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1500 struct btrfs_device *device,
1501 u64 chunk_tree, u64 chunk_objectid,
1502 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001503{
1504 int ret;
1505 struct btrfs_path *path;
1506 struct btrfs_root *root = device->dev_root;
1507 struct btrfs_dev_extent *extent;
1508 struct extent_buffer *leaf;
1509 struct btrfs_key key;
1510
Chris Masondfe25022008-05-13 13:46:40 -04001511 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001512 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001513 path = btrfs_alloc_path();
1514 if (!path)
1515 return -ENOMEM;
1516
Chris Mason0b86a832008-03-24 15:01:56 -04001517 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001518 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001519 key.type = BTRFS_DEV_EXTENT_KEY;
1520 ret = btrfs_insert_empty_item(trans, root, path, &key,
1521 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001522 if (ret)
1523 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001524
1525 leaf = path->nodes[0];
1526 extent = btrfs_item_ptr(leaf, path->slots[0],
1527 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001528 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1529 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1530 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1531
1532 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001533 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001534
Chris Mason0b86a832008-03-24 15:01:56 -04001535 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1536 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001537out:
Chris Mason0b86a832008-03-24 15:01:56 -04001538 btrfs_free_path(path);
1539 return ret;
1540}
1541
Josef Bacik6df9a952013-06-27 13:22:46 -04001542static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001543{
Josef Bacik6df9a952013-06-27 13:22:46 -04001544 struct extent_map_tree *em_tree;
1545 struct extent_map *em;
1546 struct rb_node *n;
1547 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001548
Josef Bacik6df9a952013-06-27 13:22:46 -04001549 em_tree = &fs_info->mapping_tree.map_tree;
1550 read_lock(&em_tree->lock);
1551 n = rb_last(&em_tree->map);
1552 if (n) {
1553 em = rb_entry(n, struct extent_map, rb_node);
1554 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001555 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001556 read_unlock(&em_tree->lock);
1557
Chris Mason0b86a832008-03-24 15:01:56 -04001558 return ret;
1559}
1560
Ilya Dryomov53f10652013-08-12 14:33:01 +03001561static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1562 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001563{
1564 int ret;
1565 struct btrfs_key key;
1566 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001567 struct btrfs_path *path;
1568
Yan Zheng2b820322008-11-17 21:11:30 -05001569 path = btrfs_alloc_path();
1570 if (!path)
1571 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001572
1573 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1574 key.type = BTRFS_DEV_ITEM_KEY;
1575 key.offset = (u64)-1;
1576
Ilya Dryomov53f10652013-08-12 14:33:01 +03001577 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001578 if (ret < 0)
1579 goto error;
1580
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001581 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001582
Ilya Dryomov53f10652013-08-12 14:33:01 +03001583 ret = btrfs_previous_item(fs_info->chunk_root, path,
1584 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001585 BTRFS_DEV_ITEM_KEY);
1586 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001587 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001588 } else {
1589 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1590 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001591 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001592 }
1593 ret = 0;
1594error:
Yan Zheng2b820322008-11-17 21:11:30 -05001595 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001596 return ret;
1597}
1598
1599/*
1600 * the device information is stored in the chunk root
1601 * the btrfs_device struct should be fully filled in
1602 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001603static int btrfs_add_device(struct btrfs_trans_handle *trans,
1604 struct btrfs_root *root,
1605 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001606{
1607 int ret;
1608 struct btrfs_path *path;
1609 struct btrfs_dev_item *dev_item;
1610 struct extent_buffer *leaf;
1611 struct btrfs_key key;
1612 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001613
1614 root = root->fs_info->chunk_root;
1615
1616 path = btrfs_alloc_path();
1617 if (!path)
1618 return -ENOMEM;
1619
Chris Mason0b86a832008-03-24 15:01:56 -04001620 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1621 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001622 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001623
1624 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001625 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001626 if (ret)
1627 goto out;
1628
1629 leaf = path->nodes[0];
1630 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1631
1632 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001633 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001634 btrfs_set_device_type(leaf, dev_item, device->type);
1635 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1636 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1637 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001638 btrfs_set_device_total_bytes(leaf, dev_item,
1639 btrfs_device_get_disk_total_bytes(device));
1640 btrfs_set_device_bytes_used(leaf, dev_item,
1641 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001642 btrfs_set_device_group(leaf, dev_item, 0);
1643 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1644 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001645 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001646
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001647 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001648 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001649 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001650 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001651 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001652
Yan Zheng2b820322008-11-17 21:11:30 -05001653 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001654out:
1655 btrfs_free_path(path);
1656 return ret;
1657}
Chris Mason8f18cf12008-04-25 16:53:30 -04001658
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001659/*
1660 * Function to update ctime/mtime for a given device path.
1661 * Mainly used for ctime/mtime based probe like libblkid.
1662 */
1663static void update_dev_time(char *path_name)
1664{
1665 struct file *filp;
1666
1667 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001668 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001669 return;
1670 file_update_time(filp);
1671 filp_close(filp, NULL);
1672 return;
1673}
1674
Chris Masona061fc82008-05-07 11:43:44 -04001675static int btrfs_rm_dev_item(struct btrfs_root *root,
1676 struct btrfs_device *device)
1677{
1678 int ret;
1679 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001680 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001681 struct btrfs_trans_handle *trans;
1682
1683 root = root->fs_info->chunk_root;
1684
1685 path = btrfs_alloc_path();
1686 if (!path)
1687 return -ENOMEM;
1688
Yan, Zhenga22285a2010-05-16 10:48:46 -04001689 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001690 if (IS_ERR(trans)) {
1691 btrfs_free_path(path);
1692 return PTR_ERR(trans);
1693 }
Chris Masona061fc82008-05-07 11:43:44 -04001694 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1695 key.type = BTRFS_DEV_ITEM_KEY;
1696 key.offset = device->devid;
1697
1698 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1699 if (ret < 0)
1700 goto out;
1701
1702 if (ret > 0) {
1703 ret = -ENOENT;
1704 goto out;
1705 }
1706
1707 ret = btrfs_del_item(trans, root, path);
1708 if (ret)
1709 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001710out:
1711 btrfs_free_path(path);
1712 btrfs_commit_transaction(trans, root);
1713 return ret;
1714}
1715
1716int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1717{
1718 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001719 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001720 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001721 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001722 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001723 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001724 u64 all_avail;
1725 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001726 u64 num_devices;
1727 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001728 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001729 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001730 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001731
Chris Masona061fc82008-05-07 11:43:44 -04001732 mutex_lock(&uuid_mutex);
1733
Miao Xiede98ced2013-01-29 10:13:12 +00001734 do {
1735 seq = read_seqbegin(&root->fs_info->profiles_lock);
1736
1737 all_avail = root->fs_info->avail_data_alloc_bits |
1738 root->fs_info->avail_system_alloc_bits |
1739 root->fs_info->avail_metadata_alloc_bits;
1740 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001741
Stefan Behrens8dabb742012-11-06 13:15:27 +01001742 num_devices = root->fs_info->fs_devices->num_devices;
1743 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1744 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1745 WARN_ON(num_devices < 1);
1746 num_devices--;
1747 }
1748 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1749
1750 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001751 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001752 goto out;
1753 }
1754
Stefan Behrens8dabb742012-11-06 13:15:27 +01001755 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001756 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001757 goto out;
1758 }
1759
David Woodhouse53b381b2013-01-29 18:40:14 -05001760 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1761 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001762 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001763 goto out;
1764 }
1765 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1766 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001767 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001768 goto out;
1769 }
1770
Chris Masondfe25022008-05-13 13:46:40 -04001771 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001772 struct list_head *devices;
1773 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001774
Chris Masondfe25022008-05-13 13:46:40 -04001775 device = NULL;
1776 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001777 /*
1778 * It is safe to read the devices since the volume_mutex
1779 * is held.
1780 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001781 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001782 if (tmp->in_fs_metadata &&
1783 !tmp->is_tgtdev_for_dev_replace &&
1784 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001785 device = tmp;
1786 break;
1787 }
1788 }
1789 bdev = NULL;
1790 bh = NULL;
1791 disk_super = NULL;
1792 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001793 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001794 goto out;
1795 }
Chris Masondfe25022008-05-13 13:46:40 -04001796 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001797 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001798 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001799 root->fs_info->bdev_holder, 0,
1800 &bdev, &bh);
1801 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001802 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001803 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001804 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001805 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001806 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001807 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001808 if (!device) {
1809 ret = -ENOENT;
1810 goto error_brelse;
1811 }
Chris Masondfe25022008-05-13 13:46:40 -04001812 }
Yan Zheng2b820322008-11-17 21:11:30 -05001813
Stefan Behrens63a212a2012-11-05 18:29:28 +01001814 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001815 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001816 goto error_brelse;
1817 }
1818
Yan Zheng2b820322008-11-17 21:11:30 -05001819 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001820 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001821 goto error_brelse;
1822 }
1823
1824 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001825 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001826 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001827 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001828 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001829 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001830 }
Chris Masona061fc82008-05-07 11:43:44 -04001831
Carey Underwoodd7901552013-03-04 16:37:06 -06001832 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001833 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001834 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001835 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001836 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001837
Stefan Behrens63a212a2012-11-05 18:29:28 +01001838 /*
1839 * TODO: the superblock still includes this device in its num_devices
1840 * counter although write_all_supers() is not locked out. This
1841 * could give a filesystem state which requires a degraded mount.
1842 */
Chris Masona061fc82008-05-07 11:43:44 -04001843 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1844 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001845 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001846
Yan Zheng2b820322008-11-17 21:11:30 -05001847 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001848 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001849
1850 /*
1851 * the device list mutex makes sure that we don't change
1852 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001853 * the device supers. Whoever is writing all supers, should
1854 * lock the device list mutex before getting the number of
1855 * devices in the super block (super_copy). Conversely,
1856 * whoever updates the number of devices in the super block
1857 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001858 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001859
1860 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001861 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001862 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001863
Yan Zhenge4404d62008-12-12 10:03:26 -05001864 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001865 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001866
Chris Masoncd02dca2010-12-13 14:56:23 -05001867 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001868 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001869
Yan Zheng2b820322008-11-17 21:11:30 -05001870 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1871 struct btrfs_device, dev_list);
1872 if (device->bdev == root->fs_info->sb->s_bdev)
1873 root->fs_info->sb->s_bdev = next_device->bdev;
1874 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1875 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1876
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001877 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001878 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001879 /* remove sysfs entry */
Anand Jain6c14a162015-03-10 06:38:34 +08001880 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001881 }
Anand Jain99994cd2014-06-03 11:36:00 +08001882
Xiao Guangrong1f781602011-04-20 10:09:16 +00001883 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001884
David Sterba6c417612011-04-13 15:41:04 +02001885 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1886 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001887 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001888
Xiao Guangrong1f781602011-04-20 10:09:16 +00001889 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001890 struct btrfs_fs_devices *fs_devices;
1891 fs_devices = root->fs_info->fs_devices;
1892 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001893 if (fs_devices->seed == cur_devices) {
1894 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001895 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001896 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001897 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001898 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001899 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001900 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001901 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001902 }
1903
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001904 root->fs_info->num_tolerated_disk_barrier_failures =
1905 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1906
Yan Zheng2b820322008-11-17 21:11:30 -05001907 /*
1908 * at this point, the device is zero sized. We want to
1909 * remove it from the devices list and zero out the old super
1910 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001911 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001912 u64 bytenr;
1913 int i;
1914
Chris Masondfe25022008-05-13 13:46:40 -04001915 /* make sure this device isn't detected as part of
1916 * the FS anymore
1917 */
1918 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1919 set_buffer_dirty(bh);
1920 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001921
1922 /* clear the mirror copies of super block on the disk
1923 * being removed, 0th copy is been taken care above and
1924 * the below would take of the rest
1925 */
1926 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1927 bytenr = btrfs_sb_offset(i);
1928 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1929 i_size_read(bdev->bd_inode))
1930 break;
1931
1932 brelse(bh);
1933 bh = __bread(bdev, bytenr / 4096,
1934 BTRFS_SUPER_INFO_SIZE);
1935 if (!bh)
1936 continue;
1937
1938 disk_super = (struct btrfs_super_block *)bh->b_data;
1939
1940 if (btrfs_super_bytenr(disk_super) != bytenr ||
1941 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1942 continue;
1943 }
1944 memset(&disk_super->magic, 0,
1945 sizeof(disk_super->magic));
1946 set_buffer_dirty(bh);
1947 sync_dirty_buffer(bh);
1948 }
Chris Masondfe25022008-05-13 13:46:40 -04001949 }
Chris Masona061fc82008-05-07 11:43:44 -04001950
Chris Masona061fc82008-05-07 11:43:44 -04001951 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001952
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001953 if (bdev) {
1954 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001955 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001956
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001957 /* Update ctime/mtime for device path for libblkid */
1958 update_dev_time(device_path);
1959 }
1960
Chris Masona061fc82008-05-07 11:43:44 -04001961error_brelse:
1962 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001963 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001964 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001965out:
1966 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001967 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001968error_undo:
1969 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001970 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001971 list_add(&device->dev_alloc_list,
1972 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001973 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001974 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001975 }
1976 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001977}
1978
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001979void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1980 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001981{
Anand Jaind51908c2014-08-13 14:24:19 +08001982 struct btrfs_fs_devices *fs_devices;
1983
Stefan Behrense93c89c2012-11-05 17:33:06 +01001984 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001985
Anand Jain25e8e912014-08-20 10:56:56 +08001986 /*
1987 * in case of fs with no seed, srcdev->fs_devices will point
1988 * to fs_devices of fs_info. However when the dev being replaced is
1989 * a seed dev it will point to the seed's local fs_devices. In short
1990 * srcdev will have its correct fs_devices in both the cases.
1991 */
1992 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001993
Stefan Behrense93c89c2012-11-05 17:33:06 +01001994 list_del_rcu(&srcdev->dev_list);
1995 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001996 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001997 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001998 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001999
Miao Xie82372bc2014-09-03 21:35:44 +08002000 if (srcdev->writeable) {
2001 fs_devices->rw_devices--;
2002 /* zero out the old super if it is writable */
2003 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03002004 }
2005
Miao Xie82372bc2014-09-03 21:35:44 +08002006 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08002007 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08002008}
2009
2010void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
2011 struct btrfs_device *srcdev)
2012{
2013 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002014
Stefan Behrense93c89c2012-11-05 17:33:06 +01002015 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002016
2017 /*
2018 * unless fs_devices is seed fs, num_devices shouldn't go
2019 * zero
2020 */
2021 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
2022
2023 /* if this is no devs we rather delete the fs_devices */
2024 if (!fs_devices->num_devices) {
2025 struct btrfs_fs_devices *tmp_fs_devices;
2026
2027 tmp_fs_devices = fs_info->fs_devices;
2028 while (tmp_fs_devices) {
2029 if (tmp_fs_devices->seed == fs_devices) {
2030 tmp_fs_devices->seed = fs_devices->seed;
2031 break;
2032 }
2033 tmp_fs_devices = tmp_fs_devices->seed;
2034 }
2035 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002036 __btrfs_close_devices(fs_devices);
2037 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002038 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002039}
2040
2041void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2042 struct btrfs_device *tgtdev)
2043{
2044 struct btrfs_device *next_device;
2045
Miao Xie67a2c452014-09-03 21:35:43 +08002046 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002047 WARN_ON(!tgtdev);
2048 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002049
2050 btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev);
2051
Stefan Behrense93c89c2012-11-05 17:33:06 +01002052 if (tgtdev->bdev) {
2053 btrfs_scratch_superblock(tgtdev);
2054 fs_info->fs_devices->open_devices--;
2055 }
2056 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002057
2058 next_device = list_entry(fs_info->fs_devices->devices.next,
2059 struct btrfs_device, dev_list);
2060 if (tgtdev->bdev == fs_info->sb->s_bdev)
2061 fs_info->sb->s_bdev = next_device->bdev;
2062 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
2063 fs_info->fs_devices->latest_bdev = next_device->bdev;
2064 list_del_rcu(&tgtdev->dev_list);
2065
2066 call_rcu(&tgtdev->rcu, free_device);
2067
2068 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002069 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002070}
2071
Eric Sandeen48a3b632013-04-25 20:41:01 +00002072static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2073 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002074{
2075 int ret = 0;
2076 struct btrfs_super_block *disk_super;
2077 u64 devid;
2078 u8 *dev_uuid;
2079 struct block_device *bdev;
2080 struct buffer_head *bh;
2081
2082 *device = NULL;
2083 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2084 root->fs_info->bdev_holder, 0, &bdev, &bh);
2085 if (ret)
2086 return ret;
2087 disk_super = (struct btrfs_super_block *)bh->b_data;
2088 devid = btrfs_stack_device_id(&disk_super->dev_item);
2089 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002090 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002091 disk_super->fsid);
2092 brelse(bh);
2093 if (!*device)
2094 ret = -ENOENT;
2095 blkdev_put(bdev, FMODE_READ);
2096 return ret;
2097}
2098
2099int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2100 char *device_path,
2101 struct btrfs_device **device)
2102{
2103 *device = NULL;
2104 if (strcmp(device_path, "missing") == 0) {
2105 struct list_head *devices;
2106 struct btrfs_device *tmp;
2107
2108 devices = &root->fs_info->fs_devices->devices;
2109 /*
2110 * It is safe to read the devices since the volume_mutex
2111 * is held by the caller.
2112 */
2113 list_for_each_entry(tmp, devices, dev_list) {
2114 if (tmp->in_fs_metadata && !tmp->bdev) {
2115 *device = tmp;
2116 break;
2117 }
2118 }
2119
2120 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05002121 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002122 return -ENOENT;
2123 }
2124
2125 return 0;
2126 } else {
2127 return btrfs_find_device_by_path(root, device_path, device);
2128 }
2129}
2130
Yan Zheng2b820322008-11-17 21:11:30 -05002131/*
2132 * does all the dirty work required for changing file system's UUID.
2133 */
Li Zefan125ccb02011-12-08 15:07:24 +08002134static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002135{
2136 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2137 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002138 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002139 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002140 struct btrfs_device *device;
2141 u64 super_flags;
2142
2143 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002144 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002145 return -EINVAL;
2146
Ilya Dryomov2208a372013-08-12 14:33:03 +03002147 seed_devices = __alloc_fs_devices();
2148 if (IS_ERR(seed_devices))
2149 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002150
Yan Zhenge4404d62008-12-12 10:03:26 -05002151 old_devices = clone_fs_devices(fs_devices);
2152 if (IS_ERR(old_devices)) {
2153 kfree(seed_devices);
2154 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002155 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002156
Yan Zheng2b820322008-11-17 21:11:30 -05002157 list_add(&old_devices->list, &fs_uuids);
2158
Yan Zhenge4404d62008-12-12 10:03:26 -05002159 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2160 seed_devices->opened = 1;
2161 INIT_LIST_HEAD(&seed_devices->devices);
2162 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002163 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002164
2165 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002166 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2167 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002168 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002169 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002170
2171 lock_chunks(root);
2172 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2173 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002174
Yan Zheng2b820322008-11-17 21:11:30 -05002175 fs_devices->seeding = 0;
2176 fs_devices->num_devices = 0;
2177 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002178 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002179 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002180 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002181
2182 generate_random_uuid(fs_devices->fsid);
2183 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2184 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002185 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2186
Yan Zheng2b820322008-11-17 21:11:30 -05002187 super_flags = btrfs_super_flags(disk_super) &
2188 ~BTRFS_SUPER_FLAG_SEEDING;
2189 btrfs_set_super_flags(disk_super, super_flags);
2190
2191 return 0;
2192}
2193
2194/*
2195 * strore the expected generation for seed devices in device items.
2196 */
2197static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2198 struct btrfs_root *root)
2199{
2200 struct btrfs_path *path;
2201 struct extent_buffer *leaf;
2202 struct btrfs_dev_item *dev_item;
2203 struct btrfs_device *device;
2204 struct btrfs_key key;
2205 u8 fs_uuid[BTRFS_UUID_SIZE];
2206 u8 dev_uuid[BTRFS_UUID_SIZE];
2207 u64 devid;
2208 int ret;
2209
2210 path = btrfs_alloc_path();
2211 if (!path)
2212 return -ENOMEM;
2213
2214 root = root->fs_info->chunk_root;
2215 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2216 key.offset = 0;
2217 key.type = BTRFS_DEV_ITEM_KEY;
2218
2219 while (1) {
2220 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2221 if (ret < 0)
2222 goto error;
2223
2224 leaf = path->nodes[0];
2225next_slot:
2226 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2227 ret = btrfs_next_leaf(root, path);
2228 if (ret > 0)
2229 break;
2230 if (ret < 0)
2231 goto error;
2232 leaf = path->nodes[0];
2233 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002234 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002235 continue;
2236 }
2237
2238 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2239 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2240 key.type != BTRFS_DEV_ITEM_KEY)
2241 break;
2242
2243 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2244 struct btrfs_dev_item);
2245 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002246 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002247 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002248 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002249 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002250 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2251 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002252 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002253
2254 if (device->fs_devices->seeding) {
2255 btrfs_set_device_generation(leaf, dev_item,
2256 device->generation);
2257 btrfs_mark_buffer_dirty(leaf);
2258 }
2259
2260 path->slots[0]++;
2261 goto next_slot;
2262 }
2263 ret = 0;
2264error:
2265 btrfs_free_path(path);
2266 return ret;
2267}
2268
Chris Mason788f20e2008-04-28 15:29:42 -04002269int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2270{
Josef Bacikd5e20032011-08-04 14:52:27 +00002271 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002272 struct btrfs_trans_handle *trans;
2273 struct btrfs_device *device;
2274 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002275 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002276 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002277 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002278 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002279 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002280 int ret = 0;
2281
Yan Zheng2b820322008-11-17 21:11:30 -05002282 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002283 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002284
Li Zefana5d16332011-12-07 20:08:40 -05002285 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002286 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002287 if (IS_ERR(bdev))
2288 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002289
Yan Zheng2b820322008-11-17 21:11:30 -05002290 if (root->fs_info->fs_devices->seeding) {
2291 seeding_dev = 1;
2292 down_write(&sb->s_umount);
2293 mutex_lock(&uuid_mutex);
2294 }
2295
Chris Mason8c8bee12008-09-29 11:19:10 -04002296 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002297
Chris Mason788f20e2008-04-28 15:29:42 -04002298 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002299
2300 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002301 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002302 if (device->bdev == bdev) {
2303 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002304 mutex_unlock(
2305 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002306 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002307 }
2308 }
Liu Bod25628b2012-11-14 14:35:30 +00002309 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002310
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002311 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2312 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002313 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002314 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002315 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002316 }
2317
Josef Bacik606686e2012-06-04 14:03:51 -04002318 name = rcu_string_strdup(device_path, GFP_NOFS);
2319 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002320 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002321 ret = -ENOMEM;
2322 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002323 }
Josef Bacik606686e2012-06-04 14:03:51 -04002324 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002325
Yan, Zhenga22285a2010-05-16 10:48:46 -04002326 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002327 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002328 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002329 kfree(device);
2330 ret = PTR_ERR(trans);
2331 goto error;
2332 }
2333
Josef Bacikd5e20032011-08-04 14:52:27 +00002334 q = bdev_get_queue(bdev);
2335 if (blk_queue_discard(q))
2336 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002337 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002338 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002339 device->io_width = root->sectorsize;
2340 device->io_align = root->sectorsize;
2341 device->sector_size = root->sectorsize;
2342 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002343 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002344 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002345 device->dev_root = root->fs_info->dev_root;
2346 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002347 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002348 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002349 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002350 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002351 set_blocksize(device->bdev, 4096);
2352
Yan Zheng2b820322008-11-17 21:11:30 -05002353 if (seeding_dev) {
2354 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002355 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002356 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002357 }
2358
2359 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002360
Chris Masone5e9a522009-06-10 15:17:02 -04002361 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002362 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002363 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002364 list_add(&device->dev_alloc_list,
2365 &root->fs_info->fs_devices->alloc_list);
2366 root->fs_info->fs_devices->num_devices++;
2367 root->fs_info->fs_devices->open_devices++;
2368 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002369 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002370 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2371
Josef Bacik2bf64752011-09-26 17:12:22 -04002372 spin_lock(&root->fs_info->free_chunk_lock);
2373 root->fs_info->free_chunk_space += device->total_bytes;
2374 spin_unlock(&root->fs_info->free_chunk_lock);
2375
Chris Masonc2898112009-06-10 09:51:32 -04002376 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2377 root->fs_info->fs_devices->rotating = 1;
2378
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002379 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002380 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002381 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002382
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002383 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002384 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002385 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002386
2387 /* add sysfs device entry */
Anand Jain1ba43812015-03-10 06:38:33 +08002388 btrfs_kobj_add_device(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002389
Miao Xie2196d6e2014-09-03 21:35:41 +08002390 /*
2391 * we've got more storage, clear any full flags on the space
2392 * infos
2393 */
2394 btrfs_clear_space_info_full(root->fs_info);
2395
2396 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002397 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002398
Yan Zheng2b820322008-11-17 21:11:30 -05002399 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002400 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002401 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002402 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002403 if (ret) {
2404 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002405 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002406 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002407 }
2408
2409 ret = btrfs_add_device(trans, root, device);
2410 if (ret) {
2411 btrfs_abort_transaction(trans, root, ret);
2412 goto error_trans;
2413 }
2414
2415 if (seeding_dev) {
2416 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2417
Yan Zheng2b820322008-11-17 21:11:30 -05002418 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002419 if (ret) {
2420 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002421 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002422 }
Anand Jainb2373f22014-06-03 11:36:03 +08002423
2424 /* Sprouting would change fsid of the mounted root,
2425 * so rename the fsid on the sysfs
2426 */
2427 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2428 root->fs_info->fsid);
Anand Jain2e7910d2015-03-10 06:38:29 +08002429 if (kobject_rename(&root->fs_info->fs_devices->super_kobj,
2430 fsid_buf))
Anand Jain2421a8c2015-03-10 06:38:40 +08002431 pr_warn("BTRFS: sysfs: failed to create fsid for sprout\n");
Yan Zheng2b820322008-11-17 21:11:30 -05002432 }
2433
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002434 root->fs_info->num_tolerated_disk_barrier_failures =
2435 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002436 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002437
2438 if (seeding_dev) {
2439 mutex_unlock(&uuid_mutex);
2440 up_write(&sb->s_umount);
2441
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002442 if (ret) /* transaction commit */
2443 return ret;
2444
Yan Zheng2b820322008-11-17 21:11:30 -05002445 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002446 if (ret < 0)
2447 btrfs_error(root->fs_info, ret,
2448 "Failed to relocate sys chunks after "
2449 "device initialization. This can be fixed "
2450 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002451 trans = btrfs_attach_transaction(root);
2452 if (IS_ERR(trans)) {
2453 if (PTR_ERR(trans) == -ENOENT)
2454 return 0;
2455 return PTR_ERR(trans);
2456 }
2457 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002458 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002459
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002460 /* Update ctime/mtime for libblkid */
2461 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002462 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002463
2464error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002465 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002466 rcu_string_free(device->name);
Anand Jain6c14a162015-03-10 06:38:34 +08002467 btrfs_kobj_rm_device(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002468 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002469error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002470 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002471 if (seeding_dev) {
2472 mutex_unlock(&uuid_mutex);
2473 up_write(&sb->s_umount);
2474 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002475 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002476}
2477
Stefan Behrense93c89c2012-11-05 17:33:06 +01002478int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002479 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002480 struct btrfs_device **device_out)
2481{
2482 struct request_queue *q;
2483 struct btrfs_device *device;
2484 struct block_device *bdev;
2485 struct btrfs_fs_info *fs_info = root->fs_info;
2486 struct list_head *devices;
2487 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002488 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002489 int ret = 0;
2490
2491 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002492 if (fs_info->fs_devices->seeding) {
2493 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002494 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002495 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002496
2497 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2498 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002499 if (IS_ERR(bdev)) {
2500 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002501 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002502 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002503
2504 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2505
2506 devices = &fs_info->fs_devices->devices;
2507 list_for_each_entry(device, devices, dev_list) {
2508 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002509 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002510 ret = -EEXIST;
2511 goto error;
2512 }
2513 }
2514
Miao Xie1c433662014-09-03 21:35:32 +08002515
Miao Xie7cc8e582014-09-03 21:35:38 +08002516 if (i_size_read(bdev->bd_inode) <
2517 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002518 btrfs_err(fs_info, "target device is smaller than source device!");
2519 ret = -EINVAL;
2520 goto error;
2521 }
2522
2523
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002524 device = btrfs_alloc_device(NULL, &devid, NULL);
2525 if (IS_ERR(device)) {
2526 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002527 goto error;
2528 }
2529
2530 name = rcu_string_strdup(device_path, GFP_NOFS);
2531 if (!name) {
2532 kfree(device);
2533 ret = -ENOMEM;
2534 goto error;
2535 }
2536 rcu_assign_pointer(device->name, name);
2537
2538 q = bdev_get_queue(bdev);
2539 if (blk_queue_discard(q))
2540 device->can_discard = 1;
2541 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2542 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002543 device->generation = 0;
2544 device->io_width = root->sectorsize;
2545 device->io_align = root->sectorsize;
2546 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002547 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2548 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2549 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002550 ASSERT(list_empty(&srcdev->resized_list));
2551 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002552 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002553 device->dev_root = fs_info->dev_root;
2554 device->bdev = bdev;
2555 device->in_fs_metadata = 1;
2556 device->is_tgtdev_for_dev_replace = 1;
2557 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002558 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002559 set_blocksize(device->bdev, 4096);
2560 device->fs_devices = fs_info->fs_devices;
2561 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2562 fs_info->fs_devices->num_devices++;
2563 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002564 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2565
2566 *device_out = device;
2567 return ret;
2568
2569error:
2570 blkdev_put(bdev, FMODE_EXCL);
2571 return ret;
2572}
2573
2574void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2575 struct btrfs_device *tgtdev)
2576{
2577 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2578 tgtdev->io_width = fs_info->dev_root->sectorsize;
2579 tgtdev->io_align = fs_info->dev_root->sectorsize;
2580 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2581 tgtdev->dev_root = fs_info->dev_root;
2582 tgtdev->in_fs_metadata = 1;
2583}
2584
Chris Masond3977122009-01-05 21:25:51 -05002585static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2586 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002587{
2588 int ret;
2589 struct btrfs_path *path;
2590 struct btrfs_root *root;
2591 struct btrfs_dev_item *dev_item;
2592 struct extent_buffer *leaf;
2593 struct btrfs_key key;
2594
2595 root = device->dev_root->fs_info->chunk_root;
2596
2597 path = btrfs_alloc_path();
2598 if (!path)
2599 return -ENOMEM;
2600
2601 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2602 key.type = BTRFS_DEV_ITEM_KEY;
2603 key.offset = device->devid;
2604
2605 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2606 if (ret < 0)
2607 goto out;
2608
2609 if (ret > 0) {
2610 ret = -ENOENT;
2611 goto out;
2612 }
2613
2614 leaf = path->nodes[0];
2615 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2616
2617 btrfs_set_device_id(leaf, dev_item, device->devid);
2618 btrfs_set_device_type(leaf, dev_item, device->type);
2619 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2620 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2621 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002622 btrfs_set_device_total_bytes(leaf, dev_item,
2623 btrfs_device_get_disk_total_bytes(device));
2624 btrfs_set_device_bytes_used(leaf, dev_item,
2625 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002626 btrfs_mark_buffer_dirty(leaf);
2627
2628out:
2629 btrfs_free_path(path);
2630 return ret;
2631}
2632
Miao Xie2196d6e2014-09-03 21:35:41 +08002633int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002634 struct btrfs_device *device, u64 new_size)
2635{
2636 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002637 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002638 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002639 u64 old_total;
2640 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002641
Yan Zheng2b820322008-11-17 21:11:30 -05002642 if (!device->writeable)
2643 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002644
2645 lock_chunks(device->dev_root);
2646 old_total = btrfs_super_total_bytes(super_copy);
2647 diff = new_size - device->total_bytes;
2648
Stefan Behrens63a212a2012-11-05 18:29:28 +01002649 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002650 device->is_tgtdev_for_dev_replace) {
2651 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002652 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002653 }
Yan Zheng2b820322008-11-17 21:11:30 -05002654
Miao Xie935e5cc2014-09-03 21:35:33 +08002655 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002656
2657 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002658 device->fs_devices->total_rw_bytes += diff;
2659
Miao Xie7cc8e582014-09-03 21:35:38 +08002660 btrfs_device_set_total_bytes(device, new_size);
2661 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002662 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002663 if (list_empty(&device->resized_list))
2664 list_add_tail(&device->resized_list,
2665 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002666 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002667
Chris Mason8f18cf12008-04-25 16:53:30 -04002668 return btrfs_update_device(trans, device);
2669}
2670
2671static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002672 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002673 u64 chunk_offset)
2674{
2675 int ret;
2676 struct btrfs_path *path;
2677 struct btrfs_key key;
2678
2679 root = root->fs_info->chunk_root;
2680 path = btrfs_alloc_path();
2681 if (!path)
2682 return -ENOMEM;
2683
2684 key.objectid = chunk_objectid;
2685 key.offset = chunk_offset;
2686 key.type = BTRFS_CHUNK_ITEM_KEY;
2687
2688 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 if (ret < 0)
2690 goto out;
2691 else if (ret > 0) { /* Logic error or corruption */
2692 btrfs_error(root->fs_info, -ENOENT,
2693 "Failed lookup while freeing chunk.");
2694 ret = -ENOENT;
2695 goto out;
2696 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002697
2698 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002699 if (ret < 0)
2700 btrfs_error(root->fs_info, ret,
2701 "Failed to delete chunk item.");
2702out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002703 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002704 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002705}
2706
Christoph Hellwigb2950862008-12-02 09:54:17 -05002707static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002708 chunk_offset)
2709{
David Sterba6c417612011-04-13 15:41:04 +02002710 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002711 struct btrfs_disk_key *disk_key;
2712 struct btrfs_chunk *chunk;
2713 u8 *ptr;
2714 int ret = 0;
2715 u32 num_stripes;
2716 u32 array_size;
2717 u32 len = 0;
2718 u32 cur;
2719 struct btrfs_key key;
2720
Miao Xie2196d6e2014-09-03 21:35:41 +08002721 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002722 array_size = btrfs_super_sys_array_size(super_copy);
2723
2724 ptr = super_copy->sys_chunk_array;
2725 cur = 0;
2726
2727 while (cur < array_size) {
2728 disk_key = (struct btrfs_disk_key *)ptr;
2729 btrfs_disk_key_to_cpu(&key, disk_key);
2730
2731 len = sizeof(*disk_key);
2732
2733 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2734 chunk = (struct btrfs_chunk *)(ptr + len);
2735 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2736 len += btrfs_chunk_item_size(num_stripes);
2737 } else {
2738 ret = -EIO;
2739 break;
2740 }
2741 if (key.objectid == chunk_objectid &&
2742 key.offset == chunk_offset) {
2743 memmove(ptr, ptr + len, array_size - (cur + len));
2744 array_size -= len;
2745 btrfs_set_super_sys_array_size(super_copy, array_size);
2746 } else {
2747 ptr += len;
2748 cur += len;
2749 }
2750 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002751 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002752 return ret;
2753}
2754
Josef Bacik47ab2a62014-09-18 11:20:02 -04002755int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2756 struct btrfs_root *root, u64 chunk_offset)
2757{
2758 struct extent_map_tree *em_tree;
2759 struct extent_map *em;
2760 struct btrfs_root *extent_root = root->fs_info->extent_root;
2761 struct map_lookup *map;
2762 u64 dev_extent_len = 0;
2763 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002764 int i, ret = 0;
2765
2766 /* Just in case */
2767 root = root->fs_info->chunk_root;
2768 em_tree = &root->fs_info->mapping_tree.map_tree;
2769
2770 read_lock(&em_tree->lock);
2771 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2772 read_unlock(&em_tree->lock);
2773
2774 if (!em || em->start > chunk_offset ||
2775 em->start + em->len < chunk_offset) {
2776 /*
2777 * This is a logic error, but we don't want to just rely on the
2778 * user having built with ASSERT enabled, so if ASSERT doens't
2779 * do anything we still error out.
2780 */
2781 ASSERT(0);
2782 if (em)
2783 free_extent_map(em);
2784 return -EINVAL;
2785 }
2786 map = (struct map_lookup *)em->bdev;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002787 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002788 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002789 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002790
2791 for (i = 0; i < map->num_stripes; i++) {
2792 struct btrfs_device *device = map->stripes[i].dev;
2793 ret = btrfs_free_dev_extent(trans, device,
2794 map->stripes[i].physical,
2795 &dev_extent_len);
2796 if (ret) {
2797 btrfs_abort_transaction(trans, root, ret);
2798 goto out;
2799 }
2800
2801 if (device->bytes_used > 0) {
2802 lock_chunks(root);
2803 btrfs_device_set_bytes_used(device,
2804 device->bytes_used - dev_extent_len);
2805 spin_lock(&root->fs_info->free_chunk_lock);
2806 root->fs_info->free_chunk_space += dev_extent_len;
2807 spin_unlock(&root->fs_info->free_chunk_lock);
2808 btrfs_clear_space_info_full(root->fs_info);
2809 unlock_chunks(root);
2810 }
2811
2812 if (map->stripes[i].dev) {
2813 ret = btrfs_update_device(trans, map->stripes[i].dev);
2814 if (ret) {
2815 btrfs_abort_transaction(trans, root, ret);
2816 goto out;
2817 }
2818 }
2819 }
Zhao Leia688a042015-02-09 20:31:44 +08002820 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002821 if (ret) {
2822 btrfs_abort_transaction(trans, root, ret);
2823 goto out;
2824 }
2825
2826 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2827
2828 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2829 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2830 if (ret) {
2831 btrfs_abort_transaction(trans, root, ret);
2832 goto out;
2833 }
2834 }
2835
Filipe Manana04216822014-11-27 21:14:15 +00002836 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002837 if (ret) {
2838 btrfs_abort_transaction(trans, extent_root, ret);
2839 goto out;
2840 }
2841
Josef Bacik47ab2a62014-09-18 11:20:02 -04002842out:
2843 /* once for us */
2844 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002845 return ret;
2846}
2847
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002848static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002849{
Chris Mason8f18cf12008-04-25 16:53:30 -04002850 struct btrfs_root *extent_root;
2851 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002852 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002853
2854 root = root->fs_info->chunk_root;
2855 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002856
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002857 /*
2858 * Prevent races with automatic removal of unused block groups.
2859 * After we relocate and before we remove the chunk with offset
2860 * chunk_offset, automatic removal of the block group can kick in,
2861 * resulting in a failure when calling btrfs_remove_chunk() below.
2862 *
2863 * Make sure to acquire this mutex before doing a tree search (dev
2864 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2865 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2866 * we release the path used to search the chunk/dev tree and before
2867 * the current task acquires this mutex and calls us.
2868 */
2869 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2870
Josef Bacikba1bf482009-09-11 16:11:19 -04002871 ret = btrfs_can_relocate(extent_root, chunk_offset);
2872 if (ret)
2873 return -ENOSPC;
2874
Chris Mason8f18cf12008-04-25 16:53:30 -04002875 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002876 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002877 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002878 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002879 if (ret)
2880 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002881
Yan, Zhenga22285a2010-05-16 10:48:46 -04002882 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002883 if (IS_ERR(trans)) {
2884 ret = PTR_ERR(trans);
2885 btrfs_std_error(root->fs_info, ret);
2886 return ret;
2887 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002888
2889 /*
2890 * step two, delete the device extents and the
2891 * chunk tree entries
2892 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002893 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002894 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002895 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002896}
2897
Yan Zheng2b820322008-11-17 21:11:30 -05002898static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2899{
2900 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2901 struct btrfs_path *path;
2902 struct extent_buffer *leaf;
2903 struct btrfs_chunk *chunk;
2904 struct btrfs_key key;
2905 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002906 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002907 bool retried = false;
2908 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002909 int ret;
2910
2911 path = btrfs_alloc_path();
2912 if (!path)
2913 return -ENOMEM;
2914
Josef Bacikba1bf482009-09-11 16:11:19 -04002915again:
Yan Zheng2b820322008-11-17 21:11:30 -05002916 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2917 key.offset = (u64)-1;
2918 key.type = BTRFS_CHUNK_ITEM_KEY;
2919
2920 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002921 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002922 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002923 if (ret < 0) {
2924 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002925 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002926 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002927 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002928
2929 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2930 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002931 if (ret)
2932 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002933 if (ret < 0)
2934 goto error;
2935 if (ret > 0)
2936 break;
2937
2938 leaf = path->nodes[0];
2939 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2940
2941 chunk = btrfs_item_ptr(leaf, path->slots[0],
2942 struct btrfs_chunk);
2943 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002944 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002945
2946 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002947 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002948 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002949 if (ret == -ENOSPC)
2950 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302951 else
2952 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002953 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002954 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002955
2956 if (found_key.offset == 0)
2957 break;
2958 key.offset = found_key.offset - 1;
2959 }
2960 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002961 if (failed && !retried) {
2962 failed = 0;
2963 retried = true;
2964 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302965 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002966 ret = -ENOSPC;
2967 }
Yan Zheng2b820322008-11-17 21:11:30 -05002968error:
2969 btrfs_free_path(path);
2970 return ret;
2971}
2972
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002973static int insert_balance_item(struct btrfs_root *root,
2974 struct btrfs_balance_control *bctl)
2975{
2976 struct btrfs_trans_handle *trans;
2977 struct btrfs_balance_item *item;
2978 struct btrfs_disk_balance_args disk_bargs;
2979 struct btrfs_path *path;
2980 struct extent_buffer *leaf;
2981 struct btrfs_key key;
2982 int ret, err;
2983
2984 path = btrfs_alloc_path();
2985 if (!path)
2986 return -ENOMEM;
2987
2988 trans = btrfs_start_transaction(root, 0);
2989 if (IS_ERR(trans)) {
2990 btrfs_free_path(path);
2991 return PTR_ERR(trans);
2992 }
2993
2994 key.objectid = BTRFS_BALANCE_OBJECTID;
2995 key.type = BTRFS_BALANCE_ITEM_KEY;
2996 key.offset = 0;
2997
2998 ret = btrfs_insert_empty_item(trans, root, path, &key,
2999 sizeof(*item));
3000 if (ret)
3001 goto out;
3002
3003 leaf = path->nodes[0];
3004 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3005
3006 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
3007
3008 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
3009 btrfs_set_balance_data(leaf, item, &disk_bargs);
3010 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
3011 btrfs_set_balance_meta(leaf, item, &disk_bargs);
3012 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
3013 btrfs_set_balance_sys(leaf, item, &disk_bargs);
3014
3015 btrfs_set_balance_flags(leaf, item, bctl->flags);
3016
3017 btrfs_mark_buffer_dirty(leaf);
3018out:
3019 btrfs_free_path(path);
3020 err = btrfs_commit_transaction(trans, root);
3021 if (err && !ret)
3022 ret = err;
3023 return ret;
3024}
3025
3026static int del_balance_item(struct btrfs_root *root)
3027{
3028 struct btrfs_trans_handle *trans;
3029 struct btrfs_path *path;
3030 struct btrfs_key key;
3031 int ret, err;
3032
3033 path = btrfs_alloc_path();
3034 if (!path)
3035 return -ENOMEM;
3036
3037 trans = btrfs_start_transaction(root, 0);
3038 if (IS_ERR(trans)) {
3039 btrfs_free_path(path);
3040 return PTR_ERR(trans);
3041 }
3042
3043 key.objectid = BTRFS_BALANCE_OBJECTID;
3044 key.type = BTRFS_BALANCE_ITEM_KEY;
3045 key.offset = 0;
3046
3047 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3048 if (ret < 0)
3049 goto out;
3050 if (ret > 0) {
3051 ret = -ENOENT;
3052 goto out;
3053 }
3054
3055 ret = btrfs_del_item(trans, root, path);
3056out:
3057 btrfs_free_path(path);
3058 err = btrfs_commit_transaction(trans, root);
3059 if (err && !ret)
3060 ret = err;
3061 return ret;
3062}
3063
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003064/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003065 * This is a heuristic used to reduce the number of chunks balanced on
3066 * resume after balance was interrupted.
3067 */
3068static void update_balance_args(struct btrfs_balance_control *bctl)
3069{
3070 /*
3071 * Turn on soft mode for chunk types that were being converted.
3072 */
3073 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3074 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3075 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3076 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3077 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3078 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3079
3080 /*
3081 * Turn on usage filter if is not already used. The idea is
3082 * that chunks that we have already balanced should be
3083 * reasonably full. Don't do it for chunks that are being
3084 * converted - that will keep us from relocating unconverted
3085 * (albeit full) chunks.
3086 */
3087 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3088 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3089 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3090 bctl->data.usage = 90;
3091 }
3092 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3093 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3094 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3095 bctl->sys.usage = 90;
3096 }
3097 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
3098 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3099 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3100 bctl->meta.usage = 90;
3101 }
3102}
3103
3104/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003105 * Should be called with both balance and volume mutexes held to
3106 * serialize other volume operations (add_dev/rm_dev/resize) with
3107 * restriper. Same goes for unset_balance_control.
3108 */
3109static void set_balance_control(struct btrfs_balance_control *bctl)
3110{
3111 struct btrfs_fs_info *fs_info = bctl->fs_info;
3112
3113 BUG_ON(fs_info->balance_ctl);
3114
3115 spin_lock(&fs_info->balance_lock);
3116 fs_info->balance_ctl = bctl;
3117 spin_unlock(&fs_info->balance_lock);
3118}
3119
3120static void unset_balance_control(struct btrfs_fs_info *fs_info)
3121{
3122 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3123
3124 BUG_ON(!fs_info->balance_ctl);
3125
3126 spin_lock(&fs_info->balance_lock);
3127 fs_info->balance_ctl = NULL;
3128 spin_unlock(&fs_info->balance_lock);
3129
3130 kfree(bctl);
3131}
3132
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003133/*
3134 * Balance filters. Return 1 if chunk should be filtered out
3135 * (should not be balanced).
3136 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003137static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003138 struct btrfs_balance_args *bargs)
3139{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003140 chunk_type = chunk_to_extended(chunk_type) &
3141 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003142
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003143 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003144 return 0;
3145
3146 return 1;
3147}
3148
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003149static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3150 struct btrfs_balance_args *bargs)
3151{
3152 struct btrfs_block_group_cache *cache;
3153 u64 chunk_used, user_thresh;
3154 int ret = 1;
3155
3156 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3157 chunk_used = btrfs_block_group_used(&cache->item);
3158
Ilya Dryomova105bb82013-01-21 15:15:56 +02003159 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003160 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003161 else if (bargs->usage > 100)
3162 user_thresh = cache->key.offset;
3163 else
3164 user_thresh = div_factor_fine(cache->key.offset,
3165 bargs->usage);
3166
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003167 if (chunk_used < user_thresh)
3168 ret = 0;
3169
3170 btrfs_put_block_group(cache);
3171 return ret;
3172}
3173
Ilya Dryomov409d4042012-01-16 22:04:47 +02003174static int chunk_devid_filter(struct extent_buffer *leaf,
3175 struct btrfs_chunk *chunk,
3176 struct btrfs_balance_args *bargs)
3177{
3178 struct btrfs_stripe *stripe;
3179 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3180 int i;
3181
3182 for (i = 0; i < num_stripes; i++) {
3183 stripe = btrfs_stripe_nr(chunk, i);
3184 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3185 return 0;
3186 }
3187
3188 return 1;
3189}
3190
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003191/* [pstart, pend) */
3192static int chunk_drange_filter(struct extent_buffer *leaf,
3193 struct btrfs_chunk *chunk,
3194 u64 chunk_offset,
3195 struct btrfs_balance_args *bargs)
3196{
3197 struct btrfs_stripe *stripe;
3198 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3199 u64 stripe_offset;
3200 u64 stripe_length;
3201 int factor;
3202 int i;
3203
3204 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3205 return 0;
3206
3207 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003208 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3209 factor = num_stripes / 2;
3210 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3211 factor = num_stripes - 1;
3212 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3213 factor = num_stripes - 2;
3214 } else {
3215 factor = num_stripes;
3216 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003217
3218 for (i = 0; i < num_stripes; i++) {
3219 stripe = btrfs_stripe_nr(chunk, i);
3220 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3221 continue;
3222
3223 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3224 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003225 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003226
3227 if (stripe_offset < bargs->pend &&
3228 stripe_offset + stripe_length > bargs->pstart)
3229 return 0;
3230 }
3231
3232 return 1;
3233}
3234
Ilya Dryomovea671762012-01-16 22:04:48 +02003235/* [vstart, vend) */
3236static int chunk_vrange_filter(struct extent_buffer *leaf,
3237 struct btrfs_chunk *chunk,
3238 u64 chunk_offset,
3239 struct btrfs_balance_args *bargs)
3240{
3241 if (chunk_offset < bargs->vend &&
3242 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3243 /* at least part of the chunk is inside this vrange */
3244 return 0;
3245
3246 return 1;
3247}
3248
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003249static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003250 struct btrfs_balance_args *bargs)
3251{
3252 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3253 return 0;
3254
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003255 chunk_type = chunk_to_extended(chunk_type) &
3256 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003257
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003258 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003259 return 1;
3260
3261 return 0;
3262}
3263
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003264static int should_balance_chunk(struct btrfs_root *root,
3265 struct extent_buffer *leaf,
3266 struct btrfs_chunk *chunk, u64 chunk_offset)
3267{
3268 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3269 struct btrfs_balance_args *bargs = NULL;
3270 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3271
3272 /* type filter */
3273 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3274 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3275 return 0;
3276 }
3277
3278 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3279 bargs = &bctl->data;
3280 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3281 bargs = &bctl->sys;
3282 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3283 bargs = &bctl->meta;
3284
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003285 /* profiles filter */
3286 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3287 chunk_profiles_filter(chunk_type, bargs)) {
3288 return 0;
3289 }
3290
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003291 /* usage filter */
3292 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3293 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3294 return 0;
3295 }
3296
Ilya Dryomov409d4042012-01-16 22:04:47 +02003297 /* devid filter */
3298 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3299 chunk_devid_filter(leaf, chunk, bargs)) {
3300 return 0;
3301 }
3302
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003303 /* drange filter, makes sense only with devid filter */
3304 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3305 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3306 return 0;
3307 }
3308
Ilya Dryomovea671762012-01-16 22:04:48 +02003309 /* vrange filter */
3310 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3311 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3312 return 0;
3313 }
3314
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003315 /* soft profile changing mode */
3316 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3317 chunk_soft_convert_filter(chunk_type, bargs)) {
3318 return 0;
3319 }
3320
David Sterba7d824b62014-05-07 17:37:51 +02003321 /*
3322 * limited by count, must be the last filter
3323 */
3324 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3325 if (bargs->limit == 0)
3326 return 0;
3327 else
3328 bargs->limit--;
3329 }
3330
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003331 return 1;
3332}
3333
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003334static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003335{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003336 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003337 struct btrfs_root *chunk_root = fs_info->chunk_root;
3338 struct btrfs_root *dev_root = fs_info->dev_root;
3339 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003340 struct btrfs_device *device;
3341 u64 old_size;
3342 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003343 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003344 struct btrfs_path *path;
3345 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003346 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003347 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003348 struct extent_buffer *leaf;
3349 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003350 int ret;
3351 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003352 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003353 u64 limit_data = bctl->data.limit;
3354 u64 limit_meta = bctl->meta.limit;
3355 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003356
Chris Masonec44a352008-04-28 15:29:52 -04003357 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003358 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003359 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003360 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003361 size_to_free = div_factor(old_size, 1);
3362 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003363 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003364 btrfs_device_get_total_bytes(device) -
3365 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003366 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003367 continue;
3368
3369 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003370 if (ret == -ENOSPC)
3371 break;
Chris Masonec44a352008-04-28 15:29:52 -04003372 BUG_ON(ret);
3373
Yan, Zhenga22285a2010-05-16 10:48:46 -04003374 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003375 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003376
3377 ret = btrfs_grow_device(trans, device, old_size);
3378 BUG_ON(ret);
3379
3380 btrfs_end_transaction(trans, dev_root);
3381 }
3382
3383 /* step two, relocate all the chunks */
3384 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003385 if (!path) {
3386 ret = -ENOMEM;
3387 goto error;
3388 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003389
3390 /* zero out stat counters */
3391 spin_lock(&fs_info->balance_lock);
3392 memset(&bctl->stat, 0, sizeof(bctl->stat));
3393 spin_unlock(&fs_info->balance_lock);
3394again:
David Sterba7d824b62014-05-07 17:37:51 +02003395 if (!counting) {
3396 bctl->data.limit = limit_data;
3397 bctl->meta.limit = limit_meta;
3398 bctl->sys.limit = limit_sys;
3399 }
Chris Masonec44a352008-04-28 15:29:52 -04003400 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3401 key.offset = (u64)-1;
3402 key.type = BTRFS_CHUNK_ITEM_KEY;
3403
Chris Masond3977122009-01-05 21:25:51 -05003404 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003405 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003406 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003407 ret = -ECANCELED;
3408 goto error;
3409 }
3410
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003411 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003412 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003413 if (ret < 0) {
3414 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003415 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003416 }
Chris Masonec44a352008-04-28 15:29:52 -04003417
3418 /*
3419 * this shouldn't happen, it means the last relocate
3420 * failed
3421 */
3422 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003423 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003424
3425 ret = btrfs_previous_item(chunk_root, path, 0,
3426 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003427 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003428 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003429 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003430 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003431 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003432
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003433 leaf = path->nodes[0];
3434 slot = path->slots[0];
3435 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3436
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003437 if (found_key.objectid != key.objectid) {
3438 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003439 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003440 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003441
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003442 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3443
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003444 if (!counting) {
3445 spin_lock(&fs_info->balance_lock);
3446 bctl->stat.considered++;
3447 spin_unlock(&fs_info->balance_lock);
3448 }
3449
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003450 ret = should_balance_chunk(chunk_root, leaf, chunk,
3451 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003452 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003453 if (!ret) {
3454 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003455 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003456 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003457
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003458 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003459 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003460 spin_lock(&fs_info->balance_lock);
3461 bctl->stat.expected++;
3462 spin_unlock(&fs_info->balance_lock);
3463 goto loop;
3464 }
3465
Chris Masonec44a352008-04-28 15:29:52 -04003466 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003467 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003468 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003469 if (ret && ret != -ENOSPC)
3470 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003471 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003472 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003473 } else {
3474 spin_lock(&fs_info->balance_lock);
3475 bctl->stat.completed++;
3476 spin_unlock(&fs_info->balance_lock);
3477 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003478loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003479 if (found_key.offset == 0)
3480 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003481 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003482 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003483
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003484 if (counting) {
3485 btrfs_release_path(path);
3486 counting = false;
3487 goto again;
3488 }
Chris Masonec44a352008-04-28 15:29:52 -04003489error:
3490 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003491 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003492 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003493 enospc_errors);
3494 if (!ret)
3495 ret = -ENOSPC;
3496 }
3497
Chris Masonec44a352008-04-28 15:29:52 -04003498 return ret;
3499}
3500
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003501/**
3502 * alloc_profile_is_valid - see if a given profile is valid and reduced
3503 * @flags: profile to validate
3504 * @extended: if true @flags is treated as an extended profile
3505 */
3506static int alloc_profile_is_valid(u64 flags, int extended)
3507{
3508 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3509 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3510
3511 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3512
3513 /* 1) check that all other bits are zeroed */
3514 if (flags & ~mask)
3515 return 0;
3516
3517 /* 2) see if profile is reduced */
3518 if (flags == 0)
3519 return !extended; /* "0" is valid for usual profiles */
3520
3521 /* true if exactly one bit set */
3522 return (flags & (flags - 1)) == 0;
3523}
3524
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003525static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3526{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003527 /* cancel requested || normal exit path */
3528 return atomic_read(&fs_info->balance_cancel_req) ||
3529 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3530 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003531}
3532
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003533static void __cancel_balance(struct btrfs_fs_info *fs_info)
3534{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003535 int ret;
3536
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003537 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003538 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003539 if (ret)
3540 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003541
3542 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003543}
3544
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003545/* Non-zero return value signifies invalidity */
3546static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3547 u64 allowed)
3548{
3549 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3550 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3551 (bctl_arg->target & ~allowed)));
3552}
3553
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003554/*
3555 * Should be called with both balance and volume mutexes held
3556 */
3557int btrfs_balance(struct btrfs_balance_control *bctl,
3558 struct btrfs_ioctl_balance_args *bargs)
3559{
3560 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003561 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003562 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003563 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003564 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003565 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003566
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003567 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003568 atomic_read(&fs_info->balance_pause_req) ||
3569 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003570 ret = -EINVAL;
3571 goto out;
3572 }
3573
Ilya Dryomove4837f82012-03-27 17:09:17 +03003574 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3575 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3576 mixed = 1;
3577
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003578 /*
3579 * In case of mixed groups both data and meta should be picked,
3580 * and identical options should be given for both of them.
3581 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003582 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3583 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003584 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3585 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3586 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003587 btrfs_err(fs_info, "with mixed groups data and "
3588 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003589 ret = -EINVAL;
3590 goto out;
3591 }
3592 }
3593
Stefan Behrens8dabb742012-11-06 13:15:27 +01003594 num_devices = fs_info->fs_devices->num_devices;
3595 btrfs_dev_replace_lock(&fs_info->dev_replace);
3596 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3597 BUG_ON(num_devices < 1);
3598 num_devices--;
3599 }
3600 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003601 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003602 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003603 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003604 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003605 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003606 if (num_devices > 2)
3607 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3608 if (num_devices > 3)
3609 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3610 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003611 if (validate_convert_profile(&bctl->data, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003612 btrfs_err(fs_info, "unable to start balance with target "
3613 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003614 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003615 ret = -EINVAL;
3616 goto out;
3617 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003618 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003619 btrfs_err(fs_info,
3620 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003621 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003622 ret = -EINVAL;
3623 goto out;
3624 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003625 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003626 btrfs_err(fs_info,
3627 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003628 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003629 ret = -EINVAL;
3630 goto out;
3631 }
3632
Ilya Dryomove4837f82012-03-27 17:09:17 +03003633 /* allow dup'ed data chunks only in mixed mode */
3634 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003635 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003636 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003637 ret = -EINVAL;
3638 goto out;
3639 }
3640
3641 /* allow to reduce meta or sys integrity only if force set */
3642 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003643 BTRFS_BLOCK_GROUP_RAID10 |
3644 BTRFS_BLOCK_GROUP_RAID5 |
3645 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003646 do {
3647 seq = read_seqbegin(&fs_info->profiles_lock);
3648
3649 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3650 (fs_info->avail_system_alloc_bits & allowed) &&
3651 !(bctl->sys.target & allowed)) ||
3652 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3653 (fs_info->avail_metadata_alloc_bits & allowed) &&
3654 !(bctl->meta.target & allowed))) {
3655 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003656 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003657 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003658 btrfs_err(fs_info, "balance will reduce metadata "
3659 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003660 ret = -EINVAL;
3661 goto out;
3662 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003663 }
Miao Xiede98ced2013-01-29 10:13:12 +00003664 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003665
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003666 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003667 fs_info->num_tolerated_disk_barrier_failures = min(
3668 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3669 btrfs_get_num_tolerated_disk_barrier_failures(
3670 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003671 }
3672
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003673 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003674 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003675 goto out;
3676
Ilya Dryomov59641012012-01-16 22:04:48 +02003677 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3678 BUG_ON(ret == -EEXIST);
3679 set_balance_control(bctl);
3680 } else {
3681 BUG_ON(ret != -EEXIST);
3682 spin_lock(&fs_info->balance_lock);
3683 update_balance_args(bctl);
3684 spin_unlock(&fs_info->balance_lock);
3685 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003686
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003687 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003688 mutex_unlock(&fs_info->balance_mutex);
3689
3690 ret = __btrfs_balance(fs_info);
3691
3692 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003693 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003694
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003695 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3696 fs_info->num_tolerated_disk_barrier_failures =
3697 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3698 }
3699
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003700 if (bargs) {
3701 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003702 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003703 }
3704
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003705 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3706 balance_need_close(fs_info)) {
3707 __cancel_balance(fs_info);
3708 }
3709
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003710 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003711
3712 return ret;
3713out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003714 if (bctl->flags & BTRFS_BALANCE_RESUME)
3715 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003716 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003717 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003718 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3719 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003720 return ret;
3721}
3722
3723static int balance_kthread(void *data)
3724{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003725 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003726 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003727
3728 mutex_lock(&fs_info->volume_mutex);
3729 mutex_lock(&fs_info->balance_mutex);
3730
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003731 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003732 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003733 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003734 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003735
3736 mutex_unlock(&fs_info->balance_mutex);
3737 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003738
Ilya Dryomov59641012012-01-16 22:04:48 +02003739 return ret;
3740}
3741
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003742int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3743{
3744 struct task_struct *tsk;
3745
3746 spin_lock(&fs_info->balance_lock);
3747 if (!fs_info->balance_ctl) {
3748 spin_unlock(&fs_info->balance_lock);
3749 return 0;
3750 }
3751 spin_unlock(&fs_info->balance_lock);
3752
3753 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003754 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003755 return 0;
3756 }
3757
3758 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303759 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003760}
3761
Ilya Dryomov68310a52012-06-22 12:24:12 -06003762int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003763{
Ilya Dryomov59641012012-01-16 22:04:48 +02003764 struct btrfs_balance_control *bctl;
3765 struct btrfs_balance_item *item;
3766 struct btrfs_disk_balance_args disk_bargs;
3767 struct btrfs_path *path;
3768 struct extent_buffer *leaf;
3769 struct btrfs_key key;
3770 int ret;
3771
3772 path = btrfs_alloc_path();
3773 if (!path)
3774 return -ENOMEM;
3775
Ilya Dryomov68310a52012-06-22 12:24:12 -06003776 key.objectid = BTRFS_BALANCE_OBJECTID;
3777 key.type = BTRFS_BALANCE_ITEM_KEY;
3778 key.offset = 0;
3779
3780 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3781 if (ret < 0)
3782 goto out;
3783 if (ret > 0) { /* ret = -ENOENT; */
3784 ret = 0;
3785 goto out;
3786 }
3787
Ilya Dryomov59641012012-01-16 22:04:48 +02003788 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3789 if (!bctl) {
3790 ret = -ENOMEM;
3791 goto out;
3792 }
3793
Ilya Dryomov59641012012-01-16 22:04:48 +02003794 leaf = path->nodes[0];
3795 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3796
Ilya Dryomov68310a52012-06-22 12:24:12 -06003797 bctl->fs_info = fs_info;
3798 bctl->flags = btrfs_balance_flags(leaf, item);
3799 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003800
3801 btrfs_balance_data(leaf, item, &disk_bargs);
3802 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3803 btrfs_balance_meta(leaf, item, &disk_bargs);
3804 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3805 btrfs_balance_sys(leaf, item, &disk_bargs);
3806 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3807
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003808 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3809
Ilya Dryomov68310a52012-06-22 12:24:12 -06003810 mutex_lock(&fs_info->volume_mutex);
3811 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003812
Ilya Dryomov68310a52012-06-22 12:24:12 -06003813 set_balance_control(bctl);
3814
3815 mutex_unlock(&fs_info->balance_mutex);
3816 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003817out:
3818 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003819 return ret;
3820}
3821
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003822int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3823{
3824 int ret = 0;
3825
3826 mutex_lock(&fs_info->balance_mutex);
3827 if (!fs_info->balance_ctl) {
3828 mutex_unlock(&fs_info->balance_mutex);
3829 return -ENOTCONN;
3830 }
3831
3832 if (atomic_read(&fs_info->balance_running)) {
3833 atomic_inc(&fs_info->balance_pause_req);
3834 mutex_unlock(&fs_info->balance_mutex);
3835
3836 wait_event(fs_info->balance_wait_q,
3837 atomic_read(&fs_info->balance_running) == 0);
3838
3839 mutex_lock(&fs_info->balance_mutex);
3840 /* we are good with balance_ctl ripped off from under us */
3841 BUG_ON(atomic_read(&fs_info->balance_running));
3842 atomic_dec(&fs_info->balance_pause_req);
3843 } else {
3844 ret = -ENOTCONN;
3845 }
3846
3847 mutex_unlock(&fs_info->balance_mutex);
3848 return ret;
3849}
3850
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003851int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3852{
Ilya Dryomove649e582013-10-10 20:40:21 +03003853 if (fs_info->sb->s_flags & MS_RDONLY)
3854 return -EROFS;
3855
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003856 mutex_lock(&fs_info->balance_mutex);
3857 if (!fs_info->balance_ctl) {
3858 mutex_unlock(&fs_info->balance_mutex);
3859 return -ENOTCONN;
3860 }
3861
3862 atomic_inc(&fs_info->balance_cancel_req);
3863 /*
3864 * if we are running just wait and return, balance item is
3865 * deleted in btrfs_balance in this case
3866 */
3867 if (atomic_read(&fs_info->balance_running)) {
3868 mutex_unlock(&fs_info->balance_mutex);
3869 wait_event(fs_info->balance_wait_q,
3870 atomic_read(&fs_info->balance_running) == 0);
3871 mutex_lock(&fs_info->balance_mutex);
3872 } else {
3873 /* __cancel_balance needs volume_mutex */
3874 mutex_unlock(&fs_info->balance_mutex);
3875 mutex_lock(&fs_info->volume_mutex);
3876 mutex_lock(&fs_info->balance_mutex);
3877
3878 if (fs_info->balance_ctl)
3879 __cancel_balance(fs_info);
3880
3881 mutex_unlock(&fs_info->volume_mutex);
3882 }
3883
3884 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3885 atomic_dec(&fs_info->balance_cancel_req);
3886 mutex_unlock(&fs_info->balance_mutex);
3887 return 0;
3888}
3889
Stefan Behrens803b2f52013-08-15 17:11:21 +02003890static int btrfs_uuid_scan_kthread(void *data)
3891{
3892 struct btrfs_fs_info *fs_info = data;
3893 struct btrfs_root *root = fs_info->tree_root;
3894 struct btrfs_key key;
3895 struct btrfs_key max_key;
3896 struct btrfs_path *path = NULL;
3897 int ret = 0;
3898 struct extent_buffer *eb;
3899 int slot;
3900 struct btrfs_root_item root_item;
3901 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003902 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003903
3904 path = btrfs_alloc_path();
3905 if (!path) {
3906 ret = -ENOMEM;
3907 goto out;
3908 }
3909
3910 key.objectid = 0;
3911 key.type = BTRFS_ROOT_ITEM_KEY;
3912 key.offset = 0;
3913
3914 max_key.objectid = (u64)-1;
3915 max_key.type = BTRFS_ROOT_ITEM_KEY;
3916 max_key.offset = (u64)-1;
3917
Stefan Behrens803b2f52013-08-15 17:11:21 +02003918 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003919 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003920 if (ret) {
3921 if (ret > 0)
3922 ret = 0;
3923 break;
3924 }
3925
3926 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3927 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3928 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3929 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3930 goto skip;
3931
3932 eb = path->nodes[0];
3933 slot = path->slots[0];
3934 item_size = btrfs_item_size_nr(eb, slot);
3935 if (item_size < sizeof(root_item))
3936 goto skip;
3937
Stefan Behrens803b2f52013-08-15 17:11:21 +02003938 read_extent_buffer(eb, &root_item,
3939 btrfs_item_ptr_offset(eb, slot),
3940 (int)sizeof(root_item));
3941 if (btrfs_root_refs(&root_item) == 0)
3942 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003943
3944 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3945 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3946 if (trans)
3947 goto update_tree;
3948
3949 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003950 /*
3951 * 1 - subvol uuid item
3952 * 1 - received_subvol uuid item
3953 */
3954 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3955 if (IS_ERR(trans)) {
3956 ret = PTR_ERR(trans);
3957 break;
3958 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003959 continue;
3960 } else {
3961 goto skip;
3962 }
3963update_tree:
3964 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003965 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3966 root_item.uuid,
3967 BTRFS_UUID_KEY_SUBVOL,
3968 key.objectid);
3969 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003970 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003971 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003972 break;
3973 }
3974 }
3975
3976 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003977 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3978 root_item.received_uuid,
3979 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3980 key.objectid);
3981 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003982 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003983 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003984 break;
3985 }
3986 }
3987
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003988skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003989 if (trans) {
3990 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003991 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003992 if (ret)
3993 break;
3994 }
3995
Stefan Behrens803b2f52013-08-15 17:11:21 +02003996 btrfs_release_path(path);
3997 if (key.offset < (u64)-1) {
3998 key.offset++;
3999 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4000 key.offset = 0;
4001 key.type = BTRFS_ROOT_ITEM_KEY;
4002 } else if (key.objectid < (u64)-1) {
4003 key.offset = 0;
4004 key.type = BTRFS_ROOT_ITEM_KEY;
4005 key.objectid++;
4006 } else {
4007 break;
4008 }
4009 cond_resched();
4010 }
4011
4012out:
4013 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004014 if (trans && !IS_ERR(trans))
4015 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004016 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004017 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004018 else
4019 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004020 up(&fs_info->uuid_tree_rescan_sem);
4021 return 0;
4022}
4023
Stefan Behrens70f80172013-08-15 17:11:23 +02004024/*
4025 * Callback for btrfs_uuid_tree_iterate().
4026 * returns:
4027 * 0 check succeeded, the entry is not outdated.
4028 * < 0 if an error occured.
4029 * > 0 if the check failed, which means the caller shall remove the entry.
4030 */
4031static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4032 u8 *uuid, u8 type, u64 subid)
4033{
4034 struct btrfs_key key;
4035 int ret = 0;
4036 struct btrfs_root *subvol_root;
4037
4038 if (type != BTRFS_UUID_KEY_SUBVOL &&
4039 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4040 goto out;
4041
4042 key.objectid = subid;
4043 key.type = BTRFS_ROOT_ITEM_KEY;
4044 key.offset = (u64)-1;
4045 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4046 if (IS_ERR(subvol_root)) {
4047 ret = PTR_ERR(subvol_root);
4048 if (ret == -ENOENT)
4049 ret = 1;
4050 goto out;
4051 }
4052
4053 switch (type) {
4054 case BTRFS_UUID_KEY_SUBVOL:
4055 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4056 ret = 1;
4057 break;
4058 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4059 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4060 BTRFS_UUID_SIZE))
4061 ret = 1;
4062 break;
4063 }
4064
4065out:
4066 return ret;
4067}
4068
4069static int btrfs_uuid_rescan_kthread(void *data)
4070{
4071 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4072 int ret;
4073
4074 /*
4075 * 1st step is to iterate through the existing UUID tree and
4076 * to delete all entries that contain outdated data.
4077 * 2nd step is to add all missing entries to the UUID tree.
4078 */
4079 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4080 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004081 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004082 up(&fs_info->uuid_tree_rescan_sem);
4083 return ret;
4084 }
4085 return btrfs_uuid_scan_kthread(data);
4086}
4087
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004088int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4089{
4090 struct btrfs_trans_handle *trans;
4091 struct btrfs_root *tree_root = fs_info->tree_root;
4092 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004093 struct task_struct *task;
4094 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004095
4096 /*
4097 * 1 - root node
4098 * 1 - root item
4099 */
4100 trans = btrfs_start_transaction(tree_root, 2);
4101 if (IS_ERR(trans))
4102 return PTR_ERR(trans);
4103
4104 uuid_root = btrfs_create_tree(trans, fs_info,
4105 BTRFS_UUID_TREE_OBJECTID);
4106 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004107 ret = PTR_ERR(uuid_root);
4108 btrfs_abort_transaction(trans, tree_root, ret);
4109 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004110 }
4111
4112 fs_info->uuid_root = uuid_root;
4113
Stefan Behrens803b2f52013-08-15 17:11:21 +02004114 ret = btrfs_commit_transaction(trans, tree_root);
4115 if (ret)
4116 return ret;
4117
4118 down(&fs_info->uuid_tree_rescan_sem);
4119 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4120 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004121 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004122 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004123 up(&fs_info->uuid_tree_rescan_sem);
4124 return PTR_ERR(task);
4125 }
4126
4127 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004128}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004129
Stefan Behrens70f80172013-08-15 17:11:23 +02004130int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4131{
4132 struct task_struct *task;
4133
4134 down(&fs_info->uuid_tree_rescan_sem);
4135 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4136 if (IS_ERR(task)) {
4137 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004138 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004139 up(&fs_info->uuid_tree_rescan_sem);
4140 return PTR_ERR(task);
4141 }
4142
4143 return 0;
4144}
4145
Chris Mason8f18cf12008-04-25 16:53:30 -04004146/*
4147 * shrinking a device means finding all of the device extents past
4148 * the new size, and then following the back refs to the chunks.
4149 * The chunk relocation code actually frees the device extent
4150 */
4151int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4152{
4153 struct btrfs_trans_handle *trans;
4154 struct btrfs_root *root = device->dev_root;
4155 struct btrfs_dev_extent *dev_extent = NULL;
4156 struct btrfs_path *path;
4157 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004158 u64 chunk_offset;
4159 int ret;
4160 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004161 int failed = 0;
4162 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004163 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004164 struct extent_buffer *l;
4165 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004166 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004167 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004168 u64 old_size = btrfs_device_get_total_bytes(device);
4169 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004170
Stefan Behrens63a212a2012-11-05 18:29:28 +01004171 if (device->is_tgtdev_for_dev_replace)
4172 return -EINVAL;
4173
Chris Mason8f18cf12008-04-25 16:53:30 -04004174 path = btrfs_alloc_path();
4175 if (!path)
4176 return -ENOMEM;
4177
Chris Mason8f18cf12008-04-25 16:53:30 -04004178 path->reada = 2;
4179
Chris Mason7d9eb122008-07-08 14:19:17 -04004180 lock_chunks(root);
4181
Miao Xie7cc8e582014-09-03 21:35:38 +08004182 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004183 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004184 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004185 spin_lock(&root->fs_info->free_chunk_lock);
4186 root->fs_info->free_chunk_space -= diff;
4187 spin_unlock(&root->fs_info->free_chunk_lock);
4188 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004189 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004190
Josef Bacikba1bf482009-09-11 16:11:19 -04004191again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004192 key.objectid = device->devid;
4193 key.offset = (u64)-1;
4194 key.type = BTRFS_DEV_EXTENT_KEY;
4195
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004196 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004197 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004198 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004199 if (ret < 0) {
4200 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004201 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004202 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004203
4204 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004205 if (ret)
4206 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004207 if (ret < 0)
4208 goto done;
4209 if (ret) {
4210 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004211 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004212 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004213 }
4214
4215 l = path->nodes[0];
4216 slot = path->slots[0];
4217 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4218
Josef Bacikba1bf482009-09-11 16:11:19 -04004219 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004220 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004221 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004222 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004223 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004224
4225 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4226 length = btrfs_dev_extent_length(l, dev_extent);
4227
Josef Bacikba1bf482009-09-11 16:11:19 -04004228 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004229 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004230 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004231 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004232 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004233
Chris Mason8f18cf12008-04-25 16:53:30 -04004234 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004235 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004236
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004237 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004238 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004239 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004240 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004241 if (ret == -ENOSPC)
4242 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004243 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004244
4245 if (failed && !retried) {
4246 failed = 0;
4247 retried = true;
4248 goto again;
4249 } else if (failed && retried) {
4250 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004251 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004252 }
4253
Chris Balld6397ba2009-04-27 07:29:03 -04004254 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004255 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004256 if (IS_ERR(trans)) {
4257 ret = PTR_ERR(trans);
4258 goto done;
4259 }
4260
Chris Balld6397ba2009-04-27 07:29:03 -04004261 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004262
4263 /*
4264 * We checked in the above loop all device extents that were already in
4265 * the device tree. However before we have updated the device's
4266 * total_bytes to the new size, we might have had chunk allocations that
4267 * have not complete yet (new block groups attached to transaction
4268 * handles), and therefore their device extents were not yet in the
4269 * device tree and we missed them in the loop above. So if we have any
4270 * pending chunk using a device extent that overlaps the device range
4271 * that we can not use anymore, commit the current transaction and
4272 * repeat the search on the device tree - this way we guarantee we will
4273 * not have chunks using device extents that end beyond 'new_size'.
4274 */
4275 if (!checked_pending_chunks) {
4276 u64 start = new_size;
4277 u64 len = old_size - new_size;
4278
Jeff Mahoney499f3772015-06-15 09:41:17 -04004279 if (contains_pending_extent(trans->transaction, device,
4280 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004281 unlock_chunks(root);
4282 checked_pending_chunks = true;
4283 failed = 0;
4284 retried = false;
4285 ret = btrfs_commit_transaction(trans, root);
4286 if (ret)
4287 goto done;
4288 goto again;
4289 }
4290 }
4291
Miao Xie7cc8e582014-09-03 21:35:38 +08004292 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004293 if (list_empty(&device->resized_list))
4294 list_add_tail(&device->resized_list,
4295 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004296
Chris Balld6397ba2009-04-27 07:29:03 -04004297 WARN_ON(diff > old_total);
4298 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4299 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004300
4301 /* Now btrfs_update_device() will change the on-disk size. */
4302 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004303 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004304done:
4305 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004306 if (ret) {
4307 lock_chunks(root);
4308 btrfs_device_set_total_bytes(device, old_size);
4309 if (device->writeable)
4310 device->fs_devices->total_rw_bytes += diff;
4311 spin_lock(&root->fs_info->free_chunk_lock);
4312 root->fs_info->free_chunk_space += diff;
4313 spin_unlock(&root->fs_info->free_chunk_lock);
4314 unlock_chunks(root);
4315 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004316 return ret;
4317}
4318
Li Zefan125ccb02011-12-08 15:07:24 +08004319static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004320 struct btrfs_key *key,
4321 struct btrfs_chunk *chunk, int item_size)
4322{
David Sterba6c417612011-04-13 15:41:04 +02004323 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004324 struct btrfs_disk_key disk_key;
4325 u32 array_size;
4326 u8 *ptr;
4327
Miao Xiefe48a5c2014-09-03 21:35:39 +08004328 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004329 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004330 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004331 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4332 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004333 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004334 }
Chris Mason0b86a832008-03-24 15:01:56 -04004335
4336 ptr = super_copy->sys_chunk_array + array_size;
4337 btrfs_cpu_key_to_disk(&disk_key, key);
4338 memcpy(ptr, &disk_key, sizeof(disk_key));
4339 ptr += sizeof(disk_key);
4340 memcpy(ptr, chunk, item_size);
4341 item_size += sizeof(disk_key);
4342 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004343 unlock_chunks(root);
4344
Chris Mason0b86a832008-03-24 15:01:56 -04004345 return 0;
4346}
4347
Miao Xieb2117a32011-01-05 10:07:28 +00004348/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004349 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004350 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004351static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004352{
Arne Jansen73c5de02011-04-12 12:07:57 +02004353 const struct btrfs_device_info *di_a = a;
4354 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004355
Arne Jansen73c5de02011-04-12 12:07:57 +02004356 if (di_a->max_avail > di_b->max_avail)
4357 return -1;
4358 if (di_a->max_avail < di_b->max_avail)
4359 return 1;
4360 if (di_a->total_avail > di_b->total_avail)
4361 return -1;
4362 if (di_a->total_avail < di_b->total_avail)
4363 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004364 return 0;
4365}
4366
David Woodhouse53b381b2013-01-29 18:40:14 -05004367static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4368{
4369 /* TODO allow them to set a preferred stripe size */
4370 return 64 * 1024;
4371}
4372
4373static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4374{
Zhao Leiffe2d202015-01-20 15:11:44 +08004375 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004376 return;
4377
Miao Xieceda0862013-04-11 10:30:16 +00004378 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004379}
4380
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004381#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4382 - sizeof(struct btrfs_item) \
4383 - sizeof(struct btrfs_chunk)) \
4384 / sizeof(struct btrfs_stripe) + 1)
4385
4386#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4387 - 2 * sizeof(struct btrfs_disk_key) \
4388 - 2 * sizeof(struct btrfs_chunk)) \
4389 / sizeof(struct btrfs_stripe) + 1)
4390
Miao Xieb2117a32011-01-05 10:07:28 +00004391static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004392 struct btrfs_root *extent_root, u64 start,
4393 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004394{
4395 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004396 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4397 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004398 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004399 struct extent_map_tree *em_tree;
4400 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004401 struct btrfs_device_info *devices_info = NULL;
4402 u64 total_avail;
4403 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004404 int data_stripes; /* number of stripes that count for
4405 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004406 int sub_stripes; /* sub_stripes info for map */
4407 int dev_stripes; /* stripes per dev */
4408 int devs_max; /* max devs to use */
4409 int devs_min; /* min devs needed */
4410 int devs_increment; /* ndevs has to be a multiple of this */
4411 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004412 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004413 u64 max_stripe_size;
4414 u64 max_chunk_size;
4415 u64 stripe_size;
4416 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004417 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004418 int ndevs;
4419 int i;
4420 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004421 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004422
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004423 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004424
Miao Xieb2117a32011-01-05 10:07:28 +00004425 if (list_empty(&fs_devices->alloc_list))
4426 return -ENOSPC;
4427
Liu Bo31e50222012-11-21 14:18:10 +00004428 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004429
Liu Bo31e50222012-11-21 14:18:10 +00004430 sub_stripes = btrfs_raid_array[index].sub_stripes;
4431 dev_stripes = btrfs_raid_array[index].dev_stripes;
4432 devs_max = btrfs_raid_array[index].devs_max;
4433 devs_min = btrfs_raid_array[index].devs_min;
4434 devs_increment = btrfs_raid_array[index].devs_increment;
4435 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004436
4437 if (type & BTRFS_BLOCK_GROUP_DATA) {
4438 max_stripe_size = 1024 * 1024 * 1024;
4439 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004440 if (!devs_max)
4441 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004442 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004443 /* for larger filesystems, use larger metadata chunks */
4444 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4445 max_stripe_size = 1024 * 1024 * 1024;
4446 else
4447 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004448 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004449 if (!devs_max)
4450 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004451 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004452 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004453 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004454 if (!devs_max)
4455 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004456 } else {
David Sterba351fd352014-05-15 16:48:20 +02004457 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004458 type);
4459 BUG_ON(1);
4460 }
4461
4462 /* we don't want a chunk larger than 10% of writeable space */
4463 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4464 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004465
David Sterba31e818f2015-02-20 18:00:26 +01004466 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004467 GFP_NOFS);
4468 if (!devices_info)
4469 return -ENOMEM;
4470
Arne Jansen73c5de02011-04-12 12:07:57 +02004471 cur = fs_devices->alloc_list.next;
4472
4473 /*
4474 * in the first pass through the devices list, we gather information
4475 * about the available holes on each device.
4476 */
4477 ndevs = 0;
4478 while (cur != &fs_devices->alloc_list) {
4479 struct btrfs_device *device;
4480 u64 max_avail;
4481 u64 dev_offset;
4482
4483 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4484
4485 cur = cur->next;
4486
4487 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004488 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004489 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004490 continue;
4491 }
4492
Stefan Behrens63a212a2012-11-05 18:29:28 +01004493 if (!device->in_fs_metadata ||
4494 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004495 continue;
4496
4497 if (device->total_bytes > device->bytes_used)
4498 total_avail = device->total_bytes - device->bytes_used;
4499 else
4500 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004501
4502 /* If there is no space on this device, skip it. */
4503 if (total_avail == 0)
4504 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004505
Josef Bacik6df9a952013-06-27 13:22:46 -04004506 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004507 max_stripe_size * dev_stripes,
4508 &dev_offset, &max_avail);
4509 if (ret && ret != -ENOSPC)
4510 goto error;
4511
4512 if (ret == 0)
4513 max_avail = max_stripe_size * dev_stripes;
4514
4515 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4516 continue;
4517
Eric Sandeen063d0062013-01-31 00:55:01 +00004518 if (ndevs == fs_devices->rw_devices) {
4519 WARN(1, "%s: found more than %llu devices\n",
4520 __func__, fs_devices->rw_devices);
4521 break;
4522 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004523 devices_info[ndevs].dev_offset = dev_offset;
4524 devices_info[ndevs].max_avail = max_avail;
4525 devices_info[ndevs].total_avail = total_avail;
4526 devices_info[ndevs].dev = device;
4527 ++ndevs;
4528 }
4529
4530 /*
4531 * now sort the devices by hole size / available space
4532 */
4533 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4534 btrfs_cmp_device_info, NULL);
4535
4536 /* round down to number of usable stripes */
4537 ndevs -= ndevs % devs_increment;
4538
4539 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4540 ret = -ENOSPC;
4541 goto error;
4542 }
4543
4544 if (devs_max && ndevs > devs_max)
4545 ndevs = devs_max;
4546 /*
4547 * the primary goal is to maximize the number of stripes, so use as many
4548 * devices as possible, even if the stripes are not maximum sized.
4549 */
4550 stripe_size = devices_info[ndevs-1].max_avail;
4551 num_stripes = ndevs * dev_stripes;
4552
David Woodhouse53b381b2013-01-29 18:40:14 -05004553 /*
4554 * this will have to be fixed for RAID1 and RAID10 over
4555 * more drives
4556 */
4557 data_stripes = num_stripes / ncopies;
4558
David Woodhouse53b381b2013-01-29 18:40:14 -05004559 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4560 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4561 btrfs_super_stripesize(info->super_copy));
4562 data_stripes = num_stripes - 1;
4563 }
4564 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4565 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4566 btrfs_super_stripesize(info->super_copy));
4567 data_stripes = num_stripes - 2;
4568 }
Chris Mason86db2572013-02-20 16:23:40 -05004569
4570 /*
4571 * Use the number of data stripes to figure out how big this chunk
4572 * is really going to be in terms of logical address space,
4573 * and compare that answer with the max chunk size
4574 */
4575 if (stripe_size * data_stripes > max_chunk_size) {
4576 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004577
4578 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004579
4580 /* bump the answer up to a 16MB boundary */
4581 stripe_size = (stripe_size + mask) & ~mask;
4582
4583 /* but don't go higher than the limits we found
4584 * while searching for free extents
4585 */
4586 if (stripe_size > devices_info[ndevs-1].max_avail)
4587 stripe_size = devices_info[ndevs-1].max_avail;
4588 }
4589
David Sterbab8b93ad2015-01-16 17:26:13 +01004590 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004591
4592 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004593 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004594 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004595
Miao Xieb2117a32011-01-05 10:07:28 +00004596 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4597 if (!map) {
4598 ret = -ENOMEM;
4599 goto error;
4600 }
4601 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004602
Arne Jansen73c5de02011-04-12 12:07:57 +02004603 for (i = 0; i < ndevs; ++i) {
4604 for (j = 0; j < dev_stripes; ++j) {
4605 int s = i * dev_stripes + j;
4606 map->stripes[s].dev = devices_info[i].dev;
4607 map->stripes[s].physical = devices_info[i].dev_offset +
4608 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004609 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004610 }
Chris Mason593060d2008-03-25 16:50:33 -04004611 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004612 map->stripe_len = raid_stripe_len;
4613 map->io_align = raid_stripe_len;
4614 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004615 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004616 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004617
David Woodhouse53b381b2013-01-29 18:40:14 -05004618 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004619
Arne Jansen73c5de02011-04-12 12:07:57 +02004620 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004621
David Sterba172ddd62011-04-21 00:48:27 +02004622 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004623 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004624 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004625 ret = -ENOMEM;
4626 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004627 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004628 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004629 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004630 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004631 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004632 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004633 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004634 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004635
Chris Mason0b86a832008-03-24 15:01:56 -04004636 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004637 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004638 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004639 if (!ret) {
4640 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4641 atomic_inc(&em->refs);
4642 }
Chris Mason890871b2009-09-02 16:24:52 -04004643 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004644 if (ret) {
4645 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004646 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004647 }
Yan Zheng2b820322008-11-17 21:11:30 -05004648
Josef Bacik04487482013-01-29 15:03:37 -05004649 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4650 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4651 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004652 if (ret)
4653 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004654
Miao Xie7cc8e582014-09-03 21:35:38 +08004655 for (i = 0; i < map->num_stripes; i++) {
4656 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4657 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4658 }
Miao Xie43530c42014-09-03 21:35:36 +08004659
Miao Xie1c116182014-09-03 21:35:37 +08004660 spin_lock(&extent_root->fs_info->free_chunk_lock);
4661 extent_root->fs_info->free_chunk_space -= (stripe_size *
4662 map->num_stripes);
4663 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4664
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004665 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004666 check_raid56_incompat_flag(extent_root->fs_info, type);
4667
Miao Xieb2117a32011-01-05 10:07:28 +00004668 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004669 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004670
Josef Bacik6df9a952013-06-27 13:22:46 -04004671error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004672 write_lock(&em_tree->lock);
4673 remove_extent_mapping(em_tree, em);
4674 write_unlock(&em_tree->lock);
4675
4676 /* One for our allocation */
4677 free_extent_map(em);
4678 /* One for the tree reference */
4679 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004680 /* One for the pending_chunks list reference */
4681 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004682error:
Miao Xieb2117a32011-01-05 10:07:28 +00004683 kfree(devices_info);
4684 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004685}
4686
Josef Bacik6df9a952013-06-27 13:22:46 -04004687int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004688 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004689 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004690{
Yan Zheng2b820322008-11-17 21:11:30 -05004691 struct btrfs_key key;
4692 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4693 struct btrfs_device *device;
4694 struct btrfs_chunk *chunk;
4695 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004696 struct extent_map_tree *em_tree;
4697 struct extent_map *em;
4698 struct map_lookup *map;
4699 size_t item_size;
4700 u64 dev_offset;
4701 u64 stripe_size;
4702 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004703 int ret;
4704
Josef Bacik6df9a952013-06-27 13:22:46 -04004705 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4706 read_lock(&em_tree->lock);
4707 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4708 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004709
Josef Bacik6df9a952013-06-27 13:22:46 -04004710 if (!em) {
4711 btrfs_crit(extent_root->fs_info, "unable to find logical "
4712 "%Lu len %Lu", chunk_offset, chunk_size);
4713 return -EINVAL;
4714 }
4715
4716 if (em->start != chunk_offset || em->len != chunk_size) {
4717 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004718 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004719 chunk_size, em->start, em->len);
4720 free_extent_map(em);
4721 return -EINVAL;
4722 }
4723
4724 map = (struct map_lookup *)em->bdev;
4725 item_size = btrfs_chunk_item_size(map->num_stripes);
4726 stripe_size = em->orig_block_len;
4727
4728 chunk = kzalloc(item_size, GFP_NOFS);
4729 if (!chunk) {
4730 ret = -ENOMEM;
4731 goto out;
4732 }
4733
4734 for (i = 0; i < map->num_stripes; i++) {
4735 device = map->stripes[i].dev;
4736 dev_offset = map->stripes[i].physical;
4737
Yan Zheng2b820322008-11-17 21:11:30 -05004738 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004739 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004740 goto out;
4741 ret = btrfs_alloc_dev_extent(trans, device,
4742 chunk_root->root_key.objectid,
4743 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4744 chunk_offset, dev_offset,
4745 stripe_size);
4746 if (ret)
4747 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004748 }
4749
Yan Zheng2b820322008-11-17 21:11:30 -05004750 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004751 for (i = 0; i < map->num_stripes; i++) {
4752 device = map->stripes[i].dev;
4753 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004754
4755 btrfs_set_stack_stripe_devid(stripe, device->devid);
4756 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4757 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4758 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004759 }
4760
4761 btrfs_set_stack_chunk_length(chunk, chunk_size);
4762 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4763 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4764 btrfs_set_stack_chunk_type(chunk, map->type);
4765 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4766 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4767 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4768 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4769 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4770
4771 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4772 key.type = BTRFS_CHUNK_ITEM_KEY;
4773 key.offset = chunk_offset;
4774
4775 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004776 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4777 /*
4778 * TODO: Cleanup of inserted chunk root in case of
4779 * failure.
4780 */
Li Zefan125ccb02011-12-08 15:07:24 +08004781 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004782 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004783 }
liubo1abe9b82011-03-24 11:18:59 +00004784
Josef Bacik6df9a952013-06-27 13:22:46 -04004785out:
Yan Zheng2b820322008-11-17 21:11:30 -05004786 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004787 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004788 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004789}
4790
4791/*
4792 * Chunk allocation falls into two parts. The first part does works
4793 * that make the new allocated chunk useable, but not do any operation
4794 * that modifies the chunk tree. The second part does the works that
4795 * require modifying the chunk tree. This division is important for the
4796 * bootstrap process of adding storage to a seed btrfs.
4797 */
4798int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4799 struct btrfs_root *extent_root, u64 type)
4800{
4801 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004802
Filipe Mananaa9629592015-05-18 19:11:40 +01004803 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004804 chunk_offset = find_next_chunk(extent_root->fs_info);
4805 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004806}
4807
Chris Masond3977122009-01-05 21:25:51 -05004808static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004809 struct btrfs_root *root,
4810 struct btrfs_device *device)
4811{
4812 u64 chunk_offset;
4813 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004814 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004815 struct btrfs_fs_info *fs_info = root->fs_info;
4816 struct btrfs_root *extent_root = fs_info->extent_root;
4817 int ret;
4818
Josef Bacik6df9a952013-06-27 13:22:46 -04004819 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004820 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004821 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4822 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004823 if (ret)
4824 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004825
Josef Bacik6df9a952013-06-27 13:22:46 -04004826 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004827 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004828 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4829 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004830 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004831}
4832
Miao Xied20983b2014-07-03 18:22:13 +08004833static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4834{
4835 int max_errors;
4836
4837 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4838 BTRFS_BLOCK_GROUP_RAID10 |
4839 BTRFS_BLOCK_GROUP_RAID5 |
4840 BTRFS_BLOCK_GROUP_DUP)) {
4841 max_errors = 1;
4842 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4843 max_errors = 2;
4844 } else {
4845 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004846 }
4847
Miao Xied20983b2014-07-03 18:22:13 +08004848 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004849}
4850
4851int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4852{
4853 struct extent_map *em;
4854 struct map_lookup *map;
4855 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4856 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004857 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004858 int i;
4859
Chris Mason890871b2009-09-02 16:24:52 -04004860 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004861 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004862 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004863 if (!em)
4864 return 1;
4865
4866 map = (struct map_lookup *)em->bdev;
4867 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004868 if (map->stripes[i].dev->missing) {
4869 miss_ndevs++;
4870 continue;
4871 }
4872
Yan Zheng2b820322008-11-17 21:11:30 -05004873 if (!map->stripes[i].dev->writeable) {
4874 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004875 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004876 }
4877 }
Miao Xied20983b2014-07-03 18:22:13 +08004878
4879 /*
4880 * If the number of missing devices is larger than max errors,
4881 * we can not write the data into that chunk successfully, so
4882 * set it readonly.
4883 */
4884 if (miss_ndevs > btrfs_chunk_max_errors(map))
4885 readonly = 1;
4886end:
Yan Zheng2b820322008-11-17 21:11:30 -05004887 free_extent_map(em);
4888 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004889}
4890
4891void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4892{
David Sterbaa8067e02011-04-21 00:34:43 +02004893 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004894}
4895
4896void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4897{
4898 struct extent_map *em;
4899
Chris Masond3977122009-01-05 21:25:51 -05004900 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004901 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004902 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4903 if (em)
4904 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004905 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004906 if (!em)
4907 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004908 /* once for us */
4909 free_extent_map(em);
4910 /* once for the tree */
4911 free_extent_map(em);
4912 }
4913}
4914
Stefan Behrens5d964052012-11-05 14:59:07 +01004915int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004916{
Stefan Behrens5d964052012-11-05 14:59:07 +01004917 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004918 struct extent_map *em;
4919 struct map_lookup *map;
4920 struct extent_map_tree *em_tree = &map_tree->map_tree;
4921 int ret;
4922
Chris Mason890871b2009-09-02 16:24:52 -04004923 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004924 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004925 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004926
Josef Bacikfb7669b2013-04-23 10:53:18 -04004927 /*
4928 * We could return errors for these cases, but that could get ugly and
4929 * we'd probably do the same thing which is just not do anything else
4930 * and exit, so return 1 so the callers don't try to use other copies.
4931 */
4932 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004933 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004934 logical+len);
4935 return 1;
4936 }
4937
4938 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004939 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004940 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004941 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004942 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004943 return 1;
4944 }
4945
Chris Masonf1885912008-04-09 16:28:12 -04004946 map = (struct map_lookup *)em->bdev;
4947 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4948 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004949 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4950 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004951 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4952 ret = 2;
4953 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4954 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004955 else
4956 ret = 1;
4957 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004958
4959 btrfs_dev_replace_lock(&fs_info->dev_replace);
4960 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4961 ret++;
4962 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4963
Chris Masonf1885912008-04-09 16:28:12 -04004964 return ret;
4965}
4966
David Woodhouse53b381b2013-01-29 18:40:14 -05004967unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4968 struct btrfs_mapping_tree *map_tree,
4969 u64 logical)
4970{
4971 struct extent_map *em;
4972 struct map_lookup *map;
4973 struct extent_map_tree *em_tree = &map_tree->map_tree;
4974 unsigned long len = root->sectorsize;
4975
4976 read_lock(&em_tree->lock);
4977 em = lookup_extent_mapping(em_tree, logical, len);
4978 read_unlock(&em_tree->lock);
4979 BUG_ON(!em);
4980
4981 BUG_ON(em->start > logical || em->start + em->len < logical);
4982 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004983 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004984 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004985 free_extent_map(em);
4986 return len;
4987}
4988
4989int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4990 u64 logical, u64 len, int mirror_num)
4991{
4992 struct extent_map *em;
4993 struct map_lookup *map;
4994 struct extent_map_tree *em_tree = &map_tree->map_tree;
4995 int ret = 0;
4996
4997 read_lock(&em_tree->lock);
4998 em = lookup_extent_mapping(em_tree, logical, len);
4999 read_unlock(&em_tree->lock);
5000 BUG_ON(!em);
5001
5002 BUG_ON(em->start > logical || em->start + em->len < logical);
5003 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08005004 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005005 ret = 1;
5006 free_extent_map(em);
5007 return ret;
5008}
5009
Stefan Behrens30d98612012-11-06 14:52:18 +01005010static int find_live_mirror(struct btrfs_fs_info *fs_info,
5011 struct map_lookup *map, int first, int num,
5012 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005013{
5014 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005015 int tolerance;
5016 struct btrfs_device *srcdev;
5017
5018 if (dev_replace_is_ongoing &&
5019 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5020 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5021 srcdev = fs_info->dev_replace.srcdev;
5022 else
5023 srcdev = NULL;
5024
5025 /*
5026 * try to avoid the drive that is the source drive for a
5027 * dev-replace procedure, only choose it if no other non-missing
5028 * mirror is available
5029 */
5030 for (tolerance = 0; tolerance < 2; tolerance++) {
5031 if (map->stripes[optimal].dev->bdev &&
5032 (tolerance || map->stripes[optimal].dev != srcdev))
5033 return optimal;
5034 for (i = first; i < first + num; i++) {
5035 if (map->stripes[i].dev->bdev &&
5036 (tolerance || map->stripes[i].dev != srcdev))
5037 return i;
5038 }
Chris Masondfe25022008-05-13 13:46:40 -04005039 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005040
Chris Masondfe25022008-05-13 13:46:40 -04005041 /* we couldn't find one that doesn't fail. Just return something
5042 * and the io error handling code will clean up eventually
5043 */
5044 return optimal;
5045}
5046
David Woodhouse53b381b2013-01-29 18:40:14 -05005047static inline int parity_smaller(u64 a, u64 b)
5048{
5049 return a > b;
5050}
5051
5052/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005053static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005054{
5055 struct btrfs_bio_stripe s;
5056 int i;
5057 u64 l;
5058 int again = 1;
5059
5060 while (again) {
5061 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005062 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005063 if (parity_smaller(bbio->raid_map[i],
5064 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005065 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005066 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005067 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005068 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005069 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005070 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005071
David Woodhouse53b381b2013-01-29 18:40:14 -05005072 again = 1;
5073 }
5074 }
5075 }
5076}
5077
Zhao Lei6e9606d2015-01-20 15:11:34 +08005078static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5079{
5080 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005081 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005082 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005083 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005084 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005085 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005086 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005087 /*
5088 * plus the raid_map, which includes both the tgt dev
5089 * and the stripes
5090 */
5091 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005092 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005093
5094 atomic_set(&bbio->error, 0);
5095 atomic_set(&bbio->refs, 1);
5096
5097 return bbio;
5098}
5099
5100void btrfs_get_bbio(struct btrfs_bio *bbio)
5101{
5102 WARN_ON(!atomic_read(&bbio->refs));
5103 atomic_inc(&bbio->refs);
5104}
5105
5106void btrfs_put_bbio(struct btrfs_bio *bbio)
5107{
5108 if (!bbio)
5109 return;
5110 if (atomic_dec_and_test(&bbio->refs))
5111 kfree(bbio);
5112}
5113
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005114static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005115 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005116 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005117 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005118{
5119 struct extent_map *em;
5120 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005121 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005122 struct extent_map_tree *em_tree = &map_tree->map_tree;
5123 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005124 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005125 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005126 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005127 u64 stripe_nr_orig;
5128 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005129 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005130 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005131 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005132 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005133 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005134 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005135 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005136 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005137 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5138 int dev_replace_is_ongoing = 0;
5139 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005140 int patch_the_first_stripe_for_dev_replace = 0;
5141 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005142 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005143
Chris Mason890871b2009-09-02 16:24:52 -04005144 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005145 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005146 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005147
Chris Mason3b951512008-04-17 11:29:12 -04005148 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005149 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005150 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005151 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005152 }
Chris Mason0b86a832008-03-24 15:01:56 -04005153
Josef Bacik9bb91872013-04-19 23:45:33 -04005154 if (em->start > logical || em->start + em->len < logical) {
5155 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005156 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005157 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005158 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005159 return -EINVAL;
5160 }
5161
Chris Mason0b86a832008-03-24 15:01:56 -04005162 map = (struct map_lookup *)em->bdev;
5163 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005164
David Woodhouse53b381b2013-01-29 18:40:14 -05005165 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005166 stripe_nr = offset;
5167 /*
5168 * stripe_nr counts the total number of stripes we have to stride
5169 * to get to this block
5170 */
David Sterba47c57132015-02-20 18:43:47 +01005171 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005172
David Woodhouse53b381b2013-01-29 18:40:14 -05005173 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005174 BUG_ON(offset < stripe_offset);
5175
5176 /* stripe_offset is the offset of this block in its stripe*/
5177 stripe_offset = offset - stripe_offset;
5178
David Woodhouse53b381b2013-01-29 18:40:14 -05005179 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005180 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005181 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5182 raid56_full_stripe_start = offset;
5183
5184 /* allow a write of a full stripe, but make sure we don't
5185 * allow straddling of stripes
5186 */
David Sterba47c57132015-02-20 18:43:47 +01005187 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5188 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005189 raid56_full_stripe_start *= full_stripe_len;
5190 }
5191
5192 if (rw & REQ_DISCARD) {
5193 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005194 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005195 ret = -EOPNOTSUPP;
5196 goto out;
5197 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005198 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005199 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5200 u64 max_len;
5201 /* For writes to RAID[56], allow a full stripeset across all disks.
5202 For other RAID types and for RAID[56] reads, just allow a single
5203 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005204 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005205 (rw & REQ_WRITE)) {
5206 max_len = stripe_len * nr_data_stripes(map) -
5207 (offset - raid56_full_stripe_start);
5208 } else {
5209 /* we limit the length of each bio to what fits in a stripe */
5210 max_len = stripe_len - stripe_offset;
5211 }
5212 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005213 } else {
5214 *length = em->len - offset;
5215 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005216
David Woodhouse53b381b2013-01-29 18:40:14 -05005217 /* This is for when we're called from btrfs_merge_bio_hook() and all
5218 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005219 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005220 goto out;
5221
Stefan Behrens472262f2012-11-06 14:43:46 +01005222 btrfs_dev_replace_lock(dev_replace);
5223 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5224 if (!dev_replace_is_ongoing)
5225 btrfs_dev_replace_unlock(dev_replace);
5226
Stefan Behrensad6d6202012-11-06 15:06:47 +01005227 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5228 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5229 dev_replace->tgtdev != NULL) {
5230 /*
5231 * in dev-replace case, for repair case (that's the only
5232 * case where the mirror is selected explicitly when
5233 * calling btrfs_map_block), blocks left of the left cursor
5234 * can also be read from the target drive.
5235 * For REQ_GET_READ_MIRRORS, the target drive is added as
5236 * the last one to the array of stripes. For READ, it also
5237 * needs to be supported using the same mirror number.
5238 * If the requested block is not left of the left cursor,
5239 * EIO is returned. This can happen because btrfs_num_copies()
5240 * returns one more in the dev-replace case.
5241 */
5242 u64 tmp_length = *length;
5243 struct btrfs_bio *tmp_bbio = NULL;
5244 int tmp_num_stripes;
5245 u64 srcdev_devid = dev_replace->srcdev->devid;
5246 int index_srcdev = 0;
5247 int found = 0;
5248 u64 physical_of_found = 0;
5249
5250 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005251 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005252 if (ret) {
5253 WARN_ON(tmp_bbio != NULL);
5254 goto out;
5255 }
5256
5257 tmp_num_stripes = tmp_bbio->num_stripes;
5258 if (mirror_num > tmp_num_stripes) {
5259 /*
5260 * REQ_GET_READ_MIRRORS does not contain this
5261 * mirror, that means that the requested area
5262 * is not left of the left cursor
5263 */
5264 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005265 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005266 goto out;
5267 }
5268
5269 /*
5270 * process the rest of the function using the mirror_num
5271 * of the source drive. Therefore look it up first.
5272 * At the end, patch the device pointer to the one of the
5273 * target drive.
5274 */
5275 for (i = 0; i < tmp_num_stripes; i++) {
5276 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5277 /*
5278 * In case of DUP, in order to keep it
5279 * simple, only add the mirror with the
5280 * lowest physical address
5281 */
5282 if (found &&
5283 physical_of_found <=
5284 tmp_bbio->stripes[i].physical)
5285 continue;
5286 index_srcdev = i;
5287 found = 1;
5288 physical_of_found =
5289 tmp_bbio->stripes[i].physical;
5290 }
5291 }
5292
5293 if (found) {
5294 mirror_num = index_srcdev + 1;
5295 patch_the_first_stripe_for_dev_replace = 1;
5296 physical_to_patch_in_first_stripe = physical_of_found;
5297 } else {
5298 WARN_ON(1);
5299 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005300 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005301 goto out;
5302 }
5303
Zhao Lei6e9606d2015-01-20 15:11:34 +08005304 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005305 } else if (mirror_num > map->num_stripes) {
5306 mirror_num = 0;
5307 }
5308
Chris Masonf2d8d742008-04-21 10:03:05 -04005309 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005310 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005311 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005312 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005313 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005314 stripe_end_offset = stripe_nr_end * map->stripe_len -
5315 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005316
Li Dongyangfce3bb92011-03-24 10:24:26 +00005317 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5318 if (rw & REQ_DISCARD)
5319 num_stripes = min_t(u64, map->num_stripes,
5320 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005321 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5322 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005323 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5324 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005325 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005326 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005327 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005328 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005329 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005330 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005331 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005332 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005333 current->pid % map->num_stripes,
5334 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005335 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005336 }
Chris Mason2fff7342008-04-29 14:12:09 -04005337
Chris Mason611f0e02008-04-03 16:29:03 -04005338 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005339 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005340 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005341 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005342 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005343 } else {
5344 mirror_num = 1;
5345 }
Chris Mason2fff7342008-04-29 14:12:09 -04005346
Chris Mason321aecc2008-04-16 10:49:51 -04005347 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005348 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005349
David Sterba47c57132015-02-20 18:43:47 +01005350 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005351 stripe_index *= map->sub_stripes;
5352
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005353 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005354 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005355 else if (rw & REQ_DISCARD)
5356 num_stripes = min_t(u64, map->sub_stripes *
5357 (stripe_nr_end - stripe_nr_orig),
5358 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005359 else if (mirror_num)
5360 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005361 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005362 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005363 stripe_index = find_live_mirror(fs_info, map,
5364 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005365 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005366 current->pid % map->sub_stripes,
5367 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005368 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005369 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005370
Zhao Leiffe2d202015-01-20 15:11:44 +08005371 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005372 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005373 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5374 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005375 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005376 stripe_nr = div_u64(raid56_full_stripe_start,
5377 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005378
5379 /* RAID[56] write or recovery. Return all stripes */
5380 num_stripes = map->num_stripes;
5381 max_errors = nr_parity_stripes(map);
5382
David Woodhouse53b381b2013-01-29 18:40:14 -05005383 *length = map->stripe_len;
5384 stripe_index = 0;
5385 stripe_offset = 0;
5386 } else {
5387 /*
5388 * Mirror #0 or #1 means the original data block.
5389 * Mirror #2 is RAID5 parity block.
5390 * Mirror #3 is RAID6 Q block.
5391 */
David Sterba47c57132015-02-20 18:43:47 +01005392 stripe_nr = div_u64_rem(stripe_nr,
5393 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005394 if (mirror_num > 1)
5395 stripe_index = nr_data_stripes(map) +
5396 mirror_num - 2;
5397
5398 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005399 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5400 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005401 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5402 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5403 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005404 }
Chris Mason8790d502008-04-03 16:29:03 -04005405 } else {
5406 /*
David Sterba47c57132015-02-20 18:43:47 +01005407 * after this, stripe_nr is the number of stripes on this
5408 * device we have to walk to find the data, and stripe_index is
5409 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005410 */
David Sterba47c57132015-02-20 18:43:47 +01005411 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5412 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005413 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005414 }
Chris Mason593060d2008-03-25 16:50:33 -04005415 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005416
Stefan Behrens472262f2012-11-06 14:43:46 +01005417 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005418 if (dev_replace_is_ongoing) {
5419 if (rw & (REQ_WRITE | REQ_DISCARD))
5420 num_alloc_stripes <<= 1;
5421 if (rw & REQ_GET_READ_MIRRORS)
5422 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005423 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005424 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005425
Zhao Lei6e9606d2015-01-20 15:11:34 +08005426 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005427 if (!bbio) {
5428 ret = -ENOMEM;
5429 goto out;
5430 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005431 if (dev_replace_is_ongoing)
5432 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005433
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005434 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005435 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005436 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5437 mirror_num > 1)) {
5438 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005439 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005440
5441 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5442 sizeof(struct btrfs_bio_stripe) *
5443 num_alloc_stripes +
5444 sizeof(int) * tgtdev_indexes);
5445
5446 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005447 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005448
5449 /* Fill in the logical address of each stripe */
5450 tmp = stripe_nr * nr_data_stripes(map);
5451 for (i = 0; i < nr_data_stripes(map); i++)
5452 bbio->raid_map[(i+rot) % num_stripes] =
5453 em->start + (tmp + i) * map->stripe_len;
5454
5455 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5456 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5457 bbio->raid_map[(i+rot+1) % num_stripes] =
5458 RAID6_Q_STRIPE;
5459 }
5460
Li Dongyangfce3bb92011-03-24 10:24:26 +00005461 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005462 u32 factor = 0;
5463 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005464 u64 stripes_per_dev = 0;
5465 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005466 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005467
5468 if (map->type &
5469 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5470 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5471 sub_stripes = 1;
5472 else
5473 sub_stripes = map->sub_stripes;
5474
5475 factor = map->num_stripes / sub_stripes;
5476 stripes_per_dev = div_u64_rem(stripe_nr_end -
5477 stripe_nr_orig,
5478 factor,
5479 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005480 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5481 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005482 }
5483
Li Dongyangfce3bb92011-03-24 10:24:26 +00005484 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005485 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005486 map->stripes[stripe_index].physical +
5487 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005488 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005489
Li Zefanec9ef7a2011-12-01 14:06:42 +08005490 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5491 BTRFS_BLOCK_GROUP_RAID10)) {
5492 bbio->stripes[i].length = stripes_per_dev *
5493 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005494
Li Zefanec9ef7a2011-12-01 14:06:42 +08005495 if (i / sub_stripes < remaining_stripes)
5496 bbio->stripes[i].length +=
5497 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005498
5499 /*
5500 * Special for the first stripe and
5501 * the last stripe:
5502 *
5503 * |-------|...|-------|
5504 * |----------|
5505 * off end_off
5506 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005507 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005508 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005509 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005510
5511 if (stripe_index >= last_stripe &&
5512 stripe_index <= (last_stripe +
5513 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005514 bbio->stripes[i].length -=
5515 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005516
Li Zefanec9ef7a2011-12-01 14:06:42 +08005517 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005518 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005519 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005520 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005521
5522 stripe_index++;
5523 if (stripe_index == map->num_stripes) {
5524 /* This could only happen for RAID0/10 */
5525 stripe_index = 0;
5526 stripe_nr++;
5527 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005528 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005529 } else {
5530 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005531 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005532 map->stripes[stripe_index].physical +
5533 stripe_offset +
5534 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005535 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005536 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005537 stripe_index++;
5538 }
Chris Mason593060d2008-03-25 16:50:33 -04005539 }
Li Zefande11cc12011-12-01 12:55:47 +08005540
Miao Xied20983b2014-07-03 18:22:13 +08005541 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5542 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005543
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005544 if (bbio->raid_map)
5545 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005546
Miao Xie2c8cdd62014-11-14 16:06:25 +08005547 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005548 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5549 dev_replace->tgtdev != NULL) {
5550 int index_where_to_add;
5551 u64 srcdev_devid = dev_replace->srcdev->devid;
5552
5553 /*
5554 * duplicate the write operations while the dev replace
5555 * procedure is running. Since the copying of the old disk
5556 * to the new disk takes place at run time while the
5557 * filesystem is mounted writable, the regular write
5558 * operations to the old disk have to be duplicated to go
5559 * to the new disk as well.
5560 * Note that device->missing is handled by the caller, and
5561 * that the write to the old disk is already set up in the
5562 * stripes array.
5563 */
5564 index_where_to_add = num_stripes;
5565 for (i = 0; i < num_stripes; i++) {
5566 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5567 /* write to new disk, too */
5568 struct btrfs_bio_stripe *new =
5569 bbio->stripes + index_where_to_add;
5570 struct btrfs_bio_stripe *old =
5571 bbio->stripes + i;
5572
5573 new->physical = old->physical;
5574 new->length = old->length;
5575 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005576 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005577 index_where_to_add++;
5578 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005579 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005580 }
5581 }
5582 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005583 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5584 dev_replace->tgtdev != NULL) {
5585 u64 srcdev_devid = dev_replace->srcdev->devid;
5586 int index_srcdev = 0;
5587 int found = 0;
5588 u64 physical_of_found = 0;
5589
5590 /*
5591 * During the dev-replace procedure, the target drive can
5592 * also be used to read data in case it is needed to repair
5593 * a corrupt block elsewhere. This is possible if the
5594 * requested area is left of the left cursor. In this area,
5595 * the target drive is a full copy of the source drive.
5596 */
5597 for (i = 0; i < num_stripes; i++) {
5598 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5599 /*
5600 * In case of DUP, in order to keep it
5601 * simple, only add the mirror with the
5602 * lowest physical address
5603 */
5604 if (found &&
5605 physical_of_found <=
5606 bbio->stripes[i].physical)
5607 continue;
5608 index_srcdev = i;
5609 found = 1;
5610 physical_of_found = bbio->stripes[i].physical;
5611 }
5612 }
5613 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005614 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005615 dev_replace->cursor_left) {
5616 struct btrfs_bio_stripe *tgtdev_stripe =
5617 bbio->stripes + num_stripes;
5618
5619 tgtdev_stripe->physical = physical_of_found;
5620 tgtdev_stripe->length =
5621 bbio->stripes[index_srcdev].length;
5622 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005623 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005624
Miao Xie2c8cdd62014-11-14 16:06:25 +08005625 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005626 num_stripes++;
5627 }
5628 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005629 }
5630
Li Zefande11cc12011-12-01 12:55:47 +08005631 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005632 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005633 bbio->num_stripes = num_stripes;
5634 bbio->max_errors = max_errors;
5635 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005636 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005637
5638 /*
5639 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5640 * mirror_num == num_stripes + 1 && dev_replace target drive is
5641 * available as a mirror
5642 */
5643 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5644 WARN_ON(num_stripes > 1);
5645 bbio->stripes[0].dev = dev_replace->tgtdev;
5646 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5647 bbio->mirror_num = map->num_stripes + 1;
5648 }
Chris Masoncea9e442008-04-09 16:28:12 -04005649out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005650 if (dev_replace_is_ongoing)
5651 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005652 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005653 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005654}
5655
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005656int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005657 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005658 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005659{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005660 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005661 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005662}
5663
Miao Xieaf8e2d12014-10-23 14:42:50 +08005664/* For Scrub/replace */
5665int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5666 u64 logical, u64 *length,
5667 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005668 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005669{
5670 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005671 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005672}
5673
Yan Zhenga512bbf2008-12-08 16:46:26 -05005674int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5675 u64 chunk_start, u64 physical, u64 devid,
5676 u64 **logical, int *naddrs, int *stripe_len)
5677{
5678 struct extent_map_tree *em_tree = &map_tree->map_tree;
5679 struct extent_map *em;
5680 struct map_lookup *map;
5681 u64 *buf;
5682 u64 bytenr;
5683 u64 length;
5684 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005685 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005686 int i, j, nr = 0;
5687
Chris Mason890871b2009-09-02 16:24:52 -04005688 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005689 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005690 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005691
Josef Bacik835d9742013-03-19 12:13:25 -04005692 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005693 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005694 chunk_start);
5695 return -EIO;
5696 }
5697
5698 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005699 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005700 em->start, chunk_start);
5701 free_extent_map(em);
5702 return -EIO;
5703 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005704 map = (struct map_lookup *)em->bdev;
5705
5706 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005707 rmap_len = map->stripe_len;
5708
Yan Zhenga512bbf2008-12-08 16:46:26 -05005709 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005710 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005711 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005712 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005713 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005714 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005715 rmap_len = map->stripe_len * nr_data_stripes(map);
5716 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005717
David Sterba31e818f2015-02-20 18:00:26 +01005718 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005719 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005720
5721 for (i = 0; i < map->num_stripes; i++) {
5722 if (devid && map->stripes[i].dev->devid != devid)
5723 continue;
5724 if (map->stripes[i].physical > physical ||
5725 map->stripes[i].physical + length <= physical)
5726 continue;
5727
5728 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005729 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005730
5731 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5732 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005733 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005734 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5735 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005736 } /* else if RAID[56], multiply by nr_data_stripes().
5737 * Alternatively, just use rmap_len below instead of
5738 * map->stripe_len */
5739
5740 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005741 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005742 for (j = 0; j < nr; j++) {
5743 if (buf[j] == bytenr)
5744 break;
5745 }
Chris Mason934d3752008-12-08 16:43:10 -05005746 if (j == nr) {
5747 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005748 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005749 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005750 }
5751
Yan Zhenga512bbf2008-12-08 16:46:26 -05005752 *logical = buf;
5753 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005754 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005755
5756 free_extent_map(em);
5757 return 0;
5758}
5759
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005760static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005761{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005762 bio->bi_private = bbio->private;
5763 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005764 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005765
Zhao Lei6e9606d2015-01-20 15:11:34 +08005766 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005767}
5768
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005769static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005770{
Chris Mason9be33952013-05-17 18:30:14 -04005771 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005772 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005773
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005774 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005775 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005776 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005777 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005778 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005779 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005780
5781 BUG_ON(stripe_index >= bbio->num_stripes);
5782 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005783 if (dev->bdev) {
5784 if (bio->bi_rw & WRITE)
5785 btrfs_dev_stat_inc(dev,
5786 BTRFS_DEV_STAT_WRITE_ERRS);
5787 else
5788 btrfs_dev_stat_inc(dev,
5789 BTRFS_DEV_STAT_READ_ERRS);
5790 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5791 btrfs_dev_stat_inc(dev,
5792 BTRFS_DEV_STAT_FLUSH_ERRS);
5793 btrfs_dev_stat_print_on_error(dev);
5794 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005795 }
5796 }
Chris Mason8790d502008-04-03 16:29:03 -04005797
Jan Schmidta1d3c472011-08-04 17:15:33 +02005798 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005799 is_orig_bio = 1;
5800
Miao Xiec404e0d2014-01-30 16:46:55 +08005801 btrfs_bio_counter_dec(bbio->fs_info);
5802
Jan Schmidta1d3c472011-08-04 17:15:33 +02005803 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005804 if (!is_orig_bio) {
5805 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005806 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005807 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005808
Chris Mason9be33952013-05-17 18:30:14 -04005809 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005810 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005811 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005812 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005813 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005814 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005815 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005816 /*
5817 * this bio is actually up to date, we didn't
5818 * go over the max number of errors
5819 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005820 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005821 }
Miao Xiec55f1392014-06-19 10:42:54 +08005822
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005823 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005824 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005825 bio_put(bio);
5826 }
Chris Mason8790d502008-04-03 16:29:03 -04005827}
5828
Chris Mason8b712842008-06-11 16:50:36 -04005829/*
5830 * see run_scheduled_bios for a description of why bios are collected for
5831 * async submit.
5832 *
5833 * This will add one bio to the pending list for a device and make sure
5834 * the work struct is scheduled.
5835 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005836static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5837 struct btrfs_device *device,
5838 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005839{
5840 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005841 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005842
David Woodhouse53b381b2013-01-29 18:40:14 -05005843 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005844 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005845 return;
5846 }
5847
Chris Mason8b712842008-06-11 16:50:36 -04005848 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005849 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005850 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005851 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005852 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005853 return;
Chris Mason8b712842008-06-11 16:50:36 -04005854 }
5855
5856 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005857 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005858 * higher layers. Otherwise, the async bio makes it appear we have
5859 * made progress against dirty pages when we've really just put it
5860 * on a queue for later
5861 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005862 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005863 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005864 bio->bi_next = NULL;
5865 bio->bi_rw |= rw;
5866
5867 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005868 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005869 pending_bios = &device->pending_sync_bios;
5870 else
5871 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005872
Chris Masonffbd5172009-04-20 15:50:09 -04005873 if (pending_bios->tail)
5874 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005875
Chris Masonffbd5172009-04-20 15:50:09 -04005876 pending_bios->tail = bio;
5877 if (!pending_bios->head)
5878 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005879 if (device->running_pending)
5880 should_queue = 0;
5881
5882 spin_unlock(&device->io_lock);
5883
5884 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005885 btrfs_queue_work(root->fs_info->submit_workers,
5886 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005887}
5888
Josef Bacikde1ee922012-10-19 16:50:56 -04005889static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5890 struct bio *bio, u64 physical, int dev_nr,
5891 int rw, int async)
5892{
5893 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5894
5895 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005896 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005897 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005898 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005899#ifdef DEBUG
5900 {
5901 struct rcu_string *name;
5902
5903 rcu_read_lock();
5904 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005905 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005906 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005907 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5908 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005909 rcu_read_unlock();
5910 }
5911#endif
5912 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005913
5914 btrfs_bio_counter_inc_noblocked(root->fs_info);
5915
Josef Bacikde1ee922012-10-19 16:50:56 -04005916 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005917 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005918 else
5919 btrfsic_submit_bio(rw, bio);
5920}
5921
Josef Bacikde1ee922012-10-19 16:50:56 -04005922static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5923{
5924 atomic_inc(&bbio->error);
5925 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005926 /* Shoud be the original bio. */
5927 WARN_ON(bio != bbio->orig_bio);
5928
Chris Mason9be33952013-05-17 18:30:14 -04005929 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005930 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005931 bio->bi_error = -EIO;
5932 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005933 }
5934}
5935
Chris Masonf1885912008-04-09 16:28:12 -04005936int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005937 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005938{
Chris Mason0b86a832008-03-24 15:01:56 -04005939 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005940 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005941 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005942 u64 length = 0;
5943 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005944 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005945 int dev_nr;
5946 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005947 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005948
Kent Overstreet4f024f32013-10-11 15:44:27 -07005949 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005950 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005951
Miao Xiec404e0d2014-01-30 16:46:55 +08005952 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005953 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005954 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005955 if (ret) {
5956 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005957 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005958 }
Chris Masoncea9e442008-04-09 16:28:12 -04005959
Jan Schmidta1d3c472011-08-04 17:15:33 +02005960 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005961 bbio->orig_bio = first_bio;
5962 bbio->private = first_bio->bi_private;
5963 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005964 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005965 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5966
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005967 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005968 /* In this case, map_length has been set to the length of
5969 a single stripe; not the whole write */
5970 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005971 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005972 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005973 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005974 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005975 }
Miao Xie42452152014-11-25 16:39:28 +08005976
Miao Xiec404e0d2014-01-30 16:46:55 +08005977 btrfs_bio_counter_dec(root->fs_info);
5978 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005979 }
5980
Chris Masoncea9e442008-04-09 16:28:12 -04005981 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005982 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005983 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005984 BUG();
5985 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005986
Zhao Lei08da7572015-02-12 15:42:16 +08005987 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005988 dev = bbio->stripes[dev_nr].dev;
5989 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5990 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005991 continue;
5992 }
5993
Jan Schmidta1d3c472011-08-04 17:15:33 +02005994 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005995 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005996 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04005997 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005998 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04005999
Josef Bacikde1ee922012-10-19 16:50:56 -04006000 submit_stripe_bio(root, bbio, bio,
6001 bbio->stripes[dev_nr].physical, dev_nr, rw,
6002 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006003 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006004 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006005 return 0;
6006}
6007
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006008struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006009 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006010{
Yan Zheng2b820322008-11-17 21:11:30 -05006011 struct btrfs_device *device;
6012 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006013
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006014 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006015 while (cur_devices) {
6016 if (!fsid ||
6017 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6018 device = __find_device(&cur_devices->devices,
6019 devid, uuid);
6020 if (device)
6021 return device;
6022 }
6023 cur_devices = cur_devices->seed;
6024 }
6025 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006026}
6027
Chris Masondfe25022008-05-13 13:46:40 -04006028static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006029 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006030 u64 devid, u8 *dev_uuid)
6031{
6032 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006033
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006034 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6035 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006036 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006037
6038 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006039 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006040 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006041
6042 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006043 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006044
Chris Masondfe25022008-05-13 13:46:40 -04006045 return device;
6046}
6047
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006048/**
6049 * btrfs_alloc_device - allocate struct btrfs_device
6050 * @fs_info: used only for generating a new devid, can be NULL if
6051 * devid is provided (i.e. @devid != NULL).
6052 * @devid: a pointer to devid for this device. If NULL a new devid
6053 * is generated.
6054 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6055 * is generated.
6056 *
6057 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6058 * on error. Returned struct is not linked onto any lists and can be
6059 * destroyed with kfree() right away.
6060 */
6061struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6062 const u64 *devid,
6063 const u8 *uuid)
6064{
6065 struct btrfs_device *dev;
6066 u64 tmp;
6067
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306068 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006069 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006070
6071 dev = __alloc_device();
6072 if (IS_ERR(dev))
6073 return dev;
6074
6075 if (devid)
6076 tmp = *devid;
6077 else {
6078 int ret;
6079
6080 ret = find_next_devid(fs_info, &tmp);
6081 if (ret) {
6082 kfree(dev);
6083 return ERR_PTR(ret);
6084 }
6085 }
6086 dev->devid = tmp;
6087
6088 if (uuid)
6089 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6090 else
6091 generate_random_uuid(dev->uuid);
6092
Liu Bo9e0af232014-08-15 23:36:53 +08006093 btrfs_init_work(&dev->work, btrfs_submit_helper,
6094 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006095
6096 return dev;
6097}
6098
Chris Mason0b86a832008-03-24 15:01:56 -04006099static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6100 struct extent_buffer *leaf,
6101 struct btrfs_chunk *chunk)
6102{
6103 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6104 struct map_lookup *map;
6105 struct extent_map *em;
6106 u64 logical;
6107 u64 length;
6108 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006109 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006110 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006111 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006112 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006113
Chris Masone17cade2008-04-15 15:41:47 -04006114 logical = key->offset;
6115 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006116
Chris Mason890871b2009-09-02 16:24:52 -04006117 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006118 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006119 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006120
6121 /* already mapped? */
6122 if (em && em->start <= logical && em->start + em->len > logical) {
6123 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006124 return 0;
6125 } else if (em) {
6126 free_extent_map(em);
6127 }
Chris Mason0b86a832008-03-24 15:01:56 -04006128
David Sterba172ddd62011-04-21 00:48:27 +02006129 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006130 if (!em)
6131 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006132 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6133 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006134 if (!map) {
6135 free_extent_map(em);
6136 return -ENOMEM;
6137 }
6138
Wang Shilong298a8f92014-06-19 10:42:52 +08006139 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006140 em->bdev = (struct block_device *)map;
6141 em->start = logical;
6142 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006143 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006144 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006145 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006146
Chris Mason593060d2008-03-25 16:50:33 -04006147 map->num_stripes = num_stripes;
6148 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6149 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6150 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6151 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6152 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006153 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006154 for (i = 0; i < num_stripes; i++) {
6155 map->stripes[i].physical =
6156 btrfs_stripe_offset_nr(leaf, chunk, i);
6157 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006158 read_extent_buffer(leaf, uuid, (unsigned long)
6159 btrfs_stripe_dev_uuid_nr(chunk, i),
6160 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006161 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6162 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006163 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006164 free_extent_map(em);
6165 return -EIO;
6166 }
Chris Masondfe25022008-05-13 13:46:40 -04006167 if (!map->stripes[i].dev) {
6168 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006169 add_missing_dev(root, root->fs_info->fs_devices,
6170 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006171 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006172 free_extent_map(em);
6173 return -EIO;
6174 }
Anand Jain816fceb2015-04-27 12:46:18 +08006175 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6176 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006177 }
6178 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006179 }
6180
Chris Mason890871b2009-09-02 16:24:52 -04006181 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006182 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006183 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006184 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006185 free_extent_map(em);
6186
6187 return 0;
6188}
6189
Jeff Mahoney143bede2012-03-01 14:56:26 +01006190static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006191 struct btrfs_dev_item *dev_item,
6192 struct btrfs_device *device)
6193{
6194 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006195
6196 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006197 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6198 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006199 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006200 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006201 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006202 device->type = btrfs_device_type(leaf, dev_item);
6203 device->io_align = btrfs_device_io_align(leaf, dev_item);
6204 device->io_width = btrfs_device_io_width(leaf, dev_item);
6205 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006206 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006207 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006208
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006209 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006210 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006211}
6212
Miao Xie5f375832014-09-03 21:35:46 +08006213static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6214 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006215{
6216 struct btrfs_fs_devices *fs_devices;
6217 int ret;
6218
Li Zefanb367e472011-12-07 11:38:24 +08006219 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006220
6221 fs_devices = root->fs_info->fs_devices->seed;
6222 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006223 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6224 return fs_devices;
6225
Yan Zheng2b820322008-11-17 21:11:30 -05006226 fs_devices = fs_devices->seed;
6227 }
6228
6229 fs_devices = find_fsid(fsid);
6230 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006231 if (!btrfs_test_opt(root, DEGRADED))
6232 return ERR_PTR(-ENOENT);
6233
6234 fs_devices = alloc_fs_devices(fsid);
6235 if (IS_ERR(fs_devices))
6236 return fs_devices;
6237
6238 fs_devices->seeding = 1;
6239 fs_devices->opened = 1;
6240 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006241 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006242
6243 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006244 if (IS_ERR(fs_devices))
6245 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006246
Christoph Hellwig97288f22008-12-02 06:36:09 -05006247 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006248 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006249 if (ret) {
6250 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006251 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006252 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006253 }
Yan Zheng2b820322008-11-17 21:11:30 -05006254
6255 if (!fs_devices->seeding) {
6256 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006257 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006258 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006259 goto out;
6260 }
6261
6262 fs_devices->seed = root->fs_info->fs_devices->seed;
6263 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006264out:
Miao Xie5f375832014-09-03 21:35:46 +08006265 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006266}
6267
Chris Mason0d81ba52008-03-24 15:02:07 -04006268static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006269 struct extent_buffer *leaf,
6270 struct btrfs_dev_item *dev_item)
6271{
Miao Xie5f375832014-09-03 21:35:46 +08006272 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006273 struct btrfs_device *device;
6274 u64 devid;
6275 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006276 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006277 u8 dev_uuid[BTRFS_UUID_SIZE];
6278
Chris Mason0b86a832008-03-24 15:01:56 -04006279 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006280 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006281 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006282 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006283 BTRFS_UUID_SIZE);
6284
6285 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006286 fs_devices = open_seed_devices(root, fs_uuid);
6287 if (IS_ERR(fs_devices))
6288 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006289 }
6290
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006291 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006292 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006293 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006294 return -EIO;
6295
Miao Xie5f375832014-09-03 21:35:46 +08006296 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6297 if (!device)
6298 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006299 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6300 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006301 } else {
6302 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6303 return -EIO;
6304
6305 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006306 /*
6307 * this happens when a device that was properly setup
6308 * in the device info lists suddenly goes bad.
6309 * device->bdev is NULL, and so we have to set
6310 * device->missing to one here
6311 */
Miao Xie5f375832014-09-03 21:35:46 +08006312 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006313 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006314 }
Miao Xie5f375832014-09-03 21:35:46 +08006315
6316 /* Move the device to its own fs_devices */
6317 if (device->fs_devices != fs_devices) {
6318 ASSERT(device->missing);
6319
6320 list_move(&device->dev_list, &fs_devices->devices);
6321 device->fs_devices->num_devices--;
6322 fs_devices->num_devices++;
6323
6324 device->fs_devices->missing_devices--;
6325 fs_devices->missing_devices++;
6326
6327 device->fs_devices = fs_devices;
6328 }
Yan Zheng2b820322008-11-17 21:11:30 -05006329 }
6330
6331 if (device->fs_devices != root->fs_info->fs_devices) {
6332 BUG_ON(device->writeable);
6333 if (device->generation !=
6334 btrfs_device_generation(leaf, dev_item))
6335 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006336 }
Chris Mason0b86a832008-03-24 15:01:56 -04006337
6338 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006339 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006340 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006341 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006342 spin_lock(&root->fs_info->free_chunk_lock);
6343 root->fs_info->free_chunk_space += device->total_bytes -
6344 device->bytes_used;
6345 spin_unlock(&root->fs_info->free_chunk_lock);
6346 }
Chris Mason0b86a832008-03-24 15:01:56 -04006347 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006348 return ret;
6349}
6350
Yan Zhenge4404d62008-12-12 10:03:26 -05006351int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006352{
David Sterba6c417612011-04-13 15:41:04 +02006353 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006354 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006355 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006356 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006357 u8 *array_ptr;
6358 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006359 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006360 u32 num_stripes;
6361 u32 array_size;
6362 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006363 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006364 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006365
David Sterbaa83fffb2014-06-15 02:39:54 +02006366 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6367 /*
6368 * This will create extent buffer of nodesize, superblock size is
6369 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6370 * overallocate but we can keep it as-is, only the first page is used.
6371 */
6372 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006373 if (!sb)
6374 return -ENOMEM;
6375 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006376 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006377 /*
6378 * The sb extent buffer is artifical and just used to read the system array.
6379 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6380 * pages up-to-date when the page is larger: extent does not cover the
6381 * whole page and consequently check_page_uptodate does not find all
6382 * the page's extents up-to-date (the hole beyond sb),
6383 * write_extent_buffer then triggers a WARN_ON.
6384 *
6385 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6386 * but sb spans only this function. Add an explicit SetPageUptodate call
6387 * to silence the warning eg. on PowerPC 64.
6388 */
6389 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006390 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006391
Chris Masona061fc82008-05-07 11:43:44 -04006392 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006393 array_size = btrfs_super_sys_array_size(super_copy);
6394
David Sterba1ffb22c2014-10-31 19:02:42 +01006395 array_ptr = super_copy->sys_chunk_array;
6396 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6397 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006398
David Sterba1ffb22c2014-10-31 19:02:42 +01006399 while (cur_offset < array_size) {
6400 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006401 len = sizeof(*disk_key);
6402 if (cur_offset + len > array_size)
6403 goto out_short_read;
6404
Chris Mason0b86a832008-03-24 15:01:56 -04006405 btrfs_disk_key_to_cpu(&key, disk_key);
6406
David Sterba1ffb22c2014-10-31 19:02:42 +01006407 array_ptr += len;
6408 sb_array_offset += len;
6409 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006410
Chris Mason0d81ba52008-03-24 15:02:07 -04006411 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006412 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006413 /*
6414 * At least one btrfs_chunk with one stripe must be
6415 * present, exact stripe count check comes afterwards
6416 */
6417 len = btrfs_chunk_item_size(1);
6418 if (cur_offset + len > array_size)
6419 goto out_short_read;
6420
6421 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6422 len = btrfs_chunk_item_size(num_stripes);
6423 if (cur_offset + len > array_size)
6424 goto out_short_read;
6425
Chris Mason0d81ba52008-03-24 15:02:07 -04006426 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006427 if (ret)
6428 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006429 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006430 ret = -EIO;
6431 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006432 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006433 array_ptr += len;
6434 sb_array_offset += len;
6435 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006436 }
Chris Masona061fc82008-05-07 11:43:44 -04006437 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006438 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006439
6440out_short_read:
6441 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6442 len, cur_offset);
6443 free_extent_buffer(sb);
6444 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006445}
6446
6447int btrfs_read_chunk_tree(struct btrfs_root *root)
6448{
6449 struct btrfs_path *path;
6450 struct extent_buffer *leaf;
6451 struct btrfs_key key;
6452 struct btrfs_key found_key;
6453 int ret;
6454 int slot;
6455
6456 root = root->fs_info->chunk_root;
6457
6458 path = btrfs_alloc_path();
6459 if (!path)
6460 return -ENOMEM;
6461
Li Zefanb367e472011-12-07 11:38:24 +08006462 mutex_lock(&uuid_mutex);
6463 lock_chunks(root);
6464
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006465 /*
6466 * Read all device items, and then all the chunk items. All
6467 * device items are found before any chunk item (their object id
6468 * is smaller than the lowest possible object id for a chunk
6469 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006470 */
6471 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6472 key.offset = 0;
6473 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006474 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006475 if (ret < 0)
6476 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006477 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006478 leaf = path->nodes[0];
6479 slot = path->slots[0];
6480 if (slot >= btrfs_header_nritems(leaf)) {
6481 ret = btrfs_next_leaf(root, path);
6482 if (ret == 0)
6483 continue;
6484 if (ret < 0)
6485 goto error;
6486 break;
6487 }
6488 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006489 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6490 struct btrfs_dev_item *dev_item;
6491 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006492 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006493 ret = read_one_dev(root, leaf, dev_item);
6494 if (ret)
6495 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006496 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6497 struct btrfs_chunk *chunk;
6498 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6499 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006500 if (ret)
6501 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006502 }
6503 path->slots[0]++;
6504 }
Chris Mason0b86a832008-03-24 15:01:56 -04006505 ret = 0;
6506error:
Li Zefanb367e472011-12-07 11:38:24 +08006507 unlock_chunks(root);
6508 mutex_unlock(&uuid_mutex);
6509
Yan Zheng2b820322008-11-17 21:11:30 -05006510 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006511 return ret;
6512}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006513
Miao Xiecb517ea2013-05-15 07:48:19 +00006514void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6515{
6516 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6517 struct btrfs_device *device;
6518
Liu Bo29cc83f2014-05-11 23:14:59 +08006519 while (fs_devices) {
6520 mutex_lock(&fs_devices->device_list_mutex);
6521 list_for_each_entry(device, &fs_devices->devices, dev_list)
6522 device->dev_root = fs_info->dev_root;
6523 mutex_unlock(&fs_devices->device_list_mutex);
6524
6525 fs_devices = fs_devices->seed;
6526 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006527}
6528
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006529static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6530{
6531 int i;
6532
6533 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6534 btrfs_dev_stat_reset(dev, i);
6535}
6536
6537int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6538{
6539 struct btrfs_key key;
6540 struct btrfs_key found_key;
6541 struct btrfs_root *dev_root = fs_info->dev_root;
6542 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6543 struct extent_buffer *eb;
6544 int slot;
6545 int ret = 0;
6546 struct btrfs_device *device;
6547 struct btrfs_path *path = NULL;
6548 int i;
6549
6550 path = btrfs_alloc_path();
6551 if (!path) {
6552 ret = -ENOMEM;
6553 goto out;
6554 }
6555
6556 mutex_lock(&fs_devices->device_list_mutex);
6557 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6558 int item_size;
6559 struct btrfs_dev_stats_item *ptr;
6560
6561 key.objectid = 0;
6562 key.type = BTRFS_DEV_STATS_KEY;
6563 key.offset = device->devid;
6564 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6565 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006566 __btrfs_reset_dev_stats(device);
6567 device->dev_stats_valid = 1;
6568 btrfs_release_path(path);
6569 continue;
6570 }
6571 slot = path->slots[0];
6572 eb = path->nodes[0];
6573 btrfs_item_key_to_cpu(eb, &found_key, slot);
6574 item_size = btrfs_item_size_nr(eb, slot);
6575
6576 ptr = btrfs_item_ptr(eb, slot,
6577 struct btrfs_dev_stats_item);
6578
6579 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6580 if (item_size >= (1 + i) * sizeof(__le64))
6581 btrfs_dev_stat_set(device, i,
6582 btrfs_dev_stats_value(eb, ptr, i));
6583 else
6584 btrfs_dev_stat_reset(device, i);
6585 }
6586
6587 device->dev_stats_valid = 1;
6588 btrfs_dev_stat_print_on_load(device);
6589 btrfs_release_path(path);
6590 }
6591 mutex_unlock(&fs_devices->device_list_mutex);
6592
6593out:
6594 btrfs_free_path(path);
6595 return ret < 0 ? ret : 0;
6596}
6597
6598static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6599 struct btrfs_root *dev_root,
6600 struct btrfs_device *device)
6601{
6602 struct btrfs_path *path;
6603 struct btrfs_key key;
6604 struct extent_buffer *eb;
6605 struct btrfs_dev_stats_item *ptr;
6606 int ret;
6607 int i;
6608
6609 key.objectid = 0;
6610 key.type = BTRFS_DEV_STATS_KEY;
6611 key.offset = device->devid;
6612
6613 path = btrfs_alloc_path();
6614 BUG_ON(!path);
6615 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6616 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006617 printk_in_rcu(KERN_WARNING "BTRFS: "
6618 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006619 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006620 goto out;
6621 }
6622
6623 if (ret == 0 &&
6624 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6625 /* need to delete old one and insert a new one */
6626 ret = btrfs_del_item(trans, dev_root, path);
6627 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006628 printk_in_rcu(KERN_WARNING "BTRFS: "
6629 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006630 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006631 goto out;
6632 }
6633 ret = 1;
6634 }
6635
6636 if (ret == 1) {
6637 /* need to insert a new item */
6638 btrfs_release_path(path);
6639 ret = btrfs_insert_empty_item(trans, dev_root, path,
6640 &key, sizeof(*ptr));
6641 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006642 printk_in_rcu(KERN_WARNING "BTRFS: "
6643 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006644 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006645 goto out;
6646 }
6647 }
6648
6649 eb = path->nodes[0];
6650 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6651 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6652 btrfs_set_dev_stats_value(eb, ptr, i,
6653 btrfs_dev_stat_read(device, i));
6654 btrfs_mark_buffer_dirty(eb);
6655
6656out:
6657 btrfs_free_path(path);
6658 return ret;
6659}
6660
6661/*
6662 * called from commit_transaction. Writes all changed device stats to disk.
6663 */
6664int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6665 struct btrfs_fs_info *fs_info)
6666{
6667 struct btrfs_root *dev_root = fs_info->dev_root;
6668 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6669 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006670 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006671 int ret = 0;
6672
6673 mutex_lock(&fs_devices->device_list_mutex);
6674 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006675 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006676 continue;
6677
Miao Xieaddc3fa2014-07-24 11:37:11 +08006678 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006679 ret = update_dev_stat_item(trans, dev_root, device);
6680 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006681 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006682 }
6683 mutex_unlock(&fs_devices->device_list_mutex);
6684
6685 return ret;
6686}
6687
Stefan Behrens442a4f62012-05-25 16:06:08 +02006688void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6689{
6690 btrfs_dev_stat_inc(dev, index);
6691 btrfs_dev_stat_print_on_error(dev);
6692}
6693
Eric Sandeen48a3b632013-04-25 20:41:01 +00006694static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006695{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006696 if (!dev->dev_stats_valid)
6697 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006698 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6699 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006700 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006701 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6702 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6703 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006704 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6705 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006706}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006707
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006708static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6709{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006710 int i;
6711
6712 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6713 if (btrfs_dev_stat_read(dev, i) != 0)
6714 break;
6715 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6716 return; /* all values == 0, suppress message */
6717
Frank Holtonefe120a2013-12-20 11:37:06 -05006718 printk_in_rcu(KERN_INFO "BTRFS: "
6719 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006720 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006721 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6722 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6723 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6724 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6725 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6726}
6727
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006728int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006729 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006730{
6731 struct btrfs_device *dev;
6732 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6733 int i;
6734
6735 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006736 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006737 mutex_unlock(&fs_devices->device_list_mutex);
6738
6739 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006740 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006741 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006742 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006743 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006744 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006745 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006746 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6747 if (stats->nr_items > i)
6748 stats->values[i] =
6749 btrfs_dev_stat_read_and_reset(dev, i);
6750 else
6751 btrfs_dev_stat_reset(dev, i);
6752 }
6753 } else {
6754 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6755 if (stats->nr_items > i)
6756 stats->values[i] = btrfs_dev_stat_read(dev, i);
6757 }
6758 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6759 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6760 return 0;
6761}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006762
6763int btrfs_scratch_superblock(struct btrfs_device *device)
6764{
6765 struct buffer_head *bh;
6766 struct btrfs_super_block *disk_super;
6767
6768 bh = btrfs_read_dev_super(device->bdev);
6769 if (!bh)
6770 return -EINVAL;
6771 disk_super = (struct btrfs_super_block *)bh->b_data;
6772
6773 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6774 set_buffer_dirty(bh);
6775 sync_dirty_buffer(bh);
6776 brelse(bh);
6777
6778 return 0;
6779}
Miao Xie935e5cc2014-09-03 21:35:33 +08006780
6781/*
6782 * Update the size of all devices, which is used for writing out the
6783 * super blocks.
6784 */
6785void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6786{
6787 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6788 struct btrfs_device *curr, *next;
6789
6790 if (list_empty(&fs_devices->resized_devices))
6791 return;
6792
6793 mutex_lock(&fs_devices->device_list_mutex);
6794 lock_chunks(fs_info->dev_root);
6795 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6796 resized_list) {
6797 list_del_init(&curr->resized_list);
6798 curr->commit_total_bytes = curr->disk_total_bytes;
6799 }
6800 unlock_chunks(fs_info->dev_root);
6801 mutex_unlock(&fs_devices->device_list_mutex);
6802}
Miao Xiece7213c2014-09-03 21:35:34 +08006803
6804/* Must be invoked during the transaction commit */
6805void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6806 struct btrfs_transaction *transaction)
6807{
6808 struct extent_map *em;
6809 struct map_lookup *map;
6810 struct btrfs_device *dev;
6811 int i;
6812
6813 if (list_empty(&transaction->pending_chunks))
6814 return;
6815
6816 /* In order to kick the device replace finish process */
6817 lock_chunks(root);
6818 list_for_each_entry(em, &transaction->pending_chunks, list) {
6819 map = (struct map_lookup *)em->bdev;
6820
6821 for (i = 0; i < map->num_stripes; i++) {
6822 dev = map->stripes[i].dev;
6823 dev->commit_bytes_used = dev->bytes_used;
6824 }
6825 }
6826 unlock_chunks(root);
6827}
Anand Jain5a13f432015-03-10 06:38:31 +08006828
6829void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6830{
6831 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6832 while (fs_devices) {
6833 fs_devices->fs_info = fs_info;
6834 fs_devices = fs_devices->seed;
6835 }
6836}
6837
6838void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6839{
6840 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6841 while (fs_devices) {
6842 fs_devices->fs_info = NULL;
6843 fs_devices = fs_devices->seed;
6844 }
6845}