blob: 485a5423e3c6152fd752c4f7aab9f5ee9911a3e0 [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>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason0b86a832008-03-24 15:01:56 -040029#include "ctree.h"
30#include "extent_map.h"
31#include "disk-io.h"
32#include "transaction.h"
33#include "print-tree.h"
34#include "volumes.h"
Chris Mason8b712842008-06-11 16:50:36 -040035#include "async-thread.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010036#include "check-integrity.h"
Josef Bacik606686e2012-06-04 14:03:51 -040037#include "rcu-string.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060038#include "math.h"
Stefan Behrens8dabb742012-11-06 13:15:27 +010039#include "dev-replace.h"
Chris Mason0b86a832008-03-24 15:01:56 -040040
Yan Zheng2b820322008-11-17 21:11:30 -050041static int init_first_rw_device(struct btrfs_trans_handle *trans,
42 struct btrfs_root *root,
43 struct btrfs_device *device);
44static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
Stefan Behrens733f4fb2012-05-25 16:06:10 +020045static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
46static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
Yan Zheng2b820322008-11-17 21:11:30 -050047
Chris Mason8a4b83c2008-03-24 15:02:07 -040048static DEFINE_MUTEX(uuid_mutex);
49static LIST_HEAD(fs_uuids);
50
Chris Mason7d9eb122008-07-08 14:19:17 -040051static void lock_chunks(struct btrfs_root *root)
52{
Chris Mason7d9eb122008-07-08 14:19:17 -040053 mutex_lock(&root->fs_info->chunk_mutex);
54}
55
56static void unlock_chunks(struct btrfs_root *root)
57{
Chris Mason7d9eb122008-07-08 14:19:17 -040058 mutex_unlock(&root->fs_info->chunk_mutex);
59}
60
Yan Zhenge4404d62008-12-12 10:03:26 -050061static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
62{
63 struct btrfs_device *device;
64 WARN_ON(fs_devices->opened);
65 while (!list_empty(&fs_devices->devices)) {
66 device = list_entry(fs_devices->devices.next,
67 struct btrfs_device, dev_list);
68 list_del(&device->dev_list);
Josef Bacik606686e2012-06-04 14:03:51 -040069 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -050070 kfree(device);
71 }
72 kfree(fs_devices);
73}
74
Lukas Czernerb8b8ff52012-12-06 19:25:48 +000075static void btrfs_kobject_uevent(struct block_device *bdev,
76 enum kobject_action action)
77{
78 int ret;
79
80 ret = kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, action);
81 if (ret)
82 pr_warn("Sending event '%d' to kobject: '%s' (%p): failed\n",
83 action,
84 kobject_name(&disk_to_dev(bdev->bd_disk)->kobj),
85 &disk_to_dev(bdev->bd_disk)->kobj);
86}
87
Jeff Mahoney143bede2012-03-01 14:56:26 +010088void btrfs_cleanup_fs_uuids(void)
Chris Mason8a4b83c2008-03-24 15:02:07 -040089{
90 struct btrfs_fs_devices *fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -040091
Yan Zheng2b820322008-11-17 21:11:30 -050092 while (!list_empty(&fs_uuids)) {
93 fs_devices = list_entry(fs_uuids.next,
94 struct btrfs_fs_devices, list);
95 list_del(&fs_devices->list);
Yan Zhenge4404d62008-12-12 10:03:26 -050096 free_fs_devices(fs_devices);
Chris Mason8a4b83c2008-03-24 15:02:07 -040097 }
Chris Mason8a4b83c2008-03-24 15:02:07 -040098}
99
Chris Masona1b32a52008-09-05 16:09:51 -0400100static noinline struct btrfs_device *__find_device(struct list_head *head,
101 u64 devid, u8 *uuid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400102{
103 struct btrfs_device *dev;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400104
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500105 list_for_each_entry(dev, head, dev_list) {
Chris Masona4437552008-04-18 10:29:38 -0400106 if (dev->devid == devid &&
Chris Mason8f18cf12008-04-25 16:53:30 -0400107 (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400108 return dev;
Chris Masona4437552008-04-18 10:29:38 -0400109 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400110 }
111 return NULL;
112}
113
Chris Masona1b32a52008-09-05 16:09:51 -0400114static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400115{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400116 struct btrfs_fs_devices *fs_devices;
117
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500118 list_for_each_entry(fs_devices, &fs_uuids, list) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400119 if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
120 return fs_devices;
121 }
122 return NULL;
123}
124
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100125static int
126btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
127 int flush, struct block_device **bdev,
128 struct buffer_head **bh)
129{
130 int ret;
131
132 *bdev = blkdev_get_by_path(device_path, flags, holder);
133
134 if (IS_ERR(*bdev)) {
135 ret = PTR_ERR(*bdev);
136 printk(KERN_INFO "btrfs: open %s failed\n", device_path);
137 goto error;
138 }
139
140 if (flush)
141 filemap_write_and_wait((*bdev)->bd_inode->i_mapping);
142 ret = set_blocksize(*bdev, 4096);
143 if (ret) {
144 blkdev_put(*bdev, flags);
145 goto error;
146 }
147 invalidate_bdev(*bdev);
148 *bh = btrfs_read_dev_super(*bdev);
149 if (!*bh) {
150 ret = -EINVAL;
151 blkdev_put(*bdev, flags);
152 goto error;
153 }
154
155 return 0;
156
157error:
158 *bdev = NULL;
159 *bh = NULL;
160 return ret;
161}
162
Chris Masonffbd5172009-04-20 15:50:09 -0400163static void requeue_list(struct btrfs_pending_bios *pending_bios,
164 struct bio *head, struct bio *tail)
165{
166
167 struct bio *old_head;
168
169 old_head = pending_bios->head;
170 pending_bios->head = head;
171 if (pending_bios->tail)
172 tail->bi_next = old_head;
173 else
174 pending_bios->tail = tail;
175}
176
Chris Mason8b712842008-06-11 16:50:36 -0400177/*
178 * we try to collect pending bios for a device so we don't get a large
179 * number of procs sending bios down to the same device. This greatly
180 * improves the schedulers ability to collect and merge the bios.
181 *
182 * But, it also turns into a long list of bios to process and that is sure
183 * to eventually make the worker thread block. The solution here is to
184 * make some progress and then put this work struct back at the end of
185 * the list if the block device is congested. This way, multiple devices
186 * can make progress from a single worker thread.
187 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100188static noinline void run_scheduled_bios(struct btrfs_device *device)
Chris Mason8b712842008-06-11 16:50:36 -0400189{
190 struct bio *pending;
191 struct backing_dev_info *bdi;
Chris Masonb64a2852008-08-20 13:39:41 -0400192 struct btrfs_fs_info *fs_info;
Chris Masonffbd5172009-04-20 15:50:09 -0400193 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -0400194 struct bio *tail;
195 struct bio *cur;
196 int again = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400197 unsigned long num_run;
Chris Masond644d8a2009-06-09 15:59:22 -0400198 unsigned long batch_run = 0;
Chris Masonb64a2852008-08-20 13:39:41 -0400199 unsigned long limit;
Chris Masonb765ead2009-04-03 10:27:10 -0400200 unsigned long last_waited = 0;
Chris Masond84275c2009-06-09 15:39:08 -0400201 int force_reg = 0;
Miao Xie0e588852011-08-05 09:32:37 +0000202 int sync_pending = 0;
Chris Mason211588a2011-04-19 20:12:40 -0400203 struct blk_plug plug;
204
205 /*
206 * this function runs all the bios we've collected for
207 * a particular device. We don't want to wander off to
208 * another device without first sending all of these down.
209 * So, setup a plug here and finish it off before we return
210 */
211 blk_start_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400212
Chris Masonbedf7622009-04-03 10:32:58 -0400213 bdi = blk_get_backing_dev_info(device->bdev);
Chris Masonb64a2852008-08-20 13:39:41 -0400214 fs_info = device->dev_root->fs_info;
215 limit = btrfs_async_submit_limit(fs_info);
216 limit = limit * 2 / 3;
217
Chris Mason8b712842008-06-11 16:50:36 -0400218loop:
219 spin_lock(&device->io_lock);
220
Chris Masona6837052009-02-04 09:19:41 -0500221loop_lock:
Chris Masond84275c2009-06-09 15:39:08 -0400222 num_run = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400223
Chris Mason8b712842008-06-11 16:50:36 -0400224 /* take all the bios off the list at once and process them
225 * later on (without the lock held). But, remember the
226 * tail and other pointers so the bios can be properly reinserted
227 * into the list if we hit congestion
228 */
Chris Masond84275c2009-06-09 15:39:08 -0400229 if (!force_reg && device->pending_sync_bios.head) {
Chris Masonffbd5172009-04-20 15:50:09 -0400230 pending_bios = &device->pending_sync_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400231 force_reg = 1;
232 } else {
Chris Masonffbd5172009-04-20 15:50:09 -0400233 pending_bios = &device->pending_bios;
Chris Masond84275c2009-06-09 15:39:08 -0400234 force_reg = 0;
235 }
Chris Masonffbd5172009-04-20 15:50:09 -0400236
237 pending = pending_bios->head;
238 tail = pending_bios->tail;
Chris Mason8b712842008-06-11 16:50:36 -0400239 WARN_ON(pending && !tail);
Chris Mason8b712842008-06-11 16:50:36 -0400240
241 /*
242 * if pending was null this time around, no bios need processing
243 * at all and we can stop. Otherwise it'll loop back up again
244 * and do an additional check so no bios are missed.
245 *
246 * device->running_pending is used to synchronize with the
247 * schedule_bio code.
248 */
Chris Masonffbd5172009-04-20 15:50:09 -0400249 if (device->pending_sync_bios.head == NULL &&
250 device->pending_bios.head == NULL) {
Chris Mason8b712842008-06-11 16:50:36 -0400251 again = 0;
252 device->running_pending = 0;
Chris Masonffbd5172009-04-20 15:50:09 -0400253 } else {
254 again = 1;
255 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400256 }
Chris Masonffbd5172009-04-20 15:50:09 -0400257
258 pending_bios->head = NULL;
259 pending_bios->tail = NULL;
260
Chris Mason8b712842008-06-11 16:50:36 -0400261 spin_unlock(&device->io_lock);
262
Chris Masond3977122009-01-05 21:25:51 -0500263 while (pending) {
Chris Masonffbd5172009-04-20 15:50:09 -0400264
265 rmb();
Chris Masond84275c2009-06-09 15:39:08 -0400266 /* we want to work on both lists, but do more bios on the
267 * sync list than the regular list
268 */
269 if ((num_run > 32 &&
270 pending_bios != &device->pending_sync_bios &&
271 device->pending_sync_bios.head) ||
272 (num_run > 64 && pending_bios == &device->pending_sync_bios &&
273 device->pending_bios.head)) {
Chris Masonffbd5172009-04-20 15:50:09 -0400274 spin_lock(&device->io_lock);
275 requeue_list(pending_bios, pending, tail);
276 goto loop_lock;
277 }
278
Chris Mason8b712842008-06-11 16:50:36 -0400279 cur = pending;
280 pending = pending->bi_next;
281 cur->bi_next = NULL;
Chris Masonb64a2852008-08-20 13:39:41 -0400282
Josef Bacik66657b32012-08-01 15:36:24 -0400283 if (atomic_dec_return(&fs_info->nr_async_bios) < limit &&
Chris Masonb64a2852008-08-20 13:39:41 -0400284 waitqueue_active(&fs_info->async_submit_wait))
285 wake_up(&fs_info->async_submit_wait);
Chris Mason492bb6de2008-07-31 16:29:02 -0400286
287 BUG_ON(atomic_read(&cur->bi_cnt) == 0);
Chris Masond644d8a2009-06-09 15:59:22 -0400288
Chris Mason2ab1ba62011-08-04 14:28:36 -0400289 /*
290 * if we're doing the sync list, record that our
291 * plug has some sync requests on it
292 *
293 * If we're doing the regular list and there are
294 * sync requests sitting around, unplug before
295 * we add more
296 */
297 if (pending_bios == &device->pending_sync_bios) {
298 sync_pending = 1;
299 } else if (sync_pending) {
300 blk_finish_plug(&plug);
301 blk_start_plug(&plug);
302 sync_pending = 0;
303 }
304
Stefan Behrens21adbd52011-11-09 13:44:05 +0100305 btrfsic_submit_bio(cur->bi_rw, cur);
Chris Mason5ff7ba32010-03-15 10:21:30 -0400306 num_run++;
307 batch_run++;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100308 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400309 cond_resched();
Chris Mason8b712842008-06-11 16:50:36 -0400310
311 /*
312 * we made progress, there is more work to do and the bdi
313 * is now congested. Back off and let other work structs
314 * run instead
315 */
Chris Mason57fd5a52009-08-07 09:59:15 -0400316 if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
Chris Mason5f2cc082008-11-07 18:22:45 -0500317 fs_info->fs_devices->open_devices > 1) {
Chris Masonb765ead2009-04-03 10:27:10 -0400318 struct io_context *ioc;
Chris Mason8b712842008-06-11 16:50:36 -0400319
Chris Masonb765ead2009-04-03 10:27:10 -0400320 ioc = current->io_context;
321
322 /*
323 * the main goal here is that we don't want to
324 * block if we're going to be able to submit
325 * more requests without blocking.
326 *
327 * This code does two great things, it pokes into
328 * the elevator code from a filesystem _and_
329 * it makes assumptions about how batching works.
330 */
331 if (ioc && ioc->nr_batch_requests > 0 &&
332 time_before(jiffies, ioc->last_waited + HZ/50UL) &&
333 (last_waited == 0 ||
334 ioc->last_waited == last_waited)) {
335 /*
336 * we want to go through our batch of
337 * requests and stop. So, we copy out
338 * the ioc->last_waited time and test
339 * against it before looping
340 */
341 last_waited = ioc->last_waited;
Jens Axboe7eaceac2011-03-10 08:52:07 +0100342 if (need_resched())
Chris Masonffbd5172009-04-20 15:50:09 -0400343 cond_resched();
Chris Masonb765ead2009-04-03 10:27:10 -0400344 continue;
345 }
Chris Mason8b712842008-06-11 16:50:36 -0400346 spin_lock(&device->io_lock);
Chris Masonffbd5172009-04-20 15:50:09 -0400347 requeue_list(pending_bios, pending, tail);
Chris Masona6837052009-02-04 09:19:41 -0500348 device->running_pending = 1;
Chris Mason8b712842008-06-11 16:50:36 -0400349
350 spin_unlock(&device->io_lock);
351 btrfs_requeue_work(&device->work);
352 goto done;
353 }
Chris Masond85c8a6f2011-12-15 15:38:41 -0500354 /* unplug every 64 requests just for good measure */
355 if (batch_run % 64 == 0) {
356 blk_finish_plug(&plug);
357 blk_start_plug(&plug);
358 sync_pending = 0;
359 }
Chris Mason8b712842008-06-11 16:50:36 -0400360 }
Chris Masonffbd5172009-04-20 15:50:09 -0400361
Chris Mason51684082010-03-10 15:33:32 -0500362 cond_resched();
363 if (again)
364 goto loop;
365
366 spin_lock(&device->io_lock);
367 if (device->pending_bios.head || device->pending_sync_bios.head)
368 goto loop_lock;
369 spin_unlock(&device->io_lock);
370
Chris Mason8b712842008-06-11 16:50:36 -0400371done:
Chris Mason211588a2011-04-19 20:12:40 -0400372 blk_finish_plug(&plug);
Chris Mason8b712842008-06-11 16:50:36 -0400373}
374
Christoph Hellwigb2950862008-12-02 09:54:17 -0500375static void pending_bios_fn(struct btrfs_work *work)
Chris Mason8b712842008-06-11 16:50:36 -0400376{
377 struct btrfs_device *device;
378
379 device = container_of(work, struct btrfs_device, work);
380 run_scheduled_bios(device);
381}
382
Chris Masona1b32a52008-09-05 16:09:51 -0400383static noinline int device_list_add(const char *path,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400384 struct btrfs_super_block *disk_super,
385 u64 devid, struct btrfs_fs_devices **fs_devices_ret)
386{
387 struct btrfs_device *device;
388 struct btrfs_fs_devices *fs_devices;
Josef Bacik606686e2012-06-04 14:03:51 -0400389 struct rcu_string *name;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400390 u64 found_transid = btrfs_super_generation(disk_super);
391
392 fs_devices = find_fsid(disk_super->fsid);
393 if (!fs_devices) {
Chris Mason515dc322008-05-16 13:30:15 -0400394 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400395 if (!fs_devices)
396 return -ENOMEM;
397 INIT_LIST_HEAD(&fs_devices->devices);
Chris Masonb3075712008-04-22 09:22:07 -0400398 INIT_LIST_HEAD(&fs_devices->alloc_list);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400399 list_add(&fs_devices->list, &fs_uuids);
400 memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
401 fs_devices->latest_devid = devid;
402 fs_devices->latest_trans = found_transid;
Chris Masone5e9a522009-06-10 15:17:02 -0400403 mutex_init(&fs_devices->device_list_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400404 device = NULL;
405 } else {
Chris Masona4437552008-04-18 10:29:38 -0400406 device = __find_device(&fs_devices->devices, devid,
407 disk_super->dev_item.uuid);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400408 }
409 if (!device) {
Yan Zheng2b820322008-11-17 21:11:30 -0500410 if (fs_devices->opened)
411 return -EBUSY;
412
Chris Mason8a4b83c2008-03-24 15:02:07 -0400413 device = kzalloc(sizeof(*device), GFP_NOFS);
414 if (!device) {
415 /* we can safely leave the fs_devices entry around */
416 return -ENOMEM;
417 }
418 device->devid = devid;
Stefan Behrens733f4fb2012-05-25 16:06:10 +0200419 device->dev_stats_valid = 0;
Chris Mason8b712842008-06-11 16:50:36 -0400420 device->work.func = pending_bios_fn;
Chris Masona4437552008-04-18 10:29:38 -0400421 memcpy(device->uuid, disk_super->dev_item.uuid,
422 BTRFS_UUID_SIZE);
Chris Masonb248a412008-04-14 09:48:18 -0400423 spin_lock_init(&device->io_lock);
Josef Bacik606686e2012-06-04 14:03:51 -0400424
425 name = rcu_string_strdup(path, GFP_NOFS);
426 if (!name) {
Chris Mason8a4b83c2008-03-24 15:02:07 -0400427 kfree(device);
428 return -ENOMEM;
429 }
Josef Bacik606686e2012-06-04 14:03:51 -0400430 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -0500431 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -0400432
Arne Jansen90519d62011-05-23 14:30:00 +0200433 /* init readahead state */
434 spin_lock_init(&device->reada_lock);
435 device->reada_curr_zone = NULL;
436 atomic_set(&device->reada_in_flight, 0);
437 device->reada_next = 0;
438 INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
439 INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
440
Chris Masone5e9a522009-06-10 15:17:02 -0400441 mutex_lock(&fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000442 list_add_rcu(&device->dev_list, &fs_devices->devices);
Chris Masone5e9a522009-06-10 15:17:02 -0400443 mutex_unlock(&fs_devices->device_list_mutex);
444
Yan Zheng2b820322008-11-17 21:11:30 -0500445 device->fs_devices = fs_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400446 fs_devices->num_devices++;
Josef Bacik606686e2012-06-04 14:03:51 -0400447 } else if (!device->name || strcmp(device->name->str, path)) {
448 name = rcu_string_strdup(path, GFP_NOFS);
TARUISI Hiroaki3a0524d2010-02-09 06:36:45 +0000449 if (!name)
450 return -ENOMEM;
Josef Bacik606686e2012-06-04 14:03:51 -0400451 rcu_string_free(device->name);
452 rcu_assign_pointer(device->name, name);
Chris Masoncd02dca2010-12-13 14:56:23 -0500453 if (device->missing) {
454 fs_devices->missing_devices--;
455 device->missing = 0;
456 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400457 }
458
459 if (found_transid > fs_devices->latest_trans) {
460 fs_devices->latest_devid = devid;
461 fs_devices->latest_trans = found_transid;
462 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400463 *fs_devices_ret = fs_devices;
464 return 0;
465}
466
Yan Zhenge4404d62008-12-12 10:03:26 -0500467static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
468{
469 struct btrfs_fs_devices *fs_devices;
470 struct btrfs_device *device;
471 struct btrfs_device *orig_dev;
472
473 fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
474 if (!fs_devices)
475 return ERR_PTR(-ENOMEM);
476
477 INIT_LIST_HEAD(&fs_devices->devices);
478 INIT_LIST_HEAD(&fs_devices->alloc_list);
479 INIT_LIST_HEAD(&fs_devices->list);
Chris Masone5e9a522009-06-10 15:17:02 -0400480 mutex_init(&fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500481 fs_devices->latest_devid = orig->latest_devid;
482 fs_devices->latest_trans = orig->latest_trans;
Josef Bacik02db0842012-06-21 16:03:58 -0400483 fs_devices->total_devices = orig->total_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -0500484 memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
485
Xiao Guangrong46224702011-04-20 10:08:47 +0000486 /* We have held the volume lock, it is safe to get the devices. */
Yan Zhenge4404d62008-12-12 10:03:26 -0500487 list_for_each_entry(orig_dev, &orig->devices, dev_list) {
Josef Bacik606686e2012-06-04 14:03:51 -0400488 struct rcu_string *name;
489
Yan Zhenge4404d62008-12-12 10:03:26 -0500490 device = kzalloc(sizeof(*device), GFP_NOFS);
491 if (!device)
492 goto error;
493
Josef Bacik606686e2012-06-04 14:03:51 -0400494 /*
495 * This is ok to do without rcu read locked because we hold the
496 * uuid mutex so nothing we touch in here is going to disappear.
497 */
498 name = rcu_string_strdup(orig_dev->name->str, GFP_NOFS);
499 if (!name) {
Julia Lawallfd2696f2009-09-29 13:51:04 -0400500 kfree(device);
Yan Zhenge4404d62008-12-12 10:03:26 -0500501 goto error;
Julia Lawallfd2696f2009-09-29 13:51:04 -0400502 }
Josef Bacik606686e2012-06-04 14:03:51 -0400503 rcu_assign_pointer(device->name, name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500504
505 device->devid = orig_dev->devid;
506 device->work.func = pending_bios_fn;
507 memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
Yan Zhenge4404d62008-12-12 10:03:26 -0500508 spin_lock_init(&device->io_lock);
509 INIT_LIST_HEAD(&device->dev_list);
510 INIT_LIST_HEAD(&device->dev_alloc_list);
511
512 list_add(&device->dev_list, &fs_devices->devices);
513 device->fs_devices = fs_devices;
514 fs_devices->num_devices++;
515 }
516 return fs_devices;
517error:
518 free_fs_devices(fs_devices);
519 return ERR_PTR(-ENOMEM);
520}
521
Stefan Behrens8dabb742012-11-06 13:15:27 +0100522void btrfs_close_extra_devices(struct btrfs_fs_info *fs_info,
523 struct btrfs_fs_devices *fs_devices, int step)
Chris Masondfe25022008-05-13 13:46:40 -0400524{
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500525 struct btrfs_device *device, *next;
Chris Masondfe25022008-05-13 13:46:40 -0400526
Chris Masona6b0d5c2012-02-20 20:53:43 -0500527 struct block_device *latest_bdev = NULL;
528 u64 latest_devid = 0;
529 u64 latest_transid = 0;
530
Chris Masondfe25022008-05-13 13:46:40 -0400531 mutex_lock(&uuid_mutex);
532again:
Xiao Guangrong46224702011-04-20 10:08:47 +0000533 /* This is the initialized path, it is safe to release the devices. */
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500534 list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500535 if (device->in_fs_metadata) {
Stefan Behrens63a212a2012-11-05 18:29:28 +0100536 if (!device->is_tgtdev_for_dev_replace &&
537 (!latest_transid ||
538 device->generation > latest_transid)) {
Chris Masona6b0d5c2012-02-20 20:53:43 -0500539 latest_devid = device->devid;
540 latest_transid = device->generation;
541 latest_bdev = device->bdev;
542 }
Yan Zheng2b820322008-11-17 21:11:30 -0500543 continue;
Chris Masona6b0d5c2012-02-20 20:53:43 -0500544 }
Yan Zheng2b820322008-11-17 21:11:30 -0500545
Stefan Behrens8dabb742012-11-06 13:15:27 +0100546 if (device->devid == BTRFS_DEV_REPLACE_DEVID) {
547 /*
548 * In the first step, keep the device which has
549 * the correct fsid and the devid that is used
550 * for the dev_replace procedure.
551 * In the second step, the dev_replace state is
552 * read from the device tree and it is known
553 * whether the procedure is really active or
554 * not, which means whether this device is
555 * used or whether it should be removed.
556 */
557 if (step == 0 || device->is_tgtdev_for_dev_replace) {
558 continue;
559 }
560 }
Yan Zheng2b820322008-11-17 21:11:30 -0500561 if (device->bdev) {
Tejun Heod4d77622010-11-13 11:55:18 +0100562 blkdev_put(device->bdev, device->mode);
Yan Zheng2b820322008-11-17 21:11:30 -0500563 device->bdev = NULL;
564 fs_devices->open_devices--;
565 }
566 if (device->writeable) {
567 list_del_init(&device->dev_alloc_list);
568 device->writeable = 0;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100569 if (!device->is_tgtdev_for_dev_replace)
570 fs_devices->rw_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -0500571 }
Yan Zhenge4404d62008-12-12 10:03:26 -0500572 list_del_init(&device->dev_list);
573 fs_devices->num_devices--;
Josef Bacik606686e2012-06-04 14:03:51 -0400574 rcu_string_free(device->name);
Yan Zhenge4404d62008-12-12 10:03:26 -0500575 kfree(device);
Chris Masondfe25022008-05-13 13:46:40 -0400576 }
Yan Zheng2b820322008-11-17 21:11:30 -0500577
578 if (fs_devices->seed) {
579 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -0500580 goto again;
581 }
582
Chris Masona6b0d5c2012-02-20 20:53:43 -0500583 fs_devices->latest_bdev = latest_bdev;
584 fs_devices->latest_devid = latest_devid;
585 fs_devices->latest_trans = latest_transid;
586
Chris Masondfe25022008-05-13 13:46:40 -0400587 mutex_unlock(&uuid_mutex);
Chris Masondfe25022008-05-13 13:46:40 -0400588}
Chris Masona0af4692008-05-13 16:03:06 -0400589
Xiao Guangrong1f781602011-04-20 10:09:16 +0000590static void __free_device(struct work_struct *work)
591{
592 struct btrfs_device *device;
593
594 device = container_of(work, struct btrfs_device, rcu_work);
595
596 if (device->bdev)
597 blkdev_put(device->bdev, device->mode);
598
Josef Bacik606686e2012-06-04 14:03:51 -0400599 rcu_string_free(device->name);
Xiao Guangrong1f781602011-04-20 10:09:16 +0000600 kfree(device);
601}
602
603static void free_device(struct rcu_head *head)
604{
605 struct btrfs_device *device;
606
607 device = container_of(head, struct btrfs_device, rcu);
608
609 INIT_WORK(&device->rcu_work, __free_device);
610 schedule_work(&device->rcu_work);
611}
612
Yan Zheng2b820322008-11-17 21:11:30 -0500613static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400614{
Chris Mason8a4b83c2008-03-24 15:02:07 -0400615 struct btrfs_device *device;
Yan Zhenge4404d62008-12-12 10:03:26 -0500616
Yan Zheng2b820322008-11-17 21:11:30 -0500617 if (--fs_devices->opened > 0)
618 return 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400619
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000620 mutex_lock(&fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500621 list_for_each_entry(device, &fs_devices->devices, dev_list) {
Xiao Guangrong1f781602011-04-20 10:09:16 +0000622 struct btrfs_device *new_device;
Josef Bacik606686e2012-06-04 14:03:51 -0400623 struct rcu_string *name;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000624
625 if (device->bdev)
Chris Masona0af4692008-05-13 16:03:06 -0400626 fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000627
Stefan Behrens8dabb742012-11-06 13:15:27 +0100628 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500629 list_del_init(&device->dev_alloc_list);
630 fs_devices->rw_devices--;
631 }
632
Josef Bacikd5e20032011-08-04 14:52:27 +0000633 if (device->can_discard)
634 fs_devices->num_can_discard--;
635
Xiao Guangrong1f781602011-04-20 10:09:16 +0000636 new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 BUG_ON(!new_device); /* -ENOMEM */
Xiao Guangrong1f781602011-04-20 10:09:16 +0000638 memcpy(new_device, device, sizeof(*new_device));
Josef Bacik606686e2012-06-04 14:03:51 -0400639
640 /* Safe because we are under uuid_mutex */
Josef Bacik99f59442012-08-02 10:23:59 -0400641 if (device->name) {
642 name = rcu_string_strdup(device->name->str, GFP_NOFS);
643 BUG_ON(device->name && !name); /* -ENOMEM */
644 rcu_assign_pointer(new_device->name, name);
645 }
Xiao Guangrong1f781602011-04-20 10:09:16 +0000646 new_device->bdev = NULL;
647 new_device->writeable = 0;
648 new_device->in_fs_metadata = 0;
Josef Bacikd5e20032011-08-04 14:52:27 +0000649 new_device->can_discard = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +0000650 list_replace_rcu(&device->dev_list, &new_device->dev_list);
651
652 call_rcu(&device->rcu, free_device);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400653 }
Xiao Guangrongc9513ed2011-04-20 10:07:30 +0000654 mutex_unlock(&fs_devices->device_list_mutex);
655
Yan Zhenge4404d62008-12-12 10:03:26 -0500656 WARN_ON(fs_devices->open_devices);
657 WARN_ON(fs_devices->rw_devices);
Yan Zheng2b820322008-11-17 21:11:30 -0500658 fs_devices->opened = 0;
659 fs_devices->seeding = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500660
Chris Mason8a4b83c2008-03-24 15:02:07 -0400661 return 0;
662}
663
Yan Zheng2b820322008-11-17 21:11:30 -0500664int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
665{
Yan Zhenge4404d62008-12-12 10:03:26 -0500666 struct btrfs_fs_devices *seed_devices = NULL;
Yan Zheng2b820322008-11-17 21:11:30 -0500667 int ret;
668
669 mutex_lock(&uuid_mutex);
670 ret = __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -0500671 if (!fs_devices->opened) {
672 seed_devices = fs_devices->seed;
673 fs_devices->seed = NULL;
674 }
Yan Zheng2b820322008-11-17 21:11:30 -0500675 mutex_unlock(&uuid_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -0500676
677 while (seed_devices) {
678 fs_devices = seed_devices;
679 seed_devices = fs_devices->seed;
680 __btrfs_close_devices(fs_devices);
681 free_fs_devices(fs_devices);
682 }
Yan Zheng2b820322008-11-17 21:11:30 -0500683 return ret;
684}
685
Yan Zhenge4404d62008-12-12 10:03:26 -0500686static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
687 fmode_t flags, void *holder)
Chris Mason8a4b83c2008-03-24 15:02:07 -0400688{
Josef Bacikd5e20032011-08-04 14:52:27 +0000689 struct request_queue *q;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400690 struct block_device *bdev;
691 struct list_head *head = &fs_devices->devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400692 struct btrfs_device *device;
Chris Masona0af4692008-05-13 16:03:06 -0400693 struct block_device *latest_bdev = NULL;
694 struct buffer_head *bh;
695 struct btrfs_super_block *disk_super;
696 u64 latest_devid = 0;
697 u64 latest_transid = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400698 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500699 int seeding = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400700 int ret = 0;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400701
Tejun Heod4d77622010-11-13 11:55:18 +0100702 flags |= FMODE_EXCL;
703
Qinghuang Fengc6e30872009-01-21 10:59:08 -0500704 list_for_each_entry(device, head, dev_list) {
Chris Masonc1c4d912008-05-08 15:05:58 -0400705 if (device->bdev)
706 continue;
Chris Masondfe25022008-05-13 13:46:40 -0400707 if (!device->name)
708 continue;
709
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100710 ret = btrfs_get_bdev_and_sb(device->name->str, flags, holder, 1,
711 &bdev, &bh);
712 if (ret)
713 continue;
Chris Masona0af4692008-05-13 16:03:06 -0400714
715 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000716 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masona0af4692008-05-13 16:03:06 -0400717 if (devid != device->devid)
718 goto error_brelse;
719
Yan Zheng2b820322008-11-17 21:11:30 -0500720 if (memcmp(device->uuid, disk_super->dev_item.uuid,
721 BTRFS_UUID_SIZE))
722 goto error_brelse;
723
724 device->generation = btrfs_super_generation(disk_super);
725 if (!latest_transid || device->generation > latest_transid) {
Chris Masona0af4692008-05-13 16:03:06 -0400726 latest_devid = devid;
Yan Zheng2b820322008-11-17 21:11:30 -0500727 latest_transid = device->generation;
Chris Masona0af4692008-05-13 16:03:06 -0400728 latest_bdev = bdev;
729 }
730
Yan Zheng2b820322008-11-17 21:11:30 -0500731 if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
732 device->writeable = 0;
733 } else {
734 device->writeable = !bdev_read_only(bdev);
735 seeding = 0;
736 }
737
Josef Bacikd5e20032011-08-04 14:52:27 +0000738 q = bdev_get_queue(bdev);
739 if (blk_queue_discard(q)) {
740 device->can_discard = 1;
741 fs_devices->num_can_discard++;
742 }
743
Chris Mason8a4b83c2008-03-24 15:02:07 -0400744 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -0400745 device->in_fs_metadata = 0;
Chris Mason15916de2008-11-19 21:17:22 -0500746 device->mode = flags;
747
Chris Masonc289811c2009-06-10 09:51:32 -0400748 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
749 fs_devices->rotating = 1;
750
Chris Masona0af4692008-05-13 16:03:06 -0400751 fs_devices->open_devices++;
Stefan Behrens8dabb742012-11-06 13:15:27 +0100752 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -0500753 fs_devices->rw_devices++;
754 list_add(&device->dev_alloc_list,
755 &fs_devices->alloc_list);
756 }
Xiao Guangrong4f6c9322011-04-20 10:06:40 +0000757 brelse(bh);
Chris Masona0af4692008-05-13 16:03:06 -0400758 continue;
Chris Masona061fc82008-05-07 11:43:44 -0400759
Chris Masona0af4692008-05-13 16:03:06 -0400760error_brelse:
761 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100762 blkdev_put(bdev, flags);
Chris Masona0af4692008-05-13 16:03:06 -0400763 continue;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400764 }
Chris Masona0af4692008-05-13 16:03:06 -0400765 if (fs_devices->open_devices == 0) {
Ilya Dryomov20bcd642011-10-20 00:06:20 +0300766 ret = -EINVAL;
Chris Masona0af4692008-05-13 16:03:06 -0400767 goto out;
768 }
Yan Zheng2b820322008-11-17 21:11:30 -0500769 fs_devices->seeding = seeding;
770 fs_devices->opened = 1;
Chris Masona0af4692008-05-13 16:03:06 -0400771 fs_devices->latest_bdev = latest_bdev;
772 fs_devices->latest_devid = latest_devid;
773 fs_devices->latest_trans = latest_transid;
Yan Zheng2b820322008-11-17 21:11:30 -0500774 fs_devices->total_rw_bytes = 0;
Chris Masona0af4692008-05-13 16:03:06 -0400775out:
Yan Zheng2b820322008-11-17 21:11:30 -0500776 return ret;
777}
778
779int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
Christoph Hellwig97288f22008-12-02 06:36:09 -0500780 fmode_t flags, void *holder)
Yan Zheng2b820322008-11-17 21:11:30 -0500781{
782 int ret;
783
784 mutex_lock(&uuid_mutex);
785 if (fs_devices->opened) {
Yan Zhenge4404d62008-12-12 10:03:26 -0500786 fs_devices->opened++;
787 ret = 0;
Yan Zheng2b820322008-11-17 21:11:30 -0500788 } else {
Chris Mason15916de2008-11-19 21:17:22 -0500789 ret = __btrfs_open_devices(fs_devices, flags, holder);
Yan Zheng2b820322008-11-17 21:11:30 -0500790 }
Chris Mason8a4b83c2008-03-24 15:02:07 -0400791 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400792 return ret;
793}
794
Christoph Hellwig97288f22008-12-02 06:36:09 -0500795int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
Chris Mason8a4b83c2008-03-24 15:02:07 -0400796 struct btrfs_fs_devices **fs_devices_ret)
797{
798 struct btrfs_super_block *disk_super;
799 struct block_device *bdev;
800 struct buffer_head *bh;
801 int ret;
802 u64 devid;
Chris Masonf2984462008-04-10 16:19:33 -0400803 u64 transid;
Josef Bacik02db0842012-06-21 16:03:58 -0400804 u64 total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400805
Tejun Heod4d77622010-11-13 11:55:18 +0100806 flags |= FMODE_EXCL;
Al Viro10f63272011-11-17 15:05:22 -0500807 mutex_lock(&uuid_mutex);
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100808 ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400809 if (ret)
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100810 goto error;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400811 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +0000812 devid = btrfs_stack_device_id(&disk_super->dev_item);
Chris Masonf2984462008-04-10 16:19:33 -0400813 transid = btrfs_super_generation(disk_super);
Josef Bacik02db0842012-06-21 16:03:58 -0400814 total_devices = btrfs_super_num_devices(disk_super);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000815 if (disk_super->label[0]) {
816 if (disk_super->label[BTRFS_LABEL_SIZE - 1])
817 disk_super->label[BTRFS_LABEL_SIZE - 1] = '\0';
Chris Masond3977122009-01-05 21:25:51 -0500818 printk(KERN_INFO "device label %s ", disk_super->label);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000819 } else {
Ilya Dryomov22b63a22011-02-09 16:05:31 +0200820 printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
Stefan Behrensd03f9182012-11-05 13:10:49 +0000821 }
Roland Dreier119e10c2009-01-21 10:49:16 -0500822 printk(KERN_CONT "devid %llu transid %llu %s\n",
Chris Masond3977122009-01-05 21:25:51 -0500823 (unsigned long long)devid, (unsigned long long)transid, path);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400824 ret = device_list_add(path, disk_super, devid, fs_devices_ret);
Josef Bacik02db0842012-06-21 16:03:58 -0400825 if (!ret && fs_devices_ret)
826 (*fs_devices_ret)->total_devices = total_devices;
Chris Mason8a4b83c2008-03-24 15:02:07 -0400827 brelse(bh);
Tejun Heod4d77622010-11-13 11:55:18 +0100828 blkdev_put(bdev, flags);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400829error:
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +0100830 mutex_unlock(&uuid_mutex);
Chris Mason8a4b83c2008-03-24 15:02:07 -0400831 return ret;
832}
Chris Mason0b86a832008-03-24 15:01:56 -0400833
Miao Xie6d07bce2011-01-05 10:07:31 +0000834/* helper to account the used device space in the range */
835int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
836 u64 end, u64 *length)
Chris Mason0b86a832008-03-24 15:01:56 -0400837{
838 struct btrfs_key key;
839 struct btrfs_root *root = device->dev_root;
Miao Xie6d07bce2011-01-05 10:07:31 +0000840 struct btrfs_dev_extent *dev_extent;
Yan Zheng2b820322008-11-17 21:11:30 -0500841 struct btrfs_path *path;
Miao Xie6d07bce2011-01-05 10:07:31 +0000842 u64 extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -0400843 int ret;
Miao Xie6d07bce2011-01-05 10:07:31 +0000844 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400845 struct extent_buffer *l;
846
Miao Xie6d07bce2011-01-05 10:07:31 +0000847 *length = 0;
848
Stefan Behrens63a212a2012-11-05 18:29:28 +0100849 if (start >= device->total_bytes || device->is_tgtdev_for_dev_replace)
Miao Xie6d07bce2011-01-05 10:07:31 +0000850 return 0;
851
Yan Zheng2b820322008-11-17 21:11:30 -0500852 path = btrfs_alloc_path();
853 if (!path)
854 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -0400855 path->reada = 2;
Chris Mason8f18cf12008-04-25 16:53:30 -0400856
Chris Mason0b86a832008-03-24 15:01:56 -0400857 key.objectid = device->devid;
Miao Xie6d07bce2011-01-05 10:07:31 +0000858 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -0400859 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie6d07bce2011-01-05 10:07:31 +0000860
861 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400862 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000863 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400864 if (ret > 0) {
865 ret = btrfs_previous_item(root, path, key.objectid, key.type);
866 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000867 goto out;
Yan Zheng1fcbac52009-07-24 11:06:53 -0400868 }
Miao Xie6d07bce2011-01-05 10:07:31 +0000869
Chris Mason0b86a832008-03-24 15:01:56 -0400870 while (1) {
871 l = path->nodes[0];
872 slot = path->slots[0];
873 if (slot >= btrfs_header_nritems(l)) {
874 ret = btrfs_next_leaf(root, path);
875 if (ret == 0)
876 continue;
877 if (ret < 0)
Miao Xie6d07bce2011-01-05 10:07:31 +0000878 goto out;
879
880 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400881 }
882 btrfs_item_key_to_cpu(l, &key, slot);
883
884 if (key.objectid < device->devid)
885 goto next;
886
887 if (key.objectid > device->devid)
Miao Xie6d07bce2011-01-05 10:07:31 +0000888 break;
Chris Mason0b86a832008-03-24 15:01:56 -0400889
Chris Masond3977122009-01-05 21:25:51 -0500890 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
Chris Mason0b86a832008-03-24 15:01:56 -0400891 goto next;
Chris Mason0b86a832008-03-24 15:01:56 -0400892
Chris Mason0b86a832008-03-24 15:01:56 -0400893 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie6d07bce2011-01-05 10:07:31 +0000894 extent_end = key.offset + btrfs_dev_extent_length(l,
895 dev_extent);
896 if (key.offset <= start && extent_end > end) {
897 *length = end - start + 1;
898 break;
899 } else if (key.offset <= start && extent_end > start)
900 *length += extent_end - start;
901 else if (key.offset > start && extent_end <= end)
902 *length += extent_end - key.offset;
903 else if (key.offset > start && key.offset <= end) {
904 *length += end - key.offset + 1;
905 break;
906 } else if (key.offset > end)
907 break;
908
909next:
910 path->slots[0]++;
911 }
912 ret = 0;
913out:
914 btrfs_free_path(path);
915 return ret;
916}
917
Chris Mason0b86a832008-03-24 15:01:56 -0400918/*
Miao Xie7bfc8372011-01-05 10:07:26 +0000919 * find_free_dev_extent - find free space in the specified device
Miao Xie7bfc8372011-01-05 10:07:26 +0000920 * @device: the device which we search the free space in
921 * @num_bytes: the size of the free space that we need
922 * @start: store the start of the free space.
923 * @len: the size of the free space. that we find, or the size of the max
924 * free space if we don't find suitable free space
925 *
Chris Mason0b86a832008-03-24 15:01:56 -0400926 * this uses a pretty simple search, the expectation is that it is
927 * called very infrequently and that a given device has a small number
928 * of extents
Miao Xie7bfc8372011-01-05 10:07:26 +0000929 *
930 * @start is used to store the start of the free space if we find. But if we
931 * don't find suitable free space, it will be used to store the start position
932 * of the max free space.
933 *
934 * @len is used to store the size of the free space that we find.
935 * But if we don't find suitable free space, it is used to store the size of
936 * the max free space.
Chris Mason0b86a832008-03-24 15:01:56 -0400937 */
Li Zefan125ccb02011-12-08 15:07:24 +0800938int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
Miao Xie7bfc8372011-01-05 10:07:26 +0000939 u64 *start, u64 *len)
Chris Mason0b86a832008-03-24 15:01:56 -0400940{
941 struct btrfs_key key;
942 struct btrfs_root *root = device->dev_root;
Miao Xie7bfc8372011-01-05 10:07:26 +0000943 struct btrfs_dev_extent *dev_extent;
Chris Mason0b86a832008-03-24 15:01:56 -0400944 struct btrfs_path *path;
Miao Xie7bfc8372011-01-05 10:07:26 +0000945 u64 hole_size;
946 u64 max_hole_start;
947 u64 max_hole_size;
948 u64 extent_end;
949 u64 search_start;
Chris Mason0b86a832008-03-24 15:01:56 -0400950 u64 search_end = device->total_bytes;
951 int ret;
Miao Xie7bfc8372011-01-05 10:07:26 +0000952 int slot;
Chris Mason0b86a832008-03-24 15:01:56 -0400953 struct extent_buffer *l;
954
Chris Mason0b86a832008-03-24 15:01:56 -0400955 /* FIXME use last free of some kind */
956
957 /* we don't want to overwrite the superblock on the drive,
958 * so we make sure to start at an offset of at least 1MB
959 */
Arne Jansena9c9bf62011-04-12 11:01:20 +0200960 search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
Chris Mason0b86a832008-03-24 15:01:56 -0400961
Miao Xie7bfc8372011-01-05 10:07:26 +0000962 max_hole_start = search_start;
963 max_hole_size = 0;
liubo38c01b92011-08-02 02:39:03 +0000964 hole_size = 0;
Miao Xie7bfc8372011-01-05 10:07:26 +0000965
Stefan Behrens63a212a2012-11-05 18:29:28 +0100966 if (search_start >= search_end || device->is_tgtdev_for_dev_replace) {
Miao Xie7bfc8372011-01-05 10:07:26 +0000967 ret = -ENOSPC;
968 goto error;
969 }
970
971 path = btrfs_alloc_path();
972 if (!path) {
973 ret = -ENOMEM;
974 goto error;
975 }
976 path->reada = 2;
977
Chris Mason0b86a832008-03-24 15:01:56 -0400978 key.objectid = device->devid;
979 key.offset = search_start;
980 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie7bfc8372011-01-05 10:07:26 +0000981
Li Zefan125ccb02011-12-08 15:07:24 +0800982 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason0b86a832008-03-24 15:01:56 -0400983 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000984 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400985 if (ret > 0) {
986 ret = btrfs_previous_item(root, path, key.objectid, key.type);
987 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000988 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400989 }
Miao Xie7bfc8372011-01-05 10:07:26 +0000990
Chris Mason0b86a832008-03-24 15:01:56 -0400991 while (1) {
992 l = path->nodes[0];
993 slot = path->slots[0];
994 if (slot >= btrfs_header_nritems(l)) {
995 ret = btrfs_next_leaf(root, path);
996 if (ret == 0)
997 continue;
998 if (ret < 0)
Miao Xie7bfc8372011-01-05 10:07:26 +0000999 goto out;
1000
1001 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001002 }
1003 btrfs_item_key_to_cpu(l, &key, slot);
1004
1005 if (key.objectid < device->devid)
1006 goto next;
1007
1008 if (key.objectid > device->devid)
Miao Xie7bfc8372011-01-05 10:07:26 +00001009 break;
Chris Mason0b86a832008-03-24 15:01:56 -04001010
Chris Mason0b86a832008-03-24 15:01:56 -04001011 if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
1012 goto next;
1013
Miao Xie7bfc8372011-01-05 10:07:26 +00001014 if (key.offset > search_start) {
1015 hole_size = key.offset - search_start;
1016
1017 if (hole_size > max_hole_size) {
1018 max_hole_start = search_start;
1019 max_hole_size = hole_size;
1020 }
1021
1022 /*
1023 * If this free space is greater than which we need,
1024 * it must be the max free space that we have found
1025 * until now, so max_hole_start must point to the start
1026 * of this free space and the length of this free space
1027 * is stored in max_hole_size. Thus, we return
1028 * max_hole_start and max_hole_size and go back to the
1029 * caller.
1030 */
1031 if (hole_size >= num_bytes) {
1032 ret = 0;
1033 goto out;
1034 }
1035 }
1036
Chris Mason0b86a832008-03-24 15:01:56 -04001037 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
Miao Xie7bfc8372011-01-05 10:07:26 +00001038 extent_end = key.offset + btrfs_dev_extent_length(l,
1039 dev_extent);
1040 if (extent_end > search_start)
1041 search_start = extent_end;
Chris Mason0b86a832008-03-24 15:01:56 -04001042next:
1043 path->slots[0]++;
1044 cond_resched();
1045 }
Chris Mason0b86a832008-03-24 15:01:56 -04001046
liubo38c01b92011-08-02 02:39:03 +00001047 /*
1048 * At this point, search_start should be the end of
1049 * allocated dev extents, and when shrinking the device,
1050 * search_end may be smaller than search_start.
1051 */
1052 if (search_end > search_start)
1053 hole_size = search_end - search_start;
1054
Miao Xie7bfc8372011-01-05 10:07:26 +00001055 if (hole_size > max_hole_size) {
1056 max_hole_start = search_start;
1057 max_hole_size = hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001058 }
Chris Mason0b86a832008-03-24 15:01:56 -04001059
Miao Xie7bfc8372011-01-05 10:07:26 +00001060 /* See above. */
1061 if (hole_size < num_bytes)
1062 ret = -ENOSPC;
1063 else
1064 ret = 0;
1065
1066out:
Yan Zheng2b820322008-11-17 21:11:30 -05001067 btrfs_free_path(path);
Miao Xie7bfc8372011-01-05 10:07:26 +00001068error:
1069 *start = max_hole_start;
Miao Xieb2117a32011-01-05 10:07:28 +00001070 if (len)
Miao Xie7bfc8372011-01-05 10:07:26 +00001071 *len = max_hole_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001072 return ret;
1073}
1074
Christoph Hellwigb2950862008-12-02 09:54:17 -05001075static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04001076 struct btrfs_device *device,
1077 u64 start)
1078{
1079 int ret;
1080 struct btrfs_path *path;
1081 struct btrfs_root *root = device->dev_root;
1082 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001083 struct btrfs_key found_key;
1084 struct extent_buffer *leaf = NULL;
1085 struct btrfs_dev_extent *extent = NULL;
Chris Mason8f18cf12008-04-25 16:53:30 -04001086
1087 path = btrfs_alloc_path();
1088 if (!path)
1089 return -ENOMEM;
1090
1091 key.objectid = device->devid;
1092 key.offset = start;
1093 key.type = BTRFS_DEV_EXTENT_KEY;
Miao Xie924cd8f2011-11-10 20:45:04 -05001094again:
Chris Mason8f18cf12008-04-25 16:53:30 -04001095 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Chris Masona061fc82008-05-07 11:43:44 -04001096 if (ret > 0) {
1097 ret = btrfs_previous_item(root, path, key.objectid,
1098 BTRFS_DEV_EXTENT_KEY);
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001099 if (ret)
1100 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001101 leaf = path->nodes[0];
1102 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1103 extent = btrfs_item_ptr(leaf, path->slots[0],
1104 struct btrfs_dev_extent);
1105 BUG_ON(found_key.offset > start || found_key.offset +
1106 btrfs_dev_extent_length(leaf, extent) < start);
Miao Xie924cd8f2011-11-10 20:45:04 -05001107 key = found_key;
1108 btrfs_release_path(path);
1109 goto again;
Chris Masona061fc82008-05-07 11:43:44 -04001110 } else if (ret == 0) {
1111 leaf = path->nodes[0];
1112 extent = btrfs_item_ptr(leaf, path->slots[0],
1113 struct btrfs_dev_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001114 } else {
1115 btrfs_error(root->fs_info, ret, "Slot search failed");
1116 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001117 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001118
Josef Bacik2bf64752011-09-26 17:12:22 -04001119 if (device->bytes_used > 0) {
1120 u64 len = btrfs_dev_extent_length(leaf, extent);
1121 device->bytes_used -= len;
1122 spin_lock(&root->fs_info->free_chunk_lock);
1123 root->fs_info->free_chunk_space += len;
1124 spin_unlock(&root->fs_info->free_chunk_lock);
1125 }
Chris Mason8f18cf12008-04-25 16:53:30 -04001126 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001127 if (ret) {
1128 btrfs_error(root->fs_info, ret,
1129 "Failed to remove dev extent item");
1130 }
Tsutomu Itohb0b802d2011-05-19 07:03:42 +00001131out:
Chris Mason8f18cf12008-04-25 16:53:30 -04001132 btrfs_free_path(path);
1133 return ret;
1134}
1135
Yan Zheng2b820322008-11-17 21:11:30 -05001136int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
Chris Mason0b86a832008-03-24 15:01:56 -04001137 struct btrfs_device *device,
Chris Masone17cade2008-04-15 15:41:47 -04001138 u64 chunk_tree, u64 chunk_objectid,
Yan Zheng2b820322008-11-17 21:11:30 -05001139 u64 chunk_offset, u64 start, u64 num_bytes)
Chris Mason0b86a832008-03-24 15:01:56 -04001140{
1141 int ret;
1142 struct btrfs_path *path;
1143 struct btrfs_root *root = device->dev_root;
1144 struct btrfs_dev_extent *extent;
1145 struct extent_buffer *leaf;
1146 struct btrfs_key key;
1147
Chris Masondfe25022008-05-13 13:46:40 -04001148 WARN_ON(!device->in_fs_metadata);
Stefan Behrens63a212a2012-11-05 18:29:28 +01001149 WARN_ON(device->is_tgtdev_for_dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04001150 path = btrfs_alloc_path();
1151 if (!path)
1152 return -ENOMEM;
1153
Chris Mason0b86a832008-03-24 15:01:56 -04001154 key.objectid = device->devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001155 key.offset = start;
Chris Mason0b86a832008-03-24 15:01:56 -04001156 key.type = BTRFS_DEV_EXTENT_KEY;
1157 ret = btrfs_insert_empty_item(trans, root, path, &key,
1158 sizeof(*extent));
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001159 if (ret)
1160 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04001161
1162 leaf = path->nodes[0];
1163 extent = btrfs_item_ptr(leaf, path->slots[0],
1164 struct btrfs_dev_extent);
Chris Masone17cade2008-04-15 15:41:47 -04001165 btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
1166 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1167 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1168
1169 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
1170 (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
1171 BTRFS_UUID_SIZE);
1172
Chris Mason0b86a832008-03-24 15:01:56 -04001173 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1174 btrfs_mark_buffer_dirty(leaf);
Mark Fasheh2cdcecb2011-09-08 17:14:32 -07001175out:
Chris Mason0b86a832008-03-24 15:01:56 -04001176 btrfs_free_path(path);
1177 return ret;
1178}
1179
Chris Masona1b32a52008-09-05 16:09:51 -04001180static noinline int find_next_chunk(struct btrfs_root *root,
1181 u64 objectid, u64 *offset)
Chris Mason0b86a832008-03-24 15:01:56 -04001182{
1183 struct btrfs_path *path;
1184 int ret;
1185 struct btrfs_key key;
Chris Masone17cade2008-04-15 15:41:47 -04001186 struct btrfs_chunk *chunk;
Chris Mason0b86a832008-03-24 15:01:56 -04001187 struct btrfs_key found_key;
1188
1189 path = btrfs_alloc_path();
Mark Fasheh92b8e892011-07-12 10:57:59 -07001190 if (!path)
1191 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001192
Chris Masone17cade2008-04-15 15:41:47 -04001193 key.objectid = objectid;
Chris Mason0b86a832008-03-24 15:01:56 -04001194 key.offset = (u64)-1;
1195 key.type = BTRFS_CHUNK_ITEM_KEY;
1196
1197 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1198 if (ret < 0)
1199 goto error;
1200
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001201 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001202
1203 ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
1204 if (ret) {
Chris Masone17cade2008-04-15 15:41:47 -04001205 *offset = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001206 } else {
1207 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1208 path->slots[0]);
Chris Masone17cade2008-04-15 15:41:47 -04001209 if (found_key.objectid != objectid)
1210 *offset = 0;
1211 else {
1212 chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
1213 struct btrfs_chunk);
1214 *offset = found_key.offset +
1215 btrfs_chunk_length(path->nodes[0], chunk);
1216 }
Chris Mason0b86a832008-03-24 15:01:56 -04001217 }
1218 ret = 0;
1219error:
1220 btrfs_free_path(path);
1221 return ret;
1222}
1223
Yan Zheng2b820322008-11-17 21:11:30 -05001224static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
Chris Mason0b86a832008-03-24 15:01:56 -04001225{
1226 int ret;
1227 struct btrfs_key key;
1228 struct btrfs_key found_key;
Yan Zheng2b820322008-11-17 21:11:30 -05001229 struct btrfs_path *path;
1230
1231 root = root->fs_info->chunk_root;
1232
1233 path = btrfs_alloc_path();
1234 if (!path)
1235 return -ENOMEM;
Chris Mason0b86a832008-03-24 15:01:56 -04001236
1237 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1238 key.type = BTRFS_DEV_ITEM_KEY;
1239 key.offset = (u64)-1;
1240
1241 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1242 if (ret < 0)
1243 goto error;
1244
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001245 BUG_ON(ret == 0); /* Corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04001246
1247 ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
1248 BTRFS_DEV_ITEM_KEY);
1249 if (ret) {
1250 *objectid = 1;
1251 } else {
1252 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1253 path->slots[0]);
1254 *objectid = found_key.offset + 1;
1255 }
1256 ret = 0;
1257error:
Yan Zheng2b820322008-11-17 21:11:30 -05001258 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04001259 return ret;
1260}
1261
1262/*
1263 * the device information is stored in the chunk root
1264 * the btrfs_device struct should be fully filled in
1265 */
1266int btrfs_add_device(struct btrfs_trans_handle *trans,
1267 struct btrfs_root *root,
1268 struct btrfs_device *device)
1269{
1270 int ret;
1271 struct btrfs_path *path;
1272 struct btrfs_dev_item *dev_item;
1273 struct extent_buffer *leaf;
1274 struct btrfs_key key;
1275 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04001276
1277 root = root->fs_info->chunk_root;
1278
1279 path = btrfs_alloc_path();
1280 if (!path)
1281 return -ENOMEM;
1282
Chris Mason0b86a832008-03-24 15:01:56 -04001283 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1284 key.type = BTRFS_DEV_ITEM_KEY;
Yan Zheng2b820322008-11-17 21:11:30 -05001285 key.offset = device->devid;
Chris Mason0b86a832008-03-24 15:01:56 -04001286
1287 ret = btrfs_insert_empty_item(trans, root, path, &key,
Chris Mason0d81ba52008-03-24 15:02:07 -04001288 sizeof(*dev_item));
Chris Mason0b86a832008-03-24 15:01:56 -04001289 if (ret)
1290 goto out;
1291
1292 leaf = path->nodes[0];
1293 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
1294
1295 btrfs_set_device_id(leaf, dev_item, device->devid);
Yan Zheng2b820322008-11-17 21:11:30 -05001296 btrfs_set_device_generation(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001297 btrfs_set_device_type(leaf, dev_item, device->type);
1298 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
1299 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
1300 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Mason0b86a832008-03-24 15:01:56 -04001301 btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
1302 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
Chris Masone17cade2008-04-15 15:41:47 -04001303 btrfs_set_device_group(leaf, dev_item, 0);
1304 btrfs_set_device_seek_speed(leaf, dev_item, 0);
1305 btrfs_set_device_bandwidth(leaf, dev_item, 0);
Chris Masonc3027eb2008-12-08 16:40:21 -05001306 btrfs_set_device_start_offset(leaf, dev_item, 0);
Chris Mason0b86a832008-03-24 15:01:56 -04001307
Chris Mason0b86a832008-03-24 15:01:56 -04001308 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04001309 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05001310 ptr = (unsigned long)btrfs_device_fsid(dev_item);
1311 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04001312 btrfs_mark_buffer_dirty(leaf);
Chris Mason0b86a832008-03-24 15:01:56 -04001313
Yan Zheng2b820322008-11-17 21:11:30 -05001314 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04001315out:
1316 btrfs_free_path(path);
1317 return ret;
1318}
Chris Mason8f18cf12008-04-25 16:53:30 -04001319
Chris Masona061fc82008-05-07 11:43:44 -04001320static int btrfs_rm_dev_item(struct btrfs_root *root,
1321 struct btrfs_device *device)
1322{
1323 int ret;
1324 struct btrfs_path *path;
Chris Masona061fc82008-05-07 11:43:44 -04001325 struct btrfs_key key;
Chris Masona061fc82008-05-07 11:43:44 -04001326 struct btrfs_trans_handle *trans;
1327
1328 root = root->fs_info->chunk_root;
1329
1330 path = btrfs_alloc_path();
1331 if (!path)
1332 return -ENOMEM;
1333
Yan, Zhenga22285a2010-05-16 10:48:46 -04001334 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001335 if (IS_ERR(trans)) {
1336 btrfs_free_path(path);
1337 return PTR_ERR(trans);
1338 }
Chris Masona061fc82008-05-07 11:43:44 -04001339 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1340 key.type = BTRFS_DEV_ITEM_KEY;
1341 key.offset = device->devid;
Chris Mason7d9eb122008-07-08 14:19:17 -04001342 lock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001343
1344 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1345 if (ret < 0)
1346 goto out;
1347
1348 if (ret > 0) {
1349 ret = -ENOENT;
1350 goto out;
1351 }
1352
1353 ret = btrfs_del_item(trans, root, path);
1354 if (ret)
1355 goto out;
Chris Masona061fc82008-05-07 11:43:44 -04001356out:
1357 btrfs_free_path(path);
Chris Mason7d9eb122008-07-08 14:19:17 -04001358 unlock_chunks(root);
Chris Masona061fc82008-05-07 11:43:44 -04001359 btrfs_commit_transaction(trans, root);
1360 return ret;
1361}
1362
1363int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1364{
1365 struct btrfs_device *device;
Yan Zheng2b820322008-11-17 21:11:30 -05001366 struct btrfs_device *next_device;
Chris Masona061fc82008-05-07 11:43:44 -04001367 struct block_device *bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001368 struct buffer_head *bh = NULL;
Chris Masona061fc82008-05-07 11:43:44 -04001369 struct btrfs_super_block *disk_super;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001370 struct btrfs_fs_devices *cur_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001371 u64 all_avail;
1372 u64 devid;
Yan Zheng2b820322008-11-17 21:11:30 -05001373 u64 num_devices;
1374 u8 *dev_uuid;
Chris Masona061fc82008-05-07 11:43:44 -04001375 int ret = 0;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001376 bool clear_super = false;
Chris Masona061fc82008-05-07 11:43:44 -04001377
Chris Masona061fc82008-05-07 11:43:44 -04001378 mutex_lock(&uuid_mutex);
1379
1380 all_avail = root->fs_info->avail_data_alloc_bits |
1381 root->fs_info->avail_system_alloc_bits |
1382 root->fs_info->avail_metadata_alloc_bits;
1383
Stefan Behrens8dabb742012-11-06 13:15:27 +01001384 num_devices = root->fs_info->fs_devices->num_devices;
1385 btrfs_dev_replace_lock(&root->fs_info->dev_replace);
1386 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) {
1387 WARN_ON(num_devices < 1);
1388 num_devices--;
1389 }
1390 btrfs_dev_replace_unlock(&root->fs_info->dev_replace);
1391
1392 if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) && num_devices <= 4) {
Chris Masond3977122009-01-05 21:25:51 -05001393 printk(KERN_ERR "btrfs: unable to go below four devices "
1394 "on raid10\n");
Chris Masona061fc82008-05-07 11:43:44 -04001395 ret = -EINVAL;
1396 goto out;
1397 }
1398
Stefan Behrens8dabb742012-11-06 13:15:27 +01001399 if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) && num_devices <= 2) {
Chris Masond3977122009-01-05 21:25:51 -05001400 printk(KERN_ERR "btrfs: unable to go below two "
1401 "devices on raid1\n");
Chris Masona061fc82008-05-07 11:43:44 -04001402 ret = -EINVAL;
1403 goto out;
1404 }
1405
Chris Masondfe25022008-05-13 13:46:40 -04001406 if (strcmp(device_path, "missing") == 0) {
Chris Masondfe25022008-05-13 13:46:40 -04001407 struct list_head *devices;
1408 struct btrfs_device *tmp;
Chris Masona061fc82008-05-07 11:43:44 -04001409
Chris Masondfe25022008-05-13 13:46:40 -04001410 device = NULL;
1411 devices = &root->fs_info->fs_devices->devices;
Xiao Guangrong46224702011-04-20 10:08:47 +00001412 /*
1413 * It is safe to read the devices since the volume_mutex
1414 * is held.
1415 */
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001416 list_for_each_entry(tmp, devices, dev_list) {
Stefan Behrens63a212a2012-11-05 18:29:28 +01001417 if (tmp->in_fs_metadata &&
1418 !tmp->is_tgtdev_for_dev_replace &&
1419 !tmp->bdev) {
Chris Masondfe25022008-05-13 13:46:40 -04001420 device = tmp;
1421 break;
1422 }
1423 }
1424 bdev = NULL;
1425 bh = NULL;
1426 disk_super = NULL;
1427 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05001428 printk(KERN_ERR "btrfs: no missing devices found to "
1429 "remove\n");
Chris Masondfe25022008-05-13 13:46:40 -04001430 goto out;
1431 }
Chris Masondfe25022008-05-13 13:46:40 -04001432 } else {
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001433 ret = btrfs_get_bdev_and_sb(device_path,
1434 FMODE_READ | FMODE_EXCL,
1435 root->fs_info->bdev_holder, 0,
1436 &bdev, &bh);
1437 if (ret)
Chris Masondfe25022008-05-13 13:46:40 -04001438 goto out;
Chris Masondfe25022008-05-13 13:46:40 -04001439 disk_super = (struct btrfs_super_block *)bh->b_data;
Xiao Guangronga3438322010-01-06 11:48:18 +00001440 devid = btrfs_stack_device_id(&disk_super->dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05001441 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001442 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Yan Zheng2b820322008-11-17 21:11:30 -05001443 disk_super->fsid);
Chris Masondfe25022008-05-13 13:46:40 -04001444 if (!device) {
1445 ret = -ENOENT;
1446 goto error_brelse;
1447 }
Chris Masondfe25022008-05-13 13:46:40 -04001448 }
Yan Zheng2b820322008-11-17 21:11:30 -05001449
Stefan Behrens63a212a2012-11-05 18:29:28 +01001450 if (device->is_tgtdev_for_dev_replace) {
1451 pr_err("btrfs: unable to remove the dev_replace target dev\n");
1452 ret = -EINVAL;
1453 goto error_brelse;
1454 }
1455
Yan Zheng2b820322008-11-17 21:11:30 -05001456 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
Chris Masond3977122009-01-05 21:25:51 -05001457 printk(KERN_ERR "btrfs: unable to remove the only writeable "
1458 "device\n");
Yan Zheng2b820322008-11-17 21:11:30 -05001459 ret = -EINVAL;
1460 goto error_brelse;
1461 }
1462
1463 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001464 lock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001465 list_del_init(&device->dev_alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001466 unlock_chunks(root);
Yan Zheng2b820322008-11-17 21:11:30 -05001467 root->fs_info->fs_devices->rw_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001468 clear_super = true;
Yan Zheng2b820322008-11-17 21:11:30 -05001469 }
Chris Masona061fc82008-05-07 11:43:44 -04001470
1471 ret = btrfs_shrink_device(device, 0);
1472 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001473 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001474
Stefan Behrens63a212a2012-11-05 18:29:28 +01001475 /*
1476 * TODO: the superblock still includes this device in its num_devices
1477 * counter although write_all_supers() is not locked out. This
1478 * could give a filesystem state which requires a degraded mount.
1479 */
Chris Masona061fc82008-05-07 11:43:44 -04001480 ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
1481 if (ret)
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001482 goto error_undo;
Chris Masona061fc82008-05-07 11:43:44 -04001483
Josef Bacik2bf64752011-09-26 17:12:22 -04001484 spin_lock(&root->fs_info->free_chunk_lock);
1485 root->fs_info->free_chunk_space = device->total_bytes -
1486 device->bytes_used;
1487 spin_unlock(&root->fs_info->free_chunk_lock);
1488
Yan Zheng2b820322008-11-17 21:11:30 -05001489 device->in_fs_metadata = 0;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001490 btrfs_scrub_cancel_dev(root->fs_info, device);
Chris Masone5e9a522009-06-10 15:17:02 -04001491
1492 /*
1493 * the device list mutex makes sure that we don't change
1494 * the device list while someone else is writing out all
1495 * the device supers.
1496 */
Xiao Guangrong1f781602011-04-20 10:09:16 +00001497
1498 cur_devices = device->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001499 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001500 list_del_rcu(&device->dev_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001501
Yan Zhenge4404d62008-12-12 10:03:26 -05001502 device->fs_devices->num_devices--;
Josef Bacik02db0842012-06-21 16:03:58 -04001503 device->fs_devices->total_devices--;
Yan Zheng2b820322008-11-17 21:11:30 -05001504
Chris Masoncd02dca2010-12-13 14:56:23 -05001505 if (device->missing)
1506 root->fs_info->fs_devices->missing_devices--;
1507
Yan Zheng2b820322008-11-17 21:11:30 -05001508 next_device = list_entry(root->fs_info->fs_devices->devices.next,
1509 struct btrfs_device, dev_list);
1510 if (device->bdev == root->fs_info->sb->s_bdev)
1511 root->fs_info->sb->s_bdev = next_device->bdev;
1512 if (device->bdev == root->fs_info->fs_devices->latest_bdev)
1513 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1514
Xiao Guangrong1f781602011-04-20 10:09:16 +00001515 if (device->bdev)
Yan Zhenge4404d62008-12-12 10:03:26 -05001516 device->fs_devices->open_devices--;
Xiao Guangrong1f781602011-04-20 10:09:16 +00001517
1518 call_rcu(&device->rcu, free_device);
1519 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05001520
David Sterba6c417612011-04-13 15:41:04 +02001521 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
1522 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001523
Xiao Guangrong1f781602011-04-20 10:09:16 +00001524 if (cur_devices->open_devices == 0) {
Yan Zhenge4404d62008-12-12 10:03:26 -05001525 struct btrfs_fs_devices *fs_devices;
1526 fs_devices = root->fs_info->fs_devices;
1527 while (fs_devices) {
Xiao Guangrong1f781602011-04-20 10:09:16 +00001528 if (fs_devices->seed == cur_devices)
Yan Zhenge4404d62008-12-12 10:03:26 -05001529 break;
1530 fs_devices = fs_devices->seed;
Yan Zheng2b820322008-11-17 21:11:30 -05001531 }
Xiao Guangrong1f781602011-04-20 10:09:16 +00001532 fs_devices->seed = cur_devices->seed;
1533 cur_devices->seed = NULL;
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001534 lock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001535 __btrfs_close_devices(cur_devices);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001536 unlock_chunks(root);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001537 free_fs_devices(cur_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001538 }
1539
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001540 root->fs_info->num_tolerated_disk_barrier_failures =
1541 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
1542
Yan Zheng2b820322008-11-17 21:11:30 -05001543 /*
1544 * at this point, the device is zero sized. We want to
1545 * remove it from the devices list and zero out the old super
1546 */
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001547 if (clear_super && disk_super) {
Chris Masondfe25022008-05-13 13:46:40 -04001548 /* make sure this device isn't detected as part of
1549 * the FS anymore
1550 */
1551 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
1552 set_buffer_dirty(bh);
1553 sync_dirty_buffer(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001554 }
Chris Masona061fc82008-05-07 11:43:44 -04001555
Chris Masona061fc82008-05-07 11:43:44 -04001556 ret = 0;
Chris Masona061fc82008-05-07 11:43:44 -04001557
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001558 /* Notify udev that device has changed */
Eric Sandeen3c911602013-01-31 00:55:02 +00001559 if (bdev)
1560 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
Lukas Czernerb8b8ff52012-12-06 19:25:48 +00001561
Chris Masona061fc82008-05-07 11:43:44 -04001562error_brelse:
1563 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001564 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001565 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001566out:
1567 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001568 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001569error_undo:
1570 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001571 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001572 list_add(&device->dev_alloc_list,
1573 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001574 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001575 root->fs_info->fs_devices->rw_devices++;
1576 }
1577 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001578}
1579
Stefan Behrense93c89c2012-11-05 17:33:06 +01001580void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1581 struct btrfs_device *srcdev)
1582{
1583 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1584 list_del_rcu(&srcdev->dev_list);
1585 list_del_rcu(&srcdev->dev_alloc_list);
1586 fs_info->fs_devices->num_devices--;
1587 if (srcdev->missing) {
1588 fs_info->fs_devices->missing_devices--;
1589 fs_info->fs_devices->rw_devices++;
1590 }
1591 if (srcdev->can_discard)
1592 fs_info->fs_devices->num_can_discard--;
1593 if (srcdev->bdev)
1594 fs_info->fs_devices->open_devices--;
1595
1596 call_rcu(&srcdev->rcu, free_device);
1597}
1598
1599void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1600 struct btrfs_device *tgtdev)
1601{
1602 struct btrfs_device *next_device;
1603
1604 WARN_ON(!tgtdev);
1605 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1606 if (tgtdev->bdev) {
1607 btrfs_scratch_superblock(tgtdev);
1608 fs_info->fs_devices->open_devices--;
1609 }
1610 fs_info->fs_devices->num_devices--;
1611 if (tgtdev->can_discard)
1612 fs_info->fs_devices->num_can_discard++;
1613
1614 next_device = list_entry(fs_info->fs_devices->devices.next,
1615 struct btrfs_device, dev_list);
1616 if (tgtdev->bdev == fs_info->sb->s_bdev)
1617 fs_info->sb->s_bdev = next_device->bdev;
1618 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1619 fs_info->fs_devices->latest_bdev = next_device->bdev;
1620 list_del_rcu(&tgtdev->dev_list);
1621
1622 call_rcu(&tgtdev->rcu, free_device);
1623
1624 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1625}
1626
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001627int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1628 struct btrfs_device **device)
1629{
1630 int ret = 0;
1631 struct btrfs_super_block *disk_super;
1632 u64 devid;
1633 u8 *dev_uuid;
1634 struct block_device *bdev;
1635 struct buffer_head *bh;
1636
1637 *device = NULL;
1638 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1639 root->fs_info->bdev_holder, 0, &bdev, &bh);
1640 if (ret)
1641 return ret;
1642 disk_super = (struct btrfs_super_block *)bh->b_data;
1643 devid = btrfs_stack_device_id(&disk_super->dev_item);
1644 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001645 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001646 disk_super->fsid);
1647 brelse(bh);
1648 if (!*device)
1649 ret = -ENOENT;
1650 blkdev_put(bdev, FMODE_READ);
1651 return ret;
1652}
1653
1654int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1655 char *device_path,
1656 struct btrfs_device **device)
1657{
1658 *device = NULL;
1659 if (strcmp(device_path, "missing") == 0) {
1660 struct list_head *devices;
1661 struct btrfs_device *tmp;
1662
1663 devices = &root->fs_info->fs_devices->devices;
1664 /*
1665 * It is safe to read the devices since the volume_mutex
1666 * is held by the caller.
1667 */
1668 list_for_each_entry(tmp, devices, dev_list) {
1669 if (tmp->in_fs_metadata && !tmp->bdev) {
1670 *device = tmp;
1671 break;
1672 }
1673 }
1674
1675 if (!*device) {
1676 pr_err("btrfs: no missing device found\n");
1677 return -ENOENT;
1678 }
1679
1680 return 0;
1681 } else {
1682 return btrfs_find_device_by_path(root, device_path, device);
1683 }
1684}
1685
Yan Zheng2b820322008-11-17 21:11:30 -05001686/*
1687 * does all the dirty work required for changing file system's UUID.
1688 */
Li Zefan125ccb02011-12-08 15:07:24 +08001689static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001690{
1691 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1692 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001693 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001694 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001695 struct btrfs_device *device;
1696 u64 super_flags;
1697
1698 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001699 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001700 return -EINVAL;
1701
Yan Zhenge4404d62008-12-12 10:03:26 -05001702 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1703 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001704 return -ENOMEM;
1705
Yan Zhenge4404d62008-12-12 10:03:26 -05001706 old_devices = clone_fs_devices(fs_devices);
1707 if (IS_ERR(old_devices)) {
1708 kfree(seed_devices);
1709 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001710 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001711
Yan Zheng2b820322008-11-17 21:11:30 -05001712 list_add(&old_devices->list, &fs_uuids);
1713
Yan Zhenge4404d62008-12-12 10:03:26 -05001714 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1715 seed_devices->opened = 1;
1716 INIT_LIST_HEAD(&seed_devices->devices);
1717 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001718 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001719
1720 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001721 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1722 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001723 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1724
Yan Zhenge4404d62008-12-12 10:03:26 -05001725 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1726 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1727 device->fs_devices = seed_devices;
1728 }
1729
Yan Zheng2b820322008-11-17 21:11:30 -05001730 fs_devices->seeding = 0;
1731 fs_devices->num_devices = 0;
1732 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001733 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001734 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001735
1736 generate_random_uuid(fs_devices->fsid);
1737 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1738 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1739 super_flags = btrfs_super_flags(disk_super) &
1740 ~BTRFS_SUPER_FLAG_SEEDING;
1741 btrfs_set_super_flags(disk_super, super_flags);
1742
1743 return 0;
1744}
1745
1746/*
1747 * strore the expected generation for seed devices in device items.
1748 */
1749static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1750 struct btrfs_root *root)
1751{
1752 struct btrfs_path *path;
1753 struct extent_buffer *leaf;
1754 struct btrfs_dev_item *dev_item;
1755 struct btrfs_device *device;
1756 struct btrfs_key key;
1757 u8 fs_uuid[BTRFS_UUID_SIZE];
1758 u8 dev_uuid[BTRFS_UUID_SIZE];
1759 u64 devid;
1760 int ret;
1761
1762 path = btrfs_alloc_path();
1763 if (!path)
1764 return -ENOMEM;
1765
1766 root = root->fs_info->chunk_root;
1767 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1768 key.offset = 0;
1769 key.type = BTRFS_DEV_ITEM_KEY;
1770
1771 while (1) {
1772 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1773 if (ret < 0)
1774 goto error;
1775
1776 leaf = path->nodes[0];
1777next_slot:
1778 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1779 ret = btrfs_next_leaf(root, path);
1780 if (ret > 0)
1781 break;
1782 if (ret < 0)
1783 goto error;
1784 leaf = path->nodes[0];
1785 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001786 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001787 continue;
1788 }
1789
1790 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1791 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1792 key.type != BTRFS_DEV_ITEM_KEY)
1793 break;
1794
1795 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1796 struct btrfs_dev_item);
1797 devid = btrfs_device_id(leaf, dev_item);
1798 read_extent_buffer(leaf, dev_uuid,
1799 (unsigned long)btrfs_device_uuid(dev_item),
1800 BTRFS_UUID_SIZE);
1801 read_extent_buffer(leaf, fs_uuid,
1802 (unsigned long)btrfs_device_fsid(dev_item),
1803 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001804 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1805 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001806 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001807
1808 if (device->fs_devices->seeding) {
1809 btrfs_set_device_generation(leaf, dev_item,
1810 device->generation);
1811 btrfs_mark_buffer_dirty(leaf);
1812 }
1813
1814 path->slots[0]++;
1815 goto next_slot;
1816 }
1817 ret = 0;
1818error:
1819 btrfs_free_path(path);
1820 return ret;
1821}
1822
Chris Mason788f20e2008-04-28 15:29:42 -04001823int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1824{
Josef Bacikd5e20032011-08-04 14:52:27 +00001825 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001826 struct btrfs_trans_handle *trans;
1827 struct btrfs_device *device;
1828 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001829 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001830 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001831 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001832 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001833 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001834 int ret = 0;
1835
Yan Zheng2b820322008-11-17 21:11:30 -05001836 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001837 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001838
Li Zefana5d16332011-12-07 20:08:40 -05001839 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001840 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001841 if (IS_ERR(bdev))
1842 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001843
Yan Zheng2b820322008-11-17 21:11:30 -05001844 if (root->fs_info->fs_devices->seeding) {
1845 seeding_dev = 1;
1846 down_write(&sb->s_umount);
1847 mutex_lock(&uuid_mutex);
1848 }
1849
Chris Mason8c8bee12008-09-29 11:19:10 -04001850 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001851
Chris Mason788f20e2008-04-28 15:29:42 -04001852 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001853
1854 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001855 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001856 if (device->bdev == bdev) {
1857 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001858 mutex_unlock(
1859 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001860 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001861 }
1862 }
Liu Bod25628b2012-11-14 14:35:30 +00001863 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001864
1865 device = kzalloc(sizeof(*device), GFP_NOFS);
1866 if (!device) {
1867 /* we can safely leave the fs_devices entry around */
1868 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001869 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001870 }
1871
Josef Bacik606686e2012-06-04 14:03:51 -04001872 name = rcu_string_strdup(device_path, GFP_NOFS);
1873 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001874 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001875 ret = -ENOMEM;
1876 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001877 }
Josef Bacik606686e2012-06-04 14:03:51 -04001878 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001879
1880 ret = find_next_devid(root, &device->devid);
1881 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001882 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001883 kfree(device);
1884 goto error;
1885 }
1886
Yan, Zhenga22285a2010-05-16 10:48:46 -04001887 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001888 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001889 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001890 kfree(device);
1891 ret = PTR_ERR(trans);
1892 goto error;
1893 }
1894
Yan Zheng2b820322008-11-17 21:11:30 -05001895 lock_chunks(root);
1896
Josef Bacikd5e20032011-08-04 14:52:27 +00001897 q = bdev_get_queue(bdev);
1898 if (blk_queue_discard(q))
1899 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001900 device->writeable = 1;
1901 device->work.func = pending_bios_fn;
1902 generate_random_uuid(device->uuid);
1903 spin_lock_init(&device->io_lock);
1904 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001905 device->io_width = root->sectorsize;
1906 device->io_align = root->sectorsize;
1907 device->sector_size = root->sectorsize;
1908 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001909 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001910 device->dev_root = root->fs_info->dev_root;
1911 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001912 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001913 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001914 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001915 set_blocksize(device->bdev, 4096);
1916
Yan Zheng2b820322008-11-17 21:11:30 -05001917 if (seeding_dev) {
1918 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001919 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001920 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001921 }
1922
1923 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001924
Chris Masone5e9a522009-06-10 15:17:02 -04001925 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001926 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001927 list_add(&device->dev_alloc_list,
1928 &root->fs_info->fs_devices->alloc_list);
1929 root->fs_info->fs_devices->num_devices++;
1930 root->fs_info->fs_devices->open_devices++;
1931 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001932 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001933 if (device->can_discard)
1934 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001935 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1936
Josef Bacik2bf64752011-09-26 17:12:22 -04001937 spin_lock(&root->fs_info->free_chunk_lock);
1938 root->fs_info->free_chunk_space += device->total_bytes;
1939 spin_unlock(&root->fs_info->free_chunk_lock);
1940
Chris Masonc289811c2009-06-10 09:51:32 -04001941 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1942 root->fs_info->fs_devices->rotating = 1;
1943
David Sterba6c417612011-04-13 15:41:04 +02001944 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1945 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001946 total_bytes + device->total_bytes);
1947
David Sterba6c417612011-04-13 15:41:04 +02001948 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1949 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001950 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001951 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001952
Yan Zheng2b820322008-11-17 21:11:30 -05001953 if (seeding_dev) {
1954 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001955 if (ret) {
1956 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001957 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001958 }
Yan Zheng2b820322008-11-17 21:11:30 -05001959 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001960 if (ret) {
1961 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001962 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001963 }
Yan Zheng2b820322008-11-17 21:11:30 -05001964 } else {
1965 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001966 if (ret) {
1967 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001968 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001969 }
Yan Zheng2b820322008-11-17 21:11:30 -05001970 }
1971
Chris Mason913d9522009-03-10 13:17:18 -04001972 /*
1973 * we've got more storage, clear any full flags on the space
1974 * infos
1975 */
1976 btrfs_clear_space_info_full(root->fs_info);
1977
Chris Mason7d9eb122008-07-08 14:19:17 -04001978 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001979 root->fs_info->num_tolerated_disk_barrier_failures =
1980 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001981 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001982
1983 if (seeding_dev) {
1984 mutex_unlock(&uuid_mutex);
1985 up_write(&sb->s_umount);
1986
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001987 if (ret) /* transaction commit */
1988 return ret;
1989
Yan Zheng2b820322008-11-17 21:11:30 -05001990 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001991 if (ret < 0)
1992 btrfs_error(root->fs_info, ret,
1993 "Failed to relocate sys chunks after "
1994 "device initialization. This can be fixed "
1995 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00001996 trans = btrfs_attach_transaction(root);
1997 if (IS_ERR(trans)) {
1998 if (PTR_ERR(trans) == -ENOENT)
1999 return 0;
2000 return PTR_ERR(trans);
2001 }
2002 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002003 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002004
Chris Mason788f20e2008-04-28 15:29:42 -04002005 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002006
2007error_trans:
2008 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002009 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002010 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002011 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002012error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002013 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002014 if (seeding_dev) {
2015 mutex_unlock(&uuid_mutex);
2016 up_write(&sb->s_umount);
2017 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002018 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002019}
2020
Stefan Behrense93c89c2012-11-05 17:33:06 +01002021int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2022 struct btrfs_device **device_out)
2023{
2024 struct request_queue *q;
2025 struct btrfs_device *device;
2026 struct block_device *bdev;
2027 struct btrfs_fs_info *fs_info = root->fs_info;
2028 struct list_head *devices;
2029 struct rcu_string *name;
2030 int ret = 0;
2031
2032 *device_out = NULL;
2033 if (fs_info->fs_devices->seeding)
2034 return -EINVAL;
2035
2036 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2037 fs_info->bdev_holder);
2038 if (IS_ERR(bdev))
2039 return PTR_ERR(bdev);
2040
2041 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2042
2043 devices = &fs_info->fs_devices->devices;
2044 list_for_each_entry(device, devices, dev_list) {
2045 if (device->bdev == bdev) {
2046 ret = -EEXIST;
2047 goto error;
2048 }
2049 }
2050
2051 device = kzalloc(sizeof(*device), GFP_NOFS);
2052 if (!device) {
2053 ret = -ENOMEM;
2054 goto error;
2055 }
2056
2057 name = rcu_string_strdup(device_path, GFP_NOFS);
2058 if (!name) {
2059 kfree(device);
2060 ret = -ENOMEM;
2061 goto error;
2062 }
2063 rcu_assign_pointer(device->name, name);
2064
2065 q = bdev_get_queue(bdev);
2066 if (blk_queue_discard(q))
2067 device->can_discard = 1;
2068 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2069 device->writeable = 1;
2070 device->work.func = pending_bios_fn;
2071 generate_random_uuid(device->uuid);
2072 device->devid = BTRFS_DEV_REPLACE_DEVID;
2073 spin_lock_init(&device->io_lock);
2074 device->generation = 0;
2075 device->io_width = root->sectorsize;
2076 device->io_align = root->sectorsize;
2077 device->sector_size = root->sectorsize;
2078 device->total_bytes = i_size_read(bdev->bd_inode);
2079 device->disk_total_bytes = device->total_bytes;
2080 device->dev_root = fs_info->dev_root;
2081 device->bdev = bdev;
2082 device->in_fs_metadata = 1;
2083 device->is_tgtdev_for_dev_replace = 1;
2084 device->mode = FMODE_EXCL;
2085 set_blocksize(device->bdev, 4096);
2086 device->fs_devices = fs_info->fs_devices;
2087 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2088 fs_info->fs_devices->num_devices++;
2089 fs_info->fs_devices->open_devices++;
2090 if (device->can_discard)
2091 fs_info->fs_devices->num_can_discard++;
2092 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2093
2094 *device_out = device;
2095 return ret;
2096
2097error:
2098 blkdev_put(bdev, FMODE_EXCL);
2099 return ret;
2100}
2101
2102void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2103 struct btrfs_device *tgtdev)
2104{
2105 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2106 tgtdev->io_width = fs_info->dev_root->sectorsize;
2107 tgtdev->io_align = fs_info->dev_root->sectorsize;
2108 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2109 tgtdev->dev_root = fs_info->dev_root;
2110 tgtdev->in_fs_metadata = 1;
2111}
2112
Chris Masond3977122009-01-05 21:25:51 -05002113static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2114 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002115{
2116 int ret;
2117 struct btrfs_path *path;
2118 struct btrfs_root *root;
2119 struct btrfs_dev_item *dev_item;
2120 struct extent_buffer *leaf;
2121 struct btrfs_key key;
2122
2123 root = device->dev_root->fs_info->chunk_root;
2124
2125 path = btrfs_alloc_path();
2126 if (!path)
2127 return -ENOMEM;
2128
2129 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2130 key.type = BTRFS_DEV_ITEM_KEY;
2131 key.offset = device->devid;
2132
2133 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2134 if (ret < 0)
2135 goto out;
2136
2137 if (ret > 0) {
2138 ret = -ENOENT;
2139 goto out;
2140 }
2141
2142 leaf = path->nodes[0];
2143 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2144
2145 btrfs_set_device_id(leaf, dev_item, device->devid);
2146 btrfs_set_device_type(leaf, dev_item, device->type);
2147 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2148 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2149 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002150 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002151 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2152 btrfs_mark_buffer_dirty(leaf);
2153
2154out:
2155 btrfs_free_path(path);
2156 return ret;
2157}
2158
Chris Mason7d9eb122008-07-08 14:19:17 -04002159static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002160 struct btrfs_device *device, u64 new_size)
2161{
2162 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002163 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002164 u64 old_total = btrfs_super_total_bytes(super_copy);
2165 u64 diff = new_size - device->total_bytes;
2166
Yan Zheng2b820322008-11-17 21:11:30 -05002167 if (!device->writeable)
2168 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002169 if (new_size <= device->total_bytes ||
2170 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002171 return -EINVAL;
2172
Chris Mason8f18cf12008-04-25 16:53:30 -04002173 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002174 device->fs_devices->total_rw_bytes += diff;
2175
2176 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002177 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002178 btrfs_clear_space_info_full(device->dev_root->fs_info);
2179
Chris Mason8f18cf12008-04-25 16:53:30 -04002180 return btrfs_update_device(trans, device);
2181}
2182
Chris Mason7d9eb122008-07-08 14:19:17 -04002183int btrfs_grow_device(struct btrfs_trans_handle *trans,
2184 struct btrfs_device *device, u64 new_size)
2185{
2186 int ret;
2187 lock_chunks(device->dev_root);
2188 ret = __btrfs_grow_device(trans, device, new_size);
2189 unlock_chunks(device->dev_root);
2190 return ret;
2191}
2192
Chris Mason8f18cf12008-04-25 16:53:30 -04002193static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2194 struct btrfs_root *root,
2195 u64 chunk_tree, u64 chunk_objectid,
2196 u64 chunk_offset)
2197{
2198 int ret;
2199 struct btrfs_path *path;
2200 struct btrfs_key key;
2201
2202 root = root->fs_info->chunk_root;
2203 path = btrfs_alloc_path();
2204 if (!path)
2205 return -ENOMEM;
2206
2207 key.objectid = chunk_objectid;
2208 key.offset = chunk_offset;
2209 key.type = BTRFS_CHUNK_ITEM_KEY;
2210
2211 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002212 if (ret < 0)
2213 goto out;
2214 else if (ret > 0) { /* Logic error or corruption */
2215 btrfs_error(root->fs_info, -ENOENT,
2216 "Failed lookup while freeing chunk.");
2217 ret = -ENOENT;
2218 goto out;
2219 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002220
2221 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002222 if (ret < 0)
2223 btrfs_error(root->fs_info, ret,
2224 "Failed to delete chunk item.");
2225out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002226 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002227 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002228}
2229
Christoph Hellwigb2950862008-12-02 09:54:17 -05002230static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002231 chunk_offset)
2232{
David Sterba6c417612011-04-13 15:41:04 +02002233 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002234 struct btrfs_disk_key *disk_key;
2235 struct btrfs_chunk *chunk;
2236 u8 *ptr;
2237 int ret = 0;
2238 u32 num_stripes;
2239 u32 array_size;
2240 u32 len = 0;
2241 u32 cur;
2242 struct btrfs_key key;
2243
2244 array_size = btrfs_super_sys_array_size(super_copy);
2245
2246 ptr = super_copy->sys_chunk_array;
2247 cur = 0;
2248
2249 while (cur < array_size) {
2250 disk_key = (struct btrfs_disk_key *)ptr;
2251 btrfs_disk_key_to_cpu(&key, disk_key);
2252
2253 len = sizeof(*disk_key);
2254
2255 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2256 chunk = (struct btrfs_chunk *)(ptr + len);
2257 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2258 len += btrfs_chunk_item_size(num_stripes);
2259 } else {
2260 ret = -EIO;
2261 break;
2262 }
2263 if (key.objectid == chunk_objectid &&
2264 key.offset == chunk_offset) {
2265 memmove(ptr, ptr + len, array_size - (cur + len));
2266 array_size -= len;
2267 btrfs_set_super_sys_array_size(super_copy, array_size);
2268 } else {
2269 ptr += len;
2270 cur += len;
2271 }
2272 }
2273 return ret;
2274}
2275
Christoph Hellwigb2950862008-12-02 09:54:17 -05002276static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002277 u64 chunk_tree, u64 chunk_objectid,
2278 u64 chunk_offset)
2279{
2280 struct extent_map_tree *em_tree;
2281 struct btrfs_root *extent_root;
2282 struct btrfs_trans_handle *trans;
2283 struct extent_map *em;
2284 struct map_lookup *map;
2285 int ret;
2286 int i;
2287
2288 root = root->fs_info->chunk_root;
2289 extent_root = root->fs_info->extent_root;
2290 em_tree = &root->fs_info->mapping_tree.map_tree;
2291
Josef Bacikba1bf482009-09-11 16:11:19 -04002292 ret = btrfs_can_relocate(extent_root, chunk_offset);
2293 if (ret)
2294 return -ENOSPC;
2295
Chris Mason8f18cf12008-04-25 16:53:30 -04002296 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002297 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002298 if (ret)
2299 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002300
Yan, Zhenga22285a2010-05-16 10:48:46 -04002301 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002302 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002303
Chris Mason7d9eb122008-07-08 14:19:17 -04002304 lock_chunks(root);
2305
Chris Mason8f18cf12008-04-25 16:53:30 -04002306 /*
2307 * step two, delete the device extents and the
2308 * chunk tree entries
2309 */
Chris Mason890871b2009-09-02 16:24:52 -04002310 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002311 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002312 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002313
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002314 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002315 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002316 map = (struct map_lookup *)em->bdev;
2317
2318 for (i = 0; i < map->num_stripes; i++) {
2319 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2320 map->stripes[i].physical);
2321 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002322
Chris Masondfe25022008-05-13 13:46:40 -04002323 if (map->stripes[i].dev) {
2324 ret = btrfs_update_device(trans, map->stripes[i].dev);
2325 BUG_ON(ret);
2326 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002327 }
2328 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2329 chunk_offset);
2330
2331 BUG_ON(ret);
2332
liubo1abe9b82011-03-24 11:18:59 +00002333 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2334
Chris Mason8f18cf12008-04-25 16:53:30 -04002335 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2336 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2337 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002338 }
2339
Zheng Yan1a40e232008-09-26 10:09:34 -04002340 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2341 BUG_ON(ret);
2342
Chris Mason890871b2009-09-02 16:24:52 -04002343 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002344 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002345 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002346
Chris Mason8f18cf12008-04-25 16:53:30 -04002347 kfree(map);
2348 em->bdev = NULL;
2349
2350 /* once for the tree */
2351 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002352 /* once for us */
2353 free_extent_map(em);
2354
Chris Mason7d9eb122008-07-08 14:19:17 -04002355 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002356 btrfs_end_transaction(trans, root);
2357 return 0;
2358}
2359
Yan Zheng2b820322008-11-17 21:11:30 -05002360static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2361{
2362 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2363 struct btrfs_path *path;
2364 struct extent_buffer *leaf;
2365 struct btrfs_chunk *chunk;
2366 struct btrfs_key key;
2367 struct btrfs_key found_key;
2368 u64 chunk_tree = chunk_root->root_key.objectid;
2369 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002370 bool retried = false;
2371 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002372 int ret;
2373
2374 path = btrfs_alloc_path();
2375 if (!path)
2376 return -ENOMEM;
2377
Josef Bacikba1bf482009-09-11 16:11:19 -04002378again:
Yan Zheng2b820322008-11-17 21:11:30 -05002379 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2380 key.offset = (u64)-1;
2381 key.type = BTRFS_CHUNK_ITEM_KEY;
2382
2383 while (1) {
2384 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2385 if (ret < 0)
2386 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002387 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002388
2389 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2390 key.type);
2391 if (ret < 0)
2392 goto error;
2393 if (ret > 0)
2394 break;
2395
2396 leaf = path->nodes[0];
2397 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2398
2399 chunk = btrfs_item_ptr(leaf, path->slots[0],
2400 struct btrfs_chunk);
2401 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002402 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002403
2404 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2405 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2406 found_key.objectid,
2407 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002408 if (ret == -ENOSPC)
2409 failed++;
2410 else if (ret)
2411 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002412 }
2413
2414 if (found_key.offset == 0)
2415 break;
2416 key.offset = found_key.offset - 1;
2417 }
2418 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002419 if (failed && !retried) {
2420 failed = 0;
2421 retried = true;
2422 goto again;
2423 } else if (failed && retried) {
2424 WARN_ON(1);
2425 ret = -ENOSPC;
2426 }
Yan Zheng2b820322008-11-17 21:11:30 -05002427error:
2428 btrfs_free_path(path);
2429 return ret;
2430}
2431
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002432static int insert_balance_item(struct btrfs_root *root,
2433 struct btrfs_balance_control *bctl)
2434{
2435 struct btrfs_trans_handle *trans;
2436 struct btrfs_balance_item *item;
2437 struct btrfs_disk_balance_args disk_bargs;
2438 struct btrfs_path *path;
2439 struct extent_buffer *leaf;
2440 struct btrfs_key key;
2441 int ret, err;
2442
2443 path = btrfs_alloc_path();
2444 if (!path)
2445 return -ENOMEM;
2446
2447 trans = btrfs_start_transaction(root, 0);
2448 if (IS_ERR(trans)) {
2449 btrfs_free_path(path);
2450 return PTR_ERR(trans);
2451 }
2452
2453 key.objectid = BTRFS_BALANCE_OBJECTID;
2454 key.type = BTRFS_BALANCE_ITEM_KEY;
2455 key.offset = 0;
2456
2457 ret = btrfs_insert_empty_item(trans, root, path, &key,
2458 sizeof(*item));
2459 if (ret)
2460 goto out;
2461
2462 leaf = path->nodes[0];
2463 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2464
2465 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2466
2467 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2468 btrfs_set_balance_data(leaf, item, &disk_bargs);
2469 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2470 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2471 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2472 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2473
2474 btrfs_set_balance_flags(leaf, item, bctl->flags);
2475
2476 btrfs_mark_buffer_dirty(leaf);
2477out:
2478 btrfs_free_path(path);
2479 err = btrfs_commit_transaction(trans, root);
2480 if (err && !ret)
2481 ret = err;
2482 return ret;
2483}
2484
2485static int del_balance_item(struct btrfs_root *root)
2486{
2487 struct btrfs_trans_handle *trans;
2488 struct btrfs_path *path;
2489 struct btrfs_key key;
2490 int ret, err;
2491
2492 path = btrfs_alloc_path();
2493 if (!path)
2494 return -ENOMEM;
2495
2496 trans = btrfs_start_transaction(root, 0);
2497 if (IS_ERR(trans)) {
2498 btrfs_free_path(path);
2499 return PTR_ERR(trans);
2500 }
2501
2502 key.objectid = BTRFS_BALANCE_OBJECTID;
2503 key.type = BTRFS_BALANCE_ITEM_KEY;
2504 key.offset = 0;
2505
2506 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2507 if (ret < 0)
2508 goto out;
2509 if (ret > 0) {
2510 ret = -ENOENT;
2511 goto out;
2512 }
2513
2514 ret = btrfs_del_item(trans, root, path);
2515out:
2516 btrfs_free_path(path);
2517 err = btrfs_commit_transaction(trans, root);
2518 if (err && !ret)
2519 ret = err;
2520 return ret;
2521}
2522
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002523/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002524 * This is a heuristic used to reduce the number of chunks balanced on
2525 * resume after balance was interrupted.
2526 */
2527static void update_balance_args(struct btrfs_balance_control *bctl)
2528{
2529 /*
2530 * Turn on soft mode for chunk types that were being converted.
2531 */
2532 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2533 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2534 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2535 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2536 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2537 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2538
2539 /*
2540 * Turn on usage filter if is not already used. The idea is
2541 * that chunks that we have already balanced should be
2542 * reasonably full. Don't do it for chunks that are being
2543 * converted - that will keep us from relocating unconverted
2544 * (albeit full) chunks.
2545 */
2546 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2547 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2548 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2549 bctl->data.usage = 90;
2550 }
2551 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2552 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2553 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2554 bctl->sys.usage = 90;
2555 }
2556 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2557 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2558 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2559 bctl->meta.usage = 90;
2560 }
2561}
2562
2563/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002564 * Should be called with both balance and volume mutexes held to
2565 * serialize other volume operations (add_dev/rm_dev/resize) with
2566 * restriper. Same goes for unset_balance_control.
2567 */
2568static void set_balance_control(struct btrfs_balance_control *bctl)
2569{
2570 struct btrfs_fs_info *fs_info = bctl->fs_info;
2571
2572 BUG_ON(fs_info->balance_ctl);
2573
2574 spin_lock(&fs_info->balance_lock);
2575 fs_info->balance_ctl = bctl;
2576 spin_unlock(&fs_info->balance_lock);
2577}
2578
2579static void unset_balance_control(struct btrfs_fs_info *fs_info)
2580{
2581 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2582
2583 BUG_ON(!fs_info->balance_ctl);
2584
2585 spin_lock(&fs_info->balance_lock);
2586 fs_info->balance_ctl = NULL;
2587 spin_unlock(&fs_info->balance_lock);
2588
2589 kfree(bctl);
2590}
2591
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002592/*
2593 * Balance filters. Return 1 if chunk should be filtered out
2594 * (should not be balanced).
2595 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002596static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002597 struct btrfs_balance_args *bargs)
2598{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002599 chunk_type = chunk_to_extended(chunk_type) &
2600 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002601
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002602 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002603 return 0;
2604
2605 return 1;
2606}
2607
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002608static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2609 struct btrfs_balance_args *bargs)
2610{
2611 struct btrfs_block_group_cache *cache;
2612 u64 chunk_used, user_thresh;
2613 int ret = 1;
2614
2615 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2616 chunk_used = btrfs_block_group_used(&cache->item);
2617
2618 user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
2619 if (chunk_used < user_thresh)
2620 ret = 0;
2621
2622 btrfs_put_block_group(cache);
2623 return ret;
2624}
2625
Ilya Dryomov409d4042012-01-16 22:04:47 +02002626static int chunk_devid_filter(struct extent_buffer *leaf,
2627 struct btrfs_chunk *chunk,
2628 struct btrfs_balance_args *bargs)
2629{
2630 struct btrfs_stripe *stripe;
2631 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2632 int i;
2633
2634 for (i = 0; i < num_stripes; i++) {
2635 stripe = btrfs_stripe_nr(chunk, i);
2636 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2637 return 0;
2638 }
2639
2640 return 1;
2641}
2642
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002643/* [pstart, pend) */
2644static int chunk_drange_filter(struct extent_buffer *leaf,
2645 struct btrfs_chunk *chunk,
2646 u64 chunk_offset,
2647 struct btrfs_balance_args *bargs)
2648{
2649 struct btrfs_stripe *stripe;
2650 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2651 u64 stripe_offset;
2652 u64 stripe_length;
2653 int factor;
2654 int i;
2655
2656 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2657 return 0;
2658
2659 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2660 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2661 factor = 2;
2662 else
2663 factor = 1;
2664 factor = num_stripes / factor;
2665
2666 for (i = 0; i < num_stripes; i++) {
2667 stripe = btrfs_stripe_nr(chunk, i);
2668 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2669 continue;
2670
2671 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2672 stripe_length = btrfs_chunk_length(leaf, chunk);
2673 do_div(stripe_length, factor);
2674
2675 if (stripe_offset < bargs->pend &&
2676 stripe_offset + stripe_length > bargs->pstart)
2677 return 0;
2678 }
2679
2680 return 1;
2681}
2682
Ilya Dryomovea671762012-01-16 22:04:48 +02002683/* [vstart, vend) */
2684static int chunk_vrange_filter(struct extent_buffer *leaf,
2685 struct btrfs_chunk *chunk,
2686 u64 chunk_offset,
2687 struct btrfs_balance_args *bargs)
2688{
2689 if (chunk_offset < bargs->vend &&
2690 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2691 /* at least part of the chunk is inside this vrange */
2692 return 0;
2693
2694 return 1;
2695}
2696
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002697static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002698 struct btrfs_balance_args *bargs)
2699{
2700 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2701 return 0;
2702
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002703 chunk_type = chunk_to_extended(chunk_type) &
2704 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002705
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002706 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002707 return 1;
2708
2709 return 0;
2710}
2711
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002712static int should_balance_chunk(struct btrfs_root *root,
2713 struct extent_buffer *leaf,
2714 struct btrfs_chunk *chunk, u64 chunk_offset)
2715{
2716 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2717 struct btrfs_balance_args *bargs = NULL;
2718 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2719
2720 /* type filter */
2721 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2722 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2723 return 0;
2724 }
2725
2726 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2727 bargs = &bctl->data;
2728 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2729 bargs = &bctl->sys;
2730 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2731 bargs = &bctl->meta;
2732
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002733 /* profiles filter */
2734 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2735 chunk_profiles_filter(chunk_type, bargs)) {
2736 return 0;
2737 }
2738
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002739 /* usage filter */
2740 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2741 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2742 return 0;
2743 }
2744
Ilya Dryomov409d4042012-01-16 22:04:47 +02002745 /* devid filter */
2746 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2747 chunk_devid_filter(leaf, chunk, bargs)) {
2748 return 0;
2749 }
2750
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002751 /* drange filter, makes sense only with devid filter */
2752 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2753 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2754 return 0;
2755 }
2756
Ilya Dryomovea671762012-01-16 22:04:48 +02002757 /* vrange filter */
2758 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2759 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2760 return 0;
2761 }
2762
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002763 /* soft profile changing mode */
2764 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2765 chunk_soft_convert_filter(chunk_type, bargs)) {
2766 return 0;
2767 }
2768
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002769 return 1;
2770}
2771
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002772static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002773{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002774 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002775 struct btrfs_root *chunk_root = fs_info->chunk_root;
2776 struct btrfs_root *dev_root = fs_info->dev_root;
2777 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002778 struct btrfs_device *device;
2779 u64 old_size;
2780 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002781 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002782 struct btrfs_path *path;
2783 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002784 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002785 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002786 struct extent_buffer *leaf;
2787 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002788 int ret;
2789 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002790 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002791
Chris Masonec44a352008-04-28 15:29:52 -04002792 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002793 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002794 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002795 old_size = device->total_bytes;
2796 size_to_free = div_factor(old_size, 1);
2797 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002798 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002799 device->total_bytes - device->bytes_used > size_to_free ||
2800 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002801 continue;
2802
2803 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002804 if (ret == -ENOSPC)
2805 break;
Chris Masonec44a352008-04-28 15:29:52 -04002806 BUG_ON(ret);
2807
Yan, Zhenga22285a2010-05-16 10:48:46 -04002808 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002809 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002810
2811 ret = btrfs_grow_device(trans, device, old_size);
2812 BUG_ON(ret);
2813
2814 btrfs_end_transaction(trans, dev_root);
2815 }
2816
2817 /* step two, relocate all the chunks */
2818 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002819 if (!path) {
2820 ret = -ENOMEM;
2821 goto error;
2822 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002823
2824 /* zero out stat counters */
2825 spin_lock(&fs_info->balance_lock);
2826 memset(&bctl->stat, 0, sizeof(bctl->stat));
2827 spin_unlock(&fs_info->balance_lock);
2828again:
Chris Masonec44a352008-04-28 15:29:52 -04002829 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2830 key.offset = (u64)-1;
2831 key.type = BTRFS_CHUNK_ITEM_KEY;
2832
Chris Masond3977122009-01-05 21:25:51 -05002833 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002834 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002835 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002836 ret = -ECANCELED;
2837 goto error;
2838 }
2839
Chris Masonec44a352008-04-28 15:29:52 -04002840 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2841 if (ret < 0)
2842 goto error;
2843
2844 /*
2845 * this shouldn't happen, it means the last relocate
2846 * failed
2847 */
2848 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002849 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002850
2851 ret = btrfs_previous_item(chunk_root, path, 0,
2852 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002853 if (ret) {
2854 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002855 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002856 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002857
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002858 leaf = path->nodes[0];
2859 slot = path->slots[0];
2860 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2861
Chris Masonec44a352008-04-28 15:29:52 -04002862 if (found_key.objectid != key.objectid)
2863 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002864
Chris Masonec44a352008-04-28 15:29:52 -04002865 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002866 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002867 break;
2868
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002869 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2870
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002871 if (!counting) {
2872 spin_lock(&fs_info->balance_lock);
2873 bctl->stat.considered++;
2874 spin_unlock(&fs_info->balance_lock);
2875 }
2876
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002877 ret = should_balance_chunk(chunk_root, leaf, chunk,
2878 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002879 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002880 if (!ret)
2881 goto loop;
2882
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002883 if (counting) {
2884 spin_lock(&fs_info->balance_lock);
2885 bctl->stat.expected++;
2886 spin_unlock(&fs_info->balance_lock);
2887 goto loop;
2888 }
2889
Chris Masonec44a352008-04-28 15:29:52 -04002890 ret = btrfs_relocate_chunk(chunk_root,
2891 chunk_root->root_key.objectid,
2892 found_key.objectid,
2893 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002894 if (ret && ret != -ENOSPC)
2895 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002896 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002897 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002898 } else {
2899 spin_lock(&fs_info->balance_lock);
2900 bctl->stat.completed++;
2901 spin_unlock(&fs_info->balance_lock);
2902 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002903loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002904 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002905 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002906
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002907 if (counting) {
2908 btrfs_release_path(path);
2909 counting = false;
2910 goto again;
2911 }
Chris Masonec44a352008-04-28 15:29:52 -04002912error:
2913 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002914 if (enospc_errors) {
2915 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2916 enospc_errors);
2917 if (!ret)
2918 ret = -ENOSPC;
2919 }
2920
Chris Masonec44a352008-04-28 15:29:52 -04002921 return ret;
2922}
2923
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002924/**
2925 * alloc_profile_is_valid - see if a given profile is valid and reduced
2926 * @flags: profile to validate
2927 * @extended: if true @flags is treated as an extended profile
2928 */
2929static int alloc_profile_is_valid(u64 flags, int extended)
2930{
2931 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2932 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2933
2934 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2935
2936 /* 1) check that all other bits are zeroed */
2937 if (flags & ~mask)
2938 return 0;
2939
2940 /* 2) see if profile is reduced */
2941 if (flags == 0)
2942 return !extended; /* "0" is valid for usual profiles */
2943
2944 /* true if exactly one bit set */
2945 return (flags & (flags - 1)) == 0;
2946}
2947
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002948static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2949{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002950 /* cancel requested || normal exit path */
2951 return atomic_read(&fs_info->balance_cancel_req) ||
2952 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2953 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002954}
2955
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002956static void __cancel_balance(struct btrfs_fs_info *fs_info)
2957{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002958 int ret;
2959
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002960 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002961 ret = del_balance_item(fs_info->tree_root);
2962 BUG_ON(ret);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002963}
2964
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002965void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002966 struct btrfs_ioctl_balance_args *bargs);
2967
2968/*
2969 * Should be called with both balance and volume mutexes held
2970 */
2971int btrfs_balance(struct btrfs_balance_control *bctl,
2972 struct btrfs_ioctl_balance_args *bargs)
2973{
2974 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002975 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002976 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002977 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01002978 u64 num_devices;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002979
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002980 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002981 atomic_read(&fs_info->balance_pause_req) ||
2982 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002983 ret = -EINVAL;
2984 goto out;
2985 }
2986
Ilya Dryomove4837f82012-03-27 17:09:17 +03002987 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2988 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2989 mixed = 1;
2990
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002991 /*
2992 * In case of mixed groups both data and meta should be picked,
2993 * and identical options should be given for both of them.
2994 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03002995 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
2996 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002997 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
2998 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
2999 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3000 printk(KERN_ERR "btrfs: with mixed groups data and "
3001 "metadata balance options must be the same\n");
3002 ret = -EINVAL;
3003 goto out;
3004 }
3005 }
3006
Stefan Behrens8dabb742012-11-06 13:15:27 +01003007 num_devices = fs_info->fs_devices->num_devices;
3008 btrfs_dev_replace_lock(&fs_info->dev_replace);
3009 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3010 BUG_ON(num_devices < 1);
3011 num_devices--;
3012 }
3013 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003014 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003015 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003016 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003017 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003018 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3019 else
3020 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3021 BTRFS_BLOCK_GROUP_RAID10);
3022
Ilya Dryomov6728b192012-03-27 17:09:17 +03003023 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3024 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3025 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003026 printk(KERN_ERR "btrfs: unable to start balance with target "
3027 "data profile %llu\n",
3028 (unsigned long long)bctl->data.target);
3029 ret = -EINVAL;
3030 goto out;
3031 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003032 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3033 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3034 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003035 printk(KERN_ERR "btrfs: unable to start balance with target "
3036 "metadata profile %llu\n",
3037 (unsigned long long)bctl->meta.target);
3038 ret = -EINVAL;
3039 goto out;
3040 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003041 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3042 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3043 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003044 printk(KERN_ERR "btrfs: unable to start balance with target "
3045 "system profile %llu\n",
3046 (unsigned long long)bctl->sys.target);
3047 ret = -EINVAL;
3048 goto out;
3049 }
3050
Ilya Dryomove4837f82012-03-27 17:09:17 +03003051 /* allow dup'ed data chunks only in mixed mode */
3052 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003053 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003054 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3055 ret = -EINVAL;
3056 goto out;
3057 }
3058
3059 /* allow to reduce meta or sys integrity only if force set */
3060 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3061 BTRFS_BLOCK_GROUP_RAID10;
3062 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3063 (fs_info->avail_system_alloc_bits & allowed) &&
3064 !(bctl->sys.target & allowed)) ||
3065 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3066 (fs_info->avail_metadata_alloc_bits & allowed) &&
3067 !(bctl->meta.target & allowed))) {
3068 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3069 printk(KERN_INFO "btrfs: force reducing metadata "
3070 "integrity\n");
3071 } else {
3072 printk(KERN_ERR "btrfs: balance will reduce metadata "
3073 "integrity, use force if you want this\n");
3074 ret = -EINVAL;
3075 goto out;
3076 }
3077 }
3078
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003079 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3080 int num_tolerated_disk_barrier_failures;
3081 u64 target = bctl->sys.target;
3082
3083 num_tolerated_disk_barrier_failures =
3084 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3085 if (num_tolerated_disk_barrier_failures > 0 &&
3086 (target &
3087 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3088 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3089 num_tolerated_disk_barrier_failures = 0;
3090 else if (num_tolerated_disk_barrier_failures > 1 &&
3091 (target &
3092 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3093 num_tolerated_disk_barrier_failures = 1;
3094
3095 fs_info->num_tolerated_disk_barrier_failures =
3096 num_tolerated_disk_barrier_failures;
3097 }
3098
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003099 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003100 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003101 goto out;
3102
Ilya Dryomov59641012012-01-16 22:04:48 +02003103 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3104 BUG_ON(ret == -EEXIST);
3105 set_balance_control(bctl);
3106 } else {
3107 BUG_ON(ret != -EEXIST);
3108 spin_lock(&fs_info->balance_lock);
3109 update_balance_args(bctl);
3110 spin_unlock(&fs_info->balance_lock);
3111 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003112
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003113 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003114 mutex_unlock(&fs_info->balance_mutex);
3115
3116 ret = __btrfs_balance(fs_info);
3117
3118 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003119 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003120
3121 if (bargs) {
3122 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003123 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003124 }
3125
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003126 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3127 balance_need_close(fs_info)) {
3128 __cancel_balance(fs_info);
3129 }
3130
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003131 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3132 fs_info->num_tolerated_disk_barrier_failures =
3133 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3134 }
3135
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003136 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003137
3138 return ret;
3139out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003140 if (bctl->flags & BTRFS_BALANCE_RESUME)
3141 __cancel_balance(fs_info);
3142 else
3143 kfree(bctl);
3144 return ret;
3145}
3146
3147static int balance_kthread(void *data)
3148{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003149 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003150 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003151
3152 mutex_lock(&fs_info->volume_mutex);
3153 mutex_lock(&fs_info->balance_mutex);
3154
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003155 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003156 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003157 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003158 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003159
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003160 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomov59641012012-01-16 22:04:48 +02003161 mutex_unlock(&fs_info->balance_mutex);
3162 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003163
Ilya Dryomov59641012012-01-16 22:04:48 +02003164 return ret;
3165}
3166
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003167int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3168{
3169 struct task_struct *tsk;
3170
3171 spin_lock(&fs_info->balance_lock);
3172 if (!fs_info->balance_ctl) {
3173 spin_unlock(&fs_info->balance_lock);
3174 return 0;
3175 }
3176 spin_unlock(&fs_info->balance_lock);
3177
3178 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3179 printk(KERN_INFO "btrfs: force skipping balance\n");
3180 return 0;
3181 }
3182
Stefan Behrens5ac00ad2012-11-05 17:54:08 +01003183 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003184 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3185 if (IS_ERR(tsk))
3186 return PTR_ERR(tsk);
3187
3188 return 0;
3189}
3190
Ilya Dryomov68310a52012-06-22 12:24:12 -06003191int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003192{
Ilya Dryomov59641012012-01-16 22:04:48 +02003193 struct btrfs_balance_control *bctl;
3194 struct btrfs_balance_item *item;
3195 struct btrfs_disk_balance_args disk_bargs;
3196 struct btrfs_path *path;
3197 struct extent_buffer *leaf;
3198 struct btrfs_key key;
3199 int ret;
3200
3201 path = btrfs_alloc_path();
3202 if (!path)
3203 return -ENOMEM;
3204
Ilya Dryomov68310a52012-06-22 12:24:12 -06003205 key.objectid = BTRFS_BALANCE_OBJECTID;
3206 key.type = BTRFS_BALANCE_ITEM_KEY;
3207 key.offset = 0;
3208
3209 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3210 if (ret < 0)
3211 goto out;
3212 if (ret > 0) { /* ret = -ENOENT; */
3213 ret = 0;
3214 goto out;
3215 }
3216
Ilya Dryomov59641012012-01-16 22:04:48 +02003217 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3218 if (!bctl) {
3219 ret = -ENOMEM;
3220 goto out;
3221 }
3222
Ilya Dryomov59641012012-01-16 22:04:48 +02003223 leaf = path->nodes[0];
3224 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3225
Ilya Dryomov68310a52012-06-22 12:24:12 -06003226 bctl->fs_info = fs_info;
3227 bctl->flags = btrfs_balance_flags(leaf, item);
3228 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003229
3230 btrfs_balance_data(leaf, item, &disk_bargs);
3231 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3232 btrfs_balance_meta(leaf, item, &disk_bargs);
3233 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3234 btrfs_balance_sys(leaf, item, &disk_bargs);
3235 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3236
Ilya Dryomov68310a52012-06-22 12:24:12 -06003237 mutex_lock(&fs_info->volume_mutex);
3238 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003239
Ilya Dryomov68310a52012-06-22 12:24:12 -06003240 set_balance_control(bctl);
3241
3242 mutex_unlock(&fs_info->balance_mutex);
3243 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003244out:
3245 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003246 return ret;
3247}
3248
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003249int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3250{
3251 int ret = 0;
3252
3253 mutex_lock(&fs_info->balance_mutex);
3254 if (!fs_info->balance_ctl) {
3255 mutex_unlock(&fs_info->balance_mutex);
3256 return -ENOTCONN;
3257 }
3258
3259 if (atomic_read(&fs_info->balance_running)) {
3260 atomic_inc(&fs_info->balance_pause_req);
3261 mutex_unlock(&fs_info->balance_mutex);
3262
3263 wait_event(fs_info->balance_wait_q,
3264 atomic_read(&fs_info->balance_running) == 0);
3265
3266 mutex_lock(&fs_info->balance_mutex);
3267 /* we are good with balance_ctl ripped off from under us */
3268 BUG_ON(atomic_read(&fs_info->balance_running));
3269 atomic_dec(&fs_info->balance_pause_req);
3270 } else {
3271 ret = -ENOTCONN;
3272 }
3273
3274 mutex_unlock(&fs_info->balance_mutex);
3275 return ret;
3276}
3277
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003278int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3279{
3280 mutex_lock(&fs_info->balance_mutex);
3281 if (!fs_info->balance_ctl) {
3282 mutex_unlock(&fs_info->balance_mutex);
3283 return -ENOTCONN;
3284 }
3285
3286 atomic_inc(&fs_info->balance_cancel_req);
3287 /*
3288 * if we are running just wait and return, balance item is
3289 * deleted in btrfs_balance in this case
3290 */
3291 if (atomic_read(&fs_info->balance_running)) {
3292 mutex_unlock(&fs_info->balance_mutex);
3293 wait_event(fs_info->balance_wait_q,
3294 atomic_read(&fs_info->balance_running) == 0);
3295 mutex_lock(&fs_info->balance_mutex);
3296 } else {
3297 /* __cancel_balance needs volume_mutex */
3298 mutex_unlock(&fs_info->balance_mutex);
3299 mutex_lock(&fs_info->volume_mutex);
3300 mutex_lock(&fs_info->balance_mutex);
3301
3302 if (fs_info->balance_ctl)
3303 __cancel_balance(fs_info);
3304
3305 mutex_unlock(&fs_info->volume_mutex);
3306 }
3307
3308 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3309 atomic_dec(&fs_info->balance_cancel_req);
3310 mutex_unlock(&fs_info->balance_mutex);
3311 return 0;
3312}
3313
Chris Mason8f18cf12008-04-25 16:53:30 -04003314/*
3315 * shrinking a device means finding all of the device extents past
3316 * the new size, and then following the back refs to the chunks.
3317 * The chunk relocation code actually frees the device extent
3318 */
3319int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3320{
3321 struct btrfs_trans_handle *trans;
3322 struct btrfs_root *root = device->dev_root;
3323 struct btrfs_dev_extent *dev_extent = NULL;
3324 struct btrfs_path *path;
3325 u64 length;
3326 u64 chunk_tree;
3327 u64 chunk_objectid;
3328 u64 chunk_offset;
3329 int ret;
3330 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003331 int failed = 0;
3332 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003333 struct extent_buffer *l;
3334 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003335 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003336 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003337 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003338 u64 diff = device->total_bytes - new_size;
3339
Stefan Behrens63a212a2012-11-05 18:29:28 +01003340 if (device->is_tgtdev_for_dev_replace)
3341 return -EINVAL;
3342
Chris Mason8f18cf12008-04-25 16:53:30 -04003343 path = btrfs_alloc_path();
3344 if (!path)
3345 return -ENOMEM;
3346
Chris Mason8f18cf12008-04-25 16:53:30 -04003347 path->reada = 2;
3348
Chris Mason7d9eb122008-07-08 14:19:17 -04003349 lock_chunks(root);
3350
Chris Mason8f18cf12008-04-25 16:53:30 -04003351 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003352 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003353 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003354 spin_lock(&root->fs_info->free_chunk_lock);
3355 root->fs_info->free_chunk_space -= diff;
3356 spin_unlock(&root->fs_info->free_chunk_lock);
3357 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003358 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003359
Josef Bacikba1bf482009-09-11 16:11:19 -04003360again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003361 key.objectid = device->devid;
3362 key.offset = (u64)-1;
3363 key.type = BTRFS_DEV_EXTENT_KEY;
3364
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003365 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003366 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3367 if (ret < 0)
3368 goto done;
3369
3370 ret = btrfs_previous_item(root, path, 0, key.type);
3371 if (ret < 0)
3372 goto done;
3373 if (ret) {
3374 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003375 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003376 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003377 }
3378
3379 l = path->nodes[0];
3380 slot = path->slots[0];
3381 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3382
Josef Bacikba1bf482009-09-11 16:11:19 -04003383 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003384 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003385 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003386 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003387
3388 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3389 length = btrfs_dev_extent_length(l, dev_extent);
3390
Josef Bacikba1bf482009-09-11 16:11:19 -04003391 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003392 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003393 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003394 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003395
3396 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3397 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3398 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003399 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003400
3401 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3402 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003403 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003404 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003405 if (ret == -ENOSPC)
3406 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003407 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003408
3409 if (failed && !retried) {
3410 failed = 0;
3411 retried = true;
3412 goto again;
3413 } else if (failed && retried) {
3414 ret = -ENOSPC;
3415 lock_chunks(root);
3416
3417 device->total_bytes = old_size;
3418 if (device->writeable)
3419 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003420 spin_lock(&root->fs_info->free_chunk_lock);
3421 root->fs_info->free_chunk_space += diff;
3422 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003423 unlock_chunks(root);
3424 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003425 }
3426
Chris Balld6397ba2009-04-27 07:29:03 -04003427 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003428 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003429 if (IS_ERR(trans)) {
3430 ret = PTR_ERR(trans);
3431 goto done;
3432 }
3433
Chris Balld6397ba2009-04-27 07:29:03 -04003434 lock_chunks(root);
3435
3436 device->disk_total_bytes = new_size;
3437 /* Now btrfs_update_device() will change the on-disk size. */
3438 ret = btrfs_update_device(trans, device);
3439 if (ret) {
3440 unlock_chunks(root);
3441 btrfs_end_transaction(trans, root);
3442 goto done;
3443 }
3444 WARN_ON(diff > old_total);
3445 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3446 unlock_chunks(root);
3447 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003448done:
3449 btrfs_free_path(path);
3450 return ret;
3451}
3452
Li Zefan125ccb02011-12-08 15:07:24 +08003453static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003454 struct btrfs_key *key,
3455 struct btrfs_chunk *chunk, int item_size)
3456{
David Sterba6c417612011-04-13 15:41:04 +02003457 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003458 struct btrfs_disk_key disk_key;
3459 u32 array_size;
3460 u8 *ptr;
3461
3462 array_size = btrfs_super_sys_array_size(super_copy);
3463 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3464 return -EFBIG;
3465
3466 ptr = super_copy->sys_chunk_array + array_size;
3467 btrfs_cpu_key_to_disk(&disk_key, key);
3468 memcpy(ptr, &disk_key, sizeof(disk_key));
3469 ptr += sizeof(disk_key);
3470 memcpy(ptr, chunk, item_size);
3471 item_size += sizeof(disk_key);
3472 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3473 return 0;
3474}
3475
Miao Xieb2117a32011-01-05 10:07:28 +00003476/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003477 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003478 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003479static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003480{
Arne Jansen73c5de02011-04-12 12:07:57 +02003481 const struct btrfs_device_info *di_a = a;
3482 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003483
Arne Jansen73c5de02011-04-12 12:07:57 +02003484 if (di_a->max_avail > di_b->max_avail)
3485 return -1;
3486 if (di_a->max_avail < di_b->max_avail)
3487 return 1;
3488 if (di_a->total_avail > di_b->total_avail)
3489 return -1;
3490 if (di_a->total_avail < di_b->total_avail)
3491 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003492 return 0;
3493}
3494
Liu Bo31e50222012-11-21 14:18:10 +00003495struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
3496 { 2, 1, 0, 4, 2, 2 /* raid10 */ },
3497 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3498 { 1, 2, 1, 1, 1, 2 /* dup */ },
3499 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3500 { 1, 1, 0, 1, 1, 1 /* single */ },
3501};
3502
Miao Xieb2117a32011-01-05 10:07:28 +00003503static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3504 struct btrfs_root *extent_root,
3505 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003506 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003507 u64 start, u64 type)
3508{
3509 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003510 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3511 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003512 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003513 struct extent_map_tree *em_tree;
3514 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003515 struct btrfs_device_info *devices_info = NULL;
3516 u64 total_avail;
3517 int num_stripes; /* total number of stripes to allocate */
3518 int sub_stripes; /* sub_stripes info for map */
3519 int dev_stripes; /* stripes per dev */
3520 int devs_max; /* max devs to use */
3521 int devs_min; /* min devs needed */
3522 int devs_increment; /* ndevs has to be a multiple of this */
3523 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003524 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003525 u64 max_stripe_size;
3526 u64 max_chunk_size;
3527 u64 stripe_size;
3528 u64 num_bytes;
3529 int ndevs;
3530 int i;
3531 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003532 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003533
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003534 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003535
Miao Xieb2117a32011-01-05 10:07:28 +00003536 if (list_empty(&fs_devices->alloc_list))
3537 return -ENOSPC;
3538
Liu Bo31e50222012-11-21 14:18:10 +00003539 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003540
Liu Bo31e50222012-11-21 14:18:10 +00003541 sub_stripes = btrfs_raid_array[index].sub_stripes;
3542 dev_stripes = btrfs_raid_array[index].dev_stripes;
3543 devs_max = btrfs_raid_array[index].devs_max;
3544 devs_min = btrfs_raid_array[index].devs_min;
3545 devs_increment = btrfs_raid_array[index].devs_increment;
3546 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003547
3548 if (type & BTRFS_BLOCK_GROUP_DATA) {
3549 max_stripe_size = 1024 * 1024 * 1024;
3550 max_chunk_size = 10 * max_stripe_size;
3551 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003552 /* for larger filesystems, use larger metadata chunks */
3553 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3554 max_stripe_size = 1024 * 1024 * 1024;
3555 else
3556 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003557 max_chunk_size = max_stripe_size;
3558 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003559 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003560 max_chunk_size = 2 * max_stripe_size;
3561 } else {
3562 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3563 type);
3564 BUG_ON(1);
3565 }
3566
3567 /* we don't want a chunk larger than 10% of writeable space */
3568 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3569 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003570
3571 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3572 GFP_NOFS);
3573 if (!devices_info)
3574 return -ENOMEM;
3575
Arne Jansen73c5de02011-04-12 12:07:57 +02003576 cur = fs_devices->alloc_list.next;
3577
3578 /*
3579 * in the first pass through the devices list, we gather information
3580 * about the available holes on each device.
3581 */
3582 ndevs = 0;
3583 while (cur != &fs_devices->alloc_list) {
3584 struct btrfs_device *device;
3585 u64 max_avail;
3586 u64 dev_offset;
3587
3588 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3589
3590 cur = cur->next;
3591
3592 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003593 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003594 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003595 continue;
3596 }
3597
Stefan Behrens63a212a2012-11-05 18:29:28 +01003598 if (!device->in_fs_metadata ||
3599 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003600 continue;
3601
3602 if (device->total_bytes > device->bytes_used)
3603 total_avail = device->total_bytes - device->bytes_used;
3604 else
3605 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003606
3607 /* If there is no space on this device, skip it. */
3608 if (total_avail == 0)
3609 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003610
Li Zefan125ccb02011-12-08 15:07:24 +08003611 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003612 max_stripe_size * dev_stripes,
3613 &dev_offset, &max_avail);
3614 if (ret && ret != -ENOSPC)
3615 goto error;
3616
3617 if (ret == 0)
3618 max_avail = max_stripe_size * dev_stripes;
3619
3620 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3621 continue;
3622
3623 devices_info[ndevs].dev_offset = dev_offset;
3624 devices_info[ndevs].max_avail = max_avail;
3625 devices_info[ndevs].total_avail = total_avail;
3626 devices_info[ndevs].dev = device;
3627 ++ndevs;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003628 WARN_ON(ndevs > fs_devices->rw_devices);
Arne Jansen73c5de02011-04-12 12:07:57 +02003629 }
3630
3631 /*
3632 * now sort the devices by hole size / available space
3633 */
3634 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3635 btrfs_cmp_device_info, NULL);
3636
3637 /* round down to number of usable stripes */
3638 ndevs -= ndevs % devs_increment;
3639
3640 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3641 ret = -ENOSPC;
3642 goto error;
3643 }
3644
3645 if (devs_max && ndevs > devs_max)
3646 ndevs = devs_max;
3647 /*
3648 * the primary goal is to maximize the number of stripes, so use as many
3649 * devices as possible, even if the stripes are not maximum sized.
3650 */
3651 stripe_size = devices_info[ndevs-1].max_avail;
3652 num_stripes = ndevs * dev_stripes;
3653
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003654 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003655 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003656 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003657 }
3658
3659 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003660
3661 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003662 do_div(stripe_size, BTRFS_STRIPE_LEN);
3663 stripe_size *= BTRFS_STRIPE_LEN;
3664
Miao Xieb2117a32011-01-05 10:07:28 +00003665 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3666 if (!map) {
3667 ret = -ENOMEM;
3668 goto error;
3669 }
3670 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003671
Arne Jansen73c5de02011-04-12 12:07:57 +02003672 for (i = 0; i < ndevs; ++i) {
3673 for (j = 0; j < dev_stripes; ++j) {
3674 int s = i * dev_stripes + j;
3675 map->stripes[s].dev = devices_info[i].dev;
3676 map->stripes[s].physical = devices_info[i].dev_offset +
3677 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003678 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003679 }
Chris Mason593060d2008-03-25 16:50:33 -04003680 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003681 map->stripe_len = BTRFS_STRIPE_LEN;
3682 map->io_align = BTRFS_STRIPE_LEN;
3683 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003684 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003685 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003686
Yan Zheng2b820322008-11-17 21:11:30 -05003687 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003688 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003689
Arne Jansen73c5de02011-04-12 12:07:57 +02003690 *stripe_size_out = stripe_size;
3691 *num_bytes_out = num_bytes;
3692
3693 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003694
David Sterba172ddd62011-04-21 00:48:27 +02003695 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003696 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003697 ret = -ENOMEM;
3698 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003699 }
Chris Mason0b86a832008-03-24 15:01:56 -04003700 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003701 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003702 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003703 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003704 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003705
Chris Mason0b86a832008-03-24 15:01:56 -04003706 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003707 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003708 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003709 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003710 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003711 if (ret)
3712 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003713
3714 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3715 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003716 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003717 if (ret)
3718 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003719
Arne Jansen73c5de02011-04-12 12:07:57 +02003720 for (i = 0; i < map->num_stripes; ++i) {
3721 struct btrfs_device *device;
3722 u64 dev_offset;
3723
3724 device = map->stripes[i].dev;
3725 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003726
3727 ret = btrfs_alloc_dev_extent(trans, device,
3728 info->chunk_root->root_key.objectid,
3729 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003730 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003731 if (ret) {
3732 btrfs_abort_transaction(trans, extent_root, ret);
3733 goto error;
3734 }
Yan Zheng2b820322008-11-17 21:11:30 -05003735 }
3736
Miao Xieb2117a32011-01-05 10:07:28 +00003737 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003738 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003739
3740error:
3741 kfree(map);
3742 kfree(devices_info);
3743 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003744}
3745
3746static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3747 struct btrfs_root *extent_root,
3748 struct map_lookup *map, u64 chunk_offset,
3749 u64 chunk_size, u64 stripe_size)
3750{
3751 u64 dev_offset;
3752 struct btrfs_key key;
3753 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3754 struct btrfs_device *device;
3755 struct btrfs_chunk *chunk;
3756 struct btrfs_stripe *stripe;
3757 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3758 int index = 0;
3759 int ret;
3760
3761 chunk = kzalloc(item_size, GFP_NOFS);
3762 if (!chunk)
3763 return -ENOMEM;
3764
3765 index = 0;
3766 while (index < map->num_stripes) {
3767 device = map->stripes[index].dev;
3768 device->bytes_used += stripe_size;
3769 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003770 if (ret)
3771 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003772 index++;
3773 }
3774
Josef Bacik2bf64752011-09-26 17:12:22 -04003775 spin_lock(&extent_root->fs_info->free_chunk_lock);
3776 extent_root->fs_info->free_chunk_space -= (stripe_size *
3777 map->num_stripes);
3778 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3779
Yan Zheng2b820322008-11-17 21:11:30 -05003780 index = 0;
3781 stripe = &chunk->stripe;
3782 while (index < map->num_stripes) {
3783 device = map->stripes[index].dev;
3784 dev_offset = map->stripes[index].physical;
3785
3786 btrfs_set_stack_stripe_devid(stripe, device->devid);
3787 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3788 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3789 stripe++;
3790 index++;
3791 }
3792
3793 btrfs_set_stack_chunk_length(chunk, chunk_size);
3794 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3795 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3796 btrfs_set_stack_chunk_type(chunk, map->type);
3797 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3798 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3799 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3800 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3801 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3802
3803 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3804 key.type = BTRFS_CHUNK_ITEM_KEY;
3805 key.offset = chunk_offset;
3806
3807 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003808
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003809 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3810 /*
3811 * TODO: Cleanup of inserted chunk root in case of
3812 * failure.
3813 */
Li Zefan125ccb02011-12-08 15:07:24 +08003814 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003815 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003816 }
liubo1abe9b82011-03-24 11:18:59 +00003817
Mark Fasheh3acd3952011-09-08 17:40:01 -07003818out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003819 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003820 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003821}
3822
3823/*
3824 * Chunk allocation falls into two parts. The first part does works
3825 * that make the new allocated chunk useable, but not do any operation
3826 * that modifies the chunk tree. The second part does the works that
3827 * require modifying the chunk tree. This division is important for the
3828 * bootstrap process of adding storage to a seed btrfs.
3829 */
3830int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3831 struct btrfs_root *extent_root, u64 type)
3832{
3833 u64 chunk_offset;
3834 u64 chunk_size;
3835 u64 stripe_size;
3836 struct map_lookup *map;
3837 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3838 int ret;
3839
3840 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3841 &chunk_offset);
3842 if (ret)
3843 return ret;
3844
3845 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3846 &stripe_size, chunk_offset, type);
3847 if (ret)
3848 return ret;
3849
3850 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3851 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003852 if (ret)
3853 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003854 return 0;
3855}
3856
Chris Masond3977122009-01-05 21:25:51 -05003857static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003858 struct btrfs_root *root,
3859 struct btrfs_device *device)
3860{
3861 u64 chunk_offset;
3862 u64 sys_chunk_offset;
3863 u64 chunk_size;
3864 u64 sys_chunk_size;
3865 u64 stripe_size;
3866 u64 sys_stripe_size;
3867 u64 alloc_profile;
3868 struct map_lookup *map;
3869 struct map_lookup *sys_map;
3870 struct btrfs_fs_info *fs_info = root->fs_info;
3871 struct btrfs_root *extent_root = fs_info->extent_root;
3872 int ret;
3873
3874 ret = find_next_chunk(fs_info->chunk_root,
3875 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e892011-07-12 10:57:59 -07003876 if (ret)
3877 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003878
3879 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003880 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003881 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3882
3883 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3884 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003885 if (ret)
3886 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003887
3888 sys_chunk_offset = chunk_offset + chunk_size;
3889
3890 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003891 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003892 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3893
3894 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3895 &sys_chunk_size, &sys_stripe_size,
3896 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003897 if (ret) {
3898 btrfs_abort_transaction(trans, root, ret);
3899 goto out;
3900 }
Yan Zheng2b820322008-11-17 21:11:30 -05003901
3902 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003903 if (ret) {
3904 btrfs_abort_transaction(trans, root, ret);
3905 goto out;
3906 }
Yan Zheng2b820322008-11-17 21:11:30 -05003907
3908 /*
3909 * Modifying chunk tree needs allocating new blocks from both
3910 * system block group and metadata block group. So we only can
3911 * do operations require modifying the chunk tree after both
3912 * block groups were created.
3913 */
3914 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3915 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003916 if (ret) {
3917 btrfs_abort_transaction(trans, root, ret);
3918 goto out;
3919 }
Yan Zheng2b820322008-11-17 21:11:30 -05003920
3921 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3922 sys_chunk_offset, sys_chunk_size,
3923 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003924 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003925 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003926
David Sterba005d6422012-09-18 07:52:32 -06003927out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003928
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003929 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003930}
3931
3932int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3933{
3934 struct extent_map *em;
3935 struct map_lookup *map;
3936 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3937 int readonly = 0;
3938 int i;
3939
Chris Mason890871b2009-09-02 16:24:52 -04003940 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003941 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003942 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003943 if (!em)
3944 return 1;
3945
Josef Bacikf48b9072010-01-27 02:07:59 +00003946 if (btrfs_test_opt(root, DEGRADED)) {
3947 free_extent_map(em);
3948 return 0;
3949 }
3950
Yan Zheng2b820322008-11-17 21:11:30 -05003951 map = (struct map_lookup *)em->bdev;
3952 for (i = 0; i < map->num_stripes; i++) {
3953 if (!map->stripes[i].dev->writeable) {
3954 readonly = 1;
3955 break;
3956 }
3957 }
3958 free_extent_map(em);
3959 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003960}
3961
3962void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3963{
David Sterbaa8067e02011-04-21 00:34:43 +02003964 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003965}
3966
3967void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3968{
3969 struct extent_map *em;
3970
Chris Masond3977122009-01-05 21:25:51 -05003971 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003972 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003973 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3974 if (em)
3975 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003976 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003977 if (!em)
3978 break;
3979 kfree(em->bdev);
3980 /* once for us */
3981 free_extent_map(em);
3982 /* once for the tree */
3983 free_extent_map(em);
3984 }
3985}
3986
Stefan Behrens5d964052012-11-05 14:59:07 +01003987int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04003988{
Stefan Behrens5d964052012-11-05 14:59:07 +01003989 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04003990 struct extent_map *em;
3991 struct map_lookup *map;
3992 struct extent_map_tree *em_tree = &map_tree->map_tree;
3993 int ret;
3994
Chris Mason890871b2009-09-02 16:24:52 -04003995 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003996 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04003997 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04003998 BUG_ON(!em);
3999
4000 BUG_ON(em->start > logical || em->start + em->len < logical);
4001 map = (struct map_lookup *)em->bdev;
4002 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4003 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004004 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4005 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04004006 else
4007 ret = 1;
4008 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004009
4010 btrfs_dev_replace_lock(&fs_info->dev_replace);
4011 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4012 ret++;
4013 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4014
Chris Masonf1885912008-04-09 16:28:12 -04004015 return ret;
4016}
4017
Stefan Behrens30d98612012-11-06 14:52:18 +01004018static int find_live_mirror(struct btrfs_fs_info *fs_info,
4019 struct map_lookup *map, int first, int num,
4020 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004021{
4022 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004023 int tolerance;
4024 struct btrfs_device *srcdev;
4025
4026 if (dev_replace_is_ongoing &&
4027 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4028 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4029 srcdev = fs_info->dev_replace.srcdev;
4030 else
4031 srcdev = NULL;
4032
4033 /*
4034 * try to avoid the drive that is the source drive for a
4035 * dev-replace procedure, only choose it if no other non-missing
4036 * mirror is available
4037 */
4038 for (tolerance = 0; tolerance < 2; tolerance++) {
4039 if (map->stripes[optimal].dev->bdev &&
4040 (tolerance || map->stripes[optimal].dev != srcdev))
4041 return optimal;
4042 for (i = first; i < first + num; i++) {
4043 if (map->stripes[i].dev->bdev &&
4044 (tolerance || map->stripes[i].dev != srcdev))
4045 return i;
4046 }
Chris Masondfe25022008-05-13 13:46:40 -04004047 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004048
Chris Masondfe25022008-05-13 13:46:40 -04004049 /* we couldn't find one that doesn't fail. Just return something
4050 * and the io error handling code will clean up eventually
4051 */
4052 return optimal;
4053}
4054
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004055static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004056 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004057 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004058 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04004059{
4060 struct extent_map *em;
4061 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004062 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004063 struct extent_map_tree *em_tree = &map_tree->map_tree;
4064 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004065 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004066 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004067 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004068 u64 stripe_nr_orig;
4069 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04004070 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004071 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004072 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004073 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004074 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004075 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004076 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4077 int dev_replace_is_ongoing = 0;
4078 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004079 int patch_the_first_stripe_for_dev_replace = 0;
4080 u64 physical_to_patch_in_first_stripe = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004081
Chris Mason890871b2009-09-02 16:24:52 -04004082 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004083 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004084 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004085
Chris Mason3b951512008-04-17 11:29:12 -04004086 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004087 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004088 (unsigned long long)logical,
4089 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004090 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004091 }
Chris Mason0b86a832008-03-24 15:01:56 -04004092
4093 BUG_ON(em->start > logical || em->start + em->len < logical);
4094 map = (struct map_lookup *)em->bdev;
4095 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004096
4097 stripe_nr = offset;
4098 /*
4099 * stripe_nr counts the total number of stripes we have to stride
4100 * to get to this block
4101 */
4102 do_div(stripe_nr, map->stripe_len);
4103
4104 stripe_offset = stripe_nr * map->stripe_len;
4105 BUG_ON(offset < stripe_offset);
4106
4107 /* stripe_offset is the offset of this block in its stripe*/
4108 stripe_offset = offset - stripe_offset;
4109
Li Dongyangfce3bb92011-03-24 10:24:26 +00004110 if (rw & REQ_DISCARD)
4111 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02004112 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04004113 /* we limit the length of each bio to what fits in a stripe */
4114 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00004115 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04004116 } else {
4117 *length = em->len - offset;
4118 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004119
Jan Schmidta1d3c472011-08-04 17:15:33 +02004120 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004121 goto out;
4122
Stefan Behrens472262f2012-11-06 14:43:46 +01004123 btrfs_dev_replace_lock(dev_replace);
4124 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4125 if (!dev_replace_is_ongoing)
4126 btrfs_dev_replace_unlock(dev_replace);
4127
Stefan Behrensad6d6202012-11-06 15:06:47 +01004128 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4129 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4130 dev_replace->tgtdev != NULL) {
4131 /*
4132 * in dev-replace case, for repair case (that's the only
4133 * case where the mirror is selected explicitly when
4134 * calling btrfs_map_block), blocks left of the left cursor
4135 * can also be read from the target drive.
4136 * For REQ_GET_READ_MIRRORS, the target drive is added as
4137 * the last one to the array of stripes. For READ, it also
4138 * needs to be supported using the same mirror number.
4139 * If the requested block is not left of the left cursor,
4140 * EIO is returned. This can happen because btrfs_num_copies()
4141 * returns one more in the dev-replace case.
4142 */
4143 u64 tmp_length = *length;
4144 struct btrfs_bio *tmp_bbio = NULL;
4145 int tmp_num_stripes;
4146 u64 srcdev_devid = dev_replace->srcdev->devid;
4147 int index_srcdev = 0;
4148 int found = 0;
4149 u64 physical_of_found = 0;
4150
4151 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
4152 logical, &tmp_length, &tmp_bbio, 0);
4153 if (ret) {
4154 WARN_ON(tmp_bbio != NULL);
4155 goto out;
4156 }
4157
4158 tmp_num_stripes = tmp_bbio->num_stripes;
4159 if (mirror_num > tmp_num_stripes) {
4160 /*
4161 * REQ_GET_READ_MIRRORS does not contain this
4162 * mirror, that means that the requested area
4163 * is not left of the left cursor
4164 */
4165 ret = -EIO;
4166 kfree(tmp_bbio);
4167 goto out;
4168 }
4169
4170 /*
4171 * process the rest of the function using the mirror_num
4172 * of the source drive. Therefore look it up first.
4173 * At the end, patch the device pointer to the one of the
4174 * target drive.
4175 */
4176 for (i = 0; i < tmp_num_stripes; i++) {
4177 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4178 /*
4179 * In case of DUP, in order to keep it
4180 * simple, only add the mirror with the
4181 * lowest physical address
4182 */
4183 if (found &&
4184 physical_of_found <=
4185 tmp_bbio->stripes[i].physical)
4186 continue;
4187 index_srcdev = i;
4188 found = 1;
4189 physical_of_found =
4190 tmp_bbio->stripes[i].physical;
4191 }
4192 }
4193
4194 if (found) {
4195 mirror_num = index_srcdev + 1;
4196 patch_the_first_stripe_for_dev_replace = 1;
4197 physical_to_patch_in_first_stripe = physical_of_found;
4198 } else {
4199 WARN_ON(1);
4200 ret = -EIO;
4201 kfree(tmp_bbio);
4202 goto out;
4203 }
4204
4205 kfree(tmp_bbio);
4206 } else if (mirror_num > map->num_stripes) {
4207 mirror_num = 0;
4208 }
4209
Chris Masonf2d8d742008-04-21 10:03:05 -04004210 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004211 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004212 stripe_nr_orig = stripe_nr;
4213 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
4214 (~(map->stripe_len - 1));
4215 do_div(stripe_nr_end, map->stripe_len);
4216 stripe_end_offset = stripe_nr_end * map->stripe_len -
4217 (offset + *length);
4218 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4219 if (rw & REQ_DISCARD)
4220 num_stripes = min_t(u64, map->num_stripes,
4221 stripe_nr_end - stripe_nr_orig);
4222 stripe_index = do_div(stripe_nr, map->num_stripes);
4223 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004224 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004225 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004226 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004227 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004228 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004229 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004230 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004231 current->pid % map->num_stripes,
4232 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004233 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004234 }
Chris Mason2fff7342008-04-29 14:12:09 -04004235
Chris Mason611f0e02008-04-03 16:29:03 -04004236 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004237 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004238 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004239 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004240 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004241 } else {
4242 mirror_num = 1;
4243 }
Chris Mason2fff7342008-04-29 14:12:09 -04004244
Chris Mason321aecc2008-04-16 10:49:51 -04004245 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4246 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004247
4248 stripe_index = do_div(stripe_nr, factor);
4249 stripe_index *= map->sub_stripes;
4250
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004251 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004252 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004253 else if (rw & REQ_DISCARD)
4254 num_stripes = min_t(u64, map->sub_stripes *
4255 (stripe_nr_end - stripe_nr_orig),
4256 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004257 else if (mirror_num)
4258 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004259 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004260 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004261 stripe_index = find_live_mirror(fs_info, map,
4262 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004263 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004264 current->pid % map->sub_stripes,
4265 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004266 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004267 }
Chris Mason8790d502008-04-03 16:29:03 -04004268 } else {
4269 /*
4270 * after this do_div call, stripe_nr is the number of stripes
4271 * on this device we have to walk to find the data, and
4272 * stripe_index is the number of our device in the stripe array
4273 */
4274 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004275 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004276 }
Chris Mason593060d2008-03-25 16:50:33 -04004277 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004278
Stefan Behrens472262f2012-11-06 14:43:46 +01004279 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004280 if (dev_replace_is_ongoing) {
4281 if (rw & (REQ_WRITE | REQ_DISCARD))
4282 num_alloc_stripes <<= 1;
4283 if (rw & REQ_GET_READ_MIRRORS)
4284 num_alloc_stripes++;
4285 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004286 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004287 if (!bbio) {
4288 ret = -ENOMEM;
4289 goto out;
4290 }
4291 atomic_set(&bbio->error, 0);
4292
Li Dongyangfce3bb92011-03-24 10:24:26 +00004293 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004294 int factor = 0;
4295 int sub_stripes = 0;
4296 u64 stripes_per_dev = 0;
4297 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004298 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004299
4300 if (map->type &
4301 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4302 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4303 sub_stripes = 1;
4304 else
4305 sub_stripes = map->sub_stripes;
4306
4307 factor = map->num_stripes / sub_stripes;
4308 stripes_per_dev = div_u64_rem(stripe_nr_end -
4309 stripe_nr_orig,
4310 factor,
4311 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004312 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4313 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004314 }
4315
Li Dongyangfce3bb92011-03-24 10:24:26 +00004316 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004317 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004318 map->stripes[stripe_index].physical +
4319 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004320 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004321
Li Zefanec9ef7a2011-12-01 14:06:42 +08004322 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4323 BTRFS_BLOCK_GROUP_RAID10)) {
4324 bbio->stripes[i].length = stripes_per_dev *
4325 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004326
Li Zefanec9ef7a2011-12-01 14:06:42 +08004327 if (i / sub_stripes < remaining_stripes)
4328 bbio->stripes[i].length +=
4329 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004330
4331 /*
4332 * Special for the first stripe and
4333 * the last stripe:
4334 *
4335 * |-------|...|-------|
4336 * |----------|
4337 * off end_off
4338 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004339 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004340 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004341 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004342
4343 if (stripe_index >= last_stripe &&
4344 stripe_index <= (last_stripe +
4345 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004346 bbio->stripes[i].length -=
4347 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004348
Li Zefanec9ef7a2011-12-01 14:06:42 +08004349 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004350 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004351 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004352 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004353
4354 stripe_index++;
4355 if (stripe_index == map->num_stripes) {
4356 /* This could only happen for RAID0/10 */
4357 stripe_index = 0;
4358 stripe_nr++;
4359 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004360 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004361 } else {
4362 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004363 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004364 map->stripes[stripe_index].physical +
4365 stripe_offset +
4366 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004367 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004368 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004369 stripe_index++;
4370 }
Chris Mason593060d2008-03-25 16:50:33 -04004371 }
Li Zefande11cc12011-12-01 12:55:47 +08004372
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004373 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004374 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4375 BTRFS_BLOCK_GROUP_RAID10 |
4376 BTRFS_BLOCK_GROUP_DUP)) {
4377 max_errors = 1;
4378 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004379 }
Li Zefande11cc12011-12-01 12:55:47 +08004380
Stefan Behrens472262f2012-11-06 14:43:46 +01004381 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4382 dev_replace->tgtdev != NULL) {
4383 int index_where_to_add;
4384 u64 srcdev_devid = dev_replace->srcdev->devid;
4385
4386 /*
4387 * duplicate the write operations while the dev replace
4388 * procedure is running. Since the copying of the old disk
4389 * to the new disk takes place at run time while the
4390 * filesystem is mounted writable, the regular write
4391 * operations to the old disk have to be duplicated to go
4392 * to the new disk as well.
4393 * Note that device->missing is handled by the caller, and
4394 * that the write to the old disk is already set up in the
4395 * stripes array.
4396 */
4397 index_where_to_add = num_stripes;
4398 for (i = 0; i < num_stripes; i++) {
4399 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4400 /* write to new disk, too */
4401 struct btrfs_bio_stripe *new =
4402 bbio->stripes + index_where_to_add;
4403 struct btrfs_bio_stripe *old =
4404 bbio->stripes + i;
4405
4406 new->physical = old->physical;
4407 new->length = old->length;
4408 new->dev = dev_replace->tgtdev;
4409 index_where_to_add++;
4410 max_errors++;
4411 }
4412 }
4413 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004414 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4415 dev_replace->tgtdev != NULL) {
4416 u64 srcdev_devid = dev_replace->srcdev->devid;
4417 int index_srcdev = 0;
4418 int found = 0;
4419 u64 physical_of_found = 0;
4420
4421 /*
4422 * During the dev-replace procedure, the target drive can
4423 * also be used to read data in case it is needed to repair
4424 * a corrupt block elsewhere. This is possible if the
4425 * requested area is left of the left cursor. In this area,
4426 * the target drive is a full copy of the source drive.
4427 */
4428 for (i = 0; i < num_stripes; i++) {
4429 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4430 /*
4431 * In case of DUP, in order to keep it
4432 * simple, only add the mirror with the
4433 * lowest physical address
4434 */
4435 if (found &&
4436 physical_of_found <=
4437 bbio->stripes[i].physical)
4438 continue;
4439 index_srcdev = i;
4440 found = 1;
4441 physical_of_found = bbio->stripes[i].physical;
4442 }
4443 }
4444 if (found) {
4445 u64 length = map->stripe_len;
4446
4447 if (physical_of_found + length <=
4448 dev_replace->cursor_left) {
4449 struct btrfs_bio_stripe *tgtdev_stripe =
4450 bbio->stripes + num_stripes;
4451
4452 tgtdev_stripe->physical = physical_of_found;
4453 tgtdev_stripe->length =
4454 bbio->stripes[index_srcdev].length;
4455 tgtdev_stripe->dev = dev_replace->tgtdev;
4456
4457 num_stripes++;
4458 }
4459 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004460 }
4461
Li Zefande11cc12011-12-01 12:55:47 +08004462 *bbio_ret = bbio;
4463 bbio->num_stripes = num_stripes;
4464 bbio->max_errors = max_errors;
4465 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004466
4467 /*
4468 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4469 * mirror_num == num_stripes + 1 && dev_replace target drive is
4470 * available as a mirror
4471 */
4472 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4473 WARN_ON(num_stripes > 1);
4474 bbio->stripes[0].dev = dev_replace->tgtdev;
4475 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4476 bbio->mirror_num = map->num_stripes + 1;
4477 }
Chris Masoncea9e442008-04-09 16:28:12 -04004478out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004479 if (dev_replace_is_ongoing)
4480 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004481 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004482 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004483}
4484
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004485int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004486 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004487 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004488{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004489 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004490 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004491}
4492
Yan Zhenga512bbf2008-12-08 16:46:26 -05004493int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4494 u64 chunk_start, u64 physical, u64 devid,
4495 u64 **logical, int *naddrs, int *stripe_len)
4496{
4497 struct extent_map_tree *em_tree = &map_tree->map_tree;
4498 struct extent_map *em;
4499 struct map_lookup *map;
4500 u64 *buf;
4501 u64 bytenr;
4502 u64 length;
4503 u64 stripe_nr;
4504 int i, j, nr = 0;
4505
Chris Mason890871b2009-09-02 16:24:52 -04004506 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004507 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004508 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004509
4510 BUG_ON(!em || em->start != chunk_start);
4511 map = (struct map_lookup *)em->bdev;
4512
4513 length = em->len;
4514 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4515 do_div(length, map->num_stripes / map->sub_stripes);
4516 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4517 do_div(length, map->num_stripes);
4518
4519 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004520 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004521
4522 for (i = 0; i < map->num_stripes; i++) {
4523 if (devid && map->stripes[i].dev->devid != devid)
4524 continue;
4525 if (map->stripes[i].physical > physical ||
4526 map->stripes[i].physical + length <= physical)
4527 continue;
4528
4529 stripe_nr = physical - map->stripes[i].physical;
4530 do_div(stripe_nr, map->stripe_len);
4531
4532 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4533 stripe_nr = stripe_nr * map->num_stripes + i;
4534 do_div(stripe_nr, map->sub_stripes);
4535 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4536 stripe_nr = stripe_nr * map->num_stripes + i;
4537 }
4538 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004539 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004540 for (j = 0; j < nr; j++) {
4541 if (buf[j] == bytenr)
4542 break;
4543 }
Chris Mason934d3752008-12-08 16:43:10 -05004544 if (j == nr) {
4545 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004546 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004547 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004548 }
4549
Yan Zhenga512bbf2008-12-08 16:46:26 -05004550 *logical = buf;
4551 *naddrs = nr;
4552 *stripe_len = map->stripe_len;
4553
4554 free_extent_map(em);
4555 return 0;
4556}
4557
Stefan Behrens442a4f62012-05-25 16:06:08 +02004558static void *merge_stripe_index_into_bio_private(void *bi_private,
4559 unsigned int stripe_index)
4560{
4561 /*
4562 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4563 * at most 1.
4564 * The alternative solution (instead of stealing bits from the
4565 * pointer) would be to allocate an intermediate structure
4566 * that contains the old private pointer plus the stripe_index.
4567 */
4568 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4569 BUG_ON(stripe_index > 3);
4570 return (void *)(((uintptr_t)bi_private) | stripe_index);
4571}
4572
4573static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4574{
4575 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4576}
4577
4578static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4579{
4580 return (unsigned int)((uintptr_t)bi_private) & 3;
4581}
4582
Jan Schmidta1d3c472011-08-04 17:15:33 +02004583static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004584{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004585 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004586 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004587
Stefan Behrens442a4f62012-05-25 16:06:08 +02004588 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004589 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004590 if (err == -EIO || err == -EREMOTEIO) {
4591 unsigned int stripe_index =
4592 extract_stripe_index_from_bio_private(
4593 bio->bi_private);
4594 struct btrfs_device *dev;
4595
4596 BUG_ON(stripe_index >= bbio->num_stripes);
4597 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004598 if (dev->bdev) {
4599 if (bio->bi_rw & WRITE)
4600 btrfs_dev_stat_inc(dev,
4601 BTRFS_DEV_STAT_WRITE_ERRS);
4602 else
4603 btrfs_dev_stat_inc(dev,
4604 BTRFS_DEV_STAT_READ_ERRS);
4605 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4606 btrfs_dev_stat_inc(dev,
4607 BTRFS_DEV_STAT_FLUSH_ERRS);
4608 btrfs_dev_stat_print_on_error(dev);
4609 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004610 }
4611 }
Chris Mason8790d502008-04-03 16:29:03 -04004612
Jan Schmidta1d3c472011-08-04 17:15:33 +02004613 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004614 is_orig_bio = 1;
4615
Jan Schmidta1d3c472011-08-04 17:15:33 +02004616 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004617 if (!is_orig_bio) {
4618 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004619 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004620 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004621 bio->bi_private = bbio->private;
4622 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004623 bio->bi_bdev = (struct block_device *)
4624 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004625 /* only send an error to the higher layers if it is
4626 * beyond the tolerance of the multi-bio
4627 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004628 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004629 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004630 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004631 /*
4632 * this bio is actually up to date, we didn't
4633 * go over the max number of errors
4634 */
4635 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004636 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004637 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004638 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004639
4640 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004641 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004642 bio_put(bio);
4643 }
Chris Mason8790d502008-04-03 16:29:03 -04004644}
4645
Chris Mason8b712842008-06-11 16:50:36 -04004646struct async_sched {
4647 struct bio *bio;
4648 int rw;
4649 struct btrfs_fs_info *info;
4650 struct btrfs_work work;
4651};
4652
4653/*
4654 * see run_scheduled_bios for a description of why bios are collected for
4655 * async submit.
4656 *
4657 * This will add one bio to the pending list for a device and make sure
4658 * the work struct is scheduled.
4659 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004660static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004661 struct btrfs_device *device,
4662 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004663{
4664 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004665 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004666
4667 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004668 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004669 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004670 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004671 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004672 return;
Chris Mason8b712842008-06-11 16:50:36 -04004673 }
4674
4675 /*
Chris Mason0986fe92008-08-15 15:34:15 -04004676 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004677 * higher layers. Otherwise, the async bio makes it appear we have
4678 * made progress against dirty pages when we've really just put it
4679 * on a queue for later
4680 */
Chris Mason0986fe92008-08-15 15:34:15 -04004681 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004682 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004683 bio->bi_next = NULL;
4684 bio->bi_rw |= rw;
4685
4686 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004687 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004688 pending_bios = &device->pending_sync_bios;
4689 else
4690 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004691
Chris Masonffbd5172009-04-20 15:50:09 -04004692 if (pending_bios->tail)
4693 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004694
Chris Masonffbd5172009-04-20 15:50:09 -04004695 pending_bios->tail = bio;
4696 if (!pending_bios->head)
4697 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004698 if (device->running_pending)
4699 should_queue = 0;
4700
4701 spin_unlock(&device->io_lock);
4702
4703 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004704 btrfs_queue_worker(&root->fs_info->submit_workers,
4705 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004706}
4707
Josef Bacikde1ee922012-10-19 16:50:56 -04004708static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4709 sector_t sector)
4710{
4711 struct bio_vec *prev;
4712 struct request_queue *q = bdev_get_queue(bdev);
4713 unsigned short max_sectors = queue_max_sectors(q);
4714 struct bvec_merge_data bvm = {
4715 .bi_bdev = bdev,
4716 .bi_sector = sector,
4717 .bi_rw = bio->bi_rw,
4718 };
4719
4720 if (bio->bi_vcnt == 0) {
4721 WARN_ON(1);
4722 return 1;
4723 }
4724
4725 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4726 if ((bio->bi_size >> 9) > max_sectors)
4727 return 0;
4728
4729 if (!q->merge_bvec_fn)
4730 return 1;
4731
4732 bvm.bi_size = bio->bi_size - prev->bv_len;
4733 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4734 return 0;
4735 return 1;
4736}
4737
4738static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4739 struct bio *bio, u64 physical, int dev_nr,
4740 int rw, int async)
4741{
4742 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4743
4744 bio->bi_private = bbio;
4745 bio->bi_private = merge_stripe_index_into_bio_private(
4746 bio->bi_private, (unsigned int)dev_nr);
4747 bio->bi_end_io = btrfs_end_bio;
4748 bio->bi_sector = physical >> 9;
4749#ifdef DEBUG
4750 {
4751 struct rcu_string *name;
4752
4753 rcu_read_lock();
4754 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00004755 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04004756 "(%s id %llu), size=%u\n", rw,
4757 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4758 name->str, dev->devid, bio->bi_size);
4759 rcu_read_unlock();
4760 }
4761#endif
4762 bio->bi_bdev = dev->bdev;
4763 if (async)
4764 schedule_bio(root, dev, rw, bio);
4765 else
4766 btrfsic_submit_bio(rw, bio);
4767}
4768
4769static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4770 struct bio *first_bio, struct btrfs_device *dev,
4771 int dev_nr, int rw, int async)
4772{
4773 struct bio_vec *bvec = first_bio->bi_io_vec;
4774 struct bio *bio;
4775 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4776 u64 physical = bbio->stripes[dev_nr].physical;
4777
4778again:
4779 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4780 if (!bio)
4781 return -ENOMEM;
4782
4783 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4784 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4785 bvec->bv_offset) < bvec->bv_len) {
4786 u64 len = bio->bi_size;
4787
4788 atomic_inc(&bbio->stripes_pending);
4789 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4790 rw, async);
4791 physical += len;
4792 goto again;
4793 }
4794 bvec++;
4795 }
4796
4797 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4798 return 0;
4799}
4800
4801static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4802{
4803 atomic_inc(&bbio->error);
4804 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4805 bio->bi_private = bbio->private;
4806 bio->bi_end_io = bbio->end_io;
4807 bio->bi_bdev = (struct block_device *)
4808 (unsigned long)bbio->mirror_num;
4809 bio->bi_sector = logical >> 9;
4810 kfree(bbio);
4811 bio_endio(bio, -EIO);
4812 }
4813}
4814
Chris Masonf1885912008-04-09 16:28:12 -04004815int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004816 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004817{
Chris Mason0b86a832008-03-24 15:01:56 -04004818 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004819 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004820 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004821 u64 length = 0;
4822 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004823 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004824 int dev_nr = 0;
4825 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004826 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004827
Chris Masonf2d8d742008-04-21 10:03:05 -04004828 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004829 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004830
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004831 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004832 mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01004833 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004834 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004835
Jan Schmidta1d3c472011-08-04 17:15:33 +02004836 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004837 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004838 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004839 "len %llu\n", (unsigned long long)logical,
4840 (unsigned long long)length,
4841 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004842 BUG();
4843 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004844
4845 bbio->orig_bio = first_bio;
4846 bbio->private = first_bio->bi_private;
4847 bbio->end_io = first_bio->bi_end_io;
4848 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004849
Chris Masond3977122009-01-05 21:25:51 -05004850 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04004851 dev = bbio->stripes[dev_nr].dev;
4852 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4853 bbio_error(bbio, first_bio, logical);
4854 dev_nr++;
4855 continue;
4856 }
4857
4858 /*
4859 * Check and see if we're ok with this bio based on it's size
4860 * and offset with the given device.
4861 */
4862 if (!bio_size_ok(dev->bdev, first_bio,
4863 bbio->stripes[dev_nr].physical >> 9)) {
4864 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4865 dev_nr, rw, async_submit);
4866 BUG_ON(ret);
4867 dev_nr++;
4868 continue;
4869 }
4870
Jan Schmidta1d3c472011-08-04 17:15:33 +02004871 if (dev_nr < total_devs - 1) {
4872 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004873 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004874 } else {
4875 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004876 }
Josef Bacik606686e2012-06-04 14:03:51 -04004877
Josef Bacikde1ee922012-10-19 16:50:56 -04004878 submit_stripe_bio(root, bbio, bio,
4879 bbio->stripes[dev_nr].physical, dev_nr, rw,
4880 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04004881 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004882 }
Chris Mason0b86a832008-03-24 15:01:56 -04004883 return 0;
4884}
4885
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004886struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004887 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004888{
Yan Zheng2b820322008-11-17 21:11:30 -05004889 struct btrfs_device *device;
4890 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004891
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004892 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004893 while (cur_devices) {
4894 if (!fsid ||
4895 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4896 device = __find_device(&cur_devices->devices,
4897 devid, uuid);
4898 if (device)
4899 return device;
4900 }
4901 cur_devices = cur_devices->seed;
4902 }
4903 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004904}
4905
Chris Masondfe25022008-05-13 13:46:40 -04004906static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4907 u64 devid, u8 *dev_uuid)
4908{
4909 struct btrfs_device *device;
4910 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4911
4912 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004913 if (!device)
4914 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004915 list_add(&device->dev_list,
4916 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004917 device->dev_root = root->fs_info->dev_root;
4918 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004919 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004920 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004921 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004922 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004923 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004924 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004925 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004926 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4927 return device;
4928}
4929
Chris Mason0b86a832008-03-24 15:01:56 -04004930static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4931 struct extent_buffer *leaf,
4932 struct btrfs_chunk *chunk)
4933{
4934 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4935 struct map_lookup *map;
4936 struct extent_map *em;
4937 u64 logical;
4938 u64 length;
4939 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004940 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004941 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004942 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004943 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004944
Chris Masone17cade2008-04-15 15:41:47 -04004945 logical = key->offset;
4946 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004947
Chris Mason890871b2009-09-02 16:24:52 -04004948 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004949 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004950 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004951
4952 /* already mapped? */
4953 if (em && em->start <= logical && em->start + em->len > logical) {
4954 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004955 return 0;
4956 } else if (em) {
4957 free_extent_map(em);
4958 }
Chris Mason0b86a832008-03-24 15:01:56 -04004959
David Sterba172ddd62011-04-21 00:48:27 +02004960 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004961 if (!em)
4962 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004963 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4964 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004965 if (!map) {
4966 free_extent_map(em);
4967 return -ENOMEM;
4968 }
4969
4970 em->bdev = (struct block_device *)map;
4971 em->start = logical;
4972 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04004973 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004974 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004975 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004976
Chris Mason593060d2008-03-25 16:50:33 -04004977 map->num_stripes = num_stripes;
4978 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4979 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4980 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4981 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4982 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004983 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004984 for (i = 0; i < num_stripes; i++) {
4985 map->stripes[i].physical =
4986 btrfs_stripe_offset_nr(leaf, chunk, i);
4987 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004988 read_extent_buffer(leaf, uuid, (unsigned long)
4989 btrfs_stripe_dev_uuid_nr(chunk, i),
4990 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004991 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
4992 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04004993 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04004994 kfree(map);
4995 free_extent_map(em);
4996 return -EIO;
4997 }
Chris Masondfe25022008-05-13 13:46:40 -04004998 if (!map->stripes[i].dev) {
4999 map->stripes[i].dev =
5000 add_missing_dev(root, devid, uuid);
5001 if (!map->stripes[i].dev) {
5002 kfree(map);
5003 free_extent_map(em);
5004 return -EIO;
5005 }
5006 }
5007 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005008 }
5009
Chris Mason890871b2009-09-02 16:24:52 -04005010 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005011 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005012 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005013 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005014 free_extent_map(em);
5015
5016 return 0;
5017}
5018
Jeff Mahoney143bede2012-03-01 14:56:26 +01005019static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005020 struct btrfs_dev_item *dev_item,
5021 struct btrfs_device *device)
5022{
5023 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005024
5025 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005026 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5027 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005028 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5029 device->type = btrfs_device_type(leaf, dev_item);
5030 device->io_align = btrfs_device_io_align(leaf, dev_item);
5031 device->io_width = btrfs_device_io_width(leaf, dev_item);
5032 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005033 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005034 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005035
5036 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005037 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005038}
5039
Yan Zheng2b820322008-11-17 21:11:30 -05005040static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5041{
5042 struct btrfs_fs_devices *fs_devices;
5043 int ret;
5044
Li Zefanb367e472011-12-07 11:38:24 +08005045 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005046
5047 fs_devices = root->fs_info->fs_devices->seed;
5048 while (fs_devices) {
5049 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5050 ret = 0;
5051 goto out;
5052 }
5053 fs_devices = fs_devices->seed;
5054 }
5055
5056 fs_devices = find_fsid(fsid);
5057 if (!fs_devices) {
5058 ret = -ENOENT;
5059 goto out;
5060 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005061
5062 fs_devices = clone_fs_devices(fs_devices);
5063 if (IS_ERR(fs_devices)) {
5064 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005065 goto out;
5066 }
5067
Christoph Hellwig97288f22008-12-02 06:36:09 -05005068 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005069 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005070 if (ret) {
5071 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005072 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005073 }
Yan Zheng2b820322008-11-17 21:11:30 -05005074
5075 if (!fs_devices->seeding) {
5076 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005077 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005078 ret = -EINVAL;
5079 goto out;
5080 }
5081
5082 fs_devices->seed = root->fs_info->fs_devices->seed;
5083 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005084out:
Yan Zheng2b820322008-11-17 21:11:30 -05005085 return ret;
5086}
5087
Chris Mason0d81ba52008-03-24 15:02:07 -04005088static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005089 struct extent_buffer *leaf,
5090 struct btrfs_dev_item *dev_item)
5091{
5092 struct btrfs_device *device;
5093 u64 devid;
5094 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005095 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005096 u8 dev_uuid[BTRFS_UUID_SIZE];
5097
Chris Mason0b86a832008-03-24 15:01:56 -04005098 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005099 read_extent_buffer(leaf, dev_uuid,
5100 (unsigned long)btrfs_device_uuid(dev_item),
5101 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005102 read_extent_buffer(leaf, fs_uuid,
5103 (unsigned long)btrfs_device_fsid(dev_item),
5104 BTRFS_UUID_SIZE);
5105
5106 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5107 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005108 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005109 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005110 }
5111
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005112 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005113 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005114 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005115 return -EIO;
5116
5117 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05005118 printk(KERN_WARNING "warning devid %llu missing\n",
5119 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005120 device = add_missing_dev(root, devid, dev_uuid);
5121 if (!device)
5122 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005123 } else if (!device->missing) {
5124 /*
5125 * this happens when a device that was properly setup
5126 * in the device info lists suddenly goes bad.
5127 * device->bdev is NULL, and so we have to set
5128 * device->missing to one here
5129 */
5130 root->fs_info->fs_devices->missing_devices++;
5131 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005132 }
5133 }
5134
5135 if (device->fs_devices != root->fs_info->fs_devices) {
5136 BUG_ON(device->writeable);
5137 if (device->generation !=
5138 btrfs_device_generation(leaf, dev_item))
5139 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005140 }
Chris Mason0b86a832008-03-24 15:01:56 -04005141
5142 fill_device_from_item(leaf, dev_item, device);
5143 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005144 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005145 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005146 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005147 spin_lock(&root->fs_info->free_chunk_lock);
5148 root->fs_info->free_chunk_space += device->total_bytes -
5149 device->bytes_used;
5150 spin_unlock(&root->fs_info->free_chunk_lock);
5151 }
Chris Mason0b86a832008-03-24 15:01:56 -04005152 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005153 return ret;
5154}
5155
Yan Zhenge4404d62008-12-12 10:03:26 -05005156int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005157{
David Sterba6c417612011-04-13 15:41:04 +02005158 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005159 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005160 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005161 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005162 u8 *ptr;
5163 unsigned long sb_ptr;
5164 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005165 u32 num_stripes;
5166 u32 array_size;
5167 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005168 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005169 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005170
Yan Zhenge4404d62008-12-12 10:03:26 -05005171 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005172 BTRFS_SUPER_INFO_SIZE);
5173 if (!sb)
5174 return -ENOMEM;
5175 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005176 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005177 /*
5178 * The sb extent buffer is artifical and just used to read the system array.
5179 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5180 * pages up-to-date when the page is larger: extent does not cover the
5181 * whole page and consequently check_page_uptodate does not find all
5182 * the page's extents up-to-date (the hole beyond sb),
5183 * write_extent_buffer then triggers a WARN_ON.
5184 *
5185 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5186 * but sb spans only this function. Add an explicit SetPageUptodate call
5187 * to silence the warning eg. on PowerPC 64.
5188 */
5189 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005190 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005191
Chris Masona061fc82008-05-07 11:43:44 -04005192 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005193 array_size = btrfs_super_sys_array_size(super_copy);
5194
Chris Mason0b86a832008-03-24 15:01:56 -04005195 ptr = super_copy->sys_chunk_array;
5196 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5197 cur = 0;
5198
5199 while (cur < array_size) {
5200 disk_key = (struct btrfs_disk_key *)ptr;
5201 btrfs_disk_key_to_cpu(&key, disk_key);
5202
Chris Masona061fc82008-05-07 11:43:44 -04005203 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005204 sb_ptr += len;
5205 cur += len;
5206
Chris Mason0d81ba52008-03-24 15:02:07 -04005207 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005208 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005209 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005210 if (ret)
5211 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005212 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5213 len = btrfs_chunk_item_size(num_stripes);
5214 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005215 ret = -EIO;
5216 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005217 }
5218 ptr += len;
5219 sb_ptr += len;
5220 cur += len;
5221 }
Chris Masona061fc82008-05-07 11:43:44 -04005222 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005223 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005224}
5225
5226int btrfs_read_chunk_tree(struct btrfs_root *root)
5227{
5228 struct btrfs_path *path;
5229 struct extent_buffer *leaf;
5230 struct btrfs_key key;
5231 struct btrfs_key found_key;
5232 int ret;
5233 int slot;
5234
5235 root = root->fs_info->chunk_root;
5236
5237 path = btrfs_alloc_path();
5238 if (!path)
5239 return -ENOMEM;
5240
Li Zefanb367e472011-12-07 11:38:24 +08005241 mutex_lock(&uuid_mutex);
5242 lock_chunks(root);
5243
Chris Mason0b86a832008-03-24 15:01:56 -04005244 /* first we search for all of the device items, and then we
5245 * read in all of the chunk items. This way we can create chunk
5246 * mappings that reference all of the devices that are afound
5247 */
5248 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5249 key.offset = 0;
5250 key.type = 0;
5251again:
5252 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005253 if (ret < 0)
5254 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005255 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005256 leaf = path->nodes[0];
5257 slot = path->slots[0];
5258 if (slot >= btrfs_header_nritems(leaf)) {
5259 ret = btrfs_next_leaf(root, path);
5260 if (ret == 0)
5261 continue;
5262 if (ret < 0)
5263 goto error;
5264 break;
5265 }
5266 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5267 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5268 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5269 break;
5270 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5271 struct btrfs_dev_item *dev_item;
5272 dev_item = btrfs_item_ptr(leaf, slot,
5273 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005274 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005275 if (ret)
5276 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005277 }
5278 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5279 struct btrfs_chunk *chunk;
5280 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5281 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005282 if (ret)
5283 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005284 }
5285 path->slots[0]++;
5286 }
5287 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5288 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005289 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005290 goto again;
5291 }
Chris Mason0b86a832008-03-24 15:01:56 -04005292 ret = 0;
5293error:
Li Zefanb367e472011-12-07 11:38:24 +08005294 unlock_chunks(root);
5295 mutex_unlock(&uuid_mutex);
5296
Yan Zheng2b820322008-11-17 21:11:30 -05005297 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005298 return ret;
5299}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005300
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005301static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5302{
5303 int i;
5304
5305 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5306 btrfs_dev_stat_reset(dev, i);
5307}
5308
5309int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5310{
5311 struct btrfs_key key;
5312 struct btrfs_key found_key;
5313 struct btrfs_root *dev_root = fs_info->dev_root;
5314 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5315 struct extent_buffer *eb;
5316 int slot;
5317 int ret = 0;
5318 struct btrfs_device *device;
5319 struct btrfs_path *path = NULL;
5320 int i;
5321
5322 path = btrfs_alloc_path();
5323 if (!path) {
5324 ret = -ENOMEM;
5325 goto out;
5326 }
5327
5328 mutex_lock(&fs_devices->device_list_mutex);
5329 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5330 int item_size;
5331 struct btrfs_dev_stats_item *ptr;
5332
5333 key.objectid = 0;
5334 key.type = BTRFS_DEV_STATS_KEY;
5335 key.offset = device->devid;
5336 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5337 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005338 __btrfs_reset_dev_stats(device);
5339 device->dev_stats_valid = 1;
5340 btrfs_release_path(path);
5341 continue;
5342 }
5343 slot = path->slots[0];
5344 eb = path->nodes[0];
5345 btrfs_item_key_to_cpu(eb, &found_key, slot);
5346 item_size = btrfs_item_size_nr(eb, slot);
5347
5348 ptr = btrfs_item_ptr(eb, slot,
5349 struct btrfs_dev_stats_item);
5350
5351 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5352 if (item_size >= (1 + i) * sizeof(__le64))
5353 btrfs_dev_stat_set(device, i,
5354 btrfs_dev_stats_value(eb, ptr, i));
5355 else
5356 btrfs_dev_stat_reset(device, i);
5357 }
5358
5359 device->dev_stats_valid = 1;
5360 btrfs_dev_stat_print_on_load(device);
5361 btrfs_release_path(path);
5362 }
5363 mutex_unlock(&fs_devices->device_list_mutex);
5364
5365out:
5366 btrfs_free_path(path);
5367 return ret < 0 ? ret : 0;
5368}
5369
5370static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5371 struct btrfs_root *dev_root,
5372 struct btrfs_device *device)
5373{
5374 struct btrfs_path *path;
5375 struct btrfs_key key;
5376 struct extent_buffer *eb;
5377 struct btrfs_dev_stats_item *ptr;
5378 int ret;
5379 int i;
5380
5381 key.objectid = 0;
5382 key.type = BTRFS_DEV_STATS_KEY;
5383 key.offset = device->devid;
5384
5385 path = btrfs_alloc_path();
5386 BUG_ON(!path);
5387 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5388 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005389 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5390 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005391 goto out;
5392 }
5393
5394 if (ret == 0 &&
5395 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5396 /* need to delete old one and insert a new one */
5397 ret = btrfs_del_item(trans, dev_root, path);
5398 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005399 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5400 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005401 goto out;
5402 }
5403 ret = 1;
5404 }
5405
5406 if (ret == 1) {
5407 /* need to insert a new item */
5408 btrfs_release_path(path);
5409 ret = btrfs_insert_empty_item(trans, dev_root, path,
5410 &key, sizeof(*ptr));
5411 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005412 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5413 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005414 goto out;
5415 }
5416 }
5417
5418 eb = path->nodes[0];
5419 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5420 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5421 btrfs_set_dev_stats_value(eb, ptr, i,
5422 btrfs_dev_stat_read(device, i));
5423 btrfs_mark_buffer_dirty(eb);
5424
5425out:
5426 btrfs_free_path(path);
5427 return ret;
5428}
5429
5430/*
5431 * called from commit_transaction. Writes all changed device stats to disk.
5432 */
5433int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5434 struct btrfs_fs_info *fs_info)
5435{
5436 struct btrfs_root *dev_root = fs_info->dev_root;
5437 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5438 struct btrfs_device *device;
5439 int ret = 0;
5440
5441 mutex_lock(&fs_devices->device_list_mutex);
5442 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5443 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5444 continue;
5445
5446 ret = update_dev_stat_item(trans, dev_root, device);
5447 if (!ret)
5448 device->dev_stats_dirty = 0;
5449 }
5450 mutex_unlock(&fs_devices->device_list_mutex);
5451
5452 return ret;
5453}
5454
Stefan Behrens442a4f62012-05-25 16:06:08 +02005455void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5456{
5457 btrfs_dev_stat_inc(dev, index);
5458 btrfs_dev_stat_print_on_error(dev);
5459}
5460
5461void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5462{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005463 if (!dev->dev_stats_valid)
5464 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005465 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005466 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005467 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005468 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5469 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5470 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5471 btrfs_dev_stat_read(dev,
5472 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5473 btrfs_dev_stat_read(dev,
5474 BTRFS_DEV_STAT_GENERATION_ERRS));
5475}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005476
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005477static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5478{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005479 int i;
5480
5481 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5482 if (btrfs_dev_stat_read(dev, i) != 0)
5483 break;
5484 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5485 return; /* all values == 0, suppress message */
5486
Josef Bacik606686e2012-06-04 14:03:51 -04005487 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5488 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005489 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5490 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5491 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5492 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5493 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5494}
5495
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005496int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005497 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005498{
5499 struct btrfs_device *dev;
5500 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5501 int i;
5502
5503 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005504 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005505 mutex_unlock(&fs_devices->device_list_mutex);
5506
5507 if (!dev) {
5508 printk(KERN_WARNING
5509 "btrfs: get dev_stats failed, device not found\n");
5510 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005511 } else if (!dev->dev_stats_valid) {
5512 printk(KERN_WARNING
5513 "btrfs: get dev_stats failed, not yet valid\n");
5514 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005515 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005516 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5517 if (stats->nr_items > i)
5518 stats->values[i] =
5519 btrfs_dev_stat_read_and_reset(dev, i);
5520 else
5521 btrfs_dev_stat_reset(dev, i);
5522 }
5523 } else {
5524 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5525 if (stats->nr_items > i)
5526 stats->values[i] = btrfs_dev_stat_read(dev, i);
5527 }
5528 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5529 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5530 return 0;
5531}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005532
5533int btrfs_scratch_superblock(struct btrfs_device *device)
5534{
5535 struct buffer_head *bh;
5536 struct btrfs_super_block *disk_super;
5537
5538 bh = btrfs_read_dev_super(device->bdev);
5539 if (!bh)
5540 return -EINVAL;
5541 disk_super = (struct btrfs_super_block *)bh->b_data;
5542
5543 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5544 set_buffer_dirty(bh);
5545 sync_dirty_buffer(bh);
5546 brelse(bh);
5547
5548 return 0;
5549}