blob: 7b533909d00621fe2083a6fd1e81de55b56449d3 [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 Wenruoa8c93d42014-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
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003545/*
3546 * Should be called with both balance and volume mutexes held
3547 */
3548int btrfs_balance(struct btrfs_balance_control *bctl,
3549 struct btrfs_ioctl_balance_args *bargs)
3550{
3551 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003552 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003553 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003554 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003555 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003556 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003557
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003558 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003559 atomic_read(&fs_info->balance_pause_req) ||
3560 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003561 ret = -EINVAL;
3562 goto out;
3563 }
3564
Ilya Dryomove4837f82012-03-27 17:09:17 +03003565 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3566 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3567 mixed = 1;
3568
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003569 /*
3570 * In case of mixed groups both data and meta should be picked,
3571 * and identical options should be given for both of them.
3572 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003573 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3574 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003575 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3576 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3577 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003578 btrfs_err(fs_info, "with mixed groups data and "
3579 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003580 ret = -EINVAL;
3581 goto out;
3582 }
3583 }
3584
Stefan Behrens8dabb742012-11-06 13:15:27 +01003585 num_devices = fs_info->fs_devices->num_devices;
3586 btrfs_dev_replace_lock(&fs_info->dev_replace);
3587 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3588 BUG_ON(num_devices < 1);
3589 num_devices--;
3590 }
3591 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003592 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003593 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003594 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003595 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003596 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003597 if (num_devices > 2)
3598 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3599 if (num_devices > 3)
3600 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3601 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003602 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3603 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3604 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003605 btrfs_err(fs_info, "unable to start balance with target "
3606 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003607 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003608 ret = -EINVAL;
3609 goto out;
3610 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003611 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3612 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3613 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003614 btrfs_err(fs_info,
3615 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003616 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003617 ret = -EINVAL;
3618 goto out;
3619 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003620 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3621 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3622 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003623 btrfs_err(fs_info,
3624 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003625 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003626 ret = -EINVAL;
3627 goto out;
3628 }
3629
Ilya Dryomove4837f82012-03-27 17:09:17 +03003630 /* allow dup'ed data chunks only in mixed mode */
3631 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003632 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003633 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003634 ret = -EINVAL;
3635 goto out;
3636 }
3637
3638 /* allow to reduce meta or sys integrity only if force set */
3639 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003640 BTRFS_BLOCK_GROUP_RAID10 |
3641 BTRFS_BLOCK_GROUP_RAID5 |
3642 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003643 do {
3644 seq = read_seqbegin(&fs_info->profiles_lock);
3645
3646 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3647 (fs_info->avail_system_alloc_bits & allowed) &&
3648 !(bctl->sys.target & allowed)) ||
3649 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3650 (fs_info->avail_metadata_alloc_bits & allowed) &&
3651 !(bctl->meta.target & allowed))) {
3652 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003653 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003654 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003655 btrfs_err(fs_info, "balance will reduce metadata "
3656 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003657 ret = -EINVAL;
3658 goto out;
3659 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003660 }
Miao Xiede98ced2013-01-29 10:13:12 +00003661 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003662
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003663 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003664 fs_info->num_tolerated_disk_barrier_failures = min(
3665 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3666 btrfs_get_num_tolerated_disk_barrier_failures(
3667 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003668 }
3669
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003670 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003671 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003672 goto out;
3673
Ilya Dryomov59641012012-01-16 22:04:48 +02003674 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3675 BUG_ON(ret == -EEXIST);
3676 set_balance_control(bctl);
3677 } else {
3678 BUG_ON(ret != -EEXIST);
3679 spin_lock(&fs_info->balance_lock);
3680 update_balance_args(bctl);
3681 spin_unlock(&fs_info->balance_lock);
3682 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003683
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003684 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003685 mutex_unlock(&fs_info->balance_mutex);
3686
3687 ret = __btrfs_balance(fs_info);
3688
3689 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003690 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003691
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003692 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3693 fs_info->num_tolerated_disk_barrier_failures =
3694 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3695 }
3696
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003697 if (bargs) {
3698 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003699 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003700 }
3701
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003702 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3703 balance_need_close(fs_info)) {
3704 __cancel_balance(fs_info);
3705 }
3706
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003707 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003708
3709 return ret;
3710out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003711 if (bctl->flags & BTRFS_BALANCE_RESUME)
3712 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003713 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003714 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003715 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3716 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003717 return ret;
3718}
3719
3720static int balance_kthread(void *data)
3721{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003722 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003723 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003724
3725 mutex_lock(&fs_info->volume_mutex);
3726 mutex_lock(&fs_info->balance_mutex);
3727
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003728 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003729 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003730 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003731 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003732
3733 mutex_unlock(&fs_info->balance_mutex);
3734 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003735
Ilya Dryomov59641012012-01-16 22:04:48 +02003736 return ret;
3737}
3738
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003739int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3740{
3741 struct task_struct *tsk;
3742
3743 spin_lock(&fs_info->balance_lock);
3744 if (!fs_info->balance_ctl) {
3745 spin_unlock(&fs_info->balance_lock);
3746 return 0;
3747 }
3748 spin_unlock(&fs_info->balance_lock);
3749
3750 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003751 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003752 return 0;
3753 }
3754
3755 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303756 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003757}
3758
Ilya Dryomov68310a52012-06-22 12:24:12 -06003759int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003760{
Ilya Dryomov59641012012-01-16 22:04:48 +02003761 struct btrfs_balance_control *bctl;
3762 struct btrfs_balance_item *item;
3763 struct btrfs_disk_balance_args disk_bargs;
3764 struct btrfs_path *path;
3765 struct extent_buffer *leaf;
3766 struct btrfs_key key;
3767 int ret;
3768
3769 path = btrfs_alloc_path();
3770 if (!path)
3771 return -ENOMEM;
3772
Ilya Dryomov68310a52012-06-22 12:24:12 -06003773 key.objectid = BTRFS_BALANCE_OBJECTID;
3774 key.type = BTRFS_BALANCE_ITEM_KEY;
3775 key.offset = 0;
3776
3777 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3778 if (ret < 0)
3779 goto out;
3780 if (ret > 0) { /* ret = -ENOENT; */
3781 ret = 0;
3782 goto out;
3783 }
3784
Ilya Dryomov59641012012-01-16 22:04:48 +02003785 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3786 if (!bctl) {
3787 ret = -ENOMEM;
3788 goto out;
3789 }
3790
Ilya Dryomov59641012012-01-16 22:04:48 +02003791 leaf = path->nodes[0];
3792 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3793
Ilya Dryomov68310a52012-06-22 12:24:12 -06003794 bctl->fs_info = fs_info;
3795 bctl->flags = btrfs_balance_flags(leaf, item);
3796 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003797
3798 btrfs_balance_data(leaf, item, &disk_bargs);
3799 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3800 btrfs_balance_meta(leaf, item, &disk_bargs);
3801 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3802 btrfs_balance_sys(leaf, item, &disk_bargs);
3803 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3804
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003805 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3806
Ilya Dryomov68310a52012-06-22 12:24:12 -06003807 mutex_lock(&fs_info->volume_mutex);
3808 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003809
Ilya Dryomov68310a52012-06-22 12:24:12 -06003810 set_balance_control(bctl);
3811
3812 mutex_unlock(&fs_info->balance_mutex);
3813 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003814out:
3815 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003816 return ret;
3817}
3818
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003819int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3820{
3821 int ret = 0;
3822
3823 mutex_lock(&fs_info->balance_mutex);
3824 if (!fs_info->balance_ctl) {
3825 mutex_unlock(&fs_info->balance_mutex);
3826 return -ENOTCONN;
3827 }
3828
3829 if (atomic_read(&fs_info->balance_running)) {
3830 atomic_inc(&fs_info->balance_pause_req);
3831 mutex_unlock(&fs_info->balance_mutex);
3832
3833 wait_event(fs_info->balance_wait_q,
3834 atomic_read(&fs_info->balance_running) == 0);
3835
3836 mutex_lock(&fs_info->balance_mutex);
3837 /* we are good with balance_ctl ripped off from under us */
3838 BUG_ON(atomic_read(&fs_info->balance_running));
3839 atomic_dec(&fs_info->balance_pause_req);
3840 } else {
3841 ret = -ENOTCONN;
3842 }
3843
3844 mutex_unlock(&fs_info->balance_mutex);
3845 return ret;
3846}
3847
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003848int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3849{
Ilya Dryomove649e582013-10-10 20:40:21 +03003850 if (fs_info->sb->s_flags & MS_RDONLY)
3851 return -EROFS;
3852
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003853 mutex_lock(&fs_info->balance_mutex);
3854 if (!fs_info->balance_ctl) {
3855 mutex_unlock(&fs_info->balance_mutex);
3856 return -ENOTCONN;
3857 }
3858
3859 atomic_inc(&fs_info->balance_cancel_req);
3860 /*
3861 * if we are running just wait and return, balance item is
3862 * deleted in btrfs_balance in this case
3863 */
3864 if (atomic_read(&fs_info->balance_running)) {
3865 mutex_unlock(&fs_info->balance_mutex);
3866 wait_event(fs_info->balance_wait_q,
3867 atomic_read(&fs_info->balance_running) == 0);
3868 mutex_lock(&fs_info->balance_mutex);
3869 } else {
3870 /* __cancel_balance needs volume_mutex */
3871 mutex_unlock(&fs_info->balance_mutex);
3872 mutex_lock(&fs_info->volume_mutex);
3873 mutex_lock(&fs_info->balance_mutex);
3874
3875 if (fs_info->balance_ctl)
3876 __cancel_balance(fs_info);
3877
3878 mutex_unlock(&fs_info->volume_mutex);
3879 }
3880
3881 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3882 atomic_dec(&fs_info->balance_cancel_req);
3883 mutex_unlock(&fs_info->balance_mutex);
3884 return 0;
3885}
3886
Stefan Behrens803b2f52013-08-15 17:11:21 +02003887static int btrfs_uuid_scan_kthread(void *data)
3888{
3889 struct btrfs_fs_info *fs_info = data;
3890 struct btrfs_root *root = fs_info->tree_root;
3891 struct btrfs_key key;
3892 struct btrfs_key max_key;
3893 struct btrfs_path *path = NULL;
3894 int ret = 0;
3895 struct extent_buffer *eb;
3896 int slot;
3897 struct btrfs_root_item root_item;
3898 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003899 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003900
3901 path = btrfs_alloc_path();
3902 if (!path) {
3903 ret = -ENOMEM;
3904 goto out;
3905 }
3906
3907 key.objectid = 0;
3908 key.type = BTRFS_ROOT_ITEM_KEY;
3909 key.offset = 0;
3910
3911 max_key.objectid = (u64)-1;
3912 max_key.type = BTRFS_ROOT_ITEM_KEY;
3913 max_key.offset = (u64)-1;
3914
Stefan Behrens803b2f52013-08-15 17:11:21 +02003915 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003916 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003917 if (ret) {
3918 if (ret > 0)
3919 ret = 0;
3920 break;
3921 }
3922
3923 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3924 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3925 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3926 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3927 goto skip;
3928
3929 eb = path->nodes[0];
3930 slot = path->slots[0];
3931 item_size = btrfs_item_size_nr(eb, slot);
3932 if (item_size < sizeof(root_item))
3933 goto skip;
3934
Stefan Behrens803b2f52013-08-15 17:11:21 +02003935 read_extent_buffer(eb, &root_item,
3936 btrfs_item_ptr_offset(eb, slot),
3937 (int)sizeof(root_item));
3938 if (btrfs_root_refs(&root_item) == 0)
3939 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003940
3941 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3942 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3943 if (trans)
3944 goto update_tree;
3945
3946 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003947 /*
3948 * 1 - subvol uuid item
3949 * 1 - received_subvol uuid item
3950 */
3951 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3952 if (IS_ERR(trans)) {
3953 ret = PTR_ERR(trans);
3954 break;
3955 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003956 continue;
3957 } else {
3958 goto skip;
3959 }
3960update_tree:
3961 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003962 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3963 root_item.uuid,
3964 BTRFS_UUID_KEY_SUBVOL,
3965 key.objectid);
3966 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003967 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003968 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003969 break;
3970 }
3971 }
3972
3973 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003974 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3975 root_item.received_uuid,
3976 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3977 key.objectid);
3978 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003979 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003980 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003981 break;
3982 }
3983 }
3984
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003985skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003986 if (trans) {
3987 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003988 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003989 if (ret)
3990 break;
3991 }
3992
Stefan Behrens803b2f52013-08-15 17:11:21 +02003993 btrfs_release_path(path);
3994 if (key.offset < (u64)-1) {
3995 key.offset++;
3996 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3997 key.offset = 0;
3998 key.type = BTRFS_ROOT_ITEM_KEY;
3999 } else if (key.objectid < (u64)-1) {
4000 key.offset = 0;
4001 key.type = BTRFS_ROOT_ITEM_KEY;
4002 key.objectid++;
4003 } else {
4004 break;
4005 }
4006 cond_resched();
4007 }
4008
4009out:
4010 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004011 if (trans && !IS_ERR(trans))
4012 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004013 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004014 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004015 else
4016 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004017 up(&fs_info->uuid_tree_rescan_sem);
4018 return 0;
4019}
4020
Stefan Behrens70f80172013-08-15 17:11:23 +02004021/*
4022 * Callback for btrfs_uuid_tree_iterate().
4023 * returns:
4024 * 0 check succeeded, the entry is not outdated.
4025 * < 0 if an error occured.
4026 * > 0 if the check failed, which means the caller shall remove the entry.
4027 */
4028static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4029 u8 *uuid, u8 type, u64 subid)
4030{
4031 struct btrfs_key key;
4032 int ret = 0;
4033 struct btrfs_root *subvol_root;
4034
4035 if (type != BTRFS_UUID_KEY_SUBVOL &&
4036 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4037 goto out;
4038
4039 key.objectid = subid;
4040 key.type = BTRFS_ROOT_ITEM_KEY;
4041 key.offset = (u64)-1;
4042 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4043 if (IS_ERR(subvol_root)) {
4044 ret = PTR_ERR(subvol_root);
4045 if (ret == -ENOENT)
4046 ret = 1;
4047 goto out;
4048 }
4049
4050 switch (type) {
4051 case BTRFS_UUID_KEY_SUBVOL:
4052 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4053 ret = 1;
4054 break;
4055 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4056 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4057 BTRFS_UUID_SIZE))
4058 ret = 1;
4059 break;
4060 }
4061
4062out:
4063 return ret;
4064}
4065
4066static int btrfs_uuid_rescan_kthread(void *data)
4067{
4068 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4069 int ret;
4070
4071 /*
4072 * 1st step is to iterate through the existing UUID tree and
4073 * to delete all entries that contain outdated data.
4074 * 2nd step is to add all missing entries to the UUID tree.
4075 */
4076 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4077 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004078 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004079 up(&fs_info->uuid_tree_rescan_sem);
4080 return ret;
4081 }
4082 return btrfs_uuid_scan_kthread(data);
4083}
4084
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004085int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4086{
4087 struct btrfs_trans_handle *trans;
4088 struct btrfs_root *tree_root = fs_info->tree_root;
4089 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004090 struct task_struct *task;
4091 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004092
4093 /*
4094 * 1 - root node
4095 * 1 - root item
4096 */
4097 trans = btrfs_start_transaction(tree_root, 2);
4098 if (IS_ERR(trans))
4099 return PTR_ERR(trans);
4100
4101 uuid_root = btrfs_create_tree(trans, fs_info,
4102 BTRFS_UUID_TREE_OBJECTID);
4103 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004104 ret = PTR_ERR(uuid_root);
4105 btrfs_abort_transaction(trans, tree_root, ret);
4106 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004107 }
4108
4109 fs_info->uuid_root = uuid_root;
4110
Stefan Behrens803b2f52013-08-15 17:11:21 +02004111 ret = btrfs_commit_transaction(trans, tree_root);
4112 if (ret)
4113 return ret;
4114
4115 down(&fs_info->uuid_tree_rescan_sem);
4116 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4117 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004118 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004119 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004120 up(&fs_info->uuid_tree_rescan_sem);
4121 return PTR_ERR(task);
4122 }
4123
4124 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004125}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004126
Stefan Behrens70f80172013-08-15 17:11:23 +02004127int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4128{
4129 struct task_struct *task;
4130
4131 down(&fs_info->uuid_tree_rescan_sem);
4132 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4133 if (IS_ERR(task)) {
4134 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004135 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004136 up(&fs_info->uuid_tree_rescan_sem);
4137 return PTR_ERR(task);
4138 }
4139
4140 return 0;
4141}
4142
Chris Mason8f18cf12008-04-25 16:53:30 -04004143/*
4144 * shrinking a device means finding all of the device extents past
4145 * the new size, and then following the back refs to the chunks.
4146 * The chunk relocation code actually frees the device extent
4147 */
4148int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4149{
4150 struct btrfs_trans_handle *trans;
4151 struct btrfs_root *root = device->dev_root;
4152 struct btrfs_dev_extent *dev_extent = NULL;
4153 struct btrfs_path *path;
4154 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004155 u64 chunk_offset;
4156 int ret;
4157 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004158 int failed = 0;
4159 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004160 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004161 struct extent_buffer *l;
4162 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004163 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004164 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004165 u64 old_size = btrfs_device_get_total_bytes(device);
4166 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004167
Stefan Behrens63a212a2012-11-05 18:29:28 +01004168 if (device->is_tgtdev_for_dev_replace)
4169 return -EINVAL;
4170
Chris Mason8f18cf12008-04-25 16:53:30 -04004171 path = btrfs_alloc_path();
4172 if (!path)
4173 return -ENOMEM;
4174
Chris Mason8f18cf12008-04-25 16:53:30 -04004175 path->reada = 2;
4176
Chris Mason7d9eb122008-07-08 14:19:17 -04004177 lock_chunks(root);
4178
Miao Xie7cc8e582014-09-03 21:35:38 +08004179 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004180 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004181 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004182 spin_lock(&root->fs_info->free_chunk_lock);
4183 root->fs_info->free_chunk_space -= diff;
4184 spin_unlock(&root->fs_info->free_chunk_lock);
4185 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004186 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004187
Josef Bacikba1bf482009-09-11 16:11:19 -04004188again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004189 key.objectid = device->devid;
4190 key.offset = (u64)-1;
4191 key.type = BTRFS_DEV_EXTENT_KEY;
4192
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004193 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004194 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004195 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004196 if (ret < 0) {
4197 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004198 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004199 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004200
4201 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004202 if (ret)
4203 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004204 if (ret < 0)
4205 goto done;
4206 if (ret) {
4207 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004208 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004209 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004210 }
4211
4212 l = path->nodes[0];
4213 slot = path->slots[0];
4214 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4215
Josef Bacikba1bf482009-09-11 16:11:19 -04004216 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004217 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004218 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004219 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004220 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004221
4222 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4223 length = btrfs_dev_extent_length(l, dev_extent);
4224
Josef Bacikba1bf482009-09-11 16:11:19 -04004225 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004226 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004227 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004228 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004229 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004230
Chris Mason8f18cf12008-04-25 16:53:30 -04004231 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004232 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004233
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004234 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004235 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004236 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004237 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004238 if (ret == -ENOSPC)
4239 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004240 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004241
4242 if (failed && !retried) {
4243 failed = 0;
4244 retried = true;
4245 goto again;
4246 } else if (failed && retried) {
4247 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004248 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004249 }
4250
Chris Balld6397ba2009-04-27 07:29:03 -04004251 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004252 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004253 if (IS_ERR(trans)) {
4254 ret = PTR_ERR(trans);
4255 goto done;
4256 }
4257
Chris Balld6397ba2009-04-27 07:29:03 -04004258 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004259
4260 /*
4261 * We checked in the above loop all device extents that were already in
4262 * the device tree. However before we have updated the device's
4263 * total_bytes to the new size, we might have had chunk allocations that
4264 * have not complete yet (new block groups attached to transaction
4265 * handles), and therefore their device extents were not yet in the
4266 * device tree and we missed them in the loop above. So if we have any
4267 * pending chunk using a device extent that overlaps the device range
4268 * that we can not use anymore, commit the current transaction and
4269 * repeat the search on the device tree - this way we guarantee we will
4270 * not have chunks using device extents that end beyond 'new_size'.
4271 */
4272 if (!checked_pending_chunks) {
4273 u64 start = new_size;
4274 u64 len = old_size - new_size;
4275
Jeff Mahoney499f3772015-06-15 09:41:17 -04004276 if (contains_pending_extent(trans->transaction, device,
4277 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004278 unlock_chunks(root);
4279 checked_pending_chunks = true;
4280 failed = 0;
4281 retried = false;
4282 ret = btrfs_commit_transaction(trans, root);
4283 if (ret)
4284 goto done;
4285 goto again;
4286 }
4287 }
4288
Miao Xie7cc8e582014-09-03 21:35:38 +08004289 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004290 if (list_empty(&device->resized_list))
4291 list_add_tail(&device->resized_list,
4292 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004293
Chris Balld6397ba2009-04-27 07:29:03 -04004294 WARN_ON(diff > old_total);
4295 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4296 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004297
4298 /* Now btrfs_update_device() will change the on-disk size. */
4299 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004300 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004301done:
4302 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004303 if (ret) {
4304 lock_chunks(root);
4305 btrfs_device_set_total_bytes(device, old_size);
4306 if (device->writeable)
4307 device->fs_devices->total_rw_bytes += diff;
4308 spin_lock(&root->fs_info->free_chunk_lock);
4309 root->fs_info->free_chunk_space += diff;
4310 spin_unlock(&root->fs_info->free_chunk_lock);
4311 unlock_chunks(root);
4312 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004313 return ret;
4314}
4315
Li Zefan125ccb02011-12-08 15:07:24 +08004316static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004317 struct btrfs_key *key,
4318 struct btrfs_chunk *chunk, int item_size)
4319{
David Sterba6c417612011-04-13 15:41:04 +02004320 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004321 struct btrfs_disk_key disk_key;
4322 u32 array_size;
4323 u8 *ptr;
4324
Miao Xiefe48a5c2014-09-03 21:35:39 +08004325 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004326 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004327 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004328 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4329 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004330 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004331 }
Chris Mason0b86a832008-03-24 15:01:56 -04004332
4333 ptr = super_copy->sys_chunk_array + array_size;
4334 btrfs_cpu_key_to_disk(&disk_key, key);
4335 memcpy(ptr, &disk_key, sizeof(disk_key));
4336 ptr += sizeof(disk_key);
4337 memcpy(ptr, chunk, item_size);
4338 item_size += sizeof(disk_key);
4339 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004340 unlock_chunks(root);
4341
Chris Mason0b86a832008-03-24 15:01:56 -04004342 return 0;
4343}
4344
Miao Xieb2117a32011-01-05 10:07:28 +00004345/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004346 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004347 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004348static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004349{
Arne Jansen73c5de02011-04-12 12:07:57 +02004350 const struct btrfs_device_info *di_a = a;
4351 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004352
Arne Jansen73c5de02011-04-12 12:07:57 +02004353 if (di_a->max_avail > di_b->max_avail)
4354 return -1;
4355 if (di_a->max_avail < di_b->max_avail)
4356 return 1;
4357 if (di_a->total_avail > di_b->total_avail)
4358 return -1;
4359 if (di_a->total_avail < di_b->total_avail)
4360 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004361 return 0;
4362}
4363
David Woodhouse53b381b2013-01-29 18:40:14 -05004364static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4365{
4366 /* TODO allow them to set a preferred stripe size */
4367 return 64 * 1024;
4368}
4369
4370static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4371{
Zhao Leiffe2d202015-01-20 15:11:44 +08004372 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004373 return;
4374
Miao Xieceda0862013-04-11 10:30:16 +00004375 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004376}
4377
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004378#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4379 - sizeof(struct btrfs_item) \
4380 - sizeof(struct btrfs_chunk)) \
4381 / sizeof(struct btrfs_stripe) + 1)
4382
4383#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4384 - 2 * sizeof(struct btrfs_disk_key) \
4385 - 2 * sizeof(struct btrfs_chunk)) \
4386 / sizeof(struct btrfs_stripe) + 1)
4387
Miao Xieb2117a32011-01-05 10:07:28 +00004388static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004389 struct btrfs_root *extent_root, u64 start,
4390 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004391{
4392 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004393 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4394 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004395 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004396 struct extent_map_tree *em_tree;
4397 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004398 struct btrfs_device_info *devices_info = NULL;
4399 u64 total_avail;
4400 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004401 int data_stripes; /* number of stripes that count for
4402 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004403 int sub_stripes; /* sub_stripes info for map */
4404 int dev_stripes; /* stripes per dev */
4405 int devs_max; /* max devs to use */
4406 int devs_min; /* min devs needed */
4407 int devs_increment; /* ndevs has to be a multiple of this */
4408 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004409 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004410 u64 max_stripe_size;
4411 u64 max_chunk_size;
4412 u64 stripe_size;
4413 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004414 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004415 int ndevs;
4416 int i;
4417 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004418 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004419
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004420 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004421
Miao Xieb2117a32011-01-05 10:07:28 +00004422 if (list_empty(&fs_devices->alloc_list))
4423 return -ENOSPC;
4424
Liu Bo31e50222012-11-21 14:18:10 +00004425 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004426
Liu Bo31e50222012-11-21 14:18:10 +00004427 sub_stripes = btrfs_raid_array[index].sub_stripes;
4428 dev_stripes = btrfs_raid_array[index].dev_stripes;
4429 devs_max = btrfs_raid_array[index].devs_max;
4430 devs_min = btrfs_raid_array[index].devs_min;
4431 devs_increment = btrfs_raid_array[index].devs_increment;
4432 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004433
4434 if (type & BTRFS_BLOCK_GROUP_DATA) {
4435 max_stripe_size = 1024 * 1024 * 1024;
4436 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004437 if (!devs_max)
4438 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004439 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004440 /* for larger filesystems, use larger metadata chunks */
4441 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4442 max_stripe_size = 1024 * 1024 * 1024;
4443 else
4444 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004445 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004446 if (!devs_max)
4447 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004448 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004449 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004450 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004451 if (!devs_max)
4452 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004453 } else {
David Sterba351fd352014-05-15 16:48:20 +02004454 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004455 type);
4456 BUG_ON(1);
4457 }
4458
4459 /* we don't want a chunk larger than 10% of writeable space */
4460 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4461 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004462
David Sterba31e818f2015-02-20 18:00:26 +01004463 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004464 GFP_NOFS);
4465 if (!devices_info)
4466 return -ENOMEM;
4467
Arne Jansen73c5de02011-04-12 12:07:57 +02004468 cur = fs_devices->alloc_list.next;
4469
4470 /*
4471 * in the first pass through the devices list, we gather information
4472 * about the available holes on each device.
4473 */
4474 ndevs = 0;
4475 while (cur != &fs_devices->alloc_list) {
4476 struct btrfs_device *device;
4477 u64 max_avail;
4478 u64 dev_offset;
4479
4480 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4481
4482 cur = cur->next;
4483
4484 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004485 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004486 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004487 continue;
4488 }
4489
Stefan Behrens63a212a2012-11-05 18:29:28 +01004490 if (!device->in_fs_metadata ||
4491 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004492 continue;
4493
4494 if (device->total_bytes > device->bytes_used)
4495 total_avail = device->total_bytes - device->bytes_used;
4496 else
4497 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004498
4499 /* If there is no space on this device, skip it. */
4500 if (total_avail == 0)
4501 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004502
Josef Bacik6df9a952013-06-27 13:22:46 -04004503 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004504 max_stripe_size * dev_stripes,
4505 &dev_offset, &max_avail);
4506 if (ret && ret != -ENOSPC)
4507 goto error;
4508
4509 if (ret == 0)
4510 max_avail = max_stripe_size * dev_stripes;
4511
4512 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4513 continue;
4514
Eric Sandeen063d0062013-01-31 00:55:01 +00004515 if (ndevs == fs_devices->rw_devices) {
4516 WARN(1, "%s: found more than %llu devices\n",
4517 __func__, fs_devices->rw_devices);
4518 break;
4519 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004520 devices_info[ndevs].dev_offset = dev_offset;
4521 devices_info[ndevs].max_avail = max_avail;
4522 devices_info[ndevs].total_avail = total_avail;
4523 devices_info[ndevs].dev = device;
4524 ++ndevs;
4525 }
4526
4527 /*
4528 * now sort the devices by hole size / available space
4529 */
4530 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4531 btrfs_cmp_device_info, NULL);
4532
4533 /* round down to number of usable stripes */
4534 ndevs -= ndevs % devs_increment;
4535
4536 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4537 ret = -ENOSPC;
4538 goto error;
4539 }
4540
4541 if (devs_max && ndevs > devs_max)
4542 ndevs = devs_max;
4543 /*
4544 * the primary goal is to maximize the number of stripes, so use as many
4545 * devices as possible, even if the stripes are not maximum sized.
4546 */
4547 stripe_size = devices_info[ndevs-1].max_avail;
4548 num_stripes = ndevs * dev_stripes;
4549
David Woodhouse53b381b2013-01-29 18:40:14 -05004550 /*
4551 * this will have to be fixed for RAID1 and RAID10 over
4552 * more drives
4553 */
4554 data_stripes = num_stripes / ncopies;
4555
David Woodhouse53b381b2013-01-29 18:40:14 -05004556 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4557 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4558 btrfs_super_stripesize(info->super_copy));
4559 data_stripes = num_stripes - 1;
4560 }
4561 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4562 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4563 btrfs_super_stripesize(info->super_copy));
4564 data_stripes = num_stripes - 2;
4565 }
Chris Mason86db2572013-02-20 16:23:40 -05004566
4567 /*
4568 * Use the number of data stripes to figure out how big this chunk
4569 * is really going to be in terms of logical address space,
4570 * and compare that answer with the max chunk size
4571 */
4572 if (stripe_size * data_stripes > max_chunk_size) {
4573 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004574
4575 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004576
4577 /* bump the answer up to a 16MB boundary */
4578 stripe_size = (stripe_size + mask) & ~mask;
4579
4580 /* but don't go higher than the limits we found
4581 * while searching for free extents
4582 */
4583 if (stripe_size > devices_info[ndevs-1].max_avail)
4584 stripe_size = devices_info[ndevs-1].max_avail;
4585 }
4586
David Sterbab8b93ad2015-01-16 17:26:13 +01004587 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004588
4589 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004590 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004591 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004592
Miao Xieb2117a32011-01-05 10:07:28 +00004593 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4594 if (!map) {
4595 ret = -ENOMEM;
4596 goto error;
4597 }
4598 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004599
Arne Jansen73c5de02011-04-12 12:07:57 +02004600 for (i = 0; i < ndevs; ++i) {
4601 for (j = 0; j < dev_stripes; ++j) {
4602 int s = i * dev_stripes + j;
4603 map->stripes[s].dev = devices_info[i].dev;
4604 map->stripes[s].physical = devices_info[i].dev_offset +
4605 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004606 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004607 }
Chris Mason593060d2008-03-25 16:50:33 -04004608 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004609 map->stripe_len = raid_stripe_len;
4610 map->io_align = raid_stripe_len;
4611 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004612 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004613 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004614
David Woodhouse53b381b2013-01-29 18:40:14 -05004615 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004616
Arne Jansen73c5de02011-04-12 12:07:57 +02004617 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004618
David Sterba172ddd62011-04-21 00:48:27 +02004619 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004620 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004621 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004622 ret = -ENOMEM;
4623 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004624 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004625 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004626 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004627 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004628 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004629 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004630 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004631 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004632
Chris Mason0b86a832008-03-24 15:01:56 -04004633 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004634 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004635 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004636 if (!ret) {
4637 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4638 atomic_inc(&em->refs);
4639 }
Chris Mason890871b2009-09-02 16:24:52 -04004640 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004641 if (ret) {
4642 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004643 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004644 }
Yan Zheng2b820322008-11-17 21:11:30 -05004645
Josef Bacik04487482013-01-29 15:03:37 -05004646 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4647 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4648 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004649 if (ret)
4650 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004651
Miao Xie7cc8e582014-09-03 21:35:38 +08004652 for (i = 0; i < map->num_stripes; i++) {
4653 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4654 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4655 }
Miao Xie43530c42014-09-03 21:35:36 +08004656
Miao Xie1c116182014-09-03 21:35:37 +08004657 spin_lock(&extent_root->fs_info->free_chunk_lock);
4658 extent_root->fs_info->free_chunk_space -= (stripe_size *
4659 map->num_stripes);
4660 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4661
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004662 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004663 check_raid56_incompat_flag(extent_root->fs_info, type);
4664
Miao Xieb2117a32011-01-05 10:07:28 +00004665 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004666 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004667
Josef Bacik6df9a952013-06-27 13:22:46 -04004668error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004669 write_lock(&em_tree->lock);
4670 remove_extent_mapping(em_tree, em);
4671 write_unlock(&em_tree->lock);
4672
4673 /* One for our allocation */
4674 free_extent_map(em);
4675 /* One for the tree reference */
4676 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004677 /* One for the pending_chunks list reference */
4678 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004679error:
Miao Xieb2117a32011-01-05 10:07:28 +00004680 kfree(devices_info);
4681 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004682}
4683
Josef Bacik6df9a952013-06-27 13:22:46 -04004684int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004685 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004686 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004687{
Yan Zheng2b820322008-11-17 21:11:30 -05004688 struct btrfs_key key;
4689 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4690 struct btrfs_device *device;
4691 struct btrfs_chunk *chunk;
4692 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004693 struct extent_map_tree *em_tree;
4694 struct extent_map *em;
4695 struct map_lookup *map;
4696 size_t item_size;
4697 u64 dev_offset;
4698 u64 stripe_size;
4699 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004700 int ret;
4701
Josef Bacik6df9a952013-06-27 13:22:46 -04004702 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4703 read_lock(&em_tree->lock);
4704 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4705 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004706
Josef Bacik6df9a952013-06-27 13:22:46 -04004707 if (!em) {
4708 btrfs_crit(extent_root->fs_info, "unable to find logical "
4709 "%Lu len %Lu", chunk_offset, chunk_size);
4710 return -EINVAL;
4711 }
4712
4713 if (em->start != chunk_offset || em->len != chunk_size) {
4714 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004715 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004716 chunk_size, em->start, em->len);
4717 free_extent_map(em);
4718 return -EINVAL;
4719 }
4720
4721 map = (struct map_lookup *)em->bdev;
4722 item_size = btrfs_chunk_item_size(map->num_stripes);
4723 stripe_size = em->orig_block_len;
4724
4725 chunk = kzalloc(item_size, GFP_NOFS);
4726 if (!chunk) {
4727 ret = -ENOMEM;
4728 goto out;
4729 }
4730
4731 for (i = 0; i < map->num_stripes; i++) {
4732 device = map->stripes[i].dev;
4733 dev_offset = map->stripes[i].physical;
4734
Yan Zheng2b820322008-11-17 21:11:30 -05004735 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004736 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004737 goto out;
4738 ret = btrfs_alloc_dev_extent(trans, device,
4739 chunk_root->root_key.objectid,
4740 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4741 chunk_offset, dev_offset,
4742 stripe_size);
4743 if (ret)
4744 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004745 }
4746
Yan Zheng2b820322008-11-17 21:11:30 -05004747 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004748 for (i = 0; i < map->num_stripes; i++) {
4749 device = map->stripes[i].dev;
4750 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004751
4752 btrfs_set_stack_stripe_devid(stripe, device->devid);
4753 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4754 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4755 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004756 }
4757
4758 btrfs_set_stack_chunk_length(chunk, chunk_size);
4759 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4760 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4761 btrfs_set_stack_chunk_type(chunk, map->type);
4762 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4763 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4764 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4765 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4766 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4767
4768 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4769 key.type = BTRFS_CHUNK_ITEM_KEY;
4770 key.offset = chunk_offset;
4771
4772 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004773 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4774 /*
4775 * TODO: Cleanup of inserted chunk root in case of
4776 * failure.
4777 */
Li Zefan125ccb02011-12-08 15:07:24 +08004778 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004779 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004780 }
liubo1abe9b82011-03-24 11:18:59 +00004781
Josef Bacik6df9a952013-06-27 13:22:46 -04004782out:
Yan Zheng2b820322008-11-17 21:11:30 -05004783 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004784 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004785 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004786}
4787
4788/*
4789 * Chunk allocation falls into two parts. The first part does works
4790 * that make the new allocated chunk useable, but not do any operation
4791 * that modifies the chunk tree. The second part does the works that
4792 * require modifying the chunk tree. This division is important for the
4793 * bootstrap process of adding storage to a seed btrfs.
4794 */
4795int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4796 struct btrfs_root *extent_root, u64 type)
4797{
4798 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004799
Filipe Mananaa9629592015-05-18 19:11:40 +01004800 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004801 chunk_offset = find_next_chunk(extent_root->fs_info);
4802 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004803}
4804
Chris Masond3977122009-01-05 21:25:51 -05004805static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004806 struct btrfs_root *root,
4807 struct btrfs_device *device)
4808{
4809 u64 chunk_offset;
4810 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004811 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004812 struct btrfs_fs_info *fs_info = root->fs_info;
4813 struct btrfs_root *extent_root = fs_info->extent_root;
4814 int ret;
4815
Josef Bacik6df9a952013-06-27 13:22:46 -04004816 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004817 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004818 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4819 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004820 if (ret)
4821 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004822
Josef Bacik6df9a952013-06-27 13:22:46 -04004823 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004824 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004825 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4826 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004827 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004828}
4829
Miao Xied20983b2014-07-03 18:22:13 +08004830static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4831{
4832 int max_errors;
4833
4834 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4835 BTRFS_BLOCK_GROUP_RAID10 |
4836 BTRFS_BLOCK_GROUP_RAID5 |
4837 BTRFS_BLOCK_GROUP_DUP)) {
4838 max_errors = 1;
4839 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4840 max_errors = 2;
4841 } else {
4842 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004843 }
4844
Miao Xied20983b2014-07-03 18:22:13 +08004845 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004846}
4847
4848int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4849{
4850 struct extent_map *em;
4851 struct map_lookup *map;
4852 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4853 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004854 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004855 int i;
4856
Chris Mason890871b2009-09-02 16:24:52 -04004857 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004858 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004859 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004860 if (!em)
4861 return 1;
4862
4863 map = (struct map_lookup *)em->bdev;
4864 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004865 if (map->stripes[i].dev->missing) {
4866 miss_ndevs++;
4867 continue;
4868 }
4869
Yan Zheng2b820322008-11-17 21:11:30 -05004870 if (!map->stripes[i].dev->writeable) {
4871 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004872 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004873 }
4874 }
Miao Xied20983b2014-07-03 18:22:13 +08004875
4876 /*
4877 * If the number of missing devices is larger than max errors,
4878 * we can not write the data into that chunk successfully, so
4879 * set it readonly.
4880 */
4881 if (miss_ndevs > btrfs_chunk_max_errors(map))
4882 readonly = 1;
4883end:
Yan Zheng2b820322008-11-17 21:11:30 -05004884 free_extent_map(em);
4885 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004886}
4887
4888void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4889{
David Sterbaa8067e02011-04-21 00:34:43 +02004890 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004891}
4892
4893void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4894{
4895 struct extent_map *em;
4896
Chris Masond3977122009-01-05 21:25:51 -05004897 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004898 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004899 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4900 if (em)
4901 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004902 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004903 if (!em)
4904 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004905 /* once for us */
4906 free_extent_map(em);
4907 /* once for the tree */
4908 free_extent_map(em);
4909 }
4910}
4911
Stefan Behrens5d964052012-11-05 14:59:07 +01004912int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004913{
Stefan Behrens5d964052012-11-05 14:59:07 +01004914 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004915 struct extent_map *em;
4916 struct map_lookup *map;
4917 struct extent_map_tree *em_tree = &map_tree->map_tree;
4918 int ret;
4919
Chris Mason890871b2009-09-02 16:24:52 -04004920 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004921 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004922 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004923
Josef Bacikfb7669b2013-04-23 10:53:18 -04004924 /*
4925 * We could return errors for these cases, but that could get ugly and
4926 * we'd probably do the same thing which is just not do anything else
4927 * and exit, so return 1 so the callers don't try to use other copies.
4928 */
4929 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004930 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004931 logical+len);
4932 return 1;
4933 }
4934
4935 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004936 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004937 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004938 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004939 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004940 return 1;
4941 }
4942
Chris Masonf1885912008-04-09 16:28:12 -04004943 map = (struct map_lookup *)em->bdev;
4944 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4945 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004946 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4947 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004948 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4949 ret = 2;
4950 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4951 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004952 else
4953 ret = 1;
4954 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004955
4956 btrfs_dev_replace_lock(&fs_info->dev_replace);
4957 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4958 ret++;
4959 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4960
Chris Masonf1885912008-04-09 16:28:12 -04004961 return ret;
4962}
4963
David Woodhouse53b381b2013-01-29 18:40:14 -05004964unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4965 struct btrfs_mapping_tree *map_tree,
4966 u64 logical)
4967{
4968 struct extent_map *em;
4969 struct map_lookup *map;
4970 struct extent_map_tree *em_tree = &map_tree->map_tree;
4971 unsigned long len = root->sectorsize;
4972
4973 read_lock(&em_tree->lock);
4974 em = lookup_extent_mapping(em_tree, logical, len);
4975 read_unlock(&em_tree->lock);
4976 BUG_ON(!em);
4977
4978 BUG_ON(em->start > logical || em->start + em->len < logical);
4979 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004980 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004981 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004982 free_extent_map(em);
4983 return len;
4984}
4985
4986int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4987 u64 logical, u64 len, int mirror_num)
4988{
4989 struct extent_map *em;
4990 struct map_lookup *map;
4991 struct extent_map_tree *em_tree = &map_tree->map_tree;
4992 int ret = 0;
4993
4994 read_lock(&em_tree->lock);
4995 em = lookup_extent_mapping(em_tree, logical, len);
4996 read_unlock(&em_tree->lock);
4997 BUG_ON(!em);
4998
4999 BUG_ON(em->start > logical || em->start + em->len < logical);
5000 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08005001 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005002 ret = 1;
5003 free_extent_map(em);
5004 return ret;
5005}
5006
Stefan Behrens30d98612012-11-06 14:52:18 +01005007static int find_live_mirror(struct btrfs_fs_info *fs_info,
5008 struct map_lookup *map, int first, int num,
5009 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005010{
5011 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005012 int tolerance;
5013 struct btrfs_device *srcdev;
5014
5015 if (dev_replace_is_ongoing &&
5016 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5017 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5018 srcdev = fs_info->dev_replace.srcdev;
5019 else
5020 srcdev = NULL;
5021
5022 /*
5023 * try to avoid the drive that is the source drive for a
5024 * dev-replace procedure, only choose it if no other non-missing
5025 * mirror is available
5026 */
5027 for (tolerance = 0; tolerance < 2; tolerance++) {
5028 if (map->stripes[optimal].dev->bdev &&
5029 (tolerance || map->stripes[optimal].dev != srcdev))
5030 return optimal;
5031 for (i = first; i < first + num; i++) {
5032 if (map->stripes[i].dev->bdev &&
5033 (tolerance || map->stripes[i].dev != srcdev))
5034 return i;
5035 }
Chris Masondfe25022008-05-13 13:46:40 -04005036 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005037
Chris Masondfe25022008-05-13 13:46:40 -04005038 /* we couldn't find one that doesn't fail. Just return something
5039 * and the io error handling code will clean up eventually
5040 */
5041 return optimal;
5042}
5043
David Woodhouse53b381b2013-01-29 18:40:14 -05005044static inline int parity_smaller(u64 a, u64 b)
5045{
5046 return a > b;
5047}
5048
5049/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005050static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005051{
5052 struct btrfs_bio_stripe s;
5053 int i;
5054 u64 l;
5055 int again = 1;
5056
5057 while (again) {
5058 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005059 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005060 if (parity_smaller(bbio->raid_map[i],
5061 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005062 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005063 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005064 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005065 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005066 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005067 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005068
David Woodhouse53b381b2013-01-29 18:40:14 -05005069 again = 1;
5070 }
5071 }
5072 }
5073}
5074
Zhao Lei6e9606d2015-01-20 15:11:34 +08005075static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5076{
5077 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005078 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005079 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005080 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005081 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005082 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005083 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005084 /*
5085 * plus the raid_map, which includes both the tgt dev
5086 * and the stripes
5087 */
5088 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005089 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005090
5091 atomic_set(&bbio->error, 0);
5092 atomic_set(&bbio->refs, 1);
5093
5094 return bbio;
5095}
5096
5097void btrfs_get_bbio(struct btrfs_bio *bbio)
5098{
5099 WARN_ON(!atomic_read(&bbio->refs));
5100 atomic_inc(&bbio->refs);
5101}
5102
5103void btrfs_put_bbio(struct btrfs_bio *bbio)
5104{
5105 if (!bbio)
5106 return;
5107 if (atomic_dec_and_test(&bbio->refs))
5108 kfree(bbio);
5109}
5110
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005111static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005112 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005113 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005114 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005115{
5116 struct extent_map *em;
5117 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005118 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005119 struct extent_map_tree *em_tree = &map_tree->map_tree;
5120 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005121 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005122 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005123 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005124 u64 stripe_nr_orig;
5125 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005126 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005127 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005128 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005129 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005130 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005131 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005132 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005133 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005134 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5135 int dev_replace_is_ongoing = 0;
5136 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005137 int patch_the_first_stripe_for_dev_replace = 0;
5138 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005139 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005140
Chris Mason890871b2009-09-02 16:24:52 -04005141 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005142 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005143 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005144
Chris Mason3b951512008-04-17 11:29:12 -04005145 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005146 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005147 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005148 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005149 }
Chris Mason0b86a832008-03-24 15:01:56 -04005150
Josef Bacik9bb91872013-04-19 23:45:33 -04005151 if (em->start > logical || em->start + em->len < logical) {
5152 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005153 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005154 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005155 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005156 return -EINVAL;
5157 }
5158
Chris Mason0b86a832008-03-24 15:01:56 -04005159 map = (struct map_lookup *)em->bdev;
5160 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005161
David Woodhouse53b381b2013-01-29 18:40:14 -05005162 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005163 stripe_nr = offset;
5164 /*
5165 * stripe_nr counts the total number of stripes we have to stride
5166 * to get to this block
5167 */
David Sterba47c57132015-02-20 18:43:47 +01005168 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005169
David Woodhouse53b381b2013-01-29 18:40:14 -05005170 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005171 BUG_ON(offset < stripe_offset);
5172
5173 /* stripe_offset is the offset of this block in its stripe*/
5174 stripe_offset = offset - stripe_offset;
5175
David Woodhouse53b381b2013-01-29 18:40:14 -05005176 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005177 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005178 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5179 raid56_full_stripe_start = offset;
5180
5181 /* allow a write of a full stripe, but make sure we don't
5182 * allow straddling of stripes
5183 */
David Sterba47c57132015-02-20 18:43:47 +01005184 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5185 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005186 raid56_full_stripe_start *= full_stripe_len;
5187 }
5188
5189 if (rw & REQ_DISCARD) {
5190 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005191 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005192 ret = -EOPNOTSUPP;
5193 goto out;
5194 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005195 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005196 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5197 u64 max_len;
5198 /* For writes to RAID[56], allow a full stripeset across all disks.
5199 For other RAID types and for RAID[56] reads, just allow a single
5200 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005201 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005202 (rw & REQ_WRITE)) {
5203 max_len = stripe_len * nr_data_stripes(map) -
5204 (offset - raid56_full_stripe_start);
5205 } else {
5206 /* we limit the length of each bio to what fits in a stripe */
5207 max_len = stripe_len - stripe_offset;
5208 }
5209 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005210 } else {
5211 *length = em->len - offset;
5212 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005213
David Woodhouse53b381b2013-01-29 18:40:14 -05005214 /* This is for when we're called from btrfs_merge_bio_hook() and all
5215 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005216 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005217 goto out;
5218
Stefan Behrens472262f2012-11-06 14:43:46 +01005219 btrfs_dev_replace_lock(dev_replace);
5220 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5221 if (!dev_replace_is_ongoing)
5222 btrfs_dev_replace_unlock(dev_replace);
5223
Stefan Behrensad6d6202012-11-06 15:06:47 +01005224 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5225 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5226 dev_replace->tgtdev != NULL) {
5227 /*
5228 * in dev-replace case, for repair case (that's the only
5229 * case where the mirror is selected explicitly when
5230 * calling btrfs_map_block), blocks left of the left cursor
5231 * can also be read from the target drive.
5232 * For REQ_GET_READ_MIRRORS, the target drive is added as
5233 * the last one to the array of stripes. For READ, it also
5234 * needs to be supported using the same mirror number.
5235 * If the requested block is not left of the left cursor,
5236 * EIO is returned. This can happen because btrfs_num_copies()
5237 * returns one more in the dev-replace case.
5238 */
5239 u64 tmp_length = *length;
5240 struct btrfs_bio *tmp_bbio = NULL;
5241 int tmp_num_stripes;
5242 u64 srcdev_devid = dev_replace->srcdev->devid;
5243 int index_srcdev = 0;
5244 int found = 0;
5245 u64 physical_of_found = 0;
5246
5247 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005248 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005249 if (ret) {
5250 WARN_ON(tmp_bbio != NULL);
5251 goto out;
5252 }
5253
5254 tmp_num_stripes = tmp_bbio->num_stripes;
5255 if (mirror_num > tmp_num_stripes) {
5256 /*
5257 * REQ_GET_READ_MIRRORS does not contain this
5258 * mirror, that means that the requested area
5259 * is not left of the left cursor
5260 */
5261 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005262 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005263 goto out;
5264 }
5265
5266 /*
5267 * process the rest of the function using the mirror_num
5268 * of the source drive. Therefore look it up first.
5269 * At the end, patch the device pointer to the one of the
5270 * target drive.
5271 */
5272 for (i = 0; i < tmp_num_stripes; i++) {
5273 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5274 /*
5275 * In case of DUP, in order to keep it
5276 * simple, only add the mirror with the
5277 * lowest physical address
5278 */
5279 if (found &&
5280 physical_of_found <=
5281 tmp_bbio->stripes[i].physical)
5282 continue;
5283 index_srcdev = i;
5284 found = 1;
5285 physical_of_found =
5286 tmp_bbio->stripes[i].physical;
5287 }
5288 }
5289
5290 if (found) {
5291 mirror_num = index_srcdev + 1;
5292 patch_the_first_stripe_for_dev_replace = 1;
5293 physical_to_patch_in_first_stripe = physical_of_found;
5294 } else {
5295 WARN_ON(1);
5296 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005297 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005298 goto out;
5299 }
5300
Zhao Lei6e9606d2015-01-20 15:11:34 +08005301 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005302 } else if (mirror_num > map->num_stripes) {
5303 mirror_num = 0;
5304 }
5305
Chris Masonf2d8d742008-04-21 10:03:05 -04005306 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005307 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005308 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005309 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005310 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005311 stripe_end_offset = stripe_nr_end * map->stripe_len -
5312 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005313
Li Dongyangfce3bb92011-03-24 10:24:26 +00005314 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5315 if (rw & REQ_DISCARD)
5316 num_stripes = min_t(u64, map->num_stripes,
5317 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005318 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5319 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005320 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5321 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005322 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005323 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005324 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005325 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005326 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005327 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005328 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005329 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005330 current->pid % map->num_stripes,
5331 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005332 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005333 }
Chris Mason2fff7342008-04-29 14:12:09 -04005334
Chris Mason611f0e02008-04-03 16:29:03 -04005335 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005336 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005337 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005338 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005339 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005340 } else {
5341 mirror_num = 1;
5342 }
Chris Mason2fff7342008-04-29 14:12:09 -04005343
Chris Mason321aecc2008-04-16 10:49:51 -04005344 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005345 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005346
David Sterba47c57132015-02-20 18:43:47 +01005347 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005348 stripe_index *= map->sub_stripes;
5349
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005350 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005351 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005352 else if (rw & REQ_DISCARD)
5353 num_stripes = min_t(u64, map->sub_stripes *
5354 (stripe_nr_end - stripe_nr_orig),
5355 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005356 else if (mirror_num)
5357 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005358 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005359 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005360 stripe_index = find_live_mirror(fs_info, map,
5361 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005362 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005363 current->pid % map->sub_stripes,
5364 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005365 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005366 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005367
Zhao Leiffe2d202015-01-20 15:11:44 +08005368 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005369 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005370 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5371 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005372 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005373 stripe_nr = div_u64(raid56_full_stripe_start,
5374 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005375
5376 /* RAID[56] write or recovery. Return all stripes */
5377 num_stripes = map->num_stripes;
5378 max_errors = nr_parity_stripes(map);
5379
David Woodhouse53b381b2013-01-29 18:40:14 -05005380 *length = map->stripe_len;
5381 stripe_index = 0;
5382 stripe_offset = 0;
5383 } else {
5384 /*
5385 * Mirror #0 or #1 means the original data block.
5386 * Mirror #2 is RAID5 parity block.
5387 * Mirror #3 is RAID6 Q block.
5388 */
David Sterba47c57132015-02-20 18:43:47 +01005389 stripe_nr = div_u64_rem(stripe_nr,
5390 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005391 if (mirror_num > 1)
5392 stripe_index = nr_data_stripes(map) +
5393 mirror_num - 2;
5394
5395 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005396 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5397 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005398 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5399 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5400 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005401 }
Chris Mason8790d502008-04-03 16:29:03 -04005402 } else {
5403 /*
David Sterba47c57132015-02-20 18:43:47 +01005404 * after this, stripe_nr is the number of stripes on this
5405 * device we have to walk to find the data, and stripe_index is
5406 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005407 */
David Sterba47c57132015-02-20 18:43:47 +01005408 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5409 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005410 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005411 }
Chris Mason593060d2008-03-25 16:50:33 -04005412 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005413
Stefan Behrens472262f2012-11-06 14:43:46 +01005414 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005415 if (dev_replace_is_ongoing) {
5416 if (rw & (REQ_WRITE | REQ_DISCARD))
5417 num_alloc_stripes <<= 1;
5418 if (rw & REQ_GET_READ_MIRRORS)
5419 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005420 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005421 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005422
Zhao Lei6e9606d2015-01-20 15:11:34 +08005423 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005424 if (!bbio) {
5425 ret = -ENOMEM;
5426 goto out;
5427 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005428 if (dev_replace_is_ongoing)
5429 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005430
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005431 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005432 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005433 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5434 mirror_num > 1)) {
5435 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005436 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005437
5438 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5439 sizeof(struct btrfs_bio_stripe) *
5440 num_alloc_stripes +
5441 sizeof(int) * tgtdev_indexes);
5442
5443 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005444 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005445
5446 /* Fill in the logical address of each stripe */
5447 tmp = stripe_nr * nr_data_stripes(map);
5448 for (i = 0; i < nr_data_stripes(map); i++)
5449 bbio->raid_map[(i+rot) % num_stripes] =
5450 em->start + (tmp + i) * map->stripe_len;
5451
5452 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5453 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5454 bbio->raid_map[(i+rot+1) % num_stripes] =
5455 RAID6_Q_STRIPE;
5456 }
5457
Li Dongyangfce3bb92011-03-24 10:24:26 +00005458 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005459 u32 factor = 0;
5460 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005461 u64 stripes_per_dev = 0;
5462 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005463 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005464
5465 if (map->type &
5466 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5467 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5468 sub_stripes = 1;
5469 else
5470 sub_stripes = map->sub_stripes;
5471
5472 factor = map->num_stripes / sub_stripes;
5473 stripes_per_dev = div_u64_rem(stripe_nr_end -
5474 stripe_nr_orig,
5475 factor,
5476 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005477 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5478 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005479 }
5480
Li Dongyangfce3bb92011-03-24 10:24:26 +00005481 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005482 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005483 map->stripes[stripe_index].physical +
5484 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005485 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005486
Li Zefanec9ef7a2011-12-01 14:06:42 +08005487 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5488 BTRFS_BLOCK_GROUP_RAID10)) {
5489 bbio->stripes[i].length = stripes_per_dev *
5490 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005491
Li Zefanec9ef7a2011-12-01 14:06:42 +08005492 if (i / sub_stripes < remaining_stripes)
5493 bbio->stripes[i].length +=
5494 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005495
5496 /*
5497 * Special for the first stripe and
5498 * the last stripe:
5499 *
5500 * |-------|...|-------|
5501 * |----------|
5502 * off end_off
5503 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005504 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005505 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005506 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005507
5508 if (stripe_index >= last_stripe &&
5509 stripe_index <= (last_stripe +
5510 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005511 bbio->stripes[i].length -=
5512 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005513
Li Zefanec9ef7a2011-12-01 14:06:42 +08005514 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005515 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005516 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005517 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005518
5519 stripe_index++;
5520 if (stripe_index == map->num_stripes) {
5521 /* This could only happen for RAID0/10 */
5522 stripe_index = 0;
5523 stripe_nr++;
5524 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005525 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005526 } else {
5527 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005528 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005529 map->stripes[stripe_index].physical +
5530 stripe_offset +
5531 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005532 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005533 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005534 stripe_index++;
5535 }
Chris Mason593060d2008-03-25 16:50:33 -04005536 }
Li Zefande11cc12011-12-01 12:55:47 +08005537
Miao Xied20983b2014-07-03 18:22:13 +08005538 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5539 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005540
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005541 if (bbio->raid_map)
5542 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005543
Miao Xie2c8cdd62014-11-14 16:06:25 +08005544 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005545 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5546 dev_replace->tgtdev != NULL) {
5547 int index_where_to_add;
5548 u64 srcdev_devid = dev_replace->srcdev->devid;
5549
5550 /*
5551 * duplicate the write operations while the dev replace
5552 * procedure is running. Since the copying of the old disk
5553 * to the new disk takes place at run time while the
5554 * filesystem is mounted writable, the regular write
5555 * operations to the old disk have to be duplicated to go
5556 * to the new disk as well.
5557 * Note that device->missing is handled by the caller, and
5558 * that the write to the old disk is already set up in the
5559 * stripes array.
5560 */
5561 index_where_to_add = num_stripes;
5562 for (i = 0; i < num_stripes; i++) {
5563 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5564 /* write to new disk, too */
5565 struct btrfs_bio_stripe *new =
5566 bbio->stripes + index_where_to_add;
5567 struct btrfs_bio_stripe *old =
5568 bbio->stripes + i;
5569
5570 new->physical = old->physical;
5571 new->length = old->length;
5572 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005573 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005574 index_where_to_add++;
5575 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005576 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005577 }
5578 }
5579 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005580 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5581 dev_replace->tgtdev != NULL) {
5582 u64 srcdev_devid = dev_replace->srcdev->devid;
5583 int index_srcdev = 0;
5584 int found = 0;
5585 u64 physical_of_found = 0;
5586
5587 /*
5588 * During the dev-replace procedure, the target drive can
5589 * also be used to read data in case it is needed to repair
5590 * a corrupt block elsewhere. This is possible if the
5591 * requested area is left of the left cursor. In this area,
5592 * the target drive is a full copy of the source drive.
5593 */
5594 for (i = 0; i < num_stripes; i++) {
5595 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5596 /*
5597 * In case of DUP, in order to keep it
5598 * simple, only add the mirror with the
5599 * lowest physical address
5600 */
5601 if (found &&
5602 physical_of_found <=
5603 bbio->stripes[i].physical)
5604 continue;
5605 index_srcdev = i;
5606 found = 1;
5607 physical_of_found = bbio->stripes[i].physical;
5608 }
5609 }
5610 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005611 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005612 dev_replace->cursor_left) {
5613 struct btrfs_bio_stripe *tgtdev_stripe =
5614 bbio->stripes + num_stripes;
5615
5616 tgtdev_stripe->physical = physical_of_found;
5617 tgtdev_stripe->length =
5618 bbio->stripes[index_srcdev].length;
5619 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005620 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005621
Miao Xie2c8cdd62014-11-14 16:06:25 +08005622 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005623 num_stripes++;
5624 }
5625 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005626 }
5627
Li Zefande11cc12011-12-01 12:55:47 +08005628 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005629 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005630 bbio->num_stripes = num_stripes;
5631 bbio->max_errors = max_errors;
5632 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005633 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005634
5635 /*
5636 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5637 * mirror_num == num_stripes + 1 && dev_replace target drive is
5638 * available as a mirror
5639 */
5640 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5641 WARN_ON(num_stripes > 1);
5642 bbio->stripes[0].dev = dev_replace->tgtdev;
5643 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5644 bbio->mirror_num = map->num_stripes + 1;
5645 }
Chris Masoncea9e442008-04-09 16:28:12 -04005646out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005647 if (dev_replace_is_ongoing)
5648 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005649 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005650 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005651}
5652
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005653int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005654 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005655 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005656{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005657 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005658 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005659}
5660
Miao Xieaf8e2d12014-10-23 14:42:50 +08005661/* For Scrub/replace */
5662int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5663 u64 logical, u64 *length,
5664 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005665 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005666{
5667 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005668 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005669}
5670
Yan Zhenga512bbf2008-12-08 16:46:26 -05005671int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5672 u64 chunk_start, u64 physical, u64 devid,
5673 u64 **logical, int *naddrs, int *stripe_len)
5674{
5675 struct extent_map_tree *em_tree = &map_tree->map_tree;
5676 struct extent_map *em;
5677 struct map_lookup *map;
5678 u64 *buf;
5679 u64 bytenr;
5680 u64 length;
5681 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005682 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005683 int i, j, nr = 0;
5684
Chris Mason890871b2009-09-02 16:24:52 -04005685 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005686 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005687 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005688
Josef Bacik835d9742013-03-19 12:13:25 -04005689 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005690 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005691 chunk_start);
5692 return -EIO;
5693 }
5694
5695 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005696 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005697 em->start, chunk_start);
5698 free_extent_map(em);
5699 return -EIO;
5700 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005701 map = (struct map_lookup *)em->bdev;
5702
5703 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005704 rmap_len = map->stripe_len;
5705
Yan Zhenga512bbf2008-12-08 16:46:26 -05005706 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005707 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005708 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005709 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005710 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005711 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005712 rmap_len = map->stripe_len * nr_data_stripes(map);
5713 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005714
David Sterba31e818f2015-02-20 18:00:26 +01005715 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005716 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005717
5718 for (i = 0; i < map->num_stripes; i++) {
5719 if (devid && map->stripes[i].dev->devid != devid)
5720 continue;
5721 if (map->stripes[i].physical > physical ||
5722 map->stripes[i].physical + length <= physical)
5723 continue;
5724
5725 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005726 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005727
5728 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5729 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005730 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005731 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5732 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005733 } /* else if RAID[56], multiply by nr_data_stripes().
5734 * Alternatively, just use rmap_len below instead of
5735 * map->stripe_len */
5736
5737 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005738 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005739 for (j = 0; j < nr; j++) {
5740 if (buf[j] == bytenr)
5741 break;
5742 }
Chris Mason934d3752008-12-08 16:43:10 -05005743 if (j == nr) {
5744 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005745 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005746 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005747 }
5748
Yan Zhenga512bbf2008-12-08 16:46:26 -05005749 *logical = buf;
5750 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005751 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005752
5753 free_extent_map(em);
5754 return 0;
5755}
5756
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005757static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005758{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005759 bio->bi_private = bbio->private;
5760 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005761 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005762
Zhao Lei6e9606d2015-01-20 15:11:34 +08005763 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005764}
5765
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005766static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005767{
Chris Mason9be33952013-05-17 18:30:14 -04005768 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005769 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005770
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005771 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005772 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005773 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005774 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005775 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005776 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005777
5778 BUG_ON(stripe_index >= bbio->num_stripes);
5779 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005780 if (dev->bdev) {
5781 if (bio->bi_rw & WRITE)
5782 btrfs_dev_stat_inc(dev,
5783 BTRFS_DEV_STAT_WRITE_ERRS);
5784 else
5785 btrfs_dev_stat_inc(dev,
5786 BTRFS_DEV_STAT_READ_ERRS);
5787 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5788 btrfs_dev_stat_inc(dev,
5789 BTRFS_DEV_STAT_FLUSH_ERRS);
5790 btrfs_dev_stat_print_on_error(dev);
5791 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005792 }
5793 }
Chris Mason8790d502008-04-03 16:29:03 -04005794
Jan Schmidta1d3c472011-08-04 17:15:33 +02005795 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005796 is_orig_bio = 1;
5797
Miao Xiec404e0d2014-01-30 16:46:55 +08005798 btrfs_bio_counter_dec(bbio->fs_info);
5799
Jan Schmidta1d3c472011-08-04 17:15:33 +02005800 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005801 if (!is_orig_bio) {
5802 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005803 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005804 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005805
Chris Mason9be33952013-05-17 18:30:14 -04005806 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005807 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005808 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005809 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005810 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005811 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005812 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005813 /*
5814 * this bio is actually up to date, we didn't
5815 * go over the max number of errors
5816 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005817 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005818 }
Miao Xiec55f1392014-06-19 10:42:54 +08005819
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005820 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005821 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005822 bio_put(bio);
5823 }
Chris Mason8790d502008-04-03 16:29:03 -04005824}
5825
Chris Mason8b712842008-06-11 16:50:36 -04005826/*
5827 * see run_scheduled_bios for a description of why bios are collected for
5828 * async submit.
5829 *
5830 * This will add one bio to the pending list for a device and make sure
5831 * the work struct is scheduled.
5832 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005833static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5834 struct btrfs_device *device,
5835 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005836{
5837 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005838 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005839
David Woodhouse53b381b2013-01-29 18:40:14 -05005840 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005841 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005842 return;
5843 }
5844
Chris Mason8b712842008-06-11 16:50:36 -04005845 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005846 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005847 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005848 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005849 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005850 return;
Chris Mason8b712842008-06-11 16:50:36 -04005851 }
5852
5853 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005854 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005855 * higher layers. Otherwise, the async bio makes it appear we have
5856 * made progress against dirty pages when we've really just put it
5857 * on a queue for later
5858 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005859 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005860 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005861 bio->bi_next = NULL;
5862 bio->bi_rw |= rw;
5863
5864 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005865 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005866 pending_bios = &device->pending_sync_bios;
5867 else
5868 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005869
Chris Masonffbd5172009-04-20 15:50:09 -04005870 if (pending_bios->tail)
5871 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005872
Chris Masonffbd5172009-04-20 15:50:09 -04005873 pending_bios->tail = bio;
5874 if (!pending_bios->head)
5875 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005876 if (device->running_pending)
5877 should_queue = 0;
5878
5879 spin_unlock(&device->io_lock);
5880
5881 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005882 btrfs_queue_work(root->fs_info->submit_workers,
5883 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005884}
5885
Josef Bacikde1ee922012-10-19 16:50:56 -04005886static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5887 struct bio *bio, u64 physical, int dev_nr,
5888 int rw, int async)
5889{
5890 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5891
5892 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005893 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005894 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005895 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005896#ifdef DEBUG
5897 {
5898 struct rcu_string *name;
5899
5900 rcu_read_lock();
5901 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005902 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005903 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005904 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5905 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005906 rcu_read_unlock();
5907 }
5908#endif
5909 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005910
5911 btrfs_bio_counter_inc_noblocked(root->fs_info);
5912
Josef Bacikde1ee922012-10-19 16:50:56 -04005913 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005914 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005915 else
5916 btrfsic_submit_bio(rw, bio);
5917}
5918
Josef Bacikde1ee922012-10-19 16:50:56 -04005919static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5920{
5921 atomic_inc(&bbio->error);
5922 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005923 /* Shoud be the original bio. */
5924 WARN_ON(bio != bbio->orig_bio);
5925
Chris Mason9be33952013-05-17 18:30:14 -04005926 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005927 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005928 bio->bi_error = -EIO;
5929 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005930 }
5931}
5932
Chris Masonf1885912008-04-09 16:28:12 -04005933int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005934 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005935{
Chris Mason0b86a832008-03-24 15:01:56 -04005936 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005937 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005938 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005939 u64 length = 0;
5940 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005941 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005942 int dev_nr;
5943 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005944 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005945
Kent Overstreet4f024f32013-10-11 15:44:27 -07005946 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005947 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005948
Miao Xiec404e0d2014-01-30 16:46:55 +08005949 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005950 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005951 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005952 if (ret) {
5953 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005954 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005955 }
Chris Masoncea9e442008-04-09 16:28:12 -04005956
Jan Schmidta1d3c472011-08-04 17:15:33 +02005957 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005958 bbio->orig_bio = first_bio;
5959 bbio->private = first_bio->bi_private;
5960 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005961 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005962 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5963
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005964 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005965 /* In this case, map_length has been set to the length of
5966 a single stripe; not the whole write */
5967 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005968 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005969 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005970 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005971 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005972 }
Miao Xie42452152014-11-25 16:39:28 +08005973
Miao Xiec404e0d2014-01-30 16:46:55 +08005974 btrfs_bio_counter_dec(root->fs_info);
5975 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005976 }
5977
Chris Masoncea9e442008-04-09 16:28:12 -04005978 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005979 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005980 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005981 BUG();
5982 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005983
Zhao Lei08da7572015-02-12 15:42:16 +08005984 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005985 dev = bbio->stripes[dev_nr].dev;
5986 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5987 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005988 continue;
5989 }
5990
Jan Schmidta1d3c472011-08-04 17:15:33 +02005991 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005992 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005993 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04005994 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005995 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04005996
Josef Bacikde1ee922012-10-19 16:50:56 -04005997 submit_stripe_bio(root, bbio, bio,
5998 bbio->stripes[dev_nr].physical, dev_nr, rw,
5999 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006000 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006001 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006002 return 0;
6003}
6004
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006005struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006006 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006007{
Yan Zheng2b820322008-11-17 21:11:30 -05006008 struct btrfs_device *device;
6009 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006010
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006011 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006012 while (cur_devices) {
6013 if (!fsid ||
6014 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6015 device = __find_device(&cur_devices->devices,
6016 devid, uuid);
6017 if (device)
6018 return device;
6019 }
6020 cur_devices = cur_devices->seed;
6021 }
6022 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006023}
6024
Chris Masondfe25022008-05-13 13:46:40 -04006025static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006026 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006027 u64 devid, u8 *dev_uuid)
6028{
6029 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006030
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006031 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6032 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006033 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006034
6035 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006036 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006037 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006038
6039 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006040 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006041
Chris Masondfe25022008-05-13 13:46:40 -04006042 return device;
6043}
6044
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006045/**
6046 * btrfs_alloc_device - allocate struct btrfs_device
6047 * @fs_info: used only for generating a new devid, can be NULL if
6048 * devid is provided (i.e. @devid != NULL).
6049 * @devid: a pointer to devid for this device. If NULL a new devid
6050 * is generated.
6051 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6052 * is generated.
6053 *
6054 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6055 * on error. Returned struct is not linked onto any lists and can be
6056 * destroyed with kfree() right away.
6057 */
6058struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6059 const u64 *devid,
6060 const u8 *uuid)
6061{
6062 struct btrfs_device *dev;
6063 u64 tmp;
6064
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306065 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006066 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006067
6068 dev = __alloc_device();
6069 if (IS_ERR(dev))
6070 return dev;
6071
6072 if (devid)
6073 tmp = *devid;
6074 else {
6075 int ret;
6076
6077 ret = find_next_devid(fs_info, &tmp);
6078 if (ret) {
6079 kfree(dev);
6080 return ERR_PTR(ret);
6081 }
6082 }
6083 dev->devid = tmp;
6084
6085 if (uuid)
6086 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6087 else
6088 generate_random_uuid(dev->uuid);
6089
Liu Bo9e0af232014-08-15 23:36:53 +08006090 btrfs_init_work(&dev->work, btrfs_submit_helper,
6091 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006092
6093 return dev;
6094}
6095
Chris Mason0b86a832008-03-24 15:01:56 -04006096static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6097 struct extent_buffer *leaf,
6098 struct btrfs_chunk *chunk)
6099{
6100 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6101 struct map_lookup *map;
6102 struct extent_map *em;
6103 u64 logical;
6104 u64 length;
6105 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006106 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006107 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006108 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006109 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006110
Chris Masone17cade2008-04-15 15:41:47 -04006111 logical = key->offset;
6112 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006113
Chris Mason890871b2009-09-02 16:24:52 -04006114 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006115 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006116 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006117
6118 /* already mapped? */
6119 if (em && em->start <= logical && em->start + em->len > logical) {
6120 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006121 return 0;
6122 } else if (em) {
6123 free_extent_map(em);
6124 }
Chris Mason0b86a832008-03-24 15:01:56 -04006125
David Sterba172ddd62011-04-21 00:48:27 +02006126 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006127 if (!em)
6128 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006129 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6130 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006131 if (!map) {
6132 free_extent_map(em);
6133 return -ENOMEM;
6134 }
6135
Wang Shilong298a8f92014-06-19 10:42:52 +08006136 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006137 em->bdev = (struct block_device *)map;
6138 em->start = logical;
6139 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006140 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006141 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006142 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006143
Chris Mason593060d2008-03-25 16:50:33 -04006144 map->num_stripes = num_stripes;
6145 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6146 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6147 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6148 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6149 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006150 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006151 for (i = 0; i < num_stripes; i++) {
6152 map->stripes[i].physical =
6153 btrfs_stripe_offset_nr(leaf, chunk, i);
6154 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006155 read_extent_buffer(leaf, uuid, (unsigned long)
6156 btrfs_stripe_dev_uuid_nr(chunk, i),
6157 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006158 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6159 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006160 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006161 free_extent_map(em);
6162 return -EIO;
6163 }
Chris Masondfe25022008-05-13 13:46:40 -04006164 if (!map->stripes[i].dev) {
6165 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006166 add_missing_dev(root, root->fs_info->fs_devices,
6167 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006168 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006169 free_extent_map(em);
6170 return -EIO;
6171 }
Anand Jain816fceb2015-04-27 12:46:18 +08006172 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6173 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006174 }
6175 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006176 }
6177
Chris Mason890871b2009-09-02 16:24:52 -04006178 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006179 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006180 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006181 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006182 free_extent_map(em);
6183
6184 return 0;
6185}
6186
Jeff Mahoney143bede2012-03-01 14:56:26 +01006187static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006188 struct btrfs_dev_item *dev_item,
6189 struct btrfs_device *device)
6190{
6191 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006192
6193 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006194 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6195 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006196 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006197 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006198 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006199 device->type = btrfs_device_type(leaf, dev_item);
6200 device->io_align = btrfs_device_io_align(leaf, dev_item);
6201 device->io_width = btrfs_device_io_width(leaf, dev_item);
6202 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006203 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006204 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006205
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006206 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006207 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006208}
6209
Miao Xie5f375832014-09-03 21:35:46 +08006210static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6211 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006212{
6213 struct btrfs_fs_devices *fs_devices;
6214 int ret;
6215
Li Zefanb367e472011-12-07 11:38:24 +08006216 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006217
6218 fs_devices = root->fs_info->fs_devices->seed;
6219 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006220 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6221 return fs_devices;
6222
Yan Zheng2b820322008-11-17 21:11:30 -05006223 fs_devices = fs_devices->seed;
6224 }
6225
6226 fs_devices = find_fsid(fsid);
6227 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006228 if (!btrfs_test_opt(root, DEGRADED))
6229 return ERR_PTR(-ENOENT);
6230
6231 fs_devices = alloc_fs_devices(fsid);
6232 if (IS_ERR(fs_devices))
6233 return fs_devices;
6234
6235 fs_devices->seeding = 1;
6236 fs_devices->opened = 1;
6237 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006238 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006239
6240 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006241 if (IS_ERR(fs_devices))
6242 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006243
Christoph Hellwig97288f22008-12-02 06:36:09 -05006244 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006245 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006246 if (ret) {
6247 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006248 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006249 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006250 }
Yan Zheng2b820322008-11-17 21:11:30 -05006251
6252 if (!fs_devices->seeding) {
6253 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006254 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006255 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006256 goto out;
6257 }
6258
6259 fs_devices->seed = root->fs_info->fs_devices->seed;
6260 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006261out:
Miao Xie5f375832014-09-03 21:35:46 +08006262 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006263}
6264
Chris Mason0d81ba52008-03-24 15:02:07 -04006265static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006266 struct extent_buffer *leaf,
6267 struct btrfs_dev_item *dev_item)
6268{
Miao Xie5f375832014-09-03 21:35:46 +08006269 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006270 struct btrfs_device *device;
6271 u64 devid;
6272 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006273 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006274 u8 dev_uuid[BTRFS_UUID_SIZE];
6275
Chris Mason0b86a832008-03-24 15:01:56 -04006276 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006277 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006278 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006279 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006280 BTRFS_UUID_SIZE);
6281
6282 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006283 fs_devices = open_seed_devices(root, fs_uuid);
6284 if (IS_ERR(fs_devices))
6285 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006286 }
6287
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006288 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006289 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006290 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006291 return -EIO;
6292
Miao Xie5f375832014-09-03 21:35:46 +08006293 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6294 if (!device)
6295 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006296 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6297 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006298 } else {
6299 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6300 return -EIO;
6301
6302 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006303 /*
6304 * this happens when a device that was properly setup
6305 * in the device info lists suddenly goes bad.
6306 * device->bdev is NULL, and so we have to set
6307 * device->missing to one here
6308 */
Miao Xie5f375832014-09-03 21:35:46 +08006309 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006310 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006311 }
Miao Xie5f375832014-09-03 21:35:46 +08006312
6313 /* Move the device to its own fs_devices */
6314 if (device->fs_devices != fs_devices) {
6315 ASSERT(device->missing);
6316
6317 list_move(&device->dev_list, &fs_devices->devices);
6318 device->fs_devices->num_devices--;
6319 fs_devices->num_devices++;
6320
6321 device->fs_devices->missing_devices--;
6322 fs_devices->missing_devices++;
6323
6324 device->fs_devices = fs_devices;
6325 }
Yan Zheng2b820322008-11-17 21:11:30 -05006326 }
6327
6328 if (device->fs_devices != root->fs_info->fs_devices) {
6329 BUG_ON(device->writeable);
6330 if (device->generation !=
6331 btrfs_device_generation(leaf, dev_item))
6332 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006333 }
Chris Mason0b86a832008-03-24 15:01:56 -04006334
6335 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006336 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006337 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006338 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006339 spin_lock(&root->fs_info->free_chunk_lock);
6340 root->fs_info->free_chunk_space += device->total_bytes -
6341 device->bytes_used;
6342 spin_unlock(&root->fs_info->free_chunk_lock);
6343 }
Chris Mason0b86a832008-03-24 15:01:56 -04006344 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006345 return ret;
6346}
6347
Yan Zhenge4404d62008-12-12 10:03:26 -05006348int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006349{
David Sterba6c417612011-04-13 15:41:04 +02006350 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006351 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006352 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006353 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006354 u8 *array_ptr;
6355 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006356 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006357 u32 num_stripes;
6358 u32 array_size;
6359 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006360 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006361 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006362
David Sterbaa83fffb2014-06-15 02:39:54 +02006363 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6364 /*
6365 * This will create extent buffer of nodesize, superblock size is
6366 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6367 * overallocate but we can keep it as-is, only the first page is used.
6368 */
6369 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006370 if (!sb)
6371 return -ENOMEM;
6372 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006373 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006374 /*
6375 * The sb extent buffer is artifical and just used to read the system array.
6376 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6377 * pages up-to-date when the page is larger: extent does not cover the
6378 * whole page and consequently check_page_uptodate does not find all
6379 * the page's extents up-to-date (the hole beyond sb),
6380 * write_extent_buffer then triggers a WARN_ON.
6381 *
6382 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6383 * but sb spans only this function. Add an explicit SetPageUptodate call
6384 * to silence the warning eg. on PowerPC 64.
6385 */
6386 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006387 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006388
Chris Masona061fc82008-05-07 11:43:44 -04006389 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006390 array_size = btrfs_super_sys_array_size(super_copy);
6391
David Sterba1ffb22c2014-10-31 19:02:42 +01006392 array_ptr = super_copy->sys_chunk_array;
6393 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6394 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006395
David Sterba1ffb22c2014-10-31 19:02:42 +01006396 while (cur_offset < array_size) {
6397 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006398 len = sizeof(*disk_key);
6399 if (cur_offset + len > array_size)
6400 goto out_short_read;
6401
Chris Mason0b86a832008-03-24 15:01:56 -04006402 btrfs_disk_key_to_cpu(&key, disk_key);
6403
David Sterba1ffb22c2014-10-31 19:02:42 +01006404 array_ptr += len;
6405 sb_array_offset += len;
6406 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006407
Chris Mason0d81ba52008-03-24 15:02:07 -04006408 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006409 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006410 /*
6411 * At least one btrfs_chunk with one stripe must be
6412 * present, exact stripe count check comes afterwards
6413 */
6414 len = btrfs_chunk_item_size(1);
6415 if (cur_offset + len > array_size)
6416 goto out_short_read;
6417
6418 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6419 len = btrfs_chunk_item_size(num_stripes);
6420 if (cur_offset + len > array_size)
6421 goto out_short_read;
6422
Chris Mason0d81ba52008-03-24 15:02:07 -04006423 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006424 if (ret)
6425 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006426 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006427 ret = -EIO;
6428 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006429 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006430 array_ptr += len;
6431 sb_array_offset += len;
6432 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006433 }
Chris Masona061fc82008-05-07 11:43:44 -04006434 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006435 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006436
6437out_short_read:
6438 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6439 len, cur_offset);
6440 free_extent_buffer(sb);
6441 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006442}
6443
6444int btrfs_read_chunk_tree(struct btrfs_root *root)
6445{
6446 struct btrfs_path *path;
6447 struct extent_buffer *leaf;
6448 struct btrfs_key key;
6449 struct btrfs_key found_key;
6450 int ret;
6451 int slot;
6452
6453 root = root->fs_info->chunk_root;
6454
6455 path = btrfs_alloc_path();
6456 if (!path)
6457 return -ENOMEM;
6458
Li Zefanb367e472011-12-07 11:38:24 +08006459 mutex_lock(&uuid_mutex);
6460 lock_chunks(root);
6461
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006462 /*
6463 * Read all device items, and then all the chunk items. All
6464 * device items are found before any chunk item (their object id
6465 * is smaller than the lowest possible object id for a chunk
6466 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006467 */
6468 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6469 key.offset = 0;
6470 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006472 if (ret < 0)
6473 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006474 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006475 leaf = path->nodes[0];
6476 slot = path->slots[0];
6477 if (slot >= btrfs_header_nritems(leaf)) {
6478 ret = btrfs_next_leaf(root, path);
6479 if (ret == 0)
6480 continue;
6481 if (ret < 0)
6482 goto error;
6483 break;
6484 }
6485 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006486 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6487 struct btrfs_dev_item *dev_item;
6488 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006489 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006490 ret = read_one_dev(root, leaf, dev_item);
6491 if (ret)
6492 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006493 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6494 struct btrfs_chunk *chunk;
6495 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6496 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006497 if (ret)
6498 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006499 }
6500 path->slots[0]++;
6501 }
Chris Mason0b86a832008-03-24 15:01:56 -04006502 ret = 0;
6503error:
Li Zefanb367e472011-12-07 11:38:24 +08006504 unlock_chunks(root);
6505 mutex_unlock(&uuid_mutex);
6506
Yan Zheng2b820322008-11-17 21:11:30 -05006507 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006508 return ret;
6509}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006510
Miao Xiecb517ea2013-05-15 07:48:19 +00006511void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6512{
6513 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6514 struct btrfs_device *device;
6515
Liu Bo29cc83f2014-05-11 23:14:59 +08006516 while (fs_devices) {
6517 mutex_lock(&fs_devices->device_list_mutex);
6518 list_for_each_entry(device, &fs_devices->devices, dev_list)
6519 device->dev_root = fs_info->dev_root;
6520 mutex_unlock(&fs_devices->device_list_mutex);
6521
6522 fs_devices = fs_devices->seed;
6523 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006524}
6525
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006526static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6527{
6528 int i;
6529
6530 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6531 btrfs_dev_stat_reset(dev, i);
6532}
6533
6534int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6535{
6536 struct btrfs_key key;
6537 struct btrfs_key found_key;
6538 struct btrfs_root *dev_root = fs_info->dev_root;
6539 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6540 struct extent_buffer *eb;
6541 int slot;
6542 int ret = 0;
6543 struct btrfs_device *device;
6544 struct btrfs_path *path = NULL;
6545 int i;
6546
6547 path = btrfs_alloc_path();
6548 if (!path) {
6549 ret = -ENOMEM;
6550 goto out;
6551 }
6552
6553 mutex_lock(&fs_devices->device_list_mutex);
6554 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6555 int item_size;
6556 struct btrfs_dev_stats_item *ptr;
6557
6558 key.objectid = 0;
6559 key.type = BTRFS_DEV_STATS_KEY;
6560 key.offset = device->devid;
6561 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6562 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006563 __btrfs_reset_dev_stats(device);
6564 device->dev_stats_valid = 1;
6565 btrfs_release_path(path);
6566 continue;
6567 }
6568 slot = path->slots[0];
6569 eb = path->nodes[0];
6570 btrfs_item_key_to_cpu(eb, &found_key, slot);
6571 item_size = btrfs_item_size_nr(eb, slot);
6572
6573 ptr = btrfs_item_ptr(eb, slot,
6574 struct btrfs_dev_stats_item);
6575
6576 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6577 if (item_size >= (1 + i) * sizeof(__le64))
6578 btrfs_dev_stat_set(device, i,
6579 btrfs_dev_stats_value(eb, ptr, i));
6580 else
6581 btrfs_dev_stat_reset(device, i);
6582 }
6583
6584 device->dev_stats_valid = 1;
6585 btrfs_dev_stat_print_on_load(device);
6586 btrfs_release_path(path);
6587 }
6588 mutex_unlock(&fs_devices->device_list_mutex);
6589
6590out:
6591 btrfs_free_path(path);
6592 return ret < 0 ? ret : 0;
6593}
6594
6595static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6596 struct btrfs_root *dev_root,
6597 struct btrfs_device *device)
6598{
6599 struct btrfs_path *path;
6600 struct btrfs_key key;
6601 struct extent_buffer *eb;
6602 struct btrfs_dev_stats_item *ptr;
6603 int ret;
6604 int i;
6605
6606 key.objectid = 0;
6607 key.type = BTRFS_DEV_STATS_KEY;
6608 key.offset = device->devid;
6609
6610 path = btrfs_alloc_path();
6611 BUG_ON(!path);
6612 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6613 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006614 printk_in_rcu(KERN_WARNING "BTRFS: "
6615 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006616 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006617 goto out;
6618 }
6619
6620 if (ret == 0 &&
6621 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6622 /* need to delete old one and insert a new one */
6623 ret = btrfs_del_item(trans, dev_root, path);
6624 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006625 printk_in_rcu(KERN_WARNING "BTRFS: "
6626 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006627 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006628 goto out;
6629 }
6630 ret = 1;
6631 }
6632
6633 if (ret == 1) {
6634 /* need to insert a new item */
6635 btrfs_release_path(path);
6636 ret = btrfs_insert_empty_item(trans, dev_root, path,
6637 &key, sizeof(*ptr));
6638 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006639 printk_in_rcu(KERN_WARNING "BTRFS: "
6640 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006641 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006642 goto out;
6643 }
6644 }
6645
6646 eb = path->nodes[0];
6647 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6648 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6649 btrfs_set_dev_stats_value(eb, ptr, i,
6650 btrfs_dev_stat_read(device, i));
6651 btrfs_mark_buffer_dirty(eb);
6652
6653out:
6654 btrfs_free_path(path);
6655 return ret;
6656}
6657
6658/*
6659 * called from commit_transaction. Writes all changed device stats to disk.
6660 */
6661int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6662 struct btrfs_fs_info *fs_info)
6663{
6664 struct btrfs_root *dev_root = fs_info->dev_root;
6665 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6666 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006667 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006668 int ret = 0;
6669
6670 mutex_lock(&fs_devices->device_list_mutex);
6671 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006672 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006673 continue;
6674
Miao Xieaddc3fa2014-07-24 11:37:11 +08006675 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006676 ret = update_dev_stat_item(trans, dev_root, device);
6677 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006678 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006679 }
6680 mutex_unlock(&fs_devices->device_list_mutex);
6681
6682 return ret;
6683}
6684
Stefan Behrens442a4f62012-05-25 16:06:08 +02006685void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6686{
6687 btrfs_dev_stat_inc(dev, index);
6688 btrfs_dev_stat_print_on_error(dev);
6689}
6690
Eric Sandeen48a3b632013-04-25 20:41:01 +00006691static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006692{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006693 if (!dev->dev_stats_valid)
6694 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006695 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6696 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006697 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006698 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6699 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6700 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006701 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6702 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006703}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006704
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006705static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6706{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006707 int i;
6708
6709 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6710 if (btrfs_dev_stat_read(dev, i) != 0)
6711 break;
6712 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6713 return; /* all values == 0, suppress message */
6714
Frank Holtonefe120a2013-12-20 11:37:06 -05006715 printk_in_rcu(KERN_INFO "BTRFS: "
6716 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006717 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006718 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6719 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6720 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6721 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6722 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6723}
6724
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006725int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006726 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006727{
6728 struct btrfs_device *dev;
6729 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6730 int i;
6731
6732 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006733 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006734 mutex_unlock(&fs_devices->device_list_mutex);
6735
6736 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006737 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006738 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006739 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006740 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006741 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006742 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006743 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6744 if (stats->nr_items > i)
6745 stats->values[i] =
6746 btrfs_dev_stat_read_and_reset(dev, i);
6747 else
6748 btrfs_dev_stat_reset(dev, i);
6749 }
6750 } else {
6751 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6752 if (stats->nr_items > i)
6753 stats->values[i] = btrfs_dev_stat_read(dev, i);
6754 }
6755 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6756 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6757 return 0;
6758}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006759
6760int btrfs_scratch_superblock(struct btrfs_device *device)
6761{
6762 struct buffer_head *bh;
6763 struct btrfs_super_block *disk_super;
6764
6765 bh = btrfs_read_dev_super(device->bdev);
6766 if (!bh)
6767 return -EINVAL;
6768 disk_super = (struct btrfs_super_block *)bh->b_data;
6769
6770 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6771 set_buffer_dirty(bh);
6772 sync_dirty_buffer(bh);
6773 brelse(bh);
6774
6775 return 0;
6776}
Miao Xie935e5cc2014-09-03 21:35:33 +08006777
6778/*
6779 * Update the size of all devices, which is used for writing out the
6780 * super blocks.
6781 */
6782void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6783{
6784 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6785 struct btrfs_device *curr, *next;
6786
6787 if (list_empty(&fs_devices->resized_devices))
6788 return;
6789
6790 mutex_lock(&fs_devices->device_list_mutex);
6791 lock_chunks(fs_info->dev_root);
6792 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6793 resized_list) {
6794 list_del_init(&curr->resized_list);
6795 curr->commit_total_bytes = curr->disk_total_bytes;
6796 }
6797 unlock_chunks(fs_info->dev_root);
6798 mutex_unlock(&fs_devices->device_list_mutex);
6799}
Miao Xiece7213c2014-09-03 21:35:34 +08006800
6801/* Must be invoked during the transaction commit */
6802void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6803 struct btrfs_transaction *transaction)
6804{
6805 struct extent_map *em;
6806 struct map_lookup *map;
6807 struct btrfs_device *dev;
6808 int i;
6809
6810 if (list_empty(&transaction->pending_chunks))
6811 return;
6812
6813 /* In order to kick the device replace finish process */
6814 lock_chunks(root);
6815 list_for_each_entry(em, &transaction->pending_chunks, list) {
6816 map = (struct map_lookup *)em->bdev;
6817
6818 for (i = 0; i < map->num_stripes; i++) {
6819 dev = map->stripes[i].dev;
6820 dev->commit_bytes_used = dev->bytes_used;
6821 }
6822 }
6823 unlock_chunks(root);
6824}
Anand Jain5a13f432015-03-10 06:38:31 +08006825
6826void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6827{
6828 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6829 while (fs_devices) {
6830 fs_devices->fs_info = fs_info;
6831 fs_devices = fs_devices->seed;
6832 }
6833}
6834
6835void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6836{
6837 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6838 while (fs_devices) {
6839 fs_devices->fs_info = NULL;
6840 fs_devices = fs_devices->seed;
6841 }
6842}