blob: 4bc07d910e54abdc271a1d8dfc2065ad57841a72 [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 Mason4b4e25f2008-11-20 10:22:27 -050031#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "ctree.h"
33#include "extent_map.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "print-tree.h"
37#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050038#include "raid56.h"
Chris Mason8b712842008-06-11 16:50:36 -040039#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010040#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040041#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060042#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010043#include "dev-replace.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
Chris Mason8a4b83c2008-03-24 15:02:07 -040053static DEFINE_MUTEX(uuid_mutex);
54static LIST_HEAD(fs_uuids);
55
Chris Mason7d9eb122008-07-08 14:19:17 -040056static void lock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_lock(&root->fs_info->chunk_mutex);
59}
60
61static void unlock_chunks(struct btrfs_root *root)
62{
Chris Mason7d9eb122008-07-08 14:19:17 -040063 mutex_unlock(&root->fs_info->chunk_mutex);
64}
65
Yan Zhenge4404d62008-12-12 10:03:26 -050066static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
67{
68 struct btrfs_device *device;
69 WARN_ON(fs_devices->opened);
70 while (!list_empty(&fs_devices->devices)) {
71 device = list_entry(fs_devices->devices.next,
72 struct btrfs_device, dev_list);
73 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040074 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050075 kfree(device);
76 }
77 kfree(fs_devices);
78}
79
Lukas Czernerb8b8ff52012-12-06 19:25:48 +000080static void btrfs_kobject_uevent(struct block_device *bdev,
81 enum kobject_action action)
82{
83 int ret;
84
85 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
86 if (ret)
87 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
88 action,
89 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
90 &disk_to_dev(bdev->bd_disk)->kobj);
91}
92
Jeff Mahoney143bede2012-03-01 14:56:26 +010093void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040094{
95 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040096
Yan Zheng2b820322008-11-17 21:11:30 -050097 while (!list_empty(&fs_uuids)) {
98 fs_devices = list_entry(fs_uuids.next,
99 struct btrfs_fs_devices, list);
100 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -0500101 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400103}
104
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300105static struct btrfs_device *__alloc_device(void)
106{
107 struct btrfs_device *dev;
108
109 dev = kzalloc(sizeof(*dev), GFP_NOFS);
110 if (!dev)
111 return ERR_PTR(-ENOMEM);
112
113 INIT_LIST_HEAD(&dev->dev_list);
114 INIT_LIST_HEAD(&dev->dev_alloc_list);
115
116 spin_lock_init(&dev->io_lock);
117
118 spin_lock_init(&dev->reada_lock);
119 atomic_set(&dev->reada_in_flight, 0);
120 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_WAIT);
121 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_WAIT);
122
123 return dev;
124}
125
Chris Masona1b32a52008-09-05 16:09:51 -0400126static noinline struct btrfs_device *__find_device(struct list_head *head,
127 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400128{
129 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400130
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500131 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400132 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400133 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400134 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400135 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400136 }
137 return NULL;
138}
139
Chris Masona1b32a52008-09-05 16:09:51 -0400140static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400141{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400142 struct btrfs_fs_devices *fs_devices;
143
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500144 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400145 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
146 return fs_devices;
147 }
148 return NULL;
149}
150
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100151static int
152btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
153 int flush, struct block_device **bdev,
154 struct buffer_head **bh)
155{
156 int ret;
157
158 *bdev = blkdev_get_by_path(device_path, flags, holder);
159
160 if (IS_ERR(*bdev)) {
161 ret = PTR_ERR(*bdev);
162 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
163 goto error;
164 }
165
166 if (flush)
167 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
168 ret = set_blocksize(*bdev, 4096);
169 if (ret) {
170 blkdev_put(*bdev, flags);
171 goto error;
172 }
173 invalidate_bdev(*bdev);
174 *bh = btrfs_read_dev_super(*bdev);
175 if (!*bh) {
176 ret = -EINVAL;
177 blkdev_put(*bdev, flags);
178 goto error;
179 }
180
181 return 0;
182
183error:
184 *bdev = NULL;
185 *bh = NULL;
186 return ret;
187}
188
Chris Masonffbd5172009-04-20 15:50:09 -0400189static void requeue_list(struct btrfs_pending_bios *pending_bios,
190 struct bio *head, struct bio *tail)
191{
192
193 struct bio *old_head;
194
195 old_head = pending_bios->head;
196 pending_bios->head = head;
197 if (pending_bios->tail)
198 tail->bi_next = old_head;
199 else
200 pending_bios->tail = tail;
201}
202
Chris Mason8b712842008-06-11 16:50:36 -0400203/*
204 * we try to collect pending bios for a device so we don't get a large
205 * number of procs sending bios down to the same device. This greatly
206 * improves the schedulers ability to collect and merge the bios.
207 *
208 * But, it also turns into a long list of bios to process and that is sure
209 * to eventually make the worker thread block. The solution here is to
210 * make some progress and then put this work struct back at the end of
211 * the list if the block device is congested. This way, multiple devices
212 * can make progress from a single worker thread.
213 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100214static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400215{
216 struct bio *pending;
217 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400218 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400219 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400220 struct bio *tail;
221 struct bio *cur;
222 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400223 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400224 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400225 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400226 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400227 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000228 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400229 struct blk_plug plug;
230
231 /*
232 * this function runs all the bios we've collected for
233 * a particular device. We don't want to wander off to
234 * another device without first sending all of these down.
235 * So, setup a plug here and finish it off before we return
236 */
237 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400238
Chris Masonbedf7622009-04-03 10:32:58 -0400239 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400240 fs_info = device->dev_root->fs_info;
241 limit = btrfs_async_submit_limit(fs_info);
242 limit = limit * 2 / 3;
243
Chris Mason8b712842008-06-11 16:50:36 -0400244loop:
245 spin_lock(&device->io_lock);
246
Chris Masona6837052009-02-04 09:19:41 -0500247loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400248 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400249
Chris Mason8b712842008-06-11 16:50:36 -0400250 /* take all the bios off the list at once and process them
251 * later on (without the lock held). But, remember the
252 * tail and other pointers so the bios can be properly reinserted
253 * into the list if we hit congestion
254 */
Chris Masond84275c2009-06-09 15:39:08 -0400255 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400256 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400257 force_reg = 1;
258 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400259 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400260 force_reg = 0;
261 }
Chris Masonffbd5172009-04-20 15:50:09 -0400262
263 pending = pending_bios->head;
264 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400265 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400266
267 /*
268 * if pending was null this time around, no bios need processing
269 * at all and we can stop. Otherwise it'll loop back up again
270 * and do an additional check so no bios are missed.
271 *
272 * device->running_pending is used to synchronize with the
273 * schedule_bio code.
274 */
Chris Masonffbd5172009-04-20 15:50:09 -0400275 if (device->pending_sync_bios.head == NULL &&
276 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400277 again = 0;
278 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400279 } else {
280 again = 1;
281 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400282 }
Chris Masonffbd5172009-04-20 15:50:09 -0400283
284 pending_bios->head = NULL;
285 pending_bios->tail = NULL;
286
Chris Mason8b712842008-06-11 16:50:36 -0400287 spin_unlock(&device->io_lock);
288
Chris Masond3977122009-01-05 21:25:51 -0500289 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400290
291 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400292 /* we want to work on both lists, but do more bios on the
293 * sync list than the regular list
294 */
295 if ((num_run > 32 &&
296 pending_bios != &device->pending_sync_bios &&
297 device->pending_sync_bios.head) ||
298 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
299 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400300 spin_lock(&device->io_lock);
301 requeue_list(pending_bios, pending, tail);
302 goto loop_lock;
303 }
304
Chris Mason8b712842008-06-11 16:50:36 -0400305 cur = pending;
306 pending = pending->bi_next;
307 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400308
Josef Bacik66657b32012-08-01 15:36:24 -0400309 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400310 waitqueue_active(&fs_info->async_submit_wait))
311 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400312
313 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400314
Chris Mason2ab1ba62011-08-04 14:28:36 -0400315 /*
316 * if we're doing the sync list, record that our
317 * plug has some sync requests on it
318 *
319 * If we're doing the regular list and there are
320 * sync requests sitting around, unplug before
321 * we add more
322 */
323 if (pending_bios == &device->pending_sync_bios) {
324 sync_pending = 1;
325 } else if (sync_pending) {
326 blk_finish_plug(&plug);
327 blk_start_plug(&plug);
328 sync_pending = 0;
329 }
330
Stefan Behrens21adbd52011-11-09 13:44:05 +0100331 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400332 num_run++;
333 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100334 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400335 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400336
337 /*
338 * we made progress, there is more work to do and the bdi
339 * is now congested. Back off and let other work structs
340 * run instead
341 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400342 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500343 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400344 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400345
Chris Masonb765ead2009-04-03 10:27:10 -0400346 ioc = current->io_context;
347
348 /*
349 * the main goal here is that we don't want to
350 * block if we're going to be able to submit
351 * more requests without blocking.
352 *
353 * This code does two great things, it pokes into
354 * the elevator code from a filesystem _and_
355 * it makes assumptions about how batching works.
356 */
357 if (ioc && ioc->nr_batch_requests > 0 &&
358 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
359 (last_waited == 0 ||
360 ioc->last_waited == last_waited)) {
361 /*
362 * we want to go through our batch of
363 * requests and stop. So, we copy out
364 * the ioc->last_waited time and test
365 * against it before looping
366 */
367 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100368 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400369 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400370 continue;
371 }
Chris Mason8b712842008-06-11 16:50:36 -0400372 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400373 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500374 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400375
376 spin_unlock(&device->io_lock);
377 btrfs_requeue_work(&device->work);
378 goto done;
379 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500380 /* unplug every 64 requests just for good measure */
381 if (batch_run % 64 == 0) {
382 blk_finish_plug(&plug);
383 blk_start_plug(&plug);
384 sync_pending = 0;
385 }
Chris Mason8b712842008-06-11 16:50:36 -0400386 }
Chris Masonffbd5172009-04-20 15:50:09 -0400387
Chris Mason51684082010-03-10 15:33:32 -0500388 cond_resched();
389 if (again)
390 goto loop;
391
392 spin_lock(&device->io_lock);
393 if (device->pending_bios.head || device->pending_sync_bios.head)
394 goto loop_lock;
395 spin_unlock(&device->io_lock);
396
Chris Mason8b712842008-06-11 16:50:36 -0400397done:
Chris Mason211588a2011-04-19 20:12:40 -0400398 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400399}
400
Christoph Hellwigb2950862008-12-02 09:54:17 -0500401static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400402{
403 struct btrfs_device *device;
404
405 device = container_of(work, struct btrfs_device, work);
406 run_scheduled_bios(device);
407}
408
Chris Masona1b32a52008-09-05 16:09:51 -0400409static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400410 struct btrfs_super_block *disk_super,
411 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
412{
413 struct btrfs_device *device;
414 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400415 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400416 u64 found_transid = btrfs_super_generation(disk_super);
417
418 fs_devices = find_fsid(disk_super->fsid);
419 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400420 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400421 if (!fs_devices)
422 return -ENOMEM;
423 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400424 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400425 list_add(&fs_devices->list, &fs_uuids);
426 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
427 fs_devices->latest_devid = devid;
428 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400429 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400430 device = NULL;
431 } else {
Chris Masona4437552008-04-18 10:29:38 -0400432 device = __find_device(&fs_devices->devices, devid,
433 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400434 }
435 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500436 if (fs_devices->opened)
437 return -EBUSY;
438
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300439 device = btrfs_alloc_device(NULL, &devid,
440 disk_super->dev_item.uuid);
441 if (IS_ERR(device)) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400442 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300443 return PTR_ERR(device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400444 }
Josef Bacik606686e2012-06-04 14:03:51 -0400445
446 name = rcu_string_strdup(path, GFP_NOFS);
447 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400448 kfree(device);
449 return -ENOMEM;
450 }
Josef Bacik606686e2012-06-04 14:03:51 -0400451 rcu_assign_pointer(device->name, name);
Arne Jansen90519d62011-05-23 14:30:00 +0200452
Chris Masone5e9a522009-06-10 15:17:02 -0400453 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000454 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400455 mutex_unlock(&fs_devices->device_list_mutex);
456
Yan Zheng2b820322008-11-17 21:11:30 -0500457 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400458 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400459 } else if (!device->name || strcmp(device->name->str, path)) {
460 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000461 if (!name)
462 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400463 rcu_string_free(device->name);
464 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500465 if (device->missing) {
466 fs_devices->missing_devices--;
467 device->missing = 0;
468 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400469 }
470
471 if (found_transid > fs_devices->latest_trans) {
472 fs_devices->latest_devid = devid;
473 fs_devices->latest_trans = found_transid;
474 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400475 *fs_devices_ret = fs_devices;
476 return 0;
477}
478
Yan Zhenge4404d62008-12-12 10:03:26 -0500479static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
480{
481 struct btrfs_fs_devices *fs_devices;
482 struct btrfs_device *device;
483 struct btrfs_device *orig_dev;
484
485 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
486 if (!fs_devices)
487 return ERR_PTR(-ENOMEM);
488
489 INIT_LIST_HEAD(&fs_devices->devices);
490 INIT_LIST_HEAD(&fs_devices->alloc_list);
491 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400492 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500493 fs_devices->latest_devid = orig->latest_devid;
494 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400495 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500496 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
497
Xiao Guangrong46224702011-04-20 10:08:47 +0000498 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500499 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400500 struct rcu_string *name;
501
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +0300502 device = btrfs_alloc_device(NULL, &orig_dev->devid,
503 orig_dev->uuid);
504 if (IS_ERR(device))
Yan Zhenge4404d62008-12-12 10:03:26 -0500505 goto error;
506
Josef Bacik606686e2012-06-04 14:03:51 -0400507 /*
508 * This is ok to do without rcu read locked because we hold the
509 * uuid mutex so nothing we touch in here is going to disappear.
510 */
511 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
512 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400513 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500514 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400515 }
Josef Bacik606686e2012-06-04 14:03:51 -0400516 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500517
Yan Zhenge4404d62008-12-12 10:03:26 -0500518 list_add(&device->dev_list, &fs_devices->devices);
519 device->fs_devices = fs_devices;
520 fs_devices->num_devices++;
521 }
522 return fs_devices;
523error:
524 free_fs_devices(fs_devices);
525 return ERR_PTR(-ENOMEM);
526}
527
Stefan Behrens8dabb742012-11-06 13:15:27 +0100528void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
529 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400530{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500531 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400532
Chris Masona6b0d5c2012-02-20 20:53:43 -0500533 struct block_device *latest_bdev = NULL;
534 u64 latest_devid = 0;
535 u64 latest_transid = 0;
536
Chris Masondfe25022008-05-13 13:46:40 -0400537 mutex_lock(&uuid_mutex);
538again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000539 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500540 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500541 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100542 if (!device->is_tgtdev_for_dev_replace &&
543 (!latest_transid ||
544 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500545 latest_devid = device->devid;
546 latest_transid = device->generation;
547 latest_bdev = device->bdev;
548 }
Yan Zheng2b820322008-11-17 21:11:30 -0500549 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500550 }
Yan Zheng2b820322008-11-17 21:11:30 -0500551
Stefan Behrens8dabb742012-11-06 13:15:27 +0100552 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
553 /*
554 * In the first step, keep the device which has
555 * the correct fsid and the devid that is used
556 * for the dev_replace procedure.
557 * In the second step, the dev_replace state is
558 * read from the device tree and it is known
559 * whether the procedure is really active or
560 * not, which means whether this device is
561 * used or whether it should be removed.
562 */
563 if (step == 0 || device->is_tgtdev_for_dev_replace) {
564 continue;
565 }
566 }
Yan Zheng2b820322008-11-17 21:11:30 -0500567 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100568 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500569 device->bdev = NULL;
570 fs_devices->open_devices--;
571 }
572 if (device->writeable) {
573 list_del_init(&device->dev_alloc_list);
574 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100575 if (!device->is_tgtdev_for_dev_replace)
576 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500577 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500578 list_del_init(&device->dev_list);
579 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400580 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500581 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400582 }
Yan Zheng2b820322008-11-17 21:11:30 -0500583
584 if (fs_devices->seed) {
585 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500586 goto again;
587 }
588
Chris Masona6b0d5c2012-02-20 20:53:43 -0500589 fs_devices->latest_bdev = latest_bdev;
590 fs_devices->latest_devid = latest_devid;
591 fs_devices->latest_trans = latest_transid;
592
Chris Masondfe25022008-05-13 13:46:40 -0400593 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400594}
Chris Masona0af4692008-05-13 16:03:06 -0400595
Xiao Guangrong1f781602011-04-20 10:09:16 +0000596static void __free_device(struct work_struct *work)
597{
598 struct btrfs_device *device;
599
600 device = container_of(work, struct btrfs_device, rcu_work);
601
602 if (device->bdev)
603 blkdev_put(device->bdev, device->mode);
604
Josef Bacik606686e2012-06-04 14:03:51 -0400605 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000606 kfree(device);
607}
608
609static void free_device(struct rcu_head *head)
610{
611 struct btrfs_device *device;
612
613 device = container_of(head, struct btrfs_device, rcu);
614
615 INIT_WORK(&device->rcu_work, __free_device);
616 schedule_work(&device->rcu_work);
617}
618
Yan Zheng2b820322008-11-17 21:11:30 -0500619static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400620{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400621 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500622
Yan Zheng2b820322008-11-17 21:11:30 -0500623 if (--fs_devices->opened > 0)
624 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400625
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000626 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500627 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000628 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400629 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000630
631 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400632 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000633
Stefan Behrens8dabb742012-11-06 13:15:27 +0100634 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500635 list_del_init(&device->dev_alloc_list);
636 fs_devices->rw_devices--;
637 }
638
Josef Bacikd5e20032011-08-04 14:52:27 +0000639 if (device->can_discard)
640 fs_devices->num_can_discard--;
641
Xiao Guangrong1f781602011-04-20 10:09:16 +0000642 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100643 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000644 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400645
646 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400647 if (device->name) {
648 name = rcu_string_strdup(device->name->str, GFP_NOFS);
649 BUG_ON(device->name && !name); /* -ENOMEM */
650 rcu_assign_pointer(new_device->name, name);
651 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000652 new_device->bdev = NULL;
653 new_device->writeable = 0;
654 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000655 new_device->can_discard = 0;
Thomas Gleixner1cba0cd2013-02-20 14:06:20 -0500656 spin_lock_init(&new_device->io_lock);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000657 list_replace_rcu(&device->dev_list, &new_device->dev_list);
658
659 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400660 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000661 mutex_unlock(&fs_devices->device_list_mutex);
662
Yan Zhenge4404d62008-12-12 10:03:26 -0500663 WARN_ON(fs_devices->open_devices);
664 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500665 fs_devices->opened = 0;
666 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500667
Chris Mason8a4b83c2008-03-24 15:02:07 -0400668 return 0;
669}
670
Yan Zheng2b820322008-11-17 21:11:30 -0500671int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
672{
Yan Zhenge4404d62008-12-12 10:03:26 -0500673 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500674 int ret;
675
676 mutex_lock(&uuid_mutex);
677 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500678 if (!fs_devices->opened) {
679 seed_devices = fs_devices->seed;
680 fs_devices->seed = NULL;
681 }
Yan Zheng2b820322008-11-17 21:11:30 -0500682 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500683
684 while (seed_devices) {
685 fs_devices = seed_devices;
686 seed_devices = fs_devices->seed;
687 __btrfs_close_devices(fs_devices);
688 free_fs_devices(fs_devices);
689 }
Eric Sandeenbc178622013-03-09 15:18:39 +0000690 /*
691 * Wait for rcu kworkers under __btrfs_close_devices
692 * to finish all blkdev_puts so device is really
693 * free when umount is done.
694 */
695 rcu_barrier();
Yan Zheng2b820322008-11-17 21:11:30 -0500696 return ret;
697}
698
Yan Zhenge4404d62008-12-12 10:03:26 -0500699static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
700 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701{
Josef Bacikd5e20032011-08-04 14:52:27 +0000702 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400703 struct block_device *bdev;
704 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400705 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400706 struct block_device *latest_bdev = NULL;
707 struct buffer_head *bh;
708 struct btrfs_super_block *disk_super;
709 u64 latest_devid = 0;
710 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400711 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500712 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400713 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400714
Tejun Heod4d77622010-11-13 11:55:18 +0100715 flags |= FMODE_EXCL;
716
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500717 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400718 if (device->bdev)
719 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400720 if (!device->name)
721 continue;
722
Eric Sandeenf63e0cc2013-04-04 20:45:08 +0000723 /* Just open everything we can; ignore failures here */
724 if (btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
725 &bdev, &bh))
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100726 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400727
728 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000729 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400730 if (devid != device->devid)
731 goto error_brelse;
732
Yan Zheng2b820322008-11-17 21:11:30 -0500733 if (memcmp(device->uuid, disk_super->dev_item.uuid,
734 BTRFS_UUID_SIZE))
735 goto error_brelse;
736
737 device->generation = btrfs_super_generation(disk_super);
738 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400739 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500740 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400741 latest_bdev = bdev;
742 }
743
Yan Zheng2b820322008-11-17 21:11:30 -0500744 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
745 device->writeable = 0;
746 } else {
747 device->writeable = !bdev_read_only(bdev);
748 seeding = 0;
749 }
750
Josef Bacikd5e20032011-08-04 14:52:27 +0000751 q = bdev_get_queue(bdev);
752 if (blk_queue_discard(q)) {
753 device->can_discard = 1;
754 fs_devices->num_can_discard++;
755 }
756
Chris Mason8a4b83c2008-03-24 15:02:07 -0400757 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400758 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500759 device->mode = flags;
760
Chris Masonc2898112009-06-10 09:51:32 -0400761 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
762 fs_devices->rotating = 1;
763
Chris Masona0af4692008-05-13 16:03:06 -0400764 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100765 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500766 fs_devices->rw_devices++;
767 list_add(&device->dev_alloc_list,
768 &fs_devices->alloc_list);
769 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000770 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400771 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400772
Chris Masona0af4692008-05-13 16:03:06 -0400773error_brelse:
774 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100775 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400776 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400777 }
Chris Masona0af4692008-05-13 16:03:06 -0400778 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300779 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400780 goto out;
781 }
Yan Zheng2b820322008-11-17 21:11:30 -0500782 fs_devices->seeding = seeding;
783 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400784 fs_devices->latest_bdev = latest_bdev;
785 fs_devices->latest_devid = latest_devid;
786 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500787 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400788out:
Yan Zheng2b820322008-11-17 21:11:30 -0500789 return ret;
790}
791
792int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500793 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500794{
795 int ret;
796
797 mutex_lock(&uuid_mutex);
798 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500799 fs_devices->opened++;
800 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500801 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500802 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500803 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400804 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400805 return ret;
806}
807
David Sterba6f60cbd2013-02-15 11:31:02 -0700808/*
809 * Look for a btrfs signature on a device. This may be called out of the mount path
810 * and we are not allowed to call set_blocksize during the scan. The superblock
811 * is read via pagecache
812 */
Christoph Hellwig97288f22008-12-02 06:36:09 -0500813int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400814 struct btrfs_fs_devices **fs_devices_ret)
815{
816 struct btrfs_super_block *disk_super;
817 struct block_device *bdev;
David Sterba6f60cbd2013-02-15 11:31:02 -0700818 struct page *page;
819 void *p;
820 int ret = -EINVAL;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400821 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400822 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400823 u64 total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700824 u64 bytenr;
825 pgoff_t index;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400826
David Sterba6f60cbd2013-02-15 11:31:02 -0700827 /*
828 * we would like to check all the supers, but that would make
829 * a btrfs mount succeed after a mkfs from a different FS.
830 * So, we need to add a special mount option to scan for
831 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
832 */
833 bytenr = btrfs_sb_offset(0);
Tejun Heod4d77622010-11-13 11:55:18 +0100834 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500835 mutex_lock(&uuid_mutex);
David Sterba6f60cbd2013-02-15 11:31:02 -0700836
837 bdev = blkdev_get_by_path(path, flags, holder);
838
839 if (IS_ERR(bdev)) {
840 ret = PTR_ERR(bdev);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100841 goto error;
David Sterba6f60cbd2013-02-15 11:31:02 -0700842 }
843
844 /* make sure our super fits in the device */
845 if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
846 goto error_bdev_put;
847
848 /* make sure our super fits in the page */
849 if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
850 goto error_bdev_put;
851
852 /* make sure our super doesn't straddle pages on disk */
853 index = bytenr >> PAGE_CACHE_SHIFT;
854 if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
855 goto error_bdev_put;
856
857 /* pull in the page with our super */
858 page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
859 index, GFP_NOFS);
860
861 if (IS_ERR_OR_NULL(page))
862 goto error_bdev_put;
863
864 p = kmap(page);
865
866 /* align our pointer to the offset of the super block */
867 disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
868
869 if (btrfs_super_bytenr(disk_super) != bytenr ||
Qu Wenruo3cae2102013-07-16 11:19:18 +0800870 btrfs_super_magic(disk_super) != BTRFS_MAGIC)
David Sterba6f60cbd2013-02-15 11:31:02 -0700871 goto error_unmap;
872
Xiao Guangronga3438322010-01-06 11:48:18 +0000873 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400874 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400875 total_devices = btrfs_super_num_devices(disk_super);
David Sterba6f60cbd2013-02-15 11:31:02 -0700876
Stefan Behrensd03f9182012-11-05 13:10:49 +0000877 if (disk_super->label[0]) {
878 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
879 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500880 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000881 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200882 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000883 }
David Sterba6f60cbd2013-02-15 11:31:02 -0700884
Roland Dreier119e10c2009-01-21 10:49:16 -0500885 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500886 (unsigned long long)devid, (unsigned long long)transid, path);
David Sterba6f60cbd2013-02-15 11:31:02 -0700887
Chris Mason8a4b83c2008-03-24 15:02:07 -0400888 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400889 if (!ret && fs_devices_ret)
890 (*fs_devices_ret)->total_devices = total_devices;
David Sterba6f60cbd2013-02-15 11:31:02 -0700891
892error_unmap:
893 kunmap(page);
894 page_cache_release(page);
895
896error_bdev_put:
Tejun Heod4d77622010-11-13 11:55:18 +0100897 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400898error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100899 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400900 return ret;
901}
Chris Mason0b86a832008-03-24 15:01:56 -0400902
Miao Xie6d07bce2011-01-05 10:07:31 +0000903/* helper to account the used device space in the range */
904int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
905 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400906{
907 struct btrfs_key key;
908 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000909 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500910 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000911 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400912 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000913 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400914 struct extent_buffer *l;
915
Miao Xie6d07bce2011-01-05 10:07:31 +0000916 *length = 0;
917
Stefan Behrens63a212a2012-11-05 18:29:28 +0100918 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000919 return 0;
920
Yan Zheng2b820322008-11-17 21:11:30 -0500921 path = btrfs_alloc_path();
922 if (!path)
923 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400924 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400925
Chris Mason0b86a832008-03-24 15:01:56 -0400926 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000927 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400928 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000929
930 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400931 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000932 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400933 if (ret > 0) {
934 ret = btrfs_previous_item(root, path, key.objectid, key.type);
935 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000936 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400937 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000938
Chris Mason0b86a832008-03-24 15:01:56 -0400939 while (1) {
940 l = path->nodes[0];
941 slot = path->slots[0];
942 if (slot >= btrfs_header_nritems(l)) {
943 ret = btrfs_next_leaf(root, path);
944 if (ret == 0)
945 continue;
946 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000947 goto out;
948
949 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400950 }
951 btrfs_item_key_to_cpu(l, &key, slot);
952
953 if (key.objectid < device->devid)
954 goto next;
955
956 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000957 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400958
Chris Masond3977122009-01-05 21:25:51 -0500959 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400960 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400961
Chris Mason0b86a832008-03-24 15:01:56 -0400962 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000963 extent_end = key.offset + btrfs_dev_extent_length(l,
964 dev_extent);
965 if (key.offset <= start && extent_end > end) {
966 *length = end - start + 1;
967 break;
968 } else if (key.offset <= start && extent_end > start)
969 *length += extent_end - start;
970 else if (key.offset > start && extent_end <= end)
971 *length += extent_end - key.offset;
972 else if (key.offset > start && key.offset <= end) {
973 *length += end - key.offset + 1;
974 break;
975 } else if (key.offset > end)
976 break;
977
978next:
979 path->slots[0]++;
980 }
981 ret = 0;
982out:
983 btrfs_free_path(path);
984 return ret;
985}
986
Josef Bacik6df9a952013-06-27 13:22:46 -0400987static int contains_pending_extent(struct btrfs_trans_handle *trans,
988 struct btrfs_device *device,
989 u64 *start, u64 len)
990{
991 struct extent_map *em;
992 int ret = 0;
993
994 list_for_each_entry(em, &trans->transaction->pending_chunks, list) {
995 struct map_lookup *map;
996 int i;
997
998 map = (struct map_lookup *)em->bdev;
999 for (i = 0; i < map->num_stripes; i++) {
1000 if (map->stripes[i].dev != device)
1001 continue;
1002 if (map->stripes[i].physical >= *start + len ||
1003 map->stripes[i].physical + em->orig_block_len <=
1004 *start)
1005 continue;
1006 *start = map->stripes[i].physical +
1007 em->orig_block_len;
1008 ret = 1;
1009 }
1010 }
1011
1012 return ret;
1013}
1014
1015
Chris Mason0b86a832008-03-24 15:01:56 -04001016/*
Miao Xie7bfc8372011-01-05 10:07:26 +00001017 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +00001018 * @device: the device which we search the free space in
1019 * @num_bytes: the size of the free space that we need
1020 * @start: store the start of the free space.
1021 * @len: the size of the free space. that we find, or the size of the max
1022 * free space if we don't find suitable free space
1023 *
Chris Mason0b86a832008-03-24 15:01:56 -04001024 * this uses a pretty simple search, the expectation is that it is
1025 * called very infrequently and that a given device has a small number
1026 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +00001027 *
1028 * @start is used to store the start of the free space if we find. But if we
1029 * don't find suitable free space, it will be used to store the start position
1030 * of the max free space.
1031 *
1032 * @len is used to store the size of the free space that we find.
1033 * But if we don't find suitable free space, it is used to store the size of
1034 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -04001035 */
Josef Bacik6df9a952013-06-27 13:22:46 -04001036int find_free_dev_extent(struct btrfs_trans_handle *trans,
1037 struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +00001038 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -04001039{
1040 struct btrfs_key key;
1041 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +00001042 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -04001043 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +00001044 u64 hole_size;
1045 u64 max_hole_start;
1046 u64 max_hole_size;
1047 u64 extent_end;
1048 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001049 u64 search_end = device->total_bytes;
1050 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +00001051 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -04001052 struct extent_buffer *l;
1053
Chris Mason0b86a832008-03-24 15:01:56 -04001054 /* FIXME use last free of some kind */
1055
1056 /* we don't want to overwrite the superblock on the drive,
1057 * so we make sure to start at an offset of at least 1MB
1058 */
Arne Jansena9c9bf62011-04-12 11:01:20 +02001059 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -04001060
Josef Bacik6df9a952013-06-27 13:22:46 -04001061 path = btrfs_alloc_path();
1062 if (!path)
1063 return -ENOMEM;
1064again:
Miao Xie7bfc8372011-01-05 10:07:26 +00001065 max_hole_start = search_start;
1066 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +00001067 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +00001068
Stefan Behrens63a212a2012-11-05 18:29:28 +01001069 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +00001070 ret = -ENOSPC;
Josef Bacik6df9a952013-06-27 13:22:46 -04001071 goto out;
Miao Xie7bfc8372011-01-05 10:07:26 +00001072 }
1073
Miao Xie7bfc8372011-01-05 10:07:26 +00001074 path->reada = 2;
Josef Bacik6df9a952013-06-27 13:22:46 -04001075 path->search_commit_root = 1;
1076 path->skip_locking = 1;
Miao Xie7bfc8372011-01-05 10:07:26 +00001077
Chris Mason0b86a832008-03-24 15:01:56 -04001078 key.objectid = device->devid;
1079 key.offset = search_start;
1080 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +00001081
Li Zefan125ccb02011-12-08 15:07:24 +08001082 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001083 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001084 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001085 if (ret > 0) {
1086 ret = btrfs_previous_item(root, path, key.objectid, key.type);
1087 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001088 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001089 }
Miao Xie7bfc8372011-01-05 10:07:26 +00001090
Chris Mason0b86a832008-03-24 15:01:56 -04001091 while (1) {
1092 l = path->nodes[0];
1093 slot = path->slots[0];
1094 if (slot >= btrfs_header_nritems(l)) {
1095 ret = btrfs_next_leaf(root, path);
1096 if (ret == 0)
1097 continue;
1098 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +00001099 goto out;
1100
1101 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001102 }
1103 btrfs_item_key_to_cpu(l, &key, slot);
1104
1105 if (key.objectid < device->devid)
1106 goto next;
1107
1108 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001109 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001110
Chris Mason0b86a832008-03-24 15:01:56 -04001111 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1112 goto next;
1113
Miao Xie7bfc8372011-01-05 10:07:26 +00001114 if (key.offset > search_start) {
1115 hole_size = key.offset - search_start;
1116
Josef Bacik6df9a952013-06-27 13:22:46 -04001117 /*
1118 * Have to check before we set max_hole_start, otherwise
1119 * we could end up sending back this offset anyway.
1120 */
1121 if (contains_pending_extent(trans, device,
1122 &search_start,
1123 hole_size))
1124 hole_size = 0;
1125
Miao Xie7bfc8372011-01-05 10:07:26 +00001126 if (hole_size > max_hole_size) {
1127 max_hole_start = search_start;
1128 max_hole_size = hole_size;
1129 }
1130
1131 /*
1132 * If this free space is greater than which we need,
1133 * it must be the max free space that we have found
1134 * until now, so max_hole_start must point to the start
1135 * of this free space and the length of this free space
1136 * is stored in max_hole_size. Thus, we return
1137 * max_hole_start and max_hole_size and go back to the
1138 * caller.
1139 */
1140 if (hole_size >= num_bytes) {
1141 ret = 0;
1142 goto out;
1143 }
1144 }
1145
Chris Mason0b86a832008-03-24 15:01:56 -04001146 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001147 extent_end = key.offset + btrfs_dev_extent_length(l,
1148 dev_extent);
1149 if (extent_end > search_start)
1150 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001151next:
1152 path->slots[0]++;
1153 cond_resched();
1154 }
Chris Mason0b86a832008-03-24 15:01:56 -04001155
liubo38c01b92011-08-02 02:39:03 +00001156 /*
1157 * At this point, search_start should be the end of
1158 * allocated dev extents, and when shrinking the device,
1159 * search_end may be smaller than search_start.
1160 */
1161 if (search_end > search_start)
1162 hole_size = search_end - search_start;
1163
Miao Xie7bfc8372011-01-05 10:07:26 +00001164 if (hole_size > max_hole_size) {
1165 max_hole_start = search_start;
1166 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001167 }
Chris Mason0b86a832008-03-24 15:01:56 -04001168
Josef Bacik6df9a952013-06-27 13:22:46 -04001169 if (contains_pending_extent(trans, device, &search_start, hole_size)) {
1170 btrfs_release_path(path);
1171 goto again;
1172 }
1173
Miao Xie7bfc8372011-01-05 10:07:26 +00001174 /* See above. */
1175 if (hole_size < num_bytes)
1176 ret = -ENOSPC;
1177 else
1178 ret = 0;
1179
1180out:
Yan Zheng2b820322008-11-17 21:11:30 -05001181 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001182 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001183 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001184 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001185 return ret;
1186}
1187
Christoph Hellwigb2950862008-12-02 09:54:17 -05001188static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001189 struct btrfs_device *device,
1190 u64 start)
1191{
1192 int ret;
1193 struct btrfs_path *path;
1194 struct btrfs_root *root = device->dev_root;
1195 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001196 struct btrfs_key found_key;
1197 struct extent_buffer *leaf = NULL;
1198 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001199
1200 path = btrfs_alloc_path();
1201 if (!path)
1202 return -ENOMEM;
1203
1204 key.objectid = device->devid;
1205 key.offset = start;
1206 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001207again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001208 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001209 if (ret > 0) {
1210 ret = btrfs_previous_item(root, path, key.objectid,
1211 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001212 if (ret)
1213 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001214 leaf = path->nodes[0];
1215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1216 extent = btrfs_item_ptr(leaf, path->slots[0],
1217 struct btrfs_dev_extent);
1218 BUG_ON(found_key.offset > start || found_key.offset +
1219 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001220 key = found_key;
1221 btrfs_release_path(path);
1222 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001223 } else if (ret == 0) {
1224 leaf = path->nodes[0];
1225 extent = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001227 } else {
1228 btrfs_error(root->fs_info, ret, "Slot search failed");
1229 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001230 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001231
Josef Bacik2bf64752011-09-26 17:12:22 -04001232 if (device->bytes_used > 0) {
1233 u64 len = btrfs_dev_extent_length(leaf, extent);
1234 device->bytes_used -= len;
1235 spin_lock(&root->fs_info->free_chunk_lock);
1236 root->fs_info->free_chunk_space += len;
1237 spin_unlock(&root->fs_info->free_chunk_lock);
1238 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001239 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001240 if (ret) {
1241 btrfs_error(root->fs_info, ret,
1242 "Failed to remove dev extent item");
1243 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001244out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001245 btrfs_free_path(path);
1246 return ret;
1247}
1248
Eric Sandeen48a3b632013-04-25 20:41:01 +00001249static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1250 struct btrfs_device *device,
1251 u64 chunk_tree, u64 chunk_objectid,
1252 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001253{
1254 int ret;
1255 struct btrfs_path *path;
1256 struct btrfs_root *root = device->dev_root;
1257 struct btrfs_dev_extent *extent;
1258 struct extent_buffer *leaf;
1259 struct btrfs_key key;
1260
Chris Masondfe25022008-05-13 13:46:40 -04001261 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001262 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001263 path = btrfs_alloc_path();
1264 if (!path)
1265 return -ENOMEM;
1266
Chris Mason0b86a832008-03-24 15:01:56 -04001267 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001268 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001269 key.type = BTRFS_DEV_EXTENT_KEY;
1270 ret = btrfs_insert_empty_item(trans, root, path, &key,
1271 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001272 if (ret)
1273 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001274
1275 leaf = path->nodes[0];
1276 extent = btrfs_item_ptr(leaf, path->slots[0],
1277 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001278 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1279 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1280 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1281
1282 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1283 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1284 BTRFS_UUID_SIZE);
1285
Chris Mason0b86a832008-03-24 15:01:56 -04001286 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1287 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001288out:
Chris Mason0b86a832008-03-24 15:01:56 -04001289 btrfs_free_path(path);
1290 return ret;
1291}
1292
Josef Bacik6df9a952013-06-27 13:22:46 -04001293static u64 find_next_chunk(struct btrfs_fs_info *fs_info)
Chris Mason0b86a832008-03-24 15:01:56 -04001294{
Josef Bacik6df9a952013-06-27 13:22:46 -04001295 struct extent_map_tree *em_tree;
1296 struct extent_map *em;
1297 struct rb_node *n;
1298 u64 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001299
Josef Bacik6df9a952013-06-27 13:22:46 -04001300 em_tree = &fs_info->mapping_tree.map_tree;
1301 read_lock(&em_tree->lock);
1302 n = rb_last(&em_tree->map);
1303 if (n) {
1304 em = rb_entry(n, struct extent_map, rb_node);
1305 ret = em->start + em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04001306 }
Josef Bacik6df9a952013-06-27 13:22:46 -04001307 read_unlock(&em_tree->lock);
1308
Chris Mason0b86a832008-03-24 15:01:56 -04001309 return ret;
1310}
1311
Ilya Dryomov53f10652013-08-12 14:33:01 +03001312static noinline int find_next_devid(struct btrfs_fs_info *fs_info,
1313 u64 *devid_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04001314{
1315 int ret;
1316 struct btrfs_key key;
1317 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001318 struct btrfs_path *path;
1319
Yan Zheng2b820322008-11-17 21:11:30 -05001320 path = btrfs_alloc_path();
1321 if (!path)
1322 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001323
1324 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1325 key.type = BTRFS_DEV_ITEM_KEY;
1326 key.offset = (u64)-1;
1327
Ilya Dryomov53f10652013-08-12 14:33:01 +03001328 ret = btrfs_search_slot(NULL, fs_info->chunk_root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001329 if (ret < 0)
1330 goto error;
1331
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001332 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001333
Ilya Dryomov53f10652013-08-12 14:33:01 +03001334 ret = btrfs_previous_item(fs_info->chunk_root, path,
1335 BTRFS_DEV_ITEMS_OBJECTID,
Chris Mason0b86a832008-03-24 15:01:56 -04001336 BTRFS_DEV_ITEM_KEY);
1337 if (ret) {
Ilya Dryomov53f10652013-08-12 14:33:01 +03001338 *devid_ret = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001339 } else {
1340 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1341 path->slots[0]);
Ilya Dryomov53f10652013-08-12 14:33:01 +03001342 *devid_ret = found_key.offset + 1;
Chris Mason0b86a832008-03-24 15:01:56 -04001343 }
1344 ret = 0;
1345error:
Yan Zheng2b820322008-11-17 21:11:30 -05001346 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001347 return ret;
1348}
1349
1350/*
1351 * the device information is stored in the chunk root
1352 * the btrfs_device struct should be fully filled in
1353 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001354static int btrfs_add_device(struct btrfs_trans_handle *trans,
1355 struct btrfs_root *root,
1356 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04001357{
1358 int ret;
1359 struct btrfs_path *path;
1360 struct btrfs_dev_item *dev_item;
1361 struct extent_buffer *leaf;
1362 struct btrfs_key key;
1363 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001364
1365 root = root->fs_info->chunk_root;
1366
1367 path = btrfs_alloc_path();
1368 if (!path)
1369 return -ENOMEM;
1370
Chris Mason0b86a832008-03-24 15:01:56 -04001371 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1372 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001373 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001374
1375 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001376 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001377 if (ret)
1378 goto out;
1379
1380 leaf = path->nodes[0];
1381 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1382
1383 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001384 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001385 btrfs_set_device_type(leaf, dev_item, device->type);
1386 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1387 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1388 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001389 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1390 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001391 btrfs_set_device_group(leaf, dev_item, 0);
1392 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1393 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001394 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001395
Chris Mason0b86a832008-03-24 15:01:56 -04001396 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001397 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001398 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1399 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001400 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001401
Yan Zheng2b820322008-11-17 21:11:30 -05001402 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001403out:
1404 btrfs_free_path(path);
1405 return ret;
1406}
Chris Mason8f18cf12008-04-25 16:53:30 -04001407
Chris Masona061fc82008-05-07 11:43:44 -04001408static int btrfs_rm_dev_item(struct btrfs_root *root,
1409 struct btrfs_device *device)
1410{
1411 int ret;
1412 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001413 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001414 struct btrfs_trans_handle *trans;
1415
1416 root = root->fs_info->chunk_root;
1417
1418 path = btrfs_alloc_path();
1419 if (!path)
1420 return -ENOMEM;
1421
Yan, Zhenga22285a2010-05-16 10:48:46 -04001422 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001423 if (IS_ERR(trans)) {
1424 btrfs_free_path(path);
1425 return PTR_ERR(trans);
1426 }
Chris Masona061fc82008-05-07 11:43:44 -04001427 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1428 key.type = BTRFS_DEV_ITEM_KEY;
1429 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001430 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001431
1432 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1433 if (ret < 0)
1434 goto out;
1435
1436 if (ret > 0) {
1437 ret = -ENOENT;
1438 goto out;
1439 }
1440
1441 ret = btrfs_del_item(trans, root, path);
1442 if (ret)
1443 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001444out:
1445 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001446 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001447 btrfs_commit_transaction(trans, root);
1448 return ret;
1449}
1450
1451int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1452{
1453 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001454 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001455 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001456 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001457 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001458 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001459 u64 all_avail;
1460 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001461 u64 num_devices;
1462 u8 *dev_uuid;
Miao Xiede98ced2013-01-29 10:13:12 +00001463 unsigned seq;
Chris Masona061fc82008-05-07 11:43:44 -04001464 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001465 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001466
Chris Masona061fc82008-05-07 11:43:44 -04001467 mutex_lock(&uuid_mutex);
1468
Miao Xiede98ced2013-01-29 10:13:12 +00001469 do {
1470 seq = read_seqbegin(&root->fs_info->profiles_lock);
1471
1472 all_avail = root->fs_info->avail_data_alloc_bits |
1473 root->fs_info->avail_system_alloc_bits |
1474 root->fs_info->avail_metadata_alloc_bits;
1475 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Chris Masona061fc82008-05-07 11:43:44 -04001476
Stefan Behrens8dabb742012-11-06 13:15:27 +01001477 num_devices = root->fs_info->fs_devices->num_devices;
1478 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1479 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1480 WARN_ON(num_devices < 1);
1481 num_devices--;
1482 }
1483 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1484
1485 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Anand Jain183860f2013-05-17 10:52:45 +00001486 ret = BTRFS_ERROR_DEV_RAID10_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001487 goto out;
1488 }
1489
Stefan Behrens8dabb742012-11-06 13:15:27 +01001490 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001491 ret = BTRFS_ERROR_DEV_RAID1_MIN_NOT_MET;
Chris Masona061fc82008-05-07 11:43:44 -04001492 goto out;
1493 }
1494
David Woodhouse53b381b2013-01-29 18:40:14 -05001495 if ((all_avail & BTRFS_BLOCK_GROUP_RAID5) &&
1496 root->fs_info->fs_devices->rw_devices <= 2) {
Anand Jain183860f2013-05-17 10:52:45 +00001497 ret = BTRFS_ERROR_DEV_RAID5_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001498 goto out;
1499 }
1500 if ((all_avail & BTRFS_BLOCK_GROUP_RAID6) &&
1501 root->fs_info->fs_devices->rw_devices <= 3) {
Anand Jain183860f2013-05-17 10:52:45 +00001502 ret = BTRFS_ERROR_DEV_RAID6_MIN_NOT_MET;
David Woodhouse53b381b2013-01-29 18:40:14 -05001503 goto out;
1504 }
1505
Chris Masondfe25022008-05-13 13:46:40 -04001506 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001507 struct list_head *devices;
1508 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001509
Chris Masondfe25022008-05-13 13:46:40 -04001510 device = NULL;
1511 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001512 /*
1513 * It is safe to read the devices since the volume_mutex
1514 * is held.
1515 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001516 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001517 if (tmp->in_fs_metadata &&
1518 !tmp->is_tgtdev_for_dev_replace &&
1519 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001520 device = tmp;
1521 break;
1522 }
1523 }
1524 bdev = NULL;
1525 bh = NULL;
1526 disk_super = NULL;
1527 if (!device) {
Anand Jain183860f2013-05-17 10:52:45 +00001528 ret = BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
Chris Masondfe25022008-05-13 13:46:40 -04001529 goto out;
1530 }
Chris Masondfe25022008-05-13 13:46:40 -04001531 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001532 ret = btrfs_get_bdev_and_sb(device_path,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001533 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001534 root->fs_info->bdev_holder, 0,
1535 &bdev, &bh);
1536 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001537 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001538 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001539 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001540 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001541 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001542 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001543 if (!device) {
1544 ret = -ENOENT;
1545 goto error_brelse;
1546 }
Chris Masondfe25022008-05-13 13:46:40 -04001547 }
Yan Zheng2b820322008-11-17 21:11:30 -05001548
Stefan Behrens63a212a2012-11-05 18:29:28 +01001549 if (device->is_tgtdev_for_dev_replace) {
Anand Jain183860f2013-05-17 10:52:45 +00001550 ret = BTRFS_ERROR_DEV_TGT_REPLACE;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001551 goto error_brelse;
1552 }
1553
Yan Zheng2b820322008-11-17 21:11:30 -05001554 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Anand Jain183860f2013-05-17 10:52:45 +00001555 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
Yan Zheng2b820322008-11-17 21:11:30 -05001556 goto error_brelse;
1557 }
1558
1559 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001560 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001561 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001562 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001563 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001564 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001565 }
Chris Masona061fc82008-05-07 11:43:44 -04001566
Carey Underwoodd7901552013-03-04 16:37:06 -06001567 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001568 ret = btrfs_shrink_device(device, 0);
Carey Underwoodd7901552013-03-04 16:37:06 -06001569 mutex_lock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001570 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001571 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001572
Stefan Behrens63a212a2012-11-05 18:29:28 +01001573 /*
1574 * TODO: the superblock still includes this device in its num_devices
1575 * counter although write_all_supers() is not locked out. This
1576 * could give a filesystem state which requires a degraded mount.
1577 */
Chris Masona061fc82008-05-07 11:43:44 -04001578 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1579 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001580 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001581
Josef Bacik2bf64752011-09-26 17:12:22 -04001582 spin_lock(&root->fs_info->free_chunk_lock);
1583 root->fs_info->free_chunk_space = device->total_bytes -
1584 device->bytes_used;
1585 spin_unlock(&root->fs_info->free_chunk_lock);
1586
Yan Zheng2b820322008-11-17 21:11:30 -05001587 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001588 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001589
1590 /*
1591 * the device list mutex makes sure that we don't change
1592 * the device list while someone else is writing out all
1593 * the device supers.
1594 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001595
1596 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001597 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001598 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001599
Yan Zhenge4404d62008-12-12 10:03:26 -05001600 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001601 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001602
Chris Masoncd02dca2010-12-13 14:56:23 -05001603 if (device->missing)
1604 root->fs_info->fs_devices->missing_devices--;
1605
Yan Zheng2b820322008-11-17 21:11:30 -05001606 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1607 struct btrfs_device, dev_list);
1608 if (device->bdev == root->fs_info->sb->s_bdev)
1609 root->fs_info->sb->s_bdev = next_device->bdev;
1610 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1611 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1612
Xiao Guangrong1f781602011-04-20 10:09:16 +00001613 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001614 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001615
1616 call_rcu(&device->rcu, free_device);
1617 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001618
David Sterba6c417612011-04-13 15:41:04 +02001619 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1620 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001621
Xiao Guangrong1f781602011-04-20 10:09:16 +00001622 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001623 struct btrfs_fs_devices *fs_devices;
1624 fs_devices = root->fs_info->fs_devices;
1625 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001626 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001627 break;
1628 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001629 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001630 fs_devices->seed = cur_devices->seed;
1631 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001632 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001633 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001634 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001635 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001636 }
1637
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001638 root->fs_info->num_tolerated_disk_barrier_failures =
1639 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1640
Yan Zheng2b820322008-11-17 21:11:30 -05001641 /*
1642 * at this point, the device is zero sized. We want to
1643 * remove it from the devices list and zero out the old super
1644 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001645 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001646 /* make sure this device isn't detected as part of
1647 * the FS anymore
1648 */
1649 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1650 set_buffer_dirty(bh);
1651 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001652 }
Chris Masona061fc82008-05-07 11:43:44 -04001653
Chris Masona061fc82008-05-07 11:43:44 -04001654 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001655
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001656 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001657 if (bdev)
1658 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001659
Chris Masona061fc82008-05-07 11:43:44 -04001660error_brelse:
1661 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001662 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001663 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001664out:
1665 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001666 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001667error_undo:
1668 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001669 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001670 list_add(&device->dev_alloc_list,
1671 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001672 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001673 root->fs_info->fs_devices->rw_devices++;
1674 }
1675 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001676}
1677
Stefan Behrense93c89c2012-11-05 17:33:06 +01001678void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1679 struct btrfs_device *srcdev)
1680{
1681 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1682 list_del_rcu(&srcdev->dev_list);
1683 list_del_rcu(&srcdev->dev_alloc_list);
1684 fs_info->fs_devices->num_devices--;
1685 if (srcdev->missing) {
1686 fs_info->fs_devices->missing_devices--;
1687 fs_info->fs_devices->rw_devices++;
1688 }
1689 if (srcdev->can_discard)
1690 fs_info->fs_devices->num_can_discard--;
1691 if (srcdev->bdev)
1692 fs_info->fs_devices->open_devices--;
1693
1694 call_rcu(&srcdev->rcu, free_device);
1695}
1696
1697void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1698 struct btrfs_device *tgtdev)
1699{
1700 struct btrfs_device *next_device;
1701
1702 WARN_ON(!tgtdev);
1703 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1704 if (tgtdev->bdev) {
1705 btrfs_scratch_superblock(tgtdev);
1706 fs_info->fs_devices->open_devices--;
1707 }
1708 fs_info->fs_devices->num_devices--;
1709 if (tgtdev->can_discard)
1710 fs_info->fs_devices->num_can_discard++;
1711
1712 next_device = list_entry(fs_info->fs_devices->devices.next,
1713 struct btrfs_device, dev_list);
1714 if (tgtdev->bdev == fs_info->sb->s_bdev)
1715 fs_info->sb->s_bdev = next_device->bdev;
1716 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1717 fs_info->fs_devices->latest_bdev = next_device->bdev;
1718 list_del_rcu(&tgtdev->dev_list);
1719
1720 call_rcu(&tgtdev->rcu, free_device);
1721
1722 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1723}
1724
Eric Sandeen48a3b632013-04-25 20:41:01 +00001725static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1726 struct btrfs_device **device)
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001727{
1728 int ret = 0;
1729 struct btrfs_super_block *disk_super;
1730 u64 devid;
1731 u8 *dev_uuid;
1732 struct block_device *bdev;
1733 struct buffer_head *bh;
1734
1735 *device = NULL;
1736 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1737 root->fs_info->bdev_holder, 0, &bdev, &bh);
1738 if (ret)
1739 return ret;
1740 disk_super = (struct btrfs_super_block *)bh->b_data;
1741 devid = btrfs_stack_device_id(&disk_super->dev_item);
1742 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001743 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001744 disk_super->fsid);
1745 brelse(bh);
1746 if (!*device)
1747 ret = -ENOENT;
1748 blkdev_put(bdev, FMODE_READ);
1749 return ret;
1750}
1751
1752int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1753 char *device_path,
1754 struct btrfs_device **device)
1755{
1756 *device = NULL;
1757 if (strcmp(device_path, "missing") == 0) {
1758 struct list_head *devices;
1759 struct btrfs_device *tmp;
1760
1761 devices = &root->fs_info->fs_devices->devices;
1762 /*
1763 * It is safe to read the devices since the volume_mutex
1764 * is held by the caller.
1765 */
1766 list_for_each_entry(tmp, devices, dev_list) {
1767 if (tmp->in_fs_metadata && !tmp->bdev) {
1768 *device = tmp;
1769 break;
1770 }
1771 }
1772
1773 if (!*device) {
1774 pr_err("btrfs: no missing device found\n");
1775 return -ENOENT;
1776 }
1777
1778 return 0;
1779 } else {
1780 return btrfs_find_device_by_path(root, device_path, device);
1781 }
1782}
1783
Yan Zheng2b820322008-11-17 21:11:30 -05001784/*
1785 * does all the dirty work required for changing file system's UUID.
1786 */
Li Zefan125ccb02011-12-08 15:07:24 +08001787static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001788{
1789 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1790 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001791 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001792 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001793 struct btrfs_device *device;
1794 u64 super_flags;
1795
1796 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001797 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001798 return -EINVAL;
1799
Yan Zhenge4404d62008-12-12 10:03:26 -05001800 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1801 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001802 return -ENOMEM;
1803
Yan Zhenge4404d62008-12-12 10:03:26 -05001804 old_devices = clone_fs_devices(fs_devices);
1805 if (IS_ERR(old_devices)) {
1806 kfree(seed_devices);
1807 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001808 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001809
Yan Zheng2b820322008-11-17 21:11:30 -05001810 list_add(&old_devices->list, &fs_uuids);
1811
Yan Zhenge4404d62008-12-12 10:03:26 -05001812 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1813 seed_devices->opened = 1;
1814 INIT_LIST_HEAD(&seed_devices->devices);
1815 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001816 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001817
1818 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001819 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1820 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001821 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1822
Yan Zhenge4404d62008-12-12 10:03:26 -05001823 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1824 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1825 device->fs_devices = seed_devices;
1826 }
1827
Yan Zheng2b820322008-11-17 21:11:30 -05001828 fs_devices->seeding = 0;
1829 fs_devices->num_devices = 0;
1830 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001831 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001832 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001833
1834 generate_random_uuid(fs_devices->fsid);
1835 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1836 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1837 super_flags = btrfs_super_flags(disk_super) &
1838 ~BTRFS_SUPER_FLAG_SEEDING;
1839 btrfs_set_super_flags(disk_super, super_flags);
1840
1841 return 0;
1842}
1843
1844/*
1845 * strore the expected generation for seed devices in device items.
1846 */
1847static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1848 struct btrfs_root *root)
1849{
1850 struct btrfs_path *path;
1851 struct extent_buffer *leaf;
1852 struct btrfs_dev_item *dev_item;
1853 struct btrfs_device *device;
1854 struct btrfs_key key;
1855 u8 fs_uuid[BTRFS_UUID_SIZE];
1856 u8 dev_uuid[BTRFS_UUID_SIZE];
1857 u64 devid;
1858 int ret;
1859
1860 path = btrfs_alloc_path();
1861 if (!path)
1862 return -ENOMEM;
1863
1864 root = root->fs_info->chunk_root;
1865 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1866 key.offset = 0;
1867 key.type = BTRFS_DEV_ITEM_KEY;
1868
1869 while (1) {
1870 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1871 if (ret < 0)
1872 goto error;
1873
1874 leaf = path->nodes[0];
1875next_slot:
1876 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1877 ret = btrfs_next_leaf(root, path);
1878 if (ret > 0)
1879 break;
1880 if (ret < 0)
1881 goto error;
1882 leaf = path->nodes[0];
1883 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001884 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001885 continue;
1886 }
1887
1888 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1889 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1890 key.type != BTRFS_DEV_ITEM_KEY)
1891 break;
1892
1893 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1894 struct btrfs_dev_item);
1895 devid = btrfs_device_id(leaf, dev_item);
1896 read_extent_buffer(leaf, dev_uuid,
1897 (unsigned long)btrfs_device_uuid(dev_item),
1898 BTRFS_UUID_SIZE);
1899 read_extent_buffer(leaf, fs_uuid,
1900 (unsigned long)btrfs_device_fsid(dev_item),
1901 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001902 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1903 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001904 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001905
1906 if (device->fs_devices->seeding) {
1907 btrfs_set_device_generation(leaf, dev_item,
1908 device->generation);
1909 btrfs_mark_buffer_dirty(leaf);
1910 }
1911
1912 path->slots[0]++;
1913 goto next_slot;
1914 }
1915 ret = 0;
1916error:
1917 btrfs_free_path(path);
1918 return ret;
1919}
1920
Chris Mason788f20e2008-04-28 15:29:42 -04001921int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1922{
Josef Bacikd5e20032011-08-04 14:52:27 +00001923 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001924 struct btrfs_trans_handle *trans;
1925 struct btrfs_device *device;
1926 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001927 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001928 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001929 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001930 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001931 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001932 int ret = 0;
1933
Yan Zheng2b820322008-11-17 21:11:30 -05001934 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001935 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001936
Li Zefana5d16332011-12-07 20:08:40 -05001937 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001938 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001939 if (IS_ERR(bdev))
1940 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001941
Yan Zheng2b820322008-11-17 21:11:30 -05001942 if (root->fs_info->fs_devices->seeding) {
1943 seeding_dev = 1;
1944 down_write(&sb->s_umount);
1945 mutex_lock(&uuid_mutex);
1946 }
1947
Chris Mason8c8bee12008-09-29 11:19:10 -04001948 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001949
Chris Mason788f20e2008-04-28 15:29:42 -04001950 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001951
1952 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001953 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001954 if (device->bdev == bdev) {
1955 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001956 mutex_unlock(
1957 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001958 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001959 }
1960 }
Liu Bod25628b2012-11-14 14:35:30 +00001961 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001962
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001963 device = btrfs_alloc_device(root->fs_info, NULL, NULL);
1964 if (IS_ERR(device)) {
Chris Mason788f20e2008-04-28 15:29:42 -04001965 /* we can safely leave the fs_devices entry around */
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03001966 ret = PTR_ERR(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001967 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001968 }
1969
Josef Bacik606686e2012-06-04 14:03:51 -04001970 name = rcu_string_strdup(device_path, GFP_NOFS);
1971 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001972 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001973 ret = -ENOMEM;
1974 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001975 }
Josef Bacik606686e2012-06-04 14:03:51 -04001976 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001977
Yan, Zhenga22285a2010-05-16 10:48:46 -04001978 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001979 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001980 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001981 kfree(device);
1982 ret = PTR_ERR(trans);
1983 goto error;
1984 }
1985
Yan Zheng2b820322008-11-17 21:11:30 -05001986 lock_chunks(root);
1987
Josef Bacikd5e20032011-08-04 14:52:27 +00001988 q = bdev_get_queue(bdev);
1989 if (blk_queue_discard(q))
1990 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001991 device->writeable = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001992 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001993 device->io_width = root->sectorsize;
1994 device->io_align = root->sectorsize;
1995 device->sector_size = root->sectorsize;
1996 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001997 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001998 device->dev_root = root->fs_info->dev_root;
1999 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04002000 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002001 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00002002 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04002003 set_blocksize(device->bdev, 4096);
2004
Yan Zheng2b820322008-11-17 21:11:30 -05002005 if (seeding_dev) {
2006 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08002007 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002008 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05002009 }
2010
2011 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04002012
Chris Masone5e9a522009-06-10 15:17:02 -04002013 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00002014 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05002015 list_add(&device->dev_alloc_list,
2016 &root->fs_info->fs_devices->alloc_list);
2017 root->fs_info->fs_devices->num_devices++;
2018 root->fs_info->fs_devices->open_devices++;
2019 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04002020 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00002021 if (device->can_discard)
2022 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05002023 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2024
Josef Bacik2bf64752011-09-26 17:12:22 -04002025 spin_lock(&root->fs_info->free_chunk_lock);
2026 root->fs_info->free_chunk_space += device->total_bytes;
2027 spin_unlock(&root->fs_info->free_chunk_lock);
2028
Chris Masonc2898112009-06-10 09:51:32 -04002029 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2030 root->fs_info->fs_devices->rotating = 1;
2031
David Sterba6c417612011-04-13 15:41:04 +02002032 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
2033 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002034 total_bytes + device->total_bytes);
2035
David Sterba6c417612011-04-13 15:41:04 +02002036 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
2037 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04002038 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04002039 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04002040
Yan Zheng2b820322008-11-17 21:11:30 -05002041 if (seeding_dev) {
2042 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002043 if (ret) {
2044 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002045 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002046 }
Yan Zheng2b820322008-11-17 21:11:30 -05002047 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06002048 if (ret) {
2049 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002050 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002051 }
Yan Zheng2b820322008-11-17 21:11:30 -05002052 } else {
2053 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06002054 if (ret) {
2055 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002056 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06002057 }
Yan Zheng2b820322008-11-17 21:11:30 -05002058 }
2059
Chris Mason913d9522009-03-10 13:17:18 -04002060 /*
2061 * we've got more storage, clear any full flags on the space
2062 * infos
2063 */
2064 btrfs_clear_space_info_full(root->fs_info);
2065
Chris Mason7d9eb122008-07-08 14:19:17 -04002066 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02002067 root->fs_info->num_tolerated_disk_barrier_failures =
2068 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002069 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002070
2071 if (seeding_dev) {
2072 mutex_unlock(&uuid_mutex);
2073 up_write(&sb->s_umount);
2074
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002075 if (ret) /* transaction commit */
2076 return ret;
2077
Yan Zheng2b820322008-11-17 21:11:30 -05002078 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002079 if (ret < 0)
2080 btrfs_error(root->fs_info, ret,
2081 "Failed to relocate sys chunks after "
2082 "device initialization. This can be fixed "
2083 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00002084 trans = btrfs_attach_transaction(root);
2085 if (IS_ERR(trans)) {
2086 if (PTR_ERR(trans) == -ENOENT)
2087 return 0;
2088 return PTR_ERR(trans);
2089 }
2090 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002091 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002092
Chris Mason788f20e2008-04-28 15:29:42 -04002093 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002094
2095error_trans:
2096 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002097 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002098 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002099 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002100error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002101 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002102 if (seeding_dev) {
2103 mutex_unlock(&uuid_mutex);
2104 up_write(&sb->s_umount);
2105 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002106 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002107}
2108
Stefan Behrense93c89c2012-11-05 17:33:06 +01002109int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2110 struct btrfs_device **device_out)
2111{
2112 struct request_queue *q;
2113 struct btrfs_device *device;
2114 struct block_device *bdev;
2115 struct btrfs_fs_info *fs_info = root->fs_info;
2116 struct list_head *devices;
2117 struct rcu_string *name;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002118 u64 devid = BTRFS_DEV_REPLACE_DEVID;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002119 int ret = 0;
2120
2121 *device_out = NULL;
2122 if (fs_info->fs_devices->seeding)
2123 return -EINVAL;
2124
2125 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2126 fs_info->bdev_holder);
2127 if (IS_ERR(bdev))
2128 return PTR_ERR(bdev);
2129
2130 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2131
2132 devices = &fs_info->fs_devices->devices;
2133 list_for_each_entry(device, devices, dev_list) {
2134 if (device->bdev == bdev) {
2135 ret = -EEXIST;
2136 goto error;
2137 }
2138 }
2139
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03002140 device = btrfs_alloc_device(NULL, &devid, NULL);
2141 if (IS_ERR(device)) {
2142 ret = PTR_ERR(device);
Stefan Behrense93c89c2012-11-05 17:33:06 +01002143 goto error;
2144 }
2145
2146 name = rcu_string_strdup(device_path, GFP_NOFS);
2147 if (!name) {
2148 kfree(device);
2149 ret = -ENOMEM;
2150 goto error;
2151 }
2152 rcu_assign_pointer(device->name, name);
2153
2154 q = bdev_get_queue(bdev);
2155 if (blk_queue_discard(q))
2156 device->can_discard = 1;
2157 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2158 device->writeable = 1;
Stefan Behrense93c89c2012-11-05 17:33:06 +01002159 device->generation = 0;
2160 device->io_width = root->sectorsize;
2161 device->io_align = root->sectorsize;
2162 device->sector_size = root->sectorsize;
2163 device->total_bytes = i_size_read(bdev->bd_inode);
2164 device->disk_total_bytes = device->total_bytes;
2165 device->dev_root = fs_info->dev_root;
2166 device->bdev = bdev;
2167 device->in_fs_metadata = 1;
2168 device->is_tgtdev_for_dev_replace = 1;
2169 device->mode = FMODE_EXCL;
2170 set_blocksize(device->bdev, 4096);
2171 device->fs_devices = fs_info->fs_devices;
2172 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2173 fs_info->fs_devices->num_devices++;
2174 fs_info->fs_devices->open_devices++;
2175 if (device->can_discard)
2176 fs_info->fs_devices->num_can_discard++;
2177 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2178
2179 *device_out = device;
2180 return ret;
2181
2182error:
2183 blkdev_put(bdev, FMODE_EXCL);
2184 return ret;
2185}
2186
2187void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2188 struct btrfs_device *tgtdev)
2189{
2190 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2191 tgtdev->io_width = fs_info->dev_root->sectorsize;
2192 tgtdev->io_align = fs_info->dev_root->sectorsize;
2193 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2194 tgtdev->dev_root = fs_info->dev_root;
2195 tgtdev->in_fs_metadata = 1;
2196}
2197
Chris Masond3977122009-01-05 21:25:51 -05002198static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2199 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002200{
2201 int ret;
2202 struct btrfs_path *path;
2203 struct btrfs_root *root;
2204 struct btrfs_dev_item *dev_item;
2205 struct extent_buffer *leaf;
2206 struct btrfs_key key;
2207
2208 root = device->dev_root->fs_info->chunk_root;
2209
2210 path = btrfs_alloc_path();
2211 if (!path)
2212 return -ENOMEM;
2213
2214 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2215 key.type = BTRFS_DEV_ITEM_KEY;
2216 key.offset = device->devid;
2217
2218 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2219 if (ret < 0)
2220 goto out;
2221
2222 if (ret > 0) {
2223 ret = -ENOENT;
2224 goto out;
2225 }
2226
2227 leaf = path->nodes[0];
2228 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2229
2230 btrfs_set_device_id(leaf, dev_item, device->devid);
2231 btrfs_set_device_type(leaf, dev_item, device->type);
2232 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2233 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2234 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002235 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002236 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2237 btrfs_mark_buffer_dirty(leaf);
2238
2239out:
2240 btrfs_free_path(path);
2241 return ret;
2242}
2243
Chris Mason7d9eb122008-07-08 14:19:17 -04002244static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002245 struct btrfs_device *device, u64 new_size)
2246{
2247 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002248 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002249 u64 old_total = btrfs_super_total_bytes(super_copy);
2250 u64 diff = new_size - device->total_bytes;
2251
Yan Zheng2b820322008-11-17 21:11:30 -05002252 if (!device->writeable)
2253 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002254 if (new_size <= device->total_bytes ||
2255 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002256 return -EINVAL;
2257
Chris Mason8f18cf12008-04-25 16:53:30 -04002258 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002259 device->fs_devices->total_rw_bytes += diff;
2260
2261 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002262 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002263 btrfs_clear_space_info_full(device->dev_root->fs_info);
2264
Chris Mason8f18cf12008-04-25 16:53:30 -04002265 return btrfs_update_device(trans, device);
2266}
2267
Chris Mason7d9eb122008-07-08 14:19:17 -04002268int btrfs_grow_device(struct btrfs_trans_handle *trans,
2269 struct btrfs_device *device, u64 new_size)
2270{
2271 int ret;
2272 lock_chunks(device->dev_root);
2273 ret = __btrfs_grow_device(trans, device, new_size);
2274 unlock_chunks(device->dev_root);
2275 return ret;
2276}
2277
Chris Mason8f18cf12008-04-25 16:53:30 -04002278static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2279 struct btrfs_root *root,
2280 u64 chunk_tree, u64 chunk_objectid,
2281 u64 chunk_offset)
2282{
2283 int ret;
2284 struct btrfs_path *path;
2285 struct btrfs_key key;
2286
2287 root = root->fs_info->chunk_root;
2288 path = btrfs_alloc_path();
2289 if (!path)
2290 return -ENOMEM;
2291
2292 key.objectid = chunk_objectid;
2293 key.offset = chunk_offset;
2294 key.type = BTRFS_CHUNK_ITEM_KEY;
2295
2296 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 if (ret < 0)
2298 goto out;
2299 else if (ret > 0) { /* Logic error or corruption */
2300 btrfs_error(root->fs_info, -ENOENT,
2301 "Failed lookup while freeing chunk.");
2302 ret = -ENOENT;
2303 goto out;
2304 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002305
2306 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002307 if (ret < 0)
2308 btrfs_error(root->fs_info, ret,
2309 "Failed to delete chunk item.");
2310out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002311 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002312 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002313}
2314
Christoph Hellwigb2950862008-12-02 09:54:17 -05002315static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002316 chunk_offset)
2317{
David Sterba6c417612011-04-13 15:41:04 +02002318 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002319 struct btrfs_disk_key *disk_key;
2320 struct btrfs_chunk *chunk;
2321 u8 *ptr;
2322 int ret = 0;
2323 u32 num_stripes;
2324 u32 array_size;
2325 u32 len = 0;
2326 u32 cur;
2327 struct btrfs_key key;
2328
2329 array_size = btrfs_super_sys_array_size(super_copy);
2330
2331 ptr = super_copy->sys_chunk_array;
2332 cur = 0;
2333
2334 while (cur < array_size) {
2335 disk_key = (struct btrfs_disk_key *)ptr;
2336 btrfs_disk_key_to_cpu(&key, disk_key);
2337
2338 len = sizeof(*disk_key);
2339
2340 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2341 chunk = (struct btrfs_chunk *)(ptr + len);
2342 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2343 len += btrfs_chunk_item_size(num_stripes);
2344 } else {
2345 ret = -EIO;
2346 break;
2347 }
2348 if (key.objectid == chunk_objectid &&
2349 key.offset == chunk_offset) {
2350 memmove(ptr, ptr + len, array_size - (cur + len));
2351 array_size -= len;
2352 btrfs_set_super_sys_array_size(super_copy, array_size);
2353 } else {
2354 ptr += len;
2355 cur += len;
2356 }
2357 }
2358 return ret;
2359}
2360
Christoph Hellwigb2950862008-12-02 09:54:17 -05002361static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002362 u64 chunk_tree, u64 chunk_objectid,
2363 u64 chunk_offset)
2364{
2365 struct extent_map_tree *em_tree;
2366 struct btrfs_root *extent_root;
2367 struct btrfs_trans_handle *trans;
2368 struct extent_map *em;
2369 struct map_lookup *map;
2370 int ret;
2371 int i;
2372
2373 root = root->fs_info->chunk_root;
2374 extent_root = root->fs_info->extent_root;
2375 em_tree = &root->fs_info->mapping_tree.map_tree;
2376
Josef Bacikba1bf482009-09-11 16:11:19 -04002377 ret = btrfs_can_relocate(extent_root, chunk_offset);
2378 if (ret)
2379 return -ENOSPC;
2380
Chris Mason8f18cf12008-04-25 16:53:30 -04002381 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002382 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002383 if (ret)
2384 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002385
Yan, Zhenga22285a2010-05-16 10:48:46 -04002386 trans = btrfs_start_transaction(root, 0);
Liu Bo0f788c52013-03-04 16:25:40 +00002387 if (IS_ERR(trans)) {
2388 ret = PTR_ERR(trans);
2389 btrfs_std_error(root->fs_info, ret);
2390 return ret;
2391 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002392
Chris Mason7d9eb122008-07-08 14:19:17 -04002393 lock_chunks(root);
2394
Chris Mason8f18cf12008-04-25 16:53:30 -04002395 /*
2396 * step two, delete the device extents and the
2397 * chunk tree entries
2398 */
Chris Mason890871b2009-09-02 16:24:52 -04002399 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002400 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002401 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002402
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002403 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002404 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002405 map = (struct map_lookup *)em->bdev;
2406
2407 for (i = 0; i < map->num_stripes; i++) {
2408 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2409 map->stripes[i].physical);
2410 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002411
Chris Masondfe25022008-05-13 13:46:40 -04002412 if (map->stripes[i].dev) {
2413 ret = btrfs_update_device(trans, map->stripes[i].dev);
2414 BUG_ON(ret);
2415 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002416 }
2417 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2418 chunk_offset);
2419
2420 BUG_ON(ret);
2421
liubo1abe9b82011-03-24 11:18:59 +00002422 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2423
Chris Mason8f18cf12008-04-25 16:53:30 -04002424 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2425 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2426 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002427 }
2428
Zheng Yan1a40e232008-09-26 10:09:34 -04002429 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2430 BUG_ON(ret);
2431
Chris Mason890871b2009-09-02 16:24:52 -04002432 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002433 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002434 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002435
Chris Mason8f18cf12008-04-25 16:53:30 -04002436 kfree(map);
2437 em->bdev = NULL;
2438
2439 /* once for the tree */
2440 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002441 /* once for us */
2442 free_extent_map(em);
2443
Chris Mason7d9eb122008-07-08 14:19:17 -04002444 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002445 btrfs_end_transaction(trans, root);
2446 return 0;
2447}
2448
Yan Zheng2b820322008-11-17 21:11:30 -05002449static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2450{
2451 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2452 struct btrfs_path *path;
2453 struct extent_buffer *leaf;
2454 struct btrfs_chunk *chunk;
2455 struct btrfs_key key;
2456 struct btrfs_key found_key;
2457 u64 chunk_tree = chunk_root->root_key.objectid;
2458 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002459 bool retried = false;
2460 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002461 int ret;
2462
2463 path = btrfs_alloc_path();
2464 if (!path)
2465 return -ENOMEM;
2466
Josef Bacikba1bf482009-09-11 16:11:19 -04002467again:
Yan Zheng2b820322008-11-17 21:11:30 -05002468 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2469 key.offset = (u64)-1;
2470 key.type = BTRFS_CHUNK_ITEM_KEY;
2471
2472 while (1) {
2473 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2474 if (ret < 0)
2475 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002476 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002477
2478 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2479 key.type);
2480 if (ret < 0)
2481 goto error;
2482 if (ret > 0)
2483 break;
2484
2485 leaf = path->nodes[0];
2486 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2487
2488 chunk = btrfs_item_ptr(leaf, path->slots[0],
2489 struct btrfs_chunk);
2490 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002491 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002492
2493 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2494 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2495 found_key.objectid,
2496 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002497 if (ret == -ENOSPC)
2498 failed++;
2499 else if (ret)
2500 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002501 }
2502
2503 if (found_key.offset == 0)
2504 break;
2505 key.offset = found_key.offset - 1;
2506 }
2507 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002508 if (failed && !retried) {
2509 failed = 0;
2510 retried = true;
2511 goto again;
2512 } else if (failed && retried) {
2513 WARN_ON(1);
2514 ret = -ENOSPC;
2515 }
Yan Zheng2b820322008-11-17 21:11:30 -05002516error:
2517 btrfs_free_path(path);
2518 return ret;
2519}
2520
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002521static int insert_balance_item(struct btrfs_root *root,
2522 struct btrfs_balance_control *bctl)
2523{
2524 struct btrfs_trans_handle *trans;
2525 struct btrfs_balance_item *item;
2526 struct btrfs_disk_balance_args disk_bargs;
2527 struct btrfs_path *path;
2528 struct extent_buffer *leaf;
2529 struct btrfs_key key;
2530 int ret, err;
2531
2532 path = btrfs_alloc_path();
2533 if (!path)
2534 return -ENOMEM;
2535
2536 trans = btrfs_start_transaction(root, 0);
2537 if (IS_ERR(trans)) {
2538 btrfs_free_path(path);
2539 return PTR_ERR(trans);
2540 }
2541
2542 key.objectid = BTRFS_BALANCE_OBJECTID;
2543 key.type = BTRFS_BALANCE_ITEM_KEY;
2544 key.offset = 0;
2545
2546 ret = btrfs_insert_empty_item(trans, root, path, &key,
2547 sizeof(*item));
2548 if (ret)
2549 goto out;
2550
2551 leaf = path->nodes[0];
2552 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2553
2554 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2555
2556 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2557 btrfs_set_balance_data(leaf, item, &disk_bargs);
2558 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2559 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2560 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2561 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2562
2563 btrfs_set_balance_flags(leaf, item, bctl->flags);
2564
2565 btrfs_mark_buffer_dirty(leaf);
2566out:
2567 btrfs_free_path(path);
2568 err = btrfs_commit_transaction(trans, root);
2569 if (err && !ret)
2570 ret = err;
2571 return ret;
2572}
2573
2574static int del_balance_item(struct btrfs_root *root)
2575{
2576 struct btrfs_trans_handle *trans;
2577 struct btrfs_path *path;
2578 struct btrfs_key key;
2579 int ret, err;
2580
2581 path = btrfs_alloc_path();
2582 if (!path)
2583 return -ENOMEM;
2584
2585 trans = btrfs_start_transaction(root, 0);
2586 if (IS_ERR(trans)) {
2587 btrfs_free_path(path);
2588 return PTR_ERR(trans);
2589 }
2590
2591 key.objectid = BTRFS_BALANCE_OBJECTID;
2592 key.type = BTRFS_BALANCE_ITEM_KEY;
2593 key.offset = 0;
2594
2595 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2596 if (ret < 0)
2597 goto out;
2598 if (ret > 0) {
2599 ret = -ENOENT;
2600 goto out;
2601 }
2602
2603 ret = btrfs_del_item(trans, root, path);
2604out:
2605 btrfs_free_path(path);
2606 err = btrfs_commit_transaction(trans, root);
2607 if (err && !ret)
2608 ret = err;
2609 return ret;
2610}
2611
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002612/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002613 * This is a heuristic used to reduce the number of chunks balanced on
2614 * resume after balance was interrupted.
2615 */
2616static void update_balance_args(struct btrfs_balance_control *bctl)
2617{
2618 /*
2619 * Turn on soft mode for chunk types that were being converted.
2620 */
2621 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2622 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2623 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2624 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2625 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2626 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2627
2628 /*
2629 * Turn on usage filter if is not already used. The idea is
2630 * that chunks that we have already balanced should be
2631 * reasonably full. Don't do it for chunks that are being
2632 * converted - that will keep us from relocating unconverted
2633 * (albeit full) chunks.
2634 */
2635 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2636 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2637 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2638 bctl->data.usage = 90;
2639 }
2640 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2641 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2642 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2643 bctl->sys.usage = 90;
2644 }
2645 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2646 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2647 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2648 bctl->meta.usage = 90;
2649 }
2650}
2651
2652/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002653 * Should be called with both balance and volume mutexes held to
2654 * serialize other volume operations (add_dev/rm_dev/resize) with
2655 * restriper. Same goes for unset_balance_control.
2656 */
2657static void set_balance_control(struct btrfs_balance_control *bctl)
2658{
2659 struct btrfs_fs_info *fs_info = bctl->fs_info;
2660
2661 BUG_ON(fs_info->balance_ctl);
2662
2663 spin_lock(&fs_info->balance_lock);
2664 fs_info->balance_ctl = bctl;
2665 spin_unlock(&fs_info->balance_lock);
2666}
2667
2668static void unset_balance_control(struct btrfs_fs_info *fs_info)
2669{
2670 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2671
2672 BUG_ON(!fs_info->balance_ctl);
2673
2674 spin_lock(&fs_info->balance_lock);
2675 fs_info->balance_ctl = NULL;
2676 spin_unlock(&fs_info->balance_lock);
2677
2678 kfree(bctl);
2679}
2680
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002681/*
2682 * Balance filters. Return 1 if chunk should be filtered out
2683 * (should not be balanced).
2684 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002685static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002686 struct btrfs_balance_args *bargs)
2687{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002688 chunk_type = chunk_to_extended(chunk_type) &
2689 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002690
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002691 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002692 return 0;
2693
2694 return 1;
2695}
2696
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002697static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2698 struct btrfs_balance_args *bargs)
2699{
2700 struct btrfs_block_group_cache *cache;
2701 u64 chunk_used, user_thresh;
2702 int ret = 1;
2703
2704 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2705 chunk_used = btrfs_block_group_used(&cache->item);
2706
Ilya Dryomova105bb82013-01-21 15:15:56 +02002707 if (bargs->usage == 0)
Ilya Dryomov3e39cea2013-02-12 16:28:59 +00002708 user_thresh = 1;
Ilya Dryomova105bb82013-01-21 15:15:56 +02002709 else if (bargs->usage > 100)
2710 user_thresh = cache->key.offset;
2711 else
2712 user_thresh = div_factor_fine(cache->key.offset,
2713 bargs->usage);
2714
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002715 if (chunk_used < user_thresh)
2716 ret = 0;
2717
2718 btrfs_put_block_group(cache);
2719 return ret;
2720}
2721
Ilya Dryomov409d4042012-01-16 22:04:47 +02002722static int chunk_devid_filter(struct extent_buffer *leaf,
2723 struct btrfs_chunk *chunk,
2724 struct btrfs_balance_args *bargs)
2725{
2726 struct btrfs_stripe *stripe;
2727 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2728 int i;
2729
2730 for (i = 0; i < num_stripes; i++) {
2731 stripe = btrfs_stripe_nr(chunk, i);
2732 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2733 return 0;
2734 }
2735
2736 return 1;
2737}
2738
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002739/* [pstart, pend) */
2740static int chunk_drange_filter(struct extent_buffer *leaf,
2741 struct btrfs_chunk *chunk,
2742 u64 chunk_offset,
2743 struct btrfs_balance_args *bargs)
2744{
2745 struct btrfs_stripe *stripe;
2746 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2747 u64 stripe_offset;
2748 u64 stripe_length;
2749 int factor;
2750 int i;
2751
2752 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2753 return 0;
2754
2755 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
David Woodhouse53b381b2013-01-29 18:40:14 -05002756 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)) {
2757 factor = num_stripes / 2;
2758 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID5) {
2759 factor = num_stripes - 1;
2760 } else if (btrfs_chunk_type(leaf, chunk) & BTRFS_BLOCK_GROUP_RAID6) {
2761 factor = num_stripes - 2;
2762 } else {
2763 factor = num_stripes;
2764 }
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002765
2766 for (i = 0; i < num_stripes; i++) {
2767 stripe = btrfs_stripe_nr(chunk, i);
2768 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2769 continue;
2770
2771 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2772 stripe_length = btrfs_chunk_length(leaf, chunk);
2773 do_div(stripe_length, factor);
2774
2775 if (stripe_offset < bargs->pend &&
2776 stripe_offset + stripe_length > bargs->pstart)
2777 return 0;
2778 }
2779
2780 return 1;
2781}
2782
Ilya Dryomovea671762012-01-16 22:04:48 +02002783/* [vstart, vend) */
2784static int chunk_vrange_filter(struct extent_buffer *leaf,
2785 struct btrfs_chunk *chunk,
2786 u64 chunk_offset,
2787 struct btrfs_balance_args *bargs)
2788{
2789 if (chunk_offset < bargs->vend &&
2790 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2791 /* at least part of the chunk is inside this vrange */
2792 return 0;
2793
2794 return 1;
2795}
2796
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002797static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002798 struct btrfs_balance_args *bargs)
2799{
2800 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2801 return 0;
2802
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002803 chunk_type = chunk_to_extended(chunk_type) &
2804 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002805
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002806 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002807 return 1;
2808
2809 return 0;
2810}
2811
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002812static int should_balance_chunk(struct btrfs_root *root,
2813 struct extent_buffer *leaf,
2814 struct btrfs_chunk *chunk, u64 chunk_offset)
2815{
2816 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2817 struct btrfs_balance_args *bargs = NULL;
2818 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2819
2820 /* type filter */
2821 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2822 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2823 return 0;
2824 }
2825
2826 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2827 bargs = &bctl->data;
2828 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2829 bargs = &bctl->sys;
2830 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2831 bargs = &bctl->meta;
2832
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002833 /* profiles filter */
2834 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2835 chunk_profiles_filter(chunk_type, bargs)) {
2836 return 0;
2837 }
2838
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002839 /* usage filter */
2840 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2841 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2842 return 0;
2843 }
2844
Ilya Dryomov409d4042012-01-16 22:04:47 +02002845 /* devid filter */
2846 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2847 chunk_devid_filter(leaf, chunk, bargs)) {
2848 return 0;
2849 }
2850
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002851 /* drange filter, makes sense only with devid filter */
2852 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2853 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2854 return 0;
2855 }
2856
Ilya Dryomovea671762012-01-16 22:04:48 +02002857 /* vrange filter */
2858 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2859 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2860 return 0;
2861 }
2862
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002863 /* soft profile changing mode */
2864 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2865 chunk_soft_convert_filter(chunk_type, bargs)) {
2866 return 0;
2867 }
2868
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002869 return 1;
2870}
2871
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002872static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002873{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002874 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002875 struct btrfs_root *chunk_root = fs_info->chunk_root;
2876 struct btrfs_root *dev_root = fs_info->dev_root;
2877 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002878 struct btrfs_device *device;
2879 u64 old_size;
2880 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002881 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002882 struct btrfs_path *path;
2883 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002884 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002885 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002886 struct extent_buffer *leaf;
2887 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002888 int ret;
2889 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002890 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002891
Chris Masonec44a352008-04-28 15:29:52 -04002892 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002893 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002894 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002895 old_size = device->total_bytes;
2896 size_to_free = div_factor(old_size, 1);
2897 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002898 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002899 device->total_bytes - device->bytes_used > size_to_free ||
2900 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002901 continue;
2902
2903 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002904 if (ret == -ENOSPC)
2905 break;
Chris Masonec44a352008-04-28 15:29:52 -04002906 BUG_ON(ret);
2907
Yan, Zhenga22285a2010-05-16 10:48:46 -04002908 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002909 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002910
2911 ret = btrfs_grow_device(trans, device, old_size);
2912 BUG_ON(ret);
2913
2914 btrfs_end_transaction(trans, dev_root);
2915 }
2916
2917 /* step two, relocate all the chunks */
2918 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002919 if (!path) {
2920 ret = -ENOMEM;
2921 goto error;
2922 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002923
2924 /* zero out stat counters */
2925 spin_lock(&fs_info->balance_lock);
2926 memset(&bctl->stat, 0, sizeof(bctl->stat));
2927 spin_unlock(&fs_info->balance_lock);
2928again:
Chris Masonec44a352008-04-28 15:29:52 -04002929 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2930 key.offset = (u64)-1;
2931 key.type = BTRFS_CHUNK_ITEM_KEY;
2932
Chris Masond3977122009-01-05 21:25:51 -05002933 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002934 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002935 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002936 ret = -ECANCELED;
2937 goto error;
2938 }
2939
Chris Masonec44a352008-04-28 15:29:52 -04002940 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2941 if (ret < 0)
2942 goto error;
2943
2944 /*
2945 * this shouldn't happen, it means the last relocate
2946 * failed
2947 */
2948 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002949 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002950
2951 ret = btrfs_previous_item(chunk_root, path, 0,
2952 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002953 if (ret) {
2954 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002955 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002956 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002957
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002958 leaf = path->nodes[0];
2959 slot = path->slots[0];
2960 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2961
Chris Masonec44a352008-04-28 15:29:52 -04002962 if (found_key.objectid != key.objectid)
2963 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002964
Chris Masonec44a352008-04-28 15:29:52 -04002965 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002966 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002967 break;
2968
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002969 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2970
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002971 if (!counting) {
2972 spin_lock(&fs_info->balance_lock);
2973 bctl->stat.considered++;
2974 spin_unlock(&fs_info->balance_lock);
2975 }
2976
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002977 ret = should_balance_chunk(chunk_root, leaf, chunk,
2978 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002979 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002980 if (!ret)
2981 goto loop;
2982
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002983 if (counting) {
2984 spin_lock(&fs_info->balance_lock);
2985 bctl->stat.expected++;
2986 spin_unlock(&fs_info->balance_lock);
2987 goto loop;
2988 }
2989
Chris Masonec44a352008-04-28 15:29:52 -04002990 ret = btrfs_relocate_chunk(chunk_root,
2991 chunk_root->root_key.objectid,
2992 found_key.objectid,
2993 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002994 if (ret && ret != -ENOSPC)
2995 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002996 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002997 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002998 } else {
2999 spin_lock(&fs_info->balance_lock);
3000 bctl->stat.completed++;
3001 spin_unlock(&fs_info->balance_lock);
3002 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003003loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04003004 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04003005 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003006
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003007 if (counting) {
3008 btrfs_release_path(path);
3009 counting = false;
3010 goto again;
3011 }
Chris Masonec44a352008-04-28 15:29:52 -04003012error:
3013 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003014 if (enospc_errors) {
3015 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
3016 enospc_errors);
3017 if (!ret)
3018 ret = -ENOSPC;
3019 }
3020
Chris Masonec44a352008-04-28 15:29:52 -04003021 return ret;
3022}
3023
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003024/**
3025 * alloc_profile_is_valid - see if a given profile is valid and reduced
3026 * @flags: profile to validate
3027 * @extended: if true @flags is treated as an extended profile
3028 */
3029static int alloc_profile_is_valid(u64 flags, int extended)
3030{
3031 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
3032 BTRFS_BLOCK_GROUP_PROFILE_MASK);
3033
3034 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
3035
3036 /* 1) check that all other bits are zeroed */
3037 if (flags & ~mask)
3038 return 0;
3039
3040 /* 2) see if profile is reduced */
3041 if (flags == 0)
3042 return !extended; /* "0" is valid for usual profiles */
3043
3044 /* true if exactly one bit set */
3045 return (flags & (flags - 1)) == 0;
3046}
3047
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003048static inline int balance_need_close(struct btrfs_fs_info *fs_info)
3049{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003050 /* cancel requested || normal exit path */
3051 return atomic_read(&fs_info->balance_cancel_req) ||
3052 (atomic_read(&fs_info->balance_pause_req) == 0 &&
3053 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003054}
3055
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003056static void __cancel_balance(struct btrfs_fs_info *fs_info)
3057{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003058 int ret;
3059
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003060 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003061 ret = del_balance_item(fs_info->tree_root);
Liu Bo0f788c52013-03-04 16:25:40 +00003062 if (ret)
3063 btrfs_std_error(fs_info, ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003064
3065 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003066}
3067
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003068/*
3069 * Should be called with both balance and volume mutexes held
3070 */
3071int btrfs_balance(struct btrfs_balance_control *bctl,
3072 struct btrfs_ioctl_balance_args *bargs)
3073{
3074 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003075 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03003076 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003077 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003078 u64 num_devices;
Miao Xiede98ced2013-01-29 10:13:12 +00003079 unsigned seq;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003080
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003081 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003082 atomic_read(&fs_info->balance_pause_req) ||
3083 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003084 ret = -EINVAL;
3085 goto out;
3086 }
3087
Ilya Dryomove4837f82012-03-27 17:09:17 +03003088 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
3089 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
3090 mixed = 1;
3091
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003092 /*
3093 * In case of mixed groups both data and meta should be picked,
3094 * and identical options should be given for both of them.
3095 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003096 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3097 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003098 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3099 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3100 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3101 printk(KERN_ERR "btrfs: with mixed groups data and "
3102 "metadata balance options must be the same\n");
3103 ret = -EINVAL;
3104 goto out;
3105 }
3106 }
3107
Stefan Behrens8dabb742012-11-06 13:15:27 +01003108 num_devices = fs_info->fs_devices->num_devices;
3109 btrfs_dev_replace_lock(&fs_info->dev_replace);
3110 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3111 BUG_ON(num_devices < 1);
3112 num_devices--;
3113 }
3114 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003115 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003116 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003117 allowed |= BTRFS_BLOCK_GROUP_DUP;
Andreas Philipp8250dab2013-05-11 11:13:03 +00003118 else if (num_devices > 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003119 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
Andreas Philipp8250dab2013-05-11 11:13:03 +00003120 if (num_devices > 2)
3121 allowed |= BTRFS_BLOCK_GROUP_RAID5;
3122 if (num_devices > 3)
3123 allowed |= (BTRFS_BLOCK_GROUP_RAID10 |
3124 BTRFS_BLOCK_GROUP_RAID6);
Ilya Dryomov6728b192012-03-27 17:09:17 +03003125 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3126 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3127 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003128 printk(KERN_ERR "btrfs: unable to start balance with target "
3129 "data profile %llu\n",
3130 (unsigned long long)bctl->data.target);
3131 ret = -EINVAL;
3132 goto out;
3133 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003134 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3135 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3136 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003137 printk(KERN_ERR "btrfs: unable to start balance with target "
3138 "metadata profile %llu\n",
3139 (unsigned long long)bctl->meta.target);
3140 ret = -EINVAL;
3141 goto out;
3142 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003143 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3144 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3145 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003146 printk(KERN_ERR "btrfs: unable to start balance with target "
3147 "system profile %llu\n",
3148 (unsigned long long)bctl->sys.target);
3149 ret = -EINVAL;
3150 goto out;
3151 }
3152
Ilya Dryomove4837f82012-03-27 17:09:17 +03003153 /* allow dup'ed data chunks only in mixed mode */
3154 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003155 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003156 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3157 ret = -EINVAL;
3158 goto out;
3159 }
3160
3161 /* allow to reduce meta or sys integrity only if force set */
3162 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05003163 BTRFS_BLOCK_GROUP_RAID10 |
3164 BTRFS_BLOCK_GROUP_RAID5 |
3165 BTRFS_BLOCK_GROUP_RAID6;
Miao Xiede98ced2013-01-29 10:13:12 +00003166 do {
3167 seq = read_seqbegin(&fs_info->profiles_lock);
3168
3169 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3170 (fs_info->avail_system_alloc_bits & allowed) &&
3171 !(bctl->sys.target & allowed)) ||
3172 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3173 (fs_info->avail_metadata_alloc_bits & allowed) &&
3174 !(bctl->meta.target & allowed))) {
3175 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3176 printk(KERN_INFO "btrfs: force reducing metadata "
3177 "integrity\n");
3178 } else {
3179 printk(KERN_ERR "btrfs: balance will reduce metadata "
3180 "integrity, use force if you want this\n");
3181 ret = -EINVAL;
3182 goto out;
3183 }
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003184 }
Miao Xiede98ced2013-01-29 10:13:12 +00003185 } while (read_seqretry(&fs_info->profiles_lock, seq));
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003186
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003187 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3188 int num_tolerated_disk_barrier_failures;
3189 u64 target = bctl->sys.target;
3190
3191 num_tolerated_disk_barrier_failures =
3192 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3193 if (num_tolerated_disk_barrier_failures > 0 &&
3194 (target &
3195 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3196 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3197 num_tolerated_disk_barrier_failures = 0;
3198 else if (num_tolerated_disk_barrier_failures > 1 &&
3199 (target &
3200 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3201 num_tolerated_disk_barrier_failures = 1;
3202
3203 fs_info->num_tolerated_disk_barrier_failures =
3204 num_tolerated_disk_barrier_failures;
3205 }
3206
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003207 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003208 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003209 goto out;
3210
Ilya Dryomov59641012012-01-16 22:04:48 +02003211 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3212 BUG_ON(ret == -EEXIST);
3213 set_balance_control(bctl);
3214 } else {
3215 BUG_ON(ret != -EEXIST);
3216 spin_lock(&fs_info->balance_lock);
3217 update_balance_args(bctl);
3218 spin_unlock(&fs_info->balance_lock);
3219 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003220
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003221 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003222 mutex_unlock(&fs_info->balance_mutex);
3223
3224 ret = __btrfs_balance(fs_info);
3225
3226 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003227 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003228
Ilya Dryomovbf023ec2013-02-12 16:27:46 +00003229 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3230 fs_info->num_tolerated_disk_barrier_failures =
3231 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3232 }
3233
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003234 if (bargs) {
3235 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003236 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003237 }
3238
Ilya Dryomov3a01aa72013-03-06 01:57:55 -07003239 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3240 balance_need_close(fs_info)) {
3241 __cancel_balance(fs_info);
3242 }
3243
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003244 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003245
3246 return ret;
3247out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003248 if (bctl->flags & BTRFS_BALANCE_RESUME)
3249 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003250 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003251 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003252 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3253 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003254 return ret;
3255}
3256
3257static int balance_kthread(void *data)
3258{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003259 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003260 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003261
3262 mutex_lock(&fs_info->volume_mutex);
3263 mutex_lock(&fs_info->balance_mutex);
3264
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003265 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003266 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003267 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003268 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003269
3270 mutex_unlock(&fs_info->balance_mutex);
3271 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003272
Ilya Dryomov59641012012-01-16 22:04:48 +02003273 return ret;
3274}
3275
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003276int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3277{
3278 struct task_struct *tsk;
3279
3280 spin_lock(&fs_info->balance_lock);
3281 if (!fs_info->balance_ctl) {
3282 spin_unlock(&fs_info->balance_lock);
3283 return 0;
3284 }
3285 spin_unlock(&fs_info->balance_lock);
3286
3287 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3288 printk(KERN_INFO "btrfs: force skipping balance\n");
3289 return 0;
3290 }
3291
3292 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
Thomas Meyer97a184f2013-06-01 09:45:35 +00003293 return PTR_RET(tsk);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003294}
3295
Ilya Dryomov68310a52012-06-22 12:24:12 -06003296int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003297{
Ilya Dryomov59641012012-01-16 22:04:48 +02003298 struct btrfs_balance_control *bctl;
3299 struct btrfs_balance_item *item;
3300 struct btrfs_disk_balance_args disk_bargs;
3301 struct btrfs_path *path;
3302 struct extent_buffer *leaf;
3303 struct btrfs_key key;
3304 int ret;
3305
3306 path = btrfs_alloc_path();
3307 if (!path)
3308 return -ENOMEM;
3309
Ilya Dryomov68310a52012-06-22 12:24:12 -06003310 key.objectid = BTRFS_BALANCE_OBJECTID;
3311 key.type = BTRFS_BALANCE_ITEM_KEY;
3312 key.offset = 0;
3313
3314 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3315 if (ret < 0)
3316 goto out;
3317 if (ret > 0) { /* ret = -ENOENT; */
3318 ret = 0;
3319 goto out;
3320 }
3321
Ilya Dryomov59641012012-01-16 22:04:48 +02003322 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3323 if (!bctl) {
3324 ret = -ENOMEM;
3325 goto out;
3326 }
3327
Ilya Dryomov59641012012-01-16 22:04:48 +02003328 leaf = path->nodes[0];
3329 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3330
Ilya Dryomov68310a52012-06-22 12:24:12 -06003331 bctl->fs_info = fs_info;
3332 bctl->flags = btrfs_balance_flags(leaf, item);
3333 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003334
3335 btrfs_balance_data(leaf, item, &disk_bargs);
3336 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3337 btrfs_balance_meta(leaf, item, &disk_bargs);
3338 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3339 btrfs_balance_sys(leaf, item, &disk_bargs);
3340 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3341
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003342 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3343
Ilya Dryomov68310a52012-06-22 12:24:12 -06003344 mutex_lock(&fs_info->volume_mutex);
3345 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003346
Ilya Dryomov68310a52012-06-22 12:24:12 -06003347 set_balance_control(bctl);
3348
3349 mutex_unlock(&fs_info->balance_mutex);
3350 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003351out:
3352 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003353 return ret;
3354}
3355
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003356int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3357{
3358 int ret = 0;
3359
3360 mutex_lock(&fs_info->balance_mutex);
3361 if (!fs_info->balance_ctl) {
3362 mutex_unlock(&fs_info->balance_mutex);
3363 return -ENOTCONN;
3364 }
3365
3366 if (atomic_read(&fs_info->balance_running)) {
3367 atomic_inc(&fs_info->balance_pause_req);
3368 mutex_unlock(&fs_info->balance_mutex);
3369
3370 wait_event(fs_info->balance_wait_q,
3371 atomic_read(&fs_info->balance_running) == 0);
3372
3373 mutex_lock(&fs_info->balance_mutex);
3374 /* we are good with balance_ctl ripped off from under us */
3375 BUG_ON(atomic_read(&fs_info->balance_running));
3376 atomic_dec(&fs_info->balance_pause_req);
3377 } else {
3378 ret = -ENOTCONN;
3379 }
3380
3381 mutex_unlock(&fs_info->balance_mutex);
3382 return ret;
3383}
3384
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003385int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3386{
3387 mutex_lock(&fs_info->balance_mutex);
3388 if (!fs_info->balance_ctl) {
3389 mutex_unlock(&fs_info->balance_mutex);
3390 return -ENOTCONN;
3391 }
3392
3393 atomic_inc(&fs_info->balance_cancel_req);
3394 /*
3395 * if we are running just wait and return, balance item is
3396 * deleted in btrfs_balance in this case
3397 */
3398 if (atomic_read(&fs_info->balance_running)) {
3399 mutex_unlock(&fs_info->balance_mutex);
3400 wait_event(fs_info->balance_wait_q,
3401 atomic_read(&fs_info->balance_running) == 0);
3402 mutex_lock(&fs_info->balance_mutex);
3403 } else {
3404 /* __cancel_balance needs volume_mutex */
3405 mutex_unlock(&fs_info->balance_mutex);
3406 mutex_lock(&fs_info->volume_mutex);
3407 mutex_lock(&fs_info->balance_mutex);
3408
3409 if (fs_info->balance_ctl)
3410 __cancel_balance(fs_info);
3411
3412 mutex_unlock(&fs_info->volume_mutex);
3413 }
3414
3415 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3416 atomic_dec(&fs_info->balance_cancel_req);
3417 mutex_unlock(&fs_info->balance_mutex);
3418 return 0;
3419}
3420
Stefan Behrens803b2f52013-08-15 17:11:21 +02003421static int btrfs_uuid_scan_kthread(void *data)
3422{
3423 struct btrfs_fs_info *fs_info = data;
3424 struct btrfs_root *root = fs_info->tree_root;
3425 struct btrfs_key key;
3426 struct btrfs_key max_key;
3427 struct btrfs_path *path = NULL;
3428 int ret = 0;
3429 struct extent_buffer *eb;
3430 int slot;
3431 struct btrfs_root_item root_item;
3432 u32 item_size;
3433 struct btrfs_trans_handle *trans;
3434
3435 path = btrfs_alloc_path();
3436 if (!path) {
3437 ret = -ENOMEM;
3438 goto out;
3439 }
3440
3441 key.objectid = 0;
3442 key.type = BTRFS_ROOT_ITEM_KEY;
3443 key.offset = 0;
3444
3445 max_key.objectid = (u64)-1;
3446 max_key.type = BTRFS_ROOT_ITEM_KEY;
3447 max_key.offset = (u64)-1;
3448
3449 path->keep_locks = 1;
3450
3451 while (1) {
3452 ret = btrfs_search_forward(root, &key, &max_key, path, 0);
3453 if (ret) {
3454 if (ret > 0)
3455 ret = 0;
3456 break;
3457 }
3458
3459 if (key.type != BTRFS_ROOT_ITEM_KEY ||
3460 (key.objectid < BTRFS_FIRST_FREE_OBJECTID &&
3461 key.objectid != BTRFS_FS_TREE_OBJECTID) ||
3462 key.objectid > BTRFS_LAST_FREE_OBJECTID)
3463 goto skip;
3464
3465 eb = path->nodes[0];
3466 slot = path->slots[0];
3467 item_size = btrfs_item_size_nr(eb, slot);
3468 if (item_size < sizeof(root_item))
3469 goto skip;
3470
3471 trans = NULL;
3472 read_extent_buffer(eb, &root_item,
3473 btrfs_item_ptr_offset(eb, slot),
3474 (int)sizeof(root_item));
3475 if (btrfs_root_refs(&root_item) == 0)
3476 goto skip;
3477 if (!btrfs_is_empty_uuid(root_item.uuid)) {
3478 /*
3479 * 1 - subvol uuid item
3480 * 1 - received_subvol uuid item
3481 */
3482 trans = btrfs_start_transaction(fs_info->uuid_root, 2);
3483 if (IS_ERR(trans)) {
3484 ret = PTR_ERR(trans);
3485 break;
3486 }
3487 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3488 root_item.uuid,
3489 BTRFS_UUID_KEY_SUBVOL,
3490 key.objectid);
3491 if (ret < 0) {
3492 pr_warn("btrfs: uuid_tree_add failed %d\n",
3493 ret);
3494 btrfs_end_transaction(trans,
3495 fs_info->uuid_root);
3496 break;
3497 }
3498 }
3499
3500 if (!btrfs_is_empty_uuid(root_item.received_uuid)) {
3501 if (!trans) {
3502 /* 1 - received_subvol uuid item */
3503 trans = btrfs_start_transaction(
3504 fs_info->uuid_root, 1);
3505 if (IS_ERR(trans)) {
3506 ret = PTR_ERR(trans);
3507 break;
3508 }
3509 }
3510 ret = btrfs_uuid_tree_add(trans, fs_info->uuid_root,
3511 root_item.received_uuid,
3512 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3513 key.objectid);
3514 if (ret < 0) {
3515 pr_warn("btrfs: uuid_tree_add failed %d\n",
3516 ret);
3517 btrfs_end_transaction(trans,
3518 fs_info->uuid_root);
3519 break;
3520 }
3521 }
3522
3523 if (trans) {
3524 ret = btrfs_end_transaction(trans, fs_info->uuid_root);
3525 if (ret)
3526 break;
3527 }
3528
3529skip:
3530 btrfs_release_path(path);
3531 if (key.offset < (u64)-1) {
3532 key.offset++;
3533 } else if (key.type < BTRFS_ROOT_ITEM_KEY) {
3534 key.offset = 0;
3535 key.type = BTRFS_ROOT_ITEM_KEY;
3536 } else if (key.objectid < (u64)-1) {
3537 key.offset = 0;
3538 key.type = BTRFS_ROOT_ITEM_KEY;
3539 key.objectid++;
3540 } else {
3541 break;
3542 }
3543 cond_resched();
3544 }
3545
3546out:
3547 btrfs_free_path(path);
3548 if (ret)
3549 pr_warn("btrfs: btrfs_uuid_scan_kthread failed %d\n", ret);
Stefan Behrens70f80172013-08-15 17:11:23 +02003550 else
3551 fs_info->update_uuid_tree_gen = 1;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003552 up(&fs_info->uuid_tree_rescan_sem);
3553 return 0;
3554}
3555
Stefan Behrens70f80172013-08-15 17:11:23 +02003556/*
3557 * Callback for btrfs_uuid_tree_iterate().
3558 * returns:
3559 * 0 check succeeded, the entry is not outdated.
3560 * < 0 if an error occured.
3561 * > 0 if the check failed, which means the caller shall remove the entry.
3562 */
3563static int btrfs_check_uuid_tree_entry(struct btrfs_fs_info *fs_info,
3564 u8 *uuid, u8 type, u64 subid)
3565{
3566 struct btrfs_key key;
3567 int ret = 0;
3568 struct btrfs_root *subvol_root;
3569
3570 if (type != BTRFS_UUID_KEY_SUBVOL &&
3571 type != BTRFS_UUID_KEY_RECEIVED_SUBVOL)
3572 goto out;
3573
3574 key.objectid = subid;
3575 key.type = BTRFS_ROOT_ITEM_KEY;
3576 key.offset = (u64)-1;
3577 subvol_root = btrfs_read_fs_root_no_name(fs_info, &key);
3578 if (IS_ERR(subvol_root)) {
3579 ret = PTR_ERR(subvol_root);
3580 if (ret == -ENOENT)
3581 ret = 1;
3582 goto out;
3583 }
3584
3585 switch (type) {
3586 case BTRFS_UUID_KEY_SUBVOL:
3587 if (memcmp(uuid, subvol_root->root_item.uuid, BTRFS_UUID_SIZE))
3588 ret = 1;
3589 break;
3590 case BTRFS_UUID_KEY_RECEIVED_SUBVOL:
3591 if (memcmp(uuid, subvol_root->root_item.received_uuid,
3592 BTRFS_UUID_SIZE))
3593 ret = 1;
3594 break;
3595 }
3596
3597out:
3598 return ret;
3599}
3600
3601static int btrfs_uuid_rescan_kthread(void *data)
3602{
3603 struct btrfs_fs_info *fs_info = (struct btrfs_fs_info *)data;
3604 int ret;
3605
3606 /*
3607 * 1st step is to iterate through the existing UUID tree and
3608 * to delete all entries that contain outdated data.
3609 * 2nd step is to add all missing entries to the UUID tree.
3610 */
3611 ret = btrfs_uuid_tree_iterate(fs_info, btrfs_check_uuid_tree_entry);
3612 if (ret < 0) {
3613 pr_warn("btrfs: iterating uuid_tree failed %d\n", ret);
3614 up(&fs_info->uuid_tree_rescan_sem);
3615 return ret;
3616 }
3617 return btrfs_uuid_scan_kthread(data);
3618}
3619
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003620int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info)
3621{
3622 struct btrfs_trans_handle *trans;
3623 struct btrfs_root *tree_root = fs_info->tree_root;
3624 struct btrfs_root *uuid_root;
Stefan Behrens803b2f52013-08-15 17:11:21 +02003625 struct task_struct *task;
3626 int ret;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003627
3628 /*
3629 * 1 - root node
3630 * 1 - root item
3631 */
3632 trans = btrfs_start_transaction(tree_root, 2);
3633 if (IS_ERR(trans))
3634 return PTR_ERR(trans);
3635
3636 uuid_root = btrfs_create_tree(trans, fs_info,
3637 BTRFS_UUID_TREE_OBJECTID);
3638 if (IS_ERR(uuid_root)) {
3639 btrfs_abort_transaction(trans, tree_root,
3640 PTR_ERR(uuid_root));
3641 return PTR_ERR(uuid_root);
3642 }
3643
3644 fs_info->uuid_root = uuid_root;
3645
Stefan Behrens803b2f52013-08-15 17:11:21 +02003646 ret = btrfs_commit_transaction(trans, tree_root);
3647 if (ret)
3648 return ret;
3649
3650 down(&fs_info->uuid_tree_rescan_sem);
3651 task = kthread_run(btrfs_uuid_scan_kthread, fs_info, "btrfs-uuid");
3652 if (IS_ERR(task)) {
Stefan Behrens70f80172013-08-15 17:11:23 +02003653 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
Stefan Behrens803b2f52013-08-15 17:11:21 +02003654 pr_warn("btrfs: failed to start uuid_scan task\n");
3655 up(&fs_info->uuid_tree_rescan_sem);
3656 return PTR_ERR(task);
3657 }
3658
3659 return 0;
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02003660}
Stefan Behrens803b2f52013-08-15 17:11:21 +02003661
Stefan Behrens70f80172013-08-15 17:11:23 +02003662int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
3663{
3664 struct task_struct *task;
3665
3666 down(&fs_info->uuid_tree_rescan_sem);
3667 task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
3668 if (IS_ERR(task)) {
3669 /* fs_info->update_uuid_tree_gen remains 0 in all error case */
3670 pr_warn("btrfs: failed to start uuid_rescan task\n");
3671 up(&fs_info->uuid_tree_rescan_sem);
3672 return PTR_ERR(task);
3673 }
3674
3675 return 0;
3676}
3677
Chris Mason8f18cf12008-04-25 16:53:30 -04003678/*
3679 * shrinking a device means finding all of the device extents past
3680 * the new size, and then following the back refs to the chunks.
3681 * The chunk relocation code actually frees the device extent
3682 */
3683int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3684{
3685 struct btrfs_trans_handle *trans;
3686 struct btrfs_root *root = device->dev_root;
3687 struct btrfs_dev_extent *dev_extent = NULL;
3688 struct btrfs_path *path;
3689 u64 length;
3690 u64 chunk_tree;
3691 u64 chunk_objectid;
3692 u64 chunk_offset;
3693 int ret;
3694 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003695 int failed = 0;
3696 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003697 struct extent_buffer *l;
3698 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003699 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003700 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003701 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003702 u64 diff = device->total_bytes - new_size;
3703
Stefan Behrens63a212a2012-11-05 18:29:28 +01003704 if (device->is_tgtdev_for_dev_replace)
3705 return -EINVAL;
3706
Chris Mason8f18cf12008-04-25 16:53:30 -04003707 path = btrfs_alloc_path();
3708 if (!path)
3709 return -ENOMEM;
3710
Chris Mason8f18cf12008-04-25 16:53:30 -04003711 path->reada = 2;
3712
Chris Mason7d9eb122008-07-08 14:19:17 -04003713 lock_chunks(root);
3714
Chris Mason8f18cf12008-04-25 16:53:30 -04003715 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003716 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003717 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003718 spin_lock(&root->fs_info->free_chunk_lock);
3719 root->fs_info->free_chunk_space -= diff;
3720 spin_unlock(&root->fs_info->free_chunk_lock);
3721 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003722 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003723
Josef Bacikba1bf482009-09-11 16:11:19 -04003724again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003725 key.objectid = device->devid;
3726 key.offset = (u64)-1;
3727 key.type = BTRFS_DEV_EXTENT_KEY;
3728
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003729 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003730 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3731 if (ret < 0)
3732 goto done;
3733
3734 ret = btrfs_previous_item(root, path, 0, key.type);
3735 if (ret < 0)
3736 goto done;
3737 if (ret) {
3738 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003739 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003740 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003741 }
3742
3743 l = path->nodes[0];
3744 slot = path->slots[0];
3745 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3746
Josef Bacikba1bf482009-09-11 16:11:19 -04003747 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003748 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003749 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003750 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003751
3752 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3753 length = btrfs_dev_extent_length(l, dev_extent);
3754
Josef Bacikba1bf482009-09-11 16:11:19 -04003755 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003756 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003757 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003758 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003759
3760 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3761 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3762 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003763 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003764
3765 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3766 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003767 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003768 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003769 if (ret == -ENOSPC)
3770 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003771 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003772
3773 if (failed && !retried) {
3774 failed = 0;
3775 retried = true;
3776 goto again;
3777 } else if (failed && retried) {
3778 ret = -ENOSPC;
3779 lock_chunks(root);
3780
3781 device->total_bytes = old_size;
3782 if (device->writeable)
3783 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003784 spin_lock(&root->fs_info->free_chunk_lock);
3785 root->fs_info->free_chunk_space += diff;
3786 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003787 unlock_chunks(root);
3788 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003789 }
3790
Chris Balld6397ba2009-04-27 07:29:03 -04003791 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003792 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003793 if (IS_ERR(trans)) {
3794 ret = PTR_ERR(trans);
3795 goto done;
3796 }
3797
Chris Balld6397ba2009-04-27 07:29:03 -04003798 lock_chunks(root);
3799
3800 device->disk_total_bytes = new_size;
3801 /* Now btrfs_update_device() will change the on-disk size. */
3802 ret = btrfs_update_device(trans, device);
3803 if (ret) {
3804 unlock_chunks(root);
3805 btrfs_end_transaction(trans, root);
3806 goto done;
3807 }
3808 WARN_ON(diff > old_total);
3809 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3810 unlock_chunks(root);
3811 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003812done:
3813 btrfs_free_path(path);
3814 return ret;
3815}
3816
Li Zefan125ccb02011-12-08 15:07:24 +08003817static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003818 struct btrfs_key *key,
3819 struct btrfs_chunk *chunk, int item_size)
3820{
David Sterba6c417612011-04-13 15:41:04 +02003821 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003822 struct btrfs_disk_key disk_key;
3823 u32 array_size;
3824 u8 *ptr;
3825
3826 array_size = btrfs_super_sys_array_size(super_copy);
3827 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3828 return -EFBIG;
3829
3830 ptr = super_copy->sys_chunk_array + array_size;
3831 btrfs_cpu_key_to_disk(&disk_key, key);
3832 memcpy(ptr, &disk_key, sizeof(disk_key));
3833 ptr += sizeof(disk_key);
3834 memcpy(ptr, chunk, item_size);
3835 item_size += sizeof(disk_key);
3836 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3837 return 0;
3838}
3839
Miao Xieb2117a32011-01-05 10:07:28 +00003840/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003841 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003842 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003843static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003844{
Arne Jansen73c5de02011-04-12 12:07:57 +02003845 const struct btrfs_device_info *di_a = a;
3846 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003847
Arne Jansen73c5de02011-04-12 12:07:57 +02003848 if (di_a->max_avail > di_b->max_avail)
3849 return -1;
3850 if (di_a->max_avail < di_b->max_avail)
3851 return 1;
3852 if (di_a->total_avail > di_b->total_avail)
3853 return -1;
3854 if (di_a->total_avail < di_b->total_avail)
3855 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003856 return 0;
3857}
3858
Eric Sandeen48a3b632013-04-25 20:41:01 +00003859static struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
Miao Xiee6ec7162013-01-17 05:38:51 +00003860 [BTRFS_RAID_RAID10] = {
3861 .sub_stripes = 2,
3862 .dev_stripes = 1,
3863 .devs_max = 0, /* 0 == as many as possible */
3864 .devs_min = 4,
3865 .devs_increment = 2,
3866 .ncopies = 2,
3867 },
3868 [BTRFS_RAID_RAID1] = {
3869 .sub_stripes = 1,
3870 .dev_stripes = 1,
3871 .devs_max = 2,
3872 .devs_min = 2,
3873 .devs_increment = 2,
3874 .ncopies = 2,
3875 },
3876 [BTRFS_RAID_DUP] = {
3877 .sub_stripes = 1,
3878 .dev_stripes = 2,
3879 .devs_max = 1,
3880 .devs_min = 1,
3881 .devs_increment = 1,
3882 .ncopies = 2,
3883 },
3884 [BTRFS_RAID_RAID0] = {
3885 .sub_stripes = 1,
3886 .dev_stripes = 1,
3887 .devs_max = 0,
3888 .devs_min = 2,
3889 .devs_increment = 1,
3890 .ncopies = 1,
3891 },
3892 [BTRFS_RAID_SINGLE] = {
3893 .sub_stripes = 1,
3894 .dev_stripes = 1,
3895 .devs_max = 1,
3896 .devs_min = 1,
3897 .devs_increment = 1,
3898 .ncopies = 1,
3899 },
Chris Masone942f882013-02-20 14:06:05 -05003900 [BTRFS_RAID_RAID5] = {
3901 .sub_stripes = 1,
3902 .dev_stripes = 1,
3903 .devs_max = 0,
3904 .devs_min = 2,
3905 .devs_increment = 1,
3906 .ncopies = 2,
3907 },
3908 [BTRFS_RAID_RAID6] = {
3909 .sub_stripes = 1,
3910 .dev_stripes = 1,
3911 .devs_max = 0,
3912 .devs_min = 3,
3913 .devs_increment = 1,
3914 .ncopies = 3,
3915 },
Liu Bo31e50222012-11-21 14:18:10 +00003916};
3917
David Woodhouse53b381b2013-01-29 18:40:14 -05003918static u32 find_raid56_stripe_len(u32 data_devices, u32 dev_stripe_target)
3919{
3920 /* TODO allow them to set a preferred stripe size */
3921 return 64 * 1024;
3922}
3923
3924static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
3925{
David Woodhouse53b381b2013-01-29 18:40:14 -05003926 if (!(type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)))
3927 return;
3928
Miao Xieceda0862013-04-11 10:30:16 +00003929 btrfs_set_fs_incompat(info, RAID56);
David Woodhouse53b381b2013-01-29 18:40:14 -05003930}
3931
Miao Xieb2117a32011-01-05 10:07:28 +00003932static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
Josef Bacik6df9a952013-06-27 13:22:46 -04003933 struct btrfs_root *extent_root, u64 start,
3934 u64 type)
Miao Xieb2117a32011-01-05 10:07:28 +00003935{
3936 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003937 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3938 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003939 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003940 struct extent_map_tree *em_tree;
3941 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003942 struct btrfs_device_info *devices_info = NULL;
3943 u64 total_avail;
3944 int num_stripes; /* total number of stripes to allocate */
David Woodhouse53b381b2013-01-29 18:40:14 -05003945 int data_stripes; /* number of stripes that count for
3946 block group size */
Arne Jansen73c5de02011-04-12 12:07:57 +02003947 int sub_stripes; /* sub_stripes info for map */
3948 int dev_stripes; /* stripes per dev */
3949 int devs_max; /* max devs to use */
3950 int devs_min; /* min devs needed */
3951 int devs_increment; /* ndevs has to be a multiple of this */
3952 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003953 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003954 u64 max_stripe_size;
3955 u64 max_chunk_size;
3956 u64 stripe_size;
3957 u64 num_bytes;
David Woodhouse53b381b2013-01-29 18:40:14 -05003958 u64 raid_stripe_len = BTRFS_STRIPE_LEN;
Arne Jansen73c5de02011-04-12 12:07:57 +02003959 int ndevs;
3960 int i;
3961 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003962 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003963
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003964 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003965
Miao Xieb2117a32011-01-05 10:07:28 +00003966 if (list_empty(&fs_devices->alloc_list))
3967 return -ENOSPC;
3968
Liu Bo31e50222012-11-21 14:18:10 +00003969 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003970
Liu Bo31e50222012-11-21 14:18:10 +00003971 sub_stripes = btrfs_raid_array[index].sub_stripes;
3972 dev_stripes = btrfs_raid_array[index].dev_stripes;
3973 devs_max = btrfs_raid_array[index].devs_max;
3974 devs_min = btrfs_raid_array[index].devs_min;
3975 devs_increment = btrfs_raid_array[index].devs_increment;
3976 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003977
3978 if (type & BTRFS_BLOCK_GROUP_DATA) {
3979 max_stripe_size = 1024 * 1024 * 1024;
3980 max_chunk_size = 10 * max_stripe_size;
3981 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003982 /* for larger filesystems, use larger metadata chunks */
3983 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3984 max_stripe_size = 1024 * 1024 * 1024;
3985 else
3986 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003987 max_chunk_size = max_stripe_size;
3988 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003989 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003990 max_chunk_size = 2 * max_stripe_size;
3991 } else {
3992 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3993 type);
3994 BUG_ON(1);
3995 }
3996
3997 /* we don't want a chunk larger than 10% of writeable space */
3998 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3999 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00004000
4001 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
4002 GFP_NOFS);
4003 if (!devices_info)
4004 return -ENOMEM;
4005
Arne Jansen73c5de02011-04-12 12:07:57 +02004006 cur = fs_devices->alloc_list.next;
4007
4008 /*
4009 * in the first pass through the devices list, we gather information
4010 * about the available holes on each device.
4011 */
4012 ndevs = 0;
4013 while (cur != &fs_devices->alloc_list) {
4014 struct btrfs_device *device;
4015 u64 max_avail;
4016 u64 dev_offset;
4017
4018 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
4019
4020 cur = cur->next;
4021
4022 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004023 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02004024 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02004025 continue;
4026 }
4027
Stefan Behrens63a212a2012-11-05 18:29:28 +01004028 if (!device->in_fs_metadata ||
4029 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02004030 continue;
4031
4032 if (device->total_bytes > device->bytes_used)
4033 total_avail = device->total_bytes - device->bytes_used;
4034 else
4035 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00004036
4037 /* If there is no space on this device, skip it. */
4038 if (total_avail == 0)
4039 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02004040
Josef Bacik6df9a952013-06-27 13:22:46 -04004041 ret = find_free_dev_extent(trans, device,
Arne Jansen73c5de02011-04-12 12:07:57 +02004042 max_stripe_size * dev_stripes,
4043 &dev_offset, &max_avail);
4044 if (ret && ret != -ENOSPC)
4045 goto error;
4046
4047 if (ret == 0)
4048 max_avail = max_stripe_size * dev_stripes;
4049
4050 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
4051 continue;
4052
Eric Sandeen063d0062013-01-31 00:55:01 +00004053 if (ndevs == fs_devices->rw_devices) {
4054 WARN(1, "%s: found more than %llu devices\n",
4055 __func__, fs_devices->rw_devices);
4056 break;
4057 }
Arne Jansen73c5de02011-04-12 12:07:57 +02004058 devices_info[ndevs].dev_offset = dev_offset;
4059 devices_info[ndevs].max_avail = max_avail;
4060 devices_info[ndevs].total_avail = total_avail;
4061 devices_info[ndevs].dev = device;
4062 ++ndevs;
4063 }
4064
4065 /*
4066 * now sort the devices by hole size / available space
4067 */
4068 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
4069 btrfs_cmp_device_info, NULL);
4070
4071 /* round down to number of usable stripes */
4072 ndevs -= ndevs % devs_increment;
4073
4074 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
4075 ret = -ENOSPC;
4076 goto error;
4077 }
4078
4079 if (devs_max && ndevs > devs_max)
4080 ndevs = devs_max;
4081 /*
4082 * the primary goal is to maximize the number of stripes, so use as many
4083 * devices as possible, even if the stripes are not maximum sized.
4084 */
4085 stripe_size = devices_info[ndevs-1].max_avail;
4086 num_stripes = ndevs * dev_stripes;
4087
David Woodhouse53b381b2013-01-29 18:40:14 -05004088 /*
4089 * this will have to be fixed for RAID1 and RAID10 over
4090 * more drives
4091 */
4092 data_stripes = num_stripes / ncopies;
4093
David Woodhouse53b381b2013-01-29 18:40:14 -05004094 if (type & BTRFS_BLOCK_GROUP_RAID5) {
4095 raid_stripe_len = find_raid56_stripe_len(ndevs - 1,
4096 btrfs_super_stripesize(info->super_copy));
4097 data_stripes = num_stripes - 1;
4098 }
4099 if (type & BTRFS_BLOCK_GROUP_RAID6) {
4100 raid_stripe_len = find_raid56_stripe_len(ndevs - 2,
4101 btrfs_super_stripesize(info->super_copy));
4102 data_stripes = num_stripes - 2;
4103 }
Chris Mason86db2572013-02-20 16:23:40 -05004104
4105 /*
4106 * Use the number of data stripes to figure out how big this chunk
4107 * is really going to be in terms of logical address space,
4108 * and compare that answer with the max chunk size
4109 */
4110 if (stripe_size * data_stripes > max_chunk_size) {
4111 u64 mask = (1ULL << 24) - 1;
4112 stripe_size = max_chunk_size;
4113 do_div(stripe_size, data_stripes);
4114
4115 /* bump the answer up to a 16MB boundary */
4116 stripe_size = (stripe_size + mask) & ~mask;
4117
4118 /* but don't go higher than the limits we found
4119 * while searching for free extents
4120 */
4121 if (stripe_size > devices_info[ndevs-1].max_avail)
4122 stripe_size = devices_info[ndevs-1].max_avail;
4123 }
4124
Arne Jansen73c5de02011-04-12 12:07:57 +02004125 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03004126
4127 /* align to BTRFS_STRIPE_LEN */
David Woodhouse53b381b2013-01-29 18:40:14 -05004128 do_div(stripe_size, raid_stripe_len);
4129 stripe_size *= raid_stripe_len;
Arne Jansen73c5de02011-04-12 12:07:57 +02004130
Miao Xieb2117a32011-01-05 10:07:28 +00004131 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
4132 if (!map) {
4133 ret = -ENOMEM;
4134 goto error;
4135 }
4136 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04004137
Arne Jansen73c5de02011-04-12 12:07:57 +02004138 for (i = 0; i < ndevs; ++i) {
4139 for (j = 0; j < dev_stripes; ++j) {
4140 int s = i * dev_stripes + j;
4141 map->stripes[s].dev = devices_info[i].dev;
4142 map->stripes[s].physical = devices_info[i].dev_offset +
4143 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04004144 }
Chris Mason6324fbf2008-03-24 15:01:59 -04004145 }
Chris Mason593060d2008-03-25 16:50:33 -04004146 map->sector_size = extent_root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05004147 map->stripe_len = raid_stripe_len;
4148 map->io_align = raid_stripe_len;
4149 map->io_width = raid_stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004150 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04004151 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004152
David Woodhouse53b381b2013-01-29 18:40:14 -05004153 num_bytes = stripe_size * data_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004154
Arne Jansen73c5de02011-04-12 12:07:57 +02004155 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00004156
David Sterba172ddd62011-04-21 00:48:27 +02004157 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05004158 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00004159 ret = -ENOMEM;
4160 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05004161 }
Chris Mason0b86a832008-03-24 15:01:56 -04004162 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05004163 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02004164 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04004165 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004166 em->block_len = em->len;
Josef Bacik6df9a952013-06-27 13:22:46 -04004167 em->orig_block_len = stripe_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004168
Chris Mason0b86a832008-03-24 15:01:56 -04004169 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04004170 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004171 ret = add_extent_mapping(em_tree, em, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004172 if (!ret) {
4173 list_add_tail(&em->list, &trans->transaction->pending_chunks);
4174 atomic_inc(&em->refs);
4175 }
Chris Mason890871b2009-09-02 16:24:52 -04004176 write_unlock(&em_tree->lock);
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004177 if (ret) {
4178 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07004179 goto error;
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004180 }
Yan Zheng2b820322008-11-17 21:11:30 -05004181
Josef Bacik04487482013-01-29 15:03:37 -05004182 ret = btrfs_make_block_group(trans, extent_root, 0, type,
4183 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4184 start, num_bytes);
Josef Bacik6df9a952013-06-27 13:22:46 -04004185 if (ret)
4186 goto error_del_extent;
Yan Zheng2b820322008-11-17 21:11:30 -05004187
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004188 free_extent_map(em);
David Woodhouse53b381b2013-01-29 18:40:14 -05004189 check_raid56_incompat_flag(extent_root->fs_info, type);
4190
Miao Xieb2117a32011-01-05 10:07:28 +00004191 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05004192 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00004193
Josef Bacik6df9a952013-06-27 13:22:46 -04004194error_del_extent:
Josef Bacik0f5d42b2013-01-31 10:23:04 -05004195 write_lock(&em_tree->lock);
4196 remove_extent_mapping(em_tree, em);
4197 write_unlock(&em_tree->lock);
4198
4199 /* One for our allocation */
4200 free_extent_map(em);
4201 /* One for the tree reference */
4202 free_extent_map(em);
Miao Xieb2117a32011-01-05 10:07:28 +00004203error:
4204 kfree(map);
4205 kfree(devices_info);
4206 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004207}
4208
Josef Bacik6df9a952013-06-27 13:22:46 -04004209int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004210 struct btrfs_root *extent_root,
Josef Bacik6df9a952013-06-27 13:22:46 -04004211 u64 chunk_offset, u64 chunk_size)
Yan Zheng2b820322008-11-17 21:11:30 -05004212{
Yan Zheng2b820322008-11-17 21:11:30 -05004213 struct btrfs_key key;
4214 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
4215 struct btrfs_device *device;
4216 struct btrfs_chunk *chunk;
4217 struct btrfs_stripe *stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004218 struct extent_map_tree *em_tree;
4219 struct extent_map *em;
4220 struct map_lookup *map;
4221 size_t item_size;
4222 u64 dev_offset;
4223 u64 stripe_size;
4224 int i = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05004225 int ret;
4226
Josef Bacik6df9a952013-06-27 13:22:46 -04004227 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
4228 read_lock(&em_tree->lock);
4229 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4230 read_unlock(&em_tree->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004231
Josef Bacik6df9a952013-06-27 13:22:46 -04004232 if (!em) {
4233 btrfs_crit(extent_root->fs_info, "unable to find logical "
4234 "%Lu len %Lu", chunk_offset, chunk_size);
4235 return -EINVAL;
4236 }
4237
4238 if (em->start != chunk_offset || em->len != chunk_size) {
4239 btrfs_crit(extent_root->fs_info, "found a bad mapping, wanted"
4240 " %Lu-%Lu, found %Lu-%Lu\n", chunk_offset,
4241 chunk_size, em->start, em->len);
4242 free_extent_map(em);
4243 return -EINVAL;
4244 }
4245
4246 map = (struct map_lookup *)em->bdev;
4247 item_size = btrfs_chunk_item_size(map->num_stripes);
4248 stripe_size = em->orig_block_len;
4249
4250 chunk = kzalloc(item_size, GFP_NOFS);
4251 if (!chunk) {
4252 ret = -ENOMEM;
4253 goto out;
4254 }
4255
4256 for (i = 0; i < map->num_stripes; i++) {
4257 device = map->stripes[i].dev;
4258 dev_offset = map->stripes[i].physical;
4259
Yan Zheng2b820322008-11-17 21:11:30 -05004260 device->bytes_used += stripe_size;
4261 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07004262 if (ret)
Josef Bacik6df9a952013-06-27 13:22:46 -04004263 goto out;
4264 ret = btrfs_alloc_dev_extent(trans, device,
4265 chunk_root->root_key.objectid,
4266 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4267 chunk_offset, dev_offset,
4268 stripe_size);
4269 if (ret)
4270 goto out;
Yan Zheng2b820322008-11-17 21:11:30 -05004271 }
4272
Josef Bacik2bf64752011-09-26 17:12:22 -04004273 spin_lock(&extent_root->fs_info->free_chunk_lock);
4274 extent_root->fs_info->free_chunk_space -= (stripe_size *
4275 map->num_stripes);
4276 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4277
Yan Zheng2b820322008-11-17 21:11:30 -05004278 stripe = &chunk->stripe;
Josef Bacik6df9a952013-06-27 13:22:46 -04004279 for (i = 0; i < map->num_stripes; i++) {
4280 device = map->stripes[i].dev;
4281 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05004282
4283 btrfs_set_stack_stripe_devid(stripe, device->devid);
4284 btrfs_set_stack_stripe_offset(stripe, dev_offset);
4285 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4286 stripe++;
Yan Zheng2b820322008-11-17 21:11:30 -05004287 }
4288
4289 btrfs_set_stack_chunk_length(chunk, chunk_size);
4290 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
4291 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
4292 btrfs_set_stack_chunk_type(chunk, map->type);
4293 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4294 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4295 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4296 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
4297 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4298
4299 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
4300 key.type = BTRFS_CHUNK_ITEM_KEY;
4301 key.offset = chunk_offset;
4302
4303 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004304 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
4305 /*
4306 * TODO: Cleanup of inserted chunk root in case of
4307 * failure.
4308 */
Li Zefan125ccb02011-12-08 15:07:24 +08004309 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05004310 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05004311 }
liubo1abe9b82011-03-24 11:18:59 +00004312
Josef Bacik6df9a952013-06-27 13:22:46 -04004313out:
Yan Zheng2b820322008-11-17 21:11:30 -05004314 kfree(chunk);
Josef Bacik6df9a952013-06-27 13:22:46 -04004315 free_extent_map(em);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07004316 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004317}
4318
4319/*
4320 * Chunk allocation falls into two parts. The first part does works
4321 * that make the new allocated chunk useable, but not do any operation
4322 * that modifies the chunk tree. The second part does the works that
4323 * require modifying the chunk tree. This division is important for the
4324 * bootstrap process of adding storage to a seed btrfs.
4325 */
4326int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4327 struct btrfs_root *extent_root, u64 type)
4328{
4329 u64 chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004330
Josef Bacik6df9a952013-06-27 13:22:46 -04004331 chunk_offset = find_next_chunk(extent_root->fs_info);
4332 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
Yan Zheng2b820322008-11-17 21:11:30 -05004333}
4334
Chris Masond3977122009-01-05 21:25:51 -05004335static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05004336 struct btrfs_root *root,
4337 struct btrfs_device *device)
4338{
4339 u64 chunk_offset;
4340 u64 sys_chunk_offset;
Yan Zheng2b820322008-11-17 21:11:30 -05004341 u64 alloc_profile;
Yan Zheng2b820322008-11-17 21:11:30 -05004342 struct btrfs_fs_info *fs_info = root->fs_info;
4343 struct btrfs_root *extent_root = fs_info->extent_root;
4344 int ret;
4345
Josef Bacik6df9a952013-06-27 13:22:46 -04004346 chunk_offset = find_next_chunk(fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004347 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004348 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset,
4349 alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004350 if (ret)
4351 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004352
Josef Bacik6df9a952013-06-27 13:22:46 -04004353 sys_chunk_offset = find_next_chunk(root->fs_info);
Miao Xiede98ced2013-01-29 10:13:12 +00004354 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
Josef Bacik6df9a952013-06-27 13:22:46 -04004355 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
4356 alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06004357 if (ret) {
4358 btrfs_abort_transaction(trans, root, ret);
4359 goto out;
4360 }
Yan Zheng2b820322008-11-17 21:11:30 -05004361
4362 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004363 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06004364 btrfs_abort_transaction(trans, root, ret);
David Sterba005d6422012-09-18 07:52:32 -06004365out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004366 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05004367}
4368
4369int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4370{
4371 struct extent_map *em;
4372 struct map_lookup *map;
4373 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4374 int readonly = 0;
4375 int i;
4376
Chris Mason890871b2009-09-02 16:24:52 -04004377 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004378 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004379 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05004380 if (!em)
4381 return 1;
4382
Josef Bacikf48b9072010-01-27 02:07:59 +00004383 if (btrfs_test_opt(root, DEGRADED)) {
4384 free_extent_map(em);
4385 return 0;
4386 }
4387
Yan Zheng2b820322008-11-17 21:11:30 -05004388 map = (struct map_lookup *)em->bdev;
4389 for (i = 0; i < map->num_stripes; i++) {
4390 if (!map->stripes[i].dev->writeable) {
4391 readonly = 1;
4392 break;
4393 }
4394 }
4395 free_extent_map(em);
4396 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04004397}
4398
4399void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
4400{
David Sterbaa8067e02011-04-21 00:34:43 +02004401 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04004402}
4403
4404void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4405{
4406 struct extent_map *em;
4407
Chris Masond3977122009-01-05 21:25:51 -05004408 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04004409 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004410 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
4411 if (em)
4412 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04004413 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004414 if (!em)
4415 break;
4416 kfree(em->bdev);
4417 /* once for us */
4418 free_extent_map(em);
4419 /* once for the tree */
4420 free_extent_map(em);
4421 }
4422}
4423
Stefan Behrens5d964052012-11-05 14:59:07 +01004424int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04004425{
Stefan Behrens5d964052012-11-05 14:59:07 +01004426 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004427 struct extent_map *em;
4428 struct map_lookup *map;
4429 struct extent_map_tree *em_tree = &map_tree->map_tree;
4430 int ret;
4431
Chris Mason890871b2009-09-02 16:24:52 -04004432 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004433 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004434 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004435
Josef Bacikfb7669b2013-04-23 10:53:18 -04004436 /*
4437 * We could return errors for these cases, but that could get ugly and
4438 * we'd probably do the same thing which is just not do anything else
4439 * and exit, so return 1 so the callers don't try to use other copies.
4440 */
4441 if (!em) {
David Sterbaccf39f92013-07-11 15:41:11 +02004442 btrfs_crit(fs_info, "No mapping for %Lu-%Lu\n", logical,
Josef Bacikfb7669b2013-04-23 10:53:18 -04004443 logical+len);
4444 return 1;
4445 }
4446
4447 if (em->start > logical || em->start + em->len < logical) {
David Sterbaccf39f92013-07-11 15:41:11 +02004448 btrfs_crit(fs_info, "Invalid mapping for %Lu-%Lu, got "
Josef Bacikfb7669b2013-04-23 10:53:18 -04004449 "%Lu-%Lu\n", logical, logical+len, em->start,
4450 em->start + em->len);
4451 return 1;
4452 }
4453
Chris Masonf1885912008-04-09 16:28:12 -04004454 map = (struct map_lookup *)em->bdev;
4455 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4456 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004457 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4458 ret = map->sub_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05004459 else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
4460 ret = 2;
4461 else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4462 ret = 3;
Chris Masonf1885912008-04-09 16:28:12 -04004463 else
4464 ret = 1;
4465 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004466
4467 btrfs_dev_replace_lock(&fs_info->dev_replace);
4468 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4469 ret++;
4470 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4471
Chris Masonf1885912008-04-09 16:28:12 -04004472 return ret;
4473}
4474
David Woodhouse53b381b2013-01-29 18:40:14 -05004475unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
4476 struct btrfs_mapping_tree *map_tree,
4477 u64 logical)
4478{
4479 struct extent_map *em;
4480 struct map_lookup *map;
4481 struct extent_map_tree *em_tree = &map_tree->map_tree;
4482 unsigned long len = root->sectorsize;
4483
4484 read_lock(&em_tree->lock);
4485 em = lookup_extent_mapping(em_tree, logical, len);
4486 read_unlock(&em_tree->lock);
4487 BUG_ON(!em);
4488
4489 BUG_ON(em->start > logical || em->start + em->len < logical);
4490 map = (struct map_lookup *)em->bdev;
4491 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4492 BTRFS_BLOCK_GROUP_RAID6)) {
4493 len = map->stripe_len * nr_data_stripes(map);
4494 }
4495 free_extent_map(em);
4496 return len;
4497}
4498
4499int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
4500 u64 logical, u64 len, int mirror_num)
4501{
4502 struct extent_map *em;
4503 struct map_lookup *map;
4504 struct extent_map_tree *em_tree = &map_tree->map_tree;
4505 int ret = 0;
4506
4507 read_lock(&em_tree->lock);
4508 em = lookup_extent_mapping(em_tree, logical, len);
4509 read_unlock(&em_tree->lock);
4510 BUG_ON(!em);
4511
4512 BUG_ON(em->start > logical || em->start + em->len < logical);
4513 map = (struct map_lookup *)em->bdev;
4514 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4515 BTRFS_BLOCK_GROUP_RAID6))
4516 ret = 1;
4517 free_extent_map(em);
4518 return ret;
4519}
4520
Stefan Behrens30d98612012-11-06 14:52:18 +01004521static int find_live_mirror(struct btrfs_fs_info *fs_info,
4522 struct map_lookup *map, int first, int num,
4523 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004524{
4525 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004526 int tolerance;
4527 struct btrfs_device *srcdev;
4528
4529 if (dev_replace_is_ongoing &&
4530 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4531 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4532 srcdev = fs_info->dev_replace.srcdev;
4533 else
4534 srcdev = NULL;
4535
4536 /*
4537 * try to avoid the drive that is the source drive for a
4538 * dev-replace procedure, only choose it if no other non-missing
4539 * mirror is available
4540 */
4541 for (tolerance = 0; tolerance < 2; tolerance++) {
4542 if (map->stripes[optimal].dev->bdev &&
4543 (tolerance || map->stripes[optimal].dev != srcdev))
4544 return optimal;
4545 for (i = first; i < first + num; i++) {
4546 if (map->stripes[i].dev->bdev &&
4547 (tolerance || map->stripes[i].dev != srcdev))
4548 return i;
4549 }
Chris Masondfe25022008-05-13 13:46:40 -04004550 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004551
Chris Masondfe25022008-05-13 13:46:40 -04004552 /* we couldn't find one that doesn't fail. Just return something
4553 * and the io error handling code will clean up eventually
4554 */
4555 return optimal;
4556}
4557
David Woodhouse53b381b2013-01-29 18:40:14 -05004558static inline int parity_smaller(u64 a, u64 b)
4559{
4560 return a > b;
4561}
4562
4563/* Bubble-sort the stripe set to put the parity/syndrome stripes last */
4564static void sort_parity_stripes(struct btrfs_bio *bbio, u64 *raid_map)
4565{
4566 struct btrfs_bio_stripe s;
4567 int i;
4568 u64 l;
4569 int again = 1;
4570
4571 while (again) {
4572 again = 0;
4573 for (i = 0; i < bbio->num_stripes - 1; i++) {
4574 if (parity_smaller(raid_map[i], raid_map[i+1])) {
4575 s = bbio->stripes[i];
4576 l = raid_map[i];
4577 bbio->stripes[i] = bbio->stripes[i+1];
4578 raid_map[i] = raid_map[i+1];
4579 bbio->stripes[i+1] = s;
4580 raid_map[i+1] = l;
4581 again = 1;
4582 }
4583 }
4584 }
4585}
4586
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004587static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004588 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004589 struct btrfs_bio **bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05004590 int mirror_num, u64 **raid_map_ret)
Chris Mason0b86a832008-03-24 15:01:56 -04004591{
4592 struct extent_map *em;
4593 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004594 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004595 struct extent_map_tree *em_tree = &map_tree->map_tree;
4596 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004597 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004598 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004599 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004600 u64 stripe_nr_orig;
4601 u64 stripe_nr_end;
David Woodhouse53b381b2013-01-29 18:40:14 -05004602 u64 stripe_len;
4603 u64 *raid_map = NULL;
Chris Mason593060d2008-03-25 16:50:33 -04004604 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004605 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004606 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004607 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004608 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004609 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004610 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4611 int dev_replace_is_ongoing = 0;
4612 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004613 int patch_the_first_stripe_for_dev_replace = 0;
4614 u64 physical_to_patch_in_first_stripe = 0;
David Woodhouse53b381b2013-01-29 18:40:14 -05004615 u64 raid56_full_stripe_start = (u64)-1;
Chris Mason0b86a832008-03-24 15:01:56 -04004616
Chris Mason890871b2009-09-02 16:24:52 -04004617 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004618 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004619 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004620
Chris Mason3b951512008-04-17 11:29:12 -04004621 if (!em) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00004622 btrfs_crit(fs_info, "unable to find logical %llu len %llu",
4623 (unsigned long long)logical,
4624 (unsigned long long)*length);
Josef Bacik9bb91872013-04-19 23:45:33 -04004625 return -EINVAL;
Chris Mason3b951512008-04-17 11:29:12 -04004626 }
Chris Mason0b86a832008-03-24 15:01:56 -04004627
Josef Bacik9bb91872013-04-19 23:45:33 -04004628 if (em->start > logical || em->start + em->len < logical) {
4629 btrfs_crit(fs_info, "found a bad mapping, wanted %Lu, "
4630 "found %Lu-%Lu\n", logical, em->start,
4631 em->start + em->len);
4632 return -EINVAL;
4633 }
4634
Chris Mason0b86a832008-03-24 15:01:56 -04004635 map = (struct map_lookup *)em->bdev;
4636 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004637
David Woodhouse53b381b2013-01-29 18:40:14 -05004638 stripe_len = map->stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004639 stripe_nr = offset;
4640 /*
4641 * stripe_nr counts the total number of stripes we have to stride
4642 * to get to this block
4643 */
David Woodhouse53b381b2013-01-29 18:40:14 -05004644 do_div(stripe_nr, stripe_len);
Chris Mason593060d2008-03-25 16:50:33 -04004645
David Woodhouse53b381b2013-01-29 18:40:14 -05004646 stripe_offset = stripe_nr * stripe_len;
Chris Mason593060d2008-03-25 16:50:33 -04004647 BUG_ON(offset < stripe_offset);
4648
4649 /* stripe_offset is the offset of this block in its stripe*/
4650 stripe_offset = offset - stripe_offset;
4651
David Woodhouse53b381b2013-01-29 18:40:14 -05004652 /* if we're here for raid56, we need to know the stripe aligned start */
4653 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4654 unsigned long full_stripe_len = stripe_len * nr_data_stripes(map);
4655 raid56_full_stripe_start = offset;
4656
4657 /* allow a write of a full stripe, but make sure we don't
4658 * allow straddling of stripes
4659 */
4660 do_div(raid56_full_stripe_start, full_stripe_len);
4661 raid56_full_stripe_start *= full_stripe_len;
4662 }
4663
4664 if (rw & REQ_DISCARD) {
4665 /* we don't discard raid56 yet */
4666 if (map->type &
4667 (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6)) {
4668 ret = -EOPNOTSUPP;
4669 goto out;
4670 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004671 *length = min_t(u64, em->len - offset, *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004672 } else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
4673 u64 max_len;
4674 /* For writes to RAID[56], allow a full stripeset across all disks.
4675 For other RAID types and for RAID[56] reads, just allow a single
4676 stripe (on a single disk). */
4677 if (map->type & (BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6) &&
4678 (rw & REQ_WRITE)) {
4679 max_len = stripe_len * nr_data_stripes(map) -
4680 (offset - raid56_full_stripe_start);
4681 } else {
4682 /* we limit the length of each bio to what fits in a stripe */
4683 max_len = stripe_len - stripe_offset;
4684 }
4685 *length = min_t(u64, em->len - offset, max_len);
Chris Masoncea9e442008-04-09 16:28:12 -04004686 } else {
4687 *length = em->len - offset;
4688 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004689
David Woodhouse53b381b2013-01-29 18:40:14 -05004690 /* This is for when we're called from btrfs_merge_bio_hook() and all
4691 it cares about is the length */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004692 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004693 goto out;
4694
Stefan Behrens472262f2012-11-06 14:43:46 +01004695 btrfs_dev_replace_lock(dev_replace);
4696 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4697 if (!dev_replace_is_ongoing)
4698 btrfs_dev_replace_unlock(dev_replace);
4699
Stefan Behrensad6d6202012-11-06 15:06:47 +01004700 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4701 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4702 dev_replace->tgtdev != NULL) {
4703 /*
4704 * in dev-replace case, for repair case (that's the only
4705 * case where the mirror is selected explicitly when
4706 * calling btrfs_map_block), blocks left of the left cursor
4707 * can also be read from the target drive.
4708 * For REQ_GET_READ_MIRRORS, the target drive is added as
4709 * the last one to the array of stripes. For READ, it also
4710 * needs to be supported using the same mirror number.
4711 * If the requested block is not left of the left cursor,
4712 * EIO is returned. This can happen because btrfs_num_copies()
4713 * returns one more in the dev-replace case.
4714 */
4715 u64 tmp_length = *length;
4716 struct btrfs_bio *tmp_bbio = NULL;
4717 int tmp_num_stripes;
4718 u64 srcdev_devid = dev_replace->srcdev->devid;
4719 int index_srcdev = 0;
4720 int found = 0;
4721 u64 physical_of_found = 0;
4722
4723 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
David Woodhouse53b381b2013-01-29 18:40:14 -05004724 logical, &tmp_length, &tmp_bbio, 0, NULL);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004725 if (ret) {
4726 WARN_ON(tmp_bbio != NULL);
4727 goto out;
4728 }
4729
4730 tmp_num_stripes = tmp_bbio->num_stripes;
4731 if (mirror_num > tmp_num_stripes) {
4732 /*
4733 * REQ_GET_READ_MIRRORS does not contain this
4734 * mirror, that means that the requested area
4735 * is not left of the left cursor
4736 */
4737 ret = -EIO;
4738 kfree(tmp_bbio);
4739 goto out;
4740 }
4741
4742 /*
4743 * process the rest of the function using the mirror_num
4744 * of the source drive. Therefore look it up first.
4745 * At the end, patch the device pointer to the one of the
4746 * target drive.
4747 */
4748 for (i = 0; i < tmp_num_stripes; i++) {
4749 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4750 /*
4751 * In case of DUP, in order to keep it
4752 * simple, only add the mirror with the
4753 * lowest physical address
4754 */
4755 if (found &&
4756 physical_of_found <=
4757 tmp_bbio->stripes[i].physical)
4758 continue;
4759 index_srcdev = i;
4760 found = 1;
4761 physical_of_found =
4762 tmp_bbio->stripes[i].physical;
4763 }
4764 }
4765
4766 if (found) {
4767 mirror_num = index_srcdev + 1;
4768 patch_the_first_stripe_for_dev_replace = 1;
4769 physical_to_patch_in_first_stripe = physical_of_found;
4770 } else {
4771 WARN_ON(1);
4772 ret = -EIO;
4773 kfree(tmp_bbio);
4774 goto out;
4775 }
4776
4777 kfree(tmp_bbio);
4778 } else if (mirror_num > map->num_stripes) {
4779 mirror_num = 0;
4780 }
4781
Chris Masonf2d8d742008-04-21 10:03:05 -04004782 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004783 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004784 stripe_nr_orig = stripe_nr;
Qu Wenruofda28322013-02-26 08:10:22 +00004785 stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
Li Dongyangfce3bb92011-03-24 10:24:26 +00004786 do_div(stripe_nr_end, map->stripe_len);
4787 stripe_end_offset = stripe_nr_end * map->stripe_len -
4788 (offset + *length);
David Woodhouse53b381b2013-01-29 18:40:14 -05004789
Li Dongyangfce3bb92011-03-24 10:24:26 +00004790 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4791 if (rw & REQ_DISCARD)
4792 num_stripes = min_t(u64, map->num_stripes,
4793 stripe_nr_end - stripe_nr_orig);
4794 stripe_index = do_div(stripe_nr, map->num_stripes);
4795 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004796 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004797 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004798 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004799 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004800 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004801 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004802 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004803 current->pid % map->num_stripes,
4804 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004805 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004806 }
Chris Mason2fff7342008-04-29 14:12:09 -04004807
Chris Mason611f0e02008-04-03 16:29:03 -04004808 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004809 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004810 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004811 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004812 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004813 } else {
4814 mirror_num = 1;
4815 }
Chris Mason2fff7342008-04-29 14:12:09 -04004816
Chris Mason321aecc2008-04-16 10:49:51 -04004817 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4818 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004819
4820 stripe_index = do_div(stripe_nr, factor);
4821 stripe_index *= map->sub_stripes;
4822
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004823 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004824 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004825 else if (rw & REQ_DISCARD)
4826 num_stripes = min_t(u64, map->sub_stripes *
4827 (stripe_nr_end - stripe_nr_orig),
4828 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004829 else if (mirror_num)
4830 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004831 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004832 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004833 stripe_index = find_live_mirror(fs_info, map,
4834 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004835 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004836 current->pid % map->sub_stripes,
4837 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004838 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004839 }
David Woodhouse53b381b2013-01-29 18:40:14 -05004840
4841 } else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
4842 BTRFS_BLOCK_GROUP_RAID6)) {
4843 u64 tmp;
4844
4845 if (bbio_ret && ((rw & REQ_WRITE) || mirror_num > 1)
4846 && raid_map_ret) {
4847 int i, rot;
4848
4849 /* push stripe_nr back to the start of the full stripe */
4850 stripe_nr = raid56_full_stripe_start;
4851 do_div(stripe_nr, stripe_len);
4852
4853 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4854
4855 /* RAID[56] write or recovery. Return all stripes */
4856 num_stripes = map->num_stripes;
4857 max_errors = nr_parity_stripes(map);
4858
4859 raid_map = kmalloc(sizeof(u64) * num_stripes,
4860 GFP_NOFS);
4861 if (!raid_map) {
4862 ret = -ENOMEM;
4863 goto out;
4864 }
4865
4866 /* Work out the disk rotation on this stripe-set */
4867 tmp = stripe_nr;
4868 rot = do_div(tmp, num_stripes);
4869
4870 /* Fill in the logical address of each stripe */
4871 tmp = stripe_nr * nr_data_stripes(map);
4872 for (i = 0; i < nr_data_stripes(map); i++)
4873 raid_map[(i+rot) % num_stripes] =
4874 em->start + (tmp + i) * map->stripe_len;
4875
4876 raid_map[(i+rot) % map->num_stripes] = RAID5_P_STRIPE;
4877 if (map->type & BTRFS_BLOCK_GROUP_RAID6)
4878 raid_map[(i+rot+1) % num_stripes] =
4879 RAID6_Q_STRIPE;
4880
4881 *length = map->stripe_len;
4882 stripe_index = 0;
4883 stripe_offset = 0;
4884 } else {
4885 /*
4886 * Mirror #0 or #1 means the original data block.
4887 * Mirror #2 is RAID5 parity block.
4888 * Mirror #3 is RAID6 Q block.
4889 */
4890 stripe_index = do_div(stripe_nr, nr_data_stripes(map));
4891 if (mirror_num > 1)
4892 stripe_index = nr_data_stripes(map) +
4893 mirror_num - 2;
4894
4895 /* We distribute the parity blocks across stripes */
4896 tmp = stripe_nr + stripe_index;
4897 stripe_index = do_div(tmp, map->num_stripes);
4898 }
Chris Mason8790d502008-04-03 16:29:03 -04004899 } else {
4900 /*
4901 * after this do_div call, stripe_nr is the number of stripes
4902 * on this device we have to walk to find the data, and
4903 * stripe_index is the number of our device in the stripe array
4904 */
4905 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004906 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004907 }
Chris Mason593060d2008-03-25 16:50:33 -04004908 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004909
Stefan Behrens472262f2012-11-06 14:43:46 +01004910 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004911 if (dev_replace_is_ongoing) {
4912 if (rw & (REQ_WRITE | REQ_DISCARD))
4913 num_alloc_stripes <<= 1;
4914 if (rw & REQ_GET_READ_MIRRORS)
4915 num_alloc_stripes++;
4916 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004917 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004918 if (!bbio) {
Dave Joneseb2067f2013-07-30 13:42:17 -04004919 kfree(raid_map);
Li Zefande11cc12011-12-01 12:55:47 +08004920 ret = -ENOMEM;
4921 goto out;
4922 }
4923 atomic_set(&bbio->error, 0);
4924
Li Dongyangfce3bb92011-03-24 10:24:26 +00004925 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004926 int factor = 0;
4927 int sub_stripes = 0;
4928 u64 stripes_per_dev = 0;
4929 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004930 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004931
4932 if (map->type &
4933 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4934 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4935 sub_stripes = 1;
4936 else
4937 sub_stripes = map->sub_stripes;
4938
4939 factor = map->num_stripes / sub_stripes;
4940 stripes_per_dev = div_u64_rem(stripe_nr_end -
4941 stripe_nr_orig,
4942 factor,
4943 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004944 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4945 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004946 }
4947
Li Dongyangfce3bb92011-03-24 10:24:26 +00004948 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004949 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004950 map->stripes[stripe_index].physical +
4951 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004952 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004953
Li Zefanec9ef7a2011-12-01 14:06:42 +08004954 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4955 BTRFS_BLOCK_GROUP_RAID10)) {
4956 bbio->stripes[i].length = stripes_per_dev *
4957 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004958
Li Zefanec9ef7a2011-12-01 14:06:42 +08004959 if (i / sub_stripes < remaining_stripes)
4960 bbio->stripes[i].length +=
4961 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004962
4963 /*
4964 * Special for the first stripe and
4965 * the last stripe:
4966 *
4967 * |-------|...|-------|
4968 * |----------|
4969 * off end_off
4970 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004971 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004972 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004973 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004974
4975 if (stripe_index >= last_stripe &&
4976 stripe_index <= (last_stripe +
4977 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004978 bbio->stripes[i].length -=
4979 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004980
Li Zefanec9ef7a2011-12-01 14:06:42 +08004981 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004982 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004983 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004984 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004985
4986 stripe_index++;
4987 if (stripe_index == map->num_stripes) {
4988 /* This could only happen for RAID0/10 */
4989 stripe_index = 0;
4990 stripe_nr++;
4991 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004992 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004993 } else {
4994 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004995 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004996 map->stripes[stripe_index].physical +
4997 stripe_offset +
4998 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004999 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07005000 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00005001 stripe_index++;
5002 }
Chris Mason593060d2008-03-25 16:50:33 -04005003 }
Li Zefande11cc12011-12-01 12:55:47 +08005004
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01005005 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08005006 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
5007 BTRFS_BLOCK_GROUP_RAID10 |
David Woodhouse53b381b2013-01-29 18:40:14 -05005008 BTRFS_BLOCK_GROUP_RAID5 |
Li Zefande11cc12011-12-01 12:55:47 +08005009 BTRFS_BLOCK_GROUP_DUP)) {
5010 max_errors = 1;
David Woodhouse53b381b2013-01-29 18:40:14 -05005011 } else if (map->type & BTRFS_BLOCK_GROUP_RAID6) {
5012 max_errors = 2;
Li Zefande11cc12011-12-01 12:55:47 +08005013 }
Chris Masonf2d8d742008-04-21 10:03:05 -04005014 }
Li Zefande11cc12011-12-01 12:55:47 +08005015
Stefan Behrens472262f2012-11-06 14:43:46 +01005016 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
5017 dev_replace->tgtdev != NULL) {
5018 int index_where_to_add;
5019 u64 srcdev_devid = dev_replace->srcdev->devid;
5020
5021 /*
5022 * duplicate the write operations while the dev replace
5023 * procedure is running. Since the copying of the old disk
5024 * to the new disk takes place at run time while the
5025 * filesystem is mounted writable, the regular write
5026 * operations to the old disk have to be duplicated to go
5027 * to the new disk as well.
5028 * Note that device->missing is handled by the caller, and
5029 * that the write to the old disk is already set up in the
5030 * stripes array.
5031 */
5032 index_where_to_add = num_stripes;
5033 for (i = 0; i < num_stripes; i++) {
5034 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5035 /* write to new disk, too */
5036 struct btrfs_bio_stripe *new =
5037 bbio->stripes + index_where_to_add;
5038 struct btrfs_bio_stripe *old =
5039 bbio->stripes + i;
5040
5041 new->physical = old->physical;
5042 new->length = old->length;
5043 new->dev = dev_replace->tgtdev;
5044 index_where_to_add++;
5045 max_errors++;
5046 }
5047 }
5048 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005049 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
5050 dev_replace->tgtdev != NULL) {
5051 u64 srcdev_devid = dev_replace->srcdev->devid;
5052 int index_srcdev = 0;
5053 int found = 0;
5054 u64 physical_of_found = 0;
5055
5056 /*
5057 * During the dev-replace procedure, the target drive can
5058 * also be used to read data in case it is needed to repair
5059 * a corrupt block elsewhere. This is possible if the
5060 * requested area is left of the left cursor. In this area,
5061 * the target drive is a full copy of the source drive.
5062 */
5063 for (i = 0; i < num_stripes; i++) {
5064 if (bbio->stripes[i].dev->devid == srcdev_devid) {
5065 /*
5066 * In case of DUP, in order to keep it
5067 * simple, only add the mirror with the
5068 * lowest physical address
5069 */
5070 if (found &&
5071 physical_of_found <=
5072 bbio->stripes[i].physical)
5073 continue;
5074 index_srcdev = i;
5075 found = 1;
5076 physical_of_found = bbio->stripes[i].physical;
5077 }
5078 }
5079 if (found) {
5080 u64 length = map->stripe_len;
5081
5082 if (physical_of_found + length <=
5083 dev_replace->cursor_left) {
5084 struct btrfs_bio_stripe *tgtdev_stripe =
5085 bbio->stripes + num_stripes;
5086
5087 tgtdev_stripe->physical = physical_of_found;
5088 tgtdev_stripe->length =
5089 bbio->stripes[index_srcdev].length;
5090 tgtdev_stripe->dev = dev_replace->tgtdev;
5091
5092 num_stripes++;
5093 }
5094 }
Stefan Behrens472262f2012-11-06 14:43:46 +01005095 }
5096
Li Zefande11cc12011-12-01 12:55:47 +08005097 *bbio_ret = bbio;
5098 bbio->num_stripes = num_stripes;
5099 bbio->max_errors = max_errors;
5100 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01005101
5102 /*
5103 * this is the case that REQ_READ && dev_replace_is_ongoing &&
5104 * mirror_num == num_stripes + 1 && dev_replace target drive is
5105 * available as a mirror
5106 */
5107 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
5108 WARN_ON(num_stripes > 1);
5109 bbio->stripes[0].dev = dev_replace->tgtdev;
5110 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
5111 bbio->mirror_num = map->num_stripes + 1;
5112 }
David Woodhouse53b381b2013-01-29 18:40:14 -05005113 if (raid_map) {
5114 sort_parity_stripes(bbio, raid_map);
5115 *raid_map_ret = raid_map;
5116 }
Chris Masoncea9e442008-04-09 16:28:12 -04005117out:
Stefan Behrens472262f2012-11-06 14:43:46 +01005118 if (dev_replace_is_ongoing)
5119 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04005120 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08005121 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005122}
5123
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005124int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04005125 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02005126 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04005127{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01005128 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
David Woodhouse53b381b2013-01-29 18:40:14 -05005129 mirror_num, NULL);
Chris Masonf2d8d742008-04-21 10:03:05 -04005130}
5131
Yan Zhenga512bbf2008-12-08 16:46:26 -05005132int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5133 u64 chunk_start, u64 physical, u64 devid,
5134 u64 **logical, int *naddrs, int *stripe_len)
5135{
5136 struct extent_map_tree *em_tree = &map_tree->map_tree;
5137 struct extent_map *em;
5138 struct map_lookup *map;
5139 u64 *buf;
5140 u64 bytenr;
5141 u64 length;
5142 u64 stripe_nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005143 u64 rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005144 int i, j, nr = 0;
5145
Chris Mason890871b2009-09-02 16:24:52 -04005146 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005147 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005148 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005149
Josef Bacik835d9742013-03-19 12:13:25 -04005150 if (!em) {
5151 printk(KERN_ERR "btrfs: couldn't find em for chunk %Lu\n",
5152 chunk_start);
5153 return -EIO;
5154 }
5155
5156 if (em->start != chunk_start) {
5157 printk(KERN_ERR "btrfs: bad chunk start, em=%Lu, wanted=%Lu\n",
5158 em->start, chunk_start);
5159 free_extent_map(em);
5160 return -EIO;
5161 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005162 map = (struct map_lookup *)em->bdev;
5163
5164 length = em->len;
David Woodhouse53b381b2013-01-29 18:40:14 -05005165 rmap_len = map->stripe_len;
5166
Yan Zhenga512bbf2008-12-08 16:46:26 -05005167 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
5168 do_div(length, map->num_stripes / map->sub_stripes);
5169 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
5170 do_div(length, map->num_stripes);
David Woodhouse53b381b2013-01-29 18:40:14 -05005171 else if (map->type & (BTRFS_BLOCK_GROUP_RAID5 |
5172 BTRFS_BLOCK_GROUP_RAID6)) {
5173 do_div(length, nr_data_stripes(map));
5174 rmap_len = map->stripe_len * nr_data_stripes(map);
5175 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005176
5177 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005178 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05005179
5180 for (i = 0; i < map->num_stripes; i++) {
5181 if (devid && map->stripes[i].dev->devid != devid)
5182 continue;
5183 if (map->stripes[i].physical > physical ||
5184 map->stripes[i].physical + length <= physical)
5185 continue;
5186
5187 stripe_nr = physical - map->stripes[i].physical;
5188 do_div(stripe_nr, map->stripe_len);
5189
5190 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
5191 stripe_nr = stripe_nr * map->num_stripes + i;
5192 do_div(stripe_nr, map->sub_stripes);
5193 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
5194 stripe_nr = stripe_nr * map->num_stripes + i;
David Woodhouse53b381b2013-01-29 18:40:14 -05005195 } /* else if RAID[56], multiply by nr_data_stripes().
5196 * Alternatively, just use rmap_len below instead of
5197 * map->stripe_len */
5198
5199 bytenr = chunk_start + stripe_nr * rmap_len;
Chris Mason934d3752008-12-08 16:43:10 -05005200 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005201 for (j = 0; j < nr; j++) {
5202 if (buf[j] == bytenr)
5203 break;
5204 }
Chris Mason934d3752008-12-08 16:43:10 -05005205 if (j == nr) {
5206 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05005207 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05005208 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05005209 }
5210
Yan Zhenga512bbf2008-12-08 16:46:26 -05005211 *logical = buf;
5212 *naddrs = nr;
David Woodhouse53b381b2013-01-29 18:40:14 -05005213 *stripe_len = rmap_len;
Yan Zhenga512bbf2008-12-08 16:46:26 -05005214
5215 free_extent_map(em);
5216 return 0;
5217}
5218
Jan Schmidta1d3c472011-08-04 17:15:33 +02005219static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04005220{
Chris Mason9be33952013-05-17 18:30:14 -04005221 struct btrfs_bio *bbio = bio->bi_private;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005222 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04005223
Stefan Behrens442a4f62012-05-25 16:06:08 +02005224 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02005225 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02005226 if (err == -EIO || err == -EREMOTEIO) {
5227 unsigned int stripe_index =
Chris Mason9be33952013-05-17 18:30:14 -04005228 btrfs_io_bio(bio)->stripe_index;
Stefan Behrens442a4f62012-05-25 16:06:08 +02005229 struct btrfs_device *dev;
5230
5231 BUG_ON(stripe_index >= bbio->num_stripes);
5232 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02005233 if (dev->bdev) {
5234 if (bio->bi_rw & WRITE)
5235 btrfs_dev_stat_inc(dev,
5236 BTRFS_DEV_STAT_WRITE_ERRS);
5237 else
5238 btrfs_dev_stat_inc(dev,
5239 BTRFS_DEV_STAT_READ_ERRS);
5240 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
5241 btrfs_dev_stat_inc(dev,
5242 BTRFS_DEV_STAT_FLUSH_ERRS);
5243 btrfs_dev_stat_print_on_error(dev);
5244 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02005245 }
5246 }
Chris Mason8790d502008-04-03 16:29:03 -04005247
Jan Schmidta1d3c472011-08-04 17:15:33 +02005248 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04005249 is_orig_bio = 1;
5250
Jan Schmidta1d3c472011-08-04 17:15:33 +02005251 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04005252 if (!is_orig_bio) {
5253 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02005254 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04005255 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005256 bio->bi_private = bbio->private;
5257 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005258 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04005259 /* only send an error to the higher layers if it is
David Woodhouse53b381b2013-01-29 18:40:14 -05005260 * beyond the tolerance of the btrfs bio
Chris Masona236aed2008-04-29 09:38:00 -04005261 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005262 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04005263 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05005264 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04005265 /*
5266 * this bio is actually up to date, we didn't
5267 * go over the max number of errors
5268 */
5269 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04005270 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04005271 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005272 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04005273
5274 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04005275 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04005276 bio_put(bio);
5277 }
Chris Mason8790d502008-04-03 16:29:03 -04005278}
5279
Chris Mason8b712842008-06-11 16:50:36 -04005280struct async_sched {
5281 struct bio *bio;
5282 int rw;
5283 struct btrfs_fs_info *info;
5284 struct btrfs_work work;
5285};
5286
5287/*
5288 * see run_scheduled_bios for a description of why bios are collected for
5289 * async submit.
5290 *
5291 * This will add one bio to the pending list for a device and make sure
5292 * the work struct is scheduled.
5293 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005294static noinline void btrfs_schedule_bio(struct btrfs_root *root,
5295 struct btrfs_device *device,
5296 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04005297{
5298 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04005299 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005300
David Woodhouse53b381b2013-01-29 18:40:14 -05005301 if (device->missing || !device->bdev) {
5302 bio_endio(bio, -EIO);
5303 return;
5304 }
5305
Chris Mason8b712842008-06-11 16:50:36 -04005306 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005307 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04005308 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01005309 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04005310 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005311 return;
Chris Mason8b712842008-06-11 16:50:36 -04005312 }
5313
5314 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04005315 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04005316 * higher layers. Otherwise, the async bio makes it appear we have
5317 * made progress against dirty pages when we've really just put it
5318 * on a queue for later
5319 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04005320 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04005321 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04005322 bio->bi_next = NULL;
5323 bio->bi_rw |= rw;
5324
5325 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02005326 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04005327 pending_bios = &device->pending_sync_bios;
5328 else
5329 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04005330
Chris Masonffbd5172009-04-20 15:50:09 -04005331 if (pending_bios->tail)
5332 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005333
Chris Masonffbd5172009-04-20 15:50:09 -04005334 pending_bios->tail = bio;
5335 if (!pending_bios->head)
5336 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04005337 if (device->running_pending)
5338 should_queue = 0;
5339
5340 spin_unlock(&device->io_lock);
5341
5342 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04005343 btrfs_queue_worker(&root->fs_info->submit_workers,
5344 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04005345}
5346
Josef Bacikde1ee922012-10-19 16:50:56 -04005347static int bio_size_ok(struct block_device *bdev, struct bio *bio,
5348 sector_t sector)
5349{
5350 struct bio_vec *prev;
5351 struct request_queue *q = bdev_get_queue(bdev);
5352 unsigned short max_sectors = queue_max_sectors(q);
5353 struct bvec_merge_data bvm = {
5354 .bi_bdev = bdev,
5355 .bi_sector = sector,
5356 .bi_rw = bio->bi_rw,
5357 };
5358
5359 if (bio->bi_vcnt == 0) {
5360 WARN_ON(1);
5361 return 1;
5362 }
5363
5364 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005365 if (bio_sectors(bio) > max_sectors)
Josef Bacikde1ee922012-10-19 16:50:56 -04005366 return 0;
5367
5368 if (!q->merge_bvec_fn)
5369 return 1;
5370
5371 bvm.bi_size = bio->bi_size - prev->bv_len;
5372 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
5373 return 0;
5374 return 1;
5375}
5376
5377static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5378 struct bio *bio, u64 physical, int dev_nr,
5379 int rw, int async)
5380{
5381 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
5382
5383 bio->bi_private = bbio;
Chris Mason9be33952013-05-17 18:30:14 -04005384 btrfs_io_bio(bio)->stripe_index = dev_nr;
Josef Bacikde1ee922012-10-19 16:50:56 -04005385 bio->bi_end_io = btrfs_end_bio;
5386 bio->bi_sector = physical >> 9;
5387#ifdef DEBUG
5388 {
5389 struct rcu_string *name;
5390
5391 rcu_read_lock();
5392 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00005393 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04005394 "(%s id %llu), size=%u\n", rw,
5395 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
5396 name->str, dev->devid, bio->bi_size);
5397 rcu_read_unlock();
5398 }
5399#endif
5400 bio->bi_bdev = dev->bdev;
5401 if (async)
David Woodhouse53b381b2013-01-29 18:40:14 -05005402 btrfs_schedule_bio(root, dev, rw, bio);
Josef Bacikde1ee922012-10-19 16:50:56 -04005403 else
5404 btrfsic_submit_bio(rw, bio);
5405}
5406
5407static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
5408 struct bio *first_bio, struct btrfs_device *dev,
5409 int dev_nr, int rw, int async)
5410{
5411 struct bio_vec *bvec = first_bio->bi_io_vec;
5412 struct bio *bio;
5413 int nr_vecs = bio_get_nr_vecs(dev->bdev);
5414 u64 physical = bbio->stripes[dev_nr].physical;
5415
5416again:
5417 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
5418 if (!bio)
5419 return -ENOMEM;
5420
5421 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
5422 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
5423 bvec->bv_offset) < bvec->bv_len) {
5424 u64 len = bio->bi_size;
5425
5426 atomic_inc(&bbio->stripes_pending);
5427 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
5428 rw, async);
5429 physical += len;
5430 goto again;
5431 }
5432 bvec++;
5433 }
5434
5435 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
5436 return 0;
5437}
5438
5439static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5440{
5441 atomic_inc(&bbio->error);
5442 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5443 bio->bi_private = bbio->private;
5444 bio->bi_end_io = bbio->end_io;
Chris Mason9be33952013-05-17 18:30:14 -04005445 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
Josef Bacikde1ee922012-10-19 16:50:56 -04005446 bio->bi_sector = logical >> 9;
5447 kfree(bbio);
5448 bio_endio(bio, -EIO);
5449 }
5450}
5451
Chris Masonf1885912008-04-09 16:28:12 -04005452int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04005453 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04005454{
Chris Mason0b86a832008-03-24 15:01:56 -04005455 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04005456 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04005457 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04005458 u64 length = 0;
5459 u64 map_length;
David Woodhouse53b381b2013-01-29 18:40:14 -05005460 u64 *raid_map = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005461 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04005462 int dev_nr = 0;
5463 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02005464 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005465
Chris Masonf2d8d742008-04-21 10:03:05 -04005466 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04005467 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04005468
David Woodhouse53b381b2013-01-29 18:40:14 -05005469 ret = __btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
5470 mirror_num, &raid_map);
5471 if (ret) /* -ENOMEM */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005472 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04005473
Jan Schmidta1d3c472011-08-04 17:15:33 +02005474 total_devs = bbio->num_stripes;
David Woodhouse53b381b2013-01-29 18:40:14 -05005475 bbio->orig_bio = first_bio;
5476 bbio->private = first_bio->bi_private;
5477 bbio->end_io = first_bio->bi_end_io;
5478 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
5479
5480 if (raid_map) {
5481 /* In this case, map_length has been set to the length of
5482 a single stripe; not the whole write */
5483 if (rw & WRITE) {
5484 return raid56_parity_write(root, bio, bbio,
5485 raid_map, map_length);
5486 } else {
5487 return raid56_parity_recover(root, bio, bbio,
5488 raid_map, map_length,
5489 mirror_num);
5490 }
5491 }
5492
Chris Masoncea9e442008-04-09 16:28:12 -04005493 if (map_length < length) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005494 btrfs_crit(root->fs_info, "mapping failed logical %llu bio len %llu len %llu",
5495 (unsigned long long)logical,
5496 (unsigned long long)length,
5497 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04005498 BUG();
5499 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02005500
Chris Masond3977122009-01-05 21:25:51 -05005501 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04005502 dev = bbio->stripes[dev_nr].dev;
5503 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
5504 bbio_error(bbio, first_bio, logical);
5505 dev_nr++;
5506 continue;
5507 }
5508
5509 /*
5510 * Check and see if we're ok with this bio based on it's size
5511 * and offset with the given device.
5512 */
5513 if (!bio_size_ok(dev->bdev, first_bio,
5514 bbio->stripes[dev_nr].physical >> 9)) {
5515 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
5516 dev_nr, rw, async_submit);
5517 BUG_ON(ret);
5518 dev_nr++;
5519 continue;
5520 }
5521
Jan Schmidta1d3c472011-08-04 17:15:33 +02005522 if (dev_nr < total_devs - 1) {
Chris Mason9be33952013-05-17 18:30:14 -04005523 bio = btrfs_bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005524 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02005525 } else {
5526 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04005527 }
Josef Bacik606686e2012-06-04 14:03:51 -04005528
Josef Bacikde1ee922012-10-19 16:50:56 -04005529 submit_stripe_bio(root, bbio, bio,
5530 bbio->stripes[dev_nr].physical, dev_nr, rw,
5531 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04005532 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04005533 }
Chris Mason0b86a832008-03-24 15:01:56 -04005534 return 0;
5535}
5536
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005537struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05005538 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04005539{
Yan Zheng2b820322008-11-17 21:11:30 -05005540 struct btrfs_device *device;
5541 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04005542
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005543 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005544 while (cur_devices) {
5545 if (!fsid ||
5546 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5547 device = __find_device(&cur_devices->devices,
5548 devid, uuid);
5549 if (device)
5550 return device;
5551 }
5552 cur_devices = cur_devices->seed;
5553 }
5554 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04005555}
5556
Chris Masondfe25022008-05-13 13:46:40 -04005557static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
5558 u64 devid, u8 *dev_uuid)
5559{
5560 struct btrfs_device *device;
5561 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5562
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005563 device = btrfs_alloc_device(NULL, &devid, dev_uuid);
5564 if (IS_ERR(device))
yanhai zhu7cbd8a82008-11-12 14:38:54 -05005565 return NULL;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005566
5567 list_add(&device->dev_list, &fs_devices->devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005568 device->fs_devices = fs_devices;
Chris Masondfe25022008-05-13 13:46:40 -04005569 fs_devices->num_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005570
5571 device->missing = 1;
Chris Masoncd02dca2010-12-13 14:56:23 -05005572 fs_devices->missing_devices++;
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005573
Chris Masondfe25022008-05-13 13:46:40 -04005574 return device;
5575}
5576
Ilya Dryomov12bd2fc2013-08-23 13:20:17 +03005577/**
5578 * btrfs_alloc_device - allocate struct btrfs_device
5579 * @fs_info: used only for generating a new devid, can be NULL if
5580 * devid is provided (i.e. @devid != NULL).
5581 * @devid: a pointer to devid for this device. If NULL a new devid
5582 * is generated.
5583 * @uuid: a pointer to UUID for this device. If NULL a new UUID
5584 * is generated.
5585 *
5586 * Return: a pointer to a new &struct btrfs_device on success; ERR_PTR()
5587 * on error. Returned struct is not linked onto any lists and can be
5588 * destroyed with kfree() right away.
5589 */
5590struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
5591 const u64 *devid,
5592 const u8 *uuid)
5593{
5594 struct btrfs_device *dev;
5595 u64 tmp;
5596
5597 if (!devid && !fs_info) {
5598 WARN_ON(1);
5599 return ERR_PTR(-EINVAL);
5600 }
5601
5602 dev = __alloc_device();
5603 if (IS_ERR(dev))
5604 return dev;
5605
5606 if (devid)
5607 tmp = *devid;
5608 else {
5609 int ret;
5610
5611 ret = find_next_devid(fs_info, &tmp);
5612 if (ret) {
5613 kfree(dev);
5614 return ERR_PTR(ret);
5615 }
5616 }
5617 dev->devid = tmp;
5618
5619 if (uuid)
5620 memcpy(dev->uuid, uuid, BTRFS_UUID_SIZE);
5621 else
5622 generate_random_uuid(dev->uuid);
5623
5624 dev->work.func = pending_bios_fn;
5625
5626 return dev;
5627}
5628
Chris Mason0b86a832008-03-24 15:01:56 -04005629static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5630 struct extent_buffer *leaf,
5631 struct btrfs_chunk *chunk)
5632{
5633 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
5634 struct map_lookup *map;
5635 struct extent_map *em;
5636 u64 logical;
5637 u64 length;
5638 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04005639 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04005640 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04005641 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04005642 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04005643
Chris Masone17cade2008-04-15 15:41:47 -04005644 logical = key->offset;
5645 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04005646
Chris Mason890871b2009-09-02 16:24:52 -04005647 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005648 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04005649 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005650
5651 /* already mapped? */
5652 if (em && em->start <= logical && em->start + em->len > logical) {
5653 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04005654 return 0;
5655 } else if (em) {
5656 free_extent_map(em);
5657 }
Chris Mason0b86a832008-03-24 15:01:56 -04005658
David Sterba172ddd62011-04-21 00:48:27 +02005659 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04005660 if (!em)
5661 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04005662 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
5663 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04005664 if (!map) {
5665 free_extent_map(em);
5666 return -ENOMEM;
5667 }
5668
5669 em->bdev = (struct block_device *)map;
5670 em->start = logical;
5671 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04005672 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005673 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04005674 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04005675
Chris Mason593060d2008-03-25 16:50:33 -04005676 map->num_stripes = num_stripes;
5677 map->io_width = btrfs_chunk_io_width(leaf, chunk);
5678 map->io_align = btrfs_chunk_io_align(leaf, chunk);
5679 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
5680 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
5681 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04005682 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04005683 for (i = 0; i < num_stripes; i++) {
5684 map->stripes[i].physical =
5685 btrfs_stripe_offset_nr(leaf, chunk, i);
5686 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04005687 read_extent_buffer(leaf, uuid, (unsigned long)
5688 btrfs_stripe_dev_uuid_nr(chunk, i),
5689 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005690 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5691 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005692 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005693 kfree(map);
5694 free_extent_map(em);
5695 return -EIO;
5696 }
Chris Masondfe25022008-05-13 13:46:40 -04005697 if (!map->stripes[i].dev) {
5698 map->stripes[i].dev =
5699 add_missing_dev(root, devid, uuid);
5700 if (!map->stripes[i].dev) {
5701 kfree(map);
5702 free_extent_map(em);
5703 return -EIO;
5704 }
5705 }
5706 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005707 }
5708
Chris Mason890871b2009-09-02 16:24:52 -04005709 write_lock(&map_tree->map_tree.lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04005710 ret = add_extent_mapping(&map_tree->map_tree, em, 0);
Chris Mason890871b2009-09-02 16:24:52 -04005711 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005712 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005713 free_extent_map(em);
5714
5715 return 0;
5716}
5717
Jeff Mahoney143bede2012-03-01 14:56:26 +01005718static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005719 struct btrfs_dev_item *dev_item,
5720 struct btrfs_device *device)
5721{
5722 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005723
5724 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005725 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5726 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005727 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5728 device->type = btrfs_device_type(leaf, dev_item);
5729 device->io_align = btrfs_device_io_align(leaf, dev_item);
5730 device->io_width = btrfs_device_io_width(leaf, dev_item);
5731 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005732 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005733 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005734
5735 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005736 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005737}
5738
Yan Zheng2b820322008-11-17 21:11:30 -05005739static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5740{
5741 struct btrfs_fs_devices *fs_devices;
5742 int ret;
5743
Li Zefanb367e472011-12-07 11:38:24 +08005744 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005745
5746 fs_devices = root->fs_info->fs_devices->seed;
5747 while (fs_devices) {
5748 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5749 ret = 0;
5750 goto out;
5751 }
5752 fs_devices = fs_devices->seed;
5753 }
5754
5755 fs_devices = find_fsid(fsid);
5756 if (!fs_devices) {
5757 ret = -ENOENT;
5758 goto out;
5759 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005760
5761 fs_devices = clone_fs_devices(fs_devices);
5762 if (IS_ERR(fs_devices)) {
5763 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005764 goto out;
5765 }
5766
Christoph Hellwig97288f22008-12-02 06:36:09 -05005767 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005768 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005769 if (ret) {
5770 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005771 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005772 }
Yan Zheng2b820322008-11-17 21:11:30 -05005773
5774 if (!fs_devices->seeding) {
5775 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005776 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005777 ret = -EINVAL;
5778 goto out;
5779 }
5780
5781 fs_devices->seed = root->fs_info->fs_devices->seed;
5782 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005783out:
Yan Zheng2b820322008-11-17 21:11:30 -05005784 return ret;
5785}
5786
Chris Mason0d81ba52008-03-24 15:02:07 -04005787static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005788 struct extent_buffer *leaf,
5789 struct btrfs_dev_item *dev_item)
5790{
5791 struct btrfs_device *device;
5792 u64 devid;
5793 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005794 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005795 u8 dev_uuid[BTRFS_UUID_SIZE];
5796
Chris Mason0b86a832008-03-24 15:01:56 -04005797 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005798 read_extent_buffer(leaf, dev_uuid,
5799 (unsigned long)btrfs_device_uuid(dev_item),
5800 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005801 read_extent_buffer(leaf, fs_uuid,
5802 (unsigned long)btrfs_device_fsid(dev_item),
5803 BTRFS_UUID_SIZE);
5804
5805 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5806 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005807 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005808 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005809 }
5810
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005811 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005812 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005813 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005814 return -EIO;
5815
5816 if (!device) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005817 btrfs_warn(root->fs_info, "devid %llu missing",
5818 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005819 device = add_missing_dev(root, devid, dev_uuid);
5820 if (!device)
5821 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005822 } else if (!device->missing) {
5823 /*
5824 * this happens when a device that was properly setup
5825 * in the device info lists suddenly goes bad.
5826 * device->bdev is NULL, and so we have to set
5827 * device->missing to one here
5828 */
5829 root->fs_info->fs_devices->missing_devices++;
5830 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005831 }
5832 }
5833
5834 if (device->fs_devices != root->fs_info->fs_devices) {
5835 BUG_ON(device->writeable);
5836 if (device->generation !=
5837 btrfs_device_generation(leaf, dev_item))
5838 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005839 }
Chris Mason0b86a832008-03-24 15:01:56 -04005840
5841 fill_device_from_item(leaf, dev_item, device);
Chris Masondfe25022008-05-13 13:46:40 -04005842 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005843 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005844 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005845 spin_lock(&root->fs_info->free_chunk_lock);
5846 root->fs_info->free_chunk_space += device->total_bytes -
5847 device->bytes_used;
5848 spin_unlock(&root->fs_info->free_chunk_lock);
5849 }
Chris Mason0b86a832008-03-24 15:01:56 -04005850 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005851 return ret;
5852}
5853
Yan Zhenge4404d62008-12-12 10:03:26 -05005854int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005855{
David Sterba6c417612011-04-13 15:41:04 +02005856 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005857 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005858 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005859 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005860 u8 *ptr;
5861 unsigned long sb_ptr;
5862 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005863 u32 num_stripes;
5864 u32 array_size;
5865 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005866 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005867 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005868
Yan Zhenge4404d62008-12-12 10:03:26 -05005869 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005870 BTRFS_SUPER_INFO_SIZE);
5871 if (!sb)
5872 return -ENOMEM;
5873 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005874 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005875 /*
5876 * The sb extent buffer is artifical and just used to read the system array.
5877 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5878 * pages up-to-date when the page is larger: extent does not cover the
5879 * whole page and consequently check_page_uptodate does not find all
5880 * the page's extents up-to-date (the hole beyond sb),
5881 * write_extent_buffer then triggers a WARN_ON.
5882 *
5883 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5884 * but sb spans only this function. Add an explicit SetPageUptodate call
5885 * to silence the warning eg. on PowerPC 64.
5886 */
5887 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005888 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005889
Chris Masona061fc82008-05-07 11:43:44 -04005890 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005891 array_size = btrfs_super_sys_array_size(super_copy);
5892
Chris Mason0b86a832008-03-24 15:01:56 -04005893 ptr = super_copy->sys_chunk_array;
5894 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5895 cur = 0;
5896
5897 while (cur < array_size) {
5898 disk_key = (struct btrfs_disk_key *)ptr;
5899 btrfs_disk_key_to_cpu(&key, disk_key);
5900
Chris Masona061fc82008-05-07 11:43:44 -04005901 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005902 sb_ptr += len;
5903 cur += len;
5904
Chris Mason0d81ba52008-03-24 15:02:07 -04005905 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005906 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005907 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005908 if (ret)
5909 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005910 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5911 len = btrfs_chunk_item_size(num_stripes);
5912 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005913 ret = -EIO;
5914 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005915 }
5916 ptr += len;
5917 sb_ptr += len;
5918 cur += len;
5919 }
Chris Masona061fc82008-05-07 11:43:44 -04005920 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005921 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005922}
5923
5924int btrfs_read_chunk_tree(struct btrfs_root *root)
5925{
5926 struct btrfs_path *path;
5927 struct extent_buffer *leaf;
5928 struct btrfs_key key;
5929 struct btrfs_key found_key;
5930 int ret;
5931 int slot;
5932
5933 root = root->fs_info->chunk_root;
5934
5935 path = btrfs_alloc_path();
5936 if (!path)
5937 return -ENOMEM;
5938
Li Zefanb367e472011-12-07 11:38:24 +08005939 mutex_lock(&uuid_mutex);
5940 lock_chunks(root);
5941
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01005942 /*
5943 * Read all device items, and then all the chunk items. All
5944 * device items are found before any chunk item (their object id
5945 * is smaller than the lowest possible object id for a chunk
5946 * item - BTRFS_FIRST_CHUNK_TREE_OBJECTID).
Chris Mason0b86a832008-03-24 15:01:56 -04005947 */
5948 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5949 key.offset = 0;
5950 key.type = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005951 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005952 if (ret < 0)
5953 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005954 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005955 leaf = path->nodes[0];
5956 slot = path->slots[0];
5957 if (slot >= btrfs_header_nritems(leaf)) {
5958 ret = btrfs_next_leaf(root, path);
5959 if (ret == 0)
5960 continue;
5961 if (ret < 0)
5962 goto error;
5963 break;
5964 }
5965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01005966 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5967 struct btrfs_dev_item *dev_item;
5968 dev_item = btrfs_item_ptr(leaf, slot,
Chris Mason0b86a832008-03-24 15:01:56 -04005969 struct btrfs_dev_item);
Filipe David Borba Manana395927a2013-07-30 12:03:04 +01005970 ret = read_one_dev(root, leaf, dev_item);
5971 if (ret)
5972 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005973 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5974 struct btrfs_chunk *chunk;
5975 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5976 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005977 if (ret)
5978 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005979 }
5980 path->slots[0]++;
5981 }
Chris Mason0b86a832008-03-24 15:01:56 -04005982 ret = 0;
5983error:
Li Zefanb367e472011-12-07 11:38:24 +08005984 unlock_chunks(root);
5985 mutex_unlock(&uuid_mutex);
5986
Yan Zheng2b820322008-11-17 21:11:30 -05005987 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005988 return ret;
5989}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005990
Miao Xiecb517ea2013-05-15 07:48:19 +00005991void btrfs_init_devices_late(struct btrfs_fs_info *fs_info)
5992{
5993 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5994 struct btrfs_device *device;
5995
5996 mutex_lock(&fs_devices->device_list_mutex);
5997 list_for_each_entry(device, &fs_devices->devices, dev_list)
5998 device->dev_root = fs_info->dev_root;
5999 mutex_unlock(&fs_devices->device_list_mutex);
6000}
6001
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006002static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
6003{
6004 int i;
6005
6006 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6007 btrfs_dev_stat_reset(dev, i);
6008}
6009
6010int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
6011{
6012 struct btrfs_key key;
6013 struct btrfs_key found_key;
6014 struct btrfs_root *dev_root = fs_info->dev_root;
6015 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6016 struct extent_buffer *eb;
6017 int slot;
6018 int ret = 0;
6019 struct btrfs_device *device;
6020 struct btrfs_path *path = NULL;
6021 int i;
6022
6023 path = btrfs_alloc_path();
6024 if (!path) {
6025 ret = -ENOMEM;
6026 goto out;
6027 }
6028
6029 mutex_lock(&fs_devices->device_list_mutex);
6030 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6031 int item_size;
6032 struct btrfs_dev_stats_item *ptr;
6033
6034 key.objectid = 0;
6035 key.type = BTRFS_DEV_STATS_KEY;
6036 key.offset = device->devid;
6037 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
6038 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006039 __btrfs_reset_dev_stats(device);
6040 device->dev_stats_valid = 1;
6041 btrfs_release_path(path);
6042 continue;
6043 }
6044 slot = path->slots[0];
6045 eb = path->nodes[0];
6046 btrfs_item_key_to_cpu(eb, &found_key, slot);
6047 item_size = btrfs_item_size_nr(eb, slot);
6048
6049 ptr = btrfs_item_ptr(eb, slot,
6050 struct btrfs_dev_stats_item);
6051
6052 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6053 if (item_size >= (1 + i) * sizeof(__le64))
6054 btrfs_dev_stat_set(device, i,
6055 btrfs_dev_stats_value(eb, ptr, i));
6056 else
6057 btrfs_dev_stat_reset(device, i);
6058 }
6059
6060 device->dev_stats_valid = 1;
6061 btrfs_dev_stat_print_on_load(device);
6062 btrfs_release_path(path);
6063 }
6064 mutex_unlock(&fs_devices->device_list_mutex);
6065
6066out:
6067 btrfs_free_path(path);
6068 return ret < 0 ? ret : 0;
6069}
6070
6071static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6072 struct btrfs_root *dev_root,
6073 struct btrfs_device *device)
6074{
6075 struct btrfs_path *path;
6076 struct btrfs_key key;
6077 struct extent_buffer *eb;
6078 struct btrfs_dev_stats_item *ptr;
6079 int ret;
6080 int i;
6081
6082 key.objectid = 0;
6083 key.type = BTRFS_DEV_STATS_KEY;
6084 key.offset = device->devid;
6085
6086 path = btrfs_alloc_path();
6087 BUG_ON(!path);
6088 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6089 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04006090 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
6091 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006092 goto out;
6093 }
6094
6095 if (ret == 0 &&
6096 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
6097 /* need to delete old one and insert a new one */
6098 ret = btrfs_del_item(trans, dev_root, path);
6099 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04006100 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
6101 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006102 goto out;
6103 }
6104 ret = 1;
6105 }
6106
6107 if (ret == 1) {
6108 /* need to insert a new item */
6109 btrfs_release_path(path);
6110 ret = btrfs_insert_empty_item(trans, dev_root, path,
6111 &key, sizeof(*ptr));
6112 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04006113 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
6114 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006115 goto out;
6116 }
6117 }
6118
6119 eb = path->nodes[0];
6120 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
6121 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6122 btrfs_set_dev_stats_value(eb, ptr, i,
6123 btrfs_dev_stat_read(device, i));
6124 btrfs_mark_buffer_dirty(eb);
6125
6126out:
6127 btrfs_free_path(path);
6128 return ret;
6129}
6130
6131/*
6132 * called from commit_transaction. Writes all changed device stats to disk.
6133 */
6134int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
6135 struct btrfs_fs_info *fs_info)
6136{
6137 struct btrfs_root *dev_root = fs_info->dev_root;
6138 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6139 struct btrfs_device *device;
6140 int ret = 0;
6141
6142 mutex_lock(&fs_devices->device_list_mutex);
6143 list_for_each_entry(device, &fs_devices->devices, dev_list) {
6144 if (!device->dev_stats_valid || !device->dev_stats_dirty)
6145 continue;
6146
6147 ret = update_dev_stat_item(trans, dev_root, device);
6148 if (!ret)
6149 device->dev_stats_dirty = 0;
6150 }
6151 mutex_unlock(&fs_devices->device_list_mutex);
6152
6153 return ret;
6154}
6155
Stefan Behrens442a4f62012-05-25 16:06:08 +02006156void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
6157{
6158 btrfs_dev_stat_inc(dev, index);
6159 btrfs_dev_stat_print_on_error(dev);
6160}
6161
Eric Sandeen48a3b632013-04-25 20:41:01 +00006162static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
Stefan Behrens442a4f62012-05-25 16:06:08 +02006163{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006164 if (!dev->dev_stats_valid)
6165 return;
Josef Bacik606686e2012-06-04 14:03:51 -04006166 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02006167 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04006168 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02006169 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6170 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6171 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6172 btrfs_dev_stat_read(dev,
6173 BTRFS_DEV_STAT_CORRUPTION_ERRS),
6174 btrfs_dev_stat_read(dev,
6175 BTRFS_DEV_STAT_GENERATION_ERRS));
6176}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006177
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006178static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
6179{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06006180 int i;
6181
6182 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6183 if (btrfs_dev_stat_read(dev, i) != 0)
6184 break;
6185 if (i == BTRFS_DEV_STAT_VALUES_MAX)
6186 return; /* all values == 0, suppress message */
6187
Josef Bacik606686e2012-06-04 14:03:51 -04006188 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
6189 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006190 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
6191 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
6192 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
6193 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
6194 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
6195}
6196
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006197int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06006198 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006199{
6200 struct btrfs_device *dev;
6201 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6202 int i;
6203
6204 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01006205 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006206 mutex_unlock(&fs_devices->device_list_mutex);
6207
6208 if (!dev) {
6209 printk(KERN_WARNING
6210 "btrfs: get dev_stats failed, device not found\n");
6211 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02006212 } else if (!dev->dev_stats_valid) {
6213 printk(KERN_WARNING
6214 "btrfs: get dev_stats failed, not yet valid\n");
6215 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06006216 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02006217 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
6218 if (stats->nr_items > i)
6219 stats->values[i] =
6220 btrfs_dev_stat_read_and_reset(dev, i);
6221 else
6222 btrfs_dev_stat_reset(dev, i);
6223 }
6224 } else {
6225 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
6226 if (stats->nr_items > i)
6227 stats->values[i] = btrfs_dev_stat_read(dev, i);
6228 }
6229 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
6230 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
6231 return 0;
6232}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01006233
6234int btrfs_scratch_superblock(struct btrfs_device *device)
6235{
6236 struct buffer_head *bh;
6237 struct btrfs_super_block *disk_super;
6238
6239 bh = btrfs_read_dev_super(device->bdev);
6240 if (!bh)
6241 return -EINVAL;
6242 disk_super = (struct btrfs_super_block *)bh->b_data;
6243
6244 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6245 set_buffer_dirty(bh);
6246 sync_dirty_buffer(bh);
6247 brelse(bh);
6248
6249 return 0;
6250}