blob: 46960983891333163eaddaadcbabccf5cd8f5e20 [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 Masonc2898112009-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 Fasheh92b8e8972011-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,
Lukas Czernercc975eb2012-12-07 10:09:19 +00001434 FMODE_WRITE | FMODE_EXCL,
Stefan Behrensbeaf8ab2012-11-12 14:03:45 +01001435 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 */
1559 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
1560
Chris Masona061fc82008-05-07 11:43:44 -04001561error_brelse:
1562 brelse(bh);
Chris Masondfe25022008-05-13 13:46:40 -04001563 if (bdev)
Tejun Heoe525fd82010-11-13 11:55:17 +01001564 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
Chris Masona061fc82008-05-07 11:43:44 -04001565out:
1566 mutex_unlock(&uuid_mutex);
Chris Masona061fc82008-05-07 11:43:44 -04001567 return ret;
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001568error_undo:
1569 if (device->writeable) {
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001570 lock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001571 list_add(&device->dev_alloc_list,
1572 &root->fs_info->fs_devices->alloc_list);
Xiao Guangrong0c1daee2011-04-20 10:08:16 +00001573 unlock_chunks(root);
Ilya Dryomov9b3517e2011-02-15 18:14:25 +00001574 root->fs_info->fs_devices->rw_devices++;
1575 }
1576 goto error_brelse;
Chris Masona061fc82008-05-07 11:43:44 -04001577}
1578
Stefan Behrense93c89c2012-11-05 17:33:06 +01001579void btrfs_rm_dev_replace_srcdev(struct btrfs_fs_info *fs_info,
1580 struct btrfs_device *srcdev)
1581{
1582 WARN_ON(!mutex_is_locked(&fs_info->fs_devices->device_list_mutex));
1583 list_del_rcu(&srcdev->dev_list);
1584 list_del_rcu(&srcdev->dev_alloc_list);
1585 fs_info->fs_devices->num_devices--;
1586 if (srcdev->missing) {
1587 fs_info->fs_devices->missing_devices--;
1588 fs_info->fs_devices->rw_devices++;
1589 }
1590 if (srcdev->can_discard)
1591 fs_info->fs_devices->num_can_discard--;
1592 if (srcdev->bdev)
1593 fs_info->fs_devices->open_devices--;
1594
1595 call_rcu(&srcdev->rcu, free_device);
1596}
1597
1598void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1599 struct btrfs_device *tgtdev)
1600{
1601 struct btrfs_device *next_device;
1602
1603 WARN_ON(!tgtdev);
1604 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1605 if (tgtdev->bdev) {
1606 btrfs_scratch_superblock(tgtdev);
1607 fs_info->fs_devices->open_devices--;
1608 }
1609 fs_info->fs_devices->num_devices--;
1610 if (tgtdev->can_discard)
1611 fs_info->fs_devices->num_can_discard++;
1612
1613 next_device = list_entry(fs_info->fs_devices->devices.next,
1614 struct btrfs_device, dev_list);
1615 if (tgtdev->bdev == fs_info->sb->s_bdev)
1616 fs_info->sb->s_bdev = next_device->bdev;
1617 if (tgtdev->bdev == fs_info->fs_devices->latest_bdev)
1618 fs_info->fs_devices->latest_bdev = next_device->bdev;
1619 list_del_rcu(&tgtdev->dev_list);
1620
1621 call_rcu(&tgtdev->rcu, free_device);
1622
1623 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1624}
1625
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001626int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
1627 struct btrfs_device **device)
1628{
1629 int ret = 0;
1630 struct btrfs_super_block *disk_super;
1631 u64 devid;
1632 u8 *dev_uuid;
1633 struct block_device *bdev;
1634 struct buffer_head *bh;
1635
1636 *device = NULL;
1637 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
1638 root->fs_info->bdev_holder, 0, &bdev, &bh);
1639 if (ret)
1640 return ret;
1641 disk_super = (struct btrfs_super_block *)bh->b_data;
1642 devid = btrfs_stack_device_id(&disk_super->dev_item);
1643 dev_uuid = disk_super->dev_item.uuid;
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001644 *device = btrfs_find_device(root->fs_info, devid, dev_uuid,
Stefan Behrens7ba15b72012-11-05 14:42:30 +01001645 disk_super->fsid);
1646 brelse(bh);
1647 if (!*device)
1648 ret = -ENOENT;
1649 blkdev_put(bdev, FMODE_READ);
1650 return ret;
1651}
1652
1653int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
1654 char *device_path,
1655 struct btrfs_device **device)
1656{
1657 *device = NULL;
1658 if (strcmp(device_path, "missing") == 0) {
1659 struct list_head *devices;
1660 struct btrfs_device *tmp;
1661
1662 devices = &root->fs_info->fs_devices->devices;
1663 /*
1664 * It is safe to read the devices since the volume_mutex
1665 * is held by the caller.
1666 */
1667 list_for_each_entry(tmp, devices, dev_list) {
1668 if (tmp->in_fs_metadata && !tmp->bdev) {
1669 *device = tmp;
1670 break;
1671 }
1672 }
1673
1674 if (!*device) {
1675 pr_err("btrfs: no missing device found\n");
1676 return -ENOENT;
1677 }
1678
1679 return 0;
1680 } else {
1681 return btrfs_find_device_by_path(root, device_path, device);
1682 }
1683}
1684
Yan Zheng2b820322008-11-17 21:11:30 -05001685/*
1686 * does all the dirty work required for changing file system's UUID.
1687 */
Li Zefan125ccb02011-12-08 15:07:24 +08001688static int btrfs_prepare_sprout(struct btrfs_root *root)
Yan Zheng2b820322008-11-17 21:11:30 -05001689{
1690 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
1691 struct btrfs_fs_devices *old_devices;
Yan Zhenge4404d62008-12-12 10:03:26 -05001692 struct btrfs_fs_devices *seed_devices;
David Sterba6c417612011-04-13 15:41:04 +02001693 struct btrfs_super_block *disk_super = root->fs_info->super_copy;
Yan Zheng2b820322008-11-17 21:11:30 -05001694 struct btrfs_device *device;
1695 u64 super_flags;
1696
1697 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zhenge4404d62008-12-12 10:03:26 -05001698 if (!fs_devices->seeding)
Yan Zheng2b820322008-11-17 21:11:30 -05001699 return -EINVAL;
1700
Yan Zhenge4404d62008-12-12 10:03:26 -05001701 seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
1702 if (!seed_devices)
Yan Zheng2b820322008-11-17 21:11:30 -05001703 return -ENOMEM;
1704
Yan Zhenge4404d62008-12-12 10:03:26 -05001705 old_devices = clone_fs_devices(fs_devices);
1706 if (IS_ERR(old_devices)) {
1707 kfree(seed_devices);
1708 return PTR_ERR(old_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001709 }
Yan Zhenge4404d62008-12-12 10:03:26 -05001710
Yan Zheng2b820322008-11-17 21:11:30 -05001711 list_add(&old_devices->list, &fs_uuids);
1712
Yan Zhenge4404d62008-12-12 10:03:26 -05001713 memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
1714 seed_devices->opened = 1;
1715 INIT_LIST_HEAD(&seed_devices->devices);
1716 INIT_LIST_HEAD(&seed_devices->alloc_list);
Chris Masone5e9a522009-06-10 15:17:02 -04001717 mutex_init(&seed_devices->device_list_mutex);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001718
1719 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001720 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
1721 synchronize_rcu);
Xiao Guangrongc9513ed2011-04-20 10:07:30 +00001722 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
1723
Yan Zhenge4404d62008-12-12 10:03:26 -05001724 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
1725 list_for_each_entry(device, &seed_devices->devices, dev_list) {
1726 device->fs_devices = seed_devices;
1727 }
1728
Yan Zheng2b820322008-11-17 21:11:30 -05001729 fs_devices->seeding = 0;
1730 fs_devices->num_devices = 0;
1731 fs_devices->open_devices = 0;
Josef Bacik02db0842012-06-21 16:03:58 -04001732 fs_devices->total_devices = 0;
Yan Zhenge4404d62008-12-12 10:03:26 -05001733 fs_devices->seed = seed_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001734
1735 generate_random_uuid(fs_devices->fsid);
1736 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1737 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
1738 super_flags = btrfs_super_flags(disk_super) &
1739 ~BTRFS_SUPER_FLAG_SEEDING;
1740 btrfs_set_super_flags(disk_super, super_flags);
1741
1742 return 0;
1743}
1744
1745/*
1746 * strore the expected generation for seed devices in device items.
1747 */
1748static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
1749 struct btrfs_root *root)
1750{
1751 struct btrfs_path *path;
1752 struct extent_buffer *leaf;
1753 struct btrfs_dev_item *dev_item;
1754 struct btrfs_device *device;
1755 struct btrfs_key key;
1756 u8 fs_uuid[BTRFS_UUID_SIZE];
1757 u8 dev_uuid[BTRFS_UUID_SIZE];
1758 u64 devid;
1759 int ret;
1760
1761 path = btrfs_alloc_path();
1762 if (!path)
1763 return -ENOMEM;
1764
1765 root = root->fs_info->chunk_root;
1766 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
1767 key.offset = 0;
1768 key.type = BTRFS_DEV_ITEM_KEY;
1769
1770 while (1) {
1771 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1772 if (ret < 0)
1773 goto error;
1774
1775 leaf = path->nodes[0];
1776next_slot:
1777 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1778 ret = btrfs_next_leaf(root, path);
1779 if (ret > 0)
1780 break;
1781 if (ret < 0)
1782 goto error;
1783 leaf = path->nodes[0];
1784 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
David Sterbab3b4aa72011-04-21 01:20:15 +02001785 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05001786 continue;
1787 }
1788
1789 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1790 if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
1791 key.type != BTRFS_DEV_ITEM_KEY)
1792 break;
1793
1794 dev_item = btrfs_item_ptr(leaf, path->slots[0],
1795 struct btrfs_dev_item);
1796 devid = btrfs_device_id(leaf, dev_item);
1797 read_extent_buffer(leaf, dev_uuid,
1798 (unsigned long)btrfs_device_uuid(dev_item),
1799 BTRFS_UUID_SIZE);
1800 read_extent_buffer(leaf, fs_uuid,
1801 (unsigned long)btrfs_device_fsid(dev_item),
1802 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01001803 device = btrfs_find_device(root->fs_info, devid, dev_uuid,
1804 fs_uuid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001805 BUG_ON(!device); /* Logic error */
Yan Zheng2b820322008-11-17 21:11:30 -05001806
1807 if (device->fs_devices->seeding) {
1808 btrfs_set_device_generation(leaf, dev_item,
1809 device->generation);
1810 btrfs_mark_buffer_dirty(leaf);
1811 }
1812
1813 path->slots[0]++;
1814 goto next_slot;
1815 }
1816 ret = 0;
1817error:
1818 btrfs_free_path(path);
1819 return ret;
1820}
1821
Chris Mason788f20e2008-04-28 15:29:42 -04001822int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1823{
Josef Bacikd5e20032011-08-04 14:52:27 +00001824 struct request_queue *q;
Chris Mason788f20e2008-04-28 15:29:42 -04001825 struct btrfs_trans_handle *trans;
1826 struct btrfs_device *device;
1827 struct block_device *bdev;
Chris Mason788f20e2008-04-28 15:29:42 -04001828 struct list_head *devices;
Yan Zheng2b820322008-11-17 21:11:30 -05001829 struct super_block *sb = root->fs_info->sb;
Josef Bacik606686e2012-06-04 14:03:51 -04001830 struct rcu_string *name;
Chris Mason788f20e2008-04-28 15:29:42 -04001831 u64 total_bytes;
Yan Zheng2b820322008-11-17 21:11:30 -05001832 int seeding_dev = 0;
Chris Mason788f20e2008-04-28 15:29:42 -04001833 int ret = 0;
1834
Yan Zheng2b820322008-11-17 21:11:30 -05001835 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
Liu Bof8c5d0b2012-05-10 18:10:38 +08001836 return -EROFS;
Chris Mason788f20e2008-04-28 15:29:42 -04001837
Li Zefana5d16332011-12-07 20:08:40 -05001838 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
Tejun Heod4d77622010-11-13 11:55:18 +01001839 root->fs_info->bdev_holder);
Josef Bacik7f592032010-01-27 02:09:00 +00001840 if (IS_ERR(bdev))
1841 return PTR_ERR(bdev);
Chris Masona2135012008-06-25 16:01:30 -04001842
Yan Zheng2b820322008-11-17 21:11:30 -05001843 if (root->fs_info->fs_devices->seeding) {
1844 seeding_dev = 1;
1845 down_write(&sb->s_umount);
1846 mutex_lock(&uuid_mutex);
1847 }
1848
Chris Mason8c8bee12008-09-29 11:19:10 -04001849 filemap_write_and_wait(bdev->bd_inode->i_mapping);
Chris Masona2135012008-06-25 16:01:30 -04001850
Chris Mason788f20e2008-04-28 15:29:42 -04001851 devices = &root->fs_info->fs_devices->devices;
Liu Bod25628b2012-11-14 14:35:30 +00001852
1853 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05001854 list_for_each_entry(device, devices, dev_list) {
Chris Mason788f20e2008-04-28 15:29:42 -04001855 if (device->bdev == bdev) {
1856 ret = -EEXIST;
Liu Bod25628b2012-11-14 14:35:30 +00001857 mutex_unlock(
1858 &root->fs_info->fs_devices->device_list_mutex);
Yan Zheng2b820322008-11-17 21:11:30 -05001859 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001860 }
1861 }
Liu Bod25628b2012-11-14 14:35:30 +00001862 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001863
1864 device = kzalloc(sizeof(*device), GFP_NOFS);
1865 if (!device) {
1866 /* we can safely leave the fs_devices entry around */
1867 ret = -ENOMEM;
Yan Zheng2b820322008-11-17 21:11:30 -05001868 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001869 }
1870
Josef Bacik606686e2012-06-04 14:03:51 -04001871 name = rcu_string_strdup(device_path, GFP_NOFS);
1872 if (!name) {
Chris Mason788f20e2008-04-28 15:29:42 -04001873 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05001874 ret = -ENOMEM;
1875 goto error;
Chris Mason788f20e2008-04-28 15:29:42 -04001876 }
Josef Bacik606686e2012-06-04 14:03:51 -04001877 rcu_assign_pointer(device->name, name);
Yan Zheng2b820322008-11-17 21:11:30 -05001878
1879 ret = find_next_devid(root, &device->devid);
1880 if (ret) {
Josef Bacik606686e2012-06-04 14:03:51 -04001881 rcu_string_free(device->name);
Yan Zheng2b820322008-11-17 21:11:30 -05001882 kfree(device);
1883 goto error;
1884 }
1885
Yan, Zhenga22285a2010-05-16 10:48:46 -04001886 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001887 if (IS_ERR(trans)) {
Josef Bacik606686e2012-06-04 14:03:51 -04001888 rcu_string_free(device->name);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00001889 kfree(device);
1890 ret = PTR_ERR(trans);
1891 goto error;
1892 }
1893
Yan Zheng2b820322008-11-17 21:11:30 -05001894 lock_chunks(root);
1895
Josef Bacikd5e20032011-08-04 14:52:27 +00001896 q = bdev_get_queue(bdev);
1897 if (blk_queue_discard(q))
1898 device->can_discard = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05001899 device->writeable = 1;
1900 device->work.func = pending_bios_fn;
1901 generate_random_uuid(device->uuid);
1902 spin_lock_init(&device->io_lock);
1903 device->generation = trans->transid;
Chris Mason788f20e2008-04-28 15:29:42 -04001904 device->io_width = root->sectorsize;
1905 device->io_align = root->sectorsize;
1906 device->sector_size = root->sectorsize;
1907 device->total_bytes = i_size_read(bdev->bd_inode);
Yan Zheng2cc3c552009-06-04 09:23:50 -04001908 device->disk_total_bytes = device->total_bytes;
Chris Mason788f20e2008-04-28 15:29:42 -04001909 device->dev_root = root->fs_info->dev_root;
1910 device->bdev = bdev;
Chris Masondfe25022008-05-13 13:46:40 -04001911 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01001912 device->is_tgtdev_for_dev_replace = 0;
Ilya Dryomovfb01aa82011-02-15 18:12:57 +00001913 device->mode = FMODE_EXCL;
Zheng Yan325cd4b2008-09-05 16:43:54 -04001914 set_blocksize(device->bdev, 4096);
1915
Yan Zheng2b820322008-11-17 21:11:30 -05001916 if (seeding_dev) {
1917 sb->s_flags &= ~MS_RDONLY;
Li Zefan125ccb02011-12-08 15:07:24 +08001918 ret = btrfs_prepare_sprout(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001919 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2b820322008-11-17 21:11:30 -05001920 }
1921
1922 device->fs_devices = root->fs_info->fs_devices;
Chris Masone5e9a522009-06-10 15:17:02 -04001923
Chris Masone5e9a522009-06-10 15:17:02 -04001924 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
Xiao Guangrong1f781602011-04-20 10:09:16 +00001925 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
Yan Zheng2b820322008-11-17 21:11:30 -05001926 list_add(&device->dev_alloc_list,
1927 &root->fs_info->fs_devices->alloc_list);
1928 root->fs_info->fs_devices->num_devices++;
1929 root->fs_info->fs_devices->open_devices++;
1930 root->fs_info->fs_devices->rw_devices++;
Josef Bacik02db0842012-06-21 16:03:58 -04001931 root->fs_info->fs_devices->total_devices++;
Josef Bacikd5e20032011-08-04 14:52:27 +00001932 if (device->can_discard)
1933 root->fs_info->fs_devices->num_can_discard++;
Yan Zheng2b820322008-11-17 21:11:30 -05001934 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
1935
Josef Bacik2bf64752011-09-26 17:12:22 -04001936 spin_lock(&root->fs_info->free_chunk_lock);
1937 root->fs_info->free_chunk_space += device->total_bytes;
1938 spin_unlock(&root->fs_info->free_chunk_lock);
1939
Chris Masonc2898112009-06-10 09:51:32 -04001940 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
1941 root->fs_info->fs_devices->rotating = 1;
1942
David Sterba6c417612011-04-13 15:41:04 +02001943 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
1944 btrfs_set_super_total_bytes(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001945 total_bytes + device->total_bytes);
1946
David Sterba6c417612011-04-13 15:41:04 +02001947 total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
1948 btrfs_set_super_num_devices(root->fs_info->super_copy,
Chris Mason788f20e2008-04-28 15:29:42 -04001949 total_bytes + 1);
Chris Masone5e9a522009-06-10 15:17:02 -04001950 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
Chris Mason788f20e2008-04-28 15:29:42 -04001951
Yan Zheng2b820322008-11-17 21:11:30 -05001952 if (seeding_dev) {
1953 ret = init_first_rw_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001954 if (ret) {
1955 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001956 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001957 }
Yan Zheng2b820322008-11-17 21:11:30 -05001958 ret = btrfs_finish_sprout(trans, root);
David Sterba005d6422012-09-18 07:52:32 -06001959 if (ret) {
1960 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001961 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001962 }
Yan Zheng2b820322008-11-17 21:11:30 -05001963 } else {
1964 ret = btrfs_add_device(trans, root, device);
David Sterba005d6422012-09-18 07:52:32 -06001965 if (ret) {
1966 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001967 goto error_trans;
David Sterba005d6422012-09-18 07:52:32 -06001968 }
Yan Zheng2b820322008-11-17 21:11:30 -05001969 }
1970
Chris Mason913d9522009-03-10 13:17:18 -04001971 /*
1972 * we've got more storage, clear any full flags on the space
1973 * infos
1974 */
1975 btrfs_clear_space_info_full(root->fs_info);
1976
Chris Mason7d9eb122008-07-08 14:19:17 -04001977 unlock_chunks(root);
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02001978 root->fs_info->num_tolerated_disk_barrier_failures =
1979 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001980 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05001981
1982 if (seeding_dev) {
1983 mutex_unlock(&uuid_mutex);
1984 up_write(&sb->s_umount);
1985
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001986 if (ret) /* transaction commit */
1987 return ret;
1988
Yan Zheng2b820322008-11-17 21:11:30 -05001989 ret = btrfs_relocate_sys_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001990 if (ret < 0)
1991 btrfs_error(root->fs_info, ret,
1992 "Failed to relocate sys chunks after "
1993 "device initialization. This can be fixed "
1994 "using the \"btrfs balance\" command.");
Miao Xie671415b2012-10-16 11:26:46 +00001995 trans = btrfs_attach_transaction(root);
1996 if (IS_ERR(trans)) {
1997 if (PTR_ERR(trans) == -ENOENT)
1998 return 0;
1999 return PTR_ERR(trans);
2000 }
2001 ret = btrfs_commit_transaction(trans, root);
Yan Zheng2b820322008-11-17 21:11:30 -05002002 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002003
Chris Mason788f20e2008-04-28 15:29:42 -04002004 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002005
2006error_trans:
2007 unlock_chunks(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002008 btrfs_end_transaction(trans, root);
Josef Bacik606686e2012-06-04 14:03:51 -04002009 rcu_string_free(device->name);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002010 kfree(device);
Yan Zheng2b820322008-11-17 21:11:30 -05002011error:
Tejun Heoe525fd82010-11-13 11:55:17 +01002012 blkdev_put(bdev, FMODE_EXCL);
Yan Zheng2b820322008-11-17 21:11:30 -05002013 if (seeding_dev) {
2014 mutex_unlock(&uuid_mutex);
2015 up_write(&sb->s_umount);
2016 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002017 return ret;
Chris Mason788f20e2008-04-28 15:29:42 -04002018}
2019
Stefan Behrense93c89c2012-11-05 17:33:06 +01002020int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2021 struct btrfs_device **device_out)
2022{
2023 struct request_queue *q;
2024 struct btrfs_device *device;
2025 struct block_device *bdev;
2026 struct btrfs_fs_info *fs_info = root->fs_info;
2027 struct list_head *devices;
2028 struct rcu_string *name;
2029 int ret = 0;
2030
2031 *device_out = NULL;
2032 if (fs_info->fs_devices->seeding)
2033 return -EINVAL;
2034
2035 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2036 fs_info->bdev_holder);
2037 if (IS_ERR(bdev))
2038 return PTR_ERR(bdev);
2039
2040 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2041
2042 devices = &fs_info->fs_devices->devices;
2043 list_for_each_entry(device, devices, dev_list) {
2044 if (device->bdev == bdev) {
2045 ret = -EEXIST;
2046 goto error;
2047 }
2048 }
2049
2050 device = kzalloc(sizeof(*device), GFP_NOFS);
2051 if (!device) {
2052 ret = -ENOMEM;
2053 goto error;
2054 }
2055
2056 name = rcu_string_strdup(device_path, GFP_NOFS);
2057 if (!name) {
2058 kfree(device);
2059 ret = -ENOMEM;
2060 goto error;
2061 }
2062 rcu_assign_pointer(device->name, name);
2063
2064 q = bdev_get_queue(bdev);
2065 if (blk_queue_discard(q))
2066 device->can_discard = 1;
2067 mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
2068 device->writeable = 1;
2069 device->work.func = pending_bios_fn;
2070 generate_random_uuid(device->uuid);
2071 device->devid = BTRFS_DEV_REPLACE_DEVID;
2072 spin_lock_init(&device->io_lock);
2073 device->generation = 0;
2074 device->io_width = root->sectorsize;
2075 device->io_align = root->sectorsize;
2076 device->sector_size = root->sectorsize;
2077 device->total_bytes = i_size_read(bdev->bd_inode);
2078 device->disk_total_bytes = device->total_bytes;
2079 device->dev_root = fs_info->dev_root;
2080 device->bdev = bdev;
2081 device->in_fs_metadata = 1;
2082 device->is_tgtdev_for_dev_replace = 1;
2083 device->mode = FMODE_EXCL;
2084 set_blocksize(device->bdev, 4096);
2085 device->fs_devices = fs_info->fs_devices;
2086 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2087 fs_info->fs_devices->num_devices++;
2088 fs_info->fs_devices->open_devices++;
2089 if (device->can_discard)
2090 fs_info->fs_devices->num_can_discard++;
2091 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
2092
2093 *device_out = device;
2094 return ret;
2095
2096error:
2097 blkdev_put(bdev, FMODE_EXCL);
2098 return ret;
2099}
2100
2101void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
2102 struct btrfs_device *tgtdev)
2103{
2104 WARN_ON(fs_info->fs_devices->rw_devices == 0);
2105 tgtdev->io_width = fs_info->dev_root->sectorsize;
2106 tgtdev->io_align = fs_info->dev_root->sectorsize;
2107 tgtdev->sector_size = fs_info->dev_root->sectorsize;
2108 tgtdev->dev_root = fs_info->dev_root;
2109 tgtdev->in_fs_metadata = 1;
2110}
2111
Chris Masond3977122009-01-05 21:25:51 -05002112static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2113 struct btrfs_device *device)
Chris Mason0b86a832008-03-24 15:01:56 -04002114{
2115 int ret;
2116 struct btrfs_path *path;
2117 struct btrfs_root *root;
2118 struct btrfs_dev_item *dev_item;
2119 struct extent_buffer *leaf;
2120 struct btrfs_key key;
2121
2122 root = device->dev_root->fs_info->chunk_root;
2123
2124 path = btrfs_alloc_path();
2125 if (!path)
2126 return -ENOMEM;
2127
2128 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
2129 key.type = BTRFS_DEV_ITEM_KEY;
2130 key.offset = device->devid;
2131
2132 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2133 if (ret < 0)
2134 goto out;
2135
2136 if (ret > 0) {
2137 ret = -ENOENT;
2138 goto out;
2139 }
2140
2141 leaf = path->nodes[0];
2142 dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
2143
2144 btrfs_set_device_id(leaf, dev_item, device->devid);
2145 btrfs_set_device_type(leaf, dev_item, device->type);
2146 btrfs_set_device_io_align(leaf, dev_item, device->io_align);
2147 btrfs_set_device_io_width(leaf, dev_item, device->io_width);
2148 btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
Chris Balld6397ba2009-04-27 07:29:03 -04002149 btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
Chris Mason0b86a832008-03-24 15:01:56 -04002150 btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
2151 btrfs_mark_buffer_dirty(leaf);
2152
2153out:
2154 btrfs_free_path(path);
2155 return ret;
2156}
2157
Chris Mason7d9eb122008-07-08 14:19:17 -04002158static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
Chris Mason8f18cf12008-04-25 16:53:30 -04002159 struct btrfs_device *device, u64 new_size)
2160{
2161 struct btrfs_super_block *super_copy =
David Sterba6c417612011-04-13 15:41:04 +02002162 device->dev_root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002163 u64 old_total = btrfs_super_total_bytes(super_copy);
2164 u64 diff = new_size - device->total_bytes;
2165
Yan Zheng2b820322008-11-17 21:11:30 -05002166 if (!device->writeable)
2167 return -EACCES;
Stefan Behrens63a212a2012-11-05 18:29:28 +01002168 if (new_size <= device->total_bytes ||
2169 device->is_tgtdev_for_dev_replace)
Yan Zheng2b820322008-11-17 21:11:30 -05002170 return -EINVAL;
2171
Chris Mason8f18cf12008-04-25 16:53:30 -04002172 btrfs_set_super_total_bytes(super_copy, old_total + diff);
Yan Zheng2b820322008-11-17 21:11:30 -05002173 device->fs_devices->total_rw_bytes += diff;
2174
2175 device->total_bytes = new_size;
Chris Mason9779b722009-07-24 16:41:41 -04002176 device->disk_total_bytes = new_size;
Chris Mason4184ea72009-03-10 12:39:20 -04002177 btrfs_clear_space_info_full(device->dev_root->fs_info);
2178
Chris Mason8f18cf12008-04-25 16:53:30 -04002179 return btrfs_update_device(trans, device);
2180}
2181
Chris Mason7d9eb122008-07-08 14:19:17 -04002182int btrfs_grow_device(struct btrfs_trans_handle *trans,
2183 struct btrfs_device *device, u64 new_size)
2184{
2185 int ret;
2186 lock_chunks(device->dev_root);
2187 ret = __btrfs_grow_device(trans, device, new_size);
2188 unlock_chunks(device->dev_root);
2189 return ret;
2190}
2191
Chris Mason8f18cf12008-04-25 16:53:30 -04002192static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2193 struct btrfs_root *root,
2194 u64 chunk_tree, u64 chunk_objectid,
2195 u64 chunk_offset)
2196{
2197 int ret;
2198 struct btrfs_path *path;
2199 struct btrfs_key key;
2200
2201 root = root->fs_info->chunk_root;
2202 path = btrfs_alloc_path();
2203 if (!path)
2204 return -ENOMEM;
2205
2206 key.objectid = chunk_objectid;
2207 key.offset = chunk_offset;
2208 key.type = BTRFS_CHUNK_ITEM_KEY;
2209
2210 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002211 if (ret < 0)
2212 goto out;
2213 else if (ret > 0) { /* Logic error or corruption */
2214 btrfs_error(root->fs_info, -ENOENT,
2215 "Failed lookup while freeing chunk.");
2216 ret = -ENOENT;
2217 goto out;
2218 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002219
2220 ret = btrfs_del_item(trans, root, path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002221 if (ret < 0)
2222 btrfs_error(root->fs_info, ret,
2223 "Failed to delete chunk item.");
2224out:
Chris Mason8f18cf12008-04-25 16:53:30 -04002225 btrfs_free_path(path);
Tsutomu Itoh65a246c2011-05-19 04:37:44 +00002226 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002227}
2228
Christoph Hellwigb2950862008-12-02 09:54:17 -05002229static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
Chris Mason8f18cf12008-04-25 16:53:30 -04002230 chunk_offset)
2231{
David Sterba6c417612011-04-13 15:41:04 +02002232 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04002233 struct btrfs_disk_key *disk_key;
2234 struct btrfs_chunk *chunk;
2235 u8 *ptr;
2236 int ret = 0;
2237 u32 num_stripes;
2238 u32 array_size;
2239 u32 len = 0;
2240 u32 cur;
2241 struct btrfs_key key;
2242
2243 array_size = btrfs_super_sys_array_size(super_copy);
2244
2245 ptr = super_copy->sys_chunk_array;
2246 cur = 0;
2247
2248 while (cur < array_size) {
2249 disk_key = (struct btrfs_disk_key *)ptr;
2250 btrfs_disk_key_to_cpu(&key, disk_key);
2251
2252 len = sizeof(*disk_key);
2253
2254 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
2255 chunk = (struct btrfs_chunk *)(ptr + len);
2256 num_stripes = btrfs_stack_chunk_num_stripes(chunk);
2257 len += btrfs_chunk_item_size(num_stripes);
2258 } else {
2259 ret = -EIO;
2260 break;
2261 }
2262 if (key.objectid == chunk_objectid &&
2263 key.offset == chunk_offset) {
2264 memmove(ptr, ptr + len, array_size - (cur + len));
2265 array_size -= len;
2266 btrfs_set_super_sys_array_size(super_copy, array_size);
2267 } else {
2268 ptr += len;
2269 cur += len;
2270 }
2271 }
2272 return ret;
2273}
2274
Christoph Hellwigb2950862008-12-02 09:54:17 -05002275static int btrfs_relocate_chunk(struct btrfs_root *root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002276 u64 chunk_tree, u64 chunk_objectid,
2277 u64 chunk_offset)
2278{
2279 struct extent_map_tree *em_tree;
2280 struct btrfs_root *extent_root;
2281 struct btrfs_trans_handle *trans;
2282 struct extent_map *em;
2283 struct map_lookup *map;
2284 int ret;
2285 int i;
2286
2287 root = root->fs_info->chunk_root;
2288 extent_root = root->fs_info->extent_root;
2289 em_tree = &root->fs_info->mapping_tree.map_tree;
2290
Josef Bacikba1bf482009-09-11 16:11:19 -04002291 ret = btrfs_can_relocate(extent_root, chunk_offset);
2292 if (ret)
2293 return -ENOSPC;
2294
Chris Mason8f18cf12008-04-25 16:53:30 -04002295 /* step one, relocate all the extents inside this chunk */
Zheng Yan1a40e232008-09-26 10:09:34 -04002296 ret = btrfs_relocate_block_group(extent_root, chunk_offset);
Yan, Zhenga22285a2010-05-16 10:48:46 -04002297 if (ret)
2298 return ret;
Chris Mason8f18cf12008-04-25 16:53:30 -04002299
Yan, Zhenga22285a2010-05-16 10:48:46 -04002300 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002301 BUG_ON(IS_ERR(trans));
Chris Mason8f18cf12008-04-25 16:53:30 -04002302
Chris Mason7d9eb122008-07-08 14:19:17 -04002303 lock_chunks(root);
2304
Chris Mason8f18cf12008-04-25 16:53:30 -04002305 /*
2306 * step two, delete the device extents and the
2307 * chunk tree entries
2308 */
Chris Mason890871b2009-09-02 16:24:52 -04002309 read_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002310 em = lookup_extent_mapping(em_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04002311 read_unlock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002312
Tsutomu Itoh285190d2012-02-16 16:23:58 +09002313 BUG_ON(!em || em->start > chunk_offset ||
Chris Masona061fc82008-05-07 11:43:44 -04002314 em->start + em->len < chunk_offset);
Chris Mason8f18cf12008-04-25 16:53:30 -04002315 map = (struct map_lookup *)em->bdev;
2316
2317 for (i = 0; i < map->num_stripes; i++) {
2318 ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
2319 map->stripes[i].physical);
2320 BUG_ON(ret);
Chris Masona061fc82008-05-07 11:43:44 -04002321
Chris Masondfe25022008-05-13 13:46:40 -04002322 if (map->stripes[i].dev) {
2323 ret = btrfs_update_device(trans, map->stripes[i].dev);
2324 BUG_ON(ret);
2325 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002326 }
2327 ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
2328 chunk_offset);
2329
2330 BUG_ON(ret);
2331
liubo1abe9b82011-03-24 11:18:59 +00002332 trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
2333
Chris Mason8f18cf12008-04-25 16:53:30 -04002334 if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
2335 ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
2336 BUG_ON(ret);
Chris Mason8f18cf12008-04-25 16:53:30 -04002337 }
2338
Zheng Yan1a40e232008-09-26 10:09:34 -04002339 ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
2340 BUG_ON(ret);
2341
Chris Mason890871b2009-09-02 16:24:52 -04002342 write_lock(&em_tree->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -04002343 remove_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04002344 write_unlock(&em_tree->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04002345
Chris Mason8f18cf12008-04-25 16:53:30 -04002346 kfree(map);
2347 em->bdev = NULL;
2348
2349 /* once for the tree */
2350 free_extent_map(em);
Chris Mason8f18cf12008-04-25 16:53:30 -04002351 /* once for us */
2352 free_extent_map(em);
2353
Chris Mason7d9eb122008-07-08 14:19:17 -04002354 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04002355 btrfs_end_transaction(trans, root);
2356 return 0;
2357}
2358
Yan Zheng2b820322008-11-17 21:11:30 -05002359static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2360{
2361 struct btrfs_root *chunk_root = root->fs_info->chunk_root;
2362 struct btrfs_path *path;
2363 struct extent_buffer *leaf;
2364 struct btrfs_chunk *chunk;
2365 struct btrfs_key key;
2366 struct btrfs_key found_key;
2367 u64 chunk_tree = chunk_root->root_key.objectid;
2368 u64 chunk_type;
Josef Bacikba1bf482009-09-11 16:11:19 -04002369 bool retried = false;
2370 int failed = 0;
Yan Zheng2b820322008-11-17 21:11:30 -05002371 int ret;
2372
2373 path = btrfs_alloc_path();
2374 if (!path)
2375 return -ENOMEM;
2376
Josef Bacikba1bf482009-09-11 16:11:19 -04002377again:
Yan Zheng2b820322008-11-17 21:11:30 -05002378 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2379 key.offset = (u64)-1;
2380 key.type = BTRFS_CHUNK_ITEM_KEY;
2381
2382 while (1) {
2383 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2384 if (ret < 0)
2385 goto error;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002386 BUG_ON(ret == 0); /* Corruption */
Yan Zheng2b820322008-11-17 21:11:30 -05002387
2388 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2389 key.type);
2390 if (ret < 0)
2391 goto error;
2392 if (ret > 0)
2393 break;
2394
2395 leaf = path->nodes[0];
2396 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2397
2398 chunk = btrfs_item_ptr(leaf, path->slots[0],
2399 struct btrfs_chunk);
2400 chunk_type = btrfs_chunk_type(leaf, chunk);
David Sterbab3b4aa72011-04-21 01:20:15 +02002401 btrfs_release_path(path);
Yan Zheng2b820322008-11-17 21:11:30 -05002402
2403 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
2404 ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
2405 found_key.objectid,
2406 found_key.offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04002407 if (ret == -ENOSPC)
2408 failed++;
2409 else if (ret)
2410 BUG();
Yan Zheng2b820322008-11-17 21:11:30 -05002411 }
2412
2413 if (found_key.offset == 0)
2414 break;
2415 key.offset = found_key.offset - 1;
2416 }
2417 ret = 0;
Josef Bacikba1bf482009-09-11 16:11:19 -04002418 if (failed && !retried) {
2419 failed = 0;
2420 retried = true;
2421 goto again;
2422 } else if (failed && retried) {
2423 WARN_ON(1);
2424 ret = -ENOSPC;
2425 }
Yan Zheng2b820322008-11-17 21:11:30 -05002426error:
2427 btrfs_free_path(path);
2428 return ret;
2429}
2430
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002431static int insert_balance_item(struct btrfs_root *root,
2432 struct btrfs_balance_control *bctl)
2433{
2434 struct btrfs_trans_handle *trans;
2435 struct btrfs_balance_item *item;
2436 struct btrfs_disk_balance_args disk_bargs;
2437 struct btrfs_path *path;
2438 struct extent_buffer *leaf;
2439 struct btrfs_key key;
2440 int ret, err;
2441
2442 path = btrfs_alloc_path();
2443 if (!path)
2444 return -ENOMEM;
2445
2446 trans = btrfs_start_transaction(root, 0);
2447 if (IS_ERR(trans)) {
2448 btrfs_free_path(path);
2449 return PTR_ERR(trans);
2450 }
2451
2452 key.objectid = BTRFS_BALANCE_OBJECTID;
2453 key.type = BTRFS_BALANCE_ITEM_KEY;
2454 key.offset = 0;
2455
2456 ret = btrfs_insert_empty_item(trans, root, path, &key,
2457 sizeof(*item));
2458 if (ret)
2459 goto out;
2460
2461 leaf = path->nodes[0];
2462 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
2463
2464 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
2465
2466 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
2467 btrfs_set_balance_data(leaf, item, &disk_bargs);
2468 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
2469 btrfs_set_balance_meta(leaf, item, &disk_bargs);
2470 btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
2471 btrfs_set_balance_sys(leaf, item, &disk_bargs);
2472
2473 btrfs_set_balance_flags(leaf, item, bctl->flags);
2474
2475 btrfs_mark_buffer_dirty(leaf);
2476out:
2477 btrfs_free_path(path);
2478 err = btrfs_commit_transaction(trans, root);
2479 if (err && !ret)
2480 ret = err;
2481 return ret;
2482}
2483
2484static int del_balance_item(struct btrfs_root *root)
2485{
2486 struct btrfs_trans_handle *trans;
2487 struct btrfs_path *path;
2488 struct btrfs_key key;
2489 int ret, err;
2490
2491 path = btrfs_alloc_path();
2492 if (!path)
2493 return -ENOMEM;
2494
2495 trans = btrfs_start_transaction(root, 0);
2496 if (IS_ERR(trans)) {
2497 btrfs_free_path(path);
2498 return PTR_ERR(trans);
2499 }
2500
2501 key.objectid = BTRFS_BALANCE_OBJECTID;
2502 key.type = BTRFS_BALANCE_ITEM_KEY;
2503 key.offset = 0;
2504
2505 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2506 if (ret < 0)
2507 goto out;
2508 if (ret > 0) {
2509 ret = -ENOENT;
2510 goto out;
2511 }
2512
2513 ret = btrfs_del_item(trans, root, path);
2514out:
2515 btrfs_free_path(path);
2516 err = btrfs_commit_transaction(trans, root);
2517 if (err && !ret)
2518 ret = err;
2519 return ret;
2520}
2521
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002522/*
Ilya Dryomov59641012012-01-16 22:04:48 +02002523 * This is a heuristic used to reduce the number of chunks balanced on
2524 * resume after balance was interrupted.
2525 */
2526static void update_balance_args(struct btrfs_balance_control *bctl)
2527{
2528 /*
2529 * Turn on soft mode for chunk types that were being converted.
2530 */
2531 if (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)
2532 bctl->data.flags |= BTRFS_BALANCE_ARGS_SOFT;
2533 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)
2534 bctl->sys.flags |= BTRFS_BALANCE_ARGS_SOFT;
2535 if (bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)
2536 bctl->meta.flags |= BTRFS_BALANCE_ARGS_SOFT;
2537
2538 /*
2539 * Turn on usage filter if is not already used. The idea is
2540 * that chunks that we have already balanced should be
2541 * reasonably full. Don't do it for chunks that are being
2542 * converted - that will keep us from relocating unconverted
2543 * (albeit full) chunks.
2544 */
2545 if (!(bctl->data.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2546 !(bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2547 bctl->data.flags |= BTRFS_BALANCE_ARGS_USAGE;
2548 bctl->data.usage = 90;
2549 }
2550 if (!(bctl->sys.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2551 !(bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2552 bctl->sys.flags |= BTRFS_BALANCE_ARGS_USAGE;
2553 bctl->sys.usage = 90;
2554 }
2555 if (!(bctl->meta.flags & BTRFS_BALANCE_ARGS_USAGE) &&
2556 !(bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT)) {
2557 bctl->meta.flags |= BTRFS_BALANCE_ARGS_USAGE;
2558 bctl->meta.usage = 90;
2559 }
2560}
2561
2562/*
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002563 * Should be called with both balance and volume mutexes held to
2564 * serialize other volume operations (add_dev/rm_dev/resize) with
2565 * restriper. Same goes for unset_balance_control.
2566 */
2567static void set_balance_control(struct btrfs_balance_control *bctl)
2568{
2569 struct btrfs_fs_info *fs_info = bctl->fs_info;
2570
2571 BUG_ON(fs_info->balance_ctl);
2572
2573 spin_lock(&fs_info->balance_lock);
2574 fs_info->balance_ctl = bctl;
2575 spin_unlock(&fs_info->balance_lock);
2576}
2577
2578static void unset_balance_control(struct btrfs_fs_info *fs_info)
2579{
2580 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
2581
2582 BUG_ON(!fs_info->balance_ctl);
2583
2584 spin_lock(&fs_info->balance_lock);
2585 fs_info->balance_ctl = NULL;
2586 spin_unlock(&fs_info->balance_lock);
2587
2588 kfree(bctl);
2589}
2590
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002591/*
2592 * Balance filters. Return 1 if chunk should be filtered out
2593 * (should not be balanced).
2594 */
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002595static int chunk_profiles_filter(u64 chunk_type,
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002596 struct btrfs_balance_args *bargs)
2597{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002598 chunk_type = chunk_to_extended(chunk_type) &
2599 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002600
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002601 if (bargs->profiles & chunk_type)
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002602 return 0;
2603
2604 return 1;
2605}
2606
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002607static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
2608 struct btrfs_balance_args *bargs)
2609{
2610 struct btrfs_block_group_cache *cache;
2611 u64 chunk_used, user_thresh;
2612 int ret = 1;
2613
2614 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2615 chunk_used = btrfs_block_group_used(&cache->item);
2616
Ilya Dryomova105bb82013-01-21 15:15:56 +02002617 if (bargs->usage == 0)
2618 user_thresh = 0;
2619 else if (bargs->usage > 100)
2620 user_thresh = cache->key.offset;
2621 else
2622 user_thresh = div_factor_fine(cache->key.offset,
2623 bargs->usage);
2624
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002625 if (chunk_used < user_thresh)
2626 ret = 0;
2627
2628 btrfs_put_block_group(cache);
2629 return ret;
2630}
2631
Ilya Dryomov409d4042012-01-16 22:04:47 +02002632static int chunk_devid_filter(struct extent_buffer *leaf,
2633 struct btrfs_chunk *chunk,
2634 struct btrfs_balance_args *bargs)
2635{
2636 struct btrfs_stripe *stripe;
2637 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2638 int i;
2639
2640 for (i = 0; i < num_stripes; i++) {
2641 stripe = btrfs_stripe_nr(chunk, i);
2642 if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
2643 return 0;
2644 }
2645
2646 return 1;
2647}
2648
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002649/* [pstart, pend) */
2650static int chunk_drange_filter(struct extent_buffer *leaf,
2651 struct btrfs_chunk *chunk,
2652 u64 chunk_offset,
2653 struct btrfs_balance_args *bargs)
2654{
2655 struct btrfs_stripe *stripe;
2656 int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
2657 u64 stripe_offset;
2658 u64 stripe_length;
2659 int factor;
2660 int i;
2661
2662 if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
2663 return 0;
2664
2665 if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
2666 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
2667 factor = 2;
2668 else
2669 factor = 1;
2670 factor = num_stripes / factor;
2671
2672 for (i = 0; i < num_stripes; i++) {
2673 stripe = btrfs_stripe_nr(chunk, i);
2674 if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
2675 continue;
2676
2677 stripe_offset = btrfs_stripe_offset(leaf, stripe);
2678 stripe_length = btrfs_chunk_length(leaf, chunk);
2679 do_div(stripe_length, factor);
2680
2681 if (stripe_offset < bargs->pend &&
2682 stripe_offset + stripe_length > bargs->pstart)
2683 return 0;
2684 }
2685
2686 return 1;
2687}
2688
Ilya Dryomovea671762012-01-16 22:04:48 +02002689/* [vstart, vend) */
2690static int chunk_vrange_filter(struct extent_buffer *leaf,
2691 struct btrfs_chunk *chunk,
2692 u64 chunk_offset,
2693 struct btrfs_balance_args *bargs)
2694{
2695 if (chunk_offset < bargs->vend &&
2696 chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
2697 /* at least part of the chunk is inside this vrange */
2698 return 0;
2699
2700 return 1;
2701}
2702
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002703static int chunk_soft_convert_filter(u64 chunk_type,
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002704 struct btrfs_balance_args *bargs)
2705{
2706 if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
2707 return 0;
2708
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002709 chunk_type = chunk_to_extended(chunk_type) &
2710 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002711
Ilya Dryomov899c81e2012-03-27 17:09:16 +03002712 if (bargs->target == chunk_type)
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002713 return 1;
2714
2715 return 0;
2716}
2717
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002718static int should_balance_chunk(struct btrfs_root *root,
2719 struct extent_buffer *leaf,
2720 struct btrfs_chunk *chunk, u64 chunk_offset)
2721{
2722 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
2723 struct btrfs_balance_args *bargs = NULL;
2724 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
2725
2726 /* type filter */
2727 if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
2728 (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
2729 return 0;
2730 }
2731
2732 if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
2733 bargs = &bctl->data;
2734 else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
2735 bargs = &bctl->sys;
2736 else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
2737 bargs = &bctl->meta;
2738
Ilya Dryomoved25e9b2012-01-16 22:04:47 +02002739 /* profiles filter */
2740 if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
2741 chunk_profiles_filter(chunk_type, bargs)) {
2742 return 0;
2743 }
2744
Ilya Dryomov5ce5b3c2012-01-16 22:04:47 +02002745 /* usage filter */
2746 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
2747 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
2748 return 0;
2749 }
2750
Ilya Dryomov409d4042012-01-16 22:04:47 +02002751 /* devid filter */
2752 if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
2753 chunk_devid_filter(leaf, chunk, bargs)) {
2754 return 0;
2755 }
2756
Ilya Dryomov94e60d52012-01-16 22:04:48 +02002757 /* drange filter, makes sense only with devid filter */
2758 if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
2759 chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
2760 return 0;
2761 }
2762
Ilya Dryomovea671762012-01-16 22:04:48 +02002763 /* vrange filter */
2764 if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
2765 chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
2766 return 0;
2767 }
2768
Ilya Dryomovcfa4c962012-01-16 22:04:48 +02002769 /* soft profile changing mode */
2770 if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
2771 chunk_soft_convert_filter(chunk_type, bargs)) {
2772 return 0;
2773 }
2774
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002775 return 1;
2776}
2777
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002778static int __btrfs_balance(struct btrfs_fs_info *fs_info)
Chris Masonec44a352008-04-28 15:29:52 -04002779{
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002780 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002781 struct btrfs_root *chunk_root = fs_info->chunk_root;
2782 struct btrfs_root *dev_root = fs_info->dev_root;
2783 struct list_head *devices;
Chris Masonec44a352008-04-28 15:29:52 -04002784 struct btrfs_device *device;
2785 u64 old_size;
2786 u64 size_to_free;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002787 struct btrfs_chunk *chunk;
Chris Masonec44a352008-04-28 15:29:52 -04002788 struct btrfs_path *path;
2789 struct btrfs_key key;
Chris Masonec44a352008-04-28 15:29:52 -04002790 struct btrfs_key found_key;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002791 struct btrfs_trans_handle *trans;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002792 struct extent_buffer *leaf;
2793 int slot;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002794 int ret;
2795 int enospc_errors = 0;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002796 bool counting = true;
Chris Masonec44a352008-04-28 15:29:52 -04002797
Chris Masonec44a352008-04-28 15:29:52 -04002798 /* step one make some room on all the devices */
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002799 devices = &fs_info->fs_devices->devices;
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002800 list_for_each_entry(device, devices, dev_list) {
Chris Masonec44a352008-04-28 15:29:52 -04002801 old_size = device->total_bytes;
2802 size_to_free = div_factor(old_size, 1);
2803 size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
Yan Zheng2b820322008-11-17 21:11:30 -05002804 if (!device->writeable ||
Stefan Behrens63a212a2012-11-05 18:29:28 +01002805 device->total_bytes - device->bytes_used > size_to_free ||
2806 device->is_tgtdev_for_dev_replace)
Chris Masonec44a352008-04-28 15:29:52 -04002807 continue;
2808
2809 ret = btrfs_shrink_device(device, old_size - size_to_free);
Josef Bacikba1bf482009-09-11 16:11:19 -04002810 if (ret == -ENOSPC)
2811 break;
Chris Masonec44a352008-04-28 15:29:52 -04002812 BUG_ON(ret);
2813
Yan, Zhenga22285a2010-05-16 10:48:46 -04002814 trans = btrfs_start_transaction(dev_root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00002815 BUG_ON(IS_ERR(trans));
Chris Masonec44a352008-04-28 15:29:52 -04002816
2817 ret = btrfs_grow_device(trans, device, old_size);
2818 BUG_ON(ret);
2819
2820 btrfs_end_transaction(trans, dev_root);
2821 }
2822
2823 /* step two, relocate all the chunks */
2824 path = btrfs_alloc_path();
Mark Fasheh17e9f792011-07-12 11:10:23 -07002825 if (!path) {
2826 ret = -ENOMEM;
2827 goto error;
2828 }
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002829
2830 /* zero out stat counters */
2831 spin_lock(&fs_info->balance_lock);
2832 memset(&bctl->stat, 0, sizeof(bctl->stat));
2833 spin_unlock(&fs_info->balance_lock);
2834again:
Chris Masonec44a352008-04-28 15:29:52 -04002835 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2836 key.offset = (u64)-1;
2837 key.type = BTRFS_CHUNK_ITEM_KEY;
2838
Chris Masond3977122009-01-05 21:25:51 -05002839 while (1) {
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002840 if ((!counting && atomic_read(&fs_info->balance_pause_req)) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002841 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002842 ret = -ECANCELED;
2843 goto error;
2844 }
2845
Chris Masonec44a352008-04-28 15:29:52 -04002846 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2847 if (ret < 0)
2848 goto error;
2849
2850 /*
2851 * this shouldn't happen, it means the last relocate
2852 * failed
2853 */
2854 if (ret == 0)
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002855 BUG(); /* FIXME break ? */
Chris Masonec44a352008-04-28 15:29:52 -04002856
2857 ret = btrfs_previous_item(chunk_root, path, 0,
2858 BTRFS_CHUNK_ITEM_KEY);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002859 if (ret) {
2860 ret = 0;
Chris Masonec44a352008-04-28 15:29:52 -04002861 break;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002862 }
Chris Mason7d9eb122008-07-08 14:19:17 -04002863
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002864 leaf = path->nodes[0];
2865 slot = path->slots[0];
2866 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2867
Chris Masonec44a352008-04-28 15:29:52 -04002868 if (found_key.objectid != key.objectid)
2869 break;
Chris Mason7d9eb122008-07-08 14:19:17 -04002870
Chris Masonec44a352008-04-28 15:29:52 -04002871 /* chunk zero is special */
Josef Bacikba1bf482009-09-11 16:11:19 -04002872 if (found_key.offset == 0)
Chris Masonec44a352008-04-28 15:29:52 -04002873 break;
2874
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002875 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
2876
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002877 if (!counting) {
2878 spin_lock(&fs_info->balance_lock);
2879 bctl->stat.considered++;
2880 spin_unlock(&fs_info->balance_lock);
2881 }
2882
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002883 ret = should_balance_chunk(chunk_root, leaf, chunk,
2884 found_key.offset);
David Sterbab3b4aa72011-04-21 01:20:15 +02002885 btrfs_release_path(path);
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002886 if (!ret)
2887 goto loop;
2888
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002889 if (counting) {
2890 spin_lock(&fs_info->balance_lock);
2891 bctl->stat.expected++;
2892 spin_unlock(&fs_info->balance_lock);
2893 goto loop;
2894 }
2895
Chris Masonec44a352008-04-28 15:29:52 -04002896 ret = btrfs_relocate_chunk(chunk_root,
2897 chunk_root->root_key.objectid,
2898 found_key.objectid,
2899 found_key.offset);
Josef Bacik508794e2011-07-02 21:24:41 +00002900 if (ret && ret != -ENOSPC)
2901 goto error;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002902 if (ret == -ENOSPC) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002903 enospc_errors++;
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002904 } else {
2905 spin_lock(&fs_info->balance_lock);
2906 bctl->stat.completed++;
2907 spin_unlock(&fs_info->balance_lock);
2908 }
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002909loop:
Josef Bacikba1bf482009-09-11 16:11:19 -04002910 key.offset = found_key.offset - 1;
Chris Masonec44a352008-04-28 15:29:52 -04002911 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002912
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002913 if (counting) {
2914 btrfs_release_path(path);
2915 counting = false;
2916 goto again;
2917 }
Chris Masonec44a352008-04-28 15:29:52 -04002918error:
2919 btrfs_free_path(path);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002920 if (enospc_errors) {
2921 printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
2922 enospc_errors);
2923 if (!ret)
2924 ret = -ENOSPC;
2925 }
2926
Chris Masonec44a352008-04-28 15:29:52 -04002927 return ret;
2928}
2929
Ilya Dryomov0c460c02012-03-27 17:09:17 +03002930/**
2931 * alloc_profile_is_valid - see if a given profile is valid and reduced
2932 * @flags: profile to validate
2933 * @extended: if true @flags is treated as an extended profile
2934 */
2935static int alloc_profile_is_valid(u64 flags, int extended)
2936{
2937 u64 mask = (extended ? BTRFS_EXTENDED_PROFILE_MASK :
2938 BTRFS_BLOCK_GROUP_PROFILE_MASK);
2939
2940 flags &= ~BTRFS_BLOCK_GROUP_TYPE_MASK;
2941
2942 /* 1) check that all other bits are zeroed */
2943 if (flags & ~mask)
2944 return 0;
2945
2946 /* 2) see if profile is reduced */
2947 if (flags == 0)
2948 return !extended; /* "0" is valid for usual profiles */
2949
2950 /* true if exactly one bit set */
2951 return (flags & (flags - 1)) == 0;
2952}
2953
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002954static inline int balance_need_close(struct btrfs_fs_info *fs_info)
2955{
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002956 /* cancel requested || normal exit path */
2957 return atomic_read(&fs_info->balance_cancel_req) ||
2958 (atomic_read(&fs_info->balance_pause_req) == 0 &&
2959 atomic_read(&fs_info->balance_cancel_req) == 0);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002960}
2961
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002962static void __cancel_balance(struct btrfs_fs_info *fs_info)
2963{
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002964 int ret;
2965
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002966 unset_balance_control(fs_info);
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02002967 ret = del_balance_item(fs_info->tree_root);
2968 BUG_ON(ret);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02002969
2970 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002971}
2972
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02002973void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock,
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002974 struct btrfs_ioctl_balance_args *bargs);
2975
2976/*
2977 * Should be called with both balance and volume mutexes held
2978 */
2979int btrfs_balance(struct btrfs_balance_control *bctl,
2980 struct btrfs_ioctl_balance_args *bargs)
2981{
2982 struct btrfs_fs_info *fs_info = bctl->fs_info;
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002983 u64 allowed;
Ilya Dryomove4837f82012-03-27 17:09:17 +03002984 int mixed = 0;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002985 int ret;
Stefan Behrens8dabb742012-11-06 13:15:27 +01002986 u64 num_devices;
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002987
Ilya Dryomov837d5b62012-01-16 22:04:49 +02002988 if (btrfs_fs_closing(fs_info) ||
Ilya Dryomova7e99c62012-01-16 22:04:49 +02002989 atomic_read(&fs_info->balance_pause_req) ||
2990 atomic_read(&fs_info->balance_cancel_req)) {
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02002991 ret = -EINVAL;
2992 goto out;
2993 }
2994
Ilya Dryomove4837f82012-03-27 17:09:17 +03002995 allowed = btrfs_super_incompat_flags(fs_info->super_copy);
2996 if (allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
2997 mixed = 1;
2998
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02002999 /*
3000 * In case of mixed groups both data and meta should be picked,
3001 * and identical options should be given for both of them.
3002 */
Ilya Dryomove4837f82012-03-27 17:09:17 +03003003 allowed = BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA;
3004 if (mixed && (bctl->flags & allowed)) {
Ilya Dryomovf43ffb62012-01-16 22:04:47 +02003005 if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
3006 !(bctl->flags & BTRFS_BALANCE_METADATA) ||
3007 memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
3008 printk(KERN_ERR "btrfs: with mixed groups data and "
3009 "metadata balance options must be the same\n");
3010 ret = -EINVAL;
3011 goto out;
3012 }
3013 }
3014
Stefan Behrens8dabb742012-11-06 13:15:27 +01003015 num_devices = fs_info->fs_devices->num_devices;
3016 btrfs_dev_replace_lock(&fs_info->dev_replace);
3017 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
3018 BUG_ON(num_devices < 1);
3019 num_devices--;
3020 }
3021 btrfs_dev_replace_unlock(&fs_info->dev_replace);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003022 allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003023 if (num_devices == 1)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003024 allowed |= BTRFS_BLOCK_GROUP_DUP;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003025 else if (num_devices < 4)
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003026 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
3027 else
3028 allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
3029 BTRFS_BLOCK_GROUP_RAID10);
3030
Ilya Dryomov6728b192012-03-27 17:09:17 +03003031 if ((bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3032 (!alloc_profile_is_valid(bctl->data.target, 1) ||
3033 (bctl->data.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003034 printk(KERN_ERR "btrfs: unable to start balance with target "
3035 "data profile %llu\n",
3036 (unsigned long long)bctl->data.target);
3037 ret = -EINVAL;
3038 goto out;
3039 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003040 if ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3041 (!alloc_profile_is_valid(bctl->meta.target, 1) ||
3042 (bctl->meta.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003043 printk(KERN_ERR "btrfs: unable to start balance with target "
3044 "metadata profile %llu\n",
3045 (unsigned long long)bctl->meta.target);
3046 ret = -EINVAL;
3047 goto out;
3048 }
Ilya Dryomov6728b192012-03-27 17:09:17 +03003049 if ((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3050 (!alloc_profile_is_valid(bctl->sys.target, 1) ||
3051 (bctl->sys.target & ~allowed))) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003052 printk(KERN_ERR "btrfs: unable to start balance with target "
3053 "system profile %llu\n",
3054 (unsigned long long)bctl->sys.target);
3055 ret = -EINVAL;
3056 goto out;
3057 }
3058
Ilya Dryomove4837f82012-03-27 17:09:17 +03003059 /* allow dup'ed data chunks only in mixed mode */
3060 if (!mixed && (bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
Ilya Dryomov6728b192012-03-27 17:09:17 +03003061 (bctl->data.target & BTRFS_BLOCK_GROUP_DUP)) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003062 printk(KERN_ERR "btrfs: dup for data is not allowed\n");
3063 ret = -EINVAL;
3064 goto out;
3065 }
3066
3067 /* allow to reduce meta or sys integrity only if force set */
3068 allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3069 BTRFS_BLOCK_GROUP_RAID10;
3070 if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3071 (fs_info->avail_system_alloc_bits & allowed) &&
3072 !(bctl->sys.target & allowed)) ||
3073 ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
3074 (fs_info->avail_metadata_alloc_bits & allowed) &&
3075 !(bctl->meta.target & allowed))) {
3076 if (bctl->flags & BTRFS_BALANCE_FORCE) {
3077 printk(KERN_INFO "btrfs: force reducing metadata "
3078 "integrity\n");
3079 } else {
3080 printk(KERN_ERR "btrfs: balance will reduce metadata "
3081 "integrity, use force if you want this\n");
3082 ret = -EINVAL;
3083 goto out;
3084 }
3085 }
3086
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003087 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3088 int num_tolerated_disk_barrier_failures;
3089 u64 target = bctl->sys.target;
3090
3091 num_tolerated_disk_barrier_failures =
3092 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3093 if (num_tolerated_disk_barrier_failures > 0 &&
3094 (target &
3095 (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3096 BTRFS_AVAIL_ALLOC_BIT_SINGLE)))
3097 num_tolerated_disk_barrier_failures = 0;
3098 else if (num_tolerated_disk_barrier_failures > 1 &&
3099 (target &
3100 (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10)))
3101 num_tolerated_disk_barrier_failures = 1;
3102
3103 fs_info->num_tolerated_disk_barrier_failures =
3104 num_tolerated_disk_barrier_failures;
3105 }
3106
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003107 ret = insert_balance_item(fs_info->tree_root, bctl);
Ilya Dryomov59641012012-01-16 22:04:48 +02003108 if (ret && ret != -EEXIST)
Ilya Dryomov0940ebf2012-01-16 22:04:48 +02003109 goto out;
3110
Ilya Dryomov59641012012-01-16 22:04:48 +02003111 if (!(bctl->flags & BTRFS_BALANCE_RESUME)) {
3112 BUG_ON(ret == -EEXIST);
3113 set_balance_control(bctl);
3114 } else {
3115 BUG_ON(ret != -EEXIST);
3116 spin_lock(&fs_info->balance_lock);
3117 update_balance_args(bctl);
3118 spin_unlock(&fs_info->balance_lock);
3119 }
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003120
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003121 atomic_inc(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003122 mutex_unlock(&fs_info->balance_mutex);
3123
3124 ret = __btrfs_balance(fs_info);
3125
3126 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003127 atomic_dec(&fs_info->balance_running);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003128
3129 if (bargs) {
3130 memset(bargs, 0, sizeof(*bargs));
Ilya Dryomov19a39dc2012-01-16 22:04:49 +02003131 update_ioctl_balance_args(fs_info, 0, bargs);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003132 }
3133
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003134 if ((ret && ret != -ECANCELED && ret != -ENOSPC) ||
3135 balance_need_close(fs_info)) {
3136 __cancel_balance(fs_info);
3137 }
3138
Stefan Behrens5af3e8c2012-08-01 18:56:49 +02003139 if (bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3140 fs_info->num_tolerated_disk_barrier_failures =
3141 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
3142 }
3143
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003144 wake_up(&fs_info->balance_wait_q);
Ilya Dryomovc9e9f972012-01-16 22:04:47 +02003145
3146 return ret;
3147out:
Ilya Dryomov59641012012-01-16 22:04:48 +02003148 if (bctl->flags & BTRFS_BALANCE_RESUME)
3149 __cancel_balance(fs_info);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003150 else {
Ilya Dryomov59641012012-01-16 22:04:48 +02003151 kfree(bctl);
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003152 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3153 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003154 return ret;
3155}
3156
3157static int balance_kthread(void *data)
3158{
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003159 struct btrfs_fs_info *fs_info = data;
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003160 int ret = 0;
Ilya Dryomov59641012012-01-16 22:04:48 +02003161
3162 mutex_lock(&fs_info->volume_mutex);
3163 mutex_lock(&fs_info->balance_mutex);
3164
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003165 if (fs_info->balance_ctl) {
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003166 printk(KERN_INFO "btrfs: continuing balance\n");
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003167 ret = btrfs_balance(fs_info->balance_ctl, NULL);
Ilya Dryomov9555c6c2012-01-16 22:04:48 +02003168 }
Ilya Dryomov59641012012-01-16 22:04:48 +02003169
3170 mutex_unlock(&fs_info->balance_mutex);
3171 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003172
Ilya Dryomov59641012012-01-16 22:04:48 +02003173 return ret;
3174}
3175
Ilya Dryomov2b6ba622012-06-22 12:24:13 -06003176int btrfs_resume_balance_async(struct btrfs_fs_info *fs_info)
3177{
3178 struct task_struct *tsk;
3179
3180 spin_lock(&fs_info->balance_lock);
3181 if (!fs_info->balance_ctl) {
3182 spin_unlock(&fs_info->balance_lock);
3183 return 0;
3184 }
3185 spin_unlock(&fs_info->balance_lock);
3186
3187 if (btrfs_test_opt(fs_info->tree_root, SKIP_BALANCE)) {
3188 printk(KERN_INFO "btrfs: force skipping balance\n");
3189 return 0;
3190 }
3191
3192 tsk = kthread_run(balance_kthread, fs_info, "btrfs-balance");
3193 if (IS_ERR(tsk))
3194 return PTR_ERR(tsk);
3195
3196 return 0;
3197}
3198
Ilya Dryomov68310a52012-06-22 12:24:12 -06003199int btrfs_recover_balance(struct btrfs_fs_info *fs_info)
Ilya Dryomov59641012012-01-16 22:04:48 +02003200{
Ilya Dryomov59641012012-01-16 22:04:48 +02003201 struct btrfs_balance_control *bctl;
3202 struct btrfs_balance_item *item;
3203 struct btrfs_disk_balance_args disk_bargs;
3204 struct btrfs_path *path;
3205 struct extent_buffer *leaf;
3206 struct btrfs_key key;
3207 int ret;
3208
3209 path = btrfs_alloc_path();
3210 if (!path)
3211 return -ENOMEM;
3212
Ilya Dryomov68310a52012-06-22 12:24:12 -06003213 key.objectid = BTRFS_BALANCE_OBJECTID;
3214 key.type = BTRFS_BALANCE_ITEM_KEY;
3215 key.offset = 0;
3216
3217 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3218 if (ret < 0)
3219 goto out;
3220 if (ret > 0) { /* ret = -ENOENT; */
3221 ret = 0;
3222 goto out;
3223 }
3224
Ilya Dryomov59641012012-01-16 22:04:48 +02003225 bctl = kzalloc(sizeof(*bctl), GFP_NOFS);
3226 if (!bctl) {
3227 ret = -ENOMEM;
3228 goto out;
3229 }
3230
Ilya Dryomov59641012012-01-16 22:04:48 +02003231 leaf = path->nodes[0];
3232 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
3233
Ilya Dryomov68310a52012-06-22 12:24:12 -06003234 bctl->fs_info = fs_info;
3235 bctl->flags = btrfs_balance_flags(leaf, item);
3236 bctl->flags |= BTRFS_BALANCE_RESUME;
Ilya Dryomov59641012012-01-16 22:04:48 +02003237
3238 btrfs_balance_data(leaf, item, &disk_bargs);
3239 btrfs_disk_balance_args_to_cpu(&bctl->data, &disk_bargs);
3240 btrfs_balance_meta(leaf, item, &disk_bargs);
3241 btrfs_disk_balance_args_to_cpu(&bctl->meta, &disk_bargs);
3242 btrfs_balance_sys(leaf, item, &disk_bargs);
3243 btrfs_disk_balance_args_to_cpu(&bctl->sys, &disk_bargs);
3244
Ilya Dryomoved0fb782013-01-20 15:57:57 +02003245 WARN_ON(atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1));
3246
Ilya Dryomov68310a52012-06-22 12:24:12 -06003247 mutex_lock(&fs_info->volume_mutex);
3248 mutex_lock(&fs_info->balance_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003249
Ilya Dryomov68310a52012-06-22 12:24:12 -06003250 set_balance_control(bctl);
3251
3252 mutex_unlock(&fs_info->balance_mutex);
3253 mutex_unlock(&fs_info->volume_mutex);
Ilya Dryomov59641012012-01-16 22:04:48 +02003254out:
3255 btrfs_free_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003256 return ret;
3257}
3258
Ilya Dryomov837d5b62012-01-16 22:04:49 +02003259int btrfs_pause_balance(struct btrfs_fs_info *fs_info)
3260{
3261 int ret = 0;
3262
3263 mutex_lock(&fs_info->balance_mutex);
3264 if (!fs_info->balance_ctl) {
3265 mutex_unlock(&fs_info->balance_mutex);
3266 return -ENOTCONN;
3267 }
3268
3269 if (atomic_read(&fs_info->balance_running)) {
3270 atomic_inc(&fs_info->balance_pause_req);
3271 mutex_unlock(&fs_info->balance_mutex);
3272
3273 wait_event(fs_info->balance_wait_q,
3274 atomic_read(&fs_info->balance_running) == 0);
3275
3276 mutex_lock(&fs_info->balance_mutex);
3277 /* we are good with balance_ctl ripped off from under us */
3278 BUG_ON(atomic_read(&fs_info->balance_running));
3279 atomic_dec(&fs_info->balance_pause_req);
3280 } else {
3281 ret = -ENOTCONN;
3282 }
3283
3284 mutex_unlock(&fs_info->balance_mutex);
3285 return ret;
3286}
3287
Ilya Dryomova7e99c62012-01-16 22:04:49 +02003288int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)
3289{
3290 mutex_lock(&fs_info->balance_mutex);
3291 if (!fs_info->balance_ctl) {
3292 mutex_unlock(&fs_info->balance_mutex);
3293 return -ENOTCONN;
3294 }
3295
3296 atomic_inc(&fs_info->balance_cancel_req);
3297 /*
3298 * if we are running just wait and return, balance item is
3299 * deleted in btrfs_balance in this case
3300 */
3301 if (atomic_read(&fs_info->balance_running)) {
3302 mutex_unlock(&fs_info->balance_mutex);
3303 wait_event(fs_info->balance_wait_q,
3304 atomic_read(&fs_info->balance_running) == 0);
3305 mutex_lock(&fs_info->balance_mutex);
3306 } else {
3307 /* __cancel_balance needs volume_mutex */
3308 mutex_unlock(&fs_info->balance_mutex);
3309 mutex_lock(&fs_info->volume_mutex);
3310 mutex_lock(&fs_info->balance_mutex);
3311
3312 if (fs_info->balance_ctl)
3313 __cancel_balance(fs_info);
3314
3315 mutex_unlock(&fs_info->volume_mutex);
3316 }
3317
3318 BUG_ON(fs_info->balance_ctl || atomic_read(&fs_info->balance_running));
3319 atomic_dec(&fs_info->balance_cancel_req);
3320 mutex_unlock(&fs_info->balance_mutex);
3321 return 0;
3322}
3323
Chris Mason8f18cf12008-04-25 16:53:30 -04003324/*
3325 * shrinking a device means finding all of the device extents past
3326 * the new size, and then following the back refs to the chunks.
3327 * The chunk relocation code actually frees the device extent
3328 */
3329int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
3330{
3331 struct btrfs_trans_handle *trans;
3332 struct btrfs_root *root = device->dev_root;
3333 struct btrfs_dev_extent *dev_extent = NULL;
3334 struct btrfs_path *path;
3335 u64 length;
3336 u64 chunk_tree;
3337 u64 chunk_objectid;
3338 u64 chunk_offset;
3339 int ret;
3340 int slot;
Josef Bacikba1bf482009-09-11 16:11:19 -04003341 int failed = 0;
3342 bool retried = false;
Chris Mason8f18cf12008-04-25 16:53:30 -04003343 struct extent_buffer *l;
3344 struct btrfs_key key;
David Sterba6c417612011-04-13 15:41:04 +02003345 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason8f18cf12008-04-25 16:53:30 -04003346 u64 old_total = btrfs_super_total_bytes(super_copy);
Josef Bacikba1bf482009-09-11 16:11:19 -04003347 u64 old_size = device->total_bytes;
Chris Mason8f18cf12008-04-25 16:53:30 -04003348 u64 diff = device->total_bytes - new_size;
3349
Stefan Behrens63a212a2012-11-05 18:29:28 +01003350 if (device->is_tgtdev_for_dev_replace)
3351 return -EINVAL;
3352
Chris Mason8f18cf12008-04-25 16:53:30 -04003353 path = btrfs_alloc_path();
3354 if (!path)
3355 return -ENOMEM;
3356
Chris Mason8f18cf12008-04-25 16:53:30 -04003357 path->reada = 2;
3358
Chris Mason7d9eb122008-07-08 14:19:17 -04003359 lock_chunks(root);
3360
Chris Mason8f18cf12008-04-25 16:53:30 -04003361 device->total_bytes = new_size;
Josef Bacik2bf64752011-09-26 17:12:22 -04003362 if (device->writeable) {
Yan Zheng2b820322008-11-17 21:11:30 -05003363 device->fs_devices->total_rw_bytes -= diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003364 spin_lock(&root->fs_info->free_chunk_lock);
3365 root->fs_info->free_chunk_space -= diff;
3366 spin_unlock(&root->fs_info->free_chunk_lock);
3367 }
Chris Mason7d9eb122008-07-08 14:19:17 -04003368 unlock_chunks(root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003369
Josef Bacikba1bf482009-09-11 16:11:19 -04003370again:
Chris Mason8f18cf12008-04-25 16:53:30 -04003371 key.objectid = device->devid;
3372 key.offset = (u64)-1;
3373 key.type = BTRFS_DEV_EXTENT_KEY;
3374
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003375 do {
Chris Mason8f18cf12008-04-25 16:53:30 -04003376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3377 if (ret < 0)
3378 goto done;
3379
3380 ret = btrfs_previous_item(root, path, 0, key.type);
3381 if (ret < 0)
3382 goto done;
3383 if (ret) {
3384 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02003385 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003386 break;
Chris Mason8f18cf12008-04-25 16:53:30 -04003387 }
3388
3389 l = path->nodes[0];
3390 slot = path->slots[0];
3391 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
3392
Josef Bacikba1bf482009-09-11 16:11:19 -04003393 if (key.objectid != device->devid) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003394 btrfs_release_path(path);
Yan Zhengbf1fb512009-07-22 09:59:00 -04003395 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003396 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003397
3398 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
3399 length = btrfs_dev_extent_length(l, dev_extent);
3400
Josef Bacikba1bf482009-09-11 16:11:19 -04003401 if (key.offset + length <= new_size) {
David Sterbab3b4aa72011-04-21 01:20:15 +02003402 btrfs_release_path(path);
Chris Balld6397ba2009-04-27 07:29:03 -04003403 break;
Josef Bacikba1bf482009-09-11 16:11:19 -04003404 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003405
3406 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
3407 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
3408 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
David Sterbab3b4aa72011-04-21 01:20:15 +02003409 btrfs_release_path(path);
Chris Mason8f18cf12008-04-25 16:53:30 -04003410
3411 ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
3412 chunk_offset);
Josef Bacikba1bf482009-09-11 16:11:19 -04003413 if (ret && ret != -ENOSPC)
Chris Mason8f18cf12008-04-25 16:53:30 -04003414 goto done;
Josef Bacikba1bf482009-09-11 16:11:19 -04003415 if (ret == -ENOSPC)
3416 failed++;
Ilya Dryomov213e64d2012-03-27 17:09:18 +03003417 } while (key.offset-- > 0);
Josef Bacikba1bf482009-09-11 16:11:19 -04003418
3419 if (failed && !retried) {
3420 failed = 0;
3421 retried = true;
3422 goto again;
3423 } else if (failed && retried) {
3424 ret = -ENOSPC;
3425 lock_chunks(root);
3426
3427 device->total_bytes = old_size;
3428 if (device->writeable)
3429 device->fs_devices->total_rw_bytes += diff;
Josef Bacik2bf64752011-09-26 17:12:22 -04003430 spin_lock(&root->fs_info->free_chunk_lock);
3431 root->fs_info->free_chunk_space += diff;
3432 spin_unlock(&root->fs_info->free_chunk_lock);
Josef Bacikba1bf482009-09-11 16:11:19 -04003433 unlock_chunks(root);
3434 goto done;
Chris Mason8f18cf12008-04-25 16:53:30 -04003435 }
3436
Chris Balld6397ba2009-04-27 07:29:03 -04003437 /* Shrinking succeeded, else we would be at "done". */
Yan, Zhenga22285a2010-05-16 10:48:46 -04003438 trans = btrfs_start_transaction(root, 0);
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00003439 if (IS_ERR(trans)) {
3440 ret = PTR_ERR(trans);
3441 goto done;
3442 }
3443
Chris Balld6397ba2009-04-27 07:29:03 -04003444 lock_chunks(root);
3445
3446 device->disk_total_bytes = new_size;
3447 /* Now btrfs_update_device() will change the on-disk size. */
3448 ret = btrfs_update_device(trans, device);
3449 if (ret) {
3450 unlock_chunks(root);
3451 btrfs_end_transaction(trans, root);
3452 goto done;
3453 }
3454 WARN_ON(diff > old_total);
3455 btrfs_set_super_total_bytes(super_copy, old_total - diff);
3456 unlock_chunks(root);
3457 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003458done:
3459 btrfs_free_path(path);
3460 return ret;
3461}
3462
Li Zefan125ccb02011-12-08 15:07:24 +08003463static int btrfs_add_system_chunk(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04003464 struct btrfs_key *key,
3465 struct btrfs_chunk *chunk, int item_size)
3466{
David Sterba6c417612011-04-13 15:41:04 +02003467 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Mason0b86a832008-03-24 15:01:56 -04003468 struct btrfs_disk_key disk_key;
3469 u32 array_size;
3470 u8 *ptr;
3471
3472 array_size = btrfs_super_sys_array_size(super_copy);
3473 if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
3474 return -EFBIG;
3475
3476 ptr = super_copy->sys_chunk_array + array_size;
3477 btrfs_cpu_key_to_disk(&disk_key, key);
3478 memcpy(ptr, &disk_key, sizeof(disk_key));
3479 ptr += sizeof(disk_key);
3480 memcpy(ptr, chunk, item_size);
3481 item_size += sizeof(disk_key);
3482 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
3483 return 0;
3484}
3485
Miao Xieb2117a32011-01-05 10:07:28 +00003486/*
Arne Jansen73c5de02011-04-12 12:07:57 +02003487 * sort the devices in descending order by max_avail, total_avail
Miao Xieb2117a32011-01-05 10:07:28 +00003488 */
Arne Jansen73c5de02011-04-12 12:07:57 +02003489static int btrfs_cmp_device_info(const void *a, const void *b)
Miao Xieb2117a32011-01-05 10:07:28 +00003490{
Arne Jansen73c5de02011-04-12 12:07:57 +02003491 const struct btrfs_device_info *di_a = a;
3492 const struct btrfs_device_info *di_b = b;
Miao Xieb2117a32011-01-05 10:07:28 +00003493
Arne Jansen73c5de02011-04-12 12:07:57 +02003494 if (di_a->max_avail > di_b->max_avail)
3495 return -1;
3496 if (di_a->max_avail < di_b->max_avail)
3497 return 1;
3498 if (di_a->total_avail > di_b->total_avail)
3499 return -1;
3500 if (di_a->total_avail < di_b->total_avail)
3501 return 1;
Miao Xieb2117a32011-01-05 10:07:28 +00003502 return 0;
3503}
3504
Liu Bo31e50222012-11-21 14:18:10 +00003505struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
3506 { 2, 1, 0, 4, 2, 2 /* raid10 */ },
3507 { 1, 1, 2, 2, 2, 2 /* raid1 */ },
3508 { 1, 2, 1, 1, 1, 2 /* dup */ },
3509 { 1, 1, 0, 2, 1, 1 /* raid0 */ },
3510 { 1, 1, 0, 1, 1, 1 /* single */ },
3511};
3512
Miao Xieb2117a32011-01-05 10:07:28 +00003513static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3514 struct btrfs_root *extent_root,
3515 struct map_lookup **map_ret,
Arne Jansen73c5de02011-04-12 12:07:57 +02003516 u64 *num_bytes_out, u64 *stripe_size_out,
Miao Xieb2117a32011-01-05 10:07:28 +00003517 u64 start, u64 type)
3518{
3519 struct btrfs_fs_info *info = extent_root->fs_info;
Miao Xieb2117a32011-01-05 10:07:28 +00003520 struct btrfs_fs_devices *fs_devices = info->fs_devices;
3521 struct list_head *cur;
Arne Jansen73c5de02011-04-12 12:07:57 +02003522 struct map_lookup *map = NULL;
Miao Xieb2117a32011-01-05 10:07:28 +00003523 struct extent_map_tree *em_tree;
3524 struct extent_map *em;
Arne Jansen73c5de02011-04-12 12:07:57 +02003525 struct btrfs_device_info *devices_info = NULL;
3526 u64 total_avail;
3527 int num_stripes; /* total number of stripes to allocate */
3528 int sub_stripes; /* sub_stripes info for map */
3529 int dev_stripes; /* stripes per dev */
3530 int devs_max; /* max devs to use */
3531 int devs_min; /* min devs needed */
3532 int devs_increment; /* ndevs has to be a multiple of this */
3533 int ncopies; /* how many copies to data has */
Miao Xieb2117a32011-01-05 10:07:28 +00003534 int ret;
Arne Jansen73c5de02011-04-12 12:07:57 +02003535 u64 max_stripe_size;
3536 u64 max_chunk_size;
3537 u64 stripe_size;
3538 u64 num_bytes;
3539 int ndevs;
3540 int i;
3541 int j;
Liu Bo31e50222012-11-21 14:18:10 +00003542 int index;
Miao Xieb2117a32011-01-05 10:07:28 +00003543
Ilya Dryomov0c460c02012-03-27 17:09:17 +03003544 BUG_ON(!alloc_profile_is_valid(type, 0));
Arne Jansen73c5de02011-04-12 12:07:57 +02003545
Miao Xieb2117a32011-01-05 10:07:28 +00003546 if (list_empty(&fs_devices->alloc_list))
3547 return -ENOSPC;
3548
Liu Bo31e50222012-11-21 14:18:10 +00003549 index = __get_raid_index(type);
Arne Jansen73c5de02011-04-12 12:07:57 +02003550
Liu Bo31e50222012-11-21 14:18:10 +00003551 sub_stripes = btrfs_raid_array[index].sub_stripes;
3552 dev_stripes = btrfs_raid_array[index].dev_stripes;
3553 devs_max = btrfs_raid_array[index].devs_max;
3554 devs_min = btrfs_raid_array[index].devs_min;
3555 devs_increment = btrfs_raid_array[index].devs_increment;
3556 ncopies = btrfs_raid_array[index].ncopies;
Arne Jansen73c5de02011-04-12 12:07:57 +02003557
3558 if (type & BTRFS_BLOCK_GROUP_DATA) {
3559 max_stripe_size = 1024 * 1024 * 1024;
3560 max_chunk_size = 10 * max_stripe_size;
3561 } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason11003732012-01-06 15:47:38 -05003562 /* for larger filesystems, use larger metadata chunks */
3563 if (fs_devices->total_rw_bytes > 50ULL * 1024 * 1024 * 1024)
3564 max_stripe_size = 1024 * 1024 * 1024;
3565 else
3566 max_stripe_size = 256 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003567 max_chunk_size = max_stripe_size;
3568 } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
Chris Mason96bdc7d2012-01-16 08:13:11 -05003569 max_stripe_size = 32 * 1024 * 1024;
Arne Jansen73c5de02011-04-12 12:07:57 +02003570 max_chunk_size = 2 * max_stripe_size;
3571 } else {
3572 printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
3573 type);
3574 BUG_ON(1);
3575 }
3576
3577 /* we don't want a chunk larger than 10% of writeable space */
3578 max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
3579 max_chunk_size);
Miao Xieb2117a32011-01-05 10:07:28 +00003580
3581 devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
3582 GFP_NOFS);
3583 if (!devices_info)
3584 return -ENOMEM;
3585
Arne Jansen73c5de02011-04-12 12:07:57 +02003586 cur = fs_devices->alloc_list.next;
3587
3588 /*
3589 * in the first pass through the devices list, we gather information
3590 * about the available holes on each device.
3591 */
3592 ndevs = 0;
3593 while (cur != &fs_devices->alloc_list) {
3594 struct btrfs_device *device;
3595 u64 max_avail;
3596 u64 dev_offset;
3597
3598 device = list_entry(cur, struct btrfs_device, dev_alloc_list);
3599
3600 cur = cur->next;
3601
3602 if (!device->writeable) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003603 WARN(1, KERN_ERR
Arne Jansen73c5de02011-04-12 12:07:57 +02003604 "btrfs: read-only device in alloc_list\n");
Arne Jansen73c5de02011-04-12 12:07:57 +02003605 continue;
3606 }
3607
Stefan Behrens63a212a2012-11-05 18:29:28 +01003608 if (!device->in_fs_metadata ||
3609 device->is_tgtdev_for_dev_replace)
Arne Jansen73c5de02011-04-12 12:07:57 +02003610 continue;
3611
3612 if (device->total_bytes > device->bytes_used)
3613 total_avail = device->total_bytes - device->bytes_used;
3614 else
3615 total_avail = 0;
liubo38c01b92011-08-02 02:39:03 +00003616
3617 /* If there is no space on this device, skip it. */
3618 if (total_avail == 0)
3619 continue;
Arne Jansen73c5de02011-04-12 12:07:57 +02003620
Li Zefan125ccb02011-12-08 15:07:24 +08003621 ret = find_free_dev_extent(device,
Arne Jansen73c5de02011-04-12 12:07:57 +02003622 max_stripe_size * dev_stripes,
3623 &dev_offset, &max_avail);
3624 if (ret && ret != -ENOSPC)
3625 goto error;
3626
3627 if (ret == 0)
3628 max_avail = max_stripe_size * dev_stripes;
3629
3630 if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
3631 continue;
3632
3633 devices_info[ndevs].dev_offset = dev_offset;
3634 devices_info[ndevs].max_avail = max_avail;
3635 devices_info[ndevs].total_avail = total_avail;
3636 devices_info[ndevs].dev = device;
3637 ++ndevs;
Stefan Behrens8dabb742012-11-06 13:15:27 +01003638 WARN_ON(ndevs > fs_devices->rw_devices);
Arne Jansen73c5de02011-04-12 12:07:57 +02003639 }
3640
3641 /*
3642 * now sort the devices by hole size / available space
3643 */
3644 sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
3645 btrfs_cmp_device_info, NULL);
3646
3647 /* round down to number of usable stripes */
3648 ndevs -= ndevs % devs_increment;
3649
3650 if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
3651 ret = -ENOSPC;
3652 goto error;
3653 }
3654
3655 if (devs_max && ndevs > devs_max)
3656 ndevs = devs_max;
3657 /*
3658 * the primary goal is to maximize the number of stripes, so use as many
3659 * devices as possible, even if the stripes are not maximum sized.
3660 */
3661 stripe_size = devices_info[ndevs-1].max_avail;
3662 num_stripes = ndevs * dev_stripes;
3663
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003664 if (stripe_size * ndevs > max_chunk_size * ncopies) {
Arne Jansen73c5de02011-04-12 12:07:57 +02003665 stripe_size = max_chunk_size * ncopies;
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003666 do_div(stripe_size, ndevs);
Arne Jansen73c5de02011-04-12 12:07:57 +02003667 }
3668
3669 do_div(stripe_size, dev_stripes);
Ilya Dryomov37db63a2012-04-13 17:05:08 +03003670
3671 /* align to BTRFS_STRIPE_LEN */
Arne Jansen73c5de02011-04-12 12:07:57 +02003672 do_div(stripe_size, BTRFS_STRIPE_LEN);
3673 stripe_size *= BTRFS_STRIPE_LEN;
3674
Miao Xieb2117a32011-01-05 10:07:28 +00003675 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
3676 if (!map) {
3677 ret = -ENOMEM;
3678 goto error;
3679 }
3680 map->num_stripes = num_stripes;
Chris Mason9b3f68b2008-04-18 10:29:51 -04003681
Arne Jansen73c5de02011-04-12 12:07:57 +02003682 for (i = 0; i < ndevs; ++i) {
3683 for (j = 0; j < dev_stripes; ++j) {
3684 int s = i * dev_stripes + j;
3685 map->stripes[s].dev = devices_info[i].dev;
3686 map->stripes[s].physical = devices_info[i].dev_offset +
3687 j * stripe_size;
Chris Masona40a90a2008-04-18 11:55:51 -04003688 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003689 }
Chris Mason593060d2008-03-25 16:50:33 -04003690 map->sector_size = extent_root->sectorsize;
Miao Xieb2117a32011-01-05 10:07:28 +00003691 map->stripe_len = BTRFS_STRIPE_LEN;
3692 map->io_align = BTRFS_STRIPE_LEN;
3693 map->io_width = BTRFS_STRIPE_LEN;
Chris Mason593060d2008-03-25 16:50:33 -04003694 map->type = type;
Chris Mason321aecc2008-04-16 10:49:51 -04003695 map->sub_stripes = sub_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04003696
Yan Zheng2b820322008-11-17 21:11:30 -05003697 *map_ret = map;
Arne Jansen73c5de02011-04-12 12:07:57 +02003698 num_bytes = stripe_size * (num_stripes / ncopies);
Chris Mason0b86a832008-03-24 15:01:56 -04003699
Arne Jansen73c5de02011-04-12 12:07:57 +02003700 *stripe_size_out = stripe_size;
3701 *num_bytes_out = num_bytes;
3702
3703 trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
liubo1abe9b82011-03-24 11:18:59 +00003704
David Sterba172ddd62011-04-21 00:48:27 +02003705 em = alloc_extent_map();
Yan Zheng2b820322008-11-17 21:11:30 -05003706 if (!em) {
Miao Xieb2117a32011-01-05 10:07:28 +00003707 ret = -ENOMEM;
3708 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003709 }
Chris Mason0b86a832008-03-24 15:01:56 -04003710 em->bdev = (struct block_device *)map;
Yan Zheng2b820322008-11-17 21:11:30 -05003711 em->start = start;
Arne Jansen73c5de02011-04-12 12:07:57 +02003712 em->len = num_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04003713 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003714 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04003715
Chris Mason0b86a832008-03-24 15:01:56 -04003716 em_tree = &extent_root->fs_info->mapping_tree.map_tree;
Chris Mason890871b2009-09-02 16:24:52 -04003717 write_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003718 ret = add_extent_mapping(em_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003719 write_unlock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003720 free_extent_map(em);
Mark Fasheh1dd46022011-09-08 17:29:00 -07003721 if (ret)
3722 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003723
3724 ret = btrfs_make_block_group(trans, extent_root, 0, type,
3725 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003726 start, num_bytes);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003727 if (ret)
3728 goto error;
Yan Zheng2b820322008-11-17 21:11:30 -05003729
Arne Jansen73c5de02011-04-12 12:07:57 +02003730 for (i = 0; i < map->num_stripes; ++i) {
3731 struct btrfs_device *device;
3732 u64 dev_offset;
3733
3734 device = map->stripes[i].dev;
3735 dev_offset = map->stripes[i].physical;
Yan Zheng2b820322008-11-17 21:11:30 -05003736
3737 ret = btrfs_alloc_dev_extent(trans, device,
3738 info->chunk_root->root_key.objectid,
3739 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
Arne Jansen73c5de02011-04-12 12:07:57 +02003740 start, dev_offset, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003741 if (ret) {
3742 btrfs_abort_transaction(trans, extent_root, ret);
3743 goto error;
3744 }
Yan Zheng2b820322008-11-17 21:11:30 -05003745 }
3746
Miao Xieb2117a32011-01-05 10:07:28 +00003747 kfree(devices_info);
Yan Zheng2b820322008-11-17 21:11:30 -05003748 return 0;
Miao Xieb2117a32011-01-05 10:07:28 +00003749
3750error:
3751 kfree(map);
3752 kfree(devices_info);
3753 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003754}
3755
3756static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
3757 struct btrfs_root *extent_root,
3758 struct map_lookup *map, u64 chunk_offset,
3759 u64 chunk_size, u64 stripe_size)
3760{
3761 u64 dev_offset;
3762 struct btrfs_key key;
3763 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3764 struct btrfs_device *device;
3765 struct btrfs_chunk *chunk;
3766 struct btrfs_stripe *stripe;
3767 size_t item_size = btrfs_chunk_item_size(map->num_stripes);
3768 int index = 0;
3769 int ret;
3770
3771 chunk = kzalloc(item_size, GFP_NOFS);
3772 if (!chunk)
3773 return -ENOMEM;
3774
3775 index = 0;
3776 while (index < map->num_stripes) {
3777 device = map->stripes[index].dev;
3778 device->bytes_used += stripe_size;
3779 ret = btrfs_update_device(trans, device);
Mark Fasheh3acd3952011-09-08 17:40:01 -07003780 if (ret)
3781 goto out_free;
Yan Zheng2b820322008-11-17 21:11:30 -05003782 index++;
3783 }
3784
Josef Bacik2bf64752011-09-26 17:12:22 -04003785 spin_lock(&extent_root->fs_info->free_chunk_lock);
3786 extent_root->fs_info->free_chunk_space -= (stripe_size *
3787 map->num_stripes);
3788 spin_unlock(&extent_root->fs_info->free_chunk_lock);
3789
Yan Zheng2b820322008-11-17 21:11:30 -05003790 index = 0;
3791 stripe = &chunk->stripe;
3792 while (index < map->num_stripes) {
3793 device = map->stripes[index].dev;
3794 dev_offset = map->stripes[index].physical;
3795
3796 btrfs_set_stack_stripe_devid(stripe, device->devid);
3797 btrfs_set_stack_stripe_offset(stripe, dev_offset);
3798 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
3799 stripe++;
3800 index++;
3801 }
3802
3803 btrfs_set_stack_chunk_length(chunk, chunk_size);
3804 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
3805 btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
3806 btrfs_set_stack_chunk_type(chunk, map->type);
3807 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
3808 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
3809 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
3810 btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
3811 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
3812
3813 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3814 key.type = BTRFS_CHUNK_ITEM_KEY;
3815 key.offset = chunk_offset;
3816
3817 ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003818
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003819 if (ret == 0 && map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
3820 /*
3821 * TODO: Cleanup of inserted chunk root in case of
3822 * failure.
3823 */
Li Zefan125ccb02011-12-08 15:07:24 +08003824 ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
Yan Zheng2b820322008-11-17 21:11:30 -05003825 item_size);
Yan Zheng2b820322008-11-17 21:11:30 -05003826 }
liubo1abe9b82011-03-24 11:18:59 +00003827
Mark Fasheh3acd3952011-09-08 17:40:01 -07003828out_free:
Yan Zheng2b820322008-11-17 21:11:30 -05003829 kfree(chunk);
Mark Fasheh4ed1d162011-08-10 12:32:10 -07003830 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003831}
3832
3833/*
3834 * Chunk allocation falls into two parts. The first part does works
3835 * that make the new allocated chunk useable, but not do any operation
3836 * that modifies the chunk tree. The second part does the works that
3837 * require modifying the chunk tree. This division is important for the
3838 * bootstrap process of adding storage to a seed btrfs.
3839 */
3840int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
3841 struct btrfs_root *extent_root, u64 type)
3842{
3843 u64 chunk_offset;
3844 u64 chunk_size;
3845 u64 stripe_size;
3846 struct map_lookup *map;
3847 struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
3848 int ret;
3849
3850 ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
3851 &chunk_offset);
3852 if (ret)
3853 return ret;
3854
3855 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3856 &stripe_size, chunk_offset, type);
3857 if (ret)
3858 return ret;
3859
3860 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3861 chunk_size, stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003862 if (ret)
3863 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003864 return 0;
3865}
3866
Chris Masond3977122009-01-05 21:25:51 -05003867static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
Yan Zheng2b820322008-11-17 21:11:30 -05003868 struct btrfs_root *root,
3869 struct btrfs_device *device)
3870{
3871 u64 chunk_offset;
3872 u64 sys_chunk_offset;
3873 u64 chunk_size;
3874 u64 sys_chunk_size;
3875 u64 stripe_size;
3876 u64 sys_stripe_size;
3877 u64 alloc_profile;
3878 struct map_lookup *map;
3879 struct map_lookup *sys_map;
3880 struct btrfs_fs_info *fs_info = root->fs_info;
3881 struct btrfs_root *extent_root = fs_info->extent_root;
3882 int ret;
3883
3884 ret = find_next_chunk(fs_info->chunk_root,
3885 BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003886 if (ret)
3887 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003888
3889 alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003890 fs_info->avail_metadata_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003891 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3892
3893 ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
3894 &stripe_size, chunk_offset, alloc_profile);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003895 if (ret)
3896 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003897
3898 sys_chunk_offset = chunk_offset + chunk_size;
3899
3900 alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003901 fs_info->avail_system_alloc_bits;
Yan Zheng2b820322008-11-17 21:11:30 -05003902 alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
3903
3904 ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
3905 &sys_chunk_size, &sys_stripe_size,
3906 sys_chunk_offset, alloc_profile);
David Sterba005d6422012-09-18 07:52:32 -06003907 if (ret) {
3908 btrfs_abort_transaction(trans, root, ret);
3909 goto out;
3910 }
Yan Zheng2b820322008-11-17 21:11:30 -05003911
3912 ret = btrfs_add_device(trans, fs_info->chunk_root, device);
David Sterba005d6422012-09-18 07:52:32 -06003913 if (ret) {
3914 btrfs_abort_transaction(trans, root, ret);
3915 goto out;
3916 }
Yan Zheng2b820322008-11-17 21:11:30 -05003917
3918 /*
3919 * Modifying chunk tree needs allocating new blocks from both
3920 * system block group and metadata block group. So we only can
3921 * do operations require modifying the chunk tree after both
3922 * block groups were created.
3923 */
3924 ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
3925 chunk_size, stripe_size);
David Sterba005d6422012-09-18 07:52:32 -06003926 if (ret) {
3927 btrfs_abort_transaction(trans, root, ret);
3928 goto out;
3929 }
Yan Zheng2b820322008-11-17 21:11:30 -05003930
3931 ret = __finish_chunk_alloc(trans, extent_root, sys_map,
3932 sys_chunk_offset, sys_chunk_size,
3933 sys_stripe_size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003934 if (ret)
David Sterba005d6422012-09-18 07:52:32 -06003935 btrfs_abort_transaction(trans, root, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003936
David Sterba005d6422012-09-18 07:52:32 -06003937out:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003938
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003939 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05003940}
3941
3942int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
3943{
3944 struct extent_map *em;
3945 struct map_lookup *map;
3946 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
3947 int readonly = 0;
3948 int i;
3949
Chris Mason890871b2009-09-02 16:24:52 -04003950 read_lock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003951 em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
Chris Mason890871b2009-09-02 16:24:52 -04003952 read_unlock(&map_tree->map_tree.lock);
Yan Zheng2b820322008-11-17 21:11:30 -05003953 if (!em)
3954 return 1;
3955
Josef Bacikf48b9072010-01-27 02:07:59 +00003956 if (btrfs_test_opt(root, DEGRADED)) {
3957 free_extent_map(em);
3958 return 0;
3959 }
3960
Yan Zheng2b820322008-11-17 21:11:30 -05003961 map = (struct map_lookup *)em->bdev;
3962 for (i = 0; i < map->num_stripes; i++) {
3963 if (!map->stripes[i].dev->writeable) {
3964 readonly = 1;
3965 break;
3966 }
3967 }
3968 free_extent_map(em);
3969 return readonly;
Chris Mason0b86a832008-03-24 15:01:56 -04003970}
3971
3972void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
3973{
David Sterbaa8067e02011-04-21 00:34:43 +02003974 extent_map_tree_init(&tree->map_tree);
Chris Mason0b86a832008-03-24 15:01:56 -04003975}
3976
3977void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
3978{
3979 struct extent_map *em;
3980
Chris Masond3977122009-01-05 21:25:51 -05003981 while (1) {
Chris Mason890871b2009-09-02 16:24:52 -04003982 write_lock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003983 em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
3984 if (em)
3985 remove_extent_mapping(&tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04003986 write_unlock(&tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04003987 if (!em)
3988 break;
3989 kfree(em->bdev);
3990 /* once for us */
3991 free_extent_map(em);
3992 /* once for the tree */
3993 free_extent_map(em);
3994 }
3995}
3996
Stefan Behrens5d964052012-11-05 14:59:07 +01003997int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
Chris Masonf1885912008-04-09 16:28:12 -04003998{
Stefan Behrens5d964052012-11-05 14:59:07 +01003999 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Masonf1885912008-04-09 16:28:12 -04004000 struct extent_map *em;
4001 struct map_lookup *map;
4002 struct extent_map_tree *em_tree = &map_tree->map_tree;
4003 int ret;
4004
Chris Mason890871b2009-09-02 16:24:52 -04004005 read_lock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004006 em = lookup_extent_mapping(em_tree, logical, len);
Chris Mason890871b2009-09-02 16:24:52 -04004007 read_unlock(&em_tree->lock);
Chris Masonf1885912008-04-09 16:28:12 -04004008 BUG_ON(!em);
4009
4010 BUG_ON(em->start > logical || em->start + em->len < logical);
4011 map = (struct map_lookup *)em->bdev;
4012 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
4013 ret = map->num_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004014 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4015 ret = map->sub_stripes;
Chris Masonf1885912008-04-09 16:28:12 -04004016 else
4017 ret = 1;
4018 free_extent_map(em);
Stefan Behrensad6d6202012-11-06 15:06:47 +01004019
4020 btrfs_dev_replace_lock(&fs_info->dev_replace);
4021 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace))
4022 ret++;
4023 btrfs_dev_replace_unlock(&fs_info->dev_replace);
4024
Chris Masonf1885912008-04-09 16:28:12 -04004025 return ret;
4026}
4027
Stefan Behrens30d98612012-11-06 14:52:18 +01004028static int find_live_mirror(struct btrfs_fs_info *fs_info,
4029 struct map_lookup *map, int first, int num,
4030 int optimal, int dev_replace_is_ongoing)
Chris Masondfe25022008-05-13 13:46:40 -04004031{
4032 int i;
Stefan Behrens30d98612012-11-06 14:52:18 +01004033 int tolerance;
4034 struct btrfs_device *srcdev;
4035
4036 if (dev_replace_is_ongoing &&
4037 fs_info->dev_replace.cont_reading_from_srcdev_mode ==
4038 BTRFS_DEV_REPLACE_ITEM_CONT_READING_FROM_SRCDEV_MODE_AVOID)
4039 srcdev = fs_info->dev_replace.srcdev;
4040 else
4041 srcdev = NULL;
4042
4043 /*
4044 * try to avoid the drive that is the source drive for a
4045 * dev-replace procedure, only choose it if no other non-missing
4046 * mirror is available
4047 */
4048 for (tolerance = 0; tolerance < 2; tolerance++) {
4049 if (map->stripes[optimal].dev->bdev &&
4050 (tolerance || map->stripes[optimal].dev != srcdev))
4051 return optimal;
4052 for (i = first; i < first + num; i++) {
4053 if (map->stripes[i].dev->bdev &&
4054 (tolerance || map->stripes[i].dev != srcdev))
4055 return i;
4056 }
Chris Masondfe25022008-05-13 13:46:40 -04004057 }
Stefan Behrens30d98612012-11-06 14:52:18 +01004058
Chris Masondfe25022008-05-13 13:46:40 -04004059 /* we couldn't find one that doesn't fail. Just return something
4060 * and the io error handling code will clean up eventually
4061 */
4062 return optimal;
4063}
4064
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004065static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004066 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004067 struct btrfs_bio **bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004068 int mirror_num)
Chris Mason0b86a832008-03-24 15:01:56 -04004069{
4070 struct extent_map *em;
4071 struct map_lookup *map;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004072 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Chris Mason0b86a832008-03-24 15:01:56 -04004073 struct extent_map_tree *em_tree = &map_tree->map_tree;
4074 u64 offset;
Chris Mason593060d2008-03-25 16:50:33 -04004075 u64 stripe_offset;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004076 u64 stripe_end_offset;
Chris Mason593060d2008-03-25 16:50:33 -04004077 u64 stripe_nr;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004078 u64 stripe_nr_orig;
4079 u64 stripe_nr_end;
Chris Mason593060d2008-03-25 16:50:33 -04004080 int stripe_index;
Chris Masoncea9e442008-04-09 16:28:12 -04004081 int i;
Li Zefande11cc12011-12-01 12:55:47 +08004082 int ret = 0;
Chris Masonf2d8d742008-04-21 10:03:05 -04004083 int num_stripes;
Chris Masona236aed2008-04-29 09:38:00 -04004084 int max_errors = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004085 struct btrfs_bio *bbio = NULL;
Stefan Behrens472262f2012-11-06 14:43:46 +01004086 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
4087 int dev_replace_is_ongoing = 0;
4088 int num_alloc_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004089 int patch_the_first_stripe_for_dev_replace = 0;
4090 u64 physical_to_patch_in_first_stripe = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004091
Chris Mason890871b2009-09-02 16:24:52 -04004092 read_lock(&em_tree->lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004093 em = lookup_extent_mapping(em_tree, logical, *length);
Chris Mason890871b2009-09-02 16:24:52 -04004094 read_unlock(&em_tree->lock);
Chris Masonf2d8d742008-04-21 10:03:05 -04004095
Chris Mason3b951512008-04-17 11:29:12 -04004096 if (!em) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004097 printk(KERN_CRIT "btrfs: unable to find logical %llu len %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05004098 (unsigned long long)logical,
4099 (unsigned long long)*length);
Chris Masonf2d8d742008-04-21 10:03:05 -04004100 BUG();
Chris Mason3b951512008-04-17 11:29:12 -04004101 }
Chris Mason0b86a832008-03-24 15:01:56 -04004102
4103 BUG_ON(em->start > logical || em->start + em->len < logical);
4104 map = (struct map_lookup *)em->bdev;
4105 offset = logical - em->start;
Chris Mason593060d2008-03-25 16:50:33 -04004106
4107 stripe_nr = offset;
4108 /*
4109 * stripe_nr counts the total number of stripes we have to stride
4110 * to get to this block
4111 */
4112 do_div(stripe_nr, map->stripe_len);
4113
4114 stripe_offset = stripe_nr * map->stripe_len;
4115 BUG_ON(offset < stripe_offset);
4116
4117 /* stripe_offset is the offset of this block in its stripe*/
4118 stripe_offset = offset - stripe_offset;
4119
Li Dongyangfce3bb92011-03-24 10:24:26 +00004120 if (rw & REQ_DISCARD)
4121 *length = min_t(u64, em->len - offset, *length);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02004122 else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
Chris Masoncea9e442008-04-09 16:28:12 -04004123 /* we limit the length of each bio to what fits in a stripe */
4124 *length = min_t(u64, em->len - offset,
Li Dongyangfce3bb92011-03-24 10:24:26 +00004125 map->stripe_len - stripe_offset);
Chris Masoncea9e442008-04-09 16:28:12 -04004126 } else {
4127 *length = em->len - offset;
4128 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004129
Jan Schmidta1d3c472011-08-04 17:15:33 +02004130 if (!bbio_ret)
Chris Masoncea9e442008-04-09 16:28:12 -04004131 goto out;
4132
Stefan Behrens472262f2012-11-06 14:43:46 +01004133 btrfs_dev_replace_lock(dev_replace);
4134 dev_replace_is_ongoing = btrfs_dev_replace_is_ongoing(dev_replace);
4135 if (!dev_replace_is_ongoing)
4136 btrfs_dev_replace_unlock(dev_replace);
4137
Stefan Behrensad6d6202012-11-06 15:06:47 +01004138 if (dev_replace_is_ongoing && mirror_num == map->num_stripes + 1 &&
4139 !(rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) &&
4140 dev_replace->tgtdev != NULL) {
4141 /*
4142 * in dev-replace case, for repair case (that's the only
4143 * case where the mirror is selected explicitly when
4144 * calling btrfs_map_block), blocks left of the left cursor
4145 * can also be read from the target drive.
4146 * For REQ_GET_READ_MIRRORS, the target drive is added as
4147 * the last one to the array of stripes. For READ, it also
4148 * needs to be supported using the same mirror number.
4149 * If the requested block is not left of the left cursor,
4150 * EIO is returned. This can happen because btrfs_num_copies()
4151 * returns one more in the dev-replace case.
4152 */
4153 u64 tmp_length = *length;
4154 struct btrfs_bio *tmp_bbio = NULL;
4155 int tmp_num_stripes;
4156 u64 srcdev_devid = dev_replace->srcdev->devid;
4157 int index_srcdev = 0;
4158 int found = 0;
4159 u64 physical_of_found = 0;
4160
4161 ret = __btrfs_map_block(fs_info, REQ_GET_READ_MIRRORS,
4162 logical, &tmp_length, &tmp_bbio, 0);
4163 if (ret) {
4164 WARN_ON(tmp_bbio != NULL);
4165 goto out;
4166 }
4167
4168 tmp_num_stripes = tmp_bbio->num_stripes;
4169 if (mirror_num > tmp_num_stripes) {
4170 /*
4171 * REQ_GET_READ_MIRRORS does not contain this
4172 * mirror, that means that the requested area
4173 * is not left of the left cursor
4174 */
4175 ret = -EIO;
4176 kfree(tmp_bbio);
4177 goto out;
4178 }
4179
4180 /*
4181 * process the rest of the function using the mirror_num
4182 * of the source drive. Therefore look it up first.
4183 * At the end, patch the device pointer to the one of the
4184 * target drive.
4185 */
4186 for (i = 0; i < tmp_num_stripes; i++) {
4187 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) {
4188 /*
4189 * In case of DUP, in order to keep it
4190 * simple, only add the mirror with the
4191 * lowest physical address
4192 */
4193 if (found &&
4194 physical_of_found <=
4195 tmp_bbio->stripes[i].physical)
4196 continue;
4197 index_srcdev = i;
4198 found = 1;
4199 physical_of_found =
4200 tmp_bbio->stripes[i].physical;
4201 }
4202 }
4203
4204 if (found) {
4205 mirror_num = index_srcdev + 1;
4206 patch_the_first_stripe_for_dev_replace = 1;
4207 physical_to_patch_in_first_stripe = physical_of_found;
4208 } else {
4209 WARN_ON(1);
4210 ret = -EIO;
4211 kfree(tmp_bbio);
4212 goto out;
4213 }
4214
4215 kfree(tmp_bbio);
4216 } else if (mirror_num > map->num_stripes) {
4217 mirror_num = 0;
4218 }
4219
Chris Masonf2d8d742008-04-21 10:03:05 -04004220 num_stripes = 1;
Chris Masoncea9e442008-04-09 16:28:12 -04004221 stripe_index = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004222 stripe_nr_orig = stripe_nr;
4223 stripe_nr_end = (offset + *length + map->stripe_len - 1) &
4224 (~(map->stripe_len - 1));
4225 do_div(stripe_nr_end, map->stripe_len);
4226 stripe_end_offset = stripe_nr_end * map->stripe_len -
4227 (offset + *length);
4228 if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4229 if (rw & REQ_DISCARD)
4230 num_stripes = min_t(u64, map->num_stripes,
4231 stripe_nr_end - stripe_nr_orig);
4232 stripe_index = do_div(stripe_nr, map->num_stripes);
4233 } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004234 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004235 num_stripes = map->num_stripes;
Chris Mason2fff7342008-04-29 14:12:09 -04004236 else if (mirror_num)
Chris Masonf1885912008-04-09 16:28:12 -04004237 stripe_index = mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004238 else {
Stefan Behrens30d98612012-11-06 14:52:18 +01004239 stripe_index = find_live_mirror(fs_info, map, 0,
Chris Masondfe25022008-05-13 13:46:40 -04004240 map->num_stripes,
Stefan Behrens30d98612012-11-06 14:52:18 +01004241 current->pid % map->num_stripes,
4242 dev_replace_is_ongoing);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004243 mirror_num = stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004244 }
Chris Mason2fff7342008-04-29 14:12:09 -04004245
Chris Mason611f0e02008-04-03 16:29:03 -04004246 } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004247 if (rw & (REQ_WRITE | REQ_DISCARD | REQ_GET_READ_MIRRORS)) {
Chris Masonf2d8d742008-04-21 10:03:05 -04004248 num_stripes = map->num_stripes;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004249 } else if (mirror_num) {
Chris Masonf1885912008-04-09 16:28:12 -04004250 stripe_index = mirror_num - 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004251 } else {
4252 mirror_num = 1;
4253 }
Chris Mason2fff7342008-04-29 14:12:09 -04004254
Chris Mason321aecc2008-04-16 10:49:51 -04004255 } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4256 int factor = map->num_stripes / map->sub_stripes;
Chris Mason321aecc2008-04-16 10:49:51 -04004257
4258 stripe_index = do_div(stripe_nr, factor);
4259 stripe_index *= map->sub_stripes;
4260
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004261 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS))
Chris Masonf2d8d742008-04-21 10:03:05 -04004262 num_stripes = map->sub_stripes;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004263 else if (rw & REQ_DISCARD)
4264 num_stripes = min_t(u64, map->sub_stripes *
4265 (stripe_nr_end - stripe_nr_orig),
4266 map->num_stripes);
Chris Mason321aecc2008-04-16 10:49:51 -04004267 else if (mirror_num)
4268 stripe_index += mirror_num - 1;
Chris Masondfe25022008-05-13 13:46:40 -04004269 else {
Jan Schmidt3e743172012-04-27 12:41:45 -04004270 int old_stripe_index = stripe_index;
Stefan Behrens30d98612012-11-06 14:52:18 +01004271 stripe_index = find_live_mirror(fs_info, map,
4272 stripe_index,
Chris Masondfe25022008-05-13 13:46:40 -04004273 map->sub_stripes, stripe_index +
Stefan Behrens30d98612012-11-06 14:52:18 +01004274 current->pid % map->sub_stripes,
4275 dev_replace_is_ongoing);
Jan Schmidt3e743172012-04-27 12:41:45 -04004276 mirror_num = stripe_index - old_stripe_index + 1;
Chris Masondfe25022008-05-13 13:46:40 -04004277 }
Chris Mason8790d502008-04-03 16:29:03 -04004278 } else {
4279 /*
4280 * after this do_div call, stripe_nr is the number of stripes
4281 * on this device we have to walk to find the data, and
4282 * stripe_index is the number of our device in the stripe array
4283 */
4284 stripe_index = do_div(stripe_nr, map->num_stripes);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004285 mirror_num = stripe_index + 1;
Chris Mason8790d502008-04-03 16:29:03 -04004286 }
Chris Mason593060d2008-03-25 16:50:33 -04004287 BUG_ON(stripe_index >= map->num_stripes);
Chris Mason593060d2008-03-25 16:50:33 -04004288
Stefan Behrens472262f2012-11-06 14:43:46 +01004289 num_alloc_stripes = num_stripes;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004290 if (dev_replace_is_ongoing) {
4291 if (rw & (REQ_WRITE | REQ_DISCARD))
4292 num_alloc_stripes <<= 1;
4293 if (rw & REQ_GET_READ_MIRRORS)
4294 num_alloc_stripes++;
4295 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004296 bbio = kzalloc(btrfs_bio_size(num_alloc_stripes), GFP_NOFS);
Li Zefande11cc12011-12-01 12:55:47 +08004297 if (!bbio) {
4298 ret = -ENOMEM;
4299 goto out;
4300 }
4301 atomic_set(&bbio->error, 0);
4302
Li Dongyangfce3bb92011-03-24 10:24:26 +00004303 if (rw & REQ_DISCARD) {
Li Zefanec9ef7a2011-12-01 14:06:42 +08004304 int factor = 0;
4305 int sub_stripes = 0;
4306 u64 stripes_per_dev = 0;
4307 u32 remaining_stripes = 0;
Liu Bob89203f2012-04-12 16:03:56 -04004308 u32 last_stripe = 0;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004309
4310 if (map->type &
4311 (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
4312 if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4313 sub_stripes = 1;
4314 else
4315 sub_stripes = map->sub_stripes;
4316
4317 factor = map->num_stripes / sub_stripes;
4318 stripes_per_dev = div_u64_rem(stripe_nr_end -
4319 stripe_nr_orig,
4320 factor,
4321 &remaining_stripes);
Liu Bob89203f2012-04-12 16:03:56 -04004322 div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
4323 last_stripe *= sub_stripes;
Li Zefanec9ef7a2011-12-01 14:06:42 +08004324 }
4325
Li Dongyangfce3bb92011-03-24 10:24:26 +00004326 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004327 bbio->stripes[i].physical =
Chris Masonf2d8d742008-04-21 10:03:05 -04004328 map->stripes[stripe_index].physical +
4329 stripe_offset + stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004330 bbio->stripes[i].dev = map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004331
Li Zefanec9ef7a2011-12-01 14:06:42 +08004332 if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
4333 BTRFS_BLOCK_GROUP_RAID10)) {
4334 bbio->stripes[i].length = stripes_per_dev *
4335 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004336
Li Zefanec9ef7a2011-12-01 14:06:42 +08004337 if (i / sub_stripes < remaining_stripes)
4338 bbio->stripes[i].length +=
4339 map->stripe_len;
Liu Bob89203f2012-04-12 16:03:56 -04004340
4341 /*
4342 * Special for the first stripe and
4343 * the last stripe:
4344 *
4345 * |-------|...|-------|
4346 * |----------|
4347 * off end_off
4348 */
Li Zefanec9ef7a2011-12-01 14:06:42 +08004349 if (i < sub_stripes)
Jan Schmidta1d3c472011-08-04 17:15:33 +02004350 bbio->stripes[i].length -=
Li Dongyangfce3bb92011-03-24 10:24:26 +00004351 stripe_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004352
4353 if (stripe_index >= last_stripe &&
4354 stripe_index <= (last_stripe +
4355 sub_stripes - 1))
Li Zefanec9ef7a2011-12-01 14:06:42 +08004356 bbio->stripes[i].length -=
4357 stripe_end_offset;
Liu Bob89203f2012-04-12 16:03:56 -04004358
Li Zefanec9ef7a2011-12-01 14:06:42 +08004359 if (i == sub_stripes - 1)
Li Dongyangfce3bb92011-03-24 10:24:26 +00004360 stripe_offset = 0;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004361 } else
Jan Schmidta1d3c472011-08-04 17:15:33 +02004362 bbio->stripes[i].length = *length;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004363
4364 stripe_index++;
4365 if (stripe_index == map->num_stripes) {
4366 /* This could only happen for RAID0/10 */
4367 stripe_index = 0;
4368 stripe_nr++;
4369 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004370 }
Li Dongyangfce3bb92011-03-24 10:24:26 +00004371 } else {
4372 for (i = 0; i < num_stripes; i++) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004373 bbio->stripes[i].physical =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004374 map->stripes[stripe_index].physical +
4375 stripe_offset +
4376 stripe_nr * map->stripe_len;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004377 bbio->stripes[i].dev =
Linus Torvalds212a17a2011-03-28 15:31:05 -07004378 map->stripes[stripe_index].dev;
Li Dongyangfce3bb92011-03-24 10:24:26 +00004379 stripe_index++;
4380 }
Chris Mason593060d2008-03-25 16:50:33 -04004381 }
Li Zefande11cc12011-12-01 12:55:47 +08004382
Stefan Behrens29a8d9a2012-11-06 14:16:24 +01004383 if (rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) {
Li Zefande11cc12011-12-01 12:55:47 +08004384 if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
4385 BTRFS_BLOCK_GROUP_RAID10 |
4386 BTRFS_BLOCK_GROUP_DUP)) {
4387 max_errors = 1;
4388 }
Chris Masonf2d8d742008-04-21 10:03:05 -04004389 }
Li Zefande11cc12011-12-01 12:55:47 +08004390
Stefan Behrens472262f2012-11-06 14:43:46 +01004391 if (dev_replace_is_ongoing && (rw & (REQ_WRITE | REQ_DISCARD)) &&
4392 dev_replace->tgtdev != NULL) {
4393 int index_where_to_add;
4394 u64 srcdev_devid = dev_replace->srcdev->devid;
4395
4396 /*
4397 * duplicate the write operations while the dev replace
4398 * procedure is running. Since the copying of the old disk
4399 * to the new disk takes place at run time while the
4400 * filesystem is mounted writable, the regular write
4401 * operations to the old disk have to be duplicated to go
4402 * to the new disk as well.
4403 * Note that device->missing is handled by the caller, and
4404 * that the write to the old disk is already set up in the
4405 * stripes array.
4406 */
4407 index_where_to_add = num_stripes;
4408 for (i = 0; i < num_stripes; i++) {
4409 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4410 /* write to new disk, too */
4411 struct btrfs_bio_stripe *new =
4412 bbio->stripes + index_where_to_add;
4413 struct btrfs_bio_stripe *old =
4414 bbio->stripes + i;
4415
4416 new->physical = old->physical;
4417 new->length = old->length;
4418 new->dev = dev_replace->tgtdev;
4419 index_where_to_add++;
4420 max_errors++;
4421 }
4422 }
4423 num_stripes = index_where_to_add;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004424 } else if (dev_replace_is_ongoing && (rw & REQ_GET_READ_MIRRORS) &&
4425 dev_replace->tgtdev != NULL) {
4426 u64 srcdev_devid = dev_replace->srcdev->devid;
4427 int index_srcdev = 0;
4428 int found = 0;
4429 u64 physical_of_found = 0;
4430
4431 /*
4432 * During the dev-replace procedure, the target drive can
4433 * also be used to read data in case it is needed to repair
4434 * a corrupt block elsewhere. This is possible if the
4435 * requested area is left of the left cursor. In this area,
4436 * the target drive is a full copy of the source drive.
4437 */
4438 for (i = 0; i < num_stripes; i++) {
4439 if (bbio->stripes[i].dev->devid == srcdev_devid) {
4440 /*
4441 * In case of DUP, in order to keep it
4442 * simple, only add the mirror with the
4443 * lowest physical address
4444 */
4445 if (found &&
4446 physical_of_found <=
4447 bbio->stripes[i].physical)
4448 continue;
4449 index_srcdev = i;
4450 found = 1;
4451 physical_of_found = bbio->stripes[i].physical;
4452 }
4453 }
4454 if (found) {
4455 u64 length = map->stripe_len;
4456
4457 if (physical_of_found + length <=
4458 dev_replace->cursor_left) {
4459 struct btrfs_bio_stripe *tgtdev_stripe =
4460 bbio->stripes + num_stripes;
4461
4462 tgtdev_stripe->physical = physical_of_found;
4463 tgtdev_stripe->length =
4464 bbio->stripes[index_srcdev].length;
4465 tgtdev_stripe->dev = dev_replace->tgtdev;
4466
4467 num_stripes++;
4468 }
4469 }
Stefan Behrens472262f2012-11-06 14:43:46 +01004470 }
4471
Li Zefande11cc12011-12-01 12:55:47 +08004472 *bbio_ret = bbio;
4473 bbio->num_stripes = num_stripes;
4474 bbio->max_errors = max_errors;
4475 bbio->mirror_num = mirror_num;
Stefan Behrensad6d6202012-11-06 15:06:47 +01004476
4477 /*
4478 * this is the case that REQ_READ && dev_replace_is_ongoing &&
4479 * mirror_num == num_stripes + 1 && dev_replace target drive is
4480 * available as a mirror
4481 */
4482 if (patch_the_first_stripe_for_dev_replace && num_stripes > 0) {
4483 WARN_ON(num_stripes > 1);
4484 bbio->stripes[0].dev = dev_replace->tgtdev;
4485 bbio->stripes[0].physical = physical_to_patch_in_first_stripe;
4486 bbio->mirror_num = map->num_stripes + 1;
4487 }
Chris Masoncea9e442008-04-09 16:28:12 -04004488out:
Stefan Behrens472262f2012-11-06 14:43:46 +01004489 if (dev_replace_is_ongoing)
4490 btrfs_dev_replace_unlock(dev_replace);
Chris Mason0b86a832008-03-24 15:01:56 -04004491 free_extent_map(em);
Li Zefande11cc12011-12-01 12:55:47 +08004492 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04004493}
4494
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004495int btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
Chris Masonf2d8d742008-04-21 10:03:05 -04004496 u64 logical, u64 *length,
Jan Schmidta1d3c472011-08-04 17:15:33 +02004497 struct btrfs_bio **bbio_ret, int mirror_num)
Chris Masonf2d8d742008-04-21 10:03:05 -04004498{
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004499 return __btrfs_map_block(fs_info, rw, logical, length, bbio_ret,
Jens Axboe7eaceac2011-03-10 08:52:07 +01004500 mirror_num);
Chris Masonf2d8d742008-04-21 10:03:05 -04004501}
4502
Yan Zhenga512bbf2008-12-08 16:46:26 -05004503int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
4504 u64 chunk_start, u64 physical, u64 devid,
4505 u64 **logical, int *naddrs, int *stripe_len)
4506{
4507 struct extent_map_tree *em_tree = &map_tree->map_tree;
4508 struct extent_map *em;
4509 struct map_lookup *map;
4510 u64 *buf;
4511 u64 bytenr;
4512 u64 length;
4513 u64 stripe_nr;
4514 int i, j, nr = 0;
4515
Chris Mason890871b2009-09-02 16:24:52 -04004516 read_lock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004517 em = lookup_extent_mapping(em_tree, chunk_start, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004518 read_unlock(&em_tree->lock);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004519
4520 BUG_ON(!em || em->start != chunk_start);
4521 map = (struct map_lookup *)em->bdev;
4522
4523 length = em->len;
4524 if (map->type & BTRFS_BLOCK_GROUP_RAID10)
4525 do_div(length, map->num_stripes / map->sub_stripes);
4526 else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
4527 do_div(length, map->num_stripes);
4528
4529 buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004530 BUG_ON(!buf); /* -ENOMEM */
Yan Zhenga512bbf2008-12-08 16:46:26 -05004531
4532 for (i = 0; i < map->num_stripes; i++) {
4533 if (devid && map->stripes[i].dev->devid != devid)
4534 continue;
4535 if (map->stripes[i].physical > physical ||
4536 map->stripes[i].physical + length <= physical)
4537 continue;
4538
4539 stripe_nr = physical - map->stripes[i].physical;
4540 do_div(stripe_nr, map->stripe_len);
4541
4542 if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
4543 stripe_nr = stripe_nr * map->num_stripes + i;
4544 do_div(stripe_nr, map->sub_stripes);
4545 } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
4546 stripe_nr = stripe_nr * map->num_stripes + i;
4547 }
4548 bytenr = chunk_start + stripe_nr * map->stripe_len;
Chris Mason934d3752008-12-08 16:43:10 -05004549 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004550 for (j = 0; j < nr; j++) {
4551 if (buf[j] == bytenr)
4552 break;
4553 }
Chris Mason934d3752008-12-08 16:43:10 -05004554 if (j == nr) {
4555 WARN_ON(nr >= map->num_stripes);
Yan Zhenga512bbf2008-12-08 16:46:26 -05004556 buf[nr++] = bytenr;
Chris Mason934d3752008-12-08 16:43:10 -05004557 }
Yan Zhenga512bbf2008-12-08 16:46:26 -05004558 }
4559
Yan Zhenga512bbf2008-12-08 16:46:26 -05004560 *logical = buf;
4561 *naddrs = nr;
4562 *stripe_len = map->stripe_len;
4563
4564 free_extent_map(em);
4565 return 0;
4566}
4567
Stefan Behrens442a4f62012-05-25 16:06:08 +02004568static void *merge_stripe_index_into_bio_private(void *bi_private,
4569 unsigned int stripe_index)
4570{
4571 /*
4572 * with single, dup, RAID0, RAID1 and RAID10, stripe_index is
4573 * at most 1.
4574 * The alternative solution (instead of stealing bits from the
4575 * pointer) would be to allocate an intermediate structure
4576 * that contains the old private pointer plus the stripe_index.
4577 */
4578 BUG_ON((((uintptr_t)bi_private) & 3) != 0);
4579 BUG_ON(stripe_index > 3);
4580 return (void *)(((uintptr_t)bi_private) | stripe_index);
4581}
4582
4583static struct btrfs_bio *extract_bbio_from_bio_private(void *bi_private)
4584{
4585 return (struct btrfs_bio *)(((uintptr_t)bi_private) & ~((uintptr_t)3));
4586}
4587
4588static unsigned int extract_stripe_index_from_bio_private(void *bi_private)
4589{
4590 return (unsigned int)((uintptr_t)bi_private) & 3;
4591}
4592
Jan Schmidta1d3c472011-08-04 17:15:33 +02004593static void btrfs_end_bio(struct bio *bio, int err)
Chris Mason8790d502008-04-03 16:29:03 -04004594{
Stefan Behrens442a4f62012-05-25 16:06:08 +02004595 struct btrfs_bio *bbio = extract_bbio_from_bio_private(bio->bi_private);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004596 int is_orig_bio = 0;
Chris Mason8790d502008-04-03 16:29:03 -04004597
Stefan Behrens442a4f62012-05-25 16:06:08 +02004598 if (err) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02004599 atomic_inc(&bbio->error);
Stefan Behrens442a4f62012-05-25 16:06:08 +02004600 if (err == -EIO || err == -EREMOTEIO) {
4601 unsigned int stripe_index =
4602 extract_stripe_index_from_bio_private(
4603 bio->bi_private);
4604 struct btrfs_device *dev;
4605
4606 BUG_ON(stripe_index >= bbio->num_stripes);
4607 dev = bbio->stripes[stripe_index].dev;
Stefan Behrens597a60f2012-06-14 16:42:31 +02004608 if (dev->bdev) {
4609 if (bio->bi_rw & WRITE)
4610 btrfs_dev_stat_inc(dev,
4611 BTRFS_DEV_STAT_WRITE_ERRS);
4612 else
4613 btrfs_dev_stat_inc(dev,
4614 BTRFS_DEV_STAT_READ_ERRS);
4615 if ((bio->bi_rw & WRITE_FLUSH) == WRITE_FLUSH)
4616 btrfs_dev_stat_inc(dev,
4617 BTRFS_DEV_STAT_FLUSH_ERRS);
4618 btrfs_dev_stat_print_on_error(dev);
4619 }
Stefan Behrens442a4f62012-05-25 16:06:08 +02004620 }
4621 }
Chris Mason8790d502008-04-03 16:29:03 -04004622
Jan Schmidta1d3c472011-08-04 17:15:33 +02004623 if (bio == bbio->orig_bio)
Chris Mason7d2b4da2008-08-05 10:13:57 -04004624 is_orig_bio = 1;
4625
Jan Schmidta1d3c472011-08-04 17:15:33 +02004626 if (atomic_dec_and_test(&bbio->stripes_pending)) {
Chris Mason7d2b4da2008-08-05 10:13:57 -04004627 if (!is_orig_bio) {
4628 bio_put(bio);
Jan Schmidta1d3c472011-08-04 17:15:33 +02004629 bio = bbio->orig_bio;
Chris Mason7d2b4da2008-08-05 10:13:57 -04004630 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004631 bio->bi_private = bbio->private;
4632 bio->bi_end_io = bbio->end_io;
Jan Schmidt2774b2c2011-06-16 12:05:19 +02004633 bio->bi_bdev = (struct block_device *)
4634 (unsigned long)bbio->mirror_num;
Chris Masona236aed2008-04-29 09:38:00 -04004635 /* only send an error to the higher layers if it is
4636 * beyond the tolerance of the multi-bio
4637 */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004638 if (atomic_read(&bbio->error) > bbio->max_errors) {
Chris Masona236aed2008-04-29 09:38:00 -04004639 err = -EIO;
Chris Mason5dbc8fc2011-12-09 11:07:37 -05004640 } else {
Chris Mason1259ab72008-05-12 13:39:03 -04004641 /*
4642 * this bio is actually up to date, we didn't
4643 * go over the max number of errors
4644 */
4645 set_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masona236aed2008-04-29 09:38:00 -04004646 err = 0;
Chris Mason1259ab72008-05-12 13:39:03 -04004647 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004648 kfree(bbio);
Chris Mason8790d502008-04-03 16:29:03 -04004649
4650 bio_endio(bio, err);
Chris Mason7d2b4da2008-08-05 10:13:57 -04004651 } else if (!is_orig_bio) {
Chris Mason8790d502008-04-03 16:29:03 -04004652 bio_put(bio);
4653 }
Chris Mason8790d502008-04-03 16:29:03 -04004654}
4655
Chris Mason8b712842008-06-11 16:50:36 -04004656struct async_sched {
4657 struct bio *bio;
4658 int rw;
4659 struct btrfs_fs_info *info;
4660 struct btrfs_work work;
4661};
4662
4663/*
4664 * see run_scheduled_bios for a description of why bios are collected for
4665 * async submit.
4666 *
4667 * This will add one bio to the pending list for a device and make sure
4668 * the work struct is scheduled.
4669 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004670static noinline void schedule_bio(struct btrfs_root *root,
Chris Masona1b32a52008-09-05 16:09:51 -04004671 struct btrfs_device *device,
4672 int rw, struct bio *bio)
Chris Mason8b712842008-06-11 16:50:36 -04004673{
4674 int should_queue = 1;
Chris Masonffbd5172009-04-20 15:50:09 -04004675 struct btrfs_pending_bios *pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004676
4677 /* don't bother with additional async steps for reads, right now */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004678 if (!(rw & REQ_WRITE)) {
Chris Mason492bb6de2008-07-31 16:29:02 -04004679 bio_get(bio);
Stefan Behrens21adbd52011-11-09 13:44:05 +01004680 btrfsic_submit_bio(rw, bio);
Chris Mason492bb6de2008-07-31 16:29:02 -04004681 bio_put(bio);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004682 return;
Chris Mason8b712842008-06-11 16:50:36 -04004683 }
4684
4685 /*
Chris Mason0986fe9e2008-08-15 15:34:15 -04004686 * nr_async_bios allows us to reliably return congestion to the
Chris Mason8b712842008-06-11 16:50:36 -04004687 * higher layers. Otherwise, the async bio makes it appear we have
4688 * made progress against dirty pages when we've really just put it
4689 * on a queue for later
4690 */
Chris Mason0986fe9e2008-08-15 15:34:15 -04004691 atomic_inc(&root->fs_info->nr_async_bios);
Chris Mason492bb6de2008-07-31 16:29:02 -04004692 WARN_ON(bio->bi_next);
Chris Mason8b712842008-06-11 16:50:36 -04004693 bio->bi_next = NULL;
4694 bio->bi_rw |= rw;
4695
4696 spin_lock(&device->io_lock);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02004697 if (bio->bi_rw & REQ_SYNC)
Chris Masonffbd5172009-04-20 15:50:09 -04004698 pending_bios = &device->pending_sync_bios;
4699 else
4700 pending_bios = &device->pending_bios;
Chris Mason8b712842008-06-11 16:50:36 -04004701
Chris Masonffbd5172009-04-20 15:50:09 -04004702 if (pending_bios->tail)
4703 pending_bios->tail->bi_next = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004704
Chris Masonffbd5172009-04-20 15:50:09 -04004705 pending_bios->tail = bio;
4706 if (!pending_bios->head)
4707 pending_bios->head = bio;
Chris Mason8b712842008-06-11 16:50:36 -04004708 if (device->running_pending)
4709 should_queue = 0;
4710
4711 spin_unlock(&device->io_lock);
4712
4713 if (should_queue)
Chris Mason1cc127b2008-06-12 14:46:17 -04004714 btrfs_queue_worker(&root->fs_info->submit_workers,
4715 &device->work);
Chris Mason8b712842008-06-11 16:50:36 -04004716}
4717
Josef Bacikde1ee922012-10-19 16:50:56 -04004718static int bio_size_ok(struct block_device *bdev, struct bio *bio,
4719 sector_t sector)
4720{
4721 struct bio_vec *prev;
4722 struct request_queue *q = bdev_get_queue(bdev);
4723 unsigned short max_sectors = queue_max_sectors(q);
4724 struct bvec_merge_data bvm = {
4725 .bi_bdev = bdev,
4726 .bi_sector = sector,
4727 .bi_rw = bio->bi_rw,
4728 };
4729
4730 if (bio->bi_vcnt == 0) {
4731 WARN_ON(1);
4732 return 1;
4733 }
4734
4735 prev = &bio->bi_io_vec[bio->bi_vcnt - 1];
4736 if ((bio->bi_size >> 9) > max_sectors)
4737 return 0;
4738
4739 if (!q->merge_bvec_fn)
4740 return 1;
4741
4742 bvm.bi_size = bio->bi_size - prev->bv_len;
4743 if (q->merge_bvec_fn(q, &bvm, prev) < prev->bv_len)
4744 return 0;
4745 return 1;
4746}
4747
4748static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4749 struct bio *bio, u64 physical, int dev_nr,
4750 int rw, int async)
4751{
4752 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
4753
4754 bio->bi_private = bbio;
4755 bio->bi_private = merge_stripe_index_into_bio_private(
4756 bio->bi_private, (unsigned int)dev_nr);
4757 bio->bi_end_io = btrfs_end_bio;
4758 bio->bi_sector = physical >> 9;
4759#ifdef DEBUG
4760 {
4761 struct rcu_string *name;
4762
4763 rcu_read_lock();
4764 name = rcu_dereference(dev->name);
Masanari Iidad1423242012-10-31 15:16:32 +00004765 pr_debug("btrfs_map_bio: rw %d, sector=%llu, dev=%lu "
Josef Bacikde1ee922012-10-19 16:50:56 -04004766 "(%s id %llu), size=%u\n", rw,
4767 (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
4768 name->str, dev->devid, bio->bi_size);
4769 rcu_read_unlock();
4770 }
4771#endif
4772 bio->bi_bdev = dev->bdev;
4773 if (async)
4774 schedule_bio(root, dev, rw, bio);
4775 else
4776 btrfsic_submit_bio(rw, bio);
4777}
4778
4779static int breakup_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
4780 struct bio *first_bio, struct btrfs_device *dev,
4781 int dev_nr, int rw, int async)
4782{
4783 struct bio_vec *bvec = first_bio->bi_io_vec;
4784 struct bio *bio;
4785 int nr_vecs = bio_get_nr_vecs(dev->bdev);
4786 u64 physical = bbio->stripes[dev_nr].physical;
4787
4788again:
4789 bio = btrfs_bio_alloc(dev->bdev, physical >> 9, nr_vecs, GFP_NOFS);
4790 if (!bio)
4791 return -ENOMEM;
4792
4793 while (bvec <= (first_bio->bi_io_vec + first_bio->bi_vcnt - 1)) {
4794 if (bio_add_page(bio, bvec->bv_page, bvec->bv_len,
4795 bvec->bv_offset) < bvec->bv_len) {
4796 u64 len = bio->bi_size;
4797
4798 atomic_inc(&bbio->stripes_pending);
4799 submit_stripe_bio(root, bbio, bio, physical, dev_nr,
4800 rw, async);
4801 physical += len;
4802 goto again;
4803 }
4804 bvec++;
4805 }
4806
4807 submit_stripe_bio(root, bbio, bio, physical, dev_nr, rw, async);
4808 return 0;
4809}
4810
4811static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
4812{
4813 atomic_inc(&bbio->error);
4814 if (atomic_dec_and_test(&bbio->stripes_pending)) {
4815 bio->bi_private = bbio->private;
4816 bio->bi_end_io = bbio->end_io;
4817 bio->bi_bdev = (struct block_device *)
4818 (unsigned long)bbio->mirror_num;
4819 bio->bi_sector = logical >> 9;
4820 kfree(bbio);
4821 bio_endio(bio, -EIO);
4822 }
4823}
4824
Chris Masonf1885912008-04-09 16:28:12 -04004825int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
Chris Mason8b712842008-06-11 16:50:36 -04004826 int mirror_num, int async_submit)
Chris Mason0b86a832008-03-24 15:01:56 -04004827{
Chris Mason0b86a832008-03-24 15:01:56 -04004828 struct btrfs_device *dev;
Chris Mason8790d502008-04-03 16:29:03 -04004829 struct bio *first_bio = bio;
Chris Masona62b9402008-10-03 16:31:08 -04004830 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason0b86a832008-03-24 15:01:56 -04004831 u64 length = 0;
4832 u64 map_length;
Chris Mason0b86a832008-03-24 15:01:56 -04004833 int ret;
Chris Mason8790d502008-04-03 16:29:03 -04004834 int dev_nr = 0;
4835 int total_devs = 1;
Jan Schmidta1d3c472011-08-04 17:15:33 +02004836 struct btrfs_bio *bbio = NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004837
Chris Masonf2d8d742008-04-21 10:03:05 -04004838 length = bio->bi_size;
Chris Mason0b86a832008-03-24 15:01:56 -04004839 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -04004840
Stefan Behrens3ec706c2012-11-05 15:46:42 +01004841 ret = btrfs_map_block(root->fs_info, rw, logical, &map_length, &bbio,
Chris Masonf1885912008-04-09 16:28:12 -04004842 mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01004843 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004844 return ret;
Chris Masoncea9e442008-04-09 16:28:12 -04004845
Jan Schmidta1d3c472011-08-04 17:15:33 +02004846 total_devs = bbio->num_stripes;
Chris Masoncea9e442008-04-09 16:28:12 -04004847 if (map_length < length) {
Daniel J Blueman48940662012-05-07 06:35:38 -06004848 printk(KERN_CRIT "btrfs: mapping failed logical %llu bio len %llu "
Chris Masond3977122009-01-05 21:25:51 -05004849 "len %llu\n", (unsigned long long)logical,
4850 (unsigned long long)length,
4851 (unsigned long long)map_length);
Chris Masoncea9e442008-04-09 16:28:12 -04004852 BUG();
4853 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02004854
4855 bbio->orig_bio = first_bio;
4856 bbio->private = first_bio->bi_private;
4857 bbio->end_io = first_bio->bi_end_io;
4858 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
Chris Masoncea9e442008-04-09 16:28:12 -04004859
Chris Masond3977122009-01-05 21:25:51 -05004860 while (dev_nr < total_devs) {
Josef Bacikde1ee922012-10-19 16:50:56 -04004861 dev = bbio->stripes[dev_nr].dev;
4862 if (!dev || !dev->bdev || (rw & WRITE && !dev->writeable)) {
4863 bbio_error(bbio, first_bio, logical);
4864 dev_nr++;
4865 continue;
4866 }
4867
4868 /*
4869 * Check and see if we're ok with this bio based on it's size
4870 * and offset with the given device.
4871 */
4872 if (!bio_size_ok(dev->bdev, first_bio,
4873 bbio->stripes[dev_nr].physical >> 9)) {
4874 ret = breakup_stripe_bio(root, bbio, first_bio, dev,
4875 dev_nr, rw, async_submit);
4876 BUG_ON(ret);
4877 dev_nr++;
4878 continue;
4879 }
4880
Jan Schmidta1d3c472011-08-04 17:15:33 +02004881 if (dev_nr < total_devs - 1) {
4882 bio = bio_clone(first_bio, GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004883 BUG_ON(!bio); /* -ENOMEM */
Jan Schmidta1d3c472011-08-04 17:15:33 +02004884 } else {
4885 bio = first_bio;
Chris Mason8790d502008-04-03 16:29:03 -04004886 }
Josef Bacik606686e2012-06-04 14:03:51 -04004887
Josef Bacikde1ee922012-10-19 16:50:56 -04004888 submit_stripe_bio(root, bbio, bio,
4889 bbio->stripes[dev_nr].physical, dev_nr, rw,
4890 async_submit);
Chris Mason8790d502008-04-03 16:29:03 -04004891 dev_nr++;
Chris Mason239b14b2008-03-24 15:02:07 -04004892 }
Chris Mason0b86a832008-03-24 15:01:56 -04004893 return 0;
4894}
4895
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004896struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
Yan Zheng2b820322008-11-17 21:11:30 -05004897 u8 *uuid, u8 *fsid)
Chris Mason0b86a832008-03-24 15:01:56 -04004898{
Yan Zheng2b820322008-11-17 21:11:30 -05004899 struct btrfs_device *device;
4900 struct btrfs_fs_devices *cur_devices;
Chris Mason0b86a832008-03-24 15:01:56 -04004901
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01004902 cur_devices = fs_info->fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05004903 while (cur_devices) {
4904 if (!fsid ||
4905 !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
4906 device = __find_device(&cur_devices->devices,
4907 devid, uuid);
4908 if (device)
4909 return device;
4910 }
4911 cur_devices = cur_devices->seed;
4912 }
4913 return NULL;
Chris Mason0b86a832008-03-24 15:01:56 -04004914}
4915
Chris Masondfe25022008-05-13 13:46:40 -04004916static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
4917 u64 devid, u8 *dev_uuid)
4918{
4919 struct btrfs_device *device;
4920 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
4921
4922 device = kzalloc(sizeof(*device), GFP_NOFS);
yanhai zhu7cbd8a82008-11-12 14:38:54 -05004923 if (!device)
4924 return NULL;
Chris Masondfe25022008-05-13 13:46:40 -04004925 list_add(&device->dev_list,
4926 &fs_devices->devices);
Chris Masondfe25022008-05-13 13:46:40 -04004927 device->dev_root = root->fs_info->dev_root;
4928 device->devid = devid;
Chris Mason8b712842008-06-11 16:50:36 -04004929 device->work.func = pending_bios_fn;
Yan Zhenge4404d62008-12-12 10:03:26 -05004930 device->fs_devices = fs_devices;
Chris Masoncd02dca2010-12-13 14:56:23 -05004931 device->missing = 1;
Chris Masondfe25022008-05-13 13:46:40 -04004932 fs_devices->num_devices++;
Chris Masoncd02dca2010-12-13 14:56:23 -05004933 fs_devices->missing_devices++;
Chris Masondfe25022008-05-13 13:46:40 -04004934 spin_lock_init(&device->io_lock);
Chris Masond20f7042008-12-08 16:58:54 -05004935 INIT_LIST_HEAD(&device->dev_alloc_list);
Chris Masondfe25022008-05-13 13:46:40 -04004936 memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
4937 return device;
4938}
4939
Chris Mason0b86a832008-03-24 15:01:56 -04004940static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
4941 struct extent_buffer *leaf,
4942 struct btrfs_chunk *chunk)
4943{
4944 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
4945 struct map_lookup *map;
4946 struct extent_map *em;
4947 u64 logical;
4948 u64 length;
4949 u64 devid;
Chris Masona4437552008-04-18 10:29:38 -04004950 u8 uuid[BTRFS_UUID_SIZE];
Chris Mason593060d2008-03-25 16:50:33 -04004951 int num_stripes;
Chris Mason0b86a832008-03-24 15:01:56 -04004952 int ret;
Chris Mason593060d2008-03-25 16:50:33 -04004953 int i;
Chris Mason0b86a832008-03-24 15:01:56 -04004954
Chris Masone17cade2008-04-15 15:41:47 -04004955 logical = key->offset;
4956 length = btrfs_chunk_length(leaf, chunk);
Chris Masona061fc82008-05-07 11:43:44 -04004957
Chris Mason890871b2009-09-02 16:24:52 -04004958 read_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004959 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
Chris Mason890871b2009-09-02 16:24:52 -04004960 read_unlock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04004961
4962 /* already mapped? */
4963 if (em && em->start <= logical && em->start + em->len > logical) {
4964 free_extent_map(em);
Chris Mason0b86a832008-03-24 15:01:56 -04004965 return 0;
4966 } else if (em) {
4967 free_extent_map(em);
4968 }
Chris Mason0b86a832008-03-24 15:01:56 -04004969
David Sterba172ddd62011-04-21 00:48:27 +02004970 em = alloc_extent_map();
Chris Mason0b86a832008-03-24 15:01:56 -04004971 if (!em)
4972 return -ENOMEM;
Chris Mason593060d2008-03-25 16:50:33 -04004973 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
4974 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
Chris Mason0b86a832008-03-24 15:01:56 -04004975 if (!map) {
4976 free_extent_map(em);
4977 return -ENOMEM;
4978 }
4979
4980 em->bdev = (struct block_device *)map;
4981 em->start = logical;
4982 em->len = length;
Josef Bacik70c8a912012-10-11 16:54:30 -04004983 em->orig_start = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04004984 em->block_start = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04004985 em->block_len = em->len;
Chris Mason0b86a832008-03-24 15:01:56 -04004986
Chris Mason593060d2008-03-25 16:50:33 -04004987 map->num_stripes = num_stripes;
4988 map->io_width = btrfs_chunk_io_width(leaf, chunk);
4989 map->io_align = btrfs_chunk_io_align(leaf, chunk);
4990 map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
4991 map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
4992 map->type = btrfs_chunk_type(leaf, chunk);
Chris Mason321aecc2008-04-16 10:49:51 -04004993 map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
Chris Mason593060d2008-03-25 16:50:33 -04004994 for (i = 0; i < num_stripes; i++) {
4995 map->stripes[i].physical =
4996 btrfs_stripe_offset_nr(leaf, chunk, i);
4997 devid = btrfs_stripe_devid_nr(leaf, chunk, i);
Chris Masona4437552008-04-18 10:29:38 -04004998 read_extent_buffer(leaf, uuid, (unsigned long)
4999 btrfs_stripe_dev_uuid_nr(chunk, i),
5000 BTRFS_UUID_SIZE);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005001 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5002 uuid, NULL);
Chris Masondfe25022008-05-13 13:46:40 -04005003 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
Chris Mason593060d2008-03-25 16:50:33 -04005004 kfree(map);
5005 free_extent_map(em);
5006 return -EIO;
5007 }
Chris Masondfe25022008-05-13 13:46:40 -04005008 if (!map->stripes[i].dev) {
5009 map->stripes[i].dev =
5010 add_missing_dev(root, devid, uuid);
5011 if (!map->stripes[i].dev) {
5012 kfree(map);
5013 free_extent_map(em);
5014 return -EIO;
5015 }
5016 }
5017 map->stripes[i].dev->in_fs_metadata = 1;
Chris Mason0b86a832008-03-24 15:01:56 -04005018 }
5019
Chris Mason890871b2009-09-02 16:24:52 -04005020 write_lock(&map_tree->map_tree.lock);
Chris Mason0b86a832008-03-24 15:01:56 -04005021 ret = add_extent_mapping(&map_tree->map_tree, em);
Chris Mason890871b2009-09-02 16:24:52 -04005022 write_unlock(&map_tree->map_tree.lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005023 BUG_ON(ret); /* Tree corruption */
Chris Mason0b86a832008-03-24 15:01:56 -04005024 free_extent_map(em);
5025
5026 return 0;
5027}
5028
Jeff Mahoney143bede2012-03-01 14:56:26 +01005029static void fill_device_from_item(struct extent_buffer *leaf,
Chris Mason0b86a832008-03-24 15:01:56 -04005030 struct btrfs_dev_item *dev_item,
5031 struct btrfs_device *device)
5032{
5033 unsigned long ptr;
Chris Mason0b86a832008-03-24 15:01:56 -04005034
5035 device->devid = btrfs_device_id(leaf, dev_item);
Chris Balld6397ba2009-04-27 07:29:03 -04005036 device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
5037 device->total_bytes = device->disk_total_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -04005038 device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
5039 device->type = btrfs_device_type(leaf, dev_item);
5040 device->io_align = btrfs_device_io_align(leaf, dev_item);
5041 device->io_width = btrfs_device_io_width(leaf, dev_item);
5042 device->sector_size = btrfs_device_sector_size(leaf, dev_item);
Stefan Behrens8dabb742012-11-06 13:15:27 +01005043 WARN_ON(device->devid == BTRFS_DEV_REPLACE_DEVID);
Stefan Behrens63a212a2012-11-05 18:29:28 +01005044 device->is_tgtdev_for_dev_replace = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005045
5046 ptr = (unsigned long)btrfs_device_uuid(dev_item);
Chris Masone17cade2008-04-15 15:41:47 -04005047 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005048}
5049
Yan Zheng2b820322008-11-17 21:11:30 -05005050static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
5051{
5052 struct btrfs_fs_devices *fs_devices;
5053 int ret;
5054
Li Zefanb367e472011-12-07 11:38:24 +08005055 BUG_ON(!mutex_is_locked(&uuid_mutex));
Yan Zheng2b820322008-11-17 21:11:30 -05005056
5057 fs_devices = root->fs_info->fs_devices->seed;
5058 while (fs_devices) {
5059 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
5060 ret = 0;
5061 goto out;
5062 }
5063 fs_devices = fs_devices->seed;
5064 }
5065
5066 fs_devices = find_fsid(fsid);
5067 if (!fs_devices) {
5068 ret = -ENOENT;
5069 goto out;
5070 }
Yan Zhenge4404d62008-12-12 10:03:26 -05005071
5072 fs_devices = clone_fs_devices(fs_devices);
5073 if (IS_ERR(fs_devices)) {
5074 ret = PTR_ERR(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005075 goto out;
5076 }
5077
Christoph Hellwig97288f22008-12-02 06:36:09 -05005078 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
Chris Mason15916de2008-11-19 21:17:22 -05005079 root->fs_info->bdev_holder);
Julia Lawall48d28232012-04-14 11:24:33 +02005080 if (ret) {
5081 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005082 goto out;
Julia Lawall48d28232012-04-14 11:24:33 +02005083 }
Yan Zheng2b820322008-11-17 21:11:30 -05005084
5085 if (!fs_devices->seeding) {
5086 __btrfs_close_devices(fs_devices);
Yan Zhenge4404d62008-12-12 10:03:26 -05005087 free_fs_devices(fs_devices);
Yan Zheng2b820322008-11-17 21:11:30 -05005088 ret = -EINVAL;
5089 goto out;
5090 }
5091
5092 fs_devices->seed = root->fs_info->fs_devices->seed;
5093 root->fs_info->fs_devices->seed = fs_devices;
Yan Zheng2b820322008-11-17 21:11:30 -05005094out:
Yan Zheng2b820322008-11-17 21:11:30 -05005095 return ret;
5096}
5097
Chris Mason0d81ba52008-03-24 15:02:07 -04005098static int read_one_dev(struct btrfs_root *root,
Chris Mason0b86a832008-03-24 15:01:56 -04005099 struct extent_buffer *leaf,
5100 struct btrfs_dev_item *dev_item)
5101{
5102 struct btrfs_device *device;
5103 u64 devid;
5104 int ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005105 u8 fs_uuid[BTRFS_UUID_SIZE];
Chris Masona4437552008-04-18 10:29:38 -04005106 u8 dev_uuid[BTRFS_UUID_SIZE];
5107
Chris Mason0b86a832008-03-24 15:01:56 -04005108 devid = btrfs_device_id(leaf, dev_item);
Chris Masona4437552008-04-18 10:29:38 -04005109 read_extent_buffer(leaf, dev_uuid,
5110 (unsigned long)btrfs_device_uuid(dev_item),
5111 BTRFS_UUID_SIZE);
Yan Zheng2b820322008-11-17 21:11:30 -05005112 read_extent_buffer(leaf, fs_uuid,
5113 (unsigned long)btrfs_device_fsid(dev_item),
5114 BTRFS_UUID_SIZE);
5115
5116 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
5117 ret = open_seed_devices(root, fs_uuid);
Yan Zhenge4404d62008-12-12 10:03:26 -05005118 if (ret && !btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005119 return ret;
Yan Zheng2b820322008-11-17 21:11:30 -05005120 }
5121
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005122 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05005123 if (!device || !device->bdev) {
Yan Zhenge4404d62008-12-12 10:03:26 -05005124 if (!btrfs_test_opt(root, DEGRADED))
Yan Zheng2b820322008-11-17 21:11:30 -05005125 return -EIO;
5126
5127 if (!device) {
Chris Masond3977122009-01-05 21:25:51 -05005128 printk(KERN_WARNING "warning devid %llu missing\n",
5129 (unsigned long long)devid);
Yan Zheng2b820322008-11-17 21:11:30 -05005130 device = add_missing_dev(root, devid, dev_uuid);
5131 if (!device)
5132 return -ENOMEM;
Chris Masoncd02dca2010-12-13 14:56:23 -05005133 } else if (!device->missing) {
5134 /*
5135 * this happens when a device that was properly setup
5136 * in the device info lists suddenly goes bad.
5137 * device->bdev is NULL, and so we have to set
5138 * device->missing to one here
5139 */
5140 root->fs_info->fs_devices->missing_devices++;
5141 device->missing = 1;
Yan Zheng2b820322008-11-17 21:11:30 -05005142 }
5143 }
5144
5145 if (device->fs_devices != root->fs_info->fs_devices) {
5146 BUG_ON(device->writeable);
5147 if (device->generation !=
5148 btrfs_device_generation(leaf, dev_item))
5149 return -EINVAL;
Chris Mason6324fbf2008-03-24 15:01:59 -04005150 }
Chris Mason0b86a832008-03-24 15:01:56 -04005151
5152 fill_device_from_item(leaf, dev_item, device);
5153 device->dev_root = root->fs_info->dev_root;
Chris Masondfe25022008-05-13 13:46:40 -04005154 device->in_fs_metadata = 1;
Stefan Behrens63a212a2012-11-05 18:29:28 +01005155 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
Yan Zheng2b820322008-11-17 21:11:30 -05005156 device->fs_devices->total_rw_bytes += device->total_bytes;
Josef Bacik2bf64752011-09-26 17:12:22 -04005157 spin_lock(&root->fs_info->free_chunk_lock);
5158 root->fs_info->free_chunk_space += device->total_bytes -
5159 device->bytes_used;
5160 spin_unlock(&root->fs_info->free_chunk_lock);
5161 }
Chris Mason0b86a832008-03-24 15:01:56 -04005162 ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005163 return ret;
5164}
5165
Yan Zhenge4404d62008-12-12 10:03:26 -05005166int btrfs_read_sys_array(struct btrfs_root *root)
Chris Mason0b86a832008-03-24 15:01:56 -04005167{
David Sterba6c417612011-04-13 15:41:04 +02005168 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
Chris Masona061fc82008-05-07 11:43:44 -04005169 struct extent_buffer *sb;
Chris Mason0b86a832008-03-24 15:01:56 -04005170 struct btrfs_disk_key *disk_key;
Chris Mason0b86a832008-03-24 15:01:56 -04005171 struct btrfs_chunk *chunk;
Chris Mason84eed902008-04-25 09:04:37 -04005172 u8 *ptr;
5173 unsigned long sb_ptr;
5174 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005175 u32 num_stripes;
5176 u32 array_size;
5177 u32 len = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005178 u32 cur;
Chris Mason84eed902008-04-25 09:04:37 -04005179 struct btrfs_key key;
Chris Mason0b86a832008-03-24 15:01:56 -04005180
Yan Zhenge4404d62008-12-12 10:03:26 -05005181 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
Chris Masona061fc82008-05-07 11:43:44 -04005182 BTRFS_SUPER_INFO_SIZE);
5183 if (!sb)
5184 return -ENOMEM;
5185 btrfs_set_buffer_uptodate(sb);
Chris Mason85d4e462011-07-26 16:11:19 -04005186 btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
David Sterba8a334422011-10-07 18:06:13 +02005187 /*
5188 * The sb extent buffer is artifical and just used to read the system array.
5189 * btrfs_set_buffer_uptodate() call does not properly mark all it's
5190 * pages up-to-date when the page is larger: extent does not cover the
5191 * whole page and consequently check_page_uptodate does not find all
5192 * the page's extents up-to-date (the hole beyond sb),
5193 * write_extent_buffer then triggers a WARN_ON.
5194 *
5195 * Regular short extents go through mark_extent_buffer_dirty/writeback cycle,
5196 * but sb spans only this function. Add an explicit SetPageUptodate call
5197 * to silence the warning eg. on PowerPC 64.
5198 */
5199 if (PAGE_CACHE_SIZE > BTRFS_SUPER_INFO_SIZE)
Chris Mason727011e2010-08-06 13:21:20 -04005200 SetPageUptodate(sb->pages[0]);
Chris Mason4008c042009-02-12 14:09:45 -05005201
Chris Masona061fc82008-05-07 11:43:44 -04005202 write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
Chris Mason0b86a832008-03-24 15:01:56 -04005203 array_size = btrfs_super_sys_array_size(super_copy);
5204
Chris Mason0b86a832008-03-24 15:01:56 -04005205 ptr = super_copy->sys_chunk_array;
5206 sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
5207 cur = 0;
5208
5209 while (cur < array_size) {
5210 disk_key = (struct btrfs_disk_key *)ptr;
5211 btrfs_disk_key_to_cpu(&key, disk_key);
5212
Chris Masona061fc82008-05-07 11:43:44 -04005213 len = sizeof(*disk_key); ptr += len;
Chris Mason0b86a832008-03-24 15:01:56 -04005214 sb_ptr += len;
5215 cur += len;
5216
Chris Mason0d81ba52008-03-24 15:02:07 -04005217 if (key.type == BTRFS_CHUNK_ITEM_KEY) {
Chris Mason0b86a832008-03-24 15:01:56 -04005218 chunk = (struct btrfs_chunk *)sb_ptr;
Chris Mason0d81ba52008-03-24 15:02:07 -04005219 ret = read_one_chunk(root, &key, sb, chunk);
Chris Mason84eed902008-04-25 09:04:37 -04005220 if (ret)
5221 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005222 num_stripes = btrfs_chunk_num_stripes(sb, chunk);
5223 len = btrfs_chunk_item_size(num_stripes);
5224 } else {
Chris Mason84eed902008-04-25 09:04:37 -04005225 ret = -EIO;
5226 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005227 }
5228 ptr += len;
5229 sb_ptr += len;
5230 cur += len;
5231 }
Chris Masona061fc82008-05-07 11:43:44 -04005232 free_extent_buffer(sb);
Chris Mason84eed902008-04-25 09:04:37 -04005233 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -04005234}
5235
5236int btrfs_read_chunk_tree(struct btrfs_root *root)
5237{
5238 struct btrfs_path *path;
5239 struct extent_buffer *leaf;
5240 struct btrfs_key key;
5241 struct btrfs_key found_key;
5242 int ret;
5243 int slot;
5244
5245 root = root->fs_info->chunk_root;
5246
5247 path = btrfs_alloc_path();
5248 if (!path)
5249 return -ENOMEM;
5250
Li Zefanb367e472011-12-07 11:38:24 +08005251 mutex_lock(&uuid_mutex);
5252 lock_chunks(root);
5253
Chris Mason0b86a832008-03-24 15:01:56 -04005254 /* first we search for all of the device items, and then we
5255 * read in all of the chunk items. This way we can create chunk
5256 * mappings that reference all of the devices that are afound
5257 */
5258 key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
5259 key.offset = 0;
5260 key.type = 0;
5261again:
5262 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Zhao Leiab593812010-03-25 12:34:49 +00005263 if (ret < 0)
5264 goto error;
Chris Masond3977122009-01-05 21:25:51 -05005265 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005266 leaf = path->nodes[0];
5267 slot = path->slots[0];
5268 if (slot >= btrfs_header_nritems(leaf)) {
5269 ret = btrfs_next_leaf(root, path);
5270 if (ret == 0)
5271 continue;
5272 if (ret < 0)
5273 goto error;
5274 break;
5275 }
5276 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5277 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5278 if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
5279 break;
5280 if (found_key.type == BTRFS_DEV_ITEM_KEY) {
5281 struct btrfs_dev_item *dev_item;
5282 dev_item = btrfs_item_ptr(leaf, slot,
5283 struct btrfs_dev_item);
Chris Mason0d81ba52008-03-24 15:02:07 -04005284 ret = read_one_dev(root, leaf, dev_item);
Yan Zheng2b820322008-11-17 21:11:30 -05005285 if (ret)
5286 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005287 }
5288 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
5289 struct btrfs_chunk *chunk;
5290 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
5291 ret = read_one_chunk(root, &found_key, leaf, chunk);
Yan Zheng2b820322008-11-17 21:11:30 -05005292 if (ret)
5293 goto error;
Chris Mason0b86a832008-03-24 15:01:56 -04005294 }
5295 path->slots[0]++;
5296 }
5297 if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
5298 key.objectid = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005299 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005300 goto again;
5301 }
Chris Mason0b86a832008-03-24 15:01:56 -04005302 ret = 0;
5303error:
Li Zefanb367e472011-12-07 11:38:24 +08005304 unlock_chunks(root);
5305 mutex_unlock(&uuid_mutex);
5306
Yan Zheng2b820322008-11-17 21:11:30 -05005307 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005308 return ret;
5309}
Stefan Behrens442a4f62012-05-25 16:06:08 +02005310
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005311static void __btrfs_reset_dev_stats(struct btrfs_device *dev)
5312{
5313 int i;
5314
5315 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5316 btrfs_dev_stat_reset(dev, i);
5317}
5318
5319int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info)
5320{
5321 struct btrfs_key key;
5322 struct btrfs_key found_key;
5323 struct btrfs_root *dev_root = fs_info->dev_root;
5324 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5325 struct extent_buffer *eb;
5326 int slot;
5327 int ret = 0;
5328 struct btrfs_device *device;
5329 struct btrfs_path *path = NULL;
5330 int i;
5331
5332 path = btrfs_alloc_path();
5333 if (!path) {
5334 ret = -ENOMEM;
5335 goto out;
5336 }
5337
5338 mutex_lock(&fs_devices->device_list_mutex);
5339 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5340 int item_size;
5341 struct btrfs_dev_stats_item *ptr;
5342
5343 key.objectid = 0;
5344 key.type = BTRFS_DEV_STATS_KEY;
5345 key.offset = device->devid;
5346 ret = btrfs_search_slot(NULL, dev_root, &key, path, 0, 0);
5347 if (ret) {
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005348 __btrfs_reset_dev_stats(device);
5349 device->dev_stats_valid = 1;
5350 btrfs_release_path(path);
5351 continue;
5352 }
5353 slot = path->slots[0];
5354 eb = path->nodes[0];
5355 btrfs_item_key_to_cpu(eb, &found_key, slot);
5356 item_size = btrfs_item_size_nr(eb, slot);
5357
5358 ptr = btrfs_item_ptr(eb, slot,
5359 struct btrfs_dev_stats_item);
5360
5361 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5362 if (item_size >= (1 + i) * sizeof(__le64))
5363 btrfs_dev_stat_set(device, i,
5364 btrfs_dev_stats_value(eb, ptr, i));
5365 else
5366 btrfs_dev_stat_reset(device, i);
5367 }
5368
5369 device->dev_stats_valid = 1;
5370 btrfs_dev_stat_print_on_load(device);
5371 btrfs_release_path(path);
5372 }
5373 mutex_unlock(&fs_devices->device_list_mutex);
5374
5375out:
5376 btrfs_free_path(path);
5377 return ret < 0 ? ret : 0;
5378}
5379
5380static int update_dev_stat_item(struct btrfs_trans_handle *trans,
5381 struct btrfs_root *dev_root,
5382 struct btrfs_device *device)
5383{
5384 struct btrfs_path *path;
5385 struct btrfs_key key;
5386 struct extent_buffer *eb;
5387 struct btrfs_dev_stats_item *ptr;
5388 int ret;
5389 int i;
5390
5391 key.objectid = 0;
5392 key.type = BTRFS_DEV_STATS_KEY;
5393 key.offset = device->devid;
5394
5395 path = btrfs_alloc_path();
5396 BUG_ON(!path);
5397 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
5398 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005399 printk_in_rcu(KERN_WARNING "btrfs: error %d while searching for dev_stats item for device %s!\n",
5400 ret, rcu_str_deref(device->name));
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005401 goto out;
5402 }
5403
5404 if (ret == 0 &&
5405 btrfs_item_size_nr(path->nodes[0], path->slots[0]) < sizeof(*ptr)) {
5406 /* need to delete old one and insert a new one */
5407 ret = btrfs_del_item(trans, dev_root, path);
5408 if (ret != 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005409 printk_in_rcu(KERN_WARNING "btrfs: delete too small dev_stats item for device %s failed %d!\n",
5410 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005411 goto out;
5412 }
5413 ret = 1;
5414 }
5415
5416 if (ret == 1) {
5417 /* need to insert a new item */
5418 btrfs_release_path(path);
5419 ret = btrfs_insert_empty_item(trans, dev_root, path,
5420 &key, sizeof(*ptr));
5421 if (ret < 0) {
Josef Bacik606686e2012-06-04 14:03:51 -04005422 printk_in_rcu(KERN_WARNING "btrfs: insert dev_stats item for device %s failed %d!\n",
5423 rcu_str_deref(device->name), ret);
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005424 goto out;
5425 }
5426 }
5427
5428 eb = path->nodes[0];
5429 ptr = btrfs_item_ptr(eb, path->slots[0], struct btrfs_dev_stats_item);
5430 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5431 btrfs_set_dev_stats_value(eb, ptr, i,
5432 btrfs_dev_stat_read(device, i));
5433 btrfs_mark_buffer_dirty(eb);
5434
5435out:
5436 btrfs_free_path(path);
5437 return ret;
5438}
5439
5440/*
5441 * called from commit_transaction. Writes all changed device stats to disk.
5442 */
5443int btrfs_run_dev_stats(struct btrfs_trans_handle *trans,
5444 struct btrfs_fs_info *fs_info)
5445{
5446 struct btrfs_root *dev_root = fs_info->dev_root;
5447 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
5448 struct btrfs_device *device;
5449 int ret = 0;
5450
5451 mutex_lock(&fs_devices->device_list_mutex);
5452 list_for_each_entry(device, &fs_devices->devices, dev_list) {
5453 if (!device->dev_stats_valid || !device->dev_stats_dirty)
5454 continue;
5455
5456 ret = update_dev_stat_item(trans, dev_root, device);
5457 if (!ret)
5458 device->dev_stats_dirty = 0;
5459 }
5460 mutex_unlock(&fs_devices->device_list_mutex);
5461
5462 return ret;
5463}
5464
Stefan Behrens442a4f62012-05-25 16:06:08 +02005465void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
5466{
5467 btrfs_dev_stat_inc(dev, index);
5468 btrfs_dev_stat_print_on_error(dev);
5469}
5470
5471void btrfs_dev_stat_print_on_error(struct btrfs_device *dev)
5472{
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005473 if (!dev->dev_stats_valid)
5474 return;
Josef Bacik606686e2012-06-04 14:03:51 -04005475 printk_ratelimited_in_rcu(KERN_ERR
Stefan Behrens442a4f62012-05-25 16:06:08 +02005476 "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
Josef Bacik606686e2012-06-04 14:03:51 -04005477 rcu_str_deref(dev->name),
Stefan Behrens442a4f62012-05-25 16:06:08 +02005478 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5479 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5480 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5481 btrfs_dev_stat_read(dev,
5482 BTRFS_DEV_STAT_CORRUPTION_ERRS),
5483 btrfs_dev_stat_read(dev,
5484 BTRFS_DEV_STAT_GENERATION_ERRS));
5485}
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005486
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005487static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
5488{
Stefan Behrensa98cdb82012-07-17 09:02:11 -06005489 int i;
5490
5491 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5492 if (btrfs_dev_stat_read(dev, i) != 0)
5493 break;
5494 if (i == BTRFS_DEV_STAT_VALUES_MAX)
5495 return; /* all values == 0, suppress message */
5496
Josef Bacik606686e2012-06-04 14:03:51 -04005497 printk_in_rcu(KERN_INFO "btrfs: bdev %s errs: wr %u, rd %u, flush %u, corrupt %u, gen %u\n",
5498 rcu_str_deref(dev->name),
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005499 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_WRITE_ERRS),
5500 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_READ_ERRS),
5501 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_FLUSH_ERRS),
5502 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_CORRUPTION_ERRS),
5503 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
5504}
5505
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005506int btrfs_get_dev_stats(struct btrfs_root *root,
David Sterbab27f7c02012-06-22 06:30:39 -06005507 struct btrfs_ioctl_get_dev_stats *stats)
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005508{
5509 struct btrfs_device *dev;
5510 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
5511 int i;
5512
5513 mutex_lock(&fs_devices->device_list_mutex);
Stefan Behrensaa1b8cd2012-11-05 17:03:39 +01005514 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL);
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005515 mutex_unlock(&fs_devices->device_list_mutex);
5516
5517 if (!dev) {
5518 printk(KERN_WARNING
5519 "btrfs: get dev_stats failed, device not found\n");
5520 return -ENODEV;
Stefan Behrens733f4fb2012-05-25 16:06:10 +02005521 } else if (!dev->dev_stats_valid) {
5522 printk(KERN_WARNING
5523 "btrfs: get dev_stats failed, not yet valid\n");
5524 return -ENODEV;
David Sterbab27f7c02012-06-22 06:30:39 -06005525 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
Stefan Behrensc11d2c22012-05-25 16:06:09 +02005526 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
5527 if (stats->nr_items > i)
5528 stats->values[i] =
5529 btrfs_dev_stat_read_and_reset(dev, i);
5530 else
5531 btrfs_dev_stat_reset(dev, i);
5532 }
5533 } else {
5534 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++)
5535 if (stats->nr_items > i)
5536 stats->values[i] = btrfs_dev_stat_read(dev, i);
5537 }
5538 if (stats->nr_items > BTRFS_DEV_STAT_VALUES_MAX)
5539 stats->nr_items = BTRFS_DEV_STAT_VALUES_MAX;
5540 return 0;
5541}
Stefan Behrensa8a6dab2012-11-05 15:50:14 +01005542
5543int btrfs_scratch_superblock(struct btrfs_device *device)
5544{
5545 struct buffer_head *bh;
5546 struct btrfs_super_block *disk_super;
5547
5548 bh = btrfs_read_dev_super(device->bdev);
5549 if (!bh)
5550 return -EINVAL;
5551 disk_super = (struct btrfs_super_block *)bh->b_data;
5552
5553 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
5554 set_buffer_dirty(bh);
5555 sync_dirty_buffer(bh);
5556 brelse(bh);
5557
5558 return 0;
5559}