blob: e335938825e162fdf5e811edd046234e88074620 [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);
David Sterba87ad58c2015-11-27 18:49:39 +0100128static void btrfs_close_one_device(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -0500129
Miao Xie67a2c452014-09-03 21:35:43 +0800130DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400131static LIST_HEAD(fs_uuids);
Anand Jainc73eccf2015-03-10 06:38:30 +0800132struct list_head *btrfs_get_fs_uuids(void)
133{
134 return &fs_uuids;
135}
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136
Ilya Dryomov2208a372013-08-12 14:33:03 +0300137static struct btrfs_fs_devices *__alloc_fs_devices(void)
138{
139 struct btrfs_fs_devices *fs_devs;
140
141 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
142 if (!fs_devs)
143 return ERR_PTR(-ENOMEM);
144
145 mutex_init(&fs_devs->device_list_mutex);
146
147 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +0800148 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300149 INIT_LIST_HEAD(&fs_devs->alloc_list);
150 INIT_LIST_HEAD(&fs_devs->list);
151
152 return fs_devs;
153}
154
155/**
156 * alloc_fs_devices - allocate struct btrfs_fs_devices
157 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
158 * generated.
159 *
160 * Return: a pointer to a new &struct btrfs_fs_devices on success;
161 * ERR_PTR() on error. Returned struct is not linked onto any lists and
162 * can be destroyed with kfree() right away.
163 */
164static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
165{
166 struct btrfs_fs_devices *fs_devs;
167
168 fs_devs = __alloc_fs_devices();
169 if (IS_ERR(fs_devs))
170 return fs_devs;
171
172 if (fsid)
173 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
174 else
175 generate_random_uuid(fs_devs->fsid);
176
177 return fs_devs;
178}
179
Yan Zhenge4404d62008-12-12 10:03:26 -0500180static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
181{
182 struct btrfs_device *device;
183 WARN_ON(fs_devices->opened);
184 while (!list_empty(&fs_devices->devices)) {
185 device = list_entry(fs_devices->devices.next,
186 struct btrfs_device, dev_list);
187 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400188 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500189 kfree(device);
190 }
191 kfree(fs_devices);
192}
193
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000194static void btrfs_kobject_uevent(struct block_device *bdev,
195 enum kobject_action action)
196{
197 int ret;
198
199 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
200 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500201 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000202 action,
203 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
204 &disk_to_dev(bdev->bd_disk)->kobj);
205}
206
Jeff Mahoney143bede2012-03-01 14:56:26 +0100207void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400208{
209 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400210
Yan Zheng2b820322008-11-17 21:11:30 -0500211 while (!list_empty(&fs_uuids)) {
212 fs_devices = list_entry(fs_uuids.next,
213 struct btrfs_fs_devices, list);
214 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500215 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400216 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400217}
218
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300219static struct btrfs_device *__alloc_device(void)
220{
221 struct btrfs_device *dev;
222
223 dev = kzalloc(sizeof(*dev), GFP_NOFS);
224 if (!dev)
225 return ERR_PTR(-ENOMEM);
226
227 INIT_LIST_HEAD(&dev->dev_list);
228 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800229 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300230
231 spin_lock_init(&dev->io_lock);
232
233 spin_lock_init(&dev->reada_lock);
234 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800235 atomic_set(&dev->dev_stats_ccnt, 0);
Mel Gormand0164ad2015-11-06 16:28:21 -0800236 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
237 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300238
239 return dev;
240}
241
Chris Masona1b32a52008-09-05 16:09:51 -0400242static noinline struct btrfs_device *__find_device(struct list_head *head,
243 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400244{
245 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400246
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500247 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400248 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400249 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400250 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400251 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400252 }
253 return NULL;
254}
255
Chris Masona1b32a52008-09-05 16:09:51 -0400256static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400257{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400258 struct btrfs_fs_devices *fs_devices;
259
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500260 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400261 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
262 return fs_devices;
263 }
264 return NULL;
265}
266
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100267static int
268btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
269 int flush, struct block_device **bdev,
270 struct buffer_head **bh)
271{
272 int ret;
273
274 *bdev = blkdev_get_by_path(device_path, flags, holder);
275
276 if (IS_ERR(*bdev)) {
277 ret = PTR_ERR(*bdev);
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);
Anand Jain92fc03f2015-08-14 18:32:51 +0800290 if (IS_ERR(*bh)) {
291 ret = PTR_ERR(*bh);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100292 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
David Sterbaee863952015-02-16 19:41:40 +0100424 /*
425 * atomic_dec_return implies a barrier for waitqueue_active
426 */
Josef Bacik66657b32012-08-01 15:36:24 -0400427 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400428 waitqueue_active(&fs_info->async_submit_wait))
429 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400430
Jens Axboedac56212015-04-17 16:23:59 -0600431 BUG_ON(atomic_read(&cur->__bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400432
Chris Mason2ab1ba62011-08-04 14:28:36 -0400433 /*
434 * if we're doing the sync list, record that our
435 * plug has some sync requests on it
436 *
437 * If we're doing the regular list and there are
438 * sync requests sitting around, unplug before
439 * we add more
440 */
441 if (pending_bios == &device->pending_sync_bios) {
442 sync_pending = 1;
443 } else if (sync_pending) {
444 blk_finish_plug(&plug);
445 blk_start_plug(&plug);
446 sync_pending = 0;
447 }
448
Stefan Behrens21adbd52011-11-09 13:44:05 +0100449 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400450 num_run++;
451 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100452
453 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400454
455 /*
456 * we made progress, there is more work to do and the bdi
457 * is now congested. Back off and let other work structs
458 * run instead
459 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400460 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500461 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400462 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400463
Chris Masonb765ead2009-04-03 10:27:10 -0400464 ioc = current->io_context;
465
466 /*
467 * the main goal here is that we don't want to
468 * block if we're going to be able to submit
469 * more requests without blocking.
470 *
471 * This code does two great things, it pokes into
472 * the elevator code from a filesystem _and_
473 * it makes assumptions about how batching works.
474 */
475 if (ioc && ioc->nr_batch_requests > 0 &&
476 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
477 (last_waited == 0 ||
478 ioc->last_waited == last_waited)) {
479 /*
480 * we want to go through our batch of
481 * requests and stop. So, we copy out
482 * the ioc->last_waited time and test
483 * against it before looping
484 */
485 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100486 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400487 continue;
488 }
Chris Mason8b712842008-06-11 16:50:36 -0400489 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400490 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500491 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400492
493 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +0800494 btrfs_queue_work(fs_info->submit_workers,
495 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400496 goto done;
497 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500498 /* unplug every 64 requests just for good measure */
499 if (batch_run % 64 == 0) {
500 blk_finish_plug(&plug);
501 blk_start_plug(&plug);
502 sync_pending = 0;
503 }
Chris Mason8b712842008-06-11 16:50:36 -0400504 }
Chris Masonffbd5172009-04-20 15:50:09 -0400505
Chris Mason51684082010-03-10 15:33:32 -0500506 cond_resched();
507 if (again)
508 goto loop;
509
510 spin_lock(&device->io_lock);
511 if (device->pending_bios.head || device->pending_sync_bios.head)
512 goto loop_lock;
513 spin_unlock(&device->io_lock);
514
Chris Mason8b712842008-06-11 16:50:36 -0400515done:
Chris Mason211588a2011-04-19 20:12:40 -0400516 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400517}
518
Christoph Hellwigb2950862008-12-02 09:54:17 -0500519static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400520{
521 struct btrfs_device *device;
522
523 device = container_of(work, struct btrfs_device, work);
524 run_scheduled_bios(device);
525}
526
Anand Jain4fde46f2015-06-17 21:10:48 +0800527
528void btrfs_free_stale_device(struct btrfs_device *cur_dev)
529{
530 struct btrfs_fs_devices *fs_devs;
531 struct btrfs_device *dev;
532
533 if (!cur_dev->name)
534 return;
535
536 list_for_each_entry(fs_devs, &fs_uuids, list) {
537 int del = 1;
538
539 if (fs_devs->opened)
540 continue;
541 if (fs_devs->seeding)
542 continue;
543
544 list_for_each_entry(dev, &fs_devs->devices, dev_list) {
545
546 if (dev == cur_dev)
547 continue;
548 if (!dev->name)
549 continue;
550
551 /*
552 * Todo: This won't be enough. What if the same device
553 * comes back (with new uuid and) with its mapper path?
554 * But for now, this does help as mostly an admin will
555 * either use mapper or non mapper path throughout.
556 */
557 rcu_read_lock();
558 del = strcmp(rcu_str_deref(dev->name),
559 rcu_str_deref(cur_dev->name));
560 rcu_read_unlock();
561 if (!del)
562 break;
563 }
564
565 if (!del) {
566 /* delete the stale device */
567 if (fs_devs->num_devices == 1) {
568 btrfs_sysfs_remove_fsid(fs_devs);
569 list_del(&fs_devs->list);
570 free_fs_devices(fs_devs);
571 } else {
572 fs_devs->num_devices--;
573 list_del(&dev->dev_list);
574 rcu_string_free(dev->name);
575 kfree(dev);
576 }
577 break;
578 }
579 }
580}
581
David Sterba60999ca2014-03-26 18:26:36 +0100582/*
583 * Add new device to list of registered devices
584 *
585 * Returns:
586 * 1 - first time device is seen
587 * 0 - device already known
588 * < 0 - error
589 */
Chris Masona1b32a52008-09-05 16:09:51 -0400590static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400591 struct btrfs_super_block *disk_super,
592 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
593{
594 struct btrfs_device *device;
595 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400596 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100597 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400598 u64 found_transid = btrfs_super_generation(disk_super);
599
600 fs_devices = find_fsid(disk_super->fsid);
601 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300602 fs_devices = alloc_fs_devices(disk_super->fsid);
603 if (IS_ERR(fs_devices))
604 return PTR_ERR(fs_devices);
605
Chris Mason8a4b83c2008-03-24 15:02:07 -0400606 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300607
Chris Mason8a4b83c2008-03-24 15:02:07 -0400608 device = NULL;
609 } else {
Chris Masona4437552008-04-18 10:29:38 -0400610 device = __find_device(&fs_devices->devices, devid,
611 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400612 }
Miao Xie443f24f2014-07-24 11:37:15 +0800613
Chris Mason8a4b83c2008-03-24 15:02:07 -0400614 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500615 if (fs_devices->opened)
616 return -EBUSY;
617
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300618 device = btrfs_alloc_device(NULL, &devid,
619 disk_super->dev_item.uuid);
620 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300622 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400623 }
Josef Bacik606686e2012-06-04 14:03:51 -0400624
625 name = rcu_string_strdup(path, GFP_NOFS);
626 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400627 kfree(device);
628 return -ENOMEM;
629 }
Josef Bacik606686e2012-06-04 14:03:51 -0400630 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200631
Chris Masone5e9a522009-06-10 15:17:02 -0400632 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000633 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100634 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400635 mutex_unlock(&fs_devices->device_list_mutex);
636
David Sterba60999ca2014-03-26 18:26:36 +0100637 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500638 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400639 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800640 /*
641 * When FS is already mounted.
642 * 1. If you are here and if the device->name is NULL that
643 * means this device was missing at time of FS mount.
644 * 2. If you are here and if the device->name is different
645 * from 'path' that means either
646 * a. The same device disappeared and reappeared with
647 * different name. or
648 * b. The missing-disk-which-was-replaced, has
649 * reappeared now.
650 *
651 * We must allow 1 and 2a above. But 2b would be a spurious
652 * and unintentional.
653 *
654 * Further in case of 1 and 2a above, the disk at 'path'
655 * would have missed some transaction when it was away and
656 * in case of 2a the stale bdev has to be updated as well.
657 * 2b must not be allowed at all time.
658 */
659
660 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700661 * For now, we do allow update to btrfs_fs_device through the
662 * btrfs dev scan cli after FS has been mounted. We're still
663 * tracking a problem where systems fail mount by subvolume id
664 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800665 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700666 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800667 /*
668 * That is if the FS is _not_ mounted and if you
669 * are here, that means there is more than one
670 * disk with same uuid and devid.We keep the one
671 * with larger generation number or the last-in if
672 * generation are equal.
673 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700674 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800675 }
Anand Jainb96de002014-07-03 18:22:05 +0800676
Josef Bacik606686e2012-06-04 14:03:51 -0400677 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000678 if (!name)
679 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400680 rcu_string_free(device->name);
681 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500682 if (device->missing) {
683 fs_devices->missing_devices--;
684 device->missing = 0;
685 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400686 }
687
Anand Jain77bdae42014-07-03 18:22:06 +0800688 /*
689 * Unmount does not free the btrfs_device struct but would zero
690 * generation along with most of the other members. So just update
691 * it back. We need it to pick the disk with largest generation
692 * (as above).
693 */
694 if (!fs_devices->opened)
695 device->generation = found_transid;
696
Anand Jain4fde46f2015-06-17 21:10:48 +0800697 /*
698 * if there is new btrfs on an already registered device,
699 * then remove the stale device entry.
700 */
701 btrfs_free_stale_device(device);
702
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100704
705 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400706}
707
Yan Zhenge4404d62008-12-12 10:03:26 -0500708static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
709{
710 struct btrfs_fs_devices *fs_devices;
711 struct btrfs_device *device;
712 struct btrfs_device *orig_dev;
713
Ilya Dryomov2208a372013-08-12 14:33:03 +0300714 fs_devices = alloc_fs_devices(orig->fsid);
715 if (IS_ERR(fs_devices))
716 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500717
Miao Xieadbbb862014-09-03 21:35:42 +0800718 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400719 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500720
Xiao Guangrong46224702011-04-20 10:08:47 +0000721 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500722 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400723 struct rcu_string *name;
724
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300725 device = btrfs_alloc_device(NULL, &orig_dev->devid,
726 orig_dev->uuid);
727 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500728 goto error;
729
Josef Bacik606686e2012-06-04 14:03:51 -0400730 /*
731 * This is ok to do without rcu read locked because we hold the
732 * uuid mutex so nothing we touch in here is going to disappear.
733 */
Anand Jaine755f782014-06-30 17:12:47 +0800734 if (orig_dev->name) {
735 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
736 if (!name) {
737 kfree(device);
738 goto error;
739 }
740 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400741 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500742
Yan Zhenge4404d62008-12-12 10:03:26 -0500743 list_add(&device->dev_list, &fs_devices->devices);
744 device->fs_devices = fs_devices;
745 fs_devices->num_devices++;
746 }
Miao Xieadbbb862014-09-03 21:35:42 +0800747 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500748 return fs_devices;
749error:
Miao Xieadbbb862014-09-03 21:35:42 +0800750 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500751 free_fs_devices(fs_devices);
752 return ERR_PTR(-ENOMEM);
753}
754
Eric Sandeen9eaed212014-08-01 18:12:35 -0500755void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400756{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500757 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800758 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500759
Chris Masondfe25022008-05-13 13:46:40 -0400760 mutex_lock(&uuid_mutex);
761again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000762 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500763 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500764 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100765 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800766 (!latest_dev ||
767 device->generation > latest_dev->generation)) {
768 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500769 }
Yan Zheng2b820322008-11-17 21:11:30 -0500770 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500771 }
Yan Zheng2b820322008-11-17 21:11:30 -0500772
Stefan Behrens8dabb742012-11-06 13:15:27 +0100773 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
774 /*
775 * In the first step, keep the device which has
776 * the correct fsid and the devid that is used
777 * for the dev_replace procedure.
778 * In the second step, the dev_replace state is
779 * read from the device tree and it is known
780 * whether the procedure is really active or
781 * not, which means whether this device is
782 * used or whether it should be removed.
783 */
784 if (step == 0 || device->is_tgtdev_for_dev_replace) {
785 continue;
786 }
787 }
Yan Zheng2b820322008-11-17 21:11:30 -0500788 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100789 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500790 device->bdev = NULL;
791 fs_devices->open_devices--;
792 }
793 if (device->writeable) {
794 list_del_init(&device->dev_alloc_list);
795 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100796 if (!device->is_tgtdev_for_dev_replace)
797 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500798 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500799 list_del_init(&device->dev_list);
800 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400801 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500802 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400803 }
Yan Zheng2b820322008-11-17 21:11:30 -0500804
805 if (fs_devices->seed) {
806 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500807 goto again;
808 }
809
Miao Xie443f24f2014-07-24 11:37:15 +0800810 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500811
Chris Masondfe25022008-05-13 13:46:40 -0400812 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400813}
Chris Masona0af4692008-05-13 16:03:06 -0400814
Xiao Guangrong1f781602011-04-20 10:09:16 +0000815static void __free_device(struct work_struct *work)
816{
817 struct btrfs_device *device;
818
819 device = container_of(work, struct btrfs_device, rcu_work);
820
821 if (device->bdev)
822 blkdev_put(device->bdev, device->mode);
823
Josef Bacik606686e2012-06-04 14:03:51 -0400824 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000825 kfree(device);
826}
827
828static void free_device(struct rcu_head *head)
829{
830 struct btrfs_device *device;
831
832 device = container_of(head, struct btrfs_device, rcu);
833
834 INIT_WORK(&device->rcu_work, __free_device);
835 schedule_work(&device->rcu_work);
836}
837
Yan Zheng2b820322008-11-17 21:11:30 -0500838static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400839{
Sasha Levin2037a092015-05-12 19:31:37 -0400840 struct btrfs_device *device, *tmp;
Yan Zhenge4404d62008-12-12 10:03:26 -0500841
Yan Zheng2b820322008-11-17 21:11:30 -0500842 if (--fs_devices->opened > 0)
843 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400844
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000845 mutex_lock(&fs_devices->device_list_mutex);
Sasha Levin2037a092015-05-12 19:31:37 -0400846 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
Anand Jainf190aa42015-08-14 18:33:05 +0800847 btrfs_close_one_device(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400848 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000849 mutex_unlock(&fs_devices->device_list_mutex);
850
Yan Zhenge4404d62008-12-12 10:03:26 -0500851 WARN_ON(fs_devices->open_devices);
852 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500853 fs_devices->opened = 0;
854 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500855
Chris Mason8a4b83c2008-03-24 15:02:07 -0400856 return 0;
857}
858
Yan Zheng2b820322008-11-17 21:11:30 -0500859int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
860{
Yan Zhenge4404d62008-12-12 10:03:26 -0500861 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500862 int ret;
863
864 mutex_lock(&uuid_mutex);
865 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500866 if (!fs_devices->opened) {
867 seed_devices = fs_devices->seed;
868 fs_devices->seed = NULL;
869 }
Yan Zheng2b820322008-11-17 21:11:30 -0500870 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500871
872 while (seed_devices) {
873 fs_devices = seed_devices;
874 seed_devices = fs_devices->seed;
875 __btrfs_close_devices(fs_devices);
876 free_fs_devices(fs_devices);
877 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000878 /*
879 * Wait for rcu kworkers under __btrfs_close_devices
880 * to finish all blkdev_puts so device is really
881 * free when umount is done.
882 */
883 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500884 return ret;
885}
886
Yan Zhenge4404d62008-12-12 10:03:26 -0500887static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
888 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400889{
Josef Bacikd5e20032011-08-04 14:52:27 +0000890 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400891 struct block_device *bdev;
892 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400893 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800894 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400895 struct buffer_head *bh;
896 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400897 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500898 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400899 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400900
Tejun Heod4d77622010-11-13 11:55:18 +0100901 flags |= FMODE_EXCL;
902
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500903 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400904 if (device->bdev)
905 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400906 if (!device->name)
907 continue;
908
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000909 /* Just open everything we can; ignore failures here */
910 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
911 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100912 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400913
914 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000915 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400916 if (devid != device->devid)
917 goto error_brelse;
918
Yan Zheng2b820322008-11-17 21:11:30 -0500919 if (memcmp(device->uuid, disk_super->dev_item.uuid,
920 BTRFS_UUID_SIZE))
921 goto error_brelse;
922
923 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800924 if (!latest_dev ||
925 device->generation > latest_dev->generation)
926 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400927
Yan Zheng2b820322008-11-17 21:11:30 -0500928 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
929 device->writeable = 0;
930 } else {
931 device->writeable = !bdev_read_only(bdev);
932 seeding = 0;
933 }
934
Josef Bacikd5e20032011-08-04 14:52:27 +0000935 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800936 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000937 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000938
Chris Mason8a4b83c2008-03-24 15:02:07 -0400939 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400940 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500941 device->mode = flags;
942
Chris Masonc2898112009-06-10 09:51:32 -0400943 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
944 fs_devices->rotating = 1;
945
Chris Masona0af4692008-05-13 16:03:06 -0400946 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300947 if (device->writeable &&
948 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500949 fs_devices->rw_devices++;
950 list_add(&device->dev_alloc_list,
951 &fs_devices->alloc_list);
952 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000953 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400954 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400955
Chris Masona0af4692008-05-13 16:03:06 -0400956error_brelse:
957 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100958 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400959 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400960 }
Chris Masona0af4692008-05-13 16:03:06 -0400961 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300962 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400963 goto out;
964 }
Yan Zheng2b820322008-11-17 21:11:30 -0500965 fs_devices->seeding = seeding;
966 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800967 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500968 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400969out:
Yan Zheng2b820322008-11-17 21:11:30 -0500970 return ret;
971}
972
973int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500974 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500975{
976 int ret;
977
978 mutex_lock(&uuid_mutex);
979 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500980 fs_devices->opened++;
981 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500982 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500983 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500984 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400985 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400986 return ret;
987}
988
David Sterba6f60cbd2013-02-15 11:31:02 -0700989/*
990 * Look for a btrfs signature on a device. This may be called out of the mount path
991 * and we are not allowed to call set_blocksize during the scan. The superblock
992 * is read via pagecache
993 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500994int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400995 struct btrfs_fs_devices **fs_devices_ret)
996{
997 struct btrfs_super_block *disk_super;
998 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700999 struct page *page;
1000 void *p;
1001 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001002 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -04001003 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -04001004 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001005 u64 bytenr;
1006 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -04001007
David Sterba6f60cbd2013-02-15 11:31:02 -07001008 /*
1009 * we would like to check all the supers, but that would make
1010 * a btrfs mount succeed after a mkfs from a different FS.
1011 * So, we need to add a special mount option to scan for
1012 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
1013 */
1014 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +01001015 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -05001016 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -07001017
1018 bdev = blkdev_get_by_path(path, flags, holder);
1019
1020 if (IS_ERR(bdev)) {
1021 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001022 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -07001023 }
1024
1025 /* make sure our super fits in the device */
1026 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
1027 goto error_bdev_put;
1028
1029 /* make sure our super fits in the page */
1030 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
1031 goto error_bdev_put;
1032
1033 /* make sure our super doesn't straddle pages on disk */
1034 index = bytenr >> PAGE_CACHE_SHIFT;
1035 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
1036 goto error_bdev_put;
1037
1038 /* pull in the page with our super */
1039 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
1040 index, GFP_NOFS);
1041
1042 if (IS_ERR_OR_NULL(page))
1043 goto error_bdev_put;
1044
1045 p = kmap(page);
1046
1047 /* align our pointer to the offset of the super block */
1048 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
1049
1050 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +08001051 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -07001052 goto error_unmap;
1053
Xiao Guangronga3438322010-01-06 11:48:18 +00001054 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -04001055 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -04001056 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -07001057
Chris Mason8a4b83c2008-03-24 15:02:07 -04001058 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +01001059 if (ret > 0) {
1060 if (disk_super->label[0]) {
1061 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
1062 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
1063 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
1064 } else {
1065 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
1066 }
1067
1068 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
1069 ret = 0;
1070 }
Josef Bacik02db0842012-06-21 16:03:58 -04001071 if (!ret && fs_devices_ret)
1072 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -07001073
1074error_unmap:
1075 kunmap(page);
1076 page_cache_release(page);
1077
1078error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +01001079 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001080error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001081 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -04001082 return ret;
1083}
Chris Mason0b86a832008-03-24 15:01:56 -04001084
Miao Xie6d07bce2011-01-05 10:07:31 +00001085/* helper to account the used device space in the range */
1086int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
1087 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -04001088{
1089 struct btrfs_key key;
1090 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +00001091 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05001092 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +00001093 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001094 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +00001095 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001096 struct extent_buffer *l;
1097
Miao Xie6d07bce2011-01-05 10:07:31 +00001098 *length = 0;
1099
Stefan Behrens63a212a2012-11-05 18:29:28 +01001100 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +00001101 return 0;
1102
Yan Zheng2b820322008-11-17 21:11:30 -05001103 path = btrfs_alloc_path();
1104 if (!path)
1105 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001106 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -04001107
Chris Mason0b86a832008-03-24 15:01:56 -04001108 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +00001109 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001110 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +00001111
1112 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001113 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001114 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001115 if (ret > 0) {
1116 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1117 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001118 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001119 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001120
Chris Mason0b86a832008-03-24 15:01:56 -04001121 while (1) {
1122 l = path->nodes[0];
1123 slot = path->slots[0];
1124 if (slot >= btrfs_header_nritems(l)) {
1125 ret = btrfs_next_leaf(root, path);
1126 if (ret == 0)
1127 continue;
1128 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001129 goto out;
1130
1131 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001132 }
1133 btrfs_item_key_to_cpu(l, &key, slot);
1134
1135 if (key.objectid < device->devid)
1136 goto next;
1137
1138 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001139 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001140
David Sterba962a2982014-06-04 18:41:45 +02001141 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001142 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001143
Chris Mason0b86a832008-03-24 15:01:56 -04001144 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001145 extent_end = key.offset + btrfs_dev_extent_length(l,
1146 dev_extent);
1147 if (key.offset <= start && extent_end > end) {
1148 *length = end - start + 1;
1149 break;
1150 } else if (key.offset <= start && extent_end > start)
1151 *length += extent_end - start;
1152 else if (key.offset > start && extent_end <= end)
1153 *length += extent_end - key.offset;
1154 else if (key.offset > start && key.offset <= end) {
1155 *length += end - key.offset + 1;
1156 break;
1157 } else if (key.offset > end)
1158 break;
1159
1160next:
1161 path->slots[0]++;
1162 }
1163 ret = 0;
1164out:
1165 btrfs_free_path(path);
1166 return ret;
1167}
1168
Jeff Mahoney499f3772015-06-15 09:41:17 -04001169static int contains_pending_extent(struct btrfs_transaction *transaction,
Josef Bacik6df9a952013-06-27 13:22:46 -04001170 struct btrfs_device *device,
1171 u64 *start, u64 len)
1172{
Jeff Mahoney499f3772015-06-15 09:41:17 -04001173 struct btrfs_fs_info *fs_info = device->dev_root->fs_info;
Josef Bacik6df9a952013-06-27 13:22:46 -04001174 struct extent_map *em;
Jeff Mahoney499f3772015-06-15 09:41:17 -04001175 struct list_head *search_list = &fs_info->pinned_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001176 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001177 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001178
Jeff Mahoney499f3772015-06-15 09:41:17 -04001179 if (transaction)
1180 search_list = &transaction->pending_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001181again:
1182 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001183 struct map_lookup *map;
1184 int i;
1185
1186 map = (struct map_lookup *)em->bdev;
1187 for (i = 0; i < map->num_stripes; i++) {
Filipe Mananac152b632015-05-14 10:46:03 +01001188 u64 end;
1189
Josef Bacik6df9a952013-06-27 13:22:46 -04001190 if (map->stripes[i].dev != device)
1191 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001192 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001193 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001194 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001195 continue;
Filipe Mananac152b632015-05-14 10:46:03 +01001196 /*
1197 * Make sure that while processing the pinned list we do
1198 * not override our *start with a lower value, because
1199 * we can have pinned chunks that fall within this
1200 * device hole and that have lower physical addresses
1201 * than the pending chunks we processed before. If we
1202 * do not take this special care we can end up getting
1203 * 2 pending chunks that start at the same physical
1204 * device offsets because the end offset of a pinned
1205 * chunk can be equal to the start offset of some
1206 * pending chunk.
1207 */
1208 end = map->stripes[i].physical + em->orig_block_len;
1209 if (end > *start) {
1210 *start = end;
1211 ret = 1;
1212 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001213 }
1214 }
Jeff Mahoney499f3772015-06-15 09:41:17 -04001215 if (search_list != &fs_info->pinned_chunks) {
1216 search_list = &fs_info->pinned_chunks;
Filipe Manana04216822014-11-27 21:14:15 +00001217 goto again;
1218 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001219
1220 return ret;
1221}
1222
1223
Chris Mason0b86a832008-03-24 15:01:56 -04001224/*
Jeff Mahoney499f3772015-06-15 09:41:17 -04001225 * find_free_dev_extent_start - find free space in the specified device
1226 * @device: the device which we search the free space in
1227 * @num_bytes: the size of the free space that we need
1228 * @search_start: the position from which to begin the search
1229 * @start: store the start of the free space.
1230 * @len: the size of the free space. that we find, or the size
1231 * of the max free space if we don't find suitable free space
Miao Xie7bfc8372011-01-05 10:07:26 +00001232 *
Chris Mason0b86a832008-03-24 15:01:56 -04001233 * this uses a pretty simple search, the expectation is that it is
1234 * called very infrequently and that a given device has a small number
1235 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001236 *
1237 * @start is used to store the start of the free space if we find. But if we
1238 * don't find suitable free space, it will be used to store the start position
1239 * of the max free space.
1240 *
1241 * @len is used to store the size of the free space that we find.
1242 * But if we don't find suitable free space, it is used to store the size of
1243 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001244 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001245int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1246 struct btrfs_device *device, u64 num_bytes,
1247 u64 search_start, u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001248{
1249 struct btrfs_key key;
1250 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001251 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001252 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001253 u64 hole_size;
1254 u64 max_hole_start;
1255 u64 max_hole_size;
1256 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001257 u64 search_end = device->total_bytes;
1258 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001259 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001260 struct extent_buffer *l;
1261
Josef Bacik6df9a952013-06-27 13:22:46 -04001262 path = btrfs_alloc_path();
1263 if (!path)
1264 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001265
Miao Xie7bfc8372011-01-05 10:07:26 +00001266 max_hole_start = search_start;
1267 max_hole_size = 0;
1268
Zhao Leif2ab7612015-02-16 18:52:17 +08001269again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001270 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001271 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001272 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001273 }
1274
Miao Xie7bfc8372011-01-05 10:07:26 +00001275 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001276 path->search_commit_root = 1;
1277 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001278
Chris Mason0b86a832008-03-24 15:01:56 -04001279 key.objectid = device->devid;
1280 key.offset = search_start;
1281 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001282
Li Zefan125ccb02011-12-08 15:07:24 +08001283 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001284 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001285 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001286 if (ret > 0) {
1287 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1288 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001289 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001290 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001291
Chris Mason0b86a832008-03-24 15:01:56 -04001292 while (1) {
1293 l = path->nodes[0];
1294 slot = path->slots[0];
1295 if (slot >= btrfs_header_nritems(l)) {
1296 ret = btrfs_next_leaf(root, path);
1297 if (ret == 0)
1298 continue;
1299 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001300 goto out;
1301
1302 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001303 }
1304 btrfs_item_key_to_cpu(l, &key, slot);
1305
1306 if (key.objectid < device->devid)
1307 goto next;
1308
1309 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001310 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001311
David Sterba962a2982014-06-04 18:41:45 +02001312 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001313 goto next;
1314
Miao Xie7bfc8372011-01-05 10:07:26 +00001315 if (key.offset > search_start) {
1316 hole_size = key.offset - search_start;
1317
Josef Bacik6df9a952013-06-27 13:22:46 -04001318 /*
1319 * Have to check before we set max_hole_start, otherwise
1320 * we could end up sending back this offset anyway.
1321 */
Jeff Mahoney499f3772015-06-15 09:41:17 -04001322 if (contains_pending_extent(transaction, device,
Josef Bacik6df9a952013-06-27 13:22:46 -04001323 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001324 hole_size)) {
1325 if (key.offset >= search_start) {
1326 hole_size = key.offset - search_start;
1327 } else {
1328 WARN_ON_ONCE(1);
1329 hole_size = 0;
1330 }
1331 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001332
Miao Xie7bfc8372011-01-05 10:07:26 +00001333 if (hole_size > max_hole_size) {
1334 max_hole_start = search_start;
1335 max_hole_size = hole_size;
1336 }
1337
1338 /*
1339 * If this free space is greater than which we need,
1340 * it must be the max free space that we have found
1341 * until now, so max_hole_start must point to the start
1342 * of this free space and the length of this free space
1343 * is stored in max_hole_size. Thus, we return
1344 * max_hole_start and max_hole_size and go back to the
1345 * caller.
1346 */
1347 if (hole_size >= num_bytes) {
1348 ret = 0;
1349 goto out;
1350 }
1351 }
1352
Chris Mason0b86a832008-03-24 15:01:56 -04001353 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001354 extent_end = key.offset + btrfs_dev_extent_length(l,
1355 dev_extent);
1356 if (extent_end > search_start)
1357 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001358next:
1359 path->slots[0]++;
1360 cond_resched();
1361 }
Chris Mason0b86a832008-03-24 15:01:56 -04001362
liubo38c01b92011-08-02 02:39:03 +00001363 /*
1364 * At this point, search_start should be the end of
1365 * allocated dev extents, and when shrinking the device,
1366 * search_end may be smaller than search_start.
1367 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001368 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001369 hole_size = search_end - search_start;
1370
Jeff Mahoney499f3772015-06-15 09:41:17 -04001371 if (contains_pending_extent(transaction, device, &search_start,
Zhao Leif2ab7612015-02-16 18:52:17 +08001372 hole_size)) {
1373 btrfs_release_path(path);
1374 goto again;
1375 }
Chris Mason0b86a832008-03-24 15:01:56 -04001376
Zhao Leif2ab7612015-02-16 18:52:17 +08001377 if (hole_size > max_hole_size) {
1378 max_hole_start = search_start;
1379 max_hole_size = hole_size;
1380 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001381 }
1382
Miao Xie7bfc8372011-01-05 10:07:26 +00001383 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001384 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001385 ret = -ENOSPC;
1386 else
1387 ret = 0;
1388
1389out:
Yan Zheng2b820322008-11-17 21:11:30 -05001390 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001391 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001392 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001393 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001394 return ret;
1395}
1396
Jeff Mahoney499f3772015-06-15 09:41:17 -04001397int find_free_dev_extent(struct btrfs_trans_handle *trans,
1398 struct btrfs_device *device, u64 num_bytes,
1399 u64 *start, u64 *len)
1400{
1401 struct btrfs_root *root = device->dev_root;
1402 u64 search_start;
1403
1404 /* FIXME use last free of some kind */
1405
1406 /*
1407 * we don't want to overwrite the superblock on the drive,
1408 * so we make sure to start at an offset of at least 1MB
1409 */
1410 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
1411 return find_free_dev_extent_start(trans->transaction, device,
1412 num_bytes, search_start, start, len);
1413}
1414
Christoph Hellwigb2950862008-12-02 09:54:17 -05001415static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001416 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001417 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001418{
1419 int ret;
1420 struct btrfs_path *path;
1421 struct btrfs_root *root = device->dev_root;
1422 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001423 struct btrfs_key found_key;
1424 struct extent_buffer *leaf = NULL;
1425 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001426
1427 path = btrfs_alloc_path();
1428 if (!path)
1429 return -ENOMEM;
1430
1431 key.objectid = device->devid;
1432 key.offset = start;
1433 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001434again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001435 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001436 if (ret > 0) {
1437 ret = btrfs_previous_item(root, path, key.objectid,
1438 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001439 if (ret)
1440 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001441 leaf = path->nodes[0];
1442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1443 extent = btrfs_item_ptr(leaf, path->slots[0],
1444 struct btrfs_dev_extent);
1445 BUG_ON(found_key.offset > start || found_key.offset +
1446 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001447 key = found_key;
1448 btrfs_release_path(path);
1449 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001450 } else if (ret == 0) {
1451 leaf = path->nodes[0];
1452 extent = btrfs_item_ptr(leaf, path->slots[0],
1453 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001454 } else {
Anand Jaina4553fe2015-09-25 14:43:01 +08001455 btrfs_std_error(root->fs_info, ret, "Slot search failed");
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001456 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001457 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001458
Miao Xie2196d6e2014-09-03 21:35:41 +08001459 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1460
Chris Mason8f18cf12008-04-25 16:53:30 -04001461 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001462 if (ret) {
Anand Jaina4553fe2015-09-25 14:43:01 +08001463 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001464 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001465 } else {
Josef Bacik3204d332015-09-24 10:46:10 -04001466 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001467 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001468out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001469 btrfs_free_path(path);
1470 return ret;
1471}
1472
Eric Sandeen48a3b632013-04-25 20:41:01 +00001473static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1474 struct btrfs_device *device,
1475 u64 chunk_tree, u64 chunk_objectid,
1476 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001477{
1478 int ret;
1479 struct btrfs_path *path;
1480 struct btrfs_root *root = device->dev_root;
1481 struct btrfs_dev_extent *extent;
1482 struct extent_buffer *leaf;
1483 struct btrfs_key key;
1484
Chris Masondfe25022008-05-13 13:46:40 -04001485 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001486 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001487 path = btrfs_alloc_path();
1488 if (!path)
1489 return -ENOMEM;
1490
Chris Mason0b86a832008-03-24 15:01:56 -04001491 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001492 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001493 key.type = BTRFS_DEV_EXTENT_KEY;
1494 ret = btrfs_insert_empty_item(trans, root, path, &key,
1495 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001496 if (ret)
1497 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001498
1499 leaf = path->nodes[0];
1500 extent = btrfs_item_ptr(leaf, path->slots[0],
1501 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001502 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1503 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1504 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1505
1506 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001507 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001508
Chris Mason0b86a832008-03-24 15:01:56 -04001509 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1510 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001511out:
Chris Mason0b86a832008-03-24 15:01:56 -04001512 btrfs_free_path(path);
1513 return ret;
1514}
1515
Josef Bacik6df9a952013-06-27 13:22:46 -04001516static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001517{
Josef Bacik6df9a952013-06-27 13:22:46 -04001518 struct extent_map_tree *em_tree;
1519 struct extent_map *em;
1520 struct rb_node *n;
1521 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001522
Josef Bacik6df9a952013-06-27 13:22:46 -04001523 em_tree = &fs_info->mapping_tree.map_tree;
1524 read_lock(&em_tree->lock);
1525 n = rb_last(&em_tree->map);
1526 if (n) {
1527 em = rb_entry(n, struct extent_map, rb_node);
1528 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001529 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001530 read_unlock(&em_tree->lock);
1531
Chris Mason0b86a832008-03-24 15:01:56 -04001532 return ret;
1533}
1534
Ilya Dryomov53f10652013-08-12 14:33:01 +03001535static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1536 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001537{
1538 int ret;
1539 struct btrfs_key key;
1540 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001541 struct btrfs_path *path;
1542
Yan Zheng2b820322008-11-17 21:11:30 -05001543 path = btrfs_alloc_path();
1544 if (!path)
1545 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001546
1547 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1548 key.type = BTRFS_DEV_ITEM_KEY;
1549 key.offset = (u64)-1;
1550
Ilya Dryomov53f10652013-08-12 14:33:01 +03001551 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001552 if (ret < 0)
1553 goto error;
1554
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001555 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001556
Ilya Dryomov53f10652013-08-12 14:33:01 +03001557 ret = btrfs_previous_item(fs_info->chunk_root, path,
1558 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001559 BTRFS_DEV_ITEM_KEY);
1560 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001561 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001562 } else {
1563 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1564 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001565 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001566 }
1567 ret = 0;
1568error:
Yan Zheng2b820322008-11-17 21:11:30 -05001569 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001570 return ret;
1571}
1572
1573/*
1574 * the device information is stored in the chunk root
1575 * the btrfs_device struct should be fully filled in
1576 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001577static int btrfs_add_device(struct btrfs_trans_handle *trans,
1578 struct btrfs_root *root,
1579 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001580{
1581 int ret;
1582 struct btrfs_path *path;
1583 struct btrfs_dev_item *dev_item;
1584 struct extent_buffer *leaf;
1585 struct btrfs_key key;
1586 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001587
1588 root = root->fs_info->chunk_root;
1589
1590 path = btrfs_alloc_path();
1591 if (!path)
1592 return -ENOMEM;
1593
Chris Mason0b86a832008-03-24 15:01:56 -04001594 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1595 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001596 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001597
1598 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001599 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001600 if (ret)
1601 goto out;
1602
1603 leaf = path->nodes[0];
1604 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1605
1606 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001607 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001608 btrfs_set_device_type(leaf, dev_item, device->type);
1609 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1610 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1611 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001612 btrfs_set_device_total_bytes(leaf, dev_item,
1613 btrfs_device_get_disk_total_bytes(device));
1614 btrfs_set_device_bytes_used(leaf, dev_item,
1615 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001616 btrfs_set_device_group(leaf, dev_item, 0);
1617 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1618 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001619 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001620
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001621 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001622 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001623 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001624 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001625 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001626
Yan Zheng2b820322008-11-17 21:11:30 -05001627 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001628out:
1629 btrfs_free_path(path);
1630 return ret;
1631}
Chris Mason8f18cf12008-04-25 16:53:30 -04001632
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001633/*
1634 * Function to update ctime/mtime for a given device path.
1635 * Mainly used for ctime/mtime based probe like libblkid.
1636 */
1637static void update_dev_time(char *path_name)
1638{
1639 struct file *filp;
1640
1641 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001642 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001643 return;
1644 file_update_time(filp);
1645 filp_close(filp, NULL);
1646 return;
1647}
1648
Chris Masona061fc82008-05-07 11:43:44 -04001649static int btrfs_rm_dev_item(struct btrfs_root *root,
1650 struct btrfs_device *device)
1651{
1652 int ret;
1653 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001654 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001655 struct btrfs_trans_handle *trans;
1656
1657 root = root->fs_info->chunk_root;
1658
1659 path = btrfs_alloc_path();
1660 if (!path)
1661 return -ENOMEM;
1662
Yan, Zhenga22285a2010-05-16 10:48:46 -04001663 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001664 if (IS_ERR(trans)) {
1665 btrfs_free_path(path);
1666 return PTR_ERR(trans);
1667 }
Chris Masona061fc82008-05-07 11:43:44 -04001668 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1669 key.type = BTRFS_DEV_ITEM_KEY;
1670 key.offset = device->devid;
1671
1672 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1673 if (ret < 0)
1674 goto out;
1675
1676 if (ret > 0) {
1677 ret = -ENOENT;
1678 goto out;
1679 }
1680
1681 ret = btrfs_del_item(trans, root, path);
1682 if (ret)
1683 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001684out:
1685 btrfs_free_path(path);
1686 btrfs_commit_transaction(trans, root);
1687 return ret;
1688}
1689
1690int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1691{
1692 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001693 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001694 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001695 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001696 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001697 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001698 u64 all_avail;
1699 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001700 u64 num_devices;
1701 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001702 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001703 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001704 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001705
Chris Masona061fc82008-05-07 11:43:44 -04001706 mutex_lock(&uuid_mutex);
1707
Miao Xiede98ced2013-01-29 10:13:12 +00001708 do {
1709 seq = read_seqbegin(&root->fs_info->profiles_lock);
1710
1711 all_avail = root->fs_info->avail_data_alloc_bits |
1712 root->fs_info->avail_system_alloc_bits |
1713 root->fs_info->avail_metadata_alloc_bits;
1714 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001715
Stefan Behrens8dabb742012-11-06 13:15:27 +01001716 num_devices = root->fs_info->fs_devices->num_devices;
1717 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1718 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1719 WARN_ON(num_devices < 1);
1720 num_devices--;
1721 }
1722 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1723
1724 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001725 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001726 goto out;
1727 }
1728
Stefan Behrens8dabb742012-11-06 13:15:27 +01001729 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001730 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001731 goto out;
1732 }
1733
David Woodhouse53b381b2013-01-29 18:40:14 -05001734 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1735 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001736 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001737 goto out;
1738 }
1739 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1740 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001741 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001742 goto out;
1743 }
1744
Chris Masondfe25022008-05-13 13:46:40 -04001745 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001746 struct list_head *devices;
1747 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001748
Chris Masondfe25022008-05-13 13:46:40 -04001749 device = NULL;
1750 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001751 /*
1752 * It is safe to read the devices since the volume_mutex
1753 * is held.
1754 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001755 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001756 if (tmp->in_fs_metadata &&
1757 !tmp->is_tgtdev_for_dev_replace &&
1758 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001759 device = tmp;
1760 break;
1761 }
1762 }
1763 bdev = NULL;
1764 bh = NULL;
1765 disk_super = NULL;
1766 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001767 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001768 goto out;
1769 }
Chris Masondfe25022008-05-13 13:46:40 -04001770 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001771 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001772 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001773 root->fs_info->bdev_holder, 0,
1774 &bdev, &bh);
1775 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001776 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001777 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001778 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001779 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001780 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001781 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001782 if (!device) {
1783 ret = -ENOENT;
1784 goto error_brelse;
1785 }
Chris Masondfe25022008-05-13 13:46:40 -04001786 }
Yan Zheng2b820322008-11-17 21:11:30 -05001787
Stefan Behrens63a212a2012-11-05 18:29:28 +01001788 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001789 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001790 goto error_brelse;
1791 }
1792
Yan Zheng2b820322008-11-17 21:11:30 -05001793 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001794 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001795 goto error_brelse;
1796 }
1797
1798 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001799 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001800 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001801 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001802 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001803 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001804 }
Chris Masona061fc82008-05-07 11:43:44 -04001805
Carey Underwoodd7901552013-03-04 16:37:06 -06001806 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001807 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001808 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001809 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001810 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001811
Stefan Behrens63a212a2012-11-05 18:29:28 +01001812 /*
1813 * TODO: the superblock still includes this device in its num_devices
1814 * counter although write_all_supers() is not locked out. This
1815 * could give a filesystem state which requires a degraded mount.
1816 */
Chris Masona061fc82008-05-07 11:43:44 -04001817 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1818 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001819 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001820
Yan Zheng2b820322008-11-17 21:11:30 -05001821 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001822 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001823
1824 /*
1825 * the device list mutex makes sure that we don't change
1826 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001827 * the device supers. Whoever is writing all supers, should
1828 * lock the device list mutex before getting the number of
1829 * devices in the super block (super_copy). Conversely,
1830 * whoever updates the number of devices in the super block
1831 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001832 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001833
1834 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001835 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001836 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001837
Yan Zhenge4404d62008-12-12 10:03:26 -05001838 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001839 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001840
Chris Masoncd02dca2010-12-13 14:56:23 -05001841 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001842 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001843
Yan Zheng2b820322008-11-17 21:11:30 -05001844 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1845 struct btrfs_device, dev_list);
1846 if (device->bdev == root->fs_info->sb->s_bdev)
1847 root->fs_info->sb->s_bdev = next_device->bdev;
1848 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1849 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1850
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001851 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001852 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001853 /* remove sysfs entry */
Anand Jain32576042015-08-14 18:32:49 +08001854 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001855 }
Anand Jain99994cd2014-06-03 11:36:00 +08001856
Xiao Guangrong1f781602011-04-20 10:09:16 +00001857 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001858
David Sterba6c417612011-04-13 15:41:04 +02001859 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1860 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001861 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001862
Xiao Guangrong1f781602011-04-20 10:09:16 +00001863 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001864 struct btrfs_fs_devices *fs_devices;
1865 fs_devices = root->fs_info->fs_devices;
1866 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001867 if (fs_devices->seed == cur_devices) {
1868 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001869 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001870 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001871 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001872 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001873 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001874 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001875 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001876 }
1877
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001878 root->fs_info->num_tolerated_disk_barrier_failures =
1879 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1880
Yan Zheng2b820322008-11-17 21:11:30 -05001881 /*
1882 * at this point, the device is zero sized. We want to
1883 * remove it from the devices list and zero out the old super
1884 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001885 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001886 u64 bytenr;
1887 int i;
1888
Chris Masondfe25022008-05-13 13:46:40 -04001889 /* make sure this device isn't detected as part of
1890 * the FS anymore
1891 */
1892 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1893 set_buffer_dirty(bh);
1894 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001895
1896 /* clear the mirror copies of super block on the disk
1897 * being removed, 0th copy is been taken care above and
1898 * the below would take of the rest
1899 */
1900 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1901 bytenr = btrfs_sb_offset(i);
1902 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1903 i_size_read(bdev->bd_inode))
1904 break;
1905
1906 brelse(bh);
1907 bh = __bread(bdev, bytenr / 4096,
1908 BTRFS_SUPER_INFO_SIZE);
1909 if (!bh)
1910 continue;
1911
1912 disk_super = (struct btrfs_super_block *)bh->b_data;
1913
1914 if (btrfs_super_bytenr(disk_super) != bytenr ||
1915 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1916 continue;
1917 }
1918 memset(&disk_super->magic, 0,
1919 sizeof(disk_super->magic));
1920 set_buffer_dirty(bh);
1921 sync_dirty_buffer(bh);
1922 }
Chris Masondfe25022008-05-13 13:46:40 -04001923 }
Chris Masona061fc82008-05-07 11:43:44 -04001924
Chris Masona061fc82008-05-07 11:43:44 -04001925 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001926
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001927 if (bdev) {
1928 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001929 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001930
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001931 /* Update ctime/mtime for device path for libblkid */
1932 update_dev_time(device_path);
1933 }
1934
Chris Masona061fc82008-05-07 11:43:44 -04001935error_brelse:
1936 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001937 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001938 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001939out:
1940 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001941 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001942error_undo:
1943 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001944 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001945 list_add(&device->dev_alloc_list,
1946 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001947 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001948 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001949 }
1950 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001951}
1952
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001953void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1954 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001955{
Anand Jaind51908c2014-08-13 14:24:19 +08001956 struct btrfs_fs_devices *fs_devices;
1957
Stefan Behrense93c89c2012-11-05 17:33:06 +01001958 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001959
Anand Jain25e8e912014-08-20 10:56:56 +08001960 /*
1961 * in case of fs with no seed, srcdev->fs_devices will point
1962 * to fs_devices of fs_info. However when the dev being replaced is
1963 * a seed dev it will point to the seed's local fs_devices. In short
1964 * srcdev will have its correct fs_devices in both the cases.
1965 */
1966 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001967
Stefan Behrense93c89c2012-11-05 17:33:06 +01001968 list_del_rcu(&srcdev->dev_list);
1969 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001970 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001971 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001972 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001973
Miao Xie82372bc2014-09-03 21:35:44 +08001974 if (srcdev->writeable) {
1975 fs_devices->rw_devices--;
1976 /* zero out the old super if it is writable */
Anand Jain12b1c262015-08-14 18:32:59 +08001977 btrfs_scratch_superblocks(srcdev->bdev,
1978 rcu_str_deref(srcdev->name));
Ilya Dryomov13572722013-10-02 20:41:01 +03001979 }
1980
Miao Xie82372bc2014-09-03 21:35:44 +08001981 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001982 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001983}
1984
1985void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1986 struct btrfs_device *srcdev)
1987{
1988 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001989
Stefan Behrense93c89c2012-11-05 17:33:06 +01001990 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001991
1992 /*
1993 * unless fs_devices is seed fs, num_devices shouldn't go
1994 * zero
1995 */
1996 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1997
1998 /* if this is no devs we rather delete the fs_devices */
1999 if (!fs_devices->num_devices) {
2000 struct btrfs_fs_devices *tmp_fs_devices;
2001
2002 tmp_fs_devices = fs_info->fs_devices;
2003 while (tmp_fs_devices) {
2004 if (tmp_fs_devices->seed == fs_devices) {
2005 tmp_fs_devices->seed = fs_devices->seed;
2006 break;
2007 }
2008 tmp_fs_devices = tmp_fs_devices->seed;
2009 }
2010 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08002011 __btrfs_close_devices(fs_devices);
2012 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08002013 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002014}
2015
2016void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2017 struct btrfs_device *tgtdev)
2018{
2019 struct btrfs_device *next_device;
2020
Miao Xie67a2c452014-09-03 21:35:43 +08002021 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002022 WARN_ON(!tgtdev);
2023 mutex_lock(&fs_info->fs_devices->device_list_mutex);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002024
Anand Jain32576042015-08-14 18:32:49 +08002025 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
Anand Jaind2ff1b22015-03-10 06:38:42 +08002026
Stefan Behrense93c89c2012-11-05 17:33:06 +01002027 if (tgtdev->bdev) {
Anand Jain12b1c262015-08-14 18:32:59 +08002028 btrfs_scratch_superblocks(tgtdev->bdev,
2029 rcu_str_deref(tgtdev->name));
Stefan Behrense93c89c2012-11-05 17:33:06 +01002030 fs_info->fs_devices->open_devices--;
2031 }
2032 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002033
2034 next_device = list_entry(fs_info->fs_devices->devices.next,
2035 struct btrfs_device, dev_list);
2036 if (tgtdev->bdev == fs_info->sb->s_bdev)
2037 fs_info->sb->s_bdev = next_device->bdev;
2038 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
2039 fs_info->fs_devices->latest_bdev = next_device->bdev;
2040 list_del_rcu(&tgtdev->dev_list);
2041
2042 call_rcu(&tgtdev->rcu, free_device);
2043
2044 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08002045 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002046}
2047
Eric Sandeen48a3b632013-04-25 20:41:01 +00002048static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2049 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002050{
2051 int ret = 0;
2052 struct btrfs_super_block *disk_super;
2053 u64 devid;
2054 u8 *dev_uuid;
2055 struct block_device *bdev;
2056 struct buffer_head *bh;
2057
2058 *device = NULL;
2059 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2060 root->fs_info->bdev_holder, 0, &bdev, &bh);
2061 if (ret)
2062 return ret;
2063 disk_super = (struct btrfs_super_block *)bh->b_data;
2064 devid = btrfs_stack_device_id(&disk_super->dev_item);
2065 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002066 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002067 disk_super->fsid);
2068 brelse(bh);
2069 if (!*device)
2070 ret = -ENOENT;
2071 blkdev_put(bdev, FMODE_READ);
2072 return ret;
2073}
2074
2075int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2076 char *device_path,
2077 struct btrfs_device **device)
2078{
2079 *device = NULL;
2080 if (strcmp(device_path, "missing") == 0) {
2081 struct list_head *devices;
2082 struct btrfs_device *tmp;
2083
2084 devices = &root->fs_info->fs_devices->devices;
2085 /*
2086 * It is safe to read the devices since the volume_mutex
2087 * is held by the caller.
2088 */
2089 list_for_each_entry(tmp, devices, dev_list) {
2090 if (tmp->in_fs_metadata && !tmp->bdev) {
2091 *device = tmp;
2092 break;
2093 }
2094 }
2095
Anand Jaind74a6252015-08-14 18:32:56 +08002096 if (!*device)
2097 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Stefan Behrens7ba15b72012-11-05 14:42:30 +01002098
2099 return 0;
2100 } else {
2101 return btrfs_find_device_by_path(root, device_path, device);
2102 }
2103}
2104
Yan Zheng2b820322008-11-17 21:11:30 -05002105/*
2106 * does all the dirty work required for changing file system's UUID.
2107 */
Li Zefan125ccb02011-12-08 15:07:24 +08002108static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05002109{
2110 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
2111 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05002112 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02002113 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05002114 struct btrfs_device *device;
2115 u64 super_flags;
2116
2117 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05002118 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05002119 return -EINVAL;
2120
Ilya Dryomov2208a372013-08-12 14:33:03 +03002121 seed_devices = __alloc_fs_devices();
2122 if (IS_ERR(seed_devices))
2123 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002124
Yan Zhenge4404d62008-12-12 10:03:26 -05002125 old_devices = clone_fs_devices(fs_devices);
2126 if (IS_ERR(old_devices)) {
2127 kfree(seed_devices);
2128 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002129 }
Yan Zhenge4404d62008-12-12 10:03:26 -05002130
Yan Zheng2b820322008-11-17 21:11:30 -05002131 list_add(&old_devices->list, &fs_uuids);
2132
Yan Zhenge4404d62008-12-12 10:03:26 -05002133 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
2134 seed_devices->opened = 1;
2135 INIT_LIST_HEAD(&seed_devices->devices);
2136 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04002137 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00002138
2139 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002140 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2141 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08002142 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05002143 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002144
2145 lock_chunks(root);
2146 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2147 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002148
Yan Zheng2b820322008-11-17 21:11:30 -05002149 fs_devices->seeding = 0;
2150 fs_devices->num_devices = 0;
2151 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002152 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002153 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002154 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002155
2156 generate_random_uuid(fs_devices->fsid);
2157 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2158 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002159 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2160
Yan Zheng2b820322008-11-17 21:11:30 -05002161 super_flags = btrfs_super_flags(disk_super) &
2162 ~BTRFS_SUPER_FLAG_SEEDING;
2163 btrfs_set_super_flags(disk_super, super_flags);
2164
2165 return 0;
2166}
2167
2168/*
2169 * strore the expected generation for seed devices in device items.
2170 */
2171static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2172 struct btrfs_root *root)
2173{
2174 struct btrfs_path *path;
2175 struct extent_buffer *leaf;
2176 struct btrfs_dev_item *dev_item;
2177 struct btrfs_device *device;
2178 struct btrfs_key key;
2179 u8 fs_uuid[BTRFS_UUID_SIZE];
2180 u8 dev_uuid[BTRFS_UUID_SIZE];
2181 u64 devid;
2182 int ret;
2183
2184 path = btrfs_alloc_path();
2185 if (!path)
2186 return -ENOMEM;
2187
2188 root = root->fs_info->chunk_root;
2189 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2190 key.offset = 0;
2191 key.type = BTRFS_DEV_ITEM_KEY;
2192
2193 while (1) {
2194 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2195 if (ret < 0)
2196 goto error;
2197
2198 leaf = path->nodes[0];
2199next_slot:
2200 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2201 ret = btrfs_next_leaf(root, path);
2202 if (ret > 0)
2203 break;
2204 if (ret < 0)
2205 goto error;
2206 leaf = path->nodes[0];
2207 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002208 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002209 continue;
2210 }
2211
2212 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2213 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2214 key.type != BTRFS_DEV_ITEM_KEY)
2215 break;
2216
2217 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2218 struct btrfs_dev_item);
2219 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002220 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002221 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002222 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002223 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002224 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2225 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002226 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002227
2228 if (device->fs_devices->seeding) {
2229 btrfs_set_device_generation(leaf, dev_item,
2230 device->generation);
2231 btrfs_mark_buffer_dirty(leaf);
2232 }
2233
2234 path->slots[0]++;
2235 goto next_slot;
2236 }
2237 ret = 0;
2238error:
2239 btrfs_free_path(path);
2240 return ret;
2241}
2242
Chris Mason788f20e2008-04-28 15:29:42 -04002243int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2244{
Josef Bacikd5e20032011-08-04 14:52:27 +00002245 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002246 struct btrfs_trans_handle *trans;
2247 struct btrfs_device *device;
2248 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002249 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002250 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002251 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002252 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002253 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002254 int ret = 0;
2255
Yan Zheng2b820322008-11-17 21:11:30 -05002256 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002257 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002258
Li Zefana5d16332011-12-07 20:08:40 -05002259 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002260 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002261 if (IS_ERR(bdev))
2262 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002263
Yan Zheng2b820322008-11-17 21:11:30 -05002264 if (root->fs_info->fs_devices->seeding) {
2265 seeding_dev = 1;
2266 down_write(&sb->s_umount);
2267 mutex_lock(&uuid_mutex);
2268 }
2269
Chris Mason8c8bee12008-09-29 11:19:10 -04002270 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002271
Chris Mason788f20e2008-04-28 15:29:42 -04002272 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002273
2274 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002275 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002276 if (device->bdev == bdev) {
2277 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002278 mutex_unlock(
2279 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002280 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002281 }
2282 }
Liu Bod25628b2012-11-14 14:35:30 +00002283 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002284
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002285 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2286 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002287 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002288 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002289 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002290 }
2291
Josef Bacik606686e2012-06-04 14:03:51 -04002292 name = rcu_string_strdup(device_path, GFP_NOFS);
2293 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002294 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002295 ret = -ENOMEM;
2296 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002297 }
Josef Bacik606686e2012-06-04 14:03:51 -04002298 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002299
Yan, Zhenga22285a2010-05-16 10:48:46 -04002300 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002301 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002302 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002303 kfree(device);
2304 ret = PTR_ERR(trans);
2305 goto error;
2306 }
2307
Josef Bacikd5e20032011-08-04 14:52:27 +00002308 q = bdev_get_queue(bdev);
2309 if (blk_queue_discard(q))
2310 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002311 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002312 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002313 device->io_width = root->sectorsize;
2314 device->io_align = root->sectorsize;
2315 device->sector_size = root->sectorsize;
2316 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002317 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002318 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002319 device->dev_root = root->fs_info->dev_root;
2320 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002321 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002322 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002323 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002324 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002325 set_blocksize(device->bdev, 4096);
2326
Yan Zheng2b820322008-11-17 21:11:30 -05002327 if (seeding_dev) {
2328 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002329 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002330 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002331 }
2332
2333 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002334
Chris Masone5e9a522009-06-10 15:17:02 -04002335 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002336 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002337 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002338 list_add(&device->dev_alloc_list,
2339 &root->fs_info->fs_devices->alloc_list);
2340 root->fs_info->fs_devices->num_devices++;
2341 root->fs_info->fs_devices->open_devices++;
2342 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002343 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002344 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2345
Josef Bacik2bf64752011-09-26 17:12:22 -04002346 spin_lock(&root->fs_info->free_chunk_lock);
2347 root->fs_info->free_chunk_space += device->total_bytes;
2348 spin_unlock(&root->fs_info->free_chunk_lock);
2349
Chris Masonc2898112009-06-10 09:51:32 -04002350 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2351 root->fs_info->fs_devices->rotating = 1;
2352
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002353 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002354 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002355 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002356
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002357 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002358 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002359 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002360
2361 /* add sysfs device entry */
Anand Jaine3bd6972015-08-14 18:32:48 +08002362 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
Anand Jain0d393762014-06-03 11:36:01 +08002363
Miao Xie2196d6e2014-09-03 21:35:41 +08002364 /*
2365 * we've got more storage, clear any full flags on the space
2366 * infos
2367 */
2368 btrfs_clear_space_info_full(root->fs_info);
2369
2370 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002371 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002372
Yan Zheng2b820322008-11-17 21:11:30 -05002373 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002374 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002375 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002376 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002377 if (ret) {
2378 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002379 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002380 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002381 }
2382
2383 ret = btrfs_add_device(trans, root, device);
2384 if (ret) {
2385 btrfs_abort_transaction(trans, root, ret);
2386 goto error_trans;
2387 }
2388
2389 if (seeding_dev) {
2390 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2391
Yan Zheng2b820322008-11-17 21:11:30 -05002392 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002393 if (ret) {
2394 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002395 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002396 }
Anand Jainb2373f22014-06-03 11:36:03 +08002397
2398 /* Sprouting would change fsid of the mounted root,
2399 * so rename the fsid on the sysfs
2400 */
2401 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2402 root->fs_info->fsid);
Anand Jainc1b7e472015-08-14 18:32:50 +08002403 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
Anand Jain2e7910d2015-03-10 06:38:29 +08002404 fsid_buf))
David Sterbaf14d1042015-10-08 11:37:06 +02002405 btrfs_warn(root->fs_info,
2406 "sysfs: failed to create fsid for sprout");
Yan Zheng2b820322008-11-17 21:11:30 -05002407 }
2408
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002409 root->fs_info->num_tolerated_disk_barrier_failures =
2410 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002411 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002412
2413 if (seeding_dev) {
2414 mutex_unlock(&uuid_mutex);
2415 up_write(&sb->s_umount);
2416
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002417 if (ret) /* transaction commit */
2418 return ret;
2419
Yan Zheng2b820322008-11-17 21:11:30 -05002420 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002421 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002422 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002423 "Failed to relocate sys chunks after "
2424 "device initialization. This can be fixed "
2425 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002426 trans = btrfs_attach_transaction(root);
2427 if (IS_ERR(trans)) {
2428 if (PTR_ERR(trans) == -ENOENT)
2429 return 0;
2430 return PTR_ERR(trans);
2431 }
2432 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002433 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002434
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002435 /* Update ctime/mtime for libblkid */
2436 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002437 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002438
2439error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002440 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002441 rcu_string_free(device->name);
Anand Jain32576042015-08-14 18:32:49 +08002442 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002443 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002444error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002445 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002446 if (seeding_dev) {
2447 mutex_unlock(&uuid_mutex);
2448 up_write(&sb->s_umount);
2449 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002450 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002451}
2452
Stefan Behrense93c89c2012-11-05 17:33:06 +01002453int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002454 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002455 struct btrfs_device **device_out)
2456{
2457 struct request_queue *q;
2458 struct btrfs_device *device;
2459 struct block_device *bdev;
2460 struct btrfs_fs_info *fs_info = root->fs_info;
2461 struct list_head *devices;
2462 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002463 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002464 int ret = 0;
2465
2466 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002467 if (fs_info->fs_devices->seeding) {
2468 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002469 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002470 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002471
2472 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2473 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002474 if (IS_ERR(bdev)) {
2475 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002476 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002477 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002478
2479 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2480
2481 devices = &fs_info->fs_devices->devices;
2482 list_for_each_entry(device, devices, dev_list) {
2483 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002484 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002485 ret = -EEXIST;
2486 goto error;
2487 }
2488 }
2489
Miao Xie1c433662014-09-03 21:35:32 +08002490
Miao Xie7cc8e582014-09-03 21:35:38 +08002491 if (i_size_read(bdev->bd_inode) <
2492 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002493 btrfs_err(fs_info, "target device is smaller than source device!");
2494 ret = -EINVAL;
2495 goto error;
2496 }
2497
2498
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002499 device = btrfs_alloc_device(NULL, &devid, NULL);
2500 if (IS_ERR(device)) {
2501 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002502 goto error;
2503 }
2504
2505 name = rcu_string_strdup(device_path, GFP_NOFS);
2506 if (!name) {
2507 kfree(device);
2508 ret = -ENOMEM;
2509 goto error;
2510 }
2511 rcu_assign_pointer(device->name, name);
2512
2513 q = bdev_get_queue(bdev);
2514 if (blk_queue_discard(q))
2515 device->can_discard = 1;
2516 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2517 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002518 device->generation = 0;
2519 device->io_width = root->sectorsize;
2520 device->io_align = root->sectorsize;
2521 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002522 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2523 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2524 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002525 ASSERT(list_empty(&srcdev->resized_list));
2526 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002527 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002528 device->dev_root = fs_info->dev_root;
2529 device->bdev = bdev;
2530 device->in_fs_metadata = 1;
2531 device->is_tgtdev_for_dev_replace = 1;
2532 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002533 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002534 set_blocksize(device->bdev, 4096);
2535 device->fs_devices = fs_info->fs_devices;
2536 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2537 fs_info->fs_devices->num_devices++;
2538 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002539 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2540
2541 *device_out = device;
2542 return ret;
2543
2544error:
2545 blkdev_put(bdev, FMODE_EXCL);
2546 return ret;
2547}
2548
2549void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2550 struct btrfs_device *tgtdev)
2551{
2552 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2553 tgtdev->io_width = fs_info->dev_root->sectorsize;
2554 tgtdev->io_align = fs_info->dev_root->sectorsize;
2555 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2556 tgtdev->dev_root = fs_info->dev_root;
2557 tgtdev->in_fs_metadata = 1;
2558}
2559
Chris Masond3977122009-01-05 21:25:51 -05002560static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2561 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002562{
2563 int ret;
2564 struct btrfs_path *path;
2565 struct btrfs_root *root;
2566 struct btrfs_dev_item *dev_item;
2567 struct extent_buffer *leaf;
2568 struct btrfs_key key;
2569
2570 root = device->dev_root->fs_info->chunk_root;
2571
2572 path = btrfs_alloc_path();
2573 if (!path)
2574 return -ENOMEM;
2575
2576 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2577 key.type = BTRFS_DEV_ITEM_KEY;
2578 key.offset = device->devid;
2579
2580 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2581 if (ret < 0)
2582 goto out;
2583
2584 if (ret > 0) {
2585 ret = -ENOENT;
2586 goto out;
2587 }
2588
2589 leaf = path->nodes[0];
2590 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2591
2592 btrfs_set_device_id(leaf, dev_item, device->devid);
2593 btrfs_set_device_type(leaf, dev_item, device->type);
2594 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2595 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2596 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002597 btrfs_set_device_total_bytes(leaf, dev_item,
2598 btrfs_device_get_disk_total_bytes(device));
2599 btrfs_set_device_bytes_used(leaf, dev_item,
2600 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002601 btrfs_mark_buffer_dirty(leaf);
2602
2603out:
2604 btrfs_free_path(path);
2605 return ret;
2606}
2607
Miao Xie2196d6e2014-09-03 21:35:41 +08002608int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002609 struct btrfs_device *device, u64 new_size)
2610{
2611 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002612 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002613 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002614 u64 old_total;
2615 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002616
Yan Zheng2b820322008-11-17 21:11:30 -05002617 if (!device->writeable)
2618 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002619
2620 lock_chunks(device->dev_root);
2621 old_total = btrfs_super_total_bytes(super_copy);
2622 diff = new_size - device->total_bytes;
2623
Stefan Behrens63a212a2012-11-05 18:29:28 +01002624 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002625 device->is_tgtdev_for_dev_replace) {
2626 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002627 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002628 }
Yan Zheng2b820322008-11-17 21:11:30 -05002629
Miao Xie935e5cc2014-09-03 21:35:33 +08002630 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002631
2632 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002633 device->fs_devices->total_rw_bytes += diff;
2634
Miao Xie7cc8e582014-09-03 21:35:38 +08002635 btrfs_device_set_total_bytes(device, new_size);
2636 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002637 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002638 if (list_empty(&device->resized_list))
2639 list_add_tail(&device->resized_list,
2640 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002641 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002642
Chris Mason8f18cf12008-04-25 16:53:30 -04002643 return btrfs_update_device(trans, device);
2644}
2645
2646static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002647 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002648 u64 chunk_offset)
2649{
2650 int ret;
2651 struct btrfs_path *path;
2652 struct btrfs_key key;
2653
2654 root = root->fs_info->chunk_root;
2655 path = btrfs_alloc_path();
2656 if (!path)
2657 return -ENOMEM;
2658
2659 key.objectid = chunk_objectid;
2660 key.offset = chunk_offset;
2661 key.type = BTRFS_CHUNK_ITEM_KEY;
2662
2663 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002664 if (ret < 0)
2665 goto out;
2666 else if (ret > 0) { /* Logic error or corruption */
Anand Jaina4553fe2015-09-25 14:43:01 +08002667 btrfs_std_error(root->fs_info, -ENOENT,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002668 "Failed lookup while freeing chunk.");
2669 ret = -ENOENT;
2670 goto out;
2671 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002672
2673 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002674 if (ret < 0)
Anand Jaina4553fe2015-09-25 14:43:01 +08002675 btrfs_std_error(root->fs_info, ret,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676 "Failed to delete chunk item.");
2677out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002678 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002679 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002680}
2681
Christoph Hellwigb2950862008-12-02 09:54:17 -05002682static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002683 chunk_offset)
2684{
David Sterba6c417612011-04-13 15:41:04 +02002685 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002686 struct btrfs_disk_key *disk_key;
2687 struct btrfs_chunk *chunk;
2688 u8 *ptr;
2689 int ret = 0;
2690 u32 num_stripes;
2691 u32 array_size;
2692 u32 len = 0;
2693 u32 cur;
2694 struct btrfs_key key;
2695
Miao Xie2196d6e2014-09-03 21:35:41 +08002696 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002697 array_size = btrfs_super_sys_array_size(super_copy);
2698
2699 ptr = super_copy->sys_chunk_array;
2700 cur = 0;
2701
2702 while (cur < array_size) {
2703 disk_key = (struct btrfs_disk_key *)ptr;
2704 btrfs_disk_key_to_cpu(&key, disk_key);
2705
2706 len = sizeof(*disk_key);
2707
2708 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2709 chunk = (struct btrfs_chunk *)(ptr + len);
2710 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2711 len += btrfs_chunk_item_size(num_stripes);
2712 } else {
2713 ret = -EIO;
2714 break;
2715 }
2716 if (key.objectid == chunk_objectid &&
2717 key.offset == chunk_offset) {
2718 memmove(ptr, ptr + len, array_size - (cur + len));
2719 array_size -= len;
2720 btrfs_set_super_sys_array_size(super_copy, array_size);
2721 } else {
2722 ptr += len;
2723 cur += len;
2724 }
2725 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002726 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002727 return ret;
2728}
2729
Josef Bacik47ab2a62014-09-18 11:20:02 -04002730int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2731 struct btrfs_root *root, u64 chunk_offset)
2732{
2733 struct extent_map_tree *em_tree;
2734 struct extent_map *em;
2735 struct btrfs_root *extent_root = root->fs_info->extent_root;
2736 struct map_lookup *map;
2737 u64 dev_extent_len = 0;
2738 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002739 int i, ret = 0;
2740
2741 /* Just in case */
2742 root = root->fs_info->chunk_root;
2743 em_tree = &root->fs_info->mapping_tree.map_tree;
2744
2745 read_lock(&em_tree->lock);
2746 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2747 read_unlock(&em_tree->lock);
2748
2749 if (!em || em->start > chunk_offset ||
2750 em->start + em->len < chunk_offset) {
2751 /*
2752 * This is a logic error, but we don't want to just rely on the
2753 * user having built with ASSERT enabled, so if ASSERT doens't
2754 * do anything we still error out.
2755 */
2756 ASSERT(0);
2757 if (em)
2758 free_extent_map(em);
2759 return -EINVAL;
2760 }
2761 map = (struct map_lookup *)em->bdev;
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002762 lock_chunks(root->fs_info->chunk_root);
Filipe Manana4617ea32015-06-09 17:48:21 +01002763 check_system_chunk(trans, extent_root, map->type);
Filipe Manana39c2d7f2015-05-20 14:01:55 +01002764 unlock_chunks(root->fs_info->chunk_root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002765
2766 for (i = 0; i < map->num_stripes; i++) {
2767 struct btrfs_device *device = map->stripes[i].dev;
2768 ret = btrfs_free_dev_extent(trans, device,
2769 map->stripes[i].physical,
2770 &dev_extent_len);
2771 if (ret) {
2772 btrfs_abort_transaction(trans, root, ret);
2773 goto out;
2774 }
2775
2776 if (device->bytes_used > 0) {
2777 lock_chunks(root);
2778 btrfs_device_set_bytes_used(device,
2779 device->bytes_used - dev_extent_len);
2780 spin_lock(&root->fs_info->free_chunk_lock);
2781 root->fs_info->free_chunk_space += dev_extent_len;
2782 spin_unlock(&root->fs_info->free_chunk_lock);
2783 btrfs_clear_space_info_full(root->fs_info);
2784 unlock_chunks(root);
2785 }
2786
2787 if (map->stripes[i].dev) {
2788 ret = btrfs_update_device(trans, map->stripes[i].dev);
2789 if (ret) {
2790 btrfs_abort_transaction(trans, root, ret);
2791 goto out;
2792 }
2793 }
2794 }
Zhao Leia688a042015-02-09 20:31:44 +08002795 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002796 if (ret) {
2797 btrfs_abort_transaction(trans, root, ret);
2798 goto out;
2799 }
2800
2801 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2802
2803 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2804 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2805 if (ret) {
2806 btrfs_abort_transaction(trans, root, ret);
2807 goto out;
2808 }
2809 }
2810
Filipe Manana04216822014-11-27 21:14:15 +00002811 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002812 if (ret) {
2813 btrfs_abort_transaction(trans, extent_root, ret);
2814 goto out;
2815 }
2816
Josef Bacik47ab2a62014-09-18 11:20:02 -04002817out:
2818 /* once for us */
2819 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002820 return ret;
2821}
2822
Zhaoleidc2ee4e2015-08-05 18:00:04 +08002823static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002824{
Chris Mason8f18cf12008-04-25 16:53:30 -04002825 struct btrfs_root *extent_root;
2826 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002827 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002828
2829 root = root->fs_info->chunk_root;
2830 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002831
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002832 /*
2833 * Prevent races with automatic removal of unused block groups.
2834 * After we relocate and before we remove the chunk with offset
2835 * chunk_offset, automatic removal of the block group can kick in,
2836 * resulting in a failure when calling btrfs_remove_chunk() below.
2837 *
2838 * Make sure to acquire this mutex before doing a tree search (dev
2839 * or chunk trees) to find chunks. Otherwise the cleaner kthread might
2840 * call btrfs_remove_chunk() (through btrfs_delete_unused_bgs()) after
2841 * we release the path used to search the chunk/dev tree and before
2842 * the current task acquires this mutex and calls us.
2843 */
2844 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex));
2845
Josef Bacikba1bf482009-09-11 16:11:19 -04002846 ret = btrfs_can_relocate(extent_root, chunk_offset);
2847 if (ret)
2848 return -ENOSPC;
2849
Chris Mason8f18cf12008-04-25 16:53:30 -04002850 /* step one, relocate all the extents inside this chunk */
Zhaolei55e3a602015-08-05 16:43:30 +08002851 btrfs_scrub_pause(root);
Zheng Yan1a40e232008-09-26 10:09:34 -04002852 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Zhaolei55e3a602015-08-05 16:43:30 +08002853 btrfs_scrub_continue(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002854 if (ret)
2855 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002856
Yan, Zhenga22285a2010-05-16 10:48:46 -04002857 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002858 if (IS_ERR(trans)) {
2859 ret = PTR_ERR(trans);
Anand Jaina4553fe2015-09-25 14:43:01 +08002860 btrfs_std_error(root->fs_info, ret, NULL);
Liu Bo0f788c52013-03-04 16:25:40 +00002861 return ret;
2862 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002863
2864 /*
2865 * step two, delete the device extents and the
2866 * chunk tree entries
2867 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002868 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002869 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002870 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002871}
2872
Yan Zheng2b820322008-11-17 21:11:30 -05002873static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2874{
2875 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2876 struct btrfs_path *path;
2877 struct extent_buffer *leaf;
2878 struct btrfs_chunk *chunk;
2879 struct btrfs_key key;
2880 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002881 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002882 bool retried = false;
2883 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002884 int ret;
2885
2886 path = btrfs_alloc_path();
2887 if (!path)
2888 return -ENOMEM;
2889
Josef Bacikba1bf482009-09-11 16:11:19 -04002890again:
Yan Zheng2b820322008-11-17 21:11:30 -05002891 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2892 key.offset = (u64)-1;
2893 key.type = BTRFS_CHUNK_ITEM_KEY;
2894
2895 while (1) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002896 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002897 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002898 if (ret < 0) {
2899 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002900 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002901 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002902 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002903
2904 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2905 key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002906 if (ret)
2907 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002908 if (ret < 0)
2909 goto error;
2910 if (ret > 0)
2911 break;
2912
2913 leaf = path->nodes[0];
2914 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2915
2916 chunk = btrfs_item_ptr(leaf, path->slots[0],
2917 struct btrfs_chunk);
2918 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002919 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002920
2921 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002922 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002923 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002924 if (ret == -ENOSPC)
2925 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302926 else
2927 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002928 }
Filipe Manana67c5e7d2015-06-11 00:58:53 +01002929 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002930
2931 if (found_key.offset == 0)
2932 break;
2933 key.offset = found_key.offset - 1;
2934 }
2935 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002936 if (failed && !retried) {
2937 failed = 0;
2938 retried = true;
2939 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302940 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002941 ret = -ENOSPC;
2942 }
Yan Zheng2b820322008-11-17 21:11:30 -05002943error:
2944 btrfs_free_path(path);
2945 return ret;
2946}
2947
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002948static int insert_balance_item(struct btrfs_root *root,
2949 struct btrfs_balance_control *bctl)
2950{
2951 struct btrfs_trans_handle *trans;
2952 struct btrfs_balance_item *item;
2953 struct btrfs_disk_balance_args disk_bargs;
2954 struct btrfs_path *path;
2955 struct extent_buffer *leaf;
2956 struct btrfs_key key;
2957 int ret, err;
2958
2959 path = btrfs_alloc_path();
2960 if (!path)
2961 return -ENOMEM;
2962
2963 trans = btrfs_start_transaction(root, 0);
2964 if (IS_ERR(trans)) {
2965 btrfs_free_path(path);
2966 return PTR_ERR(trans);
2967 }
2968
2969 key.objectid = BTRFS_BALANCE_OBJECTID;
2970 key.type = BTRFS_BALANCE_ITEM_KEY;
2971 key.offset = 0;
2972
2973 ret = btrfs_insert_empty_item(trans, root, path, &key,
2974 sizeof(*item));
2975 if (ret)
2976 goto out;
2977
2978 leaf = path->nodes[0];
2979 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2980
2981 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2982
2983 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2984 btrfs_set_balance_data(leaf, item, &disk_bargs);
2985 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2986 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2987 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2988 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2989
2990 btrfs_set_balance_flags(leaf, item, bctl->flags);
2991
2992 btrfs_mark_buffer_dirty(leaf);
2993out:
2994 btrfs_free_path(path);
2995 err = btrfs_commit_transaction(trans, root);
2996 if (err && !ret)
2997 ret = err;
2998 return ret;
2999}
3000
3001static int del_balance_item(struct btrfs_root *root)
3002{
3003 struct btrfs_trans_handle *trans;
3004 struct btrfs_path *path;
3005 struct btrfs_key key;
3006 int ret, err;
3007
3008 path = btrfs_alloc_path();
3009 if (!path)
3010 return -ENOMEM;
3011
3012 trans = btrfs_start_transaction(root, 0);
3013 if (IS_ERR(trans)) {
3014 btrfs_free_path(path);
3015 return PTR_ERR(trans);
3016 }
3017
3018 key.objectid = BTRFS_BALANCE_OBJECTID;
3019 key.type = BTRFS_BALANCE_ITEM_KEY;
3020 key.offset = 0;
3021
3022 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3023 if (ret < 0)
3024 goto out;
3025 if (ret > 0) {
3026 ret = -ENOENT;
3027 goto out;
3028 }
3029
3030 ret = btrfs_del_item(trans, root, path);
3031out:
3032 btrfs_free_path(path);
3033 err = btrfs_commit_transaction(trans, root);
3034 if (err && !ret)
3035 ret = err;
3036 return ret;
3037}
3038
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003039/*
Ilya Dryomov59641012012-01-16 22:04:48 +02003040 * This is a heuristic used to reduce the number of chunks balanced on
3041 * resume after balance was interrupted.
3042 */
3043static void update_balance_args(struct btrfs_balance_control *bctl)
3044{
3045 /*
3046 * Turn on soft mode for chunk types that were being converted.
3047 */
3048 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
3049 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
3050 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
3051 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
3052 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
3053 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
3054
3055 /*
3056 * Turn on usage filter if is not already used. The idea is
3057 * that chunks that we have already balanced should be
3058 * reasonably full. Don't do it for chunks that are being
3059 * converted - that will keep us from relocating unconverted
3060 * (albeit full) chunks.
3061 */
3062 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003063 !(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003064 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3065 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
3066 bctl->data.usage = 90;
3067 }
3068 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003069 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003070 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3071 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
3072 bctl->sys.usage = 90;
3073 }
3074 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
David Sterbabc309462015-10-20 18:22:13 +02003075 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
Ilya Dryomov59641012012-01-16 22:04:48 +02003076 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
3077 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
3078 bctl->meta.usage = 90;
3079 }
3080}
3081
3082/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003083 * Should be called with both balance and volume mutexes held to
3084 * serialize other volume operations (add_dev/rm_dev/resize) with
3085 * restriper. Same goes for unset_balance_control.
3086 */
3087static void set_balance_control(struct btrfs_balance_control *bctl)
3088{
3089 struct btrfs_fs_info *fs_info = bctl->fs_info;
3090
3091 BUG_ON(fs_info->balance_ctl);
3092
3093 spin_lock(&fs_info->balance_lock);
3094 fs_info->balance_ctl = bctl;
3095 spin_unlock(&fs_info->balance_lock);
3096}
3097
3098static void unset_balance_control(struct btrfs_fs_info *fs_info)
3099{
3100 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3101
3102 BUG_ON(!fs_info->balance_ctl);
3103
3104 spin_lock(&fs_info->balance_lock);
3105 fs_info->balance_ctl = NULL;
3106 spin_unlock(&fs_info->balance_lock);
3107
3108 kfree(bctl);
3109}
3110
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003111/*
3112 * Balance filters. Return 1 if chunk should be filtered out
3113 * (should not be balanced).
3114 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003115static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003116 struct btrfs_balance_args *bargs)
3117{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003118 chunk_type = chunk_to_extended(chunk_type) &
3119 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003120
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003121 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003122 return 0;
3123
3124 return 1;
3125}
3126
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003127static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
3128 struct btrfs_balance_args *bargs)
3129{
3130 struct btrfs_block_group_cache *cache;
David Sterbabc309462015-10-20 18:22:13 +02003131 u64 chunk_used;
3132 u64 user_thresh_min;
3133 u64 user_thresh_max;
3134 int ret = 1;
3135
3136 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3137 chunk_used = btrfs_block_group_used(&cache->item);
3138
3139 if (bargs->usage_min == 0)
3140 user_thresh_min = 0;
3141 else
3142 user_thresh_min = div_factor_fine(cache->key.offset,
3143 bargs->usage_min);
3144
3145 if (bargs->usage_max == 0)
3146 user_thresh_max = 1;
3147 else if (bargs->usage_max > 100)
3148 user_thresh_max = cache->key.offset;
3149 else
3150 user_thresh_max = div_factor_fine(cache->key.offset,
3151 bargs->usage_max);
3152
3153 if (user_thresh_min <= chunk_used && chunk_used < user_thresh_max)
3154 ret = 0;
3155
3156 btrfs_put_block_group(cache);
3157 return ret;
3158}
3159
3160static int chunk_usage_range_filter(struct btrfs_fs_info *fs_info,
3161 u64 chunk_offset, struct btrfs_balance_args *bargs)
3162{
3163 struct btrfs_block_group_cache *cache;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003164 u64 chunk_used, user_thresh;
3165 int ret = 1;
3166
3167 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
3168 chunk_used = btrfs_block_group_used(&cache->item);
3169
David Sterbabc309462015-10-20 18:22:13 +02003170 if (bargs->usage_min == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00003171 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02003172 else if (bargs->usage > 100)
3173 user_thresh = cache->key.offset;
3174 else
3175 user_thresh = div_factor_fine(cache->key.offset,
3176 bargs->usage);
3177
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003178 if (chunk_used < user_thresh)
3179 ret = 0;
3180
3181 btrfs_put_block_group(cache);
3182 return ret;
3183}
3184
Ilya Dryomov409d4042012-01-16 22:04:47 +02003185static int chunk_devid_filter(struct extent_buffer *leaf,
3186 struct btrfs_chunk *chunk,
3187 struct btrfs_balance_args *bargs)
3188{
3189 struct btrfs_stripe *stripe;
3190 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3191 int i;
3192
3193 for (i = 0; i < num_stripes; i++) {
3194 stripe = btrfs_stripe_nr(chunk, i);
3195 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
3196 return 0;
3197 }
3198
3199 return 1;
3200}
3201
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003202/* [pstart, pend) */
3203static int chunk_drange_filter(struct extent_buffer *leaf,
3204 struct btrfs_chunk *chunk,
3205 u64 chunk_offset,
3206 struct btrfs_balance_args *bargs)
3207{
3208 struct btrfs_stripe *stripe;
3209 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3210 u64 stripe_offset;
3211 u64 stripe_length;
3212 int factor;
3213 int i;
3214
3215 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3216 return 0;
3217
3218 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003219 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3220 factor = num_stripes / 2;
3221 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3222 factor = num_stripes - 1;
3223 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3224 factor = num_stripes - 2;
3225 } else {
3226 factor = num_stripes;
3227 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003228
3229 for (i = 0; i < num_stripes; i++) {
3230 stripe = btrfs_stripe_nr(chunk, i);
3231 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3232 continue;
3233
3234 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3235 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003236 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003237
3238 if (stripe_offset < bargs->pend &&
3239 stripe_offset + stripe_length > bargs->pstart)
3240 return 0;
3241 }
3242
3243 return 1;
3244}
3245
Ilya Dryomovea671762012-01-16 22:04:48 +02003246/* [vstart, vend) */
3247static int chunk_vrange_filter(struct extent_buffer *leaf,
3248 struct btrfs_chunk *chunk,
3249 u64 chunk_offset,
3250 struct btrfs_balance_args *bargs)
3251{
3252 if (chunk_offset < bargs->vend &&
3253 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3254 /* at least part of the chunk is inside this vrange */
3255 return 0;
3256
3257 return 1;
3258}
3259
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003260static int chunk_stripes_range_filter(struct extent_buffer *leaf,
3261 struct btrfs_chunk *chunk,
3262 struct btrfs_balance_args *bargs)
3263{
3264 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3265
3266 if (bargs->stripes_min <= num_stripes
3267 && num_stripes <= bargs->stripes_max)
3268 return 0;
3269
3270 return 1;
3271}
3272
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003273static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003274 struct btrfs_balance_args *bargs)
3275{
3276 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3277 return 0;
3278
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003279 chunk_type = chunk_to_extended(chunk_type) &
3280 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003281
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003282 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003283 return 1;
3284
3285 return 0;
3286}
3287
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003288static int should_balance_chunk(struct btrfs_root *root,
3289 struct extent_buffer *leaf,
3290 struct btrfs_chunk *chunk, u64 chunk_offset)
3291{
3292 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3293 struct btrfs_balance_args *bargs = NULL;
3294 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3295
3296 /* type filter */
3297 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3298 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3299 return 0;
3300 }
3301
3302 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3303 bargs = &bctl->data;
3304 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3305 bargs = &bctl->sys;
3306 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3307 bargs = &bctl->meta;
3308
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003309 /* profiles filter */
3310 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3311 chunk_profiles_filter(chunk_type, bargs)) {
3312 return 0;
3313 }
3314
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003315 /* usage filter */
3316 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3317 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3318 return 0;
David Sterbabc309462015-10-20 18:22:13 +02003319 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3320 chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) {
3321 return 0;
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003322 }
3323
Ilya Dryomov409d4042012-01-16 22:04:47 +02003324 /* devid filter */
3325 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3326 chunk_devid_filter(leaf, chunk, bargs)) {
3327 return 0;
3328 }
3329
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003330 /* drange filter, makes sense only with devid filter */
3331 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3332 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3333 return 0;
3334 }
3335
Ilya Dryomovea671762012-01-16 22:04:48 +02003336 /* vrange filter */
3337 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3338 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3339 return 0;
3340 }
3341
Gabríel Arthúr Péturssondee32d02015-09-28 22:32:41 +00003342 /* stripes filter */
3343 if ((bargs->flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) &&
3344 chunk_stripes_range_filter(leaf, chunk, bargs)) {
3345 return 0;
3346 }
3347
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003348 /* soft profile changing mode */
3349 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3350 chunk_soft_convert_filter(chunk_type, bargs)) {
3351 return 0;
3352 }
3353
David Sterba7d824b62014-05-07 17:37:51 +02003354 /*
3355 * limited by count, must be the last filter
3356 */
3357 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3358 if (bargs->limit == 0)
3359 return 0;
3360 else
3361 bargs->limit--;
David Sterba12907fc2015-10-10 17:16:50 +02003362 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE)) {
3363 /*
3364 * Same logic as the 'limit' filter; the minimum cannot be
3365 * determined here because we do not have the global informatoin
3366 * about the count of all chunks that satisfy the filters.
3367 */
3368 if (bargs->limit_max == 0)
3369 return 0;
3370 else
3371 bargs->limit_max--;
David Sterba7d824b62014-05-07 17:37:51 +02003372 }
3373
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003374 return 1;
3375}
3376
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003377static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003378{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003379 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003380 struct btrfs_root *chunk_root = fs_info->chunk_root;
3381 struct btrfs_root *dev_root = fs_info->dev_root;
3382 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003383 struct btrfs_device *device;
3384 u64 old_size;
3385 u64 size_to_free;
David Sterba12907fc2015-10-10 17:16:50 +02003386 u64 chunk_type;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003387 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003388 struct btrfs_path *path;
3389 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003390 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003391 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003392 struct extent_buffer *leaf;
3393 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003394 int ret;
3395 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003396 bool counting = true;
David Sterba12907fc2015-10-10 17:16:50 +02003397 /* The single value limit and min/max limits use the same bytes in the */
David Sterba7d824b62014-05-07 17:37:51 +02003398 u64 limit_data = bctl->data.limit;
3399 u64 limit_meta = bctl->meta.limit;
3400 u64 limit_sys = bctl->sys.limit;
David Sterba12907fc2015-10-10 17:16:50 +02003401 u32 count_data = 0;
3402 u32 count_meta = 0;
3403 u32 count_sys = 0;
Zhao Lei2c9fe832015-11-09 11:51:32 +08003404 int chunk_reserved = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003405
Chris Masonec44a352008-04-28 15:29:52 -04003406 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003407 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003408 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003409 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003410 size_to_free = div_factor(old_size, 1);
3411 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003412 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003413 btrfs_device_get_total_bytes(device) -
3414 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003415 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003416 continue;
3417
3418 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003419 if (ret == -ENOSPC)
3420 break;
Chris Masonec44a352008-04-28 15:29:52 -04003421 BUG_ON(ret);
3422
Yan, Zhenga22285a2010-05-16 10:48:46 -04003423 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003424 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003425
3426 ret = btrfs_grow_device(trans, device, old_size);
3427 BUG_ON(ret);
3428
3429 btrfs_end_transaction(trans, dev_root);
3430 }
3431
3432 /* step two, relocate all the chunks */
3433 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003434 if (!path) {
3435 ret = -ENOMEM;
3436 goto error;
3437 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003438
3439 /* zero out stat counters */
3440 spin_lock(&fs_info->balance_lock);
3441 memset(&bctl->stat, 0, sizeof(bctl->stat));
3442 spin_unlock(&fs_info->balance_lock);
3443again:
David Sterba7d824b62014-05-07 17:37:51 +02003444 if (!counting) {
David Sterba12907fc2015-10-10 17:16:50 +02003445 /*
3446 * The single value limit and min/max limits use the same bytes
3447 * in the
3448 */
David Sterba7d824b62014-05-07 17:37:51 +02003449 bctl->data.limit = limit_data;
3450 bctl->meta.limit = limit_meta;
3451 bctl->sys.limit = limit_sys;
3452 }
Chris Masonec44a352008-04-28 15:29:52 -04003453 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3454 key.offset = (u64)-1;
3455 key.type = BTRFS_CHUNK_ITEM_KEY;
3456
Chris Masond3977122009-01-05 21:25:51 -05003457 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003458 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003459 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003460 ret = -ECANCELED;
3461 goto error;
3462 }
3463
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003464 mutex_lock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003465 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003466 if (ret < 0) {
3467 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003468 goto error;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003469 }
Chris Masonec44a352008-04-28 15:29:52 -04003470
3471 /*
3472 * this shouldn't happen, it means the last relocate
3473 * failed
3474 */
3475 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003476 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003477
3478 ret = btrfs_previous_item(chunk_root, path, 0,
3479 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003480 if (ret) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003481 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003482 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003483 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003484 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003485
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003486 leaf = path->nodes[0];
3487 slot = path->slots[0];
3488 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3489
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003490 if (found_key.objectid != key.objectid) {
3491 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Chris Masonec44a352008-04-28 15:29:52 -04003492 break;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003493 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003494
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003495 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba12907fc2015-10-10 17:16:50 +02003496 chunk_type = btrfs_chunk_type(leaf, chunk);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003497
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003498 if (!counting) {
3499 spin_lock(&fs_info->balance_lock);
3500 bctl->stat.considered++;
3501 spin_unlock(&fs_info->balance_lock);
3502 }
3503
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003504 ret = should_balance_chunk(chunk_root, leaf, chunk,
3505 found_key.offset);
Zhao Lei2c9fe832015-11-09 11:51:32 +08003506
David Sterbab3b4aa72011-04-21 01:20:15 +02003507 btrfs_release_path(path);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003508 if (!ret) {
3509 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003510 goto loop;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003511 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003512
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003513 if (counting) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003514 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003515 spin_lock(&fs_info->balance_lock);
3516 bctl->stat.expected++;
3517 spin_unlock(&fs_info->balance_lock);
David Sterba12907fc2015-10-10 17:16:50 +02003518
3519 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3520 count_data++;
3521 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3522 count_sys++;
3523 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3524 count_meta++;
3525
3526 goto loop;
3527 }
3528
3529 /*
3530 * Apply limit_min filter, no need to check if the LIMITS
3531 * filter is used, limit_min is 0 by default
3532 */
3533 if (((chunk_type & BTRFS_BLOCK_GROUP_DATA) &&
3534 count_data < bctl->data.limit_min)
3535 || ((chunk_type & BTRFS_BLOCK_GROUP_METADATA) &&
3536 count_meta < bctl->meta.limit_min)
3537 || ((chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) &&
3538 count_sys < bctl->sys.limit_min)) {
3539 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003540 goto loop;
3541 }
3542
Zhao Lei2c9fe832015-11-09 11:51:32 +08003543 if ((chunk_type & BTRFS_BLOCK_GROUP_DATA) && !chunk_reserved) {
3544 trans = btrfs_start_transaction(chunk_root, 0);
3545 if (IS_ERR(trans)) {
3546 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3547 ret = PTR_ERR(trans);
3548 goto error;
3549 }
3550
3551 ret = btrfs_force_chunk_alloc(trans, chunk_root,
3552 BTRFS_BLOCK_GROUP_DATA);
3553 if (ret < 0) {
3554 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3555 goto error;
3556 }
3557
3558 btrfs_end_transaction(trans, chunk_root);
3559 chunk_reserved = 1;
3560 }
3561
Chris Masonec44a352008-04-28 15:29:52 -04003562 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003563 found_key.offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01003564 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
Josef Bacik508794e2011-07-02 21:24:41 +00003565 if (ret && ret != -ENOSPC)
3566 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003567 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003568 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003569 } else {
3570 spin_lock(&fs_info->balance_lock);
3571 bctl->stat.completed++;
3572 spin_unlock(&fs_info->balance_lock);
3573 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003574loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003575 if (found_key.offset == 0)
3576 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003577 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003578 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003579
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003580 if (counting) {
3581 btrfs_release_path(path);
3582 counting = false;
3583 goto again;
3584 }
Chris Masonec44a352008-04-28 15:29:52 -04003585error:
3586 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003587 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003588 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003589 enospc_errors);
3590 if (!ret)
3591 ret = -ENOSPC;
3592 }
3593
Chris Masonec44a352008-04-28 15:29:52 -04003594 return ret;
3595}
3596
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003597/**
3598 * alloc_profile_is_valid - see if a given profile is valid and reduced
3599 * @flags: profile to validate
3600 * @extended: if true @flags is treated as an extended profile
3601 */
3602static int alloc_profile_is_valid(u64 flags, int extended)
3603{
3604 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3605 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3606
3607 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3608
3609 /* 1) check that all other bits are zeroed */
3610 if (flags & ~mask)
3611 return 0;
3612
3613 /* 2) see if profile is reduced */
3614 if (flags == 0)
3615 return !extended; /* "0" is valid for usual profiles */
3616
3617 /* true if exactly one bit set */
3618 return (flags & (flags - 1)) == 0;
3619}
3620
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003621static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3622{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003623 /* cancel requested || normal exit path */
3624 return atomic_read(&fs_info->balance_cancel_req) ||
3625 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3626 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003627}
3628
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003629static void __cancel_balance(struct btrfs_fs_info *fs_info)
3630{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003631 int ret;
3632
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003633 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003634 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003635 if (ret)
Anand Jaina4553fe2015-09-25 14:43:01 +08003636 btrfs_std_error(fs_info, ret, NULL);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003637
3638 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003639}
3640
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003641/* Non-zero return value signifies invalidity */
3642static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
3643 u64 allowed)
3644{
3645 return ((bctl_arg->flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3646 (!alloc_profile_is_valid(bctl_arg->target, 1) ||
3647 (bctl_arg->target & ~allowed)));
3648}
3649
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003650/*
3651 * Should be called with both balance and volume mutexes held
3652 */
3653int btrfs_balance(struct btrfs_balance_control *bctl,
3654 struct btrfs_ioctl_balance_args *bargs)
3655{
3656 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003657 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003658 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003659 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003660 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003661 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003662
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003663 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003664 atomic_read(&fs_info->balance_pause_req) ||
3665 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003666 ret = -EINVAL;
3667 goto out;
3668 }
3669
Ilya Dryomove4837f82012-03-27 17:09:17 +03003670 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3671 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3672 mixed = 1;
3673
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003674 /*
3675 * In case of mixed groups both data and meta should be picked,
3676 * and identical options should be given for both of them.
3677 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003678 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3679 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003680 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3681 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3682 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003683 btrfs_err(fs_info, "with mixed groups data and "
3684 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003685 ret = -EINVAL;
3686 goto out;
3687 }
3688 }
3689
Stefan Behrens8dabb742012-11-06 13:15:27 +01003690 num_devices = fs_info->fs_devices->num_devices;
3691 btrfs_dev_replace_lock(&fs_info->dev_replace);
3692 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3693 BUG_ON(num_devices < 1);
3694 num_devices--;
3695 }
3696 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003697 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003698 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003699 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003700 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003701 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003702 if (num_devices > 2)
3703 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3704 if (num_devices > 3)
3705 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3706 BTRFS_BLOCK_GROUP_RAID6);
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003707 if (validate_convert_profile(&bctl->data, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003708 btrfs_err(fs_info, "unable to start balance with target "
3709 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003710 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003711 ret = -EINVAL;
3712 goto out;
3713 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003714 if (validate_convert_profile(&bctl->meta, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003715 btrfs_err(fs_info,
3716 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003717 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003718 ret = -EINVAL;
3719 goto out;
3720 }
Alexandru Moisebdcd3c92015-09-22 20:02:25 +00003721 if (validate_convert_profile(&bctl->sys, allowed)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003722 btrfs_err(fs_info,
3723 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003724 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003725 ret = -EINVAL;
3726 goto out;
3727 }
3728
Ilya Dryomove4837f82012-03-27 17:09:17 +03003729 /* allow dup'ed data chunks only in mixed mode */
3730 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003731 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003732 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003733 ret = -EINVAL;
3734 goto out;
3735 }
3736
3737 /* allow to reduce meta or sys integrity only if force set */
3738 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003739 BTRFS_BLOCK_GROUP_RAID10 |
3740 BTRFS_BLOCK_GROUP_RAID5 |
3741 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003742 do {
3743 seq = read_seqbegin(&fs_info->profiles_lock);
3744
3745 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3746 (fs_info->avail_system_alloc_bits & allowed) &&
3747 !(bctl->sys.target & allowed)) ||
3748 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3749 (fs_info->avail_metadata_alloc_bits & allowed) &&
3750 !(bctl->meta.target & allowed))) {
3751 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003752 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003753 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003754 btrfs_err(fs_info, "balance will reduce metadata "
3755 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003756 ret = -EINVAL;
3757 goto out;
3758 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003759 }
Miao Xiede98ced2013-01-29 10:13:12 +00003760 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003761
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003762 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
Zhao Lei943c6e92015-08-19 15:54:15 +08003763 fs_info->num_tolerated_disk_barrier_failures = min(
3764 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info),
3765 btrfs_get_num_tolerated_disk_barrier_failures(
3766 bctl->sys.target));
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003767 }
3768
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003769 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003770 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003771 goto out;
3772
Ilya Dryomov59641012012-01-16 22:04:48 +02003773 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3774 BUG_ON(ret == -EEXIST);
3775 set_balance_control(bctl);
3776 } else {
3777 BUG_ON(ret != -EEXIST);
3778 spin_lock(&fs_info->balance_lock);
3779 update_balance_args(bctl);
3780 spin_unlock(&fs_info->balance_lock);
3781 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003782
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003783 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003784 mutex_unlock(&fs_info->balance_mutex);
3785
3786 ret = __btrfs_balance(fs_info);
3787
3788 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003789 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003790
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003791 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3792 fs_info->num_tolerated_disk_barrier_failures =
3793 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3794 }
3795
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003796 if (bargs) {
3797 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003798 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003799 }
3800
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003801 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3802 balance_need_close(fs_info)) {
3803 __cancel_balance(fs_info);
3804 }
3805
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003806 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003807
3808 return ret;
3809out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003810 if (bctl->flags & BTRFS_BALANCE_RESUME)
3811 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003812 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003813 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003814 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3815 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003816 return ret;
3817}
3818
3819static int balance_kthread(void *data)
3820{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003821 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003822 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003823
3824 mutex_lock(&fs_info->volume_mutex);
3825 mutex_lock(&fs_info->balance_mutex);
3826
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003827 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003828 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003829 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003830 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003831
3832 mutex_unlock(&fs_info->balance_mutex);
3833 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003834
Ilya Dryomov59641012012-01-16 22:04:48 +02003835 return ret;
3836}
3837
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003838int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3839{
3840 struct task_struct *tsk;
3841
3842 spin_lock(&fs_info->balance_lock);
3843 if (!fs_info->balance_ctl) {
3844 spin_unlock(&fs_info->balance_lock);
3845 return 0;
3846 }
3847 spin_unlock(&fs_info->balance_lock);
3848
3849 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003850 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003851 return 0;
3852 }
3853
3854 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303855 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003856}
3857
Ilya Dryomov68310a52012-06-22 12:24:12 -06003858int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003859{
Ilya Dryomov59641012012-01-16 22:04:48 +02003860 struct btrfs_balance_control *bctl;
3861 struct btrfs_balance_item *item;
3862 struct btrfs_disk_balance_args disk_bargs;
3863 struct btrfs_path *path;
3864 struct extent_buffer *leaf;
3865 struct btrfs_key key;
3866 int ret;
3867
3868 path = btrfs_alloc_path();
3869 if (!path)
3870 return -ENOMEM;
3871
Ilya Dryomov68310a52012-06-22 12:24:12 -06003872 key.objectid = BTRFS_BALANCE_OBJECTID;
3873 key.type = BTRFS_BALANCE_ITEM_KEY;
3874 key.offset = 0;
3875
3876 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3877 if (ret < 0)
3878 goto out;
3879 if (ret > 0) { /* ret = -ENOENT; */
3880 ret = 0;
3881 goto out;
3882 }
3883
Ilya Dryomov59641012012-01-16 22:04:48 +02003884 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3885 if (!bctl) {
3886 ret = -ENOMEM;
3887 goto out;
3888 }
3889
Ilya Dryomov59641012012-01-16 22:04:48 +02003890 leaf = path->nodes[0];
3891 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3892
Ilya Dryomov68310a52012-06-22 12:24:12 -06003893 bctl->fs_info = fs_info;
3894 bctl->flags = btrfs_balance_flags(leaf, item);
3895 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003896
3897 btrfs_balance_data(leaf, item, &disk_bargs);
3898 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3899 btrfs_balance_meta(leaf, item, &disk_bargs);
3900 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3901 btrfs_balance_sys(leaf, item, &disk_bargs);
3902 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3903
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003904 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3905
Ilya Dryomov68310a52012-06-22 12:24:12 -06003906 mutex_lock(&fs_info->volume_mutex);
3907 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003908
Ilya Dryomov68310a52012-06-22 12:24:12 -06003909 set_balance_control(bctl);
3910
3911 mutex_unlock(&fs_info->balance_mutex);
3912 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003913out:
3914 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003915 return ret;
3916}
3917
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003918int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3919{
3920 int ret = 0;
3921
3922 mutex_lock(&fs_info->balance_mutex);
3923 if (!fs_info->balance_ctl) {
3924 mutex_unlock(&fs_info->balance_mutex);
3925 return -ENOTCONN;
3926 }
3927
3928 if (atomic_read(&fs_info->balance_running)) {
3929 atomic_inc(&fs_info->balance_pause_req);
3930 mutex_unlock(&fs_info->balance_mutex);
3931
3932 wait_event(fs_info->balance_wait_q,
3933 atomic_read(&fs_info->balance_running) == 0);
3934
3935 mutex_lock(&fs_info->balance_mutex);
3936 /* we are good with balance_ctl ripped off from under us */
3937 BUG_ON(atomic_read(&fs_info->balance_running));
3938 atomic_dec(&fs_info->balance_pause_req);
3939 } else {
3940 ret = -ENOTCONN;
3941 }
3942
3943 mutex_unlock(&fs_info->balance_mutex);
3944 return ret;
3945}
3946
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003947int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3948{
Ilya Dryomove649e582013-10-10 20:40:21 +03003949 if (fs_info->sb->s_flags & MS_RDONLY)
3950 return -EROFS;
3951
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003952 mutex_lock(&fs_info->balance_mutex);
3953 if (!fs_info->balance_ctl) {
3954 mutex_unlock(&fs_info->balance_mutex);
3955 return -ENOTCONN;
3956 }
3957
3958 atomic_inc(&fs_info->balance_cancel_req);
3959 /*
3960 * if we are running just wait and return, balance item is
3961 * deleted in btrfs_balance in this case
3962 */
3963 if (atomic_read(&fs_info->balance_running)) {
3964 mutex_unlock(&fs_info->balance_mutex);
3965 wait_event(fs_info->balance_wait_q,
3966 atomic_read(&fs_info->balance_running) == 0);
3967 mutex_lock(&fs_info->balance_mutex);
3968 } else {
3969 /* __cancel_balance needs volume_mutex */
3970 mutex_unlock(&fs_info->balance_mutex);
3971 mutex_lock(&fs_info->volume_mutex);
3972 mutex_lock(&fs_info->balance_mutex);
3973
3974 if (fs_info->balance_ctl)
3975 __cancel_balance(fs_info);
3976
3977 mutex_unlock(&fs_info->volume_mutex);
3978 }
3979
3980 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3981 atomic_dec(&fs_info->balance_cancel_req);
3982 mutex_unlock(&fs_info->balance_mutex);
3983 return 0;
3984}
3985
Stefan Behrens803b2f52013-08-15 17:11:21 +02003986static int btrfs_uuid_scan_kthread(void *data)
3987{
3988 struct btrfs_fs_info *fs_info = data;
3989 struct btrfs_root *root = fs_info->tree_root;
3990 struct btrfs_key key;
3991 struct btrfs_key max_key;
3992 struct btrfs_path *path = NULL;
3993 int ret = 0;
3994 struct extent_buffer *eb;
3995 int slot;
3996 struct btrfs_root_item root_item;
3997 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003998 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003999
4000 path = btrfs_alloc_path();
4001 if (!path) {
4002 ret = -ENOMEM;
4003 goto out;
4004 }
4005
4006 key.objectid = 0;
4007 key.type = BTRFS_ROOT_ITEM_KEY;
4008 key.offset = 0;
4009
4010 max_key.objectid = (u64)-1;
4011 max_key.type = BTRFS_ROOT_ITEM_KEY;
4012 max_key.offset = (u64)-1;
4013
Stefan Behrens803b2f52013-08-15 17:11:21 +02004014 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01004015 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004016 if (ret) {
4017 if (ret > 0)
4018 ret = 0;
4019 break;
4020 }
4021
4022 if (key.type != BTRFS_ROOT_ITEM_KEY ||
4023 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
4024 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
4025 key.objectid > BTRFS_LAST_FREE_OBJECTID)
4026 goto skip;
4027
4028 eb = path->nodes[0];
4029 slot = path->slots[0];
4030 item_size = btrfs_item_size_nr(eb, slot);
4031 if (item_size < sizeof(root_item))
4032 goto skip;
4033
Stefan Behrens803b2f52013-08-15 17:11:21 +02004034 read_extent_buffer(eb, &root_item,
4035 btrfs_item_ptr_offset(eb, slot),
4036 (int)sizeof(root_item));
4037 if (btrfs_root_refs(&root_item) == 0)
4038 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004039
4040 if (!btrfs_is_empty_uuid(root_item.uuid) ||
4041 !btrfs_is_empty_uuid(root_item.received_uuid)) {
4042 if (trans)
4043 goto update_tree;
4044
4045 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004046 /*
4047 * 1 - subvol uuid item
4048 * 1 - received_subvol uuid item
4049 */
4050 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
4051 if (IS_ERR(trans)) {
4052 ret = PTR_ERR(trans);
4053 break;
4054 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004055 continue;
4056 } else {
4057 goto skip;
4058 }
4059update_tree:
4060 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004061 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4062 root_item.uuid,
4063 BTRFS_UUID_KEY_SUBVOL,
4064 key.objectid);
4065 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004066 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004067 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004068 break;
4069 }
4070 }
4071
4072 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02004073 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
4074 root_item.received_uuid,
4075 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4076 key.objectid);
4077 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004078 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02004079 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004080 break;
4081 }
4082 }
4083
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004084skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02004085 if (trans) {
4086 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004087 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004088 if (ret)
4089 break;
4090 }
4091
Stefan Behrens803b2f52013-08-15 17:11:21 +02004092 btrfs_release_path(path);
4093 if (key.offset < (u64)-1) {
4094 key.offset++;
4095 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
4096 key.offset = 0;
4097 key.type = BTRFS_ROOT_ITEM_KEY;
4098 } else if (key.objectid < (u64)-1) {
4099 key.offset = 0;
4100 key.type = BTRFS_ROOT_ITEM_KEY;
4101 key.objectid++;
4102 } else {
4103 break;
4104 }
4105 cond_resched();
4106 }
4107
4108out:
4109 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01004110 if (trans && !IS_ERR(trans))
4111 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02004112 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05004113 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004114 else
4115 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004116 up(&fs_info->uuid_tree_rescan_sem);
4117 return 0;
4118}
4119
Stefan Behrens70f80172013-08-15 17:11:23 +02004120/*
4121 * Callback for btrfs_uuid_tree_iterate().
4122 * returns:
4123 * 0 check succeeded, the entry is not outdated.
4124 * < 0 if an error occured.
4125 * > 0 if the check failed, which means the caller shall remove the entry.
4126 */
4127static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
4128 u8 *uuid, u8 type, u64 subid)
4129{
4130 struct btrfs_key key;
4131 int ret = 0;
4132 struct btrfs_root *subvol_root;
4133
4134 if (type != BTRFS_UUID_KEY_SUBVOL &&
4135 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
4136 goto out;
4137
4138 key.objectid = subid;
4139 key.type = BTRFS_ROOT_ITEM_KEY;
4140 key.offset = (u64)-1;
4141 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
4142 if (IS_ERR(subvol_root)) {
4143 ret = PTR_ERR(subvol_root);
4144 if (ret == -ENOENT)
4145 ret = 1;
4146 goto out;
4147 }
4148
4149 switch (type) {
4150 case BTRFS_UUID_KEY_SUBVOL:
4151 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
4152 ret = 1;
4153 break;
4154 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
4155 if (memcmp(uuid, subvol_root->root_item.received_uuid,
4156 BTRFS_UUID_SIZE))
4157 ret = 1;
4158 break;
4159 }
4160
4161out:
4162 return ret;
4163}
4164
4165static int btrfs_uuid_rescan_kthread(void *data)
4166{
4167 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
4168 int ret;
4169
4170 /*
4171 * 1st step is to iterate through the existing UUID tree and
4172 * to delete all entries that contain outdated data.
4173 * 2nd step is to add all missing entries to the UUID tree.
4174 */
4175 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
4176 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05004177 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02004178 up(&fs_info->uuid_tree_rescan_sem);
4179 return ret;
4180 }
4181 return btrfs_uuid_scan_kthread(data);
4182}
4183
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004184int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
4185{
4186 struct btrfs_trans_handle *trans;
4187 struct btrfs_root *tree_root = fs_info->tree_root;
4188 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02004189 struct task_struct *task;
4190 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004191
4192 /*
4193 * 1 - root node
4194 * 1 - root item
4195 */
4196 trans = btrfs_start_transaction(tree_root, 2);
4197 if (IS_ERR(trans))
4198 return PTR_ERR(trans);
4199
4200 uuid_root = btrfs_create_tree(trans, fs_info,
4201 BTRFS_UUID_TREE_OBJECTID);
4202 if (IS_ERR(uuid_root)) {
David Sterba6d13f542015-04-24 19:12:01 +02004203 ret = PTR_ERR(uuid_root);
4204 btrfs_abort_transaction(trans, tree_root, ret);
4205 return ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004206 }
4207
4208 fs_info->uuid_root = uuid_root;
4209
Stefan Behrens803b2f52013-08-15 17:11:21 +02004210 ret = btrfs_commit_transaction(trans, tree_root);
4211 if (ret)
4212 return ret;
4213
4214 down(&fs_info->uuid_tree_rescan_sem);
4215 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
4216 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02004217 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004218 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02004219 up(&fs_info->uuid_tree_rescan_sem);
4220 return PTR_ERR(task);
4221 }
4222
4223 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004224}
Stefan Behrens803b2f52013-08-15 17:11:21 +02004225
Stefan Behrens70f80172013-08-15 17:11:23 +02004226int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4227{
4228 struct task_struct *task;
4229
4230 down(&fs_info->uuid_tree_rescan_sem);
4231 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
4232 if (IS_ERR(task)) {
4233 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05004234 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02004235 up(&fs_info->uuid_tree_rescan_sem);
4236 return PTR_ERR(task);
4237 }
4238
4239 return 0;
4240}
4241
Chris Mason8f18cf12008-04-25 16:53:30 -04004242/*
4243 * shrinking a device means finding all of the device extents past
4244 * the new size, and then following the back refs to the chunks.
4245 * The chunk relocation code actually frees the device extent
4246 */
4247int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4248{
4249 struct btrfs_trans_handle *trans;
4250 struct btrfs_root *root = device->dev_root;
4251 struct btrfs_dev_extent *dev_extent = NULL;
4252 struct btrfs_path *path;
4253 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04004254 u64 chunk_offset;
4255 int ret;
4256 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04004257 int failed = 0;
4258 bool retried = false;
Filipe Manana53e489b2015-06-02 14:43:21 +01004259 bool checked_pending_chunks = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04004260 struct extent_buffer *l;
4261 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02004262 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04004263 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08004264 u64 old_size = btrfs_device_get_total_bytes(device);
4265 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04004266
Stefan Behrens63a212a2012-11-05 18:29:28 +01004267 if (device->is_tgtdev_for_dev_replace)
4268 return -EINVAL;
4269
Chris Mason8f18cf12008-04-25 16:53:30 -04004270 path = btrfs_alloc_path();
4271 if (!path)
4272 return -ENOMEM;
4273
Chris Mason8f18cf12008-04-25 16:53:30 -04004274 path->reada = 2;
4275
Chris Mason7d9eb122008-07-08 14:19:17 -04004276 lock_chunks(root);
4277
Miao Xie7cc8e582014-09-03 21:35:38 +08004278 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04004279 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05004280 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004281 spin_lock(&root->fs_info->free_chunk_lock);
4282 root->fs_info->free_chunk_space -= diff;
4283 spin_unlock(&root->fs_info->free_chunk_lock);
4284 }
Chris Mason7d9eb122008-07-08 14:19:17 -04004285 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004286
Josef Bacikba1bf482009-09-11 16:11:19 -04004287again:
Chris Mason8f18cf12008-04-25 16:53:30 -04004288 key.objectid = device->devid;
4289 key.offset = (u64)-1;
4290 key.type = BTRFS_DEV_EXTENT_KEY;
4291
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004292 do {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004293 mutex_lock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004294 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004295 if (ret < 0) {
4296 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004297 goto done;
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004298 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004299
4300 ret = btrfs_previous_item(root, path, 0, key.type);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004301 if (ret)
4302 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04004303 if (ret < 0)
4304 goto done;
4305 if (ret) {
4306 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004307 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004308 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004309 }
4310
4311 l = path->nodes[0];
4312 slot = path->slots[0];
4313 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4314
Josef Bacikba1bf482009-09-11 16:11:19 -04004315 if (key.objectid != device->devid) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004316 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004317 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004318 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004319 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004320
4321 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4322 length = btrfs_dev_extent_length(l, dev_extent);
4323
Josef Bacikba1bf482009-09-11 16:11:19 -04004324 if (key.offset + length <= new_size) {
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004325 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
David Sterbab3b4aa72011-04-21 01:20:15 +02004326 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004327 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004328 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004329
Chris Mason8f18cf12008-04-25 16:53:30 -04004330 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004331 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004332
Zhaoleidc2ee4e2015-08-05 18:00:04 +08004333 ret = btrfs_relocate_chunk(root, chunk_offset);
Filipe Manana67c5e7d2015-06-11 00:58:53 +01004334 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex);
Josef Bacikba1bf482009-09-11 16:11:19 -04004335 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004336 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004337 if (ret == -ENOSPC)
4338 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004339 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004340
4341 if (failed && !retried) {
4342 failed = 0;
4343 retried = true;
4344 goto again;
4345 } else if (failed && retried) {
4346 ret = -ENOSPC;
Josef Bacikba1bf482009-09-11 16:11:19 -04004347 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004348 }
4349
Chris Balld6397ba2009-04-27 07:29:03 -04004350 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004351 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004352 if (IS_ERR(trans)) {
4353 ret = PTR_ERR(trans);
4354 goto done;
4355 }
4356
Chris Balld6397ba2009-04-27 07:29:03 -04004357 lock_chunks(root);
Filipe Manana53e489b2015-06-02 14:43:21 +01004358
4359 /*
4360 * We checked in the above loop all device extents that were already in
4361 * the device tree. However before we have updated the device's
4362 * total_bytes to the new size, we might have had chunk allocations that
4363 * have not complete yet (new block groups attached to transaction
4364 * handles), and therefore their device extents were not yet in the
4365 * device tree and we missed them in the loop above. So if we have any
4366 * pending chunk using a device extent that overlaps the device range
4367 * that we can not use anymore, commit the current transaction and
4368 * repeat the search on the device tree - this way we guarantee we will
4369 * not have chunks using device extents that end beyond 'new_size'.
4370 */
4371 if (!checked_pending_chunks) {
4372 u64 start = new_size;
4373 u64 len = old_size - new_size;
4374
Jeff Mahoney499f3772015-06-15 09:41:17 -04004375 if (contains_pending_extent(trans->transaction, device,
4376 &start, len)) {
Filipe Manana53e489b2015-06-02 14:43:21 +01004377 unlock_chunks(root);
4378 checked_pending_chunks = true;
4379 failed = 0;
4380 retried = false;
4381 ret = btrfs_commit_transaction(trans, root);
4382 if (ret)
4383 goto done;
4384 goto again;
4385 }
4386 }
4387
Miao Xie7cc8e582014-09-03 21:35:38 +08004388 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004389 if (list_empty(&device->resized_list))
4390 list_add_tail(&device->resized_list,
4391 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004392
Chris Balld6397ba2009-04-27 07:29:03 -04004393 WARN_ON(diff > old_total);
4394 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4395 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004396
4397 /* Now btrfs_update_device() will change the on-disk size. */
4398 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004399 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004400done:
4401 btrfs_free_path(path);
Filipe Manana53e489b2015-06-02 14:43:21 +01004402 if (ret) {
4403 lock_chunks(root);
4404 btrfs_device_set_total_bytes(device, old_size);
4405 if (device->writeable)
4406 device->fs_devices->total_rw_bytes += diff;
4407 spin_lock(&root->fs_info->free_chunk_lock);
4408 root->fs_info->free_chunk_space += diff;
4409 spin_unlock(&root->fs_info->free_chunk_lock);
4410 unlock_chunks(root);
4411 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004412 return ret;
4413}
4414
Li Zefan125ccb02011-12-08 15:07:24 +08004415static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004416 struct btrfs_key *key,
4417 struct btrfs_chunk *chunk, int item_size)
4418{
David Sterba6c417612011-04-13 15:41:04 +02004419 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004420 struct btrfs_disk_key disk_key;
4421 u32 array_size;
4422 u8 *ptr;
4423
Miao Xiefe48a5c2014-09-03 21:35:39 +08004424 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004425 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004426 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004427 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4428 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004429 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004430 }
Chris Mason0b86a832008-03-24 15:01:56 -04004431
4432 ptr = super_copy->sys_chunk_array + array_size;
4433 btrfs_cpu_key_to_disk(&disk_key, key);
4434 memcpy(ptr, &disk_key, sizeof(disk_key));
4435 ptr += sizeof(disk_key);
4436 memcpy(ptr, chunk, item_size);
4437 item_size += sizeof(disk_key);
4438 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004439 unlock_chunks(root);
4440
Chris Mason0b86a832008-03-24 15:01:56 -04004441 return 0;
4442}
4443
Miao Xieb2117a32011-01-05 10:07:28 +00004444/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004445 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004446 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004447static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004448{
Arne Jansen73c5de02011-04-12 12:07:57 +02004449 const struct btrfs_device_info *di_a = a;
4450 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004451
Arne Jansen73c5de02011-04-12 12:07:57 +02004452 if (di_a->max_avail > di_b->max_avail)
4453 return -1;
4454 if (di_a->max_avail < di_b->max_avail)
4455 return 1;
4456 if (di_a->total_avail > di_b->total_avail)
4457 return -1;
4458 if (di_a->total_avail < di_b->total_avail)
4459 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004460 return 0;
4461}
4462
David Woodhouse53b381b2013-01-29 18:40:14 -05004463static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4464{
4465 /* TODO allow them to set a preferred stripe size */
4466 return 64 * 1024;
4467}
4468
4469static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4470{
Zhao Leiffe2d202015-01-20 15:11:44 +08004471 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004472 return;
4473
Miao Xieceda0862013-04-11 10:30:16 +00004474 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004475}
4476
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004477#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4478 - sizeof(struct btrfs_item) \
4479 - sizeof(struct btrfs_chunk)) \
4480 / sizeof(struct btrfs_stripe) + 1)
4481
4482#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4483 - 2 * sizeof(struct btrfs_disk_key) \
4484 - 2 * sizeof(struct btrfs_chunk)) \
4485 / sizeof(struct btrfs_stripe) + 1)
4486
Miao Xieb2117a32011-01-05 10:07:28 +00004487static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004488 struct btrfs_root *extent_root, u64 start,
4489 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004490{
4491 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004492 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4493 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004494 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004495 struct extent_map_tree *em_tree;
4496 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004497 struct btrfs_device_info *devices_info = NULL;
4498 u64 total_avail;
4499 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004500 int data_stripes; /* number of stripes that count for
4501 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004502 int sub_stripes; /* sub_stripes info for map */
4503 int dev_stripes; /* stripes per dev */
4504 int devs_max; /* max devs to use */
4505 int devs_min; /* min devs needed */
4506 int devs_increment; /* ndevs has to be a multiple of this */
4507 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004508 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004509 u64 max_stripe_size;
4510 u64 max_chunk_size;
4511 u64 stripe_size;
4512 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004513 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004514 int ndevs;
4515 int i;
4516 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004517 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004518
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004519 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004520
Miao Xieb2117a32011-01-05 10:07:28 +00004521 if (list_empty(&fs_devices->alloc_list))
4522 return -ENOSPC;
4523
Liu Bo31e50222012-11-21 14:18:10 +00004524 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004525
Liu Bo31e50222012-11-21 14:18:10 +00004526 sub_stripes = btrfs_raid_array[index].sub_stripes;
4527 dev_stripes = btrfs_raid_array[index].dev_stripes;
4528 devs_max = btrfs_raid_array[index].devs_max;
4529 devs_min = btrfs_raid_array[index].devs_min;
4530 devs_increment = btrfs_raid_array[index].devs_increment;
4531 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004532
4533 if (type & BTRFS_BLOCK_GROUP_DATA) {
4534 max_stripe_size = 1024 * 1024 * 1024;
4535 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004536 if (!devs_max)
4537 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004538 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004539 /* for larger filesystems, use larger metadata chunks */
4540 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4541 max_stripe_size = 1024 * 1024 * 1024;
4542 else
4543 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004544 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004545 if (!devs_max)
4546 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004547 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004548 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004549 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004550 if (!devs_max)
4551 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004552 } else {
David Sterba351fd352014-05-15 16:48:20 +02004553 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004554 type);
4555 BUG_ON(1);
4556 }
4557
4558 /* we don't want a chunk larger than 10% of writeable space */
4559 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4560 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004561
David Sterba31e818f2015-02-20 18:00:26 +01004562 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004563 GFP_NOFS);
4564 if (!devices_info)
4565 return -ENOMEM;
4566
Arne Jansen73c5de02011-04-12 12:07:57 +02004567 cur = fs_devices->alloc_list.next;
4568
4569 /*
4570 * in the first pass through the devices list, we gather information
4571 * about the available holes on each device.
4572 */
4573 ndevs = 0;
4574 while (cur != &fs_devices->alloc_list) {
4575 struct btrfs_device *device;
4576 u64 max_avail;
4577 u64 dev_offset;
4578
4579 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4580
4581 cur = cur->next;
4582
4583 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004584 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004585 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004586 continue;
4587 }
4588
Stefan Behrens63a212a2012-11-05 18:29:28 +01004589 if (!device->in_fs_metadata ||
4590 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004591 continue;
4592
4593 if (device->total_bytes > device->bytes_used)
4594 total_avail = device->total_bytes - device->bytes_used;
4595 else
4596 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004597
4598 /* If there is no space on this device, skip it. */
4599 if (total_avail == 0)
4600 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004601
Josef Bacik6df9a952013-06-27 13:22:46 -04004602 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004603 max_stripe_size * dev_stripes,
4604 &dev_offset, &max_avail);
4605 if (ret && ret != -ENOSPC)
4606 goto error;
4607
4608 if (ret == 0)
4609 max_avail = max_stripe_size * dev_stripes;
4610
4611 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4612 continue;
4613
Eric Sandeen063d0062013-01-31 00:55:01 +00004614 if (ndevs == fs_devices->rw_devices) {
4615 WARN(1, "%s: found more than %llu devices\n",
4616 __func__, fs_devices->rw_devices);
4617 break;
4618 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004619 devices_info[ndevs].dev_offset = dev_offset;
4620 devices_info[ndevs].max_avail = max_avail;
4621 devices_info[ndevs].total_avail = total_avail;
4622 devices_info[ndevs].dev = device;
4623 ++ndevs;
4624 }
4625
4626 /*
4627 * now sort the devices by hole size / available space
4628 */
4629 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4630 btrfs_cmp_device_info, NULL);
4631
4632 /* round down to number of usable stripes */
4633 ndevs -= ndevs % devs_increment;
4634
4635 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4636 ret = -ENOSPC;
4637 goto error;
4638 }
4639
4640 if (devs_max && ndevs > devs_max)
4641 ndevs = devs_max;
4642 /*
4643 * the primary goal is to maximize the number of stripes, so use as many
4644 * devices as possible, even if the stripes are not maximum sized.
4645 */
4646 stripe_size = devices_info[ndevs-1].max_avail;
4647 num_stripes = ndevs * dev_stripes;
4648
David Woodhouse53b381b2013-01-29 18:40:14 -05004649 /*
4650 * this will have to be fixed for RAID1 and RAID10 over
4651 * more drives
4652 */
4653 data_stripes = num_stripes / ncopies;
4654
David Woodhouse53b381b2013-01-29 18:40:14 -05004655 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4656 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4657 btrfs_super_stripesize(info->super_copy));
4658 data_stripes = num_stripes - 1;
4659 }
4660 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4661 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4662 btrfs_super_stripesize(info->super_copy));
4663 data_stripes = num_stripes - 2;
4664 }
Chris Mason86db2572013-02-20 16:23:40 -05004665
4666 /*
4667 * Use the number of data stripes to figure out how big this chunk
4668 * is really going to be in terms of logical address space,
4669 * and compare that answer with the max chunk size
4670 */
4671 if (stripe_size * data_stripes > max_chunk_size) {
4672 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004673
4674 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004675
4676 /* bump the answer up to a 16MB boundary */
4677 stripe_size = (stripe_size + mask) & ~mask;
4678
4679 /* but don't go higher than the limits we found
4680 * while searching for free extents
4681 */
4682 if (stripe_size > devices_info[ndevs-1].max_avail)
4683 stripe_size = devices_info[ndevs-1].max_avail;
4684 }
4685
David Sterbab8b93ad2015-01-16 17:26:13 +01004686 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004687
4688 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004689 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004690 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004691
Miao Xieb2117a32011-01-05 10:07:28 +00004692 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4693 if (!map) {
4694 ret = -ENOMEM;
4695 goto error;
4696 }
4697 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004698
Arne Jansen73c5de02011-04-12 12:07:57 +02004699 for (i = 0; i < ndevs; ++i) {
4700 for (j = 0; j < dev_stripes; ++j) {
4701 int s = i * dev_stripes + j;
4702 map->stripes[s].dev = devices_info[i].dev;
4703 map->stripes[s].physical = devices_info[i].dev_offset +
4704 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004705 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004706 }
Chris Mason593060d2008-03-25 16:50:33 -04004707 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004708 map->stripe_len = raid_stripe_len;
4709 map->io_align = raid_stripe_len;
4710 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004711 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004712 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004713
David Woodhouse53b381b2013-01-29 18:40:14 -05004714 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004715
Arne Jansen73c5de02011-04-12 12:07:57 +02004716 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004717
David Sterba172ddd62011-04-21 00:48:27 +02004718 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004719 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004720 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004721 ret = -ENOMEM;
4722 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004723 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004724 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004725 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004726 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004727 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004728 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004729 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004730 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004731
Chris Mason0b86a832008-03-24 15:01:56 -04004732 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004733 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004734 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004735 if (!ret) {
4736 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4737 atomic_inc(&em->refs);
4738 }
Chris Mason890871b2009-09-02 16:24:52 -04004739 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004740 if (ret) {
4741 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004742 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004743 }
Yan Zheng2b820322008-11-17 21:11:30 -05004744
Josef Bacik04487482013-01-29 15:03:37 -05004745 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4746 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4747 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004748 if (ret)
4749 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004750
Miao Xie7cc8e582014-09-03 21:35:38 +08004751 for (i = 0; i < map->num_stripes; i++) {
4752 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4753 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4754 }
Miao Xie43530c42014-09-03 21:35:36 +08004755
Miao Xie1c116182014-09-03 21:35:37 +08004756 spin_lock(&extent_root->fs_info->free_chunk_lock);
4757 extent_root->fs_info->free_chunk_space -= (stripe_size *
4758 map->num_stripes);
4759 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4760
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004761 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004762 check_raid56_incompat_flag(extent_root->fs_info, type);
4763
Miao Xieb2117a32011-01-05 10:07:28 +00004764 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004765 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004766
Josef Bacik6df9a952013-06-27 13:22:46 -04004767error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004768 write_lock(&em_tree->lock);
4769 remove_extent_mapping(em_tree, em);
4770 write_unlock(&em_tree->lock);
4771
4772 /* One for our allocation */
4773 free_extent_map(em);
4774 /* One for the tree reference */
4775 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004776 /* One for the pending_chunks list reference */
4777 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004778error:
Miao Xieb2117a32011-01-05 10:07:28 +00004779 kfree(devices_info);
4780 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004781}
4782
Josef Bacik6df9a952013-06-27 13:22:46 -04004783int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004784 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004785 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004786{
Yan Zheng2b820322008-11-17 21:11:30 -05004787 struct btrfs_key key;
4788 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4789 struct btrfs_device *device;
4790 struct btrfs_chunk *chunk;
4791 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004792 struct extent_map_tree *em_tree;
4793 struct extent_map *em;
4794 struct map_lookup *map;
4795 size_t item_size;
4796 u64 dev_offset;
4797 u64 stripe_size;
4798 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004799 int ret;
4800
Josef Bacik6df9a952013-06-27 13:22:46 -04004801 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4802 read_lock(&em_tree->lock);
4803 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4804 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004805
Josef Bacik6df9a952013-06-27 13:22:46 -04004806 if (!em) {
4807 btrfs_crit(extent_root->fs_info, "unable to find logical "
4808 "%Lu len %Lu", chunk_offset, chunk_size);
4809 return -EINVAL;
4810 }
4811
4812 if (em->start != chunk_offset || em->len != chunk_size) {
4813 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004814 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004815 chunk_size, em->start, em->len);
4816 free_extent_map(em);
4817 return -EINVAL;
4818 }
4819
4820 map = (struct map_lookup *)em->bdev;
4821 item_size = btrfs_chunk_item_size(map->num_stripes);
4822 stripe_size = em->orig_block_len;
4823
4824 chunk = kzalloc(item_size, GFP_NOFS);
4825 if (!chunk) {
4826 ret = -ENOMEM;
4827 goto out;
4828 }
4829
4830 for (i = 0; i < map->num_stripes; i++) {
4831 device = map->stripes[i].dev;
4832 dev_offset = map->stripes[i].physical;
4833
Yan Zheng2b820322008-11-17 21:11:30 -05004834 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004835 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004836 goto out;
4837 ret = btrfs_alloc_dev_extent(trans, device,
4838 chunk_root->root_key.objectid,
4839 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4840 chunk_offset, dev_offset,
4841 stripe_size);
4842 if (ret)
4843 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004844 }
4845
Yan Zheng2b820322008-11-17 21:11:30 -05004846 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004847 for (i = 0; i < map->num_stripes; i++) {
4848 device = map->stripes[i].dev;
4849 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004850
4851 btrfs_set_stack_stripe_devid(stripe, device->devid);
4852 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4853 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4854 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004855 }
4856
4857 btrfs_set_stack_chunk_length(chunk, chunk_size);
4858 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4859 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4860 btrfs_set_stack_chunk_type(chunk, map->type);
4861 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4862 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4863 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4864 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4865 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4866
4867 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4868 key.type = BTRFS_CHUNK_ITEM_KEY;
4869 key.offset = chunk_offset;
4870
4871 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004872 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4873 /*
4874 * TODO: Cleanup of inserted chunk root in case of
4875 * failure.
4876 */
Li Zefan125ccb02011-12-08 15:07:24 +08004877 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004878 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004879 }
liubo1abe9b82011-03-24 11:18:59 +00004880
Josef Bacik6df9a952013-06-27 13:22:46 -04004881out:
Yan Zheng2b820322008-11-17 21:11:30 -05004882 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004883 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004884 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004885}
4886
4887/*
4888 * Chunk allocation falls into two parts. The first part does works
4889 * that make the new allocated chunk useable, but not do any operation
4890 * that modifies the chunk tree. The second part does the works that
4891 * require modifying the chunk tree. This division is important for the
4892 * bootstrap process of adding storage to a seed btrfs.
4893 */
4894int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4895 struct btrfs_root *extent_root, u64 type)
4896{
4897 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004898
Filipe Mananaa9629592015-05-18 19:11:40 +01004899 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004900 chunk_offset = find_next_chunk(extent_root->fs_info);
4901 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004902}
4903
Chris Masond3977122009-01-05 21:25:51 -05004904static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004905 struct btrfs_root *root,
4906 struct btrfs_device *device)
4907{
4908 u64 chunk_offset;
4909 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004910 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004911 struct btrfs_fs_info *fs_info = root->fs_info;
4912 struct btrfs_root *extent_root = fs_info->extent_root;
4913 int ret;
4914
Josef Bacik6df9a952013-06-27 13:22:46 -04004915 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004916 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004917 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4918 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004919 if (ret)
4920 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004921
Josef Bacik6df9a952013-06-27 13:22:46 -04004922 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004923 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004924 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4925 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004926 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004927}
4928
Miao Xied20983b2014-07-03 18:22:13 +08004929static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4930{
4931 int max_errors;
4932
4933 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4934 BTRFS_BLOCK_GROUP_RAID10 |
4935 BTRFS_BLOCK_GROUP_RAID5 |
4936 BTRFS_BLOCK_GROUP_DUP)) {
4937 max_errors = 1;
4938 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4939 max_errors = 2;
4940 } else {
4941 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004942 }
4943
Miao Xied20983b2014-07-03 18:22:13 +08004944 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004945}
4946
4947int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4948{
4949 struct extent_map *em;
4950 struct map_lookup *map;
4951 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4952 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004953 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004954 int i;
4955
Chris Mason890871b2009-09-02 16:24:52 -04004956 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004957 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004958 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004959 if (!em)
4960 return 1;
4961
4962 map = (struct map_lookup *)em->bdev;
4963 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004964 if (map->stripes[i].dev->missing) {
4965 miss_ndevs++;
4966 continue;
4967 }
4968
Yan Zheng2b820322008-11-17 21:11:30 -05004969 if (!map->stripes[i].dev->writeable) {
4970 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004971 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004972 }
4973 }
Miao Xied20983b2014-07-03 18:22:13 +08004974
4975 /*
4976 * If the number of missing devices is larger than max errors,
4977 * we can not write the data into that chunk successfully, so
4978 * set it readonly.
4979 */
4980 if (miss_ndevs > btrfs_chunk_max_errors(map))
4981 readonly = 1;
4982end:
Yan Zheng2b820322008-11-17 21:11:30 -05004983 free_extent_map(em);
4984 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004985}
4986
4987void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4988{
David Sterbaa8067e02011-04-21 00:34:43 +02004989 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004990}
4991
4992void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4993{
4994 struct extent_map *em;
4995
Chris Masond3977122009-01-05 21:25:51 -05004996 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004997 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004998 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4999 if (em)
5000 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005001 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005002 if (!em)
5003 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005004 /* once for us */
5005 free_extent_map(em);
5006 /* once for the tree */
5007 free_extent_map(em);
5008 }
5009}
5010
Stefan Behrens5d964052012-11-05 14:59:07 +01005011int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04005012{
Stefan Behrens5d964052012-11-05 14:59:07 +01005013 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04005014 struct extent_map *em;
5015 struct map_lookup *map;
5016 struct extent_map_tree *em_tree = &map_tree->map_tree;
5017 int ret;
5018
Chris Mason890871b2009-09-02 16:24:52 -04005019 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005020 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04005021 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04005022
Josef Bacikfb7669b2013-04-23 10:53:18 -04005023 /*
5024 * We could return errors for these cases, but that could get ugly and
5025 * we'd probably do the same thing which is just not do anything else
5026 * and exit, so return 1 so the callers don't try to use other copies.
5027 */
5028 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02005029 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005030 logical+len);
5031 return 1;
5032 }
5033
5034 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02005035 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02005036 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04005037 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005038 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04005039 return 1;
5040 }
5041
Chris Masonf1885912008-04-09 16:28:12 -04005042 map = (struct map_lookup *)em->bdev;
5043 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5044 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005045 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5046 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005047 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
5048 ret = 2;
5049 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5050 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04005051 else
5052 ret = 1;
5053 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005054
5055 btrfs_dev_replace_lock(&fs_info->dev_replace);
5056 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
5057 ret++;
5058 btrfs_dev_replace_unlock(&fs_info->dev_replace);
5059
Chris Masonf1885912008-04-09 16:28:12 -04005060 return ret;
5061}
5062
David Woodhouse53b381b2013-01-29 18:40:14 -05005063unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
5064 struct btrfs_mapping_tree *map_tree,
5065 u64 logical)
5066{
5067 struct extent_map *em;
5068 struct map_lookup *map;
5069 struct extent_map_tree *em_tree = &map_tree->map_tree;
5070 unsigned long len = root->sectorsize;
5071
5072 read_lock(&em_tree->lock);
5073 em = lookup_extent_mapping(em_tree, logical, len);
5074 read_unlock(&em_tree->lock);
5075 BUG_ON(!em);
5076
5077 BUG_ON(em->start > logical || em->start + em->len < logical);
5078 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08005079 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005080 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05005081 free_extent_map(em);
5082 return len;
5083}
5084
5085int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
5086 u64 logical, u64 len, int mirror_num)
5087{
5088 struct extent_map *em;
5089 struct map_lookup *map;
5090 struct extent_map_tree *em_tree = &map_tree->map_tree;
5091 int ret = 0;
5092
5093 read_lock(&em_tree->lock);
5094 em = lookup_extent_mapping(em_tree, logical, len);
5095 read_unlock(&em_tree->lock);
5096 BUG_ON(!em);
5097
5098 BUG_ON(em->start > logical || em->start + em->len < logical);
5099 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08005100 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05005101 ret = 1;
5102 free_extent_map(em);
5103 return ret;
5104}
5105
Stefan Behrens30d98612012-11-06 14:52:18 +01005106static int find_live_mirror(struct btrfs_fs_info *fs_info,
5107 struct map_lookup *map, int first, int num,
5108 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04005109{
5110 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01005111 int tolerance;
5112 struct btrfs_device *srcdev;
5113
5114 if (dev_replace_is_ongoing &&
5115 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
5116 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
5117 srcdev = fs_info->dev_replace.srcdev;
5118 else
5119 srcdev = NULL;
5120
5121 /*
5122 * try to avoid the drive that is the source drive for a
5123 * dev-replace procedure, only choose it if no other non-missing
5124 * mirror is available
5125 */
5126 for (tolerance = 0; tolerance < 2; tolerance++) {
5127 if (map->stripes[optimal].dev->bdev &&
5128 (tolerance || map->stripes[optimal].dev != srcdev))
5129 return optimal;
5130 for (i = first; i < first + num; i++) {
5131 if (map->stripes[i].dev->bdev &&
5132 (tolerance || map->stripes[i].dev != srcdev))
5133 return i;
5134 }
Chris Masondfe25022008-05-13 13:46:40 -04005135 }
Stefan Behrens30d98612012-11-06 14:52:18 +01005136
Chris Masondfe25022008-05-13 13:46:40 -04005137 /* we couldn't find one that doesn't fail. Just return something
5138 * and the io error handling code will clean up eventually
5139 */
5140 return optimal;
5141}
5142
David Woodhouse53b381b2013-01-29 18:40:14 -05005143static inline int parity_smaller(u64 a, u64 b)
5144{
5145 return a > b;
5146}
5147
5148/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005149static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05005150{
5151 struct btrfs_bio_stripe s;
5152 int i;
5153 u64 l;
5154 int again = 1;
5155
5156 while (again) {
5157 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08005158 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005159 if (parity_smaller(bbio->raid_map[i],
5160 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005161 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005162 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05005163 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005164 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05005165 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005166 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005167
David Woodhouse53b381b2013-01-29 18:40:14 -05005168 again = 1;
5169 }
5170 }
5171 }
5172}
5173
Zhao Lei6e9606d2015-01-20 15:11:34 +08005174static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
5175{
5176 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08005177 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005178 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08005179 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005180 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005181 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08005182 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08005183 /*
5184 * plus the raid_map, which includes both the tgt dev
5185 * and the stripes
5186 */
5187 sizeof(u64) * (total_stripes),
Michal Hocko277fb5f2015-08-19 14:17:41 +02005188 GFP_NOFS|__GFP_NOFAIL);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005189
5190 atomic_set(&bbio->error, 0);
5191 atomic_set(&bbio->refs, 1);
5192
5193 return bbio;
5194}
5195
5196void btrfs_get_bbio(struct btrfs_bio *bbio)
5197{
5198 WARN_ON(!atomic_read(&bbio->refs));
5199 atomic_inc(&bbio->refs);
5200}
5201
5202void btrfs_put_bbio(struct btrfs_bio *bbio)
5203{
5204 if (!bbio)
5205 return;
5206 if (atomic_dec_and_test(&bbio->refs))
5207 kfree(bbio);
5208}
5209
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005210static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005211 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005212 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005213 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04005214{
5215 struct extent_map *em;
5216 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005217 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04005218 struct extent_map_tree *em_tree = &map_tree->map_tree;
5219 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04005220 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005221 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04005222 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005223 u64 stripe_nr_orig;
5224 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05005225 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01005226 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04005227 int i;
Li Zefande11cc12011-12-01 12:55:47 +08005228 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04005229 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04005230 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005231 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005232 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01005233 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
5234 int dev_replace_is_ongoing = 0;
5235 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005236 int patch_the_first_stripe_for_dev_replace = 0;
5237 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05005238 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04005239
Chris Mason890871b2009-09-02 16:24:52 -04005240 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005241 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04005242 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04005243
Chris Mason3b951512008-04-17 11:29:12 -04005244 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005245 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005246 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04005247 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04005248 }
Chris Mason0b86a832008-03-24 15:01:56 -04005249
Josef Bacik9bb91872013-04-19 23:45:33 -04005250 if (em->start > logical || em->start + em->len < logical) {
5251 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02005252 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04005253 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08005254 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04005255 return -EINVAL;
5256 }
5257
Chris Mason0b86a832008-03-24 15:01:56 -04005258 map = (struct map_lookup *)em->bdev;
5259 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04005260
David Woodhouse53b381b2013-01-29 18:40:14 -05005261 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005262 stripe_nr = offset;
5263 /*
5264 * stripe_nr counts the total number of stripes we have to stride
5265 * to get to this block
5266 */
David Sterba47c57132015-02-20 18:43:47 +01005267 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005268
David Woodhouse53b381b2013-01-29 18:40:14 -05005269 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005270 BUG_ON(offset < stripe_offset);
5271
5272 /* stripe_offset is the offset of this block in its stripe*/
5273 stripe_offset = offset - stripe_offset;
5274
David Woodhouse53b381b2013-01-29 18:40:14 -05005275 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005276 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005277 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5278 raid56_full_stripe_start = offset;
5279
5280 /* allow a write of a full stripe, but make sure we don't
5281 * allow straddling of stripes
5282 */
David Sterba47c57132015-02-20 18:43:47 +01005283 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5284 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005285 raid56_full_stripe_start *= full_stripe_len;
5286 }
5287
5288 if (rw & REQ_DISCARD) {
5289 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005290 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005291 ret = -EOPNOTSUPP;
5292 goto out;
5293 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005294 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005295 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5296 u64 max_len;
5297 /* For writes to RAID[56], allow a full stripeset across all disks.
5298 For other RAID types and for RAID[56] reads, just allow a single
5299 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005300 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005301 (rw & REQ_WRITE)) {
5302 max_len = stripe_len * nr_data_stripes(map) -
5303 (offset - raid56_full_stripe_start);
5304 } else {
5305 /* we limit the length of each bio to what fits in a stripe */
5306 max_len = stripe_len - stripe_offset;
5307 }
5308 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005309 } else {
5310 *length = em->len - offset;
5311 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005312
David Woodhouse53b381b2013-01-29 18:40:14 -05005313 /* This is for when we're called from btrfs_merge_bio_hook() and all
5314 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005315 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005316 goto out;
5317
Stefan Behrens472262f2012-11-06 14:43:46 +01005318 btrfs_dev_replace_lock(dev_replace);
5319 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5320 if (!dev_replace_is_ongoing)
5321 btrfs_dev_replace_unlock(dev_replace);
5322
Stefan Behrensad6d6202012-11-06 15:06:47 +01005323 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5324 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5325 dev_replace->tgtdev != NULL) {
5326 /*
5327 * in dev-replace case, for repair case (that's the only
5328 * case where the mirror is selected explicitly when
5329 * calling btrfs_map_block), blocks left of the left cursor
5330 * can also be read from the target drive.
5331 * For REQ_GET_READ_MIRRORS, the target drive is added as
5332 * the last one to the array of stripes. For READ, it also
5333 * needs to be supported using the same mirror number.
5334 * If the requested block is not left of the left cursor,
5335 * EIO is returned. This can happen because btrfs_num_copies()
5336 * returns one more in the dev-replace case.
5337 */
5338 u64 tmp_length = *length;
5339 struct btrfs_bio *tmp_bbio = NULL;
5340 int tmp_num_stripes;
5341 u64 srcdev_devid = dev_replace->srcdev->devid;
5342 int index_srcdev = 0;
5343 int found = 0;
5344 u64 physical_of_found = 0;
5345
5346 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005347 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005348 if (ret) {
5349 WARN_ON(tmp_bbio != NULL);
5350 goto out;
5351 }
5352
5353 tmp_num_stripes = tmp_bbio->num_stripes;
5354 if (mirror_num > tmp_num_stripes) {
5355 /*
5356 * REQ_GET_READ_MIRRORS does not contain this
5357 * mirror, that means that the requested area
5358 * is not left of the left cursor
5359 */
5360 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005361 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005362 goto out;
5363 }
5364
5365 /*
5366 * process the rest of the function using the mirror_num
5367 * of the source drive. Therefore look it up first.
5368 * At the end, patch the device pointer to the one of the
5369 * target drive.
5370 */
5371 for (i = 0; i < tmp_num_stripes; i++) {
5372 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5373 /*
5374 * In case of DUP, in order to keep it
5375 * simple, only add the mirror with the
5376 * lowest physical address
5377 */
5378 if (found &&
5379 physical_of_found <=
5380 tmp_bbio->stripes[i].physical)
5381 continue;
5382 index_srcdev = i;
5383 found = 1;
5384 physical_of_found =
5385 tmp_bbio->stripes[i].physical;
5386 }
5387 }
5388
5389 if (found) {
5390 mirror_num = index_srcdev + 1;
5391 patch_the_first_stripe_for_dev_replace = 1;
5392 physical_to_patch_in_first_stripe = physical_of_found;
5393 } else {
5394 WARN_ON(1);
5395 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005396 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005397 goto out;
5398 }
5399
Zhao Lei6e9606d2015-01-20 15:11:34 +08005400 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005401 } else if (mirror_num > map->num_stripes) {
5402 mirror_num = 0;
5403 }
5404
Chris Masonf2d8d742008-04-21 10:03:05 -04005405 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005406 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005407 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005408 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005409 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005410 stripe_end_offset = stripe_nr_end * map->stripe_len -
5411 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005412
Li Dongyangfce3bb92011-03-24 10:24:26 +00005413 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5414 if (rw & REQ_DISCARD)
5415 num_stripes = min_t(u64, map->num_stripes,
5416 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005417 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5418 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005419 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5420 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005421 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005422 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005423 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005424 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005425 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005426 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005427 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005428 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005429 current->pid % map->num_stripes,
5430 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005431 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005432 }
Chris Mason2fff7342008-04-29 14:12:09 -04005433
Chris Mason611f0e02008-04-03 16:29:03 -04005434 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005435 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005436 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005437 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005438 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005439 } else {
5440 mirror_num = 1;
5441 }
Chris Mason2fff7342008-04-29 14:12:09 -04005442
Chris Mason321aecc2008-04-16 10:49:51 -04005443 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005444 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005445
David Sterba47c57132015-02-20 18:43:47 +01005446 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005447 stripe_index *= map->sub_stripes;
5448
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005449 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005450 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005451 else if (rw & REQ_DISCARD)
5452 num_stripes = min_t(u64, map->sub_stripes *
5453 (stripe_nr_end - stripe_nr_orig),
5454 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005455 else if (mirror_num)
5456 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005457 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005458 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005459 stripe_index = find_live_mirror(fs_info, map,
5460 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005461 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005462 current->pid % map->sub_stripes,
5463 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005464 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005465 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005466
Zhao Leiffe2d202015-01-20 15:11:44 +08005467 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005468 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005469 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5470 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005471 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005472 stripe_nr = div_u64(raid56_full_stripe_start,
5473 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005474
5475 /* RAID[56] write or recovery. Return all stripes */
5476 num_stripes = map->num_stripes;
5477 max_errors = nr_parity_stripes(map);
5478
David Woodhouse53b381b2013-01-29 18:40:14 -05005479 *length = map->stripe_len;
5480 stripe_index = 0;
5481 stripe_offset = 0;
5482 } else {
5483 /*
5484 * Mirror #0 or #1 means the original data block.
5485 * Mirror #2 is RAID5 parity block.
5486 * Mirror #3 is RAID6 Q block.
5487 */
David Sterba47c57132015-02-20 18:43:47 +01005488 stripe_nr = div_u64_rem(stripe_nr,
5489 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005490 if (mirror_num > 1)
5491 stripe_index = nr_data_stripes(map) +
5492 mirror_num - 2;
5493
5494 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005495 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5496 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005497 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5498 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5499 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005500 }
Chris Mason8790d502008-04-03 16:29:03 -04005501 } else {
5502 /*
David Sterba47c57132015-02-20 18:43:47 +01005503 * after this, stripe_nr is the number of stripes on this
5504 * device we have to walk to find the data, and stripe_index is
5505 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005506 */
David Sterba47c57132015-02-20 18:43:47 +01005507 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5508 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005509 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005510 }
Chris Mason593060d2008-03-25 16:50:33 -04005511 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005512
Stefan Behrens472262f2012-11-06 14:43:46 +01005513 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005514 if (dev_replace_is_ongoing) {
5515 if (rw & (REQ_WRITE | REQ_DISCARD))
5516 num_alloc_stripes <<= 1;
5517 if (rw & REQ_GET_READ_MIRRORS)
5518 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005519 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005520 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005521
Zhao Lei6e9606d2015-01-20 15:11:34 +08005522 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005523 if (!bbio) {
5524 ret = -ENOMEM;
5525 goto out;
5526 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005527 if (dev_replace_is_ongoing)
5528 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005529
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005530 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005531 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005532 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5533 mirror_num > 1)) {
5534 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005535 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005536
5537 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5538 sizeof(struct btrfs_bio_stripe) *
5539 num_alloc_stripes +
5540 sizeof(int) * tgtdev_indexes);
5541
5542 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005543 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005544
5545 /* Fill in the logical address of each stripe */
5546 tmp = stripe_nr * nr_data_stripes(map);
5547 for (i = 0; i < nr_data_stripes(map); i++)
5548 bbio->raid_map[(i+rot) % num_stripes] =
5549 em->start + (tmp + i) * map->stripe_len;
5550
5551 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5552 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5553 bbio->raid_map[(i+rot+1) % num_stripes] =
5554 RAID6_Q_STRIPE;
5555 }
5556
Li Dongyangfce3bb92011-03-24 10:24:26 +00005557 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005558 u32 factor = 0;
5559 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005560 u64 stripes_per_dev = 0;
5561 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005562 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005563
5564 if (map->type &
5565 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5566 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5567 sub_stripes = 1;
5568 else
5569 sub_stripes = map->sub_stripes;
5570
5571 factor = map->num_stripes / sub_stripes;
5572 stripes_per_dev = div_u64_rem(stripe_nr_end -
5573 stripe_nr_orig,
5574 factor,
5575 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005576 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5577 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005578 }
5579
Li Dongyangfce3bb92011-03-24 10:24:26 +00005580 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005581 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005582 map->stripes[stripe_index].physical +
5583 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005584 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005585
Li Zefanec9ef7a2011-12-01 14:06:42 +08005586 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5587 BTRFS_BLOCK_GROUP_RAID10)) {
5588 bbio->stripes[i].length = stripes_per_dev *
5589 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005590
Li Zefanec9ef7a2011-12-01 14:06:42 +08005591 if (i / sub_stripes < remaining_stripes)
5592 bbio->stripes[i].length +=
5593 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005594
5595 /*
5596 * Special for the first stripe and
5597 * the last stripe:
5598 *
5599 * |-------|...|-------|
5600 * |----------|
5601 * off end_off
5602 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005603 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005604 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005605 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005606
5607 if (stripe_index >= last_stripe &&
5608 stripe_index <= (last_stripe +
5609 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005610 bbio->stripes[i].length -=
5611 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005612
Li Zefanec9ef7a2011-12-01 14:06:42 +08005613 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005614 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005615 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005616 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005617
5618 stripe_index++;
5619 if (stripe_index == map->num_stripes) {
5620 /* This could only happen for RAID0/10 */
5621 stripe_index = 0;
5622 stripe_nr++;
5623 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005624 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005625 } else {
5626 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005627 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005628 map->stripes[stripe_index].physical +
5629 stripe_offset +
5630 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005631 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005632 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005633 stripe_index++;
5634 }
Chris Mason593060d2008-03-25 16:50:33 -04005635 }
Li Zefande11cc12011-12-01 12:55:47 +08005636
Miao Xied20983b2014-07-03 18:22:13 +08005637 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5638 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005639
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005640 if (bbio->raid_map)
5641 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005642
Miao Xie2c8cdd62014-11-14 16:06:25 +08005643 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005644 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5645 dev_replace->tgtdev != NULL) {
5646 int index_where_to_add;
5647 u64 srcdev_devid = dev_replace->srcdev->devid;
5648
5649 /*
5650 * duplicate the write operations while the dev replace
5651 * procedure is running. Since the copying of the old disk
5652 * to the new disk takes place at run time while the
5653 * filesystem is mounted writable, the regular write
5654 * operations to the old disk have to be duplicated to go
5655 * to the new disk as well.
5656 * Note that device->missing is handled by the caller, and
5657 * that the write to the old disk is already set up in the
5658 * stripes array.
5659 */
5660 index_where_to_add = num_stripes;
5661 for (i = 0; i < num_stripes; i++) {
5662 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5663 /* write to new disk, too */
5664 struct btrfs_bio_stripe *new =
5665 bbio->stripes + index_where_to_add;
5666 struct btrfs_bio_stripe *old =
5667 bbio->stripes + i;
5668
5669 new->physical = old->physical;
5670 new->length = old->length;
5671 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005672 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005673 index_where_to_add++;
5674 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005675 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005676 }
5677 }
5678 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005679 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5680 dev_replace->tgtdev != NULL) {
5681 u64 srcdev_devid = dev_replace->srcdev->devid;
5682 int index_srcdev = 0;
5683 int found = 0;
5684 u64 physical_of_found = 0;
5685
5686 /*
5687 * During the dev-replace procedure, the target drive can
5688 * also be used to read data in case it is needed to repair
5689 * a corrupt block elsewhere. This is possible if the
5690 * requested area is left of the left cursor. In this area,
5691 * the target drive is a full copy of the source drive.
5692 */
5693 for (i = 0; i < num_stripes; i++) {
5694 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5695 /*
5696 * In case of DUP, in order to keep it
5697 * simple, only add the mirror with the
5698 * lowest physical address
5699 */
5700 if (found &&
5701 physical_of_found <=
5702 bbio->stripes[i].physical)
5703 continue;
5704 index_srcdev = i;
5705 found = 1;
5706 physical_of_found = bbio->stripes[i].physical;
5707 }
5708 }
5709 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005710 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005711 dev_replace->cursor_left) {
5712 struct btrfs_bio_stripe *tgtdev_stripe =
5713 bbio->stripes + num_stripes;
5714
5715 tgtdev_stripe->physical = physical_of_found;
5716 tgtdev_stripe->length =
5717 bbio->stripes[index_srcdev].length;
5718 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005719 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005720
Miao Xie2c8cdd62014-11-14 16:06:25 +08005721 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005722 num_stripes++;
5723 }
5724 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005725 }
5726
Li Zefande11cc12011-12-01 12:55:47 +08005727 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005728 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005729 bbio->num_stripes = num_stripes;
5730 bbio->max_errors = max_errors;
5731 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005732 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005733
5734 /*
5735 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5736 * mirror_num == num_stripes + 1 && dev_replace target drive is
5737 * available as a mirror
5738 */
5739 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5740 WARN_ON(num_stripes > 1);
5741 bbio->stripes[0].dev = dev_replace->tgtdev;
5742 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5743 bbio->mirror_num = map->num_stripes + 1;
5744 }
Chris Masoncea9e442008-04-09 16:28:12 -04005745out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005746 if (dev_replace_is_ongoing)
5747 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005748 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005749 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005750}
5751
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005752int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005753 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005754 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005755{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005756 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005757 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005758}
5759
Miao Xieaf8e2d12014-10-23 14:42:50 +08005760/* For Scrub/replace */
5761int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5762 u64 logical, u64 *length,
5763 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005764 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005765{
5766 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005767 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005768}
5769
Yan Zhenga512bbf2008-12-08 16:46:26 -05005770int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5771 u64 chunk_start, u64 physical, u64 devid,
5772 u64 **logical, int *naddrs, int *stripe_len)
5773{
5774 struct extent_map_tree *em_tree = &map_tree->map_tree;
5775 struct extent_map *em;
5776 struct map_lookup *map;
5777 u64 *buf;
5778 u64 bytenr;
5779 u64 length;
5780 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005781 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005782 int i, j, nr = 0;
5783
Chris Mason890871b2009-09-02 16:24:52 -04005784 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005785 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005786 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005787
Josef Bacik835d9742013-03-19 12:13:25 -04005788 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005789 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005790 chunk_start);
5791 return -EIO;
5792 }
5793
5794 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005795 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005796 em->start, chunk_start);
5797 free_extent_map(em);
5798 return -EIO;
5799 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005800 map = (struct map_lookup *)em->bdev;
5801
5802 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005803 rmap_len = map->stripe_len;
5804
Yan Zhenga512bbf2008-12-08 16:46:26 -05005805 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005806 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005807 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005808 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005809 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005810 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005811 rmap_len = map->stripe_len * nr_data_stripes(map);
5812 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005813
David Sterba31e818f2015-02-20 18:00:26 +01005814 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005815 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005816
5817 for (i = 0; i < map->num_stripes; i++) {
5818 if (devid && map->stripes[i].dev->devid != devid)
5819 continue;
5820 if (map->stripes[i].physical > physical ||
5821 map->stripes[i].physical + length <= physical)
5822 continue;
5823
5824 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005825 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005826
5827 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5828 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005829 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005830 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5831 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005832 } /* else if RAID[56], multiply by nr_data_stripes().
5833 * Alternatively, just use rmap_len below instead of
5834 * map->stripe_len */
5835
5836 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005837 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005838 for (j = 0; j < nr; j++) {
5839 if (buf[j] == bytenr)
5840 break;
5841 }
Chris Mason934d3752008-12-08 16:43:10 -05005842 if (j == nr) {
5843 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005844 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005845 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005846 }
5847
Yan Zhenga512bbf2008-12-08 16:46:26 -05005848 *logical = buf;
5849 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005850 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005851
5852 free_extent_map(em);
5853 return 0;
5854}
5855
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005856static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio)
Miao Xie8408c712014-06-19 10:42:55 +08005857{
Mike Snitzer326e1db2015-05-22 09:14:03 -04005858 bio->bi_private = bbio->private;
5859 bio->bi_end_io = bbio->end_io;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005860 bio_endio(bio);
Mike Snitzer326e1db2015-05-22 09:14:03 -04005861
Zhao Lei6e9606d2015-01-20 15:11:34 +08005862 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005863}
5864
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005865static void btrfs_end_bio(struct bio *bio)
Chris Mason8790d502008-04-03 16:29:03 -04005866{
Chris Mason9be33952013-05-17 18:30:14 -04005867 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005868 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005869
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005870 if (bio->bi_error) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005871 atomic_inc(&bbio->error);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005872 if (bio->bi_error == -EIO || bio->bi_error == -EREMOTEIO) {
Stefan Behrens442a4f62012-05-25 16:06:08 +02005873 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005874 btrfs_io_bio(bio)->stripe_index;
Zhao Lei65f53332015-06-08 20:05:50 +08005875 struct btrfs_device *dev;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005876
5877 BUG_ON(stripe_index >= bbio->num_stripes);
5878 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005879 if (dev->bdev) {
5880 if (bio->bi_rw & WRITE)
5881 btrfs_dev_stat_inc(dev,
5882 BTRFS_DEV_STAT_WRITE_ERRS);
5883 else
5884 btrfs_dev_stat_inc(dev,
5885 BTRFS_DEV_STAT_READ_ERRS);
5886 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5887 btrfs_dev_stat_inc(dev,
5888 BTRFS_DEV_STAT_FLUSH_ERRS);
5889 btrfs_dev_stat_print_on_error(dev);
5890 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005891 }
5892 }
Chris Mason8790d502008-04-03 16:29:03 -04005893
Jan Schmidta1d3c472011-08-04 17:15:33 +02005894 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005895 is_orig_bio = 1;
5896
Miao Xiec404e0d2014-01-30 16:46:55 +08005897 btrfs_bio_counter_dec(bbio->fs_info);
5898
Jan Schmidta1d3c472011-08-04 17:15:33 +02005899 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005900 if (!is_orig_bio) {
5901 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005902 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005903 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005904
Chris Mason9be33952013-05-17 18:30:14 -04005905 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005906 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005907 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005908 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005909 if (atomic_read(&bbio->error) > bbio->max_errors) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005910 bio->bi_error = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005911 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005912 /*
5913 * this bio is actually up to date, we didn't
5914 * go over the max number of errors
5915 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005916 bio->bi_error = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005917 }
Miao Xiec55f1392014-06-19 10:42:54 +08005918
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005919 btrfs_end_bbio(bbio, bio);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005920 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005921 bio_put(bio);
5922 }
Chris Mason8790d502008-04-03 16:29:03 -04005923}
5924
Chris Mason8b712842008-06-11 16:50:36 -04005925/*
5926 * see run_scheduled_bios for a description of why bios are collected for
5927 * async submit.
5928 *
5929 * This will add one bio to the pending list for a device and make sure
5930 * the work struct is scheduled.
5931 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005932static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5933 struct btrfs_device *device,
5934 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005935{
5936 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005937 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005938
David Woodhouse53b381b2013-01-29 18:40:14 -05005939 if (device->missing || !device->bdev) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005940 bio_io_error(bio);
David Woodhouse53b381b2013-01-29 18:40:14 -05005941 return;
5942 }
5943
Chris Mason8b712842008-06-11 16:50:36 -04005944 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005945 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005946 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005947 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005948 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005949 return;
Chris Mason8b712842008-06-11 16:50:36 -04005950 }
5951
5952 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005953 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005954 * higher layers. Otherwise, the async bio makes it appear we have
5955 * made progress against dirty pages when we've really just put it
5956 * on a queue for later
5957 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005958 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005959 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005960 bio->bi_next = NULL;
5961 bio->bi_rw |= rw;
5962
5963 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005964 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005965 pending_bios = &device->pending_sync_bios;
5966 else
5967 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005968
Chris Masonffbd5172009-04-20 15:50:09 -04005969 if (pending_bios->tail)
5970 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005971
Chris Masonffbd5172009-04-20 15:50:09 -04005972 pending_bios->tail = bio;
5973 if (!pending_bios->head)
5974 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005975 if (device->running_pending)
5976 should_queue = 0;
5977
5978 spin_unlock(&device->io_lock);
5979
5980 if (should_queue)
Qu Wenruoa8c93d4e2014-02-28 10:46:08 +08005981 btrfs_queue_work(root->fs_info->submit_workers,
5982 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005983}
5984
Josef Bacikde1ee922012-10-19 16:50:56 -04005985static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5986 struct bio *bio, u64 physical, int dev_nr,
5987 int rw, int async)
5988{
5989 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5990
5991 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005992 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005993 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005994 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005995#ifdef DEBUG
5996 {
5997 struct rcu_string *name;
5998
5999 rcu_read_lock();
6000 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00006001 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04006002 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02006003 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
6004 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04006005 rcu_read_unlock();
6006 }
6007#endif
6008 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08006009
6010 btrfs_bio_counter_inc_noblocked(root->fs_info);
6011
Josef Bacikde1ee922012-10-19 16:50:56 -04006012 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05006013 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006014 else
6015 btrfsic_submit_bio(rw, bio);
6016}
6017
Josef Bacikde1ee922012-10-19 16:50:56 -04006018static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6019{
6020 atomic_inc(&bbio->error);
6021 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08006022 /* Shoud be the original bio. */
6023 WARN_ON(bio != bbio->orig_bio);
6024
Chris Mason9be33952013-05-17 18:30:14 -04006025 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006026 bio->bi_iter.bi_sector = logical >> 9;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006027 bio->bi_error = -EIO;
6028 btrfs_end_bbio(bbio, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04006029 }
6030}
6031
Chris Masonf1885912008-04-09 16:28:12 -04006032int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04006033 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04006034{
Chris Mason0b86a832008-03-24 15:01:56 -04006035 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04006036 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07006037 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04006038 u64 length = 0;
6039 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04006040 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08006041 int dev_nr;
6042 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02006043 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006044
Kent Overstreet4f024f32013-10-11 15:44:27 -07006045 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04006046 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04006047
Miao Xiec404e0d2014-01-30 16:46:55 +08006048 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05006049 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006050 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08006051 if (ret) {
6052 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006053 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08006054 }
Chris Masoncea9e442008-04-09 16:28:12 -04006055
Jan Schmidta1d3c472011-08-04 17:15:33 +02006056 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05006057 bbio->orig_bio = first_bio;
6058 bbio->private = first_bio->bi_private;
6059 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08006060 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05006061 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6062
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006063 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05006064 /* In this case, map_length has been set to the length of
6065 a single stripe; not the whole write */
6066 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006067 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05006068 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08006069 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08006070 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05006071 }
Miao Xie42452152014-11-25 16:39:28 +08006072
Miao Xiec404e0d2014-01-30 16:46:55 +08006073 btrfs_bio_counter_dec(root->fs_info);
6074 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05006075 }
6076
Chris Masoncea9e442008-04-09 16:28:12 -04006077 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006078 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006079 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04006080 BUG();
6081 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02006082
Zhao Lei08da7572015-02-12 15:42:16 +08006083 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04006084 dev = bbio->stripes[dev_nr].dev;
6085 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
6086 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04006087 continue;
6088 }
6089
Jan Schmidta1d3c472011-08-04 17:15:33 +02006090 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04006091 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006092 BUG_ON(!bio); /* -ENOMEM */
Mike Snitzer326e1db2015-05-22 09:14:03 -04006093 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02006094 bio = first_bio;
Josef Bacik606686e2012-06-04 14:03:51 -04006095
Josef Bacikde1ee922012-10-19 16:50:56 -04006096 submit_stripe_bio(root, bbio, bio,
6097 bbio->stripes[dev_nr].physical, dev_nr, rw,
6098 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04006099 }
Miao Xiec404e0d2014-01-30 16:46:55 +08006100 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04006101 return 0;
6102}
6103
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006104struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05006105 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04006106{
Yan Zheng2b820322008-11-17 21:11:30 -05006107 struct btrfs_device *device;
6108 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006109
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006110 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006111 while (cur_devices) {
6112 if (!fsid ||
6113 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
6114 device = __find_device(&cur_devices->devices,
6115 devid, uuid);
6116 if (device)
6117 return device;
6118 }
6119 cur_devices = cur_devices->seed;
6120 }
6121 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04006122}
6123
Chris Masondfe25022008-05-13 13:46:40 -04006124static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08006125 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04006126 u64 devid, u8 *dev_uuid)
6127{
6128 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04006129
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006130 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
6131 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05006132 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006133
6134 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006135 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04006136 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006137
6138 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05006139 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006140
Chris Masondfe25022008-05-13 13:46:40 -04006141 return device;
6142}
6143
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006144/**
6145 * btrfs_alloc_device - allocate struct btrfs_device
6146 * @fs_info: used only for generating a new devid, can be NULL if
6147 * devid is provided (i.e. @devid != NULL).
6148 * @devid: a pointer to devid for this device. If NULL a new devid
6149 * is generated.
6150 * @uuid: a pointer to UUID for this device. If NULL a new UUID
6151 * is generated.
6152 *
6153 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
6154 * on error. Returned struct is not linked onto any lists and can be
6155 * destroyed with kfree() right away.
6156 */
6157struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6158 const u64 *devid,
6159 const u8 *uuid)
6160{
6161 struct btrfs_device *dev;
6162 u64 tmp;
6163
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05306164 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006165 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006166
6167 dev = __alloc_device();
6168 if (IS_ERR(dev))
6169 return dev;
6170
6171 if (devid)
6172 tmp = *devid;
6173 else {
6174 int ret;
6175
6176 ret = find_next_devid(fs_info, &tmp);
6177 if (ret) {
6178 kfree(dev);
6179 return ERR_PTR(ret);
6180 }
6181 }
6182 dev->devid = tmp;
6183
6184 if (uuid)
6185 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
6186 else
6187 generate_random_uuid(dev->uuid);
6188
Liu Bo9e0af232014-08-15 23:36:53 +08006189 btrfs_init_work(&dev->work, btrfs_submit_helper,
6190 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006191
6192 return dev;
6193}
6194
Chris Mason0b86a832008-03-24 15:01:56 -04006195static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6196 struct extent_buffer *leaf,
6197 struct btrfs_chunk *chunk)
6198{
6199 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6200 struct map_lookup *map;
6201 struct extent_map *em;
6202 u64 logical;
6203 u64 length;
6204 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006205 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006206 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006207 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006208 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006209
Chris Masone17cade2008-04-15 15:41:47 -04006210 logical = key->offset;
6211 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006212
Chris Mason890871b2009-09-02 16:24:52 -04006213 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006214 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006215 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006216
6217 /* already mapped? */
6218 if (em && em->start <= logical && em->start + em->len > logical) {
6219 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006220 return 0;
6221 } else if (em) {
6222 free_extent_map(em);
6223 }
Chris Mason0b86a832008-03-24 15:01:56 -04006224
David Sterba172ddd62011-04-21 00:48:27 +02006225 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006226 if (!em)
6227 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006228 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6229 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006230 if (!map) {
6231 free_extent_map(em);
6232 return -ENOMEM;
6233 }
6234
Wang Shilong298a8f92014-06-19 10:42:52 +08006235 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006236 em->bdev = (struct block_device *)map;
6237 em->start = logical;
6238 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006239 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006240 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006241 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006242
Chris Mason593060d2008-03-25 16:50:33 -04006243 map->num_stripes = num_stripes;
6244 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6245 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6246 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6247 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6248 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006249 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006250 for (i = 0; i < num_stripes; i++) {
6251 map->stripes[i].physical =
6252 btrfs_stripe_offset_nr(leaf, chunk, i);
6253 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006254 read_extent_buffer(leaf, uuid, (unsigned long)
6255 btrfs_stripe_dev_uuid_nr(chunk, i),
6256 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006257 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6258 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006259 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006260 free_extent_map(em);
6261 return -EIO;
6262 }
Chris Masondfe25022008-05-13 13:46:40 -04006263 if (!map->stripes[i].dev) {
6264 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006265 add_missing_dev(root, root->fs_info->fs_devices,
6266 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006267 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006268 free_extent_map(em);
6269 return -EIO;
6270 }
Anand Jain816fceb2015-04-27 12:46:18 +08006271 btrfs_warn(root->fs_info, "devid %llu uuid %pU is missing",
6272 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006273 }
6274 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006275 }
6276
Chris Mason890871b2009-09-02 16:24:52 -04006277 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006278 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006279 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006280 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006281 free_extent_map(em);
6282
6283 return 0;
6284}
6285
Jeff Mahoney143bede2012-03-01 14:56:26 +01006286static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006287 struct btrfs_dev_item *dev_item,
6288 struct btrfs_device *device)
6289{
6290 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006291
6292 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006293 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6294 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006295 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006296 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006297 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006298 device->type = btrfs_device_type(leaf, dev_item);
6299 device->io_align = btrfs_device_io_align(leaf, dev_item);
6300 device->io_width = btrfs_device_io_width(leaf, dev_item);
6301 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006302 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006303 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006304
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006305 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006306 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006307}
6308
Miao Xie5f375832014-09-03 21:35:46 +08006309static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6310 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006311{
6312 struct btrfs_fs_devices *fs_devices;
6313 int ret;
6314
Li Zefanb367e472011-12-07 11:38:24 +08006315 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006316
6317 fs_devices = root->fs_info->fs_devices->seed;
6318 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006319 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6320 return fs_devices;
6321
Yan Zheng2b820322008-11-17 21:11:30 -05006322 fs_devices = fs_devices->seed;
6323 }
6324
6325 fs_devices = find_fsid(fsid);
6326 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006327 if (!btrfs_test_opt(root, DEGRADED))
6328 return ERR_PTR(-ENOENT);
6329
6330 fs_devices = alloc_fs_devices(fsid);
6331 if (IS_ERR(fs_devices))
6332 return fs_devices;
6333
6334 fs_devices->seeding = 1;
6335 fs_devices->opened = 1;
6336 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006337 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006338
6339 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006340 if (IS_ERR(fs_devices))
6341 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006342
Christoph Hellwig97288f22008-12-02 06:36:09 -05006343 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006344 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006345 if (ret) {
6346 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006347 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006348 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006349 }
Yan Zheng2b820322008-11-17 21:11:30 -05006350
6351 if (!fs_devices->seeding) {
6352 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006353 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006354 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006355 goto out;
6356 }
6357
6358 fs_devices->seed = root->fs_info->fs_devices->seed;
6359 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006360out:
Miao Xie5f375832014-09-03 21:35:46 +08006361 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006362}
6363
Chris Mason0d81ba52008-03-24 15:02:07 -04006364static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006365 struct extent_buffer *leaf,
6366 struct btrfs_dev_item *dev_item)
6367{
Miao Xie5f375832014-09-03 21:35:46 +08006368 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006369 struct btrfs_device *device;
6370 u64 devid;
6371 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006372 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006373 u8 dev_uuid[BTRFS_UUID_SIZE];
6374
Chris Mason0b86a832008-03-24 15:01:56 -04006375 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006376 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006377 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006378 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006379 BTRFS_UUID_SIZE);
6380
6381 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006382 fs_devices = open_seed_devices(root, fs_uuid);
6383 if (IS_ERR(fs_devices))
6384 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006385 }
6386
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006387 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006388 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006389 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006390 return -EIO;
6391
Miao Xie5f375832014-09-03 21:35:46 +08006392 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6393 if (!device)
6394 return -ENOMEM;
Anand Jain33b97e42015-05-08 04:34:35 +08006395 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing",
6396 devid, dev_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006397 } else {
6398 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6399 return -EIO;
6400
6401 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006402 /*
6403 * this happens when a device that was properly setup
6404 * in the device info lists suddenly goes bad.
6405 * device->bdev is NULL, and so we have to set
6406 * device->missing to one here
6407 */
Miao Xie5f375832014-09-03 21:35:46 +08006408 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006409 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006410 }
Miao Xie5f375832014-09-03 21:35:46 +08006411
6412 /* Move the device to its own fs_devices */
6413 if (device->fs_devices != fs_devices) {
6414 ASSERT(device->missing);
6415
6416 list_move(&device->dev_list, &fs_devices->devices);
6417 device->fs_devices->num_devices--;
6418 fs_devices->num_devices++;
6419
6420 device->fs_devices->missing_devices--;
6421 fs_devices->missing_devices++;
6422
6423 device->fs_devices = fs_devices;
6424 }
Yan Zheng2b820322008-11-17 21:11:30 -05006425 }
6426
6427 if (device->fs_devices != root->fs_info->fs_devices) {
6428 BUG_ON(device->writeable);
6429 if (device->generation !=
6430 btrfs_device_generation(leaf, dev_item))
6431 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006432 }
Chris Mason0b86a832008-03-24 15:01:56 -04006433
6434 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006435 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006436 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006437 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006438 spin_lock(&root->fs_info->free_chunk_lock);
6439 root->fs_info->free_chunk_space += device->total_bytes -
6440 device->bytes_used;
6441 spin_unlock(&root->fs_info->free_chunk_lock);
6442 }
Chris Mason0b86a832008-03-24 15:01:56 -04006443 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006444 return ret;
6445}
6446
Yan Zhenge4404d62008-12-12 10:03:26 -05006447int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006448{
David Sterba6c417612011-04-13 15:41:04 +02006449 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006450 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006451 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006452 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006453 u8 *array_ptr;
6454 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006455 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006456 u32 num_stripes;
6457 u32 array_size;
6458 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006459 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006460 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006461
David Sterbaa83fffb2014-06-15 02:39:54 +02006462 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6463 /*
6464 * This will create extent buffer of nodesize, superblock size is
6465 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6466 * overallocate but we can keep it as-is, only the first page is used.
6467 */
6468 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006469 if (!sb)
6470 return -ENOMEM;
6471 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006472 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006473 /*
6474 * The sb extent buffer is artifical and just used to read the system array.
6475 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6476 * pages up-to-date when the page is larger: extent does not cover the
6477 * whole page and consequently check_page_uptodate does not find all
6478 * the page's extents up-to-date (the hole beyond sb),
6479 * write_extent_buffer then triggers a WARN_ON.
6480 *
6481 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6482 * but sb spans only this function. Add an explicit SetPageUptodate call
6483 * to silence the warning eg. on PowerPC 64.
6484 */
6485 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006486 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006487
Chris Masona061fc82008-05-07 11:43:44 -04006488 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006489 array_size = btrfs_super_sys_array_size(super_copy);
6490
David Sterba1ffb22c2014-10-31 19:02:42 +01006491 array_ptr = super_copy->sys_chunk_array;
6492 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6493 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006494
David Sterba1ffb22c2014-10-31 19:02:42 +01006495 while (cur_offset < array_size) {
6496 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006497 len = sizeof(*disk_key);
6498 if (cur_offset + len > array_size)
6499 goto out_short_read;
6500
Chris Mason0b86a832008-03-24 15:01:56 -04006501 btrfs_disk_key_to_cpu(&key, disk_key);
6502
David Sterba1ffb22c2014-10-31 19:02:42 +01006503 array_ptr += len;
6504 sb_array_offset += len;
6505 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006506
Chris Mason0d81ba52008-03-24 15:02:07 -04006507 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006508 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006509 /*
6510 * At least one btrfs_chunk with one stripe must be
6511 * present, exact stripe count check comes afterwards
6512 */
6513 len = btrfs_chunk_item_size(1);
6514 if (cur_offset + len > array_size)
6515 goto out_short_read;
6516
6517 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6518 len = btrfs_chunk_item_size(num_stripes);
6519 if (cur_offset + len > array_size)
6520 goto out_short_read;
6521
Chris Mason0d81ba52008-03-24 15:02:07 -04006522 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006523 if (ret)
6524 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006525 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006526 ret = -EIO;
6527 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006528 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006529 array_ptr += len;
6530 sb_array_offset += len;
6531 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006532 }
Chris Masona061fc82008-05-07 11:43:44 -04006533 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006534 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006535
6536out_short_read:
6537 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6538 len, cur_offset);
6539 free_extent_buffer(sb);
6540 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006541}
6542
6543int btrfs_read_chunk_tree(struct btrfs_root *root)
6544{
6545 struct btrfs_path *path;
6546 struct extent_buffer *leaf;
6547 struct btrfs_key key;
6548 struct btrfs_key found_key;
6549 int ret;
6550 int slot;
6551
6552 root = root->fs_info->chunk_root;
6553
6554 path = btrfs_alloc_path();
6555 if (!path)
6556 return -ENOMEM;
6557
Li Zefanb367e472011-12-07 11:38:24 +08006558 mutex_lock(&uuid_mutex);
6559 lock_chunks(root);
6560
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006561 /*
6562 * Read all device items, and then all the chunk items. All
6563 * device items are found before any chunk item (their object id
6564 * is smaller than the lowest possible object id for a chunk
6565 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006566 */
6567 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6568 key.offset = 0;
6569 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006570 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006571 if (ret < 0)
6572 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006573 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006574 leaf = path->nodes[0];
6575 slot = path->slots[0];
6576 if (slot >= btrfs_header_nritems(leaf)) {
6577 ret = btrfs_next_leaf(root, path);
6578 if (ret == 0)
6579 continue;
6580 if (ret < 0)
6581 goto error;
6582 break;
6583 }
6584 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006585 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6586 struct btrfs_dev_item *dev_item;
6587 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006588 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006589 ret = read_one_dev(root, leaf, dev_item);
6590 if (ret)
6591 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006592 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6593 struct btrfs_chunk *chunk;
6594 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6595 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006596 if (ret)
6597 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006598 }
6599 path->slots[0]++;
6600 }
Chris Mason0b86a832008-03-24 15:01:56 -04006601 ret = 0;
6602error:
Li Zefanb367e472011-12-07 11:38:24 +08006603 unlock_chunks(root);
6604 mutex_unlock(&uuid_mutex);
6605
Yan Zheng2b820322008-11-17 21:11:30 -05006606 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006607 return ret;
6608}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006609
Miao Xiecb517ea2013-05-15 07:48:19 +00006610void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6611{
6612 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6613 struct btrfs_device *device;
6614
Liu Bo29cc83f2014-05-11 23:14:59 +08006615 while (fs_devices) {
6616 mutex_lock(&fs_devices->device_list_mutex);
6617 list_for_each_entry(device, &fs_devices->devices, dev_list)
6618 device->dev_root = fs_info->dev_root;
6619 mutex_unlock(&fs_devices->device_list_mutex);
6620
6621 fs_devices = fs_devices->seed;
6622 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006623}
6624
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006625static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6626{
6627 int i;
6628
6629 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6630 btrfs_dev_stat_reset(dev, i);
6631}
6632
6633int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6634{
6635 struct btrfs_key key;
6636 struct btrfs_key found_key;
6637 struct btrfs_root *dev_root = fs_info->dev_root;
6638 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6639 struct extent_buffer *eb;
6640 int slot;
6641 int ret = 0;
6642 struct btrfs_device *device;
6643 struct btrfs_path *path = NULL;
6644 int i;
6645
6646 path = btrfs_alloc_path();
6647 if (!path) {
6648 ret = -ENOMEM;
6649 goto out;
6650 }
6651
6652 mutex_lock(&fs_devices->device_list_mutex);
6653 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6654 int item_size;
6655 struct btrfs_dev_stats_item *ptr;
6656
6657 key.objectid = 0;
6658 key.type = BTRFS_DEV_STATS_KEY;
6659 key.offset = device->devid;
6660 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6661 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006662 __btrfs_reset_dev_stats(device);
6663 device->dev_stats_valid = 1;
6664 btrfs_release_path(path);
6665 continue;
6666 }
6667 slot = path->slots[0];
6668 eb = path->nodes[0];
6669 btrfs_item_key_to_cpu(eb, &found_key, slot);
6670 item_size = btrfs_item_size_nr(eb, slot);
6671
6672 ptr = btrfs_item_ptr(eb, slot,
6673 struct btrfs_dev_stats_item);
6674
6675 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6676 if (item_size >= (1 + i) * sizeof(__le64))
6677 btrfs_dev_stat_set(device, i,
6678 btrfs_dev_stats_value(eb, ptr, i));
6679 else
6680 btrfs_dev_stat_reset(device, i);
6681 }
6682
6683 device->dev_stats_valid = 1;
6684 btrfs_dev_stat_print_on_load(device);
6685 btrfs_release_path(path);
6686 }
6687 mutex_unlock(&fs_devices->device_list_mutex);
6688
6689out:
6690 btrfs_free_path(path);
6691 return ret < 0 ? ret : 0;
6692}
6693
6694static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6695 struct btrfs_root *dev_root,
6696 struct btrfs_device *device)
6697{
6698 struct btrfs_path *path;
6699 struct btrfs_key key;
6700 struct extent_buffer *eb;
6701 struct btrfs_dev_stats_item *ptr;
6702 int ret;
6703 int i;
6704
6705 key.objectid = 0;
6706 key.type = BTRFS_DEV_STATS_KEY;
6707 key.offset = device->devid;
6708
6709 path = btrfs_alloc_path();
6710 BUG_ON(!path);
6711 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6712 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006713 btrfs_warn_in_rcu(dev_root->fs_info,
6714 "error %d while searching for dev_stats item for device %s",
Josef Bacik606686e2012-06-04 14:03:51 -04006715 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006716 goto out;
6717 }
6718
6719 if (ret == 0 &&
6720 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6721 /* need to delete old one and insert a new one */
6722 ret = btrfs_del_item(trans, dev_root, path);
6723 if (ret != 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006724 btrfs_warn_in_rcu(dev_root->fs_info,
6725 "delete too small dev_stats item for device %s failed %d",
Josef Bacik606686e2012-06-04 14:03:51 -04006726 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006727 goto out;
6728 }
6729 ret = 1;
6730 }
6731
6732 if (ret == 1) {
6733 /* need to insert a new item */
6734 btrfs_release_path(path);
6735 ret = btrfs_insert_empty_item(trans, dev_root, path,
6736 &key, sizeof(*ptr));
6737 if (ret < 0) {
David Sterbaecaeb142015-10-08 09:01:03 +02006738 btrfs_warn_in_rcu(dev_root->fs_info,
6739 "insert dev_stats item for device %s failed %d",
6740 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006741 goto out;
6742 }
6743 }
6744
6745 eb = path->nodes[0];
6746 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6747 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6748 btrfs_set_dev_stats_value(eb, ptr, i,
6749 btrfs_dev_stat_read(device, i));
6750 btrfs_mark_buffer_dirty(eb);
6751
6752out:
6753 btrfs_free_path(path);
6754 return ret;
6755}
6756
6757/*
6758 * called from commit_transaction. Writes all changed device stats to disk.
6759 */
6760int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6761 struct btrfs_fs_info *fs_info)
6762{
6763 struct btrfs_root *dev_root = fs_info->dev_root;
6764 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6765 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006766 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006767 int ret = 0;
6768
6769 mutex_lock(&fs_devices->device_list_mutex);
6770 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006771 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006772 continue;
6773
Miao Xieaddc3fa2014-07-24 11:37:11 +08006774 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006775 ret = update_dev_stat_item(trans, dev_root, device);
6776 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006777 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006778 }
6779 mutex_unlock(&fs_devices->device_list_mutex);
6780
6781 return ret;
6782}
6783
Stefan Behrens442a4f62012-05-25 16:06:08 +02006784void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6785{
6786 btrfs_dev_stat_inc(dev, index);
6787 btrfs_dev_stat_print_on_error(dev);
6788}
6789
Eric Sandeen48a3b632013-04-25 20:41:01 +00006790static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006791{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006792 if (!dev->dev_stats_valid)
6793 return;
David Sterbab14af3b2015-10-08 10:43:10 +02006794 btrfs_err_rl_in_rcu(dev->dev_root->fs_info,
6795 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006796 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006797 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6798 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6799 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006800 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6801 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006802}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006803
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006804static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6805{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006806 int i;
6807
6808 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6809 if (btrfs_dev_stat_read(dev, i) != 0)
6810 break;
6811 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6812 return; /* all values == 0, suppress message */
6813
David Sterbaecaeb142015-10-08 09:01:03 +02006814 btrfs_info_in_rcu(dev->dev_root->fs_info,
6815 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u",
Josef Bacik606686e2012-06-04 14:03:51 -04006816 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006817 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6818 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6819 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6820 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6821 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6822}
6823
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006824int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006825 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006826{
6827 struct btrfs_device *dev;
6828 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6829 int i;
6830
6831 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006832 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006833 mutex_unlock(&fs_devices->device_list_mutex);
6834
6835 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006836 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006837 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006838 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006839 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006840 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006841 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006842 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6843 if (stats->nr_items > i)
6844 stats->values[i] =
6845 btrfs_dev_stat_read_and_reset(dev, i);
6846 else
6847 btrfs_dev_stat_reset(dev, i);
6848 }
6849 } else {
6850 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6851 if (stats->nr_items > i)
6852 stats->values[i] = btrfs_dev_stat_read(dev, i);
6853 }
6854 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6855 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6856 return 0;
6857}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006858
Anand Jain12b1c262015-08-14 18:32:59 +08006859void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path)
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006860{
6861 struct buffer_head *bh;
6862 struct btrfs_super_block *disk_super;
Anand Jain12b1c262015-08-14 18:32:59 +08006863 int copy_num;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006864
Anand Jain12b1c262015-08-14 18:32:59 +08006865 if (!bdev)
6866 return;
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006867
Anand Jain12b1c262015-08-14 18:32:59 +08006868 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
6869 copy_num++) {
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006870
Anand Jain12b1c262015-08-14 18:32:59 +08006871 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
6872 continue;
6873
6874 disk_super = (struct btrfs_super_block *)bh->b_data;
6875
6876 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6877 set_buffer_dirty(bh);
6878 sync_dirty_buffer(bh);
6879 brelse(bh);
6880 }
6881
6882 /* Notify udev that device has changed */
6883 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
6884
6885 /* Update ctime/mtime for device path for libblkid */
6886 update_dev_time(device_path);
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006887}
Miao Xie935e5cc2014-09-03 21:35:33 +08006888
6889/*
6890 * Update the size of all devices, which is used for writing out the
6891 * super blocks.
6892 */
6893void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6894{
6895 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6896 struct btrfs_device *curr, *next;
6897
6898 if (list_empty(&fs_devices->resized_devices))
6899 return;
6900
6901 mutex_lock(&fs_devices->device_list_mutex);
6902 lock_chunks(fs_info->dev_root);
6903 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6904 resized_list) {
6905 list_del_init(&curr->resized_list);
6906 curr->commit_total_bytes = curr->disk_total_bytes;
6907 }
6908 unlock_chunks(fs_info->dev_root);
6909 mutex_unlock(&fs_devices->device_list_mutex);
6910}
Miao Xiece7213c2014-09-03 21:35:34 +08006911
6912/* Must be invoked during the transaction commit */
6913void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6914 struct btrfs_transaction *transaction)
6915{
6916 struct extent_map *em;
6917 struct map_lookup *map;
6918 struct btrfs_device *dev;
6919 int i;
6920
6921 if (list_empty(&transaction->pending_chunks))
6922 return;
6923
6924 /* In order to kick the device replace finish process */
6925 lock_chunks(root);
6926 list_for_each_entry(em, &transaction->pending_chunks, list) {
6927 map = (struct map_lookup *)em->bdev;
6928
6929 for (i = 0; i < map->num_stripes; i++) {
6930 dev = map->stripes[i].dev;
6931 dev->commit_bytes_used = dev->bytes_used;
6932 }
6933 }
6934 unlock_chunks(root);
6935}
Anand Jain5a13f432015-03-10 06:38:31 +08006936
6937void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)
6938{
6939 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6940 while (fs_devices) {
6941 fs_devices->fs_info = fs_info;
6942 fs_devices = fs_devices->seed;
6943 }
6944}
6945
6946void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6947{
6948 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6949 while (fs_devices) {
6950 fs_devices->fs_info = NULL;
6951 fs_devices = fs_devices->seed;
6952 }
6953}
Anand Jainf190aa42015-08-14 18:33:05 +08006954
David Sterba87ad58c2015-11-27 18:49:39 +01006955static void btrfs_close_one_device(struct btrfs_device *device)
Anand Jainf190aa42015-08-14 18:33:05 +08006956{
6957 struct btrfs_fs_devices *fs_devices = device->fs_devices;
6958 struct btrfs_device *new_device;
6959 struct rcu_string *name;
6960
6961 if (device->bdev)
6962 fs_devices->open_devices--;
6963
6964 if (device->writeable &&
6965 device->devid != BTRFS_DEV_REPLACE_DEVID) {
6966 list_del_init(&device->dev_alloc_list);
6967 fs_devices->rw_devices--;
6968 }
6969
6970 if (device->missing)
6971 fs_devices->missing_devices--;
6972
6973 new_device = btrfs_alloc_device(NULL, &device->devid,
6974 device->uuid);
6975 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
6976
6977 /* Safe because we are under uuid_mutex */
6978 if (device->name) {
6979 name = rcu_string_strdup(device->name->str, GFP_NOFS);
6980 BUG_ON(!name); /* -ENOMEM */
6981 rcu_assign_pointer(new_device->name, name);
6982 }
6983
6984 list_replace_rcu(&device->dev_list, &new_device->dev_list);
6985 new_device->fs_devices = device->fs_devices;
6986
6987 call_rcu(&device->rcu, free_device);
6988}