blob: 39ff99e4b5a6653df3e9ba606683ac95c831ee07 [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
Yan Zheng2b820322008-11-17 21:11:30 -050045static int init_first_rw_device(struct btrfs_trans_handle *trans,
46 struct btrfs_root *root,
47 struct btrfs_device *device);
48static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020049static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
Eric Sandeen48a3b632013-04-25 20:41:01 +000050static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020051static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050052
Miao Xie67a2c452014-09-03 21:35:43 +080053DEFINE_MUTEX(uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -040054static LIST_HEAD(fs_uuids);
55
Ilya Dryomov2208a372013-08-12 14:33:03 +030056static struct btrfs_fs_devices *__alloc_fs_devices(void)
57{
58 struct btrfs_fs_devices *fs_devs;
59
60 fs_devs = kzalloc(sizeof(*fs_devs), GFP_NOFS);
61 if (!fs_devs)
62 return ERR_PTR(-ENOMEM);
63
64 mutex_init(&fs_devs->device_list_mutex);
65
66 INIT_LIST_HEAD(&fs_devs->devices);
Miao Xie935e5cc2014-09-03 21:35:33 +080067 INIT_LIST_HEAD(&fs_devs->resized_devices);
Ilya Dryomov2208a372013-08-12 14:33:03 +030068 INIT_LIST_HEAD(&fs_devs->alloc_list);
69 INIT_LIST_HEAD(&fs_devs->list);
70
71 return fs_devs;
72}
73
74/**
75 * alloc_fs_devices - allocate struct btrfs_fs_devices
76 * @fsid: a pointer to UUID for this FS. If NULL a new UUID is
77 * generated.
78 *
79 * Return: a pointer to a new &struct btrfs_fs_devices on success;
80 * ERR_PTR() on error. Returned struct is not linked onto any lists and
81 * can be destroyed with kfree() right away.
82 */
83static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
84{
85 struct btrfs_fs_devices *fs_devs;
86
87 fs_devs = __alloc_fs_devices();
88 if (IS_ERR(fs_devs))
89 return fs_devs;
90
91 if (fsid)
92 memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
93 else
94 generate_random_uuid(fs_devs->fsid);
95
96 return fs_devs;
97}
98
Yan Zhenge4404d62008-12-12 10:03:26 -050099static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
100{
101 struct btrfs_device *device;
102 WARN_ON(fs_devices->opened);
103 while (!list_empty(&fs_devices->devices)) {
104 device = list_entry(fs_devices->devices.next,
105 struct btrfs_device, dev_list);
106 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -0400107 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500108 kfree(device);
109 }
110 kfree(fs_devices);
111}
112
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000113static void btrfs_kobject_uevent(struct block_device *bdev,
114 enum kobject_action action)
115{
116 int ret;
117
118 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
119 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -0500120 pr_warn("BTRFS: Sending event '%d' to kobject: '%s' (%p): failed\n",
Lukas Czernerb8b8ff52012-12-06 19:25:48 +0000121 action,
122 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
123 &disk_to_dev(bdev->bd_disk)->kobj);
124}
125
Jeff Mahoney143bede2012-03-01 14:56:26 +0100126void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400127{
128 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400129
Yan Zheng2b820322008-11-17 21:11:30 -0500130 while (!list_empty(&fs_uuids)) {
131 fs_devices = list_entry(fs_uuids.next,
132 struct btrfs_fs_devices, list);
133 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500134 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400135 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136}
137
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300138static struct btrfs_device *__alloc_device(void)
139{
140 struct btrfs_device *dev;
141
142 dev = kzalloc(sizeof(*dev), GFP_NOFS);
143 if (!dev)
144 return ERR_PTR(-ENOMEM);
145
146 INIT_LIST_HEAD(&dev->dev_list);
147 INIT_LIST_HEAD(&dev->dev_alloc_list);
Miao Xie935e5cc2014-09-03 21:35:33 +0800148 INIT_LIST_HEAD(&dev->resized_list);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300149
150 spin_lock_init(&dev->io_lock);
151
152 spin_lock_init(&dev->reada_lock);
153 atomic_set(&dev->reada_in_flight, 0);
Miao Xieaddc3fa2014-07-24 11:37:11 +0800154 atomic_set(&dev->dev_stats_ccnt, 0);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300155 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
156 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
157
158 return dev;
159}
160
Chris Masona1b32a52008-09-05 16:09:51 -0400161static noinline struct btrfs_device *__find_device(struct list_head *head,
162 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400163{
164 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400165
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500166 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400167 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400168 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400169 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400170 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400171 }
172 return NULL;
173}
174
Chris Masona1b32a52008-09-05 16:09:51 -0400175static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400176{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400177 struct btrfs_fs_devices *fs_devices;
178
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500179 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400180 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
181 return fs_devices;
182 }
183 return NULL;
184}
185
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100186static int
187btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
188 int flush, struct block_device **bdev,
189 struct buffer_head **bh)
190{
191 int ret;
192
193 *bdev = blkdev_get_by_path(device_path, flags, holder);
194
195 if (IS_ERR(*bdev)) {
196 ret = PTR_ERR(*bdev);
Frank Holtonefe120a2013-12-20 11:37:06 -0500197 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100198 goto error;
199 }
200
201 if (flush)
202 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
203 ret = set_blocksize(*bdev, 4096);
204 if (ret) {
205 blkdev_put(*bdev, flags);
206 goto error;
207 }
208 invalidate_bdev(*bdev);
209 *bh = btrfs_read_dev_super(*bdev);
210 if (!*bh) {
211 ret = -EINVAL;
212 blkdev_put(*bdev, flags);
213 goto error;
214 }
215
216 return 0;
217
218error:
219 *bdev = NULL;
220 *bh = NULL;
221 return ret;
222}
223
Chris Masonffbd5172009-04-20 15:50:09 -0400224static void requeue_list(struct btrfs_pending_bios *pending_bios,
225 struct bio *head, struct bio *tail)
226{
227
228 struct bio *old_head;
229
230 old_head = pending_bios->head;
231 pending_bios->head = head;
232 if (pending_bios->tail)
233 tail->bi_next = old_head;
234 else
235 pending_bios->tail = tail;
236}
237
Chris Mason8b712842008-06-11 16:50:36 -0400238/*
239 * we try to collect pending bios for a device so we don't get a large
240 * number of procs sending bios down to the same device. This greatly
241 * improves the schedulers ability to collect and merge the bios.
242 *
243 * But, it also turns into a long list of bios to process and that is sure
244 * to eventually make the worker thread block. The solution here is to
245 * make some progress and then put this work struct back at the end of
246 * the list if the block device is congested. This way, multiple devices
247 * can make progress from a single worker thread.
248 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100249static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400250{
251 struct bio *pending;
252 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400253 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400254 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400255 struct bio *tail;
256 struct bio *cur;
257 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400258 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400259 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400260 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400261 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400262 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000263 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400264 struct blk_plug plug;
265
266 /*
267 * this function runs all the bios we've collected for
268 * a particular device. We don't want to wander off to
269 * another device without first sending all of these down.
270 * So, setup a plug here and finish it off before we return
271 */
272 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400273
Chris Masonbedf7622009-04-03 10:32:58 -0400274 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400275 fs_info = device->dev_root->fs_info;
276 limit = btrfs_async_submit_limit(fs_info);
277 limit = limit * 2 / 3;
278
Chris Mason8b712842008-06-11 16:50:36 -0400279loop:
280 spin_lock(&device->io_lock);
281
Chris Masona6837052009-02-04 09:19:41 -0500282loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400283 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400284
Chris Mason8b712842008-06-11 16:50:36 -0400285 /* take all the bios off the list at once and process them
286 * later on (without the lock held). But, remember the
287 * tail and other pointers so the bios can be properly reinserted
288 * into the list if we hit congestion
289 */
Chris Masond84275c2009-06-09 15:39:08 -0400290 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400291 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400292 force_reg = 1;
293 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400294 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400295 force_reg = 0;
296 }
Chris Masonffbd5172009-04-20 15:50:09 -0400297
298 pending = pending_bios->head;
299 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400300 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400301
302 /*
303 * if pending was null this time around, no bios need processing
304 * at all and we can stop. Otherwise it'll loop back up again
305 * and do an additional check so no bios are missed.
306 *
307 * device->running_pending is used to synchronize with the
308 * schedule_bio code.
309 */
Chris Masonffbd5172009-04-20 15:50:09 -0400310 if (device->pending_sync_bios.head == NULL &&
311 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400312 again = 0;
313 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400314 } else {
315 again = 1;
316 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400317 }
Chris Masonffbd5172009-04-20 15:50:09 -0400318
319 pending_bios->head = NULL;
320 pending_bios->tail = NULL;
321
Chris Mason8b712842008-06-11 16:50:36 -0400322 spin_unlock(&device->io_lock);
323
Chris Masond3977122009-01-05 21:25:51 -0500324 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400325
326 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400327 /* we want to work on both lists, but do more bios on the
328 * sync list than the regular list
329 */
330 if ((num_run > 32 &&
331 pending_bios != &device->pending_sync_bios &&
332 device->pending_sync_bios.head) ||
333 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
334 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400335 spin_lock(&device->io_lock);
336 requeue_list(pending_bios, pending, tail);
337 goto loop_lock;
338 }
339
Chris Mason8b712842008-06-11 16:50:36 -0400340 cur = pending;
341 pending = pending->bi_next;
342 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400343
Josef Bacik66657b32012-08-01 15:36:24 -0400344 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400345 waitqueue_active(&fs_info->async_submit_wait))
346 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400347
348 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400349
Chris Mason2ab1ba62011-08-04 14:28:36 -0400350 /*
351 * if we're doing the sync list, record that our
352 * plug has some sync requests on it
353 *
354 * If we're doing the regular list and there are
355 * sync requests sitting around, unplug before
356 * we add more
357 */
358 if (pending_bios == &device->pending_sync_bios) {
359 sync_pending = 1;
360 } else if (sync_pending) {
361 blk_finish_plug(&plug);
362 blk_start_plug(&plug);
363 sync_pending = 0;
364 }
365
Stefan Behrens21adbd52011-11-09 13:44:05 +0100366 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400367 num_run++;
368 batch_run++;
David Sterba853d8ec2015-01-08 15:15:19 +0100369
370 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400371
372 /*
373 * we made progress, there is more work to do and the bdi
374 * is now congested. Back off and let other work structs
375 * run instead
376 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400377 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500378 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400379 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400380
Chris Masonb765ead2009-04-03 10:27:10 -0400381 ioc = current->io_context;
382
383 /*
384 * the main goal here is that we don't want to
385 * block if we're going to be able to submit
386 * more requests without blocking.
387 *
388 * This code does two great things, it pokes into
389 * the elevator code from a filesystem _and_
390 * it makes assumptions about how batching works.
391 */
392 if (ioc && ioc->nr_batch_requests > 0 &&
393 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
394 (last_waited == 0 ||
395 ioc->last_waited == last_waited)) {
396 /*
397 * we want to go through our batch of
398 * requests and stop. So, we copy out
399 * the ioc->last_waited time and test
400 * against it before looping
401 */
402 last_waited = ioc->last_waited;
David Sterba853d8ec2015-01-08 15:15:19 +0100403 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400404 continue;
405 }
Chris Mason8b712842008-06-11 16:50:36 -0400406 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400407 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500408 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400409
410 spin_unlock(&device->io_lock);
Qu Wenruoa8c93d42014-02-28 10:46:08 +0800411 btrfs_queue_work(fs_info->submit_workers,
412 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -0400413 goto done;
414 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500415 /* unplug every 64 requests just for good measure */
416 if (batch_run % 64 == 0) {
417 blk_finish_plug(&plug);
418 blk_start_plug(&plug);
419 sync_pending = 0;
420 }
Chris Mason8b712842008-06-11 16:50:36 -0400421 }
Chris Masonffbd5172009-04-20 15:50:09 -0400422
Chris Mason51684082010-03-10 15:33:32 -0500423 cond_resched();
424 if (again)
425 goto loop;
426
427 spin_lock(&device->io_lock);
428 if (device->pending_bios.head || device->pending_sync_bios.head)
429 goto loop_lock;
430 spin_unlock(&device->io_lock);
431
Chris Mason8b712842008-06-11 16:50:36 -0400432done:
Chris Mason211588a2011-04-19 20:12:40 -0400433 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400434}
435
Christoph Hellwigb2950862008-12-02 09:54:17 -0500436static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400437{
438 struct btrfs_device *device;
439
440 device = container_of(work, struct btrfs_device, work);
441 run_scheduled_bios(device);
442}
443
David Sterba60999ca2014-03-26 18:26:36 +0100444/*
445 * Add new device to list of registered devices
446 *
447 * Returns:
448 * 1 - first time device is seen
449 * 0 - device already known
450 * < 0 - error
451 */
Chris Masona1b32a52008-09-05 16:09:51 -0400452static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400453 struct btrfs_super_block *disk_super,
454 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
455{
456 struct btrfs_device *device;
457 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400458 struct rcu_string *name;
David Sterba60999ca2014-03-26 18:26:36 +0100459 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400460 u64 found_transid = btrfs_super_generation(disk_super);
461
462 fs_devices = find_fsid(disk_super->fsid);
463 if (!fs_devices) {
Ilya Dryomov2208a372013-08-12 14:33:03 +0300464 fs_devices = alloc_fs_devices(disk_super->fsid);
465 if (IS_ERR(fs_devices))
466 return PTR_ERR(fs_devices);
467
Chris Mason8a4b83c2008-03-24 15:02:07 -0400468 list_add(&fs_devices->list, &fs_uuids);
Ilya Dryomov2208a372013-08-12 14:33:03 +0300469
Chris Mason8a4b83c2008-03-24 15:02:07 -0400470 device = NULL;
471 } else {
Chris Masona4437552008-04-18 10:29:38 -0400472 device = __find_device(&fs_devices->devices, devid,
473 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400474 }
Miao Xie443f24f2014-07-24 11:37:15 +0800475
Chris Mason8a4b83c2008-03-24 15:02:07 -0400476 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500477 if (fs_devices->opened)
478 return -EBUSY;
479
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300480 device = btrfs_alloc_device(NULL, &devid,
481 disk_super->dev_item.uuid);
482 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400483 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300484 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400485 }
Josef Bacik606686e2012-06-04 14:03:51 -0400486
487 name = rcu_string_strdup(path, GFP_NOFS);
488 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400489 kfree(device);
490 return -ENOMEM;
491 }
Josef Bacik606686e2012-06-04 14:03:51 -0400492 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200493
Chris Masone5e9a522009-06-10 15:17:02 -0400494 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000495 list_add_rcu(&device->dev_list, &fs_devices->devices);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +0100496 fs_devices->num_devices++;
Chris Masone5e9a522009-06-10 15:17:02 -0400497 mutex_unlock(&fs_devices->device_list_mutex);
498
David Sterba60999ca2014-03-26 18:26:36 +0100499 ret = 1;
Yan Zheng2b820322008-11-17 21:11:30 -0500500 device->fs_devices = fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400501 } else if (!device->name || strcmp(device->name->str, path)) {
Anand Jainb96de002014-07-03 18:22:05 +0800502 /*
503 * When FS is already mounted.
504 * 1. If you are here and if the device->name is NULL that
505 * means this device was missing at time of FS mount.
506 * 2. If you are here and if the device->name is different
507 * from 'path' that means either
508 * a. The same device disappeared and reappeared with
509 * different name. or
510 * b. The missing-disk-which-was-replaced, has
511 * reappeared now.
512 *
513 * We must allow 1 and 2a above. But 2b would be a spurious
514 * and unintentional.
515 *
516 * Further in case of 1 and 2a above, the disk at 'path'
517 * would have missed some transaction when it was away and
518 * in case of 2a the stale bdev has to be updated as well.
519 * 2b must not be allowed at all time.
520 */
521
522 /*
Chris Mason0f23ae72014-09-18 07:49:05 -0700523 * For now, we do allow update to btrfs_fs_device through the
524 * btrfs dev scan cli after FS has been mounted. We're still
525 * tracking a problem where systems fail mount by subvolume id
526 * when we reject replacement on a mounted FS.
Anand Jainb96de002014-07-03 18:22:05 +0800527 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700528 if (!fs_devices->opened && found_transid < device->generation) {
Anand Jain77bdae42014-07-03 18:22:06 +0800529 /*
530 * That is if the FS is _not_ mounted and if you
531 * are here, that means there is more than one
532 * disk with same uuid and devid.We keep the one
533 * with larger generation number or the last-in if
534 * generation are equal.
535 */
Chris Mason0f23ae72014-09-18 07:49:05 -0700536 return -EEXIST;
Anand Jain77bdae42014-07-03 18:22:06 +0800537 }
Anand Jainb96de002014-07-03 18:22:05 +0800538
Josef Bacik606686e2012-06-04 14:03:51 -0400539 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000540 if (!name)
541 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400542 rcu_string_free(device->name);
543 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500544 if (device->missing) {
545 fs_devices->missing_devices--;
546 device->missing = 0;
547 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400548 }
549
Anand Jain77bdae42014-07-03 18:22:06 +0800550 /*
551 * Unmount does not free the btrfs_device struct but would zero
552 * generation along with most of the other members. So just update
553 * it back. We need it to pick the disk with largest generation
554 * (as above).
555 */
556 if (!fs_devices->opened)
557 device->generation = found_transid;
558
Chris Mason8a4b83c2008-03-24 15:02:07 -0400559 *fs_devices_ret = fs_devices;
David Sterba60999ca2014-03-26 18:26:36 +0100560
561 return ret;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400562}
563
Yan Zhenge4404d62008-12-12 10:03:26 -0500564static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
565{
566 struct btrfs_fs_devices *fs_devices;
567 struct btrfs_device *device;
568 struct btrfs_device *orig_dev;
569
Ilya Dryomov2208a372013-08-12 14:33:03 +0300570 fs_devices = alloc_fs_devices(orig->fsid);
571 if (IS_ERR(fs_devices))
572 return fs_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500573
Miao Xieadbbb862014-09-03 21:35:42 +0800574 mutex_lock(&orig->device_list_mutex);
Josef Bacik02db0842012-06-21 16:03:58 -0400575 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500576
Xiao Guangrong46224702011-04-20 10:08:47 +0000577 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500578 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400579 struct rcu_string *name;
580
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300581 device = btrfs_alloc_device(NULL, &orig_dev->devid,
582 orig_dev->uuid);
583 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500584 goto error;
585
Josef Bacik606686e2012-06-04 14:03:51 -0400586 /*
587 * This is ok to do without rcu read locked because we hold the
588 * uuid mutex so nothing we touch in here is going to disappear.
589 */
Anand Jaine755f782014-06-30 17:12:47 +0800590 if (orig_dev->name) {
591 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
592 if (!name) {
593 kfree(device);
594 goto error;
595 }
596 rcu_assign_pointer(device->name, name);
Julia Lawallfd2696f2009-09-29 13:51:04 -0400597 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500598
Yan Zhenge4404d62008-12-12 10:03:26 -0500599 list_add(&device->dev_list, &fs_devices->devices);
600 device->fs_devices = fs_devices;
601 fs_devices->num_devices++;
602 }
Miao Xieadbbb862014-09-03 21:35:42 +0800603 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500604 return fs_devices;
605error:
Miao Xieadbbb862014-09-03 21:35:42 +0800606 mutex_unlock(&orig->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500607 free_fs_devices(fs_devices);
608 return ERR_PTR(-ENOMEM);
609}
610
Eric Sandeen9eaed212014-08-01 18:12:35 -0500611void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400612{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500613 struct btrfs_device *device, *next;
Miao Xie443f24f2014-07-24 11:37:15 +0800614 struct btrfs_device *latest_dev = NULL;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500615
Chris Masondfe25022008-05-13 13:46:40 -0400616 mutex_lock(&uuid_mutex);
617again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000618 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500619 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500620 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100621 if (!device->is_tgtdev_for_dev_replace &&
Miao Xie443f24f2014-07-24 11:37:15 +0800622 (!latest_dev ||
623 device->generation > latest_dev->generation)) {
624 latest_dev = device;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500625 }
Yan Zheng2b820322008-11-17 21:11:30 -0500626 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500627 }
Yan Zheng2b820322008-11-17 21:11:30 -0500628
Stefan Behrens8dabb742012-11-06 13:15:27 +0100629 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
630 /*
631 * In the first step, keep the device which has
632 * the correct fsid and the devid that is used
633 * for the dev_replace procedure.
634 * In the second step, the dev_replace state is
635 * read from the device tree and it is known
636 * whether the procedure is really active or
637 * not, which means whether this device is
638 * used or whether it should be removed.
639 */
640 if (step == 0 || device->is_tgtdev_for_dev_replace) {
641 continue;
642 }
643 }
Yan Zheng2b820322008-11-17 21:11:30 -0500644 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100645 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500646 device->bdev = NULL;
647 fs_devices->open_devices--;
648 }
649 if (device->writeable) {
650 list_del_init(&device->dev_alloc_list);
651 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100652 if (!device->is_tgtdev_for_dev_replace)
653 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500654 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500655 list_del_init(&device->dev_list);
656 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400657 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500658 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400659 }
Yan Zheng2b820322008-11-17 21:11:30 -0500660
661 if (fs_devices->seed) {
662 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500663 goto again;
664 }
665
Miao Xie443f24f2014-07-24 11:37:15 +0800666 fs_devices->latest_bdev = latest_dev->bdev;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500667
Chris Masondfe25022008-05-13 13:46:40 -0400668 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400669}
Chris Masona0af4692008-05-13 16:03:06 -0400670
Xiao Guangrong1f781602011-04-20 10:09:16 +0000671static void __free_device(struct work_struct *work)
672{
673 struct btrfs_device *device;
674
675 device = container_of(work, struct btrfs_device, rcu_work);
676
677 if (device->bdev)
678 blkdev_put(device->bdev, device->mode);
679
Josef Bacik606686e2012-06-04 14:03:51 -0400680 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000681 kfree(device);
682}
683
684static void free_device(struct rcu_head *head)
685{
686 struct btrfs_device *device;
687
688 device = container_of(head, struct btrfs_device, rcu);
689
690 INIT_WORK(&device->rcu_work, __free_device);
691 schedule_work(&device->rcu_work);
692}
693
Yan Zheng2b820322008-11-17 21:11:30 -0500694static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400695{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400696 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500697
Yan Zheng2b820322008-11-17 21:11:30 -0500698 if (--fs_devices->opened > 0)
699 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400700
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000701 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500702 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000703 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400704 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000705
706 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400707 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000708
Ilya Dryomovf747cab2013-10-10 20:37:29 +0300709 if (device->writeable &&
710 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500711 list_del_init(&device->dev_alloc_list);
712 fs_devices->rw_devices--;
713 }
714
Josef Bacik726551e2013-08-26 13:45:53 -0400715 if (device->missing)
716 fs_devices->missing_devices--;
Josef Bacikd5e20032011-08-04 14:52:27 +0000717
Ilya Dryomova1e87802013-08-12 14:33:04 +0300718 new_device = btrfs_alloc_device(NULL, &device->devid,
719 device->uuid);
720 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
Josef Bacik606686e2012-06-04 14:03:51 -0400721
722 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400723 if (device->name) {
724 name = rcu_string_strdup(device->name->str, GFP_NOFS);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300725 BUG_ON(!name); /* -ENOMEM */
Josef Bacik99f59442012-08-02 10:23:59 -0400726 rcu_assign_pointer(new_device->name, name);
727 }
Ilya Dryomova1e87802013-08-12 14:33:04 +0300728
Xiao Guangrong1f781602011-04-20 10:09:16 +0000729 list_replace_rcu(&device->dev_list, &new_device->dev_list);
Ilya Dryomova1e87802013-08-12 14:33:04 +0300730 new_device->fs_devices = device->fs_devices;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000731
732 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400733 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000734 mutex_unlock(&fs_devices->device_list_mutex);
735
Yan Zhenge4404d62008-12-12 10:03:26 -0500736 WARN_ON(fs_devices->open_devices);
737 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500738 fs_devices->opened = 0;
739 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500740
Chris Mason8a4b83c2008-03-24 15:02:07 -0400741 return 0;
742}
743
Yan Zheng2b820322008-11-17 21:11:30 -0500744int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
745{
Yan Zhenge4404d62008-12-12 10:03:26 -0500746 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500747 int ret;
748
749 mutex_lock(&uuid_mutex);
750 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500751 if (!fs_devices->opened) {
752 seed_devices = fs_devices->seed;
753 fs_devices->seed = NULL;
754 }
Yan Zheng2b820322008-11-17 21:11:30 -0500755 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500756
757 while (seed_devices) {
758 fs_devices = seed_devices;
759 seed_devices = fs_devices->seed;
760 __btrfs_close_devices(fs_devices);
761 free_fs_devices(fs_devices);
762 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000763 /*
764 * Wait for rcu kworkers under __btrfs_close_devices
765 * to finish all blkdev_puts so device is really
766 * free when umount is done.
767 */
768 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500769 return ret;
770}
771
Yan Zhenge4404d62008-12-12 10:03:26 -0500772static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
773 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400774{
Josef Bacikd5e20032011-08-04 14:52:27 +0000775 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400776 struct block_device *bdev;
777 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400778 struct btrfs_device *device;
Miao Xie443f24f2014-07-24 11:37:15 +0800779 struct btrfs_device *latest_dev = NULL;
Chris Masona0af4692008-05-13 16:03:06 -0400780 struct buffer_head *bh;
781 struct btrfs_super_block *disk_super;
Chris Masona0af4692008-05-13 16:03:06 -0400782 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500783 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400784 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400785
Tejun Heod4d77622010-11-13 11:55:18 +0100786 flags |= FMODE_EXCL;
787
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500788 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400789 if (device->bdev)
790 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400791 if (!device->name)
792 continue;
793
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000794 /* Just open everything we can; ignore failures here */
795 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
796 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100797 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400798
799 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000800 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400801 if (devid != device->devid)
802 goto error_brelse;
803
Yan Zheng2b820322008-11-17 21:11:30 -0500804 if (memcmp(device->uuid, disk_super->dev_item.uuid,
805 BTRFS_UUID_SIZE))
806 goto error_brelse;
807
808 device->generation = btrfs_super_generation(disk_super);
Miao Xie443f24f2014-07-24 11:37:15 +0800809 if (!latest_dev ||
810 device->generation > latest_dev->generation)
811 latest_dev = device;
Chris Masona0af4692008-05-13 16:03:06 -0400812
Yan Zheng2b820322008-11-17 21:11:30 -0500813 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
814 device->writeable = 0;
815 } else {
816 device->writeable = !bdev_read_only(bdev);
817 seeding = 0;
818 }
819
Josef Bacikd5e20032011-08-04 14:52:27 +0000820 q = bdev_get_queue(bdev);
Miao Xie90180da2014-09-03 21:35:30 +0800821 if (blk_queue_discard(q))
Josef Bacikd5e20032011-08-04 14:52:27 +0000822 device->can_discard = 1;
Josef Bacikd5e20032011-08-04 14:52:27 +0000823
Chris Mason8a4b83c2008-03-24 15:02:07 -0400824 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400825 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500826 device->mode = flags;
827
Chris Masonc2898112009-06-10 09:51:32 -0400828 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
829 fs_devices->rotating = 1;
830
Chris Masona0af4692008-05-13 16:03:06 -0400831 fs_devices->open_devices++;
Ilya Dryomov55e50e42013-09-01 18:56:44 +0300832 if (device->writeable &&
833 device->devid != BTRFS_DEV_REPLACE_DEVID) {
Yan Zheng2b820322008-11-17 21:11:30 -0500834 fs_devices->rw_devices++;
835 list_add(&device->dev_alloc_list,
836 &fs_devices->alloc_list);
837 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000838 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400839 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400840
Chris Masona0af4692008-05-13 16:03:06 -0400841error_brelse:
842 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100843 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400844 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400845 }
Chris Masona0af4692008-05-13 16:03:06 -0400846 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300847 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400848 goto out;
849 }
Yan Zheng2b820322008-11-17 21:11:30 -0500850 fs_devices->seeding = seeding;
851 fs_devices->opened = 1;
Miao Xie443f24f2014-07-24 11:37:15 +0800852 fs_devices->latest_bdev = latest_dev->bdev;
Yan Zheng2b820322008-11-17 21:11:30 -0500853 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400854out:
Yan Zheng2b820322008-11-17 21:11:30 -0500855 return ret;
856}
857
858int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500859 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500860{
861 int ret;
862
863 mutex_lock(&uuid_mutex);
864 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500865 fs_devices->opened++;
866 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500867 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500868 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500869 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400870 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400871 return ret;
872}
873
David Sterba6f60cbd2013-02-15 11:31:02 -0700874/*
875 * Look for a btrfs signature on a device. This may be called out of the mount path
876 * and we are not allowed to call set_blocksize during the scan. The superblock
877 * is read via pagecache
878 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500879int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400880 struct btrfs_fs_devices **fs_devices_ret)
881{
882 struct btrfs_super_block *disk_super;
883 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700884 struct page *page;
885 void *p;
886 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400887 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400888 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400889 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700890 u64 bytenr;
891 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400892
David Sterba6f60cbd2013-02-15 11:31:02 -0700893 /*
894 * we would like to check all the supers, but that would make
895 * a btrfs mount succeed after a mkfs from a different FS.
896 * So, we need to add a special mount option to scan for
897 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
898 */
899 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100900 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500901 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700902
903 bdev = blkdev_get_by_path(path, flags, holder);
904
905 if (IS_ERR(bdev)) {
906 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100907 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700908 }
909
910 /* make sure our super fits in the device */
911 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
912 goto error_bdev_put;
913
914 /* make sure our super fits in the page */
915 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
916 goto error_bdev_put;
917
918 /* make sure our super doesn't straddle pages on disk */
919 index = bytenr >> PAGE_CACHE_SHIFT;
920 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
921 goto error_bdev_put;
922
923 /* pull in the page with our super */
924 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
925 index, GFP_NOFS);
926
927 if (IS_ERR_OR_NULL(page))
928 goto error_bdev_put;
929
930 p = kmap(page);
931
932 /* align our pointer to the offset of the super block */
933 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
934
935 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800936 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700937 goto error_unmap;
938
Xiao Guangronga3438322010-01-06 11:48:18 +0000939 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400940 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400941 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700942
Chris Mason8a4b83c2008-03-24 15:02:07 -0400943 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
David Sterba60999ca2014-03-26 18:26:36 +0100944 if (ret > 0) {
945 if (disk_super->label[0]) {
946 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
947 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
948 printk(KERN_INFO "BTRFS: device label %s ", disk_super->label);
949 } else {
950 printk(KERN_INFO "BTRFS: device fsid %pU ", disk_super->fsid);
951 }
952
953 printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
954 ret = 0;
955 }
Josef Bacik02db0842012-06-21 16:03:58 -0400956 if (!ret && fs_devices_ret)
957 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700958
959error_unmap:
960 kunmap(page);
961 page_cache_release(page);
962
963error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100964 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400965error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100966 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400967 return ret;
968}
Chris Mason0b86a832008-03-24 15:01:56 -0400969
Miao Xie6d07bce2011-01-05 10:07:31 +0000970/* helper to account the used device space in the range */
971int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
972 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400973{
974 struct btrfs_key key;
975 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000976 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500977 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000978 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400979 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000980 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400981 struct extent_buffer *l;
982
Miao Xie6d07bce2011-01-05 10:07:31 +0000983 *length = 0;
984
Stefan Behrens63a212a2012-11-05 18:29:28 +0100985 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000986 return 0;
987
Yan Zheng2b820322008-11-17 21:11:30 -0500988 path = btrfs_alloc_path();
989 if (!path)
990 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400991 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400992
Chris Mason0b86a832008-03-24 15:01:56 -0400993 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000994 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400995 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000996
997 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400998 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000999 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001000 if (ret > 0) {
1001 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1002 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001003 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -04001004 }
Miao Xie6d07bce2011-01-05 10:07:31 +00001005
Chris Mason0b86a832008-03-24 15:01:56 -04001006 while (1) {
1007 l = path->nodes[0];
1008 slot = path->slots[0];
1009 if (slot >= btrfs_header_nritems(l)) {
1010 ret = btrfs_next_leaf(root, path);
1011 if (ret == 0)
1012 continue;
1013 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +00001014 goto out;
1015
1016 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001017 }
1018 btrfs_item_key_to_cpu(l, &key, slot);
1019
1020 if (key.objectid < device->devid)
1021 goto next;
1022
1023 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +00001024 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001025
David Sterba962a2982014-06-04 18:41:45 +02001026 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001027 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -04001028
Chris Mason0b86a832008-03-24 15:01:56 -04001029 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +00001030 extent_end = key.offset + btrfs_dev_extent_length(l,
1031 dev_extent);
1032 if (key.offset <= start && extent_end > end) {
1033 *length = end - start + 1;
1034 break;
1035 } else if (key.offset <= start && extent_end > start)
1036 *length += extent_end - start;
1037 else if (key.offset > start && extent_end <= end)
1038 *length += extent_end - key.offset;
1039 else if (key.offset > start && key.offset <= end) {
1040 *length += end - key.offset + 1;
1041 break;
1042 } else if (key.offset > end)
1043 break;
1044
1045next:
1046 path->slots[0]++;
1047 }
1048 ret = 0;
1049out:
1050 btrfs_free_path(path);
1051 return ret;
1052}
1053
Josef Bacik6df9a952013-06-27 13:22:46 -04001054static int contains_pending_extent(struct btrfs_trans_handle *trans,
1055 struct btrfs_device *device,
1056 u64 *start, u64 len)
1057{
1058 struct extent_map *em;
Filipe Manana04216822014-11-27 21:14:15 +00001059 struct list_head *search_list = &trans->transaction->pending_chunks;
Josef Bacik6df9a952013-06-27 13:22:46 -04001060 int ret = 0;
Forrest Liu1b984502015-02-09 17:30:47 +08001061 u64 physical_start = *start;
Josef Bacik6df9a952013-06-27 13:22:46 -04001062
Filipe Manana04216822014-11-27 21:14:15 +00001063again:
1064 list_for_each_entry(em, search_list, list) {
Josef Bacik6df9a952013-06-27 13:22:46 -04001065 struct map_lookup *map;
1066 int i;
1067
1068 map = (struct map_lookup *)em->bdev;
1069 for (i = 0; i < map->num_stripes; i++) {
1070 if (map->stripes[i].dev != device)
1071 continue;
Forrest Liu1b984502015-02-09 17:30:47 +08001072 if (map->stripes[i].physical >= physical_start + len ||
Josef Bacik6df9a952013-06-27 13:22:46 -04001073 map->stripes[i].physical + em->orig_block_len <=
Forrest Liu1b984502015-02-09 17:30:47 +08001074 physical_start)
Josef Bacik6df9a952013-06-27 13:22:46 -04001075 continue;
1076 *start = map->stripes[i].physical +
1077 em->orig_block_len;
1078 ret = 1;
1079 }
1080 }
Filipe Manana04216822014-11-27 21:14:15 +00001081 if (search_list == &trans->transaction->pending_chunks) {
1082 search_list = &trans->root->fs_info->pinned_chunks;
1083 goto again;
1084 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001085
1086 return ret;
1087}
1088
1089
Chris Mason0b86a832008-03-24 15:01:56 -04001090/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001091 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001092 * @device: the device which we search the free space in
1093 * @num_bytes: the size of the free space that we need
1094 * @start: store the start of the free space.
1095 * @len: the size of the free space. that we find, or the size of the max
1096 * free space if we don't find suitable free space
1097 *
Chris Mason0b86a832008-03-24 15:01:56 -04001098 * this uses a pretty simple search, the expectation is that it is
1099 * called very infrequently and that a given device has a small number
1100 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001101 *
1102 * @start is used to store the start of the free space if we find. But if we
1103 * don't find suitable free space, it will be used to store the start position
1104 * of the max free space.
1105 *
1106 * @len is used to store the size of the free space that we find.
1107 * But if we don't find suitable free space, it is used to store the size of
1108 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001109 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001110int find_free_dev_extent(struct btrfs_trans_handle *trans,
1111 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001112 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001113{
1114 struct btrfs_key key;
1115 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001116 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001117 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001118 u64 hole_size;
1119 u64 max_hole_start;
1120 u64 max_hole_size;
1121 u64 extent_end;
1122 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001123 u64 search_end = device->total_bytes;
1124 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001125 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001126 struct extent_buffer *l;
1127
Chris Mason0b86a832008-03-24 15:01:56 -04001128 /* FIXME use last free of some kind */
1129
1130 /* we don't want to overwrite the superblock on the drive,
1131 * so we make sure to start at an offset of at least 1MB
1132 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001133 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001134
Josef Bacik6df9a952013-06-27 13:22:46 -04001135 path = btrfs_alloc_path();
1136 if (!path)
1137 return -ENOMEM;
Zhao Leif2ab7612015-02-16 18:52:17 +08001138
Miao Xie7bfc8372011-01-05 10:07:26 +00001139 max_hole_start = search_start;
1140 max_hole_size = 0;
1141
Zhao Leif2ab7612015-02-16 18:52:17 +08001142again:
Stefan Behrens63a212a2012-11-05 18:29:28 +01001143 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001144 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001145 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001146 }
1147
Miao Xie7bfc8372011-01-05 10:07:26 +00001148 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001149 path->search_commit_root = 1;
1150 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001151
Chris Mason0b86a832008-03-24 15:01:56 -04001152 key.objectid = device->devid;
1153 key.offset = search_start;
1154 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001155
Li Zefan125ccb02011-12-08 15:07:24 +08001156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001157 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001158 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001159 if (ret > 0) {
1160 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1161 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001162 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001163 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001164
Chris Mason0b86a832008-03-24 15:01:56 -04001165 while (1) {
1166 l = path->nodes[0];
1167 slot = path->slots[0];
1168 if (slot >= btrfs_header_nritems(l)) {
1169 ret = btrfs_next_leaf(root, path);
1170 if (ret == 0)
1171 continue;
1172 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001173 goto out;
1174
1175 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001176 }
1177 btrfs_item_key_to_cpu(l, &key, slot);
1178
1179 if (key.objectid < device->devid)
1180 goto next;
1181
1182 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001183 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001184
David Sterba962a2982014-06-04 18:41:45 +02001185 if (key.type != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -04001186 goto next;
1187
Miao Xie7bfc8372011-01-05 10:07:26 +00001188 if (key.offset > search_start) {
1189 hole_size = key.offset - search_start;
1190
Josef Bacik6df9a952013-06-27 13:22:46 -04001191 /*
1192 * Have to check before we set max_hole_start, otherwise
1193 * we could end up sending back this offset anyway.
1194 */
1195 if (contains_pending_extent(trans, device,
1196 &search_start,
Forrest Liu1b984502015-02-09 17:30:47 +08001197 hole_size)) {
1198 if (key.offset >= search_start) {
1199 hole_size = key.offset - search_start;
1200 } else {
1201 WARN_ON_ONCE(1);
1202 hole_size = 0;
1203 }
1204 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001205
Miao Xie7bfc8372011-01-05 10:07:26 +00001206 if (hole_size > max_hole_size) {
1207 max_hole_start = search_start;
1208 max_hole_size = hole_size;
1209 }
1210
1211 /*
1212 * If this free space is greater than which we need,
1213 * it must be the max free space that we have found
1214 * until now, so max_hole_start must point to the start
1215 * of this free space and the length of this free space
1216 * is stored in max_hole_size. Thus, we return
1217 * max_hole_start and max_hole_size and go back to the
1218 * caller.
1219 */
1220 if (hole_size >= num_bytes) {
1221 ret = 0;
1222 goto out;
1223 }
1224 }
1225
Chris Mason0b86a832008-03-24 15:01:56 -04001226 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001227 extent_end = key.offset + btrfs_dev_extent_length(l,
1228 dev_extent);
1229 if (extent_end > search_start)
1230 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001231next:
1232 path->slots[0]++;
1233 cond_resched();
1234 }
Chris Mason0b86a832008-03-24 15:01:56 -04001235
liubo38c01b92011-08-02 02:39:03 +00001236 /*
1237 * At this point, search_start should be the end of
1238 * allocated dev extents, and when shrinking the device,
1239 * search_end may be smaller than search_start.
1240 */
Zhao Leif2ab7612015-02-16 18:52:17 +08001241 if (search_end > search_start) {
liubo38c01b92011-08-02 02:39:03 +00001242 hole_size = search_end - search_start;
1243
Zhao Leif2ab7612015-02-16 18:52:17 +08001244 if (contains_pending_extent(trans, device, &search_start,
1245 hole_size)) {
1246 btrfs_release_path(path);
1247 goto again;
1248 }
Chris Mason0b86a832008-03-24 15:01:56 -04001249
Zhao Leif2ab7612015-02-16 18:52:17 +08001250 if (hole_size > max_hole_size) {
1251 max_hole_start = search_start;
1252 max_hole_size = hole_size;
1253 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001254 }
1255
Miao Xie7bfc8372011-01-05 10:07:26 +00001256 /* See above. */
Zhao Leif2ab7612015-02-16 18:52:17 +08001257 if (max_hole_size < num_bytes)
Miao Xie7bfc8372011-01-05 10:07:26 +00001258 ret = -ENOSPC;
1259 else
1260 ret = 0;
1261
1262out:
Yan Zheng2b820322008-11-17 21:11:30 -05001263 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001264 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001265 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001266 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001267 return ret;
1268}
1269
Christoph Hellwigb2950862008-12-02 09:54:17 -05001270static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001271 struct btrfs_device *device,
Miao Xie2196d6e2014-09-03 21:35:41 +08001272 u64 start, u64 *dev_extent_len)
Chris Mason8f18cf12008-04-25 16:53:30 -04001273{
1274 int ret;
1275 struct btrfs_path *path;
1276 struct btrfs_root *root = device->dev_root;
1277 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001278 struct btrfs_key found_key;
1279 struct extent_buffer *leaf = NULL;
1280 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001281
1282 path = btrfs_alloc_path();
1283 if (!path)
1284 return -ENOMEM;
1285
1286 key.objectid = device->devid;
1287 key.offset = start;
1288 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001289again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001290 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001291 if (ret > 0) {
1292 ret = btrfs_previous_item(root, path, key.objectid,
1293 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001294 if (ret)
1295 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001296 leaf = path->nodes[0];
1297 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1298 extent = btrfs_item_ptr(leaf, path->slots[0],
1299 struct btrfs_dev_extent);
1300 BUG_ON(found_key.offset > start || found_key.offset +
1301 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001302 key = found_key;
1303 btrfs_release_path(path);
1304 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001305 } else if (ret == 0) {
1306 leaf = path->nodes[0];
1307 extent = btrfs_item_ptr(leaf, path->slots[0],
1308 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001309 } else {
1310 btrfs_error(root->fs_info, ret, "Slot search failed");
1311 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001312 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001313
Miao Xie2196d6e2014-09-03 21:35:41 +08001314 *dev_extent_len = btrfs_dev_extent_length(leaf, extent);
1315
Chris Mason8f18cf12008-04-25 16:53:30 -04001316 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001317 if (ret) {
1318 btrfs_error(root->fs_info, ret,
1319 "Failed to remove dev extent item");
Zhao Lei13212b52015-02-12 14:18:17 +08001320 } else {
1321 trans->transaction->have_free_bgs = 1;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001322 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001323out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001324 btrfs_free_path(path);
1325 return ret;
1326}
1327
Eric Sandeen48a3b632013-04-25 20:41:01 +00001328static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1329 struct btrfs_device *device,
1330 u64 chunk_tree, u64 chunk_objectid,
1331 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001332{
1333 int ret;
1334 struct btrfs_path *path;
1335 struct btrfs_root *root = device->dev_root;
1336 struct btrfs_dev_extent *extent;
1337 struct extent_buffer *leaf;
1338 struct btrfs_key key;
1339
Chris Masondfe25022008-05-13 13:46:40 -04001340 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001341 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001342 path = btrfs_alloc_path();
1343 if (!path)
1344 return -ENOMEM;
1345
Chris Mason0b86a832008-03-24 15:01:56 -04001346 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001347 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001348 key.type = BTRFS_DEV_EXTENT_KEY;
1349 ret = btrfs_insert_empty_item(trans, root, path, &key,
1350 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001351 if (ret)
1352 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001353
1354 leaf = path->nodes[0];
1355 extent = btrfs_item_ptr(leaf, path->slots[0],
1356 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001357 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1358 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1359 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1360
1361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
Geert Uytterhoeven231e88f2013-08-20 13:20:13 +02001362 btrfs_dev_extent_chunk_tree_uuid(extent), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04001363
Chris Mason0b86a832008-03-24 15:01:56 -04001364 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1365 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001366out:
Chris Mason0b86a832008-03-24 15:01:56 -04001367 btrfs_free_path(path);
1368 return ret;
1369}
1370
Josef Bacik6df9a952013-06-27 13:22:46 -04001371static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001372{
Josef Bacik6df9a952013-06-27 13:22:46 -04001373 struct extent_map_tree *em_tree;
1374 struct extent_map *em;
1375 struct rb_node *n;
1376 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001377
Josef Bacik6df9a952013-06-27 13:22:46 -04001378 em_tree = &fs_info->mapping_tree.map_tree;
1379 read_lock(&em_tree->lock);
1380 n = rb_last(&em_tree->map);
1381 if (n) {
1382 em = rb_entry(n, struct extent_map, rb_node);
1383 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001384 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001385 read_unlock(&em_tree->lock);
1386
Chris Mason0b86a832008-03-24 15:01:56 -04001387 return ret;
1388}
1389
Ilya Dryomov53f10652013-08-12 14:33:01 +03001390static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1391 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001392{
1393 int ret;
1394 struct btrfs_key key;
1395 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001396 struct btrfs_path *path;
1397
Yan Zheng2b820322008-11-17 21:11:30 -05001398 path = btrfs_alloc_path();
1399 if (!path)
1400 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001401
1402 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1403 key.type = BTRFS_DEV_ITEM_KEY;
1404 key.offset = (u64)-1;
1405
Ilya Dryomov53f10652013-08-12 14:33:01 +03001406 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001407 if (ret < 0)
1408 goto error;
1409
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001411
Ilya Dryomov53f10652013-08-12 14:33:01 +03001412 ret = btrfs_previous_item(fs_info->chunk_root, path,
1413 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001414 BTRFS_DEV_ITEM_KEY);
1415 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001416 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001417 } else {
1418 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1419 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001420 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001421 }
1422 ret = 0;
1423error:
Yan Zheng2b820322008-11-17 21:11:30 -05001424 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001425 return ret;
1426}
1427
1428/*
1429 * the device information is stored in the chunk root
1430 * the btrfs_device struct should be fully filled in
1431 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001432static int btrfs_add_device(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root,
1434 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001435{
1436 int ret;
1437 struct btrfs_path *path;
1438 struct btrfs_dev_item *dev_item;
1439 struct extent_buffer *leaf;
1440 struct btrfs_key key;
1441 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001442
1443 root = root->fs_info->chunk_root;
1444
1445 path = btrfs_alloc_path();
1446 if (!path)
1447 return -ENOMEM;
1448
Chris Mason0b86a832008-03-24 15:01:56 -04001449 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1450 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001451 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001452
1453 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001454 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001455 if (ret)
1456 goto out;
1457
1458 leaf = path->nodes[0];
1459 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1460
1461 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001462 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001463 btrfs_set_device_type(leaf, dev_item, device->type);
1464 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1465 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1466 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08001467 btrfs_set_device_total_bytes(leaf, dev_item,
1468 btrfs_device_get_disk_total_bytes(device));
1469 btrfs_set_device_bytes_used(leaf, dev_item,
1470 btrfs_device_get_bytes_used(device));
Chris Masone17cade2008-04-15 15:41:47 -04001471 btrfs_set_device_group(leaf, dev_item, 0);
1472 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1473 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001474 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001475
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02001476 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001477 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02001478 ptr = btrfs_device_fsid(dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001479 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001480 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001481
Yan Zheng2b820322008-11-17 21:11:30 -05001482 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001483out:
1484 btrfs_free_path(path);
1485 return ret;
1486}
Chris Mason8f18cf12008-04-25 16:53:30 -04001487
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001488/*
1489 * Function to update ctime/mtime for a given device path.
1490 * Mainly used for ctime/mtime based probe like libblkid.
1491 */
1492static void update_dev_time(char *path_name)
1493{
1494 struct file *filp;
1495
1496 filp = filp_open(path_name, O_RDWR, 0);
Al Viro98af5922014-12-14 02:59:17 -05001497 if (IS_ERR(filp))
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001498 return;
1499 file_update_time(filp);
1500 filp_close(filp, NULL);
1501 return;
1502}
1503
Chris Masona061fc82008-05-07 11:43:44 -04001504static int btrfs_rm_dev_item(struct btrfs_root *root,
1505 struct btrfs_device *device)
1506{
1507 int ret;
1508 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001509 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001510 struct btrfs_trans_handle *trans;
1511
1512 root = root->fs_info->chunk_root;
1513
1514 path = btrfs_alloc_path();
1515 if (!path)
1516 return -ENOMEM;
1517
Yan, Zhenga22285a2010-05-16 10:48:46 -04001518 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001519 if (IS_ERR(trans)) {
1520 btrfs_free_path(path);
1521 return PTR_ERR(trans);
1522 }
Chris Masona061fc82008-05-07 11:43:44 -04001523 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1524 key.type = BTRFS_DEV_ITEM_KEY;
1525 key.offset = device->devid;
1526
1527 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1528 if (ret < 0)
1529 goto out;
1530
1531 if (ret > 0) {
1532 ret = -ENOENT;
1533 goto out;
1534 }
1535
1536 ret = btrfs_del_item(trans, root, path);
1537 if (ret)
1538 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001539out:
1540 btrfs_free_path(path);
1541 btrfs_commit_transaction(trans, root);
1542 return ret;
1543}
1544
1545int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1546{
1547 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001548 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001549 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001550 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001551 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001552 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001553 u64 all_avail;
1554 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001555 u64 num_devices;
1556 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001557 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001558 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001559 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001560
Chris Masona061fc82008-05-07 11:43:44 -04001561 mutex_lock(&uuid_mutex);
1562
Miao Xiede98ced2013-01-29 10:13:12 +00001563 do {
1564 seq = read_seqbegin(&root->fs_info->profiles_lock);
1565
1566 all_avail = root->fs_info->avail_data_alloc_bits |
1567 root->fs_info->avail_system_alloc_bits |
1568 root->fs_info->avail_metadata_alloc_bits;
1569 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001570
Stefan Behrens8dabb742012-11-06 13:15:27 +01001571 num_devices = root->fs_info->fs_devices->num_devices;
1572 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1573 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1574 WARN_ON(num_devices < 1);
1575 num_devices--;
1576 }
1577 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1578
1579 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001580 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001581 goto out;
1582 }
1583
Stefan Behrens8dabb742012-11-06 13:15:27 +01001584 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001585 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001586 goto out;
1587 }
1588
David Woodhouse53b381b2013-01-29 18:40:14 -05001589 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1590 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001591 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001592 goto out;
1593 }
1594 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1595 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001596 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001597 goto out;
1598 }
1599
Chris Masondfe25022008-05-13 13:46:40 -04001600 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001601 struct list_head *devices;
1602 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001603
Chris Masondfe25022008-05-13 13:46:40 -04001604 device = NULL;
1605 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001606 /*
1607 * It is safe to read the devices since the volume_mutex
1608 * is held.
1609 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001610 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001611 if (tmp->in_fs_metadata &&
1612 !tmp->is_tgtdev_for_dev_replace &&
1613 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001614 device = tmp;
1615 break;
1616 }
1617 }
1618 bdev = NULL;
1619 bh = NULL;
1620 disk_super = NULL;
1621 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001622 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001623 goto out;
1624 }
Chris Masondfe25022008-05-13 13:46:40 -04001625 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001626 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001627 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001628 root->fs_info->bdev_holder, 0,
1629 &bdev, &bh);
1630 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001631 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001632 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001633 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001634 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001635 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001636 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001637 if (!device) {
1638 ret = -ENOENT;
1639 goto error_brelse;
1640 }
Chris Masondfe25022008-05-13 13:46:40 -04001641 }
Yan Zheng2b820322008-11-17 21:11:30 -05001642
Stefan Behrens63a212a2012-11-05 18:29:28 +01001643 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001644 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001645 goto error_brelse;
1646 }
1647
Yan Zheng2b820322008-11-17 21:11:30 -05001648 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001649 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001650 goto error_brelse;
1651 }
1652
1653 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001654 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001655 list_del_init(&device->dev_alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001656 device->fs_devices->rw_devices--;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001657 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001658 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001659 }
Chris Masona061fc82008-05-07 11:43:44 -04001660
Carey Underwoodd7901552013-03-04 16:37:06 -06001661 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001662 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001663 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001664 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001665 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001666
Stefan Behrens63a212a2012-11-05 18:29:28 +01001667 /*
1668 * TODO: the superblock still includes this device in its num_devices
1669 * counter although write_all_supers() is not locked out. This
1670 * could give a filesystem state which requires a degraded mount.
1671 */
Chris Masona061fc82008-05-07 11:43:44 -04001672 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1673 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001674 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001675
Yan Zheng2b820322008-11-17 21:11:30 -05001676 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001677 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001678
1679 /*
1680 * the device list mutex makes sure that we don't change
1681 * the device list while someone else is writing out all
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001682 * the device supers. Whoever is writing all supers, should
1683 * lock the device list mutex before getting the number of
1684 * devices in the super block (super_copy). Conversely,
1685 * whoever updates the number of devices in the super block
1686 * (super_copy) should hold the device list mutex.
Chris Masone5e9a522009-06-10 15:17:02 -04001687 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001688
1689 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001690 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001691 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001692
Yan Zhenge4404d62008-12-12 10:03:26 -05001693 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001694 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001695
Chris Masoncd02dca2010-12-13 14:56:23 -05001696 if (device->missing)
Miao Xie3a7d55c2014-07-16 18:38:01 +08001697 device->fs_devices->missing_devices--;
Chris Masoncd02dca2010-12-13 14:56:23 -05001698
Yan Zheng2b820322008-11-17 21:11:30 -05001699 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1700 struct btrfs_device, dev_list);
1701 if (device->bdev == root->fs_info->sb->s_bdev)
1702 root->fs_info->sb->s_bdev = next_device->bdev;
1703 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1704 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1705
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001706 if (device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001707 device->fs_devices->open_devices--;
Eric Sandeen0bfaa9c2014-07-07 12:34:49 -05001708 /* remove sysfs entry */
1709 btrfs_kobj_rm_device(root->fs_info, device);
1710 }
Anand Jain99994cd2014-06-03 11:36:00 +08001711
Xiao Guangrong1f781602011-04-20 10:09:16 +00001712 call_rcu(&device->rcu, free_device);
Yan Zhenge4404d62008-12-12 10:03:26 -05001713
David Sterba6c417612011-04-13 15:41:04 +02001714 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1715 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Filipe David Borba Mananad7306802013-08-09 15:41:36 +01001716 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001717
Xiao Guangrong1f781602011-04-20 10:09:16 +00001718 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001719 struct btrfs_fs_devices *fs_devices;
1720 fs_devices = root->fs_info->fs_devices;
1721 while (fs_devices) {
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001722 if (fs_devices->seed == cur_devices) {
1723 fs_devices->seed = cur_devices->seed;
Yan Zhenge4404d62008-12-12 10:03:26 -05001724 break;
Rickard Strandqvist8321cf22014-05-22 22:43:43 +02001725 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001726 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001727 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001728 cur_devices->seed = NULL;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001729 __btrfs_close_devices(cur_devices);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001730 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001731 }
1732
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001733 root->fs_info->num_tolerated_disk_barrier_failures =
1734 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1735
Yan Zheng2b820322008-11-17 21:11:30 -05001736 /*
1737 * at this point, the device is zero sized. We want to
1738 * remove it from the devices list and zero out the old super
1739 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001740 if (clear_super && disk_super) {
Anand Jain4d90d282014-04-06 12:59:07 +08001741 u64 bytenr;
1742 int i;
1743
Chris Masondfe25022008-05-13 13:46:40 -04001744 /* make sure this device isn't detected as part of
1745 * the FS anymore
1746 */
1747 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1748 set_buffer_dirty(bh);
1749 sync_dirty_buffer(bh);
Anand Jain4d90d282014-04-06 12:59:07 +08001750
1751 /* clear the mirror copies of super block on the disk
1752 * being removed, 0th copy is been taken care above and
1753 * the below would take of the rest
1754 */
1755 for (i = 1; i < BTRFS_SUPER_MIRROR_MAX; i++) {
1756 bytenr = btrfs_sb_offset(i);
1757 if (bytenr + BTRFS_SUPER_INFO_SIZE >=
1758 i_size_read(bdev->bd_inode))
1759 break;
1760
1761 brelse(bh);
1762 bh = __bread(bdev, bytenr / 4096,
1763 BTRFS_SUPER_INFO_SIZE);
1764 if (!bh)
1765 continue;
1766
1767 disk_super = (struct btrfs_super_block *)bh->b_data;
1768
1769 if (btrfs_super_bytenr(disk_super) != bytenr ||
1770 btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
1771 continue;
1772 }
1773 memset(&disk_super->magic, 0,
1774 sizeof(disk_super->magic));
1775 set_buffer_dirty(bh);
1776 sync_dirty_buffer(bh);
1777 }
Chris Masondfe25022008-05-13 13:46:40 -04001778 }
Chris Masona061fc82008-05-07 11:43:44 -04001779
Chris Masona061fc82008-05-07 11:43:44 -04001780 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001781
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001782 if (bdev) {
1783 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001784 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001785
Qu Wenruo5a1972b2014-04-16 17:02:32 +08001786 /* Update ctime/mtime for device path for libblkid */
1787 update_dev_time(device_path);
1788 }
1789
Chris Masona061fc82008-05-07 11:43:44 -04001790error_brelse:
1791 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001792 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001793 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001794out:
1795 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001796 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001797error_undo:
1798 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001799 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001800 list_add(&device->dev_alloc_list,
1801 &root->fs_info->fs_devices->alloc_list);
Miao Xiec3929c32014-09-03 21:35:47 +08001802 device->fs_devices->rw_devices++;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001803 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001804 }
1805 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001806}
1807
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001808void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1809 struct btrfs_device *srcdev)
Stefan Behrense93c89c2012-11-05 17:33:06 +01001810{
Anand Jaind51908c2014-08-13 14:24:19 +08001811 struct btrfs_fs_devices *fs_devices;
1812
Stefan Behrense93c89c2012-11-05 17:33:06 +01001813 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
Ilya Dryomov13572722013-10-02 20:41:01 +03001814
Anand Jain25e8e912014-08-20 10:56:56 +08001815 /*
1816 * in case of fs with no seed, srcdev->fs_devices will point
1817 * to fs_devices of fs_info. However when the dev being replaced is
1818 * a seed dev it will point to the seed's local fs_devices. In short
1819 * srcdev will have its correct fs_devices in both the cases.
1820 */
1821 fs_devices = srcdev->fs_devices;
Anand Jaind51908c2014-08-13 14:24:19 +08001822
Stefan Behrense93c89c2012-11-05 17:33:06 +01001823 list_del_rcu(&srcdev->dev_list);
1824 list_del_rcu(&srcdev->dev_alloc_list);
Anand Jaind51908c2014-08-13 14:24:19 +08001825 fs_devices->num_devices--;
Miao Xie82372bc2014-09-03 21:35:44 +08001826 if (srcdev->missing)
Anand Jaind51908c2014-08-13 14:24:19 +08001827 fs_devices->missing_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001828
Miao Xie82372bc2014-09-03 21:35:44 +08001829 if (srcdev->writeable) {
1830 fs_devices->rw_devices--;
1831 /* zero out the old super if it is writable */
1832 btrfs_scratch_superblock(srcdev);
Ilya Dryomov13572722013-10-02 20:41:01 +03001833 }
1834
Miao Xie82372bc2014-09-03 21:35:44 +08001835 if (srcdev->bdev)
Anand Jaind51908c2014-08-13 14:24:19 +08001836 fs_devices->open_devices--;
Qu Wenruo084b6e7c2014-10-30 16:52:31 +08001837}
1838
1839void btrfs_rm_dev_replace_free_srcdev(struct btrfs_fs_info *fs_info,
1840 struct btrfs_device *srcdev)
1841{
1842 struct btrfs_fs_devices *fs_devices = srcdev->fs_devices;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001843
Stefan Behrense93c89c2012-11-05 17:33:06 +01001844 call_rcu(&srcdev->rcu, free_device);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001845
1846 /*
1847 * unless fs_devices is seed fs, num_devices shouldn't go
1848 * zero
1849 */
1850 BUG_ON(!fs_devices->num_devices && !fs_devices->seeding);
1851
1852 /* if this is no devs we rather delete the fs_devices */
1853 if (!fs_devices->num_devices) {
1854 struct btrfs_fs_devices *tmp_fs_devices;
1855
1856 tmp_fs_devices = fs_info->fs_devices;
1857 while (tmp_fs_devices) {
1858 if (tmp_fs_devices->seed == fs_devices) {
1859 tmp_fs_devices->seed = fs_devices->seed;
1860 break;
1861 }
1862 tmp_fs_devices = tmp_fs_devices->seed;
1863 }
1864 fs_devices->seed = NULL;
Anand Jain8bef8402014-08-13 14:24:23 +08001865 __btrfs_close_devices(fs_devices);
1866 free_fs_devices(fs_devices);
Anand Jain94d5f0c2014-08-13 14:24:22 +08001867 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01001868}
1869
1870void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1871 struct btrfs_device *tgtdev)
1872{
1873 struct btrfs_device *next_device;
1874
Miao Xie67a2c452014-09-03 21:35:43 +08001875 mutex_lock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001876 WARN_ON(!tgtdev);
1877 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1878 if (tgtdev->bdev) {
1879 btrfs_scratch_superblock(tgtdev);
1880 fs_info->fs_devices->open_devices--;
1881 }
1882 fs_info->fs_devices->num_devices--;
Stefan Behrense93c89c2012-11-05 17:33:06 +01001883
1884 next_device = list_entry(fs_info->fs_devices->devices.next,
1885 struct btrfs_device, dev_list);
1886 if (tgtdev->bdev == fs_info->sb->s_bdev)
1887 fs_info->sb->s_bdev = next_device->bdev;
1888 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1889 fs_info->fs_devices->latest_bdev = next_device->bdev;
1890 list_del_rcu(&tgtdev->dev_list);
1891
1892 call_rcu(&tgtdev->rcu, free_device);
1893
1894 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
Miao Xie67a2c452014-09-03 21:35:43 +08001895 mutex_unlock(&uuid_mutex);
Stefan Behrense93c89c2012-11-05 17:33:06 +01001896}
1897
Eric Sandeen48a3b632013-04-25 20:41:01 +00001898static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1899 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001900{
1901 int ret = 0;
1902 struct btrfs_super_block *disk_super;
1903 u64 devid;
1904 u8 *dev_uuid;
1905 struct block_device *bdev;
1906 struct buffer_head *bh;
1907
1908 *device = NULL;
1909 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1910 root->fs_info->bdev_holder, 0, &bdev, &bh);
1911 if (ret)
1912 return ret;
1913 disk_super = (struct btrfs_super_block *)bh->b_data;
1914 devid = btrfs_stack_device_id(&disk_super->dev_item);
1915 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001916 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001917 disk_super->fsid);
1918 brelse(bh);
1919 if (!*device)
1920 ret = -ENOENT;
1921 blkdev_put(bdev, FMODE_READ);
1922 return ret;
1923}
1924
1925int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1926 char *device_path,
1927 struct btrfs_device **device)
1928{
1929 *device = NULL;
1930 if (strcmp(device_path, "missing") == 0) {
1931 struct list_head *devices;
1932 struct btrfs_device *tmp;
1933
1934 devices = &root->fs_info->fs_devices->devices;
1935 /*
1936 * It is safe to read the devices since the volume_mutex
1937 * is held by the caller.
1938 */
1939 list_for_each_entry(tmp, devices, dev_list) {
1940 if (tmp->in_fs_metadata && !tmp->bdev) {
1941 *device = tmp;
1942 break;
1943 }
1944 }
1945
1946 if (!*device) {
Frank Holtonefe120a2013-12-20 11:37:06 -05001947 btrfs_err(root->fs_info, "no missing device found");
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001948 return -ENOENT;
1949 }
1950
1951 return 0;
1952 } else {
1953 return btrfs_find_device_by_path(root, device_path, device);
1954 }
1955}
1956
Yan Zheng2b820322008-11-17 21:11:30 -05001957/*
1958 * does all the dirty work required for changing file system's UUID.
1959 */
Li Zefan125ccb02011-12-08 15:07:24 +08001960static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001961{
1962 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1963 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001964 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001965 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001966 struct btrfs_device *device;
1967 u64 super_flags;
1968
1969 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001970 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001971 return -EINVAL;
1972
Ilya Dryomov2208a372013-08-12 14:33:03 +03001973 seed_devices = __alloc_fs_devices();
1974 if (IS_ERR(seed_devices))
1975 return PTR_ERR(seed_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001976
Yan Zhenge4404d62008-12-12 10:03:26 -05001977 old_devices = clone_fs_devices(fs_devices);
1978 if (IS_ERR(old_devices)) {
1979 kfree(seed_devices);
1980 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001981 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001982
Yan Zheng2b820322008-11-17 21:11:30 -05001983 list_add(&old_devices->list, &fs_uuids);
1984
Yan Zhenge4404d62008-12-12 10:03:26 -05001985 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1986 seed_devices->opened = 1;
1987 INIT_LIST_HEAD(&seed_devices->devices);
1988 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001989 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001990
1991 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001992 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1993 synchronize_rcu);
Miao Xie2196d6e2014-09-03 21:35:41 +08001994 list_for_each_entry(device, &seed_devices->devices, dev_list)
Yan Zhenge4404d62008-12-12 10:03:26 -05001995 device->fs_devices = seed_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08001996
1997 lock_chunks(root);
1998 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1999 unlock_chunks(root);
Yan Zhenge4404d62008-12-12 10:03:26 -05002000
Yan Zheng2b820322008-11-17 21:11:30 -05002001 fs_devices->seeding = 0;
2002 fs_devices->num_devices = 0;
2003 fs_devices->open_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002004 fs_devices->missing_devices = 0;
Miao Xie69611ac2014-07-03 18:22:12 +08002005 fs_devices->rotating = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05002006 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002007
2008 generate_random_uuid(fs_devices->fsid);
2009 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2010 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
Filipe David Borba Mananaf7171752013-08-12 20:56:58 +01002011 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2012
Yan Zheng2b820322008-11-17 21:11:30 -05002013 super_flags = btrfs_super_flags(disk_super) &
2014 ~BTRFS_SUPER_FLAG_SEEDING;
2015 btrfs_set_super_flags(disk_super, super_flags);
2016
2017 return 0;
2018}
2019
2020/*
2021 * strore the expected generation for seed devices in device items.
2022 */
2023static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
2024 struct btrfs_root *root)
2025{
2026 struct btrfs_path *path;
2027 struct extent_buffer *leaf;
2028 struct btrfs_dev_item *dev_item;
2029 struct btrfs_device *device;
2030 struct btrfs_key key;
2031 u8 fs_uuid[BTRFS_UUID_SIZE];
2032 u8 dev_uuid[BTRFS_UUID_SIZE];
2033 u64 devid;
2034 int ret;
2035
2036 path = btrfs_alloc_path();
2037 if (!path)
2038 return -ENOMEM;
2039
2040 root = root->fs_info->chunk_root;
2041 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2042 key.offset = 0;
2043 key.type = BTRFS_DEV_ITEM_KEY;
2044
2045 while (1) {
2046 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2047 if (ret < 0)
2048 goto error;
2049
2050 leaf = path->nodes[0];
2051next_slot:
2052 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
2053 ret = btrfs_next_leaf(root, path);
2054 if (ret > 0)
2055 break;
2056 if (ret < 0)
2057 goto error;
2058 leaf = path->nodes[0];
2059 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02002060 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002061 continue;
2062 }
2063
2064 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2065 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
2066 key.type != BTRFS_DEV_ITEM_KEY)
2067 break;
2068
2069 dev_item = btrfs_item_ptr(leaf, path->slots[0],
2070 struct btrfs_dev_item);
2071 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02002072 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002073 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02002074 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05002075 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01002076 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
2077 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002078 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05002079
2080 if (device->fs_devices->seeding) {
2081 btrfs_set_device_generation(leaf, dev_item,
2082 device->generation);
2083 btrfs_mark_buffer_dirty(leaf);
2084 }
2085
2086 path->slots[0]++;
2087 goto next_slot;
2088 }
2089 ret = 0;
2090error:
2091 btrfs_free_path(path);
2092 return ret;
2093}
2094
Chris Mason788f20e2008-04-28 15:29:42 -04002095int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2096{
Josef Bacikd5e20032011-08-04 14:52:27 +00002097 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04002098 struct btrfs_trans_handle *trans;
2099 struct btrfs_device *device;
2100 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04002101 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05002102 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04002103 struct rcu_string *name;
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002104 u64 tmp;
Yan Zheng2b820322008-11-17 21:11:30 -05002105 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04002106 int ret = 0;
2107
Yan Zheng2b820322008-11-17 21:11:30 -05002108 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08002109 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04002110
Li Zefana5d16332011-12-07 20:08:40 -05002111 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01002112 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00002113 if (IS_ERR(bdev))
2114 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04002115
Yan Zheng2b820322008-11-17 21:11:30 -05002116 if (root->fs_info->fs_devices->seeding) {
2117 seeding_dev = 1;
2118 down_write(&sb->s_umount);
2119 mutex_lock(&uuid_mutex);
2120 }
2121
Chris Mason8c8bee1d2008-09-29 11:19:10 -04002122 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04002123
Chris Mason788f20e2008-04-28 15:29:42 -04002124 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00002125
2126 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002127 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04002128 if (device->bdev == bdev) {
2129 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00002130 mutex_unlock(
2131 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05002132 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002133 }
2134 }
Liu Bod25628b2012-11-14 14:35:30 +00002135 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002136
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002137 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
2138 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04002139 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002140 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002141 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002142 }
2143
Josef Bacik606686e2012-06-04 14:03:51 -04002144 name = rcu_string_strdup(device_path, GFP_NOFS);
2145 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04002146 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002147 ret = -ENOMEM;
2148 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04002149 }
Josef Bacik606686e2012-06-04 14:03:51 -04002150 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05002151
Yan, Zhenga22285a2010-05-16 10:48:46 -04002152 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002153 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04002154 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002155 kfree(device);
2156 ret = PTR_ERR(trans);
2157 goto error;
2158 }
2159
Josef Bacikd5e20032011-08-04 14:52:27 +00002160 q = bdev_get_queue(bdev);
2161 if (blk_queue_discard(q))
2162 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002163 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05002164 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04002165 device->io_width = root->sectorsize;
2166 device->io_align = root->sectorsize;
2167 device->sector_size = root->sectorsize;
2168 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04002169 device->disk_total_bytes = device->total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08002170 device->commit_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04002171 device->dev_root = root->fs_info->dev_root;
2172 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002173 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002174 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002175 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002176 device->dev_stats_valid = 1;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002177 set_blocksize(device->bdev, 4096);
2178
Yan Zheng2b820322008-11-17 21:11:30 -05002179 if (seeding_dev) {
2180 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002181 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002182 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002183 }
2184
2185 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002186
Chris Masone5e9a522009-06-10 15:17:02 -04002187 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Miao Xie2196d6e2014-09-03 21:35:41 +08002188 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002189 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002190 list_add(&device->dev_alloc_list,
2191 &root->fs_info->fs_devices->alloc_list);
2192 root->fs_info->fs_devices->num_devices++;
2193 root->fs_info->fs_devices->open_devices++;
2194 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002195 root->fs_info->fs_devices->total_devices++;
Yan Zheng2b820322008-11-17 21:11:30 -05002196 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2197
Josef Bacik2bf64752011-09-26 17:12:22 -04002198 spin_lock(&root->fs_info->free_chunk_lock);
2199 root->fs_info->free_chunk_space += device->total_bytes;
2200 spin_unlock(&root->fs_info->free_chunk_lock);
2201
Chris Masonc2898112009-06-10 09:51:32 -04002202 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2203 root->fs_info->fs_devices->rotating = 1;
2204
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002205 tmp = btrfs_super_total_bytes(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002206 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002207 tmp + device->total_bytes);
Chris Mason788f20e2008-04-28 15:29:42 -04002208
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002209 tmp = btrfs_super_num_devices(root->fs_info->super_copy);
David Sterba6c417612011-04-13 15:41:04 +02002210 btrfs_set_super_num_devices(root->fs_info->super_copy,
Anand Jain3c1dbdf2014-08-20 10:54:17 +08002211 tmp + 1);
Anand Jain0d393762014-06-03 11:36:01 +08002212
2213 /* add sysfs device entry */
2214 btrfs_kobj_add_device(root->fs_info, device);
2215
Miao Xie2196d6e2014-09-03 21:35:41 +08002216 /*
2217 * we've got more storage, clear any full flags on the space
2218 * infos
2219 */
2220 btrfs_clear_space_info_full(root->fs_info);
2221
2222 unlock_chunks(root);
Chris Masone5e9a522009-06-10 15:17:02 -04002223 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002224
Yan Zheng2b820322008-11-17 21:11:30 -05002225 if (seeding_dev) {
Miao Xie2196d6e2014-09-03 21:35:41 +08002226 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05002227 ret = init_first_rw_device(trans, root, device);
Miao Xie2196d6e2014-09-03 21:35:41 +08002228 unlock_chunks(root);
David Sterba005d6422012-09-18 07:52:32 -06002229 if (ret) {
2230 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002231 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002232 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002233 }
2234
2235 ret = btrfs_add_device(trans, root, device);
2236 if (ret) {
2237 btrfs_abort_transaction(trans, root, ret);
2238 goto error_trans;
2239 }
2240
2241 if (seeding_dev) {
2242 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2243
Yan Zheng2b820322008-11-17 21:11:30 -05002244 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002245 if (ret) {
2246 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002247 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002248 }
Anand Jainb2373f22014-06-03 11:36:03 +08002249
2250 /* Sprouting would change fsid of the mounted root,
2251 * so rename the fsid on the sysfs
2252 */
2253 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2254 root->fs_info->fsid);
Anand Jain2e7910d2015-03-10 06:38:29 +08002255 if (kobject_rename(&root->fs_info->fs_devices->super_kobj,
2256 fsid_buf))
Anand Jainb2373f22014-06-03 11:36:03 +08002257 goto error_trans;
Yan Zheng2b820322008-11-17 21:11:30 -05002258 }
2259
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002260 root->fs_info->num_tolerated_disk_barrier_failures =
2261 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002262 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002263
2264 if (seeding_dev) {
2265 mutex_unlock(&uuid_mutex);
2266 up_write(&sb->s_umount);
2267
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002268 if (ret) /* transaction commit */
2269 return ret;
2270
Yan Zheng2b820322008-11-17 21:11:30 -05002271 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002272 if (ret < 0)
2273 btrfs_error(root->fs_info, ret,
2274 "Failed to relocate sys chunks after "
2275 "device initialization. This can be fixed "
2276 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002277 trans = btrfs_attach_transaction(root);
2278 if (IS_ERR(trans)) {
2279 if (PTR_ERR(trans) == -ENOENT)
2280 return 0;
2281 return PTR_ERR(trans);
2282 }
2283 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002284 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002285
Qu Wenruo5a1972b2014-04-16 17:02:32 +08002286 /* Update ctime/mtime for libblkid */
2287 update_dev_time(device_path);
Chris Mason788f20e2008-04-28 15:29:42 -04002288 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002289
2290error_trans:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002292 rcu_string_free(device->name);
Anand Jain0d393762014-06-03 11:36:01 +08002293 btrfs_kobj_rm_device(root->fs_info, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002294 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002295error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002296 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002297 if (seeding_dev) {
2298 mutex_unlock(&uuid_mutex);
2299 up_write(&sb->s_umount);
2300 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002301 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002302}
2303
Stefan Behrense93c89c2012-11-05 17:33:06 +01002304int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
Miao Xie1c433662014-09-03 21:35:32 +08002305 struct btrfs_device *srcdev,
Stefan Behrense93c89c2012-11-05 17:33:06 +01002306 struct btrfs_device **device_out)
2307{
2308 struct request_queue *q;
2309 struct btrfs_device *device;
2310 struct block_device *bdev;
2311 struct btrfs_fs_info *fs_info = root->fs_info;
2312 struct list_head *devices;
2313 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002314 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002315 int ret = 0;
2316
2317 *device_out = NULL;
Miao Xie1c433662014-09-03 21:35:32 +08002318 if (fs_info->fs_devices->seeding) {
2319 btrfs_err(fs_info, "the filesystem is a seed filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002320 return -EINVAL;
Miao Xie1c433662014-09-03 21:35:32 +08002321 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002322
2323 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2324 fs_info->bdev_holder);
Miao Xie1c433662014-09-03 21:35:32 +08002325 if (IS_ERR(bdev)) {
2326 btrfs_err(fs_info, "target device %s is invalid!", device_path);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002327 return PTR_ERR(bdev);
Miao Xie1c433662014-09-03 21:35:32 +08002328 }
Stefan Behrense93c89c2012-11-05 17:33:06 +01002329
2330 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2331
2332 devices = &fs_info->fs_devices->devices;
2333 list_for_each_entry(device, devices, dev_list) {
2334 if (device->bdev == bdev) {
Miao Xie1c433662014-09-03 21:35:32 +08002335 btrfs_err(fs_info, "target device is in the filesystem!");
Stefan Behrense93c89c2012-11-05 17:33:06 +01002336 ret = -EEXIST;
2337 goto error;
2338 }
2339 }
2340
Miao Xie1c433662014-09-03 21:35:32 +08002341
Miao Xie7cc8e582014-09-03 21:35:38 +08002342 if (i_size_read(bdev->bd_inode) <
2343 btrfs_device_get_total_bytes(srcdev)) {
Miao Xie1c433662014-09-03 21:35:32 +08002344 btrfs_err(fs_info, "target device is smaller than source device!");
2345 ret = -EINVAL;
2346 goto error;
2347 }
2348
2349
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002350 device = btrfs_alloc_device(NULL, &devid, NULL);
2351 if (IS_ERR(device)) {
2352 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002353 goto error;
2354 }
2355
2356 name = rcu_string_strdup(device_path, GFP_NOFS);
2357 if (!name) {
2358 kfree(device);
2359 ret = -ENOMEM;
2360 goto error;
2361 }
2362 rcu_assign_pointer(device->name, name);
2363
2364 q = bdev_get_queue(bdev);
2365 if (blk_queue_discard(q))
2366 device->can_discard = 1;
2367 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2368 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002369 device->generation = 0;
2370 device->io_width = root->sectorsize;
2371 device->io_align = root->sectorsize;
2372 device->sector_size = root->sectorsize;
Miao Xie7cc8e582014-09-03 21:35:38 +08002373 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2374 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2375 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
Miao Xie935e5cc2014-09-03 21:35:33 +08002376 ASSERT(list_empty(&srcdev->resized_list));
2377 device->commit_total_bytes = srcdev->commit_total_bytes;
Miao Xiece7213c2014-09-03 21:35:34 +08002378 device->commit_bytes_used = device->bytes_used;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002379 device->dev_root = fs_info->dev_root;
2380 device->bdev = bdev;
2381 device->in_fs_metadata = 1;
2382 device->is_tgtdev_for_dev_replace = 1;
2383 device->mode = FMODE_EXCL;
Stefan Behrens27087f32013-10-11 15:20:42 +02002384 device->dev_stats_valid = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002385 set_blocksize(device->bdev, 4096);
2386 device->fs_devices = fs_info->fs_devices;
2387 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2388 fs_info->fs_devices->num_devices++;
2389 fs_info->fs_devices->open_devices++;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002390 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2391
2392 *device_out = device;
2393 return ret;
2394
2395error:
2396 blkdev_put(bdev, FMODE_EXCL);
2397 return ret;
2398}
2399
2400void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2401 struct btrfs_device *tgtdev)
2402{
2403 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2404 tgtdev->io_width = fs_info->dev_root->sectorsize;
2405 tgtdev->io_align = fs_info->dev_root->sectorsize;
2406 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2407 tgtdev->dev_root = fs_info->dev_root;
2408 tgtdev->in_fs_metadata = 1;
2409}
2410
Chris Masond3977122009-01-05 21:25:51 -05002411static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2412 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002413{
2414 int ret;
2415 struct btrfs_path *path;
2416 struct btrfs_root *root;
2417 struct btrfs_dev_item *dev_item;
2418 struct extent_buffer *leaf;
2419 struct btrfs_key key;
2420
2421 root = device->dev_root->fs_info->chunk_root;
2422
2423 path = btrfs_alloc_path();
2424 if (!path)
2425 return -ENOMEM;
2426
2427 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2428 key.type = BTRFS_DEV_ITEM_KEY;
2429 key.offset = device->devid;
2430
2431 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2432 if (ret < 0)
2433 goto out;
2434
2435 if (ret > 0) {
2436 ret = -ENOENT;
2437 goto out;
2438 }
2439
2440 leaf = path->nodes[0];
2441 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2442
2443 btrfs_set_device_id(leaf, dev_item, device->devid);
2444 btrfs_set_device_type(leaf, dev_item, device->type);
2445 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2446 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2447 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Miao Xie7cc8e582014-09-03 21:35:38 +08002448 btrfs_set_device_total_bytes(leaf, dev_item,
2449 btrfs_device_get_disk_total_bytes(device));
2450 btrfs_set_device_bytes_used(leaf, dev_item,
2451 btrfs_device_get_bytes_used(device));
Chris Mason0b86a832008-03-24 15:01:56 -04002452 btrfs_mark_buffer_dirty(leaf);
2453
2454out:
2455 btrfs_free_path(path);
2456 return ret;
2457}
2458
Miao Xie2196d6e2014-09-03 21:35:41 +08002459int btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002460 struct btrfs_device *device, u64 new_size)
2461{
2462 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002463 device->dev_root->fs_info->super_copy;
Miao Xie935e5cc2014-09-03 21:35:33 +08002464 struct btrfs_fs_devices *fs_devices;
Miao Xie2196d6e2014-09-03 21:35:41 +08002465 u64 old_total;
2466 u64 diff;
Chris Mason8f18cf12008-04-25 16:53:30 -04002467
Yan Zheng2b820322008-11-17 21:11:30 -05002468 if (!device->writeable)
2469 return -EACCES;
Miao Xie2196d6e2014-09-03 21:35:41 +08002470
2471 lock_chunks(device->dev_root);
2472 old_total = btrfs_super_total_bytes(super_copy);
2473 diff = new_size - device->total_bytes;
2474
Stefan Behrens63a212a2012-11-05 18:29:28 +01002475 if (new_size <= device->total_bytes ||
Miao Xie2196d6e2014-09-03 21:35:41 +08002476 device->is_tgtdev_for_dev_replace) {
2477 unlock_chunks(device->dev_root);
Yan Zheng2b820322008-11-17 21:11:30 -05002478 return -EINVAL;
Miao Xie2196d6e2014-09-03 21:35:41 +08002479 }
Yan Zheng2b820322008-11-17 21:11:30 -05002480
Miao Xie935e5cc2014-09-03 21:35:33 +08002481 fs_devices = device->dev_root->fs_info->fs_devices;
Chris Mason8f18cf12008-04-25 16:53:30 -04002482
2483 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002484 device->fs_devices->total_rw_bytes += diff;
2485
Miao Xie7cc8e582014-09-03 21:35:38 +08002486 btrfs_device_set_total_bytes(device, new_size);
2487 btrfs_device_set_disk_total_bytes(device, new_size);
Chris Mason4184ea72009-03-10 12:39:20 -04002488 btrfs_clear_space_info_full(device->dev_root->fs_info);
Miao Xie935e5cc2014-09-03 21:35:33 +08002489 if (list_empty(&device->resized_list))
2490 list_add_tail(&device->resized_list,
2491 &fs_devices->resized_devices);
Miao Xie2196d6e2014-09-03 21:35:41 +08002492 unlock_chunks(device->dev_root);
Chris Mason4184ea72009-03-10 12:39:20 -04002493
Chris Mason8f18cf12008-04-25 16:53:30 -04002494 return btrfs_update_device(trans, device);
2495}
2496
2497static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
Zhao Leia688a042015-02-09 20:31:44 +08002498 struct btrfs_root *root, u64 chunk_objectid,
Chris Mason8f18cf12008-04-25 16:53:30 -04002499 u64 chunk_offset)
2500{
2501 int ret;
2502 struct btrfs_path *path;
2503 struct btrfs_key key;
2504
2505 root = root->fs_info->chunk_root;
2506 path = btrfs_alloc_path();
2507 if (!path)
2508 return -ENOMEM;
2509
2510 key.objectid = chunk_objectid;
2511 key.offset = chunk_offset;
2512 key.type = BTRFS_CHUNK_ITEM_KEY;
2513
2514 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002515 if (ret < 0)
2516 goto out;
2517 else if (ret > 0) { /* Logic error or corruption */
2518 btrfs_error(root->fs_info, -ENOENT,
2519 "Failed lookup while freeing chunk.");
2520 ret = -ENOENT;
2521 goto out;
2522 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002523
2524 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002525 if (ret < 0)
2526 btrfs_error(root->fs_info, ret,
2527 "Failed to delete chunk item.");
2528out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002529 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002530 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002531}
2532
Christoph Hellwigb2950862008-12-02 09:54:17 -05002533static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002534 chunk_offset)
2535{
David Sterba6c417612011-04-13 15:41:04 +02002536 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002537 struct btrfs_disk_key *disk_key;
2538 struct btrfs_chunk *chunk;
2539 u8 *ptr;
2540 int ret = 0;
2541 u32 num_stripes;
2542 u32 array_size;
2543 u32 len = 0;
2544 u32 cur;
2545 struct btrfs_key key;
2546
Miao Xie2196d6e2014-09-03 21:35:41 +08002547 lock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002548 array_size = btrfs_super_sys_array_size(super_copy);
2549
2550 ptr = super_copy->sys_chunk_array;
2551 cur = 0;
2552
2553 while (cur < array_size) {
2554 disk_key = (struct btrfs_disk_key *)ptr;
2555 btrfs_disk_key_to_cpu(&key, disk_key);
2556
2557 len = sizeof(*disk_key);
2558
2559 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2560 chunk = (struct btrfs_chunk *)(ptr + len);
2561 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2562 len += btrfs_chunk_item_size(num_stripes);
2563 } else {
2564 ret = -EIO;
2565 break;
2566 }
2567 if (key.objectid == chunk_objectid &&
2568 key.offset == chunk_offset) {
2569 memmove(ptr, ptr + len, array_size - (cur + len));
2570 array_size -= len;
2571 btrfs_set_super_sys_array_size(super_copy, array_size);
2572 } else {
2573 ptr += len;
2574 cur += len;
2575 }
2576 }
Miao Xie2196d6e2014-09-03 21:35:41 +08002577 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002578 return ret;
2579}
2580
Josef Bacik47ab2a62014-09-18 11:20:02 -04002581int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2582 struct btrfs_root *root, u64 chunk_offset)
2583{
2584 struct extent_map_tree *em_tree;
2585 struct extent_map *em;
2586 struct btrfs_root *extent_root = root->fs_info->extent_root;
2587 struct map_lookup *map;
2588 u64 dev_extent_len = 0;
2589 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
Josef Bacik47ab2a62014-09-18 11:20:02 -04002590 int i, ret = 0;
2591
2592 /* Just in case */
2593 root = root->fs_info->chunk_root;
2594 em_tree = &root->fs_info->mapping_tree.map_tree;
2595
2596 read_lock(&em_tree->lock);
2597 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
2598 read_unlock(&em_tree->lock);
2599
2600 if (!em || em->start > chunk_offset ||
2601 em->start + em->len < chunk_offset) {
2602 /*
2603 * This is a logic error, but we don't want to just rely on the
2604 * user having built with ASSERT enabled, so if ASSERT doens't
2605 * do anything we still error out.
2606 */
2607 ASSERT(0);
2608 if (em)
2609 free_extent_map(em);
2610 return -EINVAL;
2611 }
2612 map = (struct map_lookup *)em->bdev;
2613
2614 for (i = 0; i < map->num_stripes; i++) {
2615 struct btrfs_device *device = map->stripes[i].dev;
2616 ret = btrfs_free_dev_extent(trans, device,
2617 map->stripes[i].physical,
2618 &dev_extent_len);
2619 if (ret) {
2620 btrfs_abort_transaction(trans, root, ret);
2621 goto out;
2622 }
2623
2624 if (device->bytes_used > 0) {
2625 lock_chunks(root);
2626 btrfs_device_set_bytes_used(device,
2627 device->bytes_used - dev_extent_len);
2628 spin_lock(&root->fs_info->free_chunk_lock);
2629 root->fs_info->free_chunk_space += dev_extent_len;
2630 spin_unlock(&root->fs_info->free_chunk_lock);
2631 btrfs_clear_space_info_full(root->fs_info);
2632 unlock_chunks(root);
2633 }
2634
2635 if (map->stripes[i].dev) {
2636 ret = btrfs_update_device(trans, map->stripes[i].dev);
2637 if (ret) {
2638 btrfs_abort_transaction(trans, root, ret);
2639 goto out;
2640 }
2641 }
2642 }
Zhao Leia688a042015-02-09 20:31:44 +08002643 ret = btrfs_free_chunk(trans, root, chunk_objectid, chunk_offset);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002644 if (ret) {
2645 btrfs_abort_transaction(trans, root, ret);
2646 goto out;
2647 }
2648
2649 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2650
2651 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2652 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2653 if (ret) {
2654 btrfs_abort_transaction(trans, root, ret);
2655 goto out;
2656 }
2657 }
2658
Filipe Manana04216822014-11-27 21:14:15 +00002659 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset, em);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002660 if (ret) {
2661 btrfs_abort_transaction(trans, extent_root, ret);
2662 goto out;
2663 }
2664
Josef Bacik47ab2a62014-09-18 11:20:02 -04002665out:
2666 /* once for us */
2667 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002668 return ret;
2669}
2670
Christoph Hellwigb2950862008-12-02 09:54:17 -05002671static int btrfs_relocate_chunk(struct btrfs_root *root,
Zhao Leia688a042015-02-09 20:31:44 +08002672 u64 chunk_objectid,
2673 u64 chunk_offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04002674{
Chris Mason8f18cf12008-04-25 16:53:30 -04002675 struct btrfs_root *extent_root;
2676 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002677 int ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002678
2679 root = root->fs_info->chunk_root;
2680 extent_root = root->fs_info->extent_root;
Chris Mason8f18cf12008-04-25 16:53:30 -04002681
Josef Bacikba1bf482009-09-11 16:11:19 -04002682 ret = btrfs_can_relocate(extent_root, chunk_offset);
2683 if (ret)
2684 return -ENOSPC;
2685
Chris Mason8f18cf12008-04-25 16:53:30 -04002686 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002687 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002688 if (ret)
2689 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002690
Yan, Zhenga22285a2010-05-16 10:48:46 -04002691 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002692 if (IS_ERR(trans)) {
2693 ret = PTR_ERR(trans);
2694 btrfs_std_error(root->fs_info, ret);
2695 return ret;
2696 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002697
2698 /*
2699 * step two, delete the device extents and the
2700 * chunk tree entries
2701 */
Josef Bacik47ab2a62014-09-18 11:20:02 -04002702 ret = btrfs_remove_chunk(trans, root, chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002703 btrfs_end_transaction(trans, root);
Josef Bacik47ab2a62014-09-18 11:20:02 -04002704 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002705}
2706
Yan Zheng2b820322008-11-17 21:11:30 -05002707static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2708{
2709 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2710 struct btrfs_path *path;
2711 struct extent_buffer *leaf;
2712 struct btrfs_chunk *chunk;
2713 struct btrfs_key key;
2714 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05002715 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002716 bool retried = false;
2717 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002718 int ret;
2719
2720 path = btrfs_alloc_path();
2721 if (!path)
2722 return -ENOMEM;
2723
Josef Bacikba1bf482009-09-11 16:11:19 -04002724again:
Yan Zheng2b820322008-11-17 21:11:30 -05002725 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2726 key.offset = (u64)-1;
2727 key.type = BTRFS_CHUNK_ITEM_KEY;
2728
2729 while (1) {
2730 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2731 if (ret < 0)
2732 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002733 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002734
2735 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2736 key.type);
2737 if (ret < 0)
2738 goto error;
2739 if (ret > 0)
2740 break;
2741
2742 leaf = path->nodes[0];
2743 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2744
2745 chunk = btrfs_item_ptr(leaf, path->slots[0],
2746 struct btrfs_chunk);
2747 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002748 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002749
2750 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
Zhao Leia688a042015-02-09 20:31:44 +08002751 ret = btrfs_relocate_chunk(chunk_root,
Yan Zheng2b820322008-11-17 21:11:30 -05002752 found_key.objectid,
2753 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002754 if (ret == -ENOSPC)
2755 failed++;
HIMANGI SARAOGI14586652014-07-09 03:51:41 +05302756 else
2757 BUG_ON(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05002758 }
2759
2760 if (found_key.offset == 0)
2761 break;
2762 key.offset = found_key.offset - 1;
2763 }
2764 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002765 if (failed && !retried) {
2766 failed = 0;
2767 retried = true;
2768 goto again;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302769 } else if (WARN_ON(failed && retried)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04002770 ret = -ENOSPC;
2771 }
Yan Zheng2b820322008-11-17 21:11:30 -05002772error:
2773 btrfs_free_path(path);
2774 return ret;
2775}
2776
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002777static int insert_balance_item(struct btrfs_root *root,
2778 struct btrfs_balance_control *bctl)
2779{
2780 struct btrfs_trans_handle *trans;
2781 struct btrfs_balance_item *item;
2782 struct btrfs_disk_balance_args disk_bargs;
2783 struct btrfs_path *path;
2784 struct extent_buffer *leaf;
2785 struct btrfs_key key;
2786 int ret, err;
2787
2788 path = btrfs_alloc_path();
2789 if (!path)
2790 return -ENOMEM;
2791
2792 trans = btrfs_start_transaction(root, 0);
2793 if (IS_ERR(trans)) {
2794 btrfs_free_path(path);
2795 return PTR_ERR(trans);
2796 }
2797
2798 key.objectid = BTRFS_BALANCE_OBJECTID;
2799 key.type = BTRFS_BALANCE_ITEM_KEY;
2800 key.offset = 0;
2801
2802 ret = btrfs_insert_empty_item(trans, root, path, &key,
2803 sizeof(*item));
2804 if (ret)
2805 goto out;
2806
2807 leaf = path->nodes[0];
2808 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2809
2810 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2811
2812 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2813 btrfs_set_balance_data(leaf, item, &disk_bargs);
2814 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2815 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2816 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2817 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2818
2819 btrfs_set_balance_flags(leaf, item, bctl->flags);
2820
2821 btrfs_mark_buffer_dirty(leaf);
2822out:
2823 btrfs_free_path(path);
2824 err = btrfs_commit_transaction(trans, root);
2825 if (err && !ret)
2826 ret = err;
2827 return ret;
2828}
2829
2830static int del_balance_item(struct btrfs_root *root)
2831{
2832 struct btrfs_trans_handle *trans;
2833 struct btrfs_path *path;
2834 struct btrfs_key key;
2835 int ret, err;
2836
2837 path = btrfs_alloc_path();
2838 if (!path)
2839 return -ENOMEM;
2840
2841 trans = btrfs_start_transaction(root, 0);
2842 if (IS_ERR(trans)) {
2843 btrfs_free_path(path);
2844 return PTR_ERR(trans);
2845 }
2846
2847 key.objectid = BTRFS_BALANCE_OBJECTID;
2848 key.type = BTRFS_BALANCE_ITEM_KEY;
2849 key.offset = 0;
2850
2851 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2852 if (ret < 0)
2853 goto out;
2854 if (ret > 0) {
2855 ret = -ENOENT;
2856 goto out;
2857 }
2858
2859 ret = btrfs_del_item(trans, root, path);
2860out:
2861 btrfs_free_path(path);
2862 err = btrfs_commit_transaction(trans, root);
2863 if (err && !ret)
2864 ret = err;
2865 return ret;
2866}
2867
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002868/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002869 * This is a heuristic used to reduce the number of chunks balanced on
2870 * resume after balance was interrupted.
2871 */
2872static void update_balance_args(struct btrfs_balance_control *bctl)
2873{
2874 /*
2875 * Turn on soft mode for chunk types that were being converted.
2876 */
2877 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2878 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2879 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2880 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2881 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2882 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2883
2884 /*
2885 * Turn on usage filter if is not already used. The idea is
2886 * that chunks that we have already balanced should be
2887 * reasonably full. Don't do it for chunks that are being
2888 * converted - that will keep us from relocating unconverted
2889 * (albeit full) chunks.
2890 */
2891 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2892 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2893 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2894 bctl->data.usage = 90;
2895 }
2896 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2897 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2898 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2899 bctl->sys.usage = 90;
2900 }
2901 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2902 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2903 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2904 bctl->meta.usage = 90;
2905 }
2906}
2907
2908/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002909 * Should be called with both balance and volume mutexes held to
2910 * serialize other volume operations (add_dev/rm_dev/resize) with
2911 * restriper. Same goes for unset_balance_control.
2912 */
2913static void set_balance_control(struct btrfs_balance_control *bctl)
2914{
2915 struct btrfs_fs_info *fs_info = bctl->fs_info;
2916
2917 BUG_ON(fs_info->balance_ctl);
2918
2919 spin_lock(&fs_info->balance_lock);
2920 fs_info->balance_ctl = bctl;
2921 spin_unlock(&fs_info->balance_lock);
2922}
2923
2924static void unset_balance_control(struct btrfs_fs_info *fs_info)
2925{
2926 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2927
2928 BUG_ON(!fs_info->balance_ctl);
2929
2930 spin_lock(&fs_info->balance_lock);
2931 fs_info->balance_ctl = NULL;
2932 spin_unlock(&fs_info->balance_lock);
2933
2934 kfree(bctl);
2935}
2936
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002937/*
2938 * Balance filters. Return 1 if chunk should be filtered out
2939 * (should not be balanced).
2940 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002941static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002942 struct btrfs_balance_args *bargs)
2943{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002944 chunk_type = chunk_to_extended(chunk_type) &
2945 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002946
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002947 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002948 return 0;
2949
2950 return 1;
2951}
2952
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002953static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2954 struct btrfs_balance_args *bargs)
2955{
2956 struct btrfs_block_group_cache *cache;
2957 u64 chunk_used, user_thresh;
2958 int ret = 1;
2959
2960 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2961 chunk_used = btrfs_block_group_used(&cache->item);
2962
Ilya Dryomova105bb82013-01-21 15:15:56 +02002963 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002964 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002965 else if (bargs->usage > 100)
2966 user_thresh = cache->key.offset;
2967 else
2968 user_thresh = div_factor_fine(cache->key.offset,
2969 bargs->usage);
2970
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002971 if (chunk_used < user_thresh)
2972 ret = 0;
2973
2974 btrfs_put_block_group(cache);
2975 return ret;
2976}
2977
Ilya Dryomov409d4042012-01-16 22:04:47 +02002978static int chunk_devid_filter(struct extent_buffer *leaf,
2979 struct btrfs_chunk *chunk,
2980 struct btrfs_balance_args *bargs)
2981{
2982 struct btrfs_stripe *stripe;
2983 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2984 int i;
2985
2986 for (i = 0; i < num_stripes; i++) {
2987 stripe = btrfs_stripe_nr(chunk, i);
2988 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2989 return 0;
2990 }
2991
2992 return 1;
2993}
2994
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002995/* [pstart, pend) */
2996static int chunk_drange_filter(struct extent_buffer *leaf,
2997 struct btrfs_chunk *chunk,
2998 u64 chunk_offset,
2999 struct btrfs_balance_args *bargs)
3000{
3001 struct btrfs_stripe *stripe;
3002 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
3003 u64 stripe_offset;
3004 u64 stripe_length;
3005 int factor;
3006 int i;
3007
3008 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
3009 return 0;
3010
3011 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05003012 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
3013 factor = num_stripes / 2;
3014 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
3015 factor = num_stripes - 1;
3016 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
3017 factor = num_stripes - 2;
3018 } else {
3019 factor = num_stripes;
3020 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003021
3022 for (i = 0; i < num_stripes; i++) {
3023 stripe = btrfs_stripe_nr(chunk, i);
3024 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
3025 continue;
3026
3027 stripe_offset = btrfs_stripe_offset(leaf, stripe);
3028 stripe_length = btrfs_chunk_length(leaf, chunk);
David Sterbab8b93ad2015-01-16 17:26:13 +01003029 stripe_length = div_u64(stripe_length, factor);
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003030
3031 if (stripe_offset < bargs->pend &&
3032 stripe_offset + stripe_length > bargs->pstart)
3033 return 0;
3034 }
3035
3036 return 1;
3037}
3038
Ilya Dryomovea671762012-01-16 22:04:48 +02003039/* [vstart, vend) */
3040static int chunk_vrange_filter(struct extent_buffer *leaf,
3041 struct btrfs_chunk *chunk,
3042 u64 chunk_offset,
3043 struct btrfs_balance_args *bargs)
3044{
3045 if (chunk_offset < bargs->vend &&
3046 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
3047 /* at least part of the chunk is inside this vrange */
3048 return 0;
3049
3050 return 1;
3051}
3052
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003053static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003054 struct btrfs_balance_args *bargs)
3055{
3056 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
3057 return 0;
3058
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003059 chunk_type = chunk_to_extended(chunk_type) &
3060 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003061
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003062 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003063 return 1;
3064
3065 return 0;
3066}
3067
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003068static int should_balance_chunk(struct btrfs_root *root,
3069 struct extent_buffer *leaf,
3070 struct btrfs_chunk *chunk, u64 chunk_offset)
3071{
3072 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
3073 struct btrfs_balance_args *bargs = NULL;
3074 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3075
3076 /* type filter */
3077 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
3078 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
3079 return 0;
3080 }
3081
3082 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
3083 bargs = &bctl->data;
3084 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
3085 bargs = &bctl->sys;
3086 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
3087 bargs = &bctl->meta;
3088
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02003089 /* profiles filter */
3090 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
3091 chunk_profiles_filter(chunk_type, bargs)) {
3092 return 0;
3093 }
3094
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02003095 /* usage filter */
3096 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3097 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
3098 return 0;
3099 }
3100
Ilya Dryomov409d4042012-01-16 22:04:47 +02003101 /* devid filter */
3102 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
3103 chunk_devid_filter(leaf, chunk, bargs)) {
3104 return 0;
3105 }
3106
Ilya Dryomov94e60d52012-01-16 22:04:48 +02003107 /* drange filter, makes sense only with devid filter */
3108 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
3109 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
3110 return 0;
3111 }
3112
Ilya Dryomovea671762012-01-16 22:04:48 +02003113 /* vrange filter */
3114 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
3115 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
3116 return 0;
3117 }
3118
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02003119 /* soft profile changing mode */
3120 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
3121 chunk_soft_convert_filter(chunk_type, bargs)) {
3122 return 0;
3123 }
3124
David Sterba7d824b62014-05-07 17:37:51 +02003125 /*
3126 * limited by count, must be the last filter
3127 */
3128 if ((bargs->flags & BTRFS_BALANCE_ARGS_LIMIT)) {
3129 if (bargs->limit == 0)
3130 return 0;
3131 else
3132 bargs->limit--;
3133 }
3134
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003135 return 1;
3136}
3137
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003138static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04003139{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003140 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003141 struct btrfs_root *chunk_root = fs_info->chunk_root;
3142 struct btrfs_root *dev_root = fs_info->dev_root;
3143 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04003144 struct btrfs_device *device;
3145 u64 old_size;
3146 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003147 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04003148 struct btrfs_path *path;
3149 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04003150 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003151 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003152 struct extent_buffer *leaf;
3153 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003154 int ret;
3155 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003156 bool counting = true;
David Sterba7d824b62014-05-07 17:37:51 +02003157 u64 limit_data = bctl->data.limit;
3158 u64 limit_meta = bctl->meta.limit;
3159 u64 limit_sys = bctl->sys.limit;
Chris Masonec44a352008-04-28 15:29:52 -04003160
Chris Masonec44a352008-04-28 15:29:52 -04003161 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003162 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003163 list_for_each_entry(device, devices, dev_list) {
Miao Xie7cc8e582014-09-03 21:35:38 +08003164 old_size = btrfs_device_get_total_bytes(device);
Chris Masonec44a352008-04-28 15:29:52 -04003165 size_to_free = div_factor(old_size, 1);
3166 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05003167 if (!device->writeable ||
Miao Xie7cc8e582014-09-03 21:35:38 +08003168 btrfs_device_get_total_bytes(device) -
3169 btrfs_device_get_bytes_used(device) > size_to_free ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01003170 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04003171 continue;
3172
3173 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04003174 if (ret == -ENOSPC)
3175 break;
Chris Masonec44a352008-04-28 15:29:52 -04003176 BUG_ON(ret);
3177
Yan, Zhenga22285a2010-05-16 10:48:46 -04003178 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003179 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04003180
3181 ret = btrfs_grow_device(trans, device, old_size);
3182 BUG_ON(ret);
3183
3184 btrfs_end_transaction(trans, dev_root);
3185 }
3186
3187 /* step two, relocate all the chunks */
3188 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07003189 if (!path) {
3190 ret = -ENOMEM;
3191 goto error;
3192 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003193
3194 /* zero out stat counters */
3195 spin_lock(&fs_info->balance_lock);
3196 memset(&bctl->stat, 0, sizeof(bctl->stat));
3197 spin_unlock(&fs_info->balance_lock);
3198again:
David Sterba7d824b62014-05-07 17:37:51 +02003199 if (!counting) {
3200 bctl->data.limit = limit_data;
3201 bctl->meta.limit = limit_meta;
3202 bctl->sys.limit = limit_sys;
3203 }
Chris Masonec44a352008-04-28 15:29:52 -04003204 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3205 key.offset = (u64)-1;
3206 key.type = BTRFS_CHUNK_ITEM_KEY;
3207
Chris Masond3977122009-01-05 21:25:51 -05003208 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003209 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003210 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003211 ret = -ECANCELED;
3212 goto error;
3213 }
3214
Chris Masonec44a352008-04-28 15:29:52 -04003215 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
3216 if (ret < 0)
3217 goto error;
3218
3219 /*
3220 * this shouldn't happen, it means the last relocate
3221 * failed
3222 */
3223 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003224 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04003225
3226 ret = btrfs_previous_item(chunk_root, path, 0,
3227 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003228 if (ret) {
3229 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04003230 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003231 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003232
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003233 leaf = path->nodes[0];
3234 slot = path->slots[0];
3235 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3236
Chris Masonec44a352008-04-28 15:29:52 -04003237 if (found_key.objectid != key.objectid)
3238 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04003239
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003240 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
3241
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003242 if (!counting) {
3243 spin_lock(&fs_info->balance_lock);
3244 bctl->stat.considered++;
3245 spin_unlock(&fs_info->balance_lock);
3246 }
3247
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003248 ret = should_balance_chunk(chunk_root, leaf, chunk,
3249 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02003250 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003251 if (!ret)
3252 goto loop;
3253
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003254 if (counting) {
3255 spin_lock(&fs_info->balance_lock);
3256 bctl->stat.expected++;
3257 spin_unlock(&fs_info->balance_lock);
3258 goto loop;
3259 }
3260
Chris Masonec44a352008-04-28 15:29:52 -04003261 ret = btrfs_relocate_chunk(chunk_root,
Chris Masonec44a352008-04-28 15:29:52 -04003262 found_key.objectid,
3263 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00003264 if (ret && ret != -ENOSPC)
3265 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003266 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003267 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003268 } else {
3269 spin_lock(&fs_info->balance_lock);
3270 bctl->stat.completed++;
3271 spin_unlock(&fs_info->balance_lock);
3272 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003273loop:
Ilya Dryomov795a3322013-08-27 13:50:44 +03003274 if (found_key.offset == 0)
3275 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003276 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003277 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003278
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003279 if (counting) {
3280 btrfs_release_path(path);
3281 counting = false;
3282 goto again;
3283 }
Chris Masonec44a352008-04-28 15:29:52 -04003284error:
3285 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003286 if (enospc_errors) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003287 btrfs_info(fs_info, "%d enospc errors during balance",
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003288 enospc_errors);
3289 if (!ret)
3290 ret = -ENOSPC;
3291 }
3292
Chris Masonec44a352008-04-28 15:29:52 -04003293 return ret;
3294}
3295
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003296/**
3297 * alloc_profile_is_valid - see if a given profile is valid and reduced
3298 * @flags: profile to validate
3299 * @extended: if true @flags is treated as an extended profile
3300 */
3301static int alloc_profile_is_valid(u64 flags, int extended)
3302{
3303 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3304 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3305
3306 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3307
3308 /* 1) check that all other bits are zeroed */
3309 if (flags & ~mask)
3310 return 0;
3311
3312 /* 2) see if profile is reduced */
3313 if (flags == 0)
3314 return !extended; /* "0" is valid for usual profiles */
3315
3316 /* true if exactly one bit set */
3317 return (flags & (flags - 1)) == 0;
3318}
3319
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003320static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3321{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003322 /* cancel requested || normal exit path */
3323 return atomic_read(&fs_info->balance_cancel_req) ||
3324 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3325 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003326}
3327
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003328static void __cancel_balance(struct btrfs_fs_info *fs_info)
3329{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003330 int ret;
3331
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003332 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003333 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003334 if (ret)
3335 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003336
3337 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003338}
3339
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003340/*
3341 * Should be called with both balance and volume mutexes held
3342 */
3343int btrfs_balance(struct btrfs_balance_control *bctl,
3344 struct btrfs_ioctl_balance_args *bargs)
3345{
3346 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003347 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003348 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003349 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003350 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003351 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003352
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003353 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003354 atomic_read(&fs_info->balance_pause_req) ||
3355 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003356 ret = -EINVAL;
3357 goto out;
3358 }
3359
Ilya Dryomove4837f82012-03-27 17:09:17 +03003360 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3361 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3362 mixed = 1;
3363
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003364 /*
3365 * In case of mixed groups both data and meta should be picked,
3366 * and identical options should be given for both of them.
3367 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003368 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3369 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003370 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3371 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3372 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003373 btrfs_err(fs_info, "with mixed groups data and "
3374 "metadata balance options must be the same");
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003375 ret = -EINVAL;
3376 goto out;
3377 }
3378 }
3379
Stefan Behrens8dabb742012-11-06 13:15:27 +01003380 num_devices = fs_info->fs_devices->num_devices;
3381 btrfs_dev_replace_lock(&fs_info->dev_replace);
3382 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3383 BUG_ON(num_devices < 1);
3384 num_devices--;
3385 }
3386 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003387 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003388 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003389 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003390 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003391 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003392 if (num_devices > 2)
3393 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3394 if (num_devices > 3)
3395 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3396 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003397 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3398 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3399 (bctl->data.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003400 btrfs_err(fs_info, "unable to start balance with target "
3401 "data profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003402 bctl->data.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003403 ret = -EINVAL;
3404 goto out;
3405 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003406 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3407 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3408 (bctl->meta.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003409 btrfs_err(fs_info,
3410 "unable to start balance with target metadata profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003411 bctl->meta.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003412 ret = -EINVAL;
3413 goto out;
3414 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003415 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3416 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3417 (bctl->sys.target & ~allowed))) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003418 btrfs_err(fs_info,
3419 "unable to start balance with target system profile %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003420 bctl->sys.target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003421 ret = -EINVAL;
3422 goto out;
3423 }
3424
Ilya Dryomove4837f82012-03-27 17:09:17 +03003425 /* allow dup'ed data chunks only in mixed mode */
3426 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003427 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003428 btrfs_err(fs_info, "dup for data is not allowed");
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003429 ret = -EINVAL;
3430 goto out;
3431 }
3432
3433 /* allow to reduce meta or sys integrity only if force set */
3434 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003435 BTRFS_BLOCK_GROUP_RAID10 |
3436 BTRFS_BLOCK_GROUP_RAID5 |
3437 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003438 do {
3439 seq = read_seqbegin(&fs_info->profiles_lock);
3440
3441 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3442 (fs_info->avail_system_alloc_bits & allowed) &&
3443 !(bctl->sys.target & allowed)) ||
3444 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3445 (fs_info->avail_metadata_alloc_bits & allowed) &&
3446 !(bctl->meta.target & allowed))) {
3447 if (bctl->flags & BTRFS_BALANCE_FORCE) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003448 btrfs_info(fs_info, "force reducing metadata integrity");
Miao Xiede98ced2013-01-29 10:13:12 +00003449 } else {
Frank Holtonefe120a2013-12-20 11:37:06 -05003450 btrfs_err(fs_info, "balance will reduce metadata "
3451 "integrity, use force if you want this");
Miao Xiede98ced2013-01-29 10:13:12 +00003452 ret = -EINVAL;
3453 goto out;
3454 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003455 }
Miao Xiede98ced2013-01-29 10:13:12 +00003456 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003457
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003458 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3459 int num_tolerated_disk_barrier_failures;
3460 u64 target = bctl->sys.target;
3461
3462 num_tolerated_disk_barrier_failures =
3463 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3464 if (num_tolerated_disk_barrier_failures > 0 &&
3465 (target &
3466 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3467 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3468 num_tolerated_disk_barrier_failures = 0;
3469 else if (num_tolerated_disk_barrier_failures > 1 &&
3470 (target &
3471 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3472 num_tolerated_disk_barrier_failures = 1;
3473
3474 fs_info->num_tolerated_disk_barrier_failures =
3475 num_tolerated_disk_barrier_failures;
3476 }
3477
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003478 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003479 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003480 goto out;
3481
Ilya Dryomov59641012012-01-16 22:04:48 +02003482 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3483 BUG_ON(ret == -EEXIST);
3484 set_balance_control(bctl);
3485 } else {
3486 BUG_ON(ret != -EEXIST);
3487 spin_lock(&fs_info->balance_lock);
3488 update_balance_args(bctl);
3489 spin_unlock(&fs_info->balance_lock);
3490 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003491
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003492 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003493 mutex_unlock(&fs_info->balance_mutex);
3494
3495 ret = __btrfs_balance(fs_info);
3496
3497 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003498 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003499
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003500 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3501 fs_info->num_tolerated_disk_barrier_failures =
3502 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3503 }
3504
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003505 if (bargs) {
3506 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003507 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003508 }
3509
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003510 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3511 balance_need_close(fs_info)) {
3512 __cancel_balance(fs_info);
3513 }
3514
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003515 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003516
3517 return ret;
3518out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003519 if (bctl->flags & BTRFS_BALANCE_RESUME)
3520 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003521 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003522 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003523 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3524 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003525 return ret;
3526}
3527
3528static int balance_kthread(void *data)
3529{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003530 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003531 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003532
3533 mutex_lock(&fs_info->volume_mutex);
3534 mutex_lock(&fs_info->balance_mutex);
3535
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003536 if (fs_info->balance_ctl) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003537 btrfs_info(fs_info, "continuing balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003538 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003539 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003540
3541 mutex_unlock(&fs_info->balance_mutex);
3542 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003543
Ilya Dryomov59641012012-01-16 22:04:48 +02003544 return ret;
3545}
3546
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003547int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3548{
3549 struct task_struct *tsk;
3550
3551 spin_lock(&fs_info->balance_lock);
3552 if (!fs_info->balance_ctl) {
3553 spin_unlock(&fs_info->balance_lock);
3554 return 0;
3555 }
3556 spin_unlock(&fs_info->balance_lock);
3557
3558 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003559 btrfs_info(fs_info, "force skipping balance");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003560 return 0;
3561 }
3562
3563 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Sachin Kamatcd633972013-07-15 16:52:18 +05303564 return PTR_ERR_OR_ZERO(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003565}
3566
Ilya Dryomov68310a52012-06-22 12:24:12 -06003567int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003568{
Ilya Dryomov59641012012-01-16 22:04:48 +02003569 struct btrfs_balance_control *bctl;
3570 struct btrfs_balance_item *item;
3571 struct btrfs_disk_balance_args disk_bargs;
3572 struct btrfs_path *path;
3573 struct extent_buffer *leaf;
3574 struct btrfs_key key;
3575 int ret;
3576
3577 path = btrfs_alloc_path();
3578 if (!path)
3579 return -ENOMEM;
3580
Ilya Dryomov68310a52012-06-22 12:24:12 -06003581 key.objectid = BTRFS_BALANCE_OBJECTID;
3582 key.type = BTRFS_BALANCE_ITEM_KEY;
3583 key.offset = 0;
3584
3585 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3586 if (ret < 0)
3587 goto out;
3588 if (ret > 0) { /* ret = -ENOENT; */
3589 ret = 0;
3590 goto out;
3591 }
3592
Ilya Dryomov59641012012-01-16 22:04:48 +02003593 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3594 if (!bctl) {
3595 ret = -ENOMEM;
3596 goto out;
3597 }
3598
Ilya Dryomov59641012012-01-16 22:04:48 +02003599 leaf = path->nodes[0];
3600 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3601
Ilya Dryomov68310a52012-06-22 12:24:12 -06003602 bctl->fs_info = fs_info;
3603 bctl->flags = btrfs_balance_flags(leaf, item);
3604 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003605
3606 btrfs_balance_data(leaf, item, &disk_bargs);
3607 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3608 btrfs_balance_meta(leaf, item, &disk_bargs);
3609 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3610 btrfs_balance_sys(leaf, item, &disk_bargs);
3611 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3612
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003613 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3614
Ilya Dryomov68310a52012-06-22 12:24:12 -06003615 mutex_lock(&fs_info->volume_mutex);
3616 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003617
Ilya Dryomov68310a52012-06-22 12:24:12 -06003618 set_balance_control(bctl);
3619
3620 mutex_unlock(&fs_info->balance_mutex);
3621 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003622out:
3623 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003624 return ret;
3625}
3626
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003627int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3628{
3629 int ret = 0;
3630
3631 mutex_lock(&fs_info->balance_mutex);
3632 if (!fs_info->balance_ctl) {
3633 mutex_unlock(&fs_info->balance_mutex);
3634 return -ENOTCONN;
3635 }
3636
3637 if (atomic_read(&fs_info->balance_running)) {
3638 atomic_inc(&fs_info->balance_pause_req);
3639 mutex_unlock(&fs_info->balance_mutex);
3640
3641 wait_event(fs_info->balance_wait_q,
3642 atomic_read(&fs_info->balance_running) == 0);
3643
3644 mutex_lock(&fs_info->balance_mutex);
3645 /* we are good with balance_ctl ripped off from under us */
3646 BUG_ON(atomic_read(&fs_info->balance_running));
3647 atomic_dec(&fs_info->balance_pause_req);
3648 } else {
3649 ret = -ENOTCONN;
3650 }
3651
3652 mutex_unlock(&fs_info->balance_mutex);
3653 return ret;
3654}
3655
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003656int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3657{
Ilya Dryomove649e582013-10-10 20:40:21 +03003658 if (fs_info->sb->s_flags & MS_RDONLY)
3659 return -EROFS;
3660
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003661 mutex_lock(&fs_info->balance_mutex);
3662 if (!fs_info->balance_ctl) {
3663 mutex_unlock(&fs_info->balance_mutex);
3664 return -ENOTCONN;
3665 }
3666
3667 atomic_inc(&fs_info->balance_cancel_req);
3668 /*
3669 * if we are running just wait and return, balance item is
3670 * deleted in btrfs_balance in this case
3671 */
3672 if (atomic_read(&fs_info->balance_running)) {
3673 mutex_unlock(&fs_info->balance_mutex);
3674 wait_event(fs_info->balance_wait_q,
3675 atomic_read(&fs_info->balance_running) == 0);
3676 mutex_lock(&fs_info->balance_mutex);
3677 } else {
3678 /* __cancel_balance needs volume_mutex */
3679 mutex_unlock(&fs_info->balance_mutex);
3680 mutex_lock(&fs_info->volume_mutex);
3681 mutex_lock(&fs_info->balance_mutex);
3682
3683 if (fs_info->balance_ctl)
3684 __cancel_balance(fs_info);
3685
3686 mutex_unlock(&fs_info->volume_mutex);
3687 }
3688
3689 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3690 atomic_dec(&fs_info->balance_cancel_req);
3691 mutex_unlock(&fs_info->balance_mutex);
3692 return 0;
3693}
3694
Stefan Behrens803b2f52013-08-15 17:11:21 +02003695static int btrfs_uuid_scan_kthread(void *data)
3696{
3697 struct btrfs_fs_info *fs_info = data;
3698 struct btrfs_root *root = fs_info->tree_root;
3699 struct btrfs_key key;
3700 struct btrfs_key max_key;
3701 struct btrfs_path *path = NULL;
3702 int ret = 0;
3703 struct extent_buffer *eb;
3704 int slot;
3705 struct btrfs_root_item root_item;
3706 u32 item_size;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003707 struct btrfs_trans_handle *trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003708
3709 path = btrfs_alloc_path();
3710 if (!path) {
3711 ret = -ENOMEM;
3712 goto out;
3713 }
3714
3715 key.objectid = 0;
3716 key.type = BTRFS_ROOT_ITEM_KEY;
3717 key.offset = 0;
3718
3719 max_key.objectid = (u64)-1;
3720 max_key.type = BTRFS_ROOT_ITEM_KEY;
3721 max_key.offset = (u64)-1;
3722
Stefan Behrens803b2f52013-08-15 17:11:21 +02003723 while (1) {
Filipe David Borba Manana6174d3c2013-10-01 16:13:42 +01003724 ret = btrfs_search_forward(root, &key, path, 0);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003725 if (ret) {
3726 if (ret > 0)
3727 ret = 0;
3728 break;
3729 }
3730
3731 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3732 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3733 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3734 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3735 goto skip;
3736
3737 eb = path->nodes[0];
3738 slot = path->slots[0];
3739 item_size = btrfs_item_size_nr(eb, slot);
3740 if (item_size < sizeof(root_item))
3741 goto skip;
3742
Stefan Behrens803b2f52013-08-15 17:11:21 +02003743 read_extent_buffer(eb, &root_item,
3744 btrfs_item_ptr_offset(eb, slot),
3745 (int)sizeof(root_item));
3746 if (btrfs_root_refs(&root_item) == 0)
3747 goto skip;
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003748
3749 if (!btrfs_is_empty_uuid(root_item.uuid) ||
3750 !btrfs_is_empty_uuid(root_item.received_uuid)) {
3751 if (trans)
3752 goto update_tree;
3753
3754 btrfs_release_path(path);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003755 /*
3756 * 1 - subvol uuid item
3757 * 1 - received_subvol uuid item
3758 */
3759 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3760 if (IS_ERR(trans)) {
3761 ret = PTR_ERR(trans);
3762 break;
3763 }
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003764 continue;
3765 } else {
3766 goto skip;
3767 }
3768update_tree:
3769 if (!btrfs_is_empty_uuid(root_item.uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003770 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3771 root_item.uuid,
3772 BTRFS_UUID_KEY_SUBVOL,
3773 key.objectid);
3774 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003775 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003776 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003777 break;
3778 }
3779 }
3780
3781 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
Stefan Behrens803b2f52013-08-15 17:11:21 +02003782 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3783 root_item.received_uuid,
3784 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3785 key.objectid);
3786 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003787 btrfs_warn(fs_info, "uuid_tree_add failed %d",
Stefan Behrens803b2f52013-08-15 17:11:21 +02003788 ret);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003789 break;
3790 }
3791 }
3792
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003793skip:
Stefan Behrens803b2f52013-08-15 17:11:21 +02003794 if (trans) {
3795 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003796 trans = NULL;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003797 if (ret)
3798 break;
3799 }
3800
Stefan Behrens803b2f52013-08-15 17:11:21 +02003801 btrfs_release_path(path);
3802 if (key.offset < (u64)-1) {
3803 key.offset++;
3804 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3805 key.offset = 0;
3806 key.type = BTRFS_ROOT_ITEM_KEY;
3807 } else if (key.objectid < (u64)-1) {
3808 key.offset = 0;
3809 key.type = BTRFS_ROOT_ITEM_KEY;
3810 key.objectid++;
3811 } else {
3812 break;
3813 }
3814 cond_resched();
3815 }
3816
3817out:
3818 btrfs_free_path(path);
Filipe David Borba Mananaf45388f2013-08-28 10:28:34 +01003819 if (trans && !IS_ERR(trans))
3820 btrfs_end_transaction(trans, fs_info->uuid_root);
Stefan Behrens803b2f52013-08-15 17:11:21 +02003821 if (ret)
Frank Holtonefe120a2013-12-20 11:37:06 -05003822 btrfs_warn(fs_info, "btrfs_uuid_scan_kthread failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003823 else
3824 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003825 up(&fs_info->uuid_tree_rescan_sem);
3826 return 0;
3827}
3828
Stefan Behrens70f80172013-08-15 17:11:23 +02003829/*
3830 * Callback for btrfs_uuid_tree_iterate().
3831 * returns:
3832 * 0 check succeeded, the entry is not outdated.
3833 * < 0 if an error occured.
3834 * > 0 if the check failed, which means the caller shall remove the entry.
3835 */
3836static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3837 u8 *uuid, u8 type, u64 subid)
3838{
3839 struct btrfs_key key;
3840 int ret = 0;
3841 struct btrfs_root *subvol_root;
3842
3843 if (type != BTRFS_UUID_KEY_SUBVOL &&
3844 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3845 goto out;
3846
3847 key.objectid = subid;
3848 key.type = BTRFS_ROOT_ITEM_KEY;
3849 key.offset = (u64)-1;
3850 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3851 if (IS_ERR(subvol_root)) {
3852 ret = PTR_ERR(subvol_root);
3853 if (ret == -ENOENT)
3854 ret = 1;
3855 goto out;
3856 }
3857
3858 switch (type) {
3859 case BTRFS_UUID_KEY_SUBVOL:
3860 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3861 ret = 1;
3862 break;
3863 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3864 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3865 BTRFS_UUID_SIZE))
3866 ret = 1;
3867 break;
3868 }
3869
3870out:
3871 return ret;
3872}
3873
3874static int btrfs_uuid_rescan_kthread(void *data)
3875{
3876 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3877 int ret;
3878
3879 /*
3880 * 1st step is to iterate through the existing UUID tree and
3881 * to delete all entries that contain outdated data.
3882 * 2nd step is to add all missing entries to the UUID tree.
3883 */
3884 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3885 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003886 btrfs_warn(fs_info, "iterating uuid_tree failed %d", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003887 up(&fs_info->uuid_tree_rescan_sem);
3888 return ret;
3889 }
3890 return btrfs_uuid_scan_kthread(data);
3891}
3892
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003893int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3894{
3895 struct btrfs_trans_handle *trans;
3896 struct btrfs_root *tree_root = fs_info->tree_root;
3897 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003898 struct task_struct *task;
3899 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003900
3901 /*
3902 * 1 - root node
3903 * 1 - root item
3904 */
3905 trans = btrfs_start_transaction(tree_root, 2);
3906 if (IS_ERR(trans))
3907 return PTR_ERR(trans);
3908
3909 uuid_root = btrfs_create_tree(trans, fs_info,
3910 BTRFS_UUID_TREE_OBJECTID);
3911 if (IS_ERR(uuid_root)) {
3912 btrfs_abort_transaction(trans, tree_root,
3913 PTR_ERR(uuid_root));
3914 return PTR_ERR(uuid_root);
3915 }
3916
3917 fs_info->uuid_root = uuid_root;
3918
Stefan Behrens803b2f52013-08-15 17:11:21 +02003919 ret = btrfs_commit_transaction(trans, tree_root);
3920 if (ret)
3921 return ret;
3922
3923 down(&fs_info->uuid_tree_rescan_sem);
3924 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3925 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003926 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003927 btrfs_warn(fs_info, "failed to start uuid_scan task");
Stefan Behrens803b2f52013-08-15 17:11:21 +02003928 up(&fs_info->uuid_tree_rescan_sem);
3929 return PTR_ERR(task);
3930 }
3931
3932 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003933}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003934
Stefan Behrens70f80172013-08-15 17:11:23 +02003935int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3936{
3937 struct task_struct *task;
3938
3939 down(&fs_info->uuid_tree_rescan_sem);
3940 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3941 if (IS_ERR(task)) {
3942 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Frank Holtonefe120a2013-12-20 11:37:06 -05003943 btrfs_warn(fs_info, "failed to start uuid_rescan task");
Stefan Behrens70f80172013-08-15 17:11:23 +02003944 up(&fs_info->uuid_tree_rescan_sem);
3945 return PTR_ERR(task);
3946 }
3947
3948 return 0;
3949}
3950
Chris Mason8f18cf12008-04-25 16:53:30 -04003951/*
3952 * shrinking a device means finding all of the device extents past
3953 * the new size, and then following the back refs to the chunks.
3954 * The chunk relocation code actually frees the device extent
3955 */
3956int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3957{
3958 struct btrfs_trans_handle *trans;
3959 struct btrfs_root *root = device->dev_root;
3960 struct btrfs_dev_extent *dev_extent = NULL;
3961 struct btrfs_path *path;
3962 u64 length;
Chris Mason8f18cf12008-04-25 16:53:30 -04003963 u64 chunk_objectid;
3964 u64 chunk_offset;
3965 int ret;
3966 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003967 int failed = 0;
3968 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003969 struct extent_buffer *l;
3970 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003971 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003972 u64 old_total = btrfs_super_total_bytes(super_copy);
Miao Xie7cc8e582014-09-03 21:35:38 +08003973 u64 old_size = btrfs_device_get_total_bytes(device);
3974 u64 diff = old_size - new_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04003975
Stefan Behrens63a212a2012-11-05 18:29:28 +01003976 if (device->is_tgtdev_for_dev_replace)
3977 return -EINVAL;
3978
Chris Mason8f18cf12008-04-25 16:53:30 -04003979 path = btrfs_alloc_path();
3980 if (!path)
3981 return -ENOMEM;
3982
Chris Mason8f18cf12008-04-25 16:53:30 -04003983 path->reada = 2;
3984
Chris Mason7d9eb122008-07-08 14:19:17 -04003985 lock_chunks(root);
3986
Miao Xie7cc8e582014-09-03 21:35:38 +08003987 btrfs_device_set_total_bytes(device, new_size);
Josef Bacik2bf64752011-09-26 17:12:22 -04003988 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003989 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003990 spin_lock(&root->fs_info->free_chunk_lock);
3991 root->fs_info->free_chunk_space -= diff;
3992 spin_unlock(&root->fs_info->free_chunk_lock);
3993 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003994 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003995
Josef Bacikba1bf482009-09-11 16:11:19 -04003996again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003997 key.objectid = device->devid;
3998 key.offset = (u64)-1;
3999 key.type = BTRFS_DEV_EXTENT_KEY;
4000
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004001 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04004002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4003 if (ret < 0)
4004 goto done;
4005
4006 ret = btrfs_previous_item(root, path, 0, key.type);
4007 if (ret < 0)
4008 goto done;
4009 if (ret) {
4010 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004011 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004012 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04004013 }
4014
4015 l = path->nodes[0];
4016 slot = path->slots[0];
4017 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4018
Josef Bacikba1bf482009-09-11 16:11:19 -04004019 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004020 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04004021 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004022 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004023
4024 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
4025 length = btrfs_dev_extent_length(l, dev_extent);
4026
Josef Bacikba1bf482009-09-11 16:11:19 -04004027 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004028 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04004029 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04004030 }
Chris Mason8f18cf12008-04-25 16:53:30 -04004031
Chris Mason8f18cf12008-04-25 16:53:30 -04004032 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
4033 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02004034 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04004035
Zhao Leia688a042015-02-09 20:31:44 +08004036 ret = btrfs_relocate_chunk(root, chunk_objectid, chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04004037 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04004038 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04004039 if (ret == -ENOSPC)
4040 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03004041 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04004042
4043 if (failed && !retried) {
4044 failed = 0;
4045 retried = true;
4046 goto again;
4047 } else if (failed && retried) {
4048 ret = -ENOSPC;
4049 lock_chunks(root);
4050
Miao Xie7cc8e582014-09-03 21:35:38 +08004051 btrfs_device_set_total_bytes(device, old_size);
Josef Bacikba1bf482009-09-11 16:11:19 -04004052 if (device->writeable)
4053 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04004054 spin_lock(&root->fs_info->free_chunk_lock);
4055 root->fs_info->free_chunk_space += diff;
4056 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04004057 unlock_chunks(root);
4058 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04004059 }
4060
Chris Balld6397ba2009-04-27 07:29:03 -04004061 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04004062 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00004063 if (IS_ERR(trans)) {
4064 ret = PTR_ERR(trans);
4065 goto done;
4066 }
4067
Chris Balld6397ba2009-04-27 07:29:03 -04004068 lock_chunks(root);
Miao Xie7cc8e582014-09-03 21:35:38 +08004069 btrfs_device_set_disk_total_bytes(device, new_size);
Miao Xie935e5cc2014-09-03 21:35:33 +08004070 if (list_empty(&device->resized_list))
4071 list_add_tail(&device->resized_list,
4072 &root->fs_info->fs_devices->resized_devices);
Chris Balld6397ba2009-04-27 07:29:03 -04004073
Chris Balld6397ba2009-04-27 07:29:03 -04004074 WARN_ON(diff > old_total);
4075 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4076 unlock_chunks(root);
Miao Xie2196d6e2014-09-03 21:35:41 +08004077
4078 /* Now btrfs_update_device() will change the on-disk size. */
4079 ret = btrfs_update_device(trans, device);
Chris Balld6397ba2009-04-27 07:29:03 -04004080 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04004081done:
4082 btrfs_free_path(path);
4083 return ret;
4084}
4085
Li Zefan125ccb02011-12-08 15:07:24 +08004086static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04004087 struct btrfs_key *key,
4088 struct btrfs_chunk *chunk, int item_size)
4089{
David Sterba6c417612011-04-13 15:41:04 +02004090 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04004091 struct btrfs_disk_key disk_key;
4092 u32 array_size;
4093 u8 *ptr;
4094
Miao Xiefe48a5c2014-09-03 21:35:39 +08004095 lock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004096 array_size = btrfs_super_sys_array_size(super_copy);
Gui Hecheng5f43f862014-04-21 20:13:11 +08004097 if (array_size + item_size + sizeof(disk_key)
Miao Xiefe48a5c2014-09-03 21:35:39 +08004098 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4099 unlock_chunks(root);
Chris Mason0b86a832008-03-24 15:01:56 -04004100 return -EFBIG;
Miao Xiefe48a5c2014-09-03 21:35:39 +08004101 }
Chris Mason0b86a832008-03-24 15:01:56 -04004102
4103 ptr = super_copy->sys_chunk_array + array_size;
4104 btrfs_cpu_key_to_disk(&disk_key, key);
4105 memcpy(ptr, &disk_key, sizeof(disk_key));
4106 ptr += sizeof(disk_key);
4107 memcpy(ptr, chunk, item_size);
4108 item_size += sizeof(disk_key);
4109 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
Miao Xiefe48a5c2014-09-03 21:35:39 +08004110 unlock_chunks(root);
4111
Chris Mason0b86a832008-03-24 15:01:56 -04004112 return 0;
4113}
4114
Miao Xieb2117a32011-01-05 10:07:28 +00004115/*
Arne Jansen73c5de02011-04-12 12:07:57 +02004116 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00004117 */
Arne Jansen73c5de02011-04-12 12:07:57 +02004118static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00004119{
Arne Jansen73c5de02011-04-12 12:07:57 +02004120 const struct btrfs_device_info *di_a = a;
4121 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00004122
Arne Jansen73c5de02011-04-12 12:07:57 +02004123 if (di_a->max_avail > di_b->max_avail)
4124 return -1;
4125 if (di_a->max_avail < di_b->max_avail)
4126 return 1;
4127 if (di_a->total_avail > di_b->total_avail)
4128 return -1;
4129 if (di_a->total_avail < di_b->total_avail)
4130 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00004131 return 0;
4132}
4133
David Sterbae8c9f182015-01-02 18:23:10 +01004134static const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00004135 [BTRFS_RAID_RAID10] = {
4136 .sub_stripes = 2,
4137 .dev_stripes = 1,
4138 .devs_max = 0, /* 0 == as many as possible */
4139 .devs_min = 4,
4140 .devs_increment = 2,
4141 .ncopies = 2,
4142 },
4143 [BTRFS_RAID_RAID1] = {
4144 .sub_stripes = 1,
4145 .dev_stripes = 1,
4146 .devs_max = 2,
4147 .devs_min = 2,
4148 .devs_increment = 2,
4149 .ncopies = 2,
4150 },
4151 [BTRFS_RAID_DUP] = {
4152 .sub_stripes = 1,
4153 .dev_stripes = 2,
4154 .devs_max = 1,
4155 .devs_min = 1,
4156 .devs_increment = 1,
4157 .ncopies = 2,
4158 },
4159 [BTRFS_RAID_RAID0] = {
4160 .sub_stripes = 1,
4161 .dev_stripes = 1,
4162 .devs_max = 0,
4163 .devs_min = 2,
4164 .devs_increment = 1,
4165 .ncopies = 1,
4166 },
4167 [BTRFS_RAID_SINGLE] = {
4168 .sub_stripes = 1,
4169 .dev_stripes = 1,
4170 .devs_max = 1,
4171 .devs_min = 1,
4172 .devs_increment = 1,
4173 .ncopies = 1,
4174 },
Chris Masone942f882013-02-20 14:06:05 -05004175 [BTRFS_RAID_RAID5] = {
4176 .sub_stripes = 1,
4177 .dev_stripes = 1,
4178 .devs_max = 0,
4179 .devs_min = 2,
4180 .devs_increment = 1,
4181 .ncopies = 2,
4182 },
4183 [BTRFS_RAID_RAID6] = {
4184 .sub_stripes = 1,
4185 .dev_stripes = 1,
4186 .devs_max = 0,
4187 .devs_min = 3,
4188 .devs_increment = 1,
4189 .ncopies = 3,
4190 },
Liu Bo31e50222012-11-21 14:18:10 +00004191};
4192
David Woodhouse53b381b2013-01-29 18:40:14 -05004193static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
4194{
4195 /* TODO allow them to set a preferred stripe size */
4196 return 64 * 1024;
4197}
4198
4199static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4200{
Zhao Leiffe2d202015-01-20 15:11:44 +08004201 if (!(type & BTRFS_BLOCK_GROUP_RAID56_MASK))
David Woodhouse53b381b2013-01-29 18:40:14 -05004202 return;
4203
Miao Xieceda0862013-04-11 10:30:16 +00004204 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05004205}
4206
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004207#define BTRFS_MAX_DEVS(r) ((BTRFS_LEAF_DATA_SIZE(r) \
4208 - sizeof(struct btrfs_item) \
4209 - sizeof(struct btrfs_chunk)) \
4210 / sizeof(struct btrfs_stripe) + 1)
4211
4212#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4213 - 2 * sizeof(struct btrfs_disk_key) \
4214 - 2 * sizeof(struct btrfs_chunk)) \
4215 / sizeof(struct btrfs_stripe) + 1)
4216
Miao Xieb2117a32011-01-05 10:07:28 +00004217static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04004218 struct btrfs_root *extent_root, u64 start,
4219 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00004220{
4221 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00004222 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4223 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02004224 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00004225 struct extent_map_tree *em_tree;
4226 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02004227 struct btrfs_device_info *devices_info = NULL;
4228 u64 total_avail;
4229 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05004230 int data_stripes; /* number of stripes that count for
4231 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02004232 int sub_stripes; /* sub_stripes info for map */
4233 int dev_stripes; /* stripes per dev */
4234 int devs_max; /* max devs to use */
4235 int devs_min; /* min devs needed */
4236 int devs_increment; /* ndevs has to be a multiple of this */
4237 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00004238 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02004239 u64 max_stripe_size;
4240 u64 max_chunk_size;
4241 u64 stripe_size;
4242 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004243 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02004244 int ndevs;
4245 int i;
4246 int j;
Liu Bo31e50222012-11-21 14:18:10 +00004247 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00004248
Ilya Dryomov0c460c02012-03-27 17:09:17 +03004249 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02004250
Miao Xieb2117a32011-01-05 10:07:28 +00004251 if (list_empty(&fs_devices->alloc_list))
4252 return -ENOSPC;
4253
Liu Bo31e50222012-11-21 14:18:10 +00004254 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02004255
Liu Bo31e50222012-11-21 14:18:10 +00004256 sub_stripes = btrfs_raid_array[index].sub_stripes;
4257 dev_stripes = btrfs_raid_array[index].dev_stripes;
4258 devs_max = btrfs_raid_array[index].devs_max;
4259 devs_min = btrfs_raid_array[index].devs_min;
4260 devs_increment = btrfs_raid_array[index].devs_increment;
4261 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02004262
4263 if (type & BTRFS_BLOCK_GROUP_DATA) {
4264 max_stripe_size = 1024 * 1024 * 1024;
4265 max_chunk_size = 10 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004266 if (!devs_max)
4267 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004268 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05004269 /* for larger filesystems, use larger metadata chunks */
4270 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
4271 max_stripe_size = 1024 * 1024 * 1024;
4272 else
4273 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004274 max_chunk_size = max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004275 if (!devs_max)
4276 devs_max = BTRFS_MAX_DEVS(info->chunk_root);
Arne Jansen73c5de02011-04-12 12:07:57 +02004277 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05004278 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02004279 max_chunk_size = 2 * max_stripe_size;
Gui Hecheng23f8f9b2014-04-21 20:13:12 +08004280 if (!devs_max)
4281 devs_max = BTRFS_MAX_DEVS_SYS_CHUNK;
Arne Jansen73c5de02011-04-12 12:07:57 +02004282 } else {
David Sterba351fd352014-05-15 16:48:20 +02004283 btrfs_err(info, "invalid chunk type 0x%llx requested",
Arne Jansen73c5de02011-04-12 12:07:57 +02004284 type);
4285 BUG_ON(1);
4286 }
4287
4288 /* we don't want a chunk larger than 10% of writeable space */
4289 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
4290 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004291
David Sterba31e818f2015-02-20 18:00:26 +01004292 devices_info = kcalloc(fs_devices->rw_devices, sizeof(*devices_info),
Miao Xieb2117a32011-01-05 10:07:28 +00004293 GFP_NOFS);
4294 if (!devices_info)
4295 return -ENOMEM;
4296
Arne Jansen73c5de02011-04-12 12:07:57 +02004297 cur = fs_devices->alloc_list.next;
4298
4299 /*
4300 * in the first pass through the devices list, we gather information
4301 * about the available holes on each device.
4302 */
4303 ndevs = 0;
4304 while (cur != &fs_devices->alloc_list) {
4305 struct btrfs_device *device;
4306 u64 max_avail;
4307 u64 dev_offset;
4308
4309 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4310
4311 cur = cur->next;
4312
4313 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004314 WARN(1, KERN_ERR
Frank Holtonefe120a2013-12-20 11:37:06 -05004315 "BTRFS: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004316 continue;
4317 }
4318
Stefan Behrens63a212a2012-11-05 18:29:28 +01004319 if (!device->in_fs_metadata ||
4320 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004321 continue;
4322
4323 if (device->total_bytes > device->bytes_used)
4324 total_avail = device->total_bytes - device->bytes_used;
4325 else
4326 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004327
4328 /* If there is no space on this device, skip it. */
4329 if (total_avail == 0)
4330 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004331
Josef Bacik6df9a952013-06-27 13:22:46 -04004332 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004333 max_stripe_size * dev_stripes,
4334 &dev_offset, &max_avail);
4335 if (ret && ret != -ENOSPC)
4336 goto error;
4337
4338 if (ret == 0)
4339 max_avail = max_stripe_size * dev_stripes;
4340
4341 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4342 continue;
4343
Eric Sandeen063d0062013-01-31 00:55:01 +00004344 if (ndevs == fs_devices->rw_devices) {
4345 WARN(1, "%s: found more than %llu devices\n",
4346 __func__, fs_devices->rw_devices);
4347 break;
4348 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004349 devices_info[ndevs].dev_offset = dev_offset;
4350 devices_info[ndevs].max_avail = max_avail;
4351 devices_info[ndevs].total_avail = total_avail;
4352 devices_info[ndevs].dev = device;
4353 ++ndevs;
4354 }
4355
4356 /*
4357 * now sort the devices by hole size / available space
4358 */
4359 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4360 btrfs_cmp_device_info, NULL);
4361
4362 /* round down to number of usable stripes */
4363 ndevs -= ndevs % devs_increment;
4364
4365 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4366 ret = -ENOSPC;
4367 goto error;
4368 }
4369
4370 if (devs_max && ndevs > devs_max)
4371 ndevs = devs_max;
4372 /*
4373 * the primary goal is to maximize the number of stripes, so use as many
4374 * devices as possible, even if the stripes are not maximum sized.
4375 */
4376 stripe_size = devices_info[ndevs-1].max_avail;
4377 num_stripes = ndevs * dev_stripes;
4378
David Woodhouse53b381b2013-01-29 18:40:14 -05004379 /*
4380 * this will have to be fixed for RAID1 and RAID10 over
4381 * more drives
4382 */
4383 data_stripes = num_stripes / ncopies;
4384
David Woodhouse53b381b2013-01-29 18:40:14 -05004385 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4386 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4387 btrfs_super_stripesize(info->super_copy));
4388 data_stripes = num_stripes - 1;
4389 }
4390 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4391 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4392 btrfs_super_stripesize(info->super_copy));
4393 data_stripes = num_stripes - 2;
4394 }
Chris Mason86db2572013-02-20 16:23:40 -05004395
4396 /*
4397 * Use the number of data stripes to figure out how big this chunk
4398 * is really going to be in terms of logical address space,
4399 * and compare that answer with the max chunk size
4400 */
4401 if (stripe_size * data_stripes > max_chunk_size) {
4402 u64 mask = (1ULL << 24) - 1;
David Sterbab8b93ad2015-01-16 17:26:13 +01004403
4404 stripe_size = div_u64(max_chunk_size, data_stripes);
Chris Mason86db2572013-02-20 16:23:40 -05004405
4406 /* bump the answer up to a 16MB boundary */
4407 stripe_size = (stripe_size + mask) & ~mask;
4408
4409 /* but don't go higher than the limits we found
4410 * while searching for free extents
4411 */
4412 if (stripe_size > devices_info[ndevs-1].max_avail)
4413 stripe_size = devices_info[ndevs-1].max_avail;
4414 }
4415
David Sterbab8b93ad2015-01-16 17:26:13 +01004416 stripe_size = div_u64(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004417
4418 /* align to BTRFS_STRIPE_LEN */
David Sterbab8b93ad2015-01-16 17:26:13 +01004419 stripe_size = div_u64(stripe_size, raid_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05004420 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004421
Miao Xieb2117a32011-01-05 10:07:28 +00004422 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4423 if (!map) {
4424 ret = -ENOMEM;
4425 goto error;
4426 }
4427 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004428
Arne Jansen73c5de02011-04-12 12:07:57 +02004429 for (i = 0; i < ndevs; ++i) {
4430 for (j = 0; j < dev_stripes; ++j) {
4431 int s = i * dev_stripes + j;
4432 map->stripes[s].dev = devices_info[i].dev;
4433 map->stripes[s].physical = devices_info[i].dev_offset +
4434 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004435 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004436 }
Chris Mason593060d2008-03-25 16:50:33 -04004437 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004438 map->stripe_len = raid_stripe_len;
4439 map->io_align = raid_stripe_len;
4440 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004441 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004442 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004443
David Woodhouse53b381b2013-01-29 18:40:14 -05004444 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004445
Arne Jansen73c5de02011-04-12 12:07:57 +02004446 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004447
David Sterba172ddd62011-04-21 00:48:27 +02004448 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004449 if (!em) {
Wang Shilong298a8f92014-06-19 10:42:52 +08004450 kfree(map);
Miao Xieb2117a32011-01-05 10:07:28 +00004451 ret = -ENOMEM;
4452 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004453 }
Wang Shilong298a8f92014-06-19 10:42:52 +08004454 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04004455 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004456 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004457 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004458 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004459 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004460 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004461
Chris Mason0b86a832008-03-24 15:01:56 -04004462 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004463 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004464 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004465 if (!ret) {
4466 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4467 atomic_inc(&em->refs);
4468 }
Chris Mason890871b2009-09-02 16:24:52 -04004469 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004470 if (ret) {
4471 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004472 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004473 }
Yan Zheng2b820322008-11-17 21:11:30 -05004474
Josef Bacik04487482013-01-29 15:03:37 -05004475 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4476 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4477 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004478 if (ret)
4479 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004480
Miao Xie7cc8e582014-09-03 21:35:38 +08004481 for (i = 0; i < map->num_stripes; i++) {
4482 num_bytes = map->stripes[i].dev->bytes_used + stripe_size;
4483 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4484 }
Miao Xie43530c42014-09-03 21:35:36 +08004485
Miao Xie1c116182014-09-03 21:35:37 +08004486 spin_lock(&extent_root->fs_info->free_chunk_lock);
4487 extent_root->fs_info->free_chunk_space -= (stripe_size *
4488 map->num_stripes);
4489 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4490
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004491 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004492 check_raid56_incompat_flag(extent_root->fs_info, type);
4493
Miao Xieb2117a32011-01-05 10:07:28 +00004494 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004495 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004496
Josef Bacik6df9a952013-06-27 13:22:46 -04004497error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004498 write_lock(&em_tree->lock);
4499 remove_extent_mapping(em_tree, em);
4500 write_unlock(&em_tree->lock);
4501
4502 /* One for our allocation */
4503 free_extent_map(em);
4504 /* One for the tree reference */
4505 free_extent_map(em);
Filipe Manana495e64f2014-12-02 18:07:30 +00004506 /* One for the pending_chunks list reference */
4507 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004508error:
Miao Xieb2117a32011-01-05 10:07:28 +00004509 kfree(devices_info);
4510 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004511}
4512
Josef Bacik6df9a952013-06-27 13:22:46 -04004513int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004514 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004515 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004516{
Yan Zheng2b820322008-11-17 21:11:30 -05004517 struct btrfs_key key;
4518 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4519 struct btrfs_device *device;
4520 struct btrfs_chunk *chunk;
4521 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004522 struct extent_map_tree *em_tree;
4523 struct extent_map *em;
4524 struct map_lookup *map;
4525 size_t item_size;
4526 u64 dev_offset;
4527 u64 stripe_size;
4528 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004529 int ret;
4530
Josef Bacik6df9a952013-06-27 13:22:46 -04004531 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4532 read_lock(&em_tree->lock);
4533 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4534 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004535
Josef Bacik6df9a952013-06-27 13:22:46 -04004536 if (!em) {
4537 btrfs_crit(extent_root->fs_info, "unable to find logical "
4538 "%Lu len %Lu", chunk_offset, chunk_size);
4539 return -EINVAL;
4540 }
4541
4542 if (em->start != chunk_offset || em->len != chunk_size) {
4543 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
David Sterba351fd352014-05-15 16:48:20 +02004544 " %Lu-%Lu, found %Lu-%Lu", chunk_offset,
Josef Bacik6df9a952013-06-27 13:22:46 -04004545 chunk_size, em->start, em->len);
4546 free_extent_map(em);
4547 return -EINVAL;
4548 }
4549
4550 map = (struct map_lookup *)em->bdev;
4551 item_size = btrfs_chunk_item_size(map->num_stripes);
4552 stripe_size = em->orig_block_len;
4553
4554 chunk = kzalloc(item_size, GFP_NOFS);
4555 if (!chunk) {
4556 ret = -ENOMEM;
4557 goto out;
4558 }
4559
4560 for (i = 0; i < map->num_stripes; i++) {
4561 device = map->stripes[i].dev;
4562 dev_offset = map->stripes[i].physical;
4563
Yan Zheng2b820322008-11-17 21:11:30 -05004564 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004565 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004566 goto out;
4567 ret = btrfs_alloc_dev_extent(trans, device,
4568 chunk_root->root_key.objectid,
4569 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4570 chunk_offset, dev_offset,
4571 stripe_size);
4572 if (ret)
4573 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004574 }
4575
Yan Zheng2b820322008-11-17 21:11:30 -05004576 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004577 for (i = 0; i < map->num_stripes; i++) {
4578 device = map->stripes[i].dev;
4579 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004580
4581 btrfs_set_stack_stripe_devid(stripe, device->devid);
4582 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4583 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4584 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004585 }
4586
4587 btrfs_set_stack_chunk_length(chunk, chunk_size);
4588 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4589 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4590 btrfs_set_stack_chunk_type(chunk, map->type);
4591 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4592 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4593 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4594 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4595 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4596
4597 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4598 key.type = BTRFS_CHUNK_ITEM_KEY;
4599 key.offset = chunk_offset;
4600
4601 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004602 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4603 /*
4604 * TODO: Cleanup of inserted chunk root in case of
4605 * failure.
4606 */
Li Zefan125ccb02011-12-08 15:07:24 +08004607 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004608 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004609 }
liubo1abe9b82011-03-24 11:18:59 +00004610
Josef Bacik6df9a952013-06-27 13:22:46 -04004611out:
Yan Zheng2b820322008-11-17 21:11:30 -05004612 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004613 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004614 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004615}
4616
4617/*
4618 * Chunk allocation falls into two parts. The first part does works
4619 * that make the new allocated chunk useable, but not do any operation
4620 * that modifies the chunk tree. The second part does the works that
4621 * require modifying the chunk tree. This division is important for the
4622 * bootstrap process of adding storage to a seed btrfs.
4623 */
4624int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4625 struct btrfs_root *extent_root, u64 type)
4626{
4627 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004628
Filipe Mananaa9629592015-05-18 19:11:40 +01004629 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex));
Josef Bacik6df9a952013-06-27 13:22:46 -04004630 chunk_offset = find_next_chunk(extent_root->fs_info);
4631 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004632}
4633
Chris Masond3977122009-01-05 21:25:51 -05004634static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004635 struct btrfs_root *root,
4636 struct btrfs_device *device)
4637{
4638 u64 chunk_offset;
4639 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004640 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004641 struct btrfs_fs_info *fs_info = root->fs_info;
4642 struct btrfs_root *extent_root = fs_info->extent_root;
4643 int ret;
4644
Josef Bacik6df9a952013-06-27 13:22:46 -04004645 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004646 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004647 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4648 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004649 if (ret)
4650 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004651
Josef Bacik6df9a952013-06-27 13:22:46 -04004652 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004653 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004654 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4655 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004656 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004657}
4658
Miao Xied20983b2014-07-03 18:22:13 +08004659static inline int btrfs_chunk_max_errors(struct map_lookup *map)
4660{
4661 int max_errors;
4662
4663 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4664 BTRFS_BLOCK_GROUP_RAID10 |
4665 BTRFS_BLOCK_GROUP_RAID5 |
4666 BTRFS_BLOCK_GROUP_DUP)) {
4667 max_errors = 1;
4668 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
4669 max_errors = 2;
4670 } else {
4671 max_errors = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004672 }
4673
Miao Xied20983b2014-07-03 18:22:13 +08004674 return max_errors;
Yan Zheng2b820322008-11-17 21:11:30 -05004675}
4676
4677int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4678{
4679 struct extent_map *em;
4680 struct map_lookup *map;
4681 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4682 int readonly = 0;
Miao Xied20983b2014-07-03 18:22:13 +08004683 int miss_ndevs = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004684 int i;
4685
Chris Mason890871b2009-09-02 16:24:52 -04004686 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004687 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004688 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004689 if (!em)
4690 return 1;
4691
4692 map = (struct map_lookup *)em->bdev;
4693 for (i = 0; i < map->num_stripes; i++) {
Miao Xied20983b2014-07-03 18:22:13 +08004694 if (map->stripes[i].dev->missing) {
4695 miss_ndevs++;
4696 continue;
4697 }
4698
Yan Zheng2b820322008-11-17 21:11:30 -05004699 if (!map->stripes[i].dev->writeable) {
4700 readonly = 1;
Miao Xied20983b2014-07-03 18:22:13 +08004701 goto end;
Yan Zheng2b820322008-11-17 21:11:30 -05004702 }
4703 }
Miao Xied20983b2014-07-03 18:22:13 +08004704
4705 /*
4706 * If the number of missing devices is larger than max errors,
4707 * we can not write the data into that chunk successfully, so
4708 * set it readonly.
4709 */
4710 if (miss_ndevs > btrfs_chunk_max_errors(map))
4711 readonly = 1;
4712end:
Yan Zheng2b820322008-11-17 21:11:30 -05004713 free_extent_map(em);
4714 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004715}
4716
4717void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4718{
David Sterbaa8067e02011-04-21 00:34:43 +02004719 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004720}
4721
4722void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4723{
4724 struct extent_map *em;
4725
Chris Masond3977122009-01-05 21:25:51 -05004726 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004727 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004728 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4729 if (em)
4730 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004731 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004732 if (!em)
4733 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004734 /* once for us */
4735 free_extent_map(em);
4736 /* once for the tree */
4737 free_extent_map(em);
4738 }
4739}
4740
Stefan Behrens5d964052012-11-05 14:59:07 +01004741int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004742{
Stefan Behrens5d964052012-11-05 14:59:07 +01004743 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004744 struct extent_map *em;
4745 struct map_lookup *map;
4746 struct extent_map_tree *em_tree = &map_tree->map_tree;
4747 int ret;
4748
Chris Mason890871b2009-09-02 16:24:52 -04004749 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004750 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004751 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004752
Josef Bacikfb7669b2013-04-23 10:53:18 -04004753 /*
4754 * We could return errors for these cases, but that could get ugly and
4755 * we'd probably do the same thing which is just not do anything else
4756 * and exit, so return 1 so the callers don't try to use other copies.
4757 */
4758 if (!em) {
David Sterba351fd352014-05-15 16:48:20 +02004759 btrfs_crit(fs_info, "No mapping for %Lu-%Lu", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004760 logical+len);
4761 return 1;
4762 }
4763
4764 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004765 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
David Sterba351fd352014-05-15 16:48:20 +02004766 "%Lu-%Lu", logical, logical+len, em->start,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004767 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004768 free_extent_map(em);
Josef Bacikfb7669b2013-04-23 10:53:18 -04004769 return 1;
4770 }
4771
Chris Masonf1885912008-04-09 16:28:12 -04004772 map = (struct map_lookup *)em->bdev;
4773 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4774 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004775 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4776 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004777 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4778 ret = 2;
4779 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4780 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004781 else
4782 ret = 1;
4783 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004784
4785 btrfs_dev_replace_lock(&fs_info->dev_replace);
4786 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4787 ret++;
4788 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4789
Chris Masonf1885912008-04-09 16:28:12 -04004790 return ret;
4791}
4792
David Woodhouse53b381b2013-01-29 18:40:14 -05004793unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4794 struct btrfs_mapping_tree *map_tree,
4795 u64 logical)
4796{
4797 struct extent_map *em;
4798 struct map_lookup *map;
4799 struct extent_map_tree *em_tree = &map_tree->map_tree;
4800 unsigned long len = root->sectorsize;
4801
4802 read_lock(&em_tree->lock);
4803 em = lookup_extent_mapping(em_tree, logical, len);
4804 read_unlock(&em_tree->lock);
4805 BUG_ON(!em);
4806
4807 BUG_ON(em->start > logical || em->start + em->len < logical);
4808 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004809 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004810 len = map->stripe_len * nr_data_stripes(map);
David Woodhouse53b381b2013-01-29 18:40:14 -05004811 free_extent_map(em);
4812 return len;
4813}
4814
4815int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4816 u64 logical, u64 len, int mirror_num)
4817{
4818 struct extent_map *em;
4819 struct map_lookup *map;
4820 struct extent_map_tree *em_tree = &map_tree->map_tree;
4821 int ret = 0;
4822
4823 read_lock(&em_tree->lock);
4824 em = lookup_extent_mapping(em_tree, logical, len);
4825 read_unlock(&em_tree->lock);
4826 BUG_ON(!em);
4827
4828 BUG_ON(em->start > logical || em->start + em->len < logical);
4829 map = (struct map_lookup *)em->bdev;
Zhao Leiffe2d202015-01-20 15:11:44 +08004830 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05004831 ret = 1;
4832 free_extent_map(em);
4833 return ret;
4834}
4835
Stefan Behrens30d98612012-11-06 14:52:18 +01004836static int find_live_mirror(struct btrfs_fs_info *fs_info,
4837 struct map_lookup *map, int first, int num,
4838 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004839{
4840 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004841 int tolerance;
4842 struct btrfs_device *srcdev;
4843
4844 if (dev_replace_is_ongoing &&
4845 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4846 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4847 srcdev = fs_info->dev_replace.srcdev;
4848 else
4849 srcdev = NULL;
4850
4851 /*
4852 * try to avoid the drive that is the source drive for a
4853 * dev-replace procedure, only choose it if no other non-missing
4854 * mirror is available
4855 */
4856 for (tolerance = 0; tolerance < 2; tolerance++) {
4857 if (map->stripes[optimal].dev->bdev &&
4858 (tolerance || map->stripes[optimal].dev != srcdev))
4859 return optimal;
4860 for (i = first; i < first + num; i++) {
4861 if (map->stripes[i].dev->bdev &&
4862 (tolerance || map->stripes[i].dev != srcdev))
4863 return i;
4864 }
Chris Masondfe25022008-05-13 13:46:40 -04004865 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004866
Chris Masondfe25022008-05-13 13:46:40 -04004867 /* we couldn't find one that doesn't fail. Just return something
4868 * and the io error handling code will clean up eventually
4869 */
4870 return optimal;
4871}
4872
David Woodhouse53b381b2013-01-29 18:40:14 -05004873static inline int parity_smaller(u64 a, u64 b)
4874{
4875 return a > b;
4876}
4877
4878/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004879static void sort_parity_stripes(struct btrfs_bio *bbio, int num_stripes)
David Woodhouse53b381b2013-01-29 18:40:14 -05004880{
4881 struct btrfs_bio_stripe s;
4882 int i;
4883 u64 l;
4884 int again = 1;
4885
4886 while (again) {
4887 again = 0;
Zhao Leicc7539e2015-01-20 15:11:32 +08004888 for (i = 0; i < num_stripes - 1; i++) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004889 if (parity_smaller(bbio->raid_map[i],
4890 bbio->raid_map[i+1])) {
David Woodhouse53b381b2013-01-29 18:40:14 -05004891 s = bbio->stripes[i];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004892 l = bbio->raid_map[i];
David Woodhouse53b381b2013-01-29 18:40:14 -05004893 bbio->stripes[i] = bbio->stripes[i+1];
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004894 bbio->raid_map[i] = bbio->raid_map[i+1];
David Woodhouse53b381b2013-01-29 18:40:14 -05004895 bbio->stripes[i+1] = s;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004896 bbio->raid_map[i+1] = l;
Miao Xie2c8cdd62014-11-14 16:06:25 +08004897
David Woodhouse53b381b2013-01-29 18:40:14 -05004898 again = 1;
4899 }
4900 }
4901 }
4902}
4903
Zhao Lei6e9606d2015-01-20 15:11:34 +08004904static struct btrfs_bio *alloc_btrfs_bio(int total_stripes, int real_stripes)
4905{
4906 struct btrfs_bio *bbio = kzalloc(
Chris Masone57cf212015-02-19 17:51:39 -08004907 /* the size of the btrfs_bio */
Zhao Lei6e9606d2015-01-20 15:11:34 +08004908 sizeof(struct btrfs_bio) +
Chris Masone57cf212015-02-19 17:51:39 -08004909 /* plus the variable array for the stripes */
Zhao Lei6e9606d2015-01-20 15:11:34 +08004910 sizeof(struct btrfs_bio_stripe) * (total_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08004911 /* plus the variable array for the tgt dev */
Zhao Lei6e9606d2015-01-20 15:11:34 +08004912 sizeof(int) * (real_stripes) +
Chris Masone57cf212015-02-19 17:51:39 -08004913 /*
4914 * plus the raid_map, which includes both the tgt dev
4915 * and the stripes
4916 */
4917 sizeof(u64) * (total_stripes),
Zhao Lei6e9606d2015-01-20 15:11:34 +08004918 GFP_NOFS);
4919 if (!bbio)
4920 return NULL;
4921
4922 atomic_set(&bbio->error, 0);
4923 atomic_set(&bbio->refs, 1);
4924
4925 return bbio;
4926}
4927
4928void btrfs_get_bbio(struct btrfs_bio *bbio)
4929{
4930 WARN_ON(!atomic_read(&bbio->refs));
4931 atomic_inc(&bbio->refs);
4932}
4933
4934void btrfs_put_bbio(struct btrfs_bio *bbio)
4935{
4936 if (!bbio)
4937 return;
4938 if (atomic_dec_and_test(&bbio->refs))
4939 kfree(bbio);
4940}
4941
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004942static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004943 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004944 struct btrfs_bio **bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08004945 int mirror_num, int need_raid_map)
Chris Mason0b86a832008-03-24 15:01:56 -04004946{
4947 struct extent_map *em;
4948 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004949 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004950 struct extent_map_tree *em_tree = &map_tree->map_tree;
4951 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004952 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004953 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004954 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004955 u64 stripe_nr_orig;
4956 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004957 u64 stripe_len;
David Sterba9d644a62015-02-20 18:42:11 +01004958 u32 stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004959 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004960 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004961 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004962 int max_errors = 0;
Miao Xie2c8cdd62014-11-14 16:06:25 +08004963 int tgtdev_indexes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004964 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004965 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4966 int dev_replace_is_ongoing = 0;
4967 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004968 int patch_the_first_stripe_for_dev_replace = 0;
4969 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004970 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004971
Chris Mason890871b2009-09-02 16:24:52 -04004972 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004973 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004974 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004975
Chris Mason3b951512008-04-17 11:29:12 -04004976 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004977 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004978 logical, *length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004979 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004980 }
Chris Mason0b86a832008-03-24 15:01:56 -04004981
Josef Bacik9bb91872013-04-19 23:45:33 -04004982 if (em->start > logical || em->start + em->len < logical) {
4983 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
David Sterba351fd352014-05-15 16:48:20 +02004984 "found %Lu-%Lu", logical, em->start,
Josef Bacik9bb91872013-04-19 23:45:33 -04004985 em->start + em->len);
Liu Bo7d3d1742013-09-29 10:33:16 +08004986 free_extent_map(em);
Josef Bacik9bb91872013-04-19 23:45:33 -04004987 return -EINVAL;
4988 }
4989
Chris Mason0b86a832008-03-24 15:01:56 -04004990 map = (struct map_lookup *)em->bdev;
4991 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004992
David Woodhouse53b381b2013-01-29 18:40:14 -05004993 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004994 stripe_nr = offset;
4995 /*
4996 * stripe_nr counts the total number of stripes we have to stride
4997 * to get to this block
4998 */
David Sterba47c57132015-02-20 18:43:47 +01004999 stripe_nr = div64_u64(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04005000
David Woodhouse53b381b2013-01-29 18:40:14 -05005001 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04005002 BUG_ON(offset < stripe_offset);
5003
5004 /* stripe_offset is the offset of this block in its stripe*/
5005 stripe_offset = offset - stripe_offset;
5006
David Woodhouse53b381b2013-01-29 18:40:14 -05005007 /* if we're here for raid56, we need to know the stripe aligned start */
Zhao Leiffe2d202015-01-20 15:11:44 +08005008 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005009 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
5010 raid56_full_stripe_start = offset;
5011
5012 /* allow a write of a full stripe, but make sure we don't
5013 * allow straddling of stripes
5014 */
David Sterba47c57132015-02-20 18:43:47 +01005015 raid56_full_stripe_start = div64_u64(raid56_full_stripe_start,
5016 full_stripe_len);
David Woodhouse53b381b2013-01-29 18:40:14 -05005017 raid56_full_stripe_start *= full_stripe_len;
5018 }
5019
5020 if (rw & REQ_DISCARD) {
5021 /* we don't discard raid56 yet */
Zhao Leiffe2d202015-01-20 15:11:44 +08005022 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005023 ret = -EOPNOTSUPP;
5024 goto out;
5025 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005026 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005027 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
5028 u64 max_len;
5029 /* For writes to RAID[56], allow a full stripeset across all disks.
5030 For other RAID types and for RAID[56] reads, just allow a single
5031 stripe (on a single disk). */
Zhao Leiffe2d202015-01-20 15:11:44 +08005032 if ((map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
David Woodhouse53b381b2013-01-29 18:40:14 -05005033 (rw & REQ_WRITE)) {
5034 max_len = stripe_len * nr_data_stripes(map) -
5035 (offset - raid56_full_stripe_start);
5036 } else {
5037 /* we limit the length of each bio to what fits in a stripe */
5038 max_len = stripe_len - stripe_offset;
5039 }
5040 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04005041 } else {
5042 *length = em->len - offset;
5043 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005044
David Woodhouse53b381b2013-01-29 18:40:14 -05005045 /* This is for when we're called from btrfs_merge_bio_hook() and all
5046 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005047 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04005048 goto out;
5049
Stefan Behrens472262f2012-11-06 14:43:46 +01005050 btrfs_dev_replace_lock(dev_replace);
5051 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
5052 if (!dev_replace_is_ongoing)
5053 btrfs_dev_replace_unlock(dev_replace);
5054
Stefan Behrensad6d6202012-11-06 15:06:47 +01005055 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
5056 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
5057 dev_replace->tgtdev != NULL) {
5058 /*
5059 * in dev-replace case, for repair case (that's the only
5060 * case where the mirror is selected explicitly when
5061 * calling btrfs_map_block), blocks left of the left cursor
5062 * can also be read from the target drive.
5063 * For REQ_GET_READ_MIRRORS, the target drive is added as
5064 * the last one to the array of stripes. For READ, it also
5065 * needs to be supported using the same mirror number.
5066 * If the requested block is not left of the left cursor,
5067 * EIO is returned. This can happen because btrfs_num_copies()
5068 * returns one more in the dev-replace case.
5069 */
5070 u64 tmp_length = *length;
5071 struct btrfs_bio *tmp_bbio = NULL;
5072 int tmp_num_stripes;
5073 u64 srcdev_devid = dev_replace->srcdev->devid;
5074 int index_srcdev = 0;
5075 int found = 0;
5076 u64 physical_of_found = 0;
5077
5078 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005079 logical, &tmp_length, &tmp_bbio, 0, 0);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005080 if (ret) {
5081 WARN_ON(tmp_bbio != NULL);
5082 goto out;
5083 }
5084
5085 tmp_num_stripes = tmp_bbio->num_stripes;
5086 if (mirror_num > tmp_num_stripes) {
5087 /*
5088 * REQ_GET_READ_MIRRORS does not contain this
5089 * mirror, that means that the requested area
5090 * is not left of the left cursor
5091 */
5092 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005093 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005094 goto out;
5095 }
5096
5097 /*
5098 * process the rest of the function using the mirror_num
5099 * of the source drive. Therefore look it up first.
5100 * At the end, patch the device pointer to the one of the
5101 * target drive.
5102 */
5103 for (i = 0; i < tmp_num_stripes; i++) {
5104 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
5105 /*
5106 * In case of DUP, in order to keep it
5107 * simple, only add the mirror with the
5108 * lowest physical address
5109 */
5110 if (found &&
5111 physical_of_found <=
5112 tmp_bbio->stripes[i].physical)
5113 continue;
5114 index_srcdev = i;
5115 found = 1;
5116 physical_of_found =
5117 tmp_bbio->stripes[i].physical;
5118 }
5119 }
5120
5121 if (found) {
5122 mirror_num = index_srcdev + 1;
5123 patch_the_first_stripe_for_dev_replace = 1;
5124 physical_to_patch_in_first_stripe = physical_of_found;
5125 } else {
5126 WARN_ON(1);
5127 ret = -EIO;
Zhao Lei6e9606d2015-01-20 15:11:34 +08005128 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005129 goto out;
5130 }
5131
Zhao Lei6e9606d2015-01-20 15:11:34 +08005132 btrfs_put_bbio(tmp_bbio);
Stefan Behrensad6d6202012-11-06 15:06:47 +01005133 } else if (mirror_num > map->num_stripes) {
5134 mirror_num = 0;
5135 }
5136
Chris Masonf2d8d742008-04-21 10:03:05 -04005137 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04005138 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005139 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00005140 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
David Sterbab8b93ad2015-01-16 17:26:13 +01005141 stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00005142 stripe_end_offset = stripe_nr_end * map->stripe_len -
5143 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005144
Li Dongyangfce3bb92011-03-24 10:24:26 +00005145 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5146 if (rw & REQ_DISCARD)
5147 num_stripes = min_t(u64, map->num_stripes,
5148 stripe_nr_end - stripe_nr_orig);
David Sterba47c57132015-02-20 18:43:47 +01005149 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5150 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005151 if (!(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)))
5152 mirror_num = 1;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005153 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005154 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005155 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04005156 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04005157 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005158 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01005159 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04005160 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01005161 current->pid % map->num_stripes,
5162 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005163 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005164 }
Chris Mason2fff7342008-04-29 14:12:09 -04005165
Chris Mason611f0e02008-04-03 16:29:03 -04005166 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005167 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04005168 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005169 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04005170 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005171 } else {
5172 mirror_num = 1;
5173 }
Chris Mason2fff7342008-04-29 14:12:09 -04005174
Chris Mason321aecc2008-04-16 10:49:51 -04005175 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
David Sterba9d644a62015-02-20 18:42:11 +01005176 u32 factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04005177
David Sterba47c57132015-02-20 18:43:47 +01005178 stripe_nr = div_u64_rem(stripe_nr, factor, &stripe_index);
Chris Mason321aecc2008-04-16 10:49:51 -04005179 stripe_index *= map->sub_stripes;
5180
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005181 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04005182 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005183 else if (rw & REQ_DISCARD)
5184 num_stripes = min_t(u64, map->sub_stripes *
5185 (stripe_nr_end - stripe_nr_orig),
5186 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04005187 else if (mirror_num)
5188 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04005189 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04005190 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01005191 stripe_index = find_live_mirror(fs_info, map,
5192 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04005193 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01005194 current->pid % map->sub_stripes,
5195 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04005196 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04005197 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005198
Zhao Leiffe2d202015-01-20 15:11:44 +08005199 } else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005200 if (need_raid_map &&
Miao Xieaf8e2d12014-10-23 14:42:50 +08005201 ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5202 mirror_num > 1)) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005203 /* push stripe_nr back to the start of the full stripe */
David Sterbab8b93ad2015-01-16 17:26:13 +01005204 stripe_nr = div_u64(raid56_full_stripe_start,
5205 stripe_len * nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005206
5207 /* RAID[56] write or recovery. Return all stripes */
5208 num_stripes = map->num_stripes;
5209 max_errors = nr_parity_stripes(map);
5210
David Woodhouse53b381b2013-01-29 18:40:14 -05005211 *length = map->stripe_len;
5212 stripe_index = 0;
5213 stripe_offset = 0;
5214 } else {
5215 /*
5216 * Mirror #0 or #1 means the original data block.
5217 * Mirror #2 is RAID5 parity block.
5218 * Mirror #3 is RAID6 Q block.
5219 */
David Sterba47c57132015-02-20 18:43:47 +01005220 stripe_nr = div_u64_rem(stripe_nr,
5221 nr_data_stripes(map), &stripe_index);
David Woodhouse53b381b2013-01-29 18:40:14 -05005222 if (mirror_num > 1)
5223 stripe_index = nr_data_stripes(map) +
5224 mirror_num - 2;
5225
5226 /* We distribute the parity blocks across stripes */
David Sterba47c57132015-02-20 18:43:47 +01005227 div_u64_rem(stripe_nr + stripe_index, map->num_stripes,
5228 &stripe_index);
Miao Xie28e1cc72014-09-12 18:44:02 +08005229 if (!(rw & (REQ_WRITE | REQ_DISCARD |
5230 REQ_GET_READ_MIRRORS)) && mirror_num <= 1)
5231 mirror_num = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005232 }
Chris Mason8790d502008-04-03 16:29:03 -04005233 } else {
5234 /*
David Sterba47c57132015-02-20 18:43:47 +01005235 * after this, stripe_nr is the number of stripes on this
5236 * device we have to walk to find the data, and stripe_index is
5237 * the number of our device in the stripe array
Chris Mason8790d502008-04-03 16:29:03 -04005238 */
David Sterba47c57132015-02-20 18:43:47 +01005239 stripe_nr = div_u64_rem(stripe_nr, map->num_stripes,
5240 &stripe_index);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005241 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04005242 }
Chris Mason593060d2008-03-25 16:50:33 -04005243 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04005244
Stefan Behrens472262f2012-11-06 14:43:46 +01005245 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005246 if (dev_replace_is_ongoing) {
5247 if (rw & (REQ_WRITE | REQ_DISCARD))
5248 num_alloc_stripes <<= 1;
5249 if (rw & REQ_GET_READ_MIRRORS)
5250 num_alloc_stripes++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005251 tgtdev_indexes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005252 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005253
Zhao Lei6e9606d2015-01-20 15:11:34 +08005254 bbio = alloc_btrfs_bio(num_alloc_stripes, tgtdev_indexes);
Li Zefande11cc12011-12-01 12:55:47 +08005255 if (!bbio) {
5256 ret = -ENOMEM;
5257 goto out;
5258 }
Miao Xie2c8cdd62014-11-14 16:06:25 +08005259 if (dev_replace_is_ongoing)
5260 bbio->tgtdev_map = (int *)(bbio->stripes + num_alloc_stripes);
Li Zefande11cc12011-12-01 12:55:47 +08005261
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005262 /* build raid_map */
Zhao Leiffe2d202015-01-20 15:11:44 +08005263 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK &&
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005264 need_raid_map && ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
5265 mirror_num > 1)) {
5266 u64 tmp;
David Sterba9d644a62015-02-20 18:42:11 +01005267 unsigned rot;
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005268
5269 bbio->raid_map = (u64 *)((void *)bbio->stripes +
5270 sizeof(struct btrfs_bio_stripe) *
5271 num_alloc_stripes +
5272 sizeof(int) * tgtdev_indexes);
5273
5274 /* Work out the disk rotation on this stripe-set */
David Sterba47c57132015-02-20 18:43:47 +01005275 div_u64_rem(stripe_nr, num_stripes, &rot);
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005276
5277 /* Fill in the logical address of each stripe */
5278 tmp = stripe_nr * nr_data_stripes(map);
5279 for (i = 0; i < nr_data_stripes(map); i++)
5280 bbio->raid_map[(i+rot) % num_stripes] =
5281 em->start + (tmp + i) * map->stripe_len;
5282
5283 bbio->raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
5284 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
5285 bbio->raid_map[(i+rot+1) % num_stripes] =
5286 RAID6_Q_STRIPE;
5287 }
5288
Li Dongyangfce3bb92011-03-24 10:24:26 +00005289 if (rw & REQ_DISCARD) {
David Sterba9d644a62015-02-20 18:42:11 +01005290 u32 factor = 0;
5291 u32 sub_stripes = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005292 u64 stripes_per_dev = 0;
5293 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04005294 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005295
5296 if (map->type &
5297 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
5298 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5299 sub_stripes = 1;
5300 else
5301 sub_stripes = map->sub_stripes;
5302
5303 factor = map->num_stripes / sub_stripes;
5304 stripes_per_dev = div_u64_rem(stripe_nr_end -
5305 stripe_nr_orig,
5306 factor,
5307 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04005308 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
5309 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08005310 }
5311
Li Dongyangfce3bb92011-03-24 10:24:26 +00005312 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005313 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04005314 map->stripes[stripe_index].physical +
5315 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005316 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005317
Li Zefanec9ef7a2011-12-01 14:06:42 +08005318 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
5319 BTRFS_BLOCK_GROUP_RAID10)) {
5320 bbio->stripes[i].length = stripes_per_dev *
5321 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005322
Li Zefanec9ef7a2011-12-01 14:06:42 +08005323 if (i / sub_stripes < remaining_stripes)
5324 bbio->stripes[i].length +=
5325 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04005326
5327 /*
5328 * Special for the first stripe and
5329 * the last stripe:
5330 *
5331 * |-------|...|-------|
5332 * |----------|
5333 * off end_off
5334 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08005335 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02005336 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00005337 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005338
5339 if (stripe_index >= last_stripe &&
5340 stripe_index <= (last_stripe +
5341 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08005342 bbio->stripes[i].length -=
5343 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04005344
Li Zefanec9ef7a2011-12-01 14:06:42 +08005345 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00005346 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005347 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02005348 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005349
5350 stripe_index++;
5351 if (stripe_index == map->num_stripes) {
5352 /* This could only happen for RAID0/10 */
5353 stripe_index = 0;
5354 stripe_nr++;
5355 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005356 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00005357 } else {
5358 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005359 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005360 map->stripes[stripe_index].physical +
5361 stripe_offset +
5362 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005363 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005364 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005365 stripe_index++;
5366 }
Chris Mason593060d2008-03-25 16:50:33 -04005367 }
Li Zefande11cc12011-12-01 12:55:47 +08005368
Miao Xied20983b2014-07-03 18:22:13 +08005369 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
5370 max_errors = btrfs_chunk_max_errors(map);
Li Zefande11cc12011-12-01 12:55:47 +08005371
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005372 if (bbio->raid_map)
5373 sort_parity_stripes(bbio, num_stripes);
Zhao Leicc7539e2015-01-20 15:11:32 +08005374
Miao Xie2c8cdd62014-11-14 16:06:25 +08005375 tgtdev_indexes = 0;
Stefan Behrens472262f2012-11-06 14:43:46 +01005376 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5377 dev_replace->tgtdev != NULL) {
5378 int index_where_to_add;
5379 u64 srcdev_devid = dev_replace->srcdev->devid;
5380
5381 /*
5382 * duplicate the write operations while the dev replace
5383 * procedure is running. Since the copying of the old disk
5384 * to the new disk takes place at run time while the
5385 * filesystem is mounted writable, the regular write
5386 * operations to the old disk have to be duplicated to go
5387 * to the new disk as well.
5388 * Note that device->missing is handled by the caller, and
5389 * that the write to the old disk is already set up in the
5390 * stripes array.
5391 */
5392 index_where_to_add = num_stripes;
5393 for (i = 0; i < num_stripes; i++) {
5394 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5395 /* write to new disk, too */
5396 struct btrfs_bio_stripe *new =
5397 bbio->stripes + index_where_to_add;
5398 struct btrfs_bio_stripe *old =
5399 bbio->stripes + i;
5400
5401 new->physical = old->physical;
5402 new->length = old->length;
5403 new->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005404 bbio->tgtdev_map[i] = index_where_to_add;
Stefan Behrens472262f2012-11-06 14:43:46 +01005405 index_where_to_add++;
5406 max_errors++;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005407 tgtdev_indexes++;
Stefan Behrens472262f2012-11-06 14:43:46 +01005408 }
5409 }
5410 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005411 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5412 dev_replace->tgtdev != NULL) {
5413 u64 srcdev_devid = dev_replace->srcdev->devid;
5414 int index_srcdev = 0;
5415 int found = 0;
5416 u64 physical_of_found = 0;
5417
5418 /*
5419 * During the dev-replace procedure, the target drive can
5420 * also be used to read data in case it is needed to repair
5421 * a corrupt block elsewhere. This is possible if the
5422 * requested area is left of the left cursor. In this area,
5423 * the target drive is a full copy of the source drive.
5424 */
5425 for (i = 0; i < num_stripes; i++) {
5426 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5427 /*
5428 * In case of DUP, in order to keep it
5429 * simple, only add the mirror with the
5430 * lowest physical address
5431 */
5432 if (found &&
5433 physical_of_found <=
5434 bbio->stripes[i].physical)
5435 continue;
5436 index_srcdev = i;
5437 found = 1;
5438 physical_of_found = bbio->stripes[i].physical;
5439 }
5440 }
5441 if (found) {
David Sterba258ece02015-02-24 19:45:15 +01005442 if (physical_of_found + map->stripe_len <=
Stefan Behrensad6d6202012-11-06 15:06:47 +01005443 dev_replace->cursor_left) {
5444 struct btrfs_bio_stripe *tgtdev_stripe =
5445 bbio->stripes + num_stripes;
5446
5447 tgtdev_stripe->physical = physical_of_found;
5448 tgtdev_stripe->length =
5449 bbio->stripes[index_srcdev].length;
5450 tgtdev_stripe->dev = dev_replace->tgtdev;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005451 bbio->tgtdev_map[index_srcdev] = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005452
Miao Xie2c8cdd62014-11-14 16:06:25 +08005453 tgtdev_indexes++;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005454 num_stripes++;
5455 }
5456 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005457 }
5458
Li Zefande11cc12011-12-01 12:55:47 +08005459 *bbio_ret = bbio;
Zhao Lei10f11902015-01-20 15:11:43 +08005460 bbio->map_type = map->type;
Li Zefande11cc12011-12-01 12:55:47 +08005461 bbio->num_stripes = num_stripes;
5462 bbio->max_errors = max_errors;
5463 bbio->mirror_num = mirror_num;
Miao Xie2c8cdd62014-11-14 16:06:25 +08005464 bbio->num_tgtdevs = tgtdev_indexes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005465
5466 /*
5467 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5468 * mirror_num == num_stripes + 1 && dev_replace target drive is
5469 * available as a mirror
5470 */
5471 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5472 WARN_ON(num_stripes > 1);
5473 bbio->stripes[0].dev = dev_replace->tgtdev;
5474 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5475 bbio->mirror_num = map->num_stripes + 1;
5476 }
Chris Masoncea9e442008-04-09 16:28:12 -04005477out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005478 if (dev_replace_is_ongoing)
5479 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005480 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005481 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005482}
5483
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005484int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005485 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005486 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005487{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005488 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005489 mirror_num, 0);
Chris Masonf2d8d742008-04-21 10:03:05 -04005490}
5491
Miao Xieaf8e2d12014-10-23 14:42:50 +08005492/* For Scrub/replace */
5493int btrfs_map_sblock(struct btrfs_fs_info *fs_info, int rw,
5494 u64 logical, u64 *length,
5495 struct btrfs_bio **bbio_ret, int mirror_num,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005496 int need_raid_map)
Miao Xieaf8e2d12014-10-23 14:42:50 +08005497{
5498 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005499 mirror_num, need_raid_map);
Miao Xieaf8e2d12014-10-23 14:42:50 +08005500}
5501
Yan Zhenga512bbf2008-12-08 16:46:26 -05005502int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5503 u64 chunk_start, u64 physical, u64 devid,
5504 u64 **logical, int *naddrs, int *stripe_len)
5505{
5506 struct extent_map_tree *em_tree = &map_tree->map_tree;
5507 struct extent_map *em;
5508 struct map_lookup *map;
5509 u64 *buf;
5510 u64 bytenr;
5511 u64 length;
5512 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005513 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005514 int i, j, nr = 0;
5515
Chris Mason890871b2009-09-02 16:24:52 -04005516 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005517 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005518 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005519
Josef Bacik835d9742013-03-19 12:13:25 -04005520 if (!em) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005521 printk(KERN_ERR "BTRFS: couldn't find em for chunk %Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005522 chunk_start);
5523 return -EIO;
5524 }
5525
5526 if (em->start != chunk_start) {
Frank Holtonefe120a2013-12-20 11:37:06 -05005527 printk(KERN_ERR "BTRFS: bad chunk start, em=%Lu, wanted=%Lu\n",
Josef Bacik835d9742013-03-19 12:13:25 -04005528 em->start, chunk_start);
5529 free_extent_map(em);
5530 return -EIO;
5531 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005532 map = (struct map_lookup *)em->bdev;
5533
5534 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005535 rmap_len = map->stripe_len;
5536
Yan Zhenga512bbf2008-12-08 16:46:26 -05005537 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
David Sterbab8b93ad2015-01-16 17:26:13 +01005538 length = div_u64(length, map->num_stripes / map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005539 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
David Sterbab8b93ad2015-01-16 17:26:13 +01005540 length = div_u64(length, map->num_stripes);
Zhao Leiffe2d202015-01-20 15:11:44 +08005541 else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
David Sterbab8b93ad2015-01-16 17:26:13 +01005542 length = div_u64(length, nr_data_stripes(map));
David Woodhouse53b381b2013-01-29 18:40:14 -05005543 rmap_len = map->stripe_len * nr_data_stripes(map);
5544 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005545
David Sterba31e818f2015-02-20 18:00:26 +01005546 buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005547 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005548
5549 for (i = 0; i < map->num_stripes; i++) {
5550 if (devid && map->stripes[i].dev->devid != devid)
5551 continue;
5552 if (map->stripes[i].physical > physical ||
5553 map->stripes[i].physical + length <= physical)
5554 continue;
5555
5556 stripe_nr = physical - map->stripes[i].physical;
David Sterbab8b93ad2015-01-16 17:26:13 +01005557 stripe_nr = div_u64(stripe_nr, map->stripe_len);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005558
5559 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5560 stripe_nr = stripe_nr * map->num_stripes + i;
David Sterbab8b93ad2015-01-16 17:26:13 +01005561 stripe_nr = div_u64(stripe_nr, map->sub_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005562 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5563 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005564 } /* else if RAID[56], multiply by nr_data_stripes().
5565 * Alternatively, just use rmap_len below instead of
5566 * map->stripe_len */
5567
5568 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005569 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005570 for (j = 0; j < nr; j++) {
5571 if (buf[j] == bytenr)
5572 break;
5573 }
Chris Mason934d3752008-12-08 16:43:10 -05005574 if (j == nr) {
5575 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005576 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005577 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005578 }
5579
Yan Zhenga512bbf2008-12-08 16:46:26 -05005580 *logical = buf;
5581 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005582 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005583
5584 free_extent_map(em);
5585 return 0;
5586}
5587
Miao Xie8408c712014-06-19 10:42:55 +08005588static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5589{
5590 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5591 bio_endio_nodec(bio, err);
5592 else
5593 bio_endio(bio, err);
Zhao Lei6e9606d2015-01-20 15:11:34 +08005594 btrfs_put_bbio(bbio);
Miao Xie8408c712014-06-19 10:42:55 +08005595}
5596
Jan Schmidta1d3c472011-08-04 17:15:33 +02005597static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005598{
Chris Mason9be33952013-05-17 18:30:14 -04005599 struct btrfs_bio *bbio = bio->bi_private;
Miao Xiec404e0d2014-01-30 16:46:55 +08005600 struct btrfs_device *dev = bbio->stripes[0].dev;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005601 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005602
Stefan Behrens442a4f62012-05-25 16:06:08 +02005603 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005604 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005605 if (err == -EIO || err == -EREMOTEIO) {
5606 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005607 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005608
5609 BUG_ON(stripe_index >= bbio->num_stripes);
5610 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005611 if (dev->bdev) {
5612 if (bio->bi_rw & WRITE)
5613 btrfs_dev_stat_inc(dev,
5614 BTRFS_DEV_STAT_WRITE_ERRS);
5615 else
5616 btrfs_dev_stat_inc(dev,
5617 BTRFS_DEV_STAT_READ_ERRS);
5618 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5619 btrfs_dev_stat_inc(dev,
5620 BTRFS_DEV_STAT_FLUSH_ERRS);
5621 btrfs_dev_stat_print_on_error(dev);
5622 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005623 }
5624 }
Chris Mason8790d502008-04-03 16:29:03 -04005625
Jan Schmidta1d3c472011-08-04 17:15:33 +02005626 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005627 is_orig_bio = 1;
5628
Miao Xiec404e0d2014-01-30 16:46:55 +08005629 btrfs_bio_counter_dec(bbio->fs_info);
5630
Jan Schmidta1d3c472011-08-04 17:15:33 +02005631 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005632 if (!is_orig_bio) {
5633 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005634 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005635 }
Muthu Kumarc7b22bb2014-01-08 14:19:52 -07005636
Jan Schmidta1d3c472011-08-04 17:15:33 +02005637 bio->bi_private = bbio->private;
5638 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005639 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005640 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005641 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005642 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005643 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005644 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005645 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005646 /*
5647 * this bio is actually up to date, we didn't
5648 * go over the max number of errors
5649 */
5650 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005651 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005652 }
Miao Xiec55f1392014-06-19 10:42:54 +08005653
Miao Xie8408c712014-06-19 10:42:55 +08005654 btrfs_end_bbio(bbio, bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005655 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005656 bio_put(bio);
5657 }
Chris Mason8790d502008-04-03 16:29:03 -04005658}
5659
Chris Mason8b712842008-06-11 16:50:36 -04005660/*
5661 * see run_scheduled_bios for a description of why bios are collected for
5662 * async submit.
5663 *
5664 * This will add one bio to the pending list for a device and make sure
5665 * the work struct is scheduled.
5666 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005667static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5668 struct btrfs_device *device,
5669 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005670{
5671 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005672 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005673
David Woodhouse53b381b2013-01-29 18:40:14 -05005674 if (device->missing || !device->bdev) {
5675 bio_endio(bio, -EIO);
5676 return;
5677 }
5678
Chris Mason8b712842008-06-11 16:50:36 -04005679 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005680 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005681 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005682 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005683 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005684 return;
Chris Mason8b712842008-06-11 16:50:36 -04005685 }
5686
5687 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005688 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005689 * higher layers. Otherwise, the async bio makes it appear we have
5690 * made progress against dirty pages when we've really just put it
5691 * on a queue for later
5692 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005693 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005694 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005695 bio->bi_next = NULL;
5696 bio->bi_rw |= rw;
5697
5698 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005699 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005700 pending_bios = &device->pending_sync_bios;
5701 else
5702 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005703
Chris Masonffbd5172009-04-20 15:50:09 -04005704 if (pending_bios->tail)
5705 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005706
Chris Masonffbd5172009-04-20 15:50:09 -04005707 pending_bios->tail = bio;
5708 if (!pending_bios->head)
5709 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005710 if (device->running_pending)
5711 should_queue = 0;
5712
5713 spin_unlock(&device->io_lock);
5714
5715 if (should_queue)
Qu Wenruoa8c93d42014-02-28 10:46:08 +08005716 btrfs_queue_work(root->fs_info->submit_workers,
5717 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005718}
5719
Josef Bacikde1ee922012-10-19 16:50:56 -04005720static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5721 sector_t sector)
5722{
5723 struct bio_vec *prev;
5724 struct request_queue *q = bdev_get_queue(bdev);
Akinobu Mita475bf362013-11-18 22:13:18 +09005725 unsigned int max_sectors = queue_max_sectors(q);
Josef Bacikde1ee922012-10-19 16:50:56 -04005726 struct bvec_merge_data bvm = {
5727 .bi_bdev = bdev,
5728 .bi_sector = sector,
5729 .bi_rw = bio->bi_rw,
5730 };
5731
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305732 if (WARN_ON(bio->bi_vcnt == 0))
Josef Bacikde1ee922012-10-19 16:50:56 -04005733 return 1;
Josef Bacikde1ee922012-10-19 16:50:56 -04005734
5735 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005736 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005737 return 0;
5738
5739 if (!q->merge_bvec_fn)
5740 return 1;
5741
Kent Overstreet4f024f32013-10-11 15:44:27 -07005742 bvm.bi_size = bio->bi_iter.bi_size - prev->bv_len;
Josef Bacikde1ee922012-10-19 16:50:56 -04005743 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5744 return 0;
5745 return 1;
5746}
5747
5748static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5749 struct bio *bio, u64 physical, int dev_nr,
5750 int rw, int async)
5751{
5752 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5753
5754 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005755 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005756 bio->bi_end_io = btrfs_end_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005757 bio->bi_iter.bi_sector = physical >> 9;
Josef Bacikde1ee922012-10-19 16:50:56 -04005758#ifdef DEBUG
5759 {
5760 struct rcu_string *name;
5761
5762 rcu_read_lock();
5763 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005764 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005765 "(%s id %llu), size=%u\n", rw,
Fabian Frederick1b6e4462014-09-24 20:23:05 +02005766 (u64)bio->bi_iter.bi_sector, (u_long)dev->bdev->bd_dev,
5767 name->str, dev->devid, bio->bi_iter.bi_size);
Josef Bacikde1ee922012-10-19 16:50:56 -04005768 rcu_read_unlock();
5769 }
5770#endif
5771 bio->bi_bdev = dev->bdev;
Miao Xiec404e0d2014-01-30 16:46:55 +08005772
5773 btrfs_bio_counter_inc_noblocked(root->fs_info);
5774
Josef Bacikde1ee922012-10-19 16:50:56 -04005775 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005776 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005777 else
5778 btrfsic_submit_bio(rw, bio);
5779}
5780
5781static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5782 struct bio *first_bio, struct btrfs_device *dev,
5783 int dev_nr, int rw, int async)
5784{
5785 struct bio_vec *bvec = first_bio->bi_io_vec;
5786 struct bio *bio;
5787 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5788 u64 physical = bbio->stripes[dev_nr].physical;
5789
5790again:
5791 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5792 if (!bio)
5793 return -ENOMEM;
5794
5795 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5796 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5797 bvec->bv_offset) < bvec->bv_len) {
Kent Overstreet4f024f32013-10-11 15:44:27 -07005798 u64 len = bio->bi_iter.bi_size;
Josef Bacikde1ee922012-10-19 16:50:56 -04005799
5800 atomic_inc(&bbio->stripes_pending);
5801 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5802 rw, async);
5803 physical += len;
5804 goto again;
5805 }
5806 bvec++;
5807 }
5808
5809 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5810 return 0;
5811}
5812
5813static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5814{
5815 atomic_inc(&bbio->error);
5816 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Miao Xie8408c712014-06-19 10:42:55 +08005817 /* Shoud be the original bio. */
5818 WARN_ON(bio != bbio->orig_bio);
5819
Josef Bacikde1ee922012-10-19 16:50:56 -04005820 bio->bi_private = bbio->private;
5821 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005822 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005823 bio->bi_iter.bi_sector = logical >> 9;
Miao Xie8408c712014-06-19 10:42:55 +08005824
5825 btrfs_end_bbio(bbio, bio, -EIO);
Josef Bacikde1ee922012-10-19 16:50:56 -04005826 }
5827}
5828
Chris Masonf1885912008-04-09 16:28:12 -04005829int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005830 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005831{
Chris Mason0b86a832008-03-24 15:01:56 -04005832 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005833 struct bio *first_bio = bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005834 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005835 u64 length = 0;
5836 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04005837 int ret;
Zhao Lei08da7572015-02-12 15:42:16 +08005838 int dev_nr;
5839 int total_devs;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005840 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005841
Kent Overstreet4f024f32013-10-11 15:44:27 -07005842 length = bio->bi_iter.bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005843 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005844
Miao Xiec404e0d2014-01-30 16:46:55 +08005845 btrfs_bio_counter_inc_blocked(root->fs_info);
David Woodhouse53b381b2013-01-29 18:40:14 -05005846 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005847 mirror_num, 1);
Miao Xiec404e0d2014-01-30 16:46:55 +08005848 if (ret) {
5849 btrfs_bio_counter_dec(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005850 return ret;
Miao Xiec404e0d2014-01-30 16:46:55 +08005851 }
Chris Masoncea9e442008-04-09 16:28:12 -04005852
Jan Schmidta1d3c472011-08-04 17:15:33 +02005853 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005854 bbio->orig_bio = first_bio;
5855 bbio->private = first_bio->bi_private;
5856 bbio->end_io = first_bio->bi_end_io;
Miao Xiec404e0d2014-01-30 16:46:55 +08005857 bbio->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05005858 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5859
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005860 if (bbio->raid_map) {
David Woodhouse53b381b2013-01-29 18:40:14 -05005861 /* In this case, map_length has been set to the length of
5862 a single stripe; not the whole write */
5863 if (rw & WRITE) {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005864 ret = raid56_parity_write(root, bio, bbio, map_length);
David Woodhouse53b381b2013-01-29 18:40:14 -05005865 } else {
Zhao Lei8e5cfb52015-01-20 15:11:33 +08005866 ret = raid56_parity_recover(root, bio, bbio, map_length,
Miao Xie42452152014-11-25 16:39:28 +08005867 mirror_num, 1);
David Woodhouse53b381b2013-01-29 18:40:14 -05005868 }
Miao Xie42452152014-11-25 16:39:28 +08005869
Miao Xiec404e0d2014-01-30 16:46:55 +08005870 btrfs_bio_counter_dec(root->fs_info);
5871 return ret;
David Woodhouse53b381b2013-01-29 18:40:14 -05005872 }
5873
Chris Masoncea9e442008-04-09 16:28:12 -04005874 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005875 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005876 logical, length, map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005877 BUG();
5878 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005879
Zhao Lei08da7572015-02-12 15:42:16 +08005880 for (dev_nr = 0; dev_nr < total_devs; dev_nr++) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005881 dev = bbio->stripes[dev_nr].dev;
5882 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5883 bbio_error(bbio, first_bio, logical);
Josef Bacikde1ee922012-10-19 16:50:56 -04005884 continue;
5885 }
5886
5887 /*
5888 * Check and see if we're ok with this bio based on it's size
5889 * and offset with the given device.
5890 */
5891 if (!bio_size_ok(dev->bdev, first_bio,
5892 bbio->stripes[dev_nr].physical >> 9)) {
5893 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5894 dev_nr, rw, async_submit);
5895 BUG_ON(ret);
Josef Bacikde1ee922012-10-19 16:50:56 -04005896 continue;
5897 }
5898
Jan Schmidta1d3c472011-08-04 17:15:33 +02005899 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005900 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005901 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005902 } else {
5903 bio = first_bio;
Miao Xiec55f1392014-06-19 10:42:54 +08005904 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
Chris Mason8790d502008-04-03 16:29:03 -04005905 }
Josef Bacik606686e2012-06-04 14:03:51 -04005906
Josef Bacikde1ee922012-10-19 16:50:56 -04005907 submit_stripe_bio(root, bbio, bio,
5908 bbio->stripes[dev_nr].physical, dev_nr, rw,
5909 async_submit);
Chris Mason239b14b2008-03-24 15:02:07 -04005910 }
Miao Xiec404e0d2014-01-30 16:46:55 +08005911 btrfs_bio_counter_dec(root->fs_info);
Chris Mason0b86a832008-03-24 15:01:56 -04005912 return 0;
5913}
5914
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005915struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005916 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005917{
Yan Zheng2b820322008-11-17 21:11:30 -05005918 struct btrfs_device *device;
5919 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005920
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005921 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005922 while (cur_devices) {
5923 if (!fsid ||
5924 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5925 device = __find_device(&cur_devices->devices,
5926 devid, uuid);
5927 if (device)
5928 return device;
5929 }
5930 cur_devices = cur_devices->seed;
5931 }
5932 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005933}
5934
Chris Masondfe25022008-05-13 13:46:40 -04005935static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
Miao Xie5f375832014-09-03 21:35:46 +08005936 struct btrfs_fs_devices *fs_devices,
Chris Masondfe25022008-05-13 13:46:40 -04005937 u64 devid, u8 *dev_uuid)
5938{
5939 struct btrfs_device *device;
Chris Masondfe25022008-05-13 13:46:40 -04005940
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005941 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5942 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005943 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005944
5945 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005946 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005947 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005948
5949 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005950 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005951
Chris Masondfe25022008-05-13 13:46:40 -04005952 return device;
5953}
5954
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005955/**
5956 * btrfs_alloc_device - allocate struct btrfs_device
5957 * @fs_info: used only for generating a new devid, can be NULL if
5958 * devid is provided (i.e. @devid != NULL).
5959 * @devid: a pointer to devid for this device. If NULL a new devid
5960 * is generated.
5961 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5962 * is generated.
5963 *
5964 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5965 * on error. Returned struct is not linked onto any lists and can be
5966 * destroyed with kfree() right away.
5967 */
5968struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5969 const u64 *devid,
5970 const u8 *uuid)
5971{
5972 struct btrfs_device *dev;
5973 u64 tmp;
5974
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305975 if (WARN_ON(!devid && !fs_info))
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005976 return ERR_PTR(-EINVAL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005977
5978 dev = __alloc_device();
5979 if (IS_ERR(dev))
5980 return dev;
5981
5982 if (devid)
5983 tmp = *devid;
5984 else {
5985 int ret;
5986
5987 ret = find_next_devid(fs_info, &tmp);
5988 if (ret) {
5989 kfree(dev);
5990 return ERR_PTR(ret);
5991 }
5992 }
5993 dev->devid = tmp;
5994
5995 if (uuid)
5996 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5997 else
5998 generate_random_uuid(dev->uuid);
5999
Liu Bo9e0af232014-08-15 23:36:53 +08006000 btrfs_init_work(&dev->work, btrfs_submit_helper,
6001 pending_bios_fn, NULL, NULL);
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03006002
6003 return dev;
6004}
6005
Chris Mason0b86a832008-03-24 15:01:56 -04006006static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6007 struct extent_buffer *leaf,
6008 struct btrfs_chunk *chunk)
6009{
6010 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6011 struct map_lookup *map;
6012 struct extent_map *em;
6013 u64 logical;
6014 u64 length;
6015 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04006016 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04006017 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04006018 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04006019 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04006020
Chris Masone17cade2008-04-15 15:41:47 -04006021 logical = key->offset;
6022 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04006023
Chris Mason890871b2009-09-02 16:24:52 -04006024 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006025 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04006026 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04006027
6028 /* already mapped? */
6029 if (em && em->start <= logical && em->start + em->len > logical) {
6030 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04006031 return 0;
6032 } else if (em) {
6033 free_extent_map(em);
6034 }
Chris Mason0b86a832008-03-24 15:01:56 -04006035
David Sterba172ddd62011-04-21 00:48:27 +02006036 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04006037 if (!em)
6038 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04006039 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6040 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04006041 if (!map) {
6042 free_extent_map(em);
6043 return -ENOMEM;
6044 }
6045
Wang Shilong298a8f92014-06-19 10:42:52 +08006046 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
Chris Mason0b86a832008-03-24 15:01:56 -04006047 em->bdev = (struct block_device *)map;
6048 em->start = logical;
6049 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04006050 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006051 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04006052 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04006053
Chris Mason593060d2008-03-25 16:50:33 -04006054 map->num_stripes = num_stripes;
6055 map->io_width = btrfs_chunk_io_width(leaf, chunk);
6056 map->io_align = btrfs_chunk_io_align(leaf, chunk);
6057 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
6058 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6059 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04006060 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04006061 for (i = 0; i < num_stripes; i++) {
6062 map->stripes[i].physical =
6063 btrfs_stripe_offset_nr(leaf, chunk, i);
6064 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04006065 read_extent_buffer(leaf, uuid, (unsigned long)
6066 btrfs_stripe_dev_uuid_nr(chunk, i),
6067 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006068 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
6069 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04006070 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04006071 free_extent_map(em);
6072 return -EIO;
6073 }
Chris Masondfe25022008-05-13 13:46:40 -04006074 if (!map->stripes[i].dev) {
6075 map->stripes[i].dev =
Miao Xie5f375832014-09-03 21:35:46 +08006076 add_missing_dev(root, root->fs_info->fs_devices,
6077 devid, uuid);
Chris Masondfe25022008-05-13 13:46:40 -04006078 if (!map->stripes[i].dev) {
Chris Masondfe25022008-05-13 13:46:40 -04006079 free_extent_map(em);
6080 return -EIO;
6081 }
6082 }
6083 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04006084 }
6085
Chris Mason890871b2009-09-02 16:24:52 -04006086 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04006087 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04006088 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006089 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04006090 free_extent_map(em);
6091
6092 return 0;
6093}
6094
Jeff Mahoney143bede2012-03-01 14:56:26 +01006095static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04006096 struct btrfs_dev_item *dev_item,
6097 struct btrfs_device *device)
6098{
6099 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04006100
6101 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04006102 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
6103 device->total_bytes = device->disk_total_bytes;
Miao Xie935e5cc2014-09-03 21:35:33 +08006104 device->commit_total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04006105 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
Miao Xiece7213c2014-09-03 21:35:34 +08006106 device->commit_bytes_used = device->bytes_used;
Chris Mason0b86a832008-03-24 15:01:56 -04006107 device->type = btrfs_device_type(leaf, dev_item);
6108 device->io_align = btrfs_device_io_align(leaf, dev_item);
6109 device->io_width = btrfs_device_io_width(leaf, dev_item);
6110 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01006111 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01006112 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006113
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006114 ptr = btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04006115 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006116}
6117
Miao Xie5f375832014-09-03 21:35:46 +08006118static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6119 u8 *fsid)
Yan Zheng2b820322008-11-17 21:11:30 -05006120{
6121 struct btrfs_fs_devices *fs_devices;
6122 int ret;
6123
Li Zefanb367e472011-12-07 11:38:24 +08006124 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05006125
6126 fs_devices = root->fs_info->fs_devices->seed;
6127 while (fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006128 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6129 return fs_devices;
6130
Yan Zheng2b820322008-11-17 21:11:30 -05006131 fs_devices = fs_devices->seed;
6132 }
6133
6134 fs_devices = find_fsid(fsid);
6135 if (!fs_devices) {
Miao Xie5f375832014-09-03 21:35:46 +08006136 if (!btrfs_test_opt(root, DEGRADED))
6137 return ERR_PTR(-ENOENT);
6138
6139 fs_devices = alloc_fs_devices(fsid);
6140 if (IS_ERR(fs_devices))
6141 return fs_devices;
6142
6143 fs_devices->seeding = 1;
6144 fs_devices->opened = 1;
6145 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006146 }
Yan Zhenge4404d62008-12-12 10:03:26 -05006147
6148 fs_devices = clone_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006149 if (IS_ERR(fs_devices))
6150 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006151
Christoph Hellwig97288f22008-12-02 06:36:09 -05006152 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05006153 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02006154 if (ret) {
6155 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006156 fs_devices = ERR_PTR(ret);
Yan Zheng2b820322008-11-17 21:11:30 -05006157 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02006158 }
Yan Zheng2b820322008-11-17 21:11:30 -05006159
6160 if (!fs_devices->seeding) {
6161 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05006162 free_fs_devices(fs_devices);
Miao Xie5f375832014-09-03 21:35:46 +08006163 fs_devices = ERR_PTR(-EINVAL);
Yan Zheng2b820322008-11-17 21:11:30 -05006164 goto out;
6165 }
6166
6167 fs_devices->seed = root->fs_info->fs_devices->seed;
6168 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006169out:
Miao Xie5f375832014-09-03 21:35:46 +08006170 return fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05006171}
6172
Chris Mason0d81ba52008-03-24 15:02:07 -04006173static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04006174 struct extent_buffer *leaf,
6175 struct btrfs_dev_item *dev_item)
6176{
Miao Xie5f375832014-09-03 21:35:46 +08006177 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04006178 struct btrfs_device *device;
6179 u64 devid;
6180 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05006181 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04006182 u8 dev_uuid[BTRFS_UUID_SIZE];
6183
Chris Mason0b86a832008-03-24 15:01:56 -04006184 devid = btrfs_device_id(leaf, dev_item);
Geert Uytterhoeven410ba3a2013-08-20 13:20:11 +02006185 read_extent_buffer(leaf, dev_uuid, btrfs_device_uuid(dev_item),
Chris Masona4437552008-04-18 10:29:38 -04006186 BTRFS_UUID_SIZE);
Geert Uytterhoeven1473b242013-08-20 13:20:12 +02006187 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
Yan Zheng2b820322008-11-17 21:11:30 -05006188 BTRFS_UUID_SIZE);
6189
6190 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
Miao Xie5f375832014-09-03 21:35:46 +08006191 fs_devices = open_seed_devices(root, fs_uuid);
6192 if (IS_ERR(fs_devices))
6193 return PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05006194 }
6195
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006196 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Miao Xie5f375832014-09-03 21:35:46 +08006197 if (!device) {
Yan Zhenge4404d62008-12-12 10:03:26 -05006198 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05006199 return -EIO;
6200
Miao Xie5f375832014-09-03 21:35:46 +08006201 btrfs_warn(root->fs_info, "devid %llu missing", devid);
6202 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6203 if (!device)
6204 return -ENOMEM;
6205 } else {
6206 if (!device->bdev && !btrfs_test_opt(root, DEGRADED))
6207 return -EIO;
6208
6209 if(!device->bdev && !device->missing) {
Chris Masoncd02dca2010-12-13 14:56:23 -05006210 /*
6211 * this happens when a device that was properly setup
6212 * in the device info lists suddenly goes bad.
6213 * device->bdev is NULL, and so we have to set
6214 * device->missing to one here
6215 */
Miao Xie5f375832014-09-03 21:35:46 +08006216 device->fs_devices->missing_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05006217 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05006218 }
Miao Xie5f375832014-09-03 21:35:46 +08006219
6220 /* Move the device to its own fs_devices */
6221 if (device->fs_devices != fs_devices) {
6222 ASSERT(device->missing);
6223
6224 list_move(&device->dev_list, &fs_devices->devices);
6225 device->fs_devices->num_devices--;
6226 fs_devices->num_devices++;
6227
6228 device->fs_devices->missing_devices--;
6229 fs_devices->missing_devices++;
6230
6231 device->fs_devices = fs_devices;
6232 }
Yan Zheng2b820322008-11-17 21:11:30 -05006233 }
6234
6235 if (device->fs_devices != root->fs_info->fs_devices) {
6236 BUG_ON(device->writeable);
6237 if (device->generation !=
6238 btrfs_device_generation(leaf, dev_item))
6239 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04006240 }
Chris Mason0b86a832008-03-24 15:01:56 -04006241
6242 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04006243 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01006244 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05006245 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04006246 spin_lock(&root->fs_info->free_chunk_lock);
6247 root->fs_info->free_chunk_space += device->total_bytes -
6248 device->bytes_used;
6249 spin_unlock(&root->fs_info->free_chunk_lock);
6250 }
Chris Mason0b86a832008-03-24 15:01:56 -04006251 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006252 return ret;
6253}
6254
Yan Zhenge4404d62008-12-12 10:03:26 -05006255int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04006256{
David Sterba6c417612011-04-13 15:41:04 +02006257 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04006258 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04006259 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04006260 struct btrfs_chunk *chunk;
David Sterba1ffb22c2014-10-31 19:02:42 +01006261 u8 *array_ptr;
6262 unsigned long sb_array_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006263 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006264 u32 num_stripes;
6265 u32 array_size;
6266 u32 len = 0;
David Sterba1ffb22c2014-10-31 19:02:42 +01006267 u32 cur_offset;
Chris Mason84eed902008-04-25 09:04:37 -04006268 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04006269
David Sterbaa83fffb2014-06-15 02:39:54 +02006270 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
6271 /*
6272 * This will create extent buffer of nodesize, superblock size is
6273 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6274 * overallocate but we can keep it as-is, only the first page is used.
6275 */
6276 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
Chris Masona061fc82008-05-07 11:43:44 -04006277 if (!sb)
6278 return -ENOMEM;
6279 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04006280 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02006281 /*
6282 * The sb extent buffer is artifical and just used to read the system array.
6283 * btrfs_set_buffer_uptodate() call does not properly mark all it's
6284 * pages up-to-date when the page is larger: extent does not cover the
6285 * whole page and consequently check_page_uptodate does not find all
6286 * the page's extents up-to-date (the hole beyond sb),
6287 * write_extent_buffer then triggers a WARN_ON.
6288 *
6289 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
6290 * but sb spans only this function. Add an explicit SetPageUptodate call
6291 * to silence the warning eg. on PowerPC 64.
6292 */
6293 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04006294 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05006295
Chris Masona061fc82008-05-07 11:43:44 -04006296 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04006297 array_size = btrfs_super_sys_array_size(super_copy);
6298
David Sterba1ffb22c2014-10-31 19:02:42 +01006299 array_ptr = super_copy->sys_chunk_array;
6300 sb_array_offset = offsetof(struct btrfs_super_block, sys_chunk_array);
6301 cur_offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006302
David Sterba1ffb22c2014-10-31 19:02:42 +01006303 while (cur_offset < array_size) {
6304 disk_key = (struct btrfs_disk_key *)array_ptr;
David Sterbae3540ea2014-11-05 15:24:51 +01006305 len = sizeof(*disk_key);
6306 if (cur_offset + len > array_size)
6307 goto out_short_read;
6308
Chris Mason0b86a832008-03-24 15:01:56 -04006309 btrfs_disk_key_to_cpu(&key, disk_key);
6310
David Sterba1ffb22c2014-10-31 19:02:42 +01006311 array_ptr += len;
6312 sb_array_offset += len;
6313 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006314
Chris Mason0d81ba52008-03-24 15:02:07 -04006315 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
David Sterba1ffb22c2014-10-31 19:02:42 +01006316 chunk = (struct btrfs_chunk *)sb_array_offset;
David Sterbae3540ea2014-11-05 15:24:51 +01006317 /*
6318 * At least one btrfs_chunk with one stripe must be
6319 * present, exact stripe count check comes afterwards
6320 */
6321 len = btrfs_chunk_item_size(1);
6322 if (cur_offset + len > array_size)
6323 goto out_short_read;
6324
6325 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
6326 len = btrfs_chunk_item_size(num_stripes);
6327 if (cur_offset + len > array_size)
6328 goto out_short_read;
6329
Chris Mason0d81ba52008-03-24 15:02:07 -04006330 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04006331 if (ret)
6332 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006333 } else {
Chris Mason84eed902008-04-25 09:04:37 -04006334 ret = -EIO;
6335 break;
Chris Mason0b86a832008-03-24 15:01:56 -04006336 }
David Sterba1ffb22c2014-10-31 19:02:42 +01006337 array_ptr += len;
6338 sb_array_offset += len;
6339 cur_offset += len;
Chris Mason0b86a832008-03-24 15:01:56 -04006340 }
Chris Masona061fc82008-05-07 11:43:44 -04006341 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04006342 return ret;
David Sterbae3540ea2014-11-05 15:24:51 +01006343
6344out_short_read:
6345 printk(KERN_ERR "BTRFS: sys_array too short to read %u bytes at offset %u\n",
6346 len, cur_offset);
6347 free_extent_buffer(sb);
6348 return -EIO;
Chris Mason0b86a832008-03-24 15:01:56 -04006349}
6350
6351int btrfs_read_chunk_tree(struct btrfs_root *root)
6352{
6353 struct btrfs_path *path;
6354 struct extent_buffer *leaf;
6355 struct btrfs_key key;
6356 struct btrfs_key found_key;
6357 int ret;
6358 int slot;
6359
6360 root = root->fs_info->chunk_root;
6361
6362 path = btrfs_alloc_path();
6363 if (!path)
6364 return -ENOMEM;
6365
Li Zefanb367e472011-12-07 11:38:24 +08006366 mutex_lock(&uuid_mutex);
6367 lock_chunks(root);
6368
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006369 /*
6370 * Read all device items, and then all the chunk items. All
6371 * device items are found before any chunk item (their object id
6372 * is smaller than the lowest possible object id for a chunk
6373 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04006374 */
6375 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
6376 key.offset = 0;
6377 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006378 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00006379 if (ret < 0)
6380 goto error;
Chris Masond3977122009-01-05 21:25:51 -05006381 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006382 leaf = path->nodes[0];
6383 slot = path->slots[0];
6384 if (slot >= btrfs_header_nritems(leaf)) {
6385 ret = btrfs_next_leaf(root, path);
6386 if (ret == 0)
6387 continue;
6388 if (ret < 0)
6389 goto error;
6390 break;
6391 }
6392 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006393 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
6394 struct btrfs_dev_item *dev_item;
6395 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04006396 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01006397 ret = read_one_dev(root, leaf, dev_item);
6398 if (ret)
6399 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006400 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6401 struct btrfs_chunk *chunk;
6402 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6403 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05006404 if (ret)
6405 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04006406 }
6407 path->slots[0]++;
6408 }
Chris Mason0b86a832008-03-24 15:01:56 -04006409 ret = 0;
6410error:
Li Zefanb367e472011-12-07 11:38:24 +08006411 unlock_chunks(root);
6412 mutex_unlock(&uuid_mutex);
6413
Yan Zheng2b820322008-11-17 21:11:30 -05006414 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006415 return ret;
6416}
Stefan Behrens442a4f62012-05-25 16:06:08 +02006417
Miao Xiecb517ea2013-05-15 07:48:19 +00006418void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
6419{
6420 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6421 struct btrfs_device *device;
6422
Liu Bo29cc83f2014-05-11 23:14:59 +08006423 while (fs_devices) {
6424 mutex_lock(&fs_devices->device_list_mutex);
6425 list_for_each_entry(device, &fs_devices->devices, dev_list)
6426 device->dev_root = fs_info->dev_root;
6427 mutex_unlock(&fs_devices->device_list_mutex);
6428
6429 fs_devices = fs_devices->seed;
6430 }
Miao Xiecb517ea2013-05-15 07:48:19 +00006431}
6432
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006433static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6434{
6435 int i;
6436
6437 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6438 btrfs_dev_stat_reset(dev, i);
6439}
6440
6441int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6442{
6443 struct btrfs_key key;
6444 struct btrfs_key found_key;
6445 struct btrfs_root *dev_root = fs_info->dev_root;
6446 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6447 struct extent_buffer *eb;
6448 int slot;
6449 int ret = 0;
6450 struct btrfs_device *device;
6451 struct btrfs_path *path = NULL;
6452 int i;
6453
6454 path = btrfs_alloc_path();
6455 if (!path) {
6456 ret = -ENOMEM;
6457 goto out;
6458 }
6459
6460 mutex_lock(&fs_devices->device_list_mutex);
6461 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6462 int item_size;
6463 struct btrfs_dev_stats_item *ptr;
6464
6465 key.objectid = 0;
6466 key.type = BTRFS_DEV_STATS_KEY;
6467 key.offset = device->devid;
6468 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6469 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006470 __btrfs_reset_dev_stats(device);
6471 device->dev_stats_valid = 1;
6472 btrfs_release_path(path);
6473 continue;
6474 }
6475 slot = path->slots[0];
6476 eb = path->nodes[0];
6477 btrfs_item_key_to_cpu(eb, &found_key, slot);
6478 item_size = btrfs_item_size_nr(eb, slot);
6479
6480 ptr = btrfs_item_ptr(eb, slot,
6481 struct btrfs_dev_stats_item);
6482
6483 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6484 if (item_size >= (1 + i) * sizeof(__le64))
6485 btrfs_dev_stat_set(device, i,
6486 btrfs_dev_stats_value(eb, ptr, i));
6487 else
6488 btrfs_dev_stat_reset(device, i);
6489 }
6490
6491 device->dev_stats_valid = 1;
6492 btrfs_dev_stat_print_on_load(device);
6493 btrfs_release_path(path);
6494 }
6495 mutex_unlock(&fs_devices->device_list_mutex);
6496
6497out:
6498 btrfs_free_path(path);
6499 return ret < 0 ? ret : 0;
6500}
6501
6502static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6503 struct btrfs_root *dev_root,
6504 struct btrfs_device *device)
6505{
6506 struct btrfs_path *path;
6507 struct btrfs_key key;
6508 struct extent_buffer *eb;
6509 struct btrfs_dev_stats_item *ptr;
6510 int ret;
6511 int i;
6512
6513 key.objectid = 0;
6514 key.type = BTRFS_DEV_STATS_KEY;
6515 key.offset = device->devid;
6516
6517 path = btrfs_alloc_path();
6518 BUG_ON(!path);
6519 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6520 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006521 printk_in_rcu(KERN_WARNING "BTRFS: "
6522 "error %d while searching for dev_stats item for device %s!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006523 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006524 goto out;
6525 }
6526
6527 if (ret == 0 &&
6528 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6529 /* need to delete old one and insert a new one */
6530 ret = btrfs_del_item(trans, dev_root, path);
6531 if (ret != 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006532 printk_in_rcu(KERN_WARNING "BTRFS: "
6533 "delete too small dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006534 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006535 goto out;
6536 }
6537 ret = 1;
6538 }
6539
6540 if (ret == 1) {
6541 /* need to insert a new item */
6542 btrfs_release_path(path);
6543 ret = btrfs_insert_empty_item(trans, dev_root, path,
6544 &key, sizeof(*ptr));
6545 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006546 printk_in_rcu(KERN_WARNING "BTRFS: "
6547 "insert dev_stats item for device %s failed %d!\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006548 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006549 goto out;
6550 }
6551 }
6552
6553 eb = path->nodes[0];
6554 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6555 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6556 btrfs_set_dev_stats_value(eb, ptr, i,
6557 btrfs_dev_stat_read(device, i));
6558 btrfs_mark_buffer_dirty(eb);
6559
6560out:
6561 btrfs_free_path(path);
6562 return ret;
6563}
6564
6565/*
6566 * called from commit_transaction. Writes all changed device stats to disk.
6567 */
6568int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6569 struct btrfs_fs_info *fs_info)
6570{
6571 struct btrfs_root *dev_root = fs_info->dev_root;
6572 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6573 struct btrfs_device *device;
Miao Xieaddc3fa2014-07-24 11:37:11 +08006574 int stats_cnt;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006575 int ret = 0;
6576
6577 mutex_lock(&fs_devices->device_list_mutex);
6578 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Miao Xieaddc3fa2014-07-24 11:37:11 +08006579 if (!device->dev_stats_valid || !btrfs_dev_stats_dirty(device))
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006580 continue;
6581
Miao Xieaddc3fa2014-07-24 11:37:11 +08006582 stats_cnt = atomic_read(&device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006583 ret = update_dev_stat_item(trans, dev_root, device);
6584 if (!ret)
Miao Xieaddc3fa2014-07-24 11:37:11 +08006585 atomic_sub(stats_cnt, &device->dev_stats_ccnt);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006586 }
6587 mutex_unlock(&fs_devices->device_list_mutex);
6588
6589 return ret;
6590}
6591
Stefan Behrens442a4f62012-05-25 16:06:08 +02006592void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6593{
6594 btrfs_dev_stat_inc(dev, index);
6595 btrfs_dev_stat_print_on_error(dev);
6596}
6597
Eric Sandeen48a3b632013-04-25 20:41:01 +00006598static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006599{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006600 if (!dev->dev_stats_valid)
6601 return;
Frank Holtonefe120a2013-12-20 11:37:06 -05006602 printk_ratelimited_in_rcu(KERN_ERR "BTRFS: "
6603 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006604 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006605 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6606 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6607 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
Frank Holtonefe120a2013-12-20 11:37:06 -05006608 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6609 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
Stefan Behrens442a4f62012-05-25 16:06:08 +02006610}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006611
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006612static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6613{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006614 int i;
6615
6616 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6617 if (btrfs_dev_stat_read(dev, i) != 0)
6618 break;
6619 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6620 return; /* all values == 0, suppress message */
6621
Frank Holtonefe120a2013-12-20 11:37:06 -05006622 printk_in_rcu(KERN_INFO "BTRFS: "
6623 "bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006624 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006625 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6626 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6627 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6628 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6629 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6630}
6631
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006632int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006633 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006634{
6635 struct btrfs_device *dev;
6636 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6637 int i;
6638
6639 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006640 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006641 mutex_unlock(&fs_devices->device_list_mutex);
6642
6643 if (!dev) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006644 btrfs_warn(root->fs_info, "get dev_stats failed, device not found");
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006645 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006646 } else if (!dev->dev_stats_valid) {
Frank Holtonefe120a2013-12-20 11:37:06 -05006647 btrfs_warn(root->fs_info, "get dev_stats failed, not yet valid");
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006648 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006649 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006650 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6651 if (stats->nr_items > i)
6652 stats->values[i] =
6653 btrfs_dev_stat_read_and_reset(dev, i);
6654 else
6655 btrfs_dev_stat_reset(dev, i);
6656 }
6657 } else {
6658 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6659 if (stats->nr_items > i)
6660 stats->values[i] = btrfs_dev_stat_read(dev, i);
6661 }
6662 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6663 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6664 return 0;
6665}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006666
6667int btrfs_scratch_superblock(struct btrfs_device *device)
6668{
6669 struct buffer_head *bh;
6670 struct btrfs_super_block *disk_super;
6671
6672 bh = btrfs_read_dev_super(device->bdev);
6673 if (!bh)
6674 return -EINVAL;
6675 disk_super = (struct btrfs_super_block *)bh->b_data;
6676
6677 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6678 set_buffer_dirty(bh);
6679 sync_dirty_buffer(bh);
6680 brelse(bh);
6681
6682 return 0;
6683}
Miao Xie935e5cc2014-09-03 21:35:33 +08006684
6685/*
6686 * Update the size of all devices, which is used for writing out the
6687 * super blocks.
6688 */
6689void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
6690{
6691 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6692 struct btrfs_device *curr, *next;
6693
6694 if (list_empty(&fs_devices->resized_devices))
6695 return;
6696
6697 mutex_lock(&fs_devices->device_list_mutex);
6698 lock_chunks(fs_info->dev_root);
6699 list_for_each_entry_safe(curr, next, &fs_devices->resized_devices,
6700 resized_list) {
6701 list_del_init(&curr->resized_list);
6702 curr->commit_total_bytes = curr->disk_total_bytes;
6703 }
6704 unlock_chunks(fs_info->dev_root);
6705 mutex_unlock(&fs_devices->device_list_mutex);
6706}
Miao Xiece7213c2014-09-03 21:35:34 +08006707
6708/* Must be invoked during the transaction commit */
6709void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6710 struct btrfs_transaction *transaction)
6711{
6712 struct extent_map *em;
6713 struct map_lookup *map;
6714 struct btrfs_device *dev;
6715 int i;
6716
6717 if (list_empty(&transaction->pending_chunks))
6718 return;
6719
6720 /* In order to kick the device replace finish process */
6721 lock_chunks(root);
6722 list_for_each_entry(em, &transaction->pending_chunks, list) {
6723 map = (struct map_lookup *)em->bdev;
6724
6725 for (i = 0; i < map->num_stripes; i++) {
6726 dev = map->stripes[i].dev;
6727 dev->commit_bytes_used = dev->bytes_used;
6728 }
6729 }
6730 unlock_chunks(root);
6731}